xref: /freebsd/contrib/nvi/vi/v_init.c (revision 4232f826683298e85d469c0ef17259cecd13b2c7)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	Keith Bostic.  All rights reserved.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #ifndef lint
13 static const char sccsid[] = "$Id: v_init.c,v 10.10 2012/02/11 00:33:46 zy Exp $";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
19 
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "../common/common.h"
28 #include "vi.h"
29 
30 /*
31  * v_screen_copy --
32  *	Copy vi screen.
33  *
34  * PUBLIC: int v_screen_copy(SCR *, SCR *);
35  */
36 int
37 v_screen_copy(SCR *orig, SCR *sp)
38 {
39 	VI_PRIVATE *ovip, *nvip;
40 
41 	/* Create the private vi structure. */
42 	CALLOC_RET(orig, nvip, VI_PRIVATE *, 1, sizeof(VI_PRIVATE));
43 	sp->vi_private = nvip;
44 
45 	/* Invalidate the line size cache. */
46 	VI_SCR_CFLUSH(nvip);
47 
48 	if (orig == NULL) {
49 		nvip->csearchdir = CNOTSET;
50 	} else {
51 		ovip = VIP(orig);
52 
53 		/* User can replay the last input, but nothing else. */
54 		if (ovip->rep_len != 0) {
55 			MALLOC_RET(orig, nvip->rep, EVENT *, ovip->rep_len);
56 			memmove(nvip->rep, ovip->rep, ovip->rep_len);
57 			nvip->rep_len = ovip->rep_len;
58 		}
59 
60 		/* Copy the match characters information. */
61 		if (ovip->mcs != NULL && (nvip->mcs =
62 		    v_wstrdup(sp, ovip->mcs, STRLEN(ovip->mcs))) == NULL)
63 			return (1);
64 
65 		/* Copy the paragraph/section information. */
66 		if (ovip->ps != NULL && (nvip->ps =
67 		    v_strdup(sp, ovip->ps, strlen(ovip->ps))) == NULL)
68 			return (1);
69 
70 		nvip->lastckey = ovip->lastckey;
71 		nvip->csearchdir = ovip->csearchdir;
72 
73 		nvip->srows = ovip->srows;
74 	}
75 	return (0);
76 }
77 
78 /*
79  * v_screen_end --
80  *	End a vi screen.
81  *
82  * PUBLIC: int v_screen_end(SCR *);
83  */
84 int
85 v_screen_end(SCR *sp)
86 {
87 	VI_PRIVATE *vip;
88 
89 	if ((vip = VIP(sp)) == NULL)
90 		return (0);
91 	if (vip->keyw != NULL)
92 		free(vip->keyw);
93 	if (vip->rep != NULL)
94 		free(vip->rep);
95 	if (vip->mcs != NULL)
96 		free(vip->mcs);
97 	if (vip->ps != NULL)
98 		free(vip->ps);
99 
100 	if (HMAP != NULL)
101 		free(HMAP);
102 
103 	free(vip);
104 	sp->vi_private = NULL;
105 
106 	return (0);
107 }
108 
109 /*
110  * v_optchange --
111  *	Handle change of options for vi.
112  *
113  * PUBLIC: int v_optchange(SCR *, int, char *, u_long *);
114  */
115 int
116 v_optchange(SCR *sp, int offset, char *str, u_long *valp)
117 {
118 	switch (offset) {
119 	case O_MATCHCHARS:
120 		return (v_buildmcs(sp, str));
121 	case O_PARAGRAPHS:
122 		return (v_buildps(sp, str, O_STR(sp, O_SECTIONS)));
123 	case O_SECTIONS:
124 		return (v_buildps(sp, O_STR(sp, O_PARAGRAPHS), str));
125 	case O_WINDOW:
126 		return (vs_crel(sp, *valp));
127 	}
128 	return (0);
129 }
130