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