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