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