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