remove generated compile, depcomp, missing and mkinstalldirs
[nvi.git] / common / screen.c
blobc4462016829295144b92ba5de47c8f12cb75ef1d
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.22 2001/06/25 15:19:12 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:12 $";
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"
32 * screen_init --
33 * Do the default initialization of an SCR structure.
35 * PUBLIC: int screen_init __P((GS *, SCR *, SCR **));
37 int
38 screen_init(GS *gp, SCR *orig, SCR **spp)
40 SCR *sp;
41 size_t len;
43 *spp = NULL;
44 CALLOC_RET(orig, sp, SCR *, 1, sizeof(SCR));
45 *spp = sp;
47 /* INITIALIZED AT SCREEN CREATE. */
48 sp->id = ++gp->id;
49 sp->refcnt = 1;
51 sp->gp = gp; /* All ref the GS structure. */
53 sp->ccnt = 2; /* Anything > 1 */
56 * XXX
57 * sp->defscroll is initialized by the opts_init() code because
58 * we don't have the option information yet.
61 CIRCLEQ_INIT(&sp->tiq);
63 /* PARTIALLY OR COMPLETELY COPIED FROM PREVIOUS SCREEN. */
64 if (orig == NULL) {
65 sp->searchdir = NOTSET;
66 } else {
67 sp->wp = orig->wp;
69 /* Alternate file name. */
70 if (orig->alt_name != NULL &&
71 (sp->alt_name = strdup(orig->alt_name)) == NULL)
72 goto mem;
74 /* Last executed at buffer. */
75 if (F_ISSET(orig, SC_AT_SET)) {
76 F_SET(sp, SC_AT_SET);
77 sp->at_lbuf = orig->at_lbuf;
80 /* Retain searching/substitution information. */
81 sp->searchdir = orig->searchdir == NOTSET ? NOTSET : FORWARD;
82 if (orig->re != NULL && (sp->re =
83 v_wstrdup(sp, orig->re, orig->re_len)) == NULL)
84 goto mem;
85 sp->re_len = orig->re_len;
86 if (orig->subre != NULL && (sp->subre =
87 v_wstrdup(sp, orig->subre, orig->subre_len)) == NULL)
88 goto mem;
89 sp->subre_len = orig->subre_len;
90 if (orig->repl != NULL && (sp->repl =
91 v_wstrdup(sp, orig->repl, orig->repl_len)) == NULL)
92 goto mem;
93 sp->repl_len = orig->repl_len;
94 if (orig->newl_len) {
95 len = orig->newl_len * sizeof(size_t);
96 MALLOC(sp, sp->newl, size_t *, len);
97 if (sp->newl == NULL) {
98 mem: msgq(orig, M_SYSERR, NULL);
99 goto err;
101 sp->newl_len = orig->newl_len;
102 sp->newl_cnt = orig->newl_cnt;
103 memcpy(sp->newl, orig->newl, len);
106 if (opts_copy(orig, sp))
107 goto err;
109 F_SET(sp, F_ISSET(orig, SC_EX | SC_VI));
112 if (ex_screen_copy(orig, sp)) /* Ex. */
113 goto err;
114 if (v_screen_copy(orig, sp)) /* Vi. */
115 goto err;
116 sp->cl_private = 0; /* XXX */
117 conv_init(orig, sp); /* XXX */
119 *spp = sp;
120 return (0);
122 err: screen_end(sp);
123 return (1);
127 * screen_end --
128 * Release a screen, no matter what had (and had not) been
129 * initialized.
131 * PUBLIC: int screen_end __P((SCR *));
134 screen_end(SCR *sp)
136 int rval;
138 /* If multiply referenced, just decrement the count and return. */
139 if (--sp->refcnt != 0)
140 return (0);
143 * Remove the screen from the displayed queue.
145 * If a created screen failed during initialization, it may not
146 * be linked into the chain.
148 if (sp->q.cqe_next != NULL)
149 CIRCLEQ_REMOVE(&sp->wp->scrq, sp, q);
151 /* The screen is no longer real. */
152 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
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;
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);
173 /* Free any text input. */
174 if (sp->tiq.cqh_first != NULL)
175 text_lfree(&sp->tiq);
177 /* Free alternate file name. */
178 if (sp->alt_name != NULL)
179 free(sp->alt_name);
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);
195 /* Free all the options */
196 opts_free(sp);
198 /* Free the screen itself. */
199 free(sp);
201 return (rval);
205 * screen_next --
206 * Return the next screen in the queue.
208 * PUBLIC: SCR *screen_next __P((SCR *));
210 SCR *
211 screen_next(SCR *sp)
213 GS *gp;
214 WIN *wp;
215 SCR *next;
217 /* Try the display queue, without returning the current screen. */
218 gp = sp->gp;
219 wp = sp->wp;
220 for (next = wp->scrq.cqh_first;
221 next != (void *)&wp->scrq; next = next->q.cqe_next)
222 if (next != sp)
223 break;
224 if (next != (void *)&wp->scrq)
225 return (next);
227 /* Try the hidden queue; if found, move screen to the display queue. */
228 if (gp->hq.cqh_first != (void *)&gp->hq) {
229 next = gp->hq.cqh_first;
230 CIRCLEQ_REMOVE(&gp->hq, next, q);
231 CIRCLEQ_INSERT_HEAD(&wp->scrq, next, q);
232 next->wp = sp->wp;
233 return (next);
235 return (NULL);