include "perl_api/extern.h" for perl_* functions
[nvi.git] / common / screen.c
blob5371912ad3f072474c18ef3a544bd6ac3a8f6583
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"
30 #include "../perl_api/extern.h"
33 * screen_init --
34 * Do the default initialization of an SCR structure.
36 * PUBLIC: int screen_init __P((GS *, SCR *, SCR **));
38 int
39 screen_init(GS *gp, SCR *orig, SCR **spp)
41 SCR *sp;
42 size_t len;
44 *spp = NULL;
45 CALLOC_RET(orig, sp, SCR *, 1, sizeof(SCR));
46 *spp = sp;
48 /* INITIALIZED AT SCREEN CREATE. */
49 sp->id = ++gp->id;
50 sp->refcnt = 1;
52 sp->gp = gp; /* All ref the GS structure. */
54 sp->ccnt = 2; /* Anything > 1 */
57 * XXX
58 * sp->defscroll is initialized by the opts_init() code because
59 * we don't have the option information yet.
62 CIRCLEQ_INIT(&sp->tiq);
64 /* PARTIALLY OR COMPLETELY COPIED FROM PREVIOUS SCREEN. */
65 if (orig == NULL) {
66 sp->searchdir = NOTSET;
67 } else {
68 sp->wp = orig->wp;
70 /* Alternate file name. */
71 if (orig->alt_name != NULL &&
72 (sp->alt_name = strdup(orig->alt_name)) == NULL)
73 goto mem;
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;
81 /* Retain searching/substitution information. */
82 sp->searchdir = orig->searchdir == NOTSET ? NOTSET : FORWARD;
83 if (orig->re != NULL && (sp->re =
84 v_wstrdup(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_wstrdup(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_wstrdup(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;
102 sp->newl_len = orig->newl_len;
103 sp->newl_cnt = orig->newl_cnt;
104 memcpy(sp->newl, orig->newl, len);
107 if (opts_copy(orig, sp))
108 goto err;
110 F_SET(sp, F_ISSET(orig, SC_EX | SC_VI));
113 if (ex_screen_copy(orig, sp)) /* Ex. */
114 goto err;
115 if (v_screen_copy(orig, sp)) /* Vi. */
116 goto err;
117 sp->cl_private = 0; /* XXX */
118 conv_init(orig, sp); /* XXX */
120 *spp = sp;
121 return (0);
123 err: screen_end(sp);
124 return (1);
128 * screen_end --
129 * Release a screen, no matter what had (and had not) been
130 * initialized.
132 * PUBLIC: int screen_end __P((SCR *));
135 screen_end(SCR *sp)
137 int rval;
139 /* If multiply referenced, just decrement the count and return. */
140 if (--sp->refcnt != 0)
141 return (0);
144 * Remove the screen from the displayed queue.
146 * If a created screen failed during initialization, it may not
147 * be linked into the chain.
149 if (sp->q.cqe_next != NULL)
150 CIRCLEQ_REMOVE(&sp->wp->scrq, sp, q);
152 /* The screen is no longer real. */
153 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
155 rval = 0;
156 #ifdef HAVE_PERL_INTERP
157 if (perl_screen_end(sp)) /* End perl. */
158 rval = 1;
159 #endif
160 if (v_screen_end(sp)) /* End vi. */
161 rval = 1;
162 if (ex_screen_end(sp)) /* End ex. */
163 rval = 1;
165 /* Free file names. */
166 { char **ap;
167 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
168 for (ap = sp->argv; *ap != NULL; ++ap)
169 free(*ap);
170 free(sp->argv);
174 /* Free any text input. */
175 if (sp->tiq.cqh_first != NULL)
176 text_lfree(&sp->tiq);
178 /* Free alternate file name. */
179 if (sp->alt_name != NULL)
180 free(sp->alt_name);
182 /* Free up search information. */
183 if (sp->re != NULL)
184 free(sp->re);
185 if (F_ISSET(sp, SC_RE_SEARCH))
186 regfree(&sp->re_c);
187 if (sp->subre != NULL)
188 free(sp->subre);
189 if (F_ISSET(sp, SC_RE_SUBST))
190 regfree(&sp->subre_c);
191 if (sp->repl != NULL)
192 free(sp->repl);
193 if (sp->newl != NULL)
194 free(sp->newl);
196 /* Free all the options */
197 opts_free(sp);
199 /* Free the screen itself. */
200 free(sp);
202 return (rval);
206 * screen_next --
207 * Return the next screen in the queue.
209 * PUBLIC: SCR *screen_next __P((SCR *));
211 SCR *
212 screen_next(SCR *sp)
214 GS *gp;
215 WIN *wp;
216 SCR *next;
218 /* Try the display queue, without returning the current screen. */
219 gp = sp->gp;
220 wp = sp->wp;
221 for (next = wp->scrq.cqh_first;
222 next != (void *)&wp->scrq; next = next->q.cqe_next)
223 if (next != sp)
224 break;
225 if (next != (void *)&wp->scrq)
226 return (next);
228 /* Try the hidden queue; if found, move screen to the display queue. */
229 if (gp->hq.cqh_first != (void *)&gp->hq) {
230 next = gp->hq.cqh_first;
231 CIRCLEQ_REMOVE(&gp->hq, next, q);
232 CIRCLEQ_INSERT_HEAD(&wp->scrq, next, q);
233 next->wp = sp->wp;
234 return (next);
236 return (NULL);