xref: /openbsd-src/usr.bin/vi/vi/v_init.c (revision a0242ada35aa0eb3c776d837b98713a1921828a1)
1 /*	$OpenBSD: v_init.c,v 1.8 2017/04/18 01:45:35 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(SCR * orig,SCR * sp)35 v_screen_copy(SCR *orig, SCR *sp)
36 {
37 	VI_PRIVATE *ovip, *nvip;
38 
39 	/* Create the private vi structure. */
40 	CALLOC_RET(orig, nvip, 1, sizeof(VI_PRIVATE));
41 	sp->vi_private = nvip;
42 
43 	/* Invalidate the line size cache. */
44 	VI_SCR_CFLUSH(nvip);
45 
46 	if (orig == NULL) {
47 		nvip->csearchdir = CNOTSET;
48 	} else {
49 		ovip = VIP(orig);
50 
51 		/* User can replay the last input, but nothing else. */
52 		if (ovip->rep_len != 0) {
53 			MALLOC_RET(orig, nvip->rep, ovip->rep_len);
54 			memmove(nvip->rep, ovip->rep, ovip->rep_len);
55 			nvip->rep_len = ovip->rep_len;
56 		}
57 
58 		/* Copy the paragraph/section information. */
59 		if (ovip->ps != NULL && (nvip->ps =
60 		    v_strdup(sp, ovip->ps, strlen(ovip->ps))) == NULL)
61 			return (1);
62 
63 		nvip->lastckey = ovip->lastckey;
64 		nvip->csearchdir = ovip->csearchdir;
65 
66 		nvip->srows = ovip->srows;
67 	}
68 	return (0);
69 }
70 
71 /*
72  * v_screen_end --
73  *	End a vi screen.
74  *
75  * PUBLIC: int v_screen_end(SCR *);
76  */
77 int
v_screen_end(SCR * sp)78 v_screen_end(SCR *sp)
79 {
80 	VI_PRIVATE *vip;
81 
82 	if ((vip = VIP(sp)) == NULL)
83 		return (0);
84 	free(vip->keyw);
85 	free(vip->rep);
86 	free(vip->ps);
87 	free(HMAP);
88 	free(vip);
89 	sp->vi_private = NULL;
90 
91 	return (0);
92 }
93 
94 /*
95  * v_optchange --
96  *	Handle change of options for vi.
97  *
98  * PUBLIC: int v_optchange(SCR *, int, char *, u_long *);
99  */
100 int
v_optchange(SCR * sp,int offset,char * str,u_long * valp)101 v_optchange(SCR *sp, int offset, char *str, u_long *valp)
102 {
103 	switch (offset) {
104 	case O_PARAGRAPHS:
105 		return (v_buildps(sp, str, O_STR(sp, O_SECTIONS)));
106 	case O_SECTIONS:
107 		return (v_buildps(sp, O_STR(sp, O_PARAGRAPHS), str));
108 	case O_WINDOW:
109 		return (vs_crel(sp, *valp));
110 	}
111 	return (0);
112 }
113