xref: /openbsd/usr.bin/vi/ex/ex_init.c (revision df930be7)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char sccsid[] = "@(#)ex_init.c	8.18 (Berkeley) 8/17/94";
36 #endif /* not lint */
37 
38 #include <sys/types.h>
39 #include <sys/queue.h>
40 #include <sys/time.h>
41 
42 #include <bitstring.h>
43 #include <errno.h>
44 #include <limits.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <termios.h>
50 
51 #include "compat.h"
52 #include <db.h>
53 #include <regex.h>
54 
55 #include "vi.h"
56 #include "excmd.h"
57 #include "tag.h"
58 
59 /*
60  * ex_screen_copy --
61  *	Copy ex screen.
62  */
63 int
64 ex_screen_copy(orig, sp)
65 	SCR *orig, *sp;
66 {
67 	EX_PRIVATE *oexp, *nexp;
68 
69 	/* Create the private ex structure. */
70 	CALLOC_RET(orig, nexp, EX_PRIVATE *, 1, sizeof(EX_PRIVATE));
71 	sp->ex_private = nexp;
72 
73 	/* Initialize queues. */
74 	TAILQ_INIT(&nexp->tagq);
75 	TAILQ_INIT(&nexp->tagfq);
76 	TAILQ_INIT(&nexp->cdq);
77 	CIRCLEQ_INIT(&nexp->rangeq);
78 
79 	if (orig == NULL) {
80 		nexp->at_lbuf_set = 0;
81 	} else {
82 		oexp = EXP(orig);
83 
84 		nexp->at_lbuf = oexp->at_lbuf;
85 		nexp->at_lbuf_set = oexp->at_lbuf_set;
86 
87 		if (oexp->lastbcomm != NULL &&
88 		    (nexp->lastbcomm = strdup(oexp->lastbcomm)) == NULL) {
89 			msgq(sp, M_SYSERR, NULL);
90 			return(1);
91 		}
92 
93 		if (ex_tagcopy(orig, sp))
94 			return (1);
95 	}
96 	return (0);
97 }
98 
99 /*
100  * ex_screen_end --
101  *	End a vi screen.
102  */
103 int
104 ex_screen_end(sp)
105 	SCR *sp;
106 {
107 	EX_PRIVATE *exp;
108 	int rval;
109 
110 	rval = 0;
111 	exp = EXP(sp);
112 
113 	if (argv_free(sp))
114 		rval = 1;
115 
116 	if (exp->ibp != NULL)
117 		FREE(exp->ibp, exp->ibp_len);
118 
119 	if (exp->lastbcomm != NULL)
120 		FREE(exp->lastbcomm, strlen(exp->lastbcomm) + 1);
121 
122 	if (ex_tagfree(sp))
123 		rval = 1;
124 
125 	if (ex_cdfree(sp))
126 		rval = 1;
127 
128 	/* Free private memory. */
129 	FREE(exp, sizeof(EX_PRIVATE));
130 	sp->ex_private = NULL;
131 
132 	return (rval);
133 }
134 
135 /*
136  * ex_init --
137  *	Initialize ex.
138  */
139 int
140 ex_init(sp, ep)
141 	SCR *sp;
142 	EXF *ep;
143 {
144 	size_t len;
145 
146 	/*
147 	 * The default address is the last line of the file.  If the address
148 	 * set bit is on for this file, load the address, ensuring that it
149 	 * exists.
150 	 */
151 	if (F_ISSET(sp->frp, FR_CURSORSET)) {
152 		sp->lno = sp->frp->lno;
153 		sp->cno = sp->frp->cno;
154 
155 		if (file_gline(sp, ep, sp->lno, &len) == NULL) {
156 			if (file_lline(sp, ep, &sp->lno))
157 				return (1);
158 			if (sp->lno == 0)
159 				sp->lno = 1;
160 			sp->cno = 0;
161 		} else if (sp->cno >= len)
162 			sp->cno = 0;
163 	} else {
164 		if (file_lline(sp, ep, &sp->lno))
165 			return (1);
166 		if (sp->lno == 0)
167 			sp->lno = 1;
168 		sp->cno = 0;
169 	}
170 
171 	/* Display the status line. */
172 	return (msg_status(sp, ep, sp->lno, 0));
173 }
174 
175 /*
176  * ex_end --
177  *	End ex session.
178  */
179 int
180 ex_end(sp)
181 	SCR *sp;
182 {
183 	return (0);
184 }
185 
186 /*
187  * ex_optchange --
188  *	Handle change of options for vi.
189  */
190 int
191 ex_optchange(sp, opt)
192 	SCR *sp;
193 	int opt;
194 {
195 	switch (opt) {
196 	case O_CDPATH:
197 		return (ex_cdalloc(sp, O_STR(sp, O_CDPATH)));
198 	case O_TAGS:
199 		return (ex_tagalloc(sp, O_STR(sp, O_TAGS)));
200 	}
201 	return (0);
202 }
203