xref: /openbsd-src/usr.bin/vi/common/screen.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: screen.c,v 1.10 2013/11/28 22:12:40 krw Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 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 #include <unistd.h>
25 
26 #include "common.h"
27 #include "../vi/vi.h"
28 
29 /*
30  * screen_init --
31  *	Do the default initialization of an SCR structure.
32  *
33  * PUBLIC: int screen_init(GS *, SCR *, SCR **);
34  */
35 int
36 screen_init(gp, orig, spp)
37 	GS *gp;
38 	SCR *orig, **spp;
39 {
40 	SCR *sp;
41 	size_t len;
42 
43 	*spp = NULL;
44 	CALLOC_RET(orig, sp, SCR *, 1, sizeof(SCR));
45 	*spp = sp;
46 
47 /* INITIALIZED AT SCREEN CREATE. */
48 	sp->id = ++gp->id;
49 	sp->refcnt = 1;
50 
51 	sp->gp = gp;				/* All ref the GS structure. */
52 
53 	sp->ccnt = 2;				/* Anything > 1 */
54 
55 	/*
56 	 * XXX
57 	 * sp->defscroll is initialized by the opts_init() code because
58 	 * we don't have the option information yet.
59 	 */
60 
61 	TAILQ_INIT(&sp->tiq);
62 
63 /* PARTIALLY OR COMPLETELY COPIED FROM PREVIOUS SCREEN. */
64 	if (orig == NULL) {
65 		sp->searchdir = NOTSET;
66 	} else {
67 		/* Alternate file name. */
68 		if (orig->alt_name != NULL &&
69 		    (sp->alt_name = strdup(orig->alt_name)) == NULL)
70 			goto mem;
71 
72 		/* Last executed at buffer. */
73 		if (F_ISSET(orig, SC_AT_SET)) {
74 			F_SET(sp, SC_AT_SET);
75 			sp->at_lbuf = orig->at_lbuf;
76 		}
77 
78 		/* Retain searching/substitution information. */
79 		sp->searchdir = orig->searchdir == NOTSET ? NOTSET : FORWARD;
80 		if (orig->re != NULL && (sp->re =
81 		    v_strdup(sp, orig->re, orig->re_len)) == NULL)
82 			goto mem;
83 		sp->re_len = orig->re_len;
84 		if (orig->subre != NULL && (sp->subre =
85 		    v_strdup(sp, orig->subre, orig->subre_len)) == NULL)
86 			goto mem;
87 		sp->subre_len = orig->subre_len;
88 		if (orig->repl != NULL && (sp->repl =
89 		    v_strdup(sp, orig->repl, orig->repl_len)) == NULL)
90 			goto mem;
91 		sp->repl_len = orig->repl_len;
92 		if (orig->newl_len) {
93 			len = orig->newl_len * sizeof(size_t);
94 			MALLOC(sp, sp->newl, size_t *, len);
95 			if (sp->newl == NULL) {
96 mem:				msgq(orig, M_SYSERR, NULL);
97 				goto err;
98 			}
99 			sp->newl_len = orig->newl_len;
100 			sp->newl_cnt = orig->newl_cnt;
101 			memcpy(sp->newl, orig->newl, len);
102 		}
103 
104 		if (opts_copy(orig, sp))
105 			goto err;
106 
107 		F_SET(sp, F_ISSET(orig, SC_EX | SC_VI));
108 	}
109 
110 	if (ex_screen_copy(orig, sp))		/* Ex. */
111 		goto err;
112 	if (v_screen_copy(orig, sp))		/* Vi. */
113 		goto err;
114 
115 	*spp = sp;
116 	return (0);
117 
118 err:	screen_end(sp);
119 	return (1);
120 }
121 
122 /*
123  * screen_end --
124  *	Release a screen, no matter what had (and had not) been
125  *	initialized.
126  *
127  * PUBLIC: int screen_end(SCR *);
128  */
129 int
130 screen_end(sp)
131 	SCR *sp;
132 {
133 	int rval;
134 	SCR *tsp;
135 
136 	/* If multiply referenced, just decrement the count and return. */
137 	 if (--sp->refcnt != 0)
138 		 return (0);
139 
140 	/*
141 	 * Remove the screen from the displayed and hidden queues.
142 	 *
143 	 * If a created screen failed during initialization, it may not
144 	 * be linked into a queue.
145 	 */
146 	TAILQ_FOREACH(tsp, &sp->gp->dq, q) {
147 		if (tsp == sp) {
148 			TAILQ_REMOVE(&sp->gp->dq, sp, q);
149 			break;
150 		}
151 	}
152 	TAILQ_FOREACH(tsp, &sp->gp->hq, q) {
153 		if (tsp == sp) {
154 			TAILQ_REMOVE(&sp->gp->hq, sp, q);
155 			break;
156 		}
157 	}
158 
159 	/* The screen is no longer real. */
160 	F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
161 
162 	rval = 0;
163 #ifdef HAVE_PERL_INTERP
164 	if (perl_screen_end(sp))		/* End perl. */
165 		rval = 1;
166 #endif
167 	if (v_screen_end(sp))			/* End vi. */
168 		rval = 1;
169 	if (ex_screen_end(sp))			/* End ex. */
170 		rval = 1;
171 
172 	/* Free file names. */
173 	{ char **ap;
174 		if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
175 			for (ap = sp->argv; *ap != NULL; ++ap)
176 				free(*ap);
177 			free(sp->argv);
178 		}
179 	}
180 
181 	/* Free any text input. */
182 	if (TAILQ_FIRST(&sp->tiq) != NULL)
183 		text_lfree(&sp->tiq);
184 
185 	/* Free alternate file name. */
186 	if (sp->alt_name != NULL)
187 		free(sp->alt_name);
188 
189 	/* Free up search information. */
190 	if (sp->re != NULL)
191 		free(sp->re);
192 	if (F_ISSET(sp, SC_RE_SEARCH))
193 		regfree(&sp->re_c);
194 	if (sp->subre != NULL)
195 		free(sp->subre);
196 	if (F_ISSET(sp, SC_RE_SUBST))
197 		regfree(&sp->subre_c);
198 	if (sp->repl != NULL)
199 		free(sp->repl);
200 	if (sp->newl != NULL)
201 		free(sp->newl);
202 
203 	/* Free all the options */
204 	opts_free(sp);
205 
206 	/* Free the screen itself. */
207 	free(sp);
208 
209 	return (rval);
210 }
211 
212 /*
213  * screen_next --
214  *	Return the next screen in the queue.
215  *
216  * PUBLIC: SCR *screen_next(SCR *);
217  */
218 SCR *
219 screen_next(sp)
220 	SCR *sp;
221 {
222 	GS *gp;
223 	SCR *next;
224 
225 	/* Try the display queue, without returning the current screen. */
226 	gp = sp->gp;
227 	TAILQ_FOREACH(next, &gp->dq, q)
228 		if (next != sp)
229 			return (next);
230 
231 	/* Try the hidden queue; if found, move screen to the display queue. */
232 	if (!TAILQ_EMPTY(&gp->hq)) {
233 		next = TAILQ_FIRST(&gp->hq);
234 		TAILQ_REMOVE(&gp->hq, next, q);
235 		TAILQ_INSERT_HEAD(&gp->dq, next, q);
236 		return (next);
237 	}
238 	return (NULL);
239 }
240