the script used to extract a release
[nvi.git] / common / screen.c
blobdfa78566a274c8adbd3e8528b52d82629690198c
1 /*-
2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: screen.c,v 10.20 2000/07/22 10:20:31 skimo Exp $ (Berkeley) $Date: 2000/07/22 10:20:31 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "common.h"
29 #include "../vi/vi.h"
31 CHAR_T RE_WSTART[] = {'[','[',':','<',':',']',']',0};
32 CHAR_T RE_WSTOP[] = {'[','[',':','>',':',']',']',0};
35 * screen_init --
36 * Do the default initialization of an SCR structure.
38 * PUBLIC: int screen_init __P((GS *, SCR *, SCR **));
40 int
41 screen_init(gp, orig, spp)
42 GS *gp;
43 SCR *orig, **spp;
45 SCR *sp;
46 size_t len;
48 *spp = NULL;
49 CALLOC_RET(orig, sp, SCR *, 1, sizeof(SCR));
50 *spp = sp;
52 /* INITIALIZED AT SCREEN CREATE. */
53 sp->id = ++gp->id;
54 sp->refcnt = 1;
56 sp->gp = gp; /* All ref the GS structure. */
58 sp->ccnt = 2; /* Anything > 1 */
61 * XXX
62 * sp->defscroll is initialized by the opts_init() code because
63 * we don't have the option information yet.
66 CIRCLEQ_INIT(&sp->tiq);
68 /* PARTIALLY OR COMPLETELY COPIED FROM PREVIOUS SCREEN. */
69 if (orig == NULL) {
70 sp->searchdir = NOTSET;
71 } else {
72 sp->wp = orig->wp;
74 /* Alternate file name. */
75 if (orig->alt_name != NULL &&
76 (sp->alt_name = strdup(orig->alt_name)) == NULL)
77 goto mem;
79 /* Last executed at buffer. */
80 if (F_ISSET(orig, SC_AT_SET)) {
81 F_SET(sp, SC_AT_SET);
82 sp->at_lbuf = orig->at_lbuf;
85 /* Retain searching/substitution information. */
86 sp->searchdir = orig->searchdir == NOTSET ? NOTSET : FORWARD;
87 if (orig->re != NULL && (sp->re =
88 v_wstrdup(sp, orig->re, orig->re_len)) == NULL)
89 goto mem;
90 sp->re_len = orig->re_len;
91 if (orig->subre != NULL && (sp->subre =
92 v_wstrdup(sp, orig->subre, orig->subre_len)) == NULL)
93 goto mem;
94 sp->subre_len = orig->subre_len;
95 if (orig->repl != NULL && (sp->repl =
96 v_wstrdup(sp, orig->repl, orig->repl_len)) == NULL)
97 goto mem;
98 sp->repl_len = orig->repl_len;
99 if (orig->newl_len) {
100 len = orig->newl_len * sizeof(size_t);
101 MALLOC(sp, sp->newl, size_t *, len);
102 if (sp->newl == NULL) {
103 mem: msgq(orig, M_SYSERR, NULL);
104 goto err;
106 sp->newl_len = orig->newl_len;
107 sp->newl_cnt = orig->newl_cnt;
108 memcpy(sp->newl, orig->newl, len);
111 if (opts_copy(orig, sp))
112 goto err;
114 F_SET(sp, F_ISSET(orig, SC_EX | SC_VI));
117 if (ex_screen_copy(orig, sp)) /* Ex. */
118 goto err;
119 if (v_screen_copy(orig, sp)) /* Vi. */
120 goto err;
121 sp->cl_private = 0; /* XXX */
122 conv_init(orig, sp); /* XXX */
124 *spp = sp;
125 return (0);
127 err: screen_end(sp);
128 return (1);
132 * screen_end --
133 * Release a screen, no matter what had (and had not) been
134 * initialized.
136 * PUBLIC: int screen_end __P((SCR *));
139 screen_end(sp)
140 SCR *sp;
142 int rval;
144 /* If multiply referenced, just decrement the count and return. */
145 if (--sp->refcnt != 0)
146 return (0);
149 * Remove the screen from the displayed queue.
151 * If a created screen failed during initialization, it may not
152 * be linked into the chain.
154 if (sp->q.cqe_next != NULL)
155 CIRCLEQ_REMOVE(&sp->wp->scrq, sp, q);
157 /* The screen is no longer real. */
158 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
160 rval = 0;
161 #ifdef HAVE_PERL_INTERP
162 if (perl_screen_end(sp)) /* End perl. */
163 rval = 1;
164 #endif
165 if (v_screen_end(sp)) /* End vi. */
166 rval = 1;
167 if (ex_screen_end(sp)) /* End ex. */
168 rval = 1;
170 /* Free file names. */
171 { char **ap;
172 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
173 for (ap = sp->argv; *ap != NULL; ++ap)
174 free(*ap);
175 free(sp->argv);
179 /* Free any text input. */
180 if (sp->tiq.cqh_first != NULL)
181 text_lfree(&sp->tiq);
183 /* Free alternate file name. */
184 if (sp->alt_name != NULL)
185 free(sp->alt_name);
187 /* Free up search information. */
188 if (sp->re != NULL)
189 free(sp->re);
190 if (F_ISSET(sp, SC_RE_SEARCH))
191 regfree(&sp->re_c);
192 if (sp->subre != NULL)
193 free(sp->subre);
194 if (F_ISSET(sp, SC_RE_SUBST))
195 regfree(&sp->subre_c);
196 if (sp->repl != NULL)
197 free(sp->repl);
198 if (sp->newl != NULL)
199 free(sp->newl);
201 /* Free all the options */
202 opts_free(sp);
204 /* Free the screen itself. */
205 free(sp);
207 return (rval);
211 * screen_next --
212 * Return the next screen in the queue.
214 * PUBLIC: SCR *screen_next __P((SCR *));
216 SCR *
217 screen_next(sp)
218 SCR *sp;
220 GS *gp;
221 WIN *wp;
222 SCR *next;
224 /* Try the display queue, without returning the current screen. */
225 gp = sp->gp;
226 wp = sp->wp;
227 for (next = wp->scrq.cqh_first;
228 next != (void *)&wp->scrq; next = next->q.cqe_next)
229 if (next != sp)
230 break;
231 if (next != (void *)&wp->scrq)
232 return (next);
234 /* Try the hidden queue; if found, move screen to the display queue. */
235 if (gp->hq.cqh_first != (void *)&gp->hq) {
236 next = gp->hq.cqh_first;
237 CIRCLEQ_REMOVE(&gp->hq, next, q);
238 CIRCLEQ_INSERT_HEAD(&wp->scrq, next, q);
239 next->wp = sp->wp;
240 return (next);
242 return (NULL);