Croak on first time a conversion error occurs in a screen.
[nvi.git] / common / screen.c
blob5a12f4718c771ce68e10db98949f624206c68359
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.21 2001/05/10 19:28:43 skimo Exp $ (Berkeley) $Date: 2001/05/10 19:28:43 $";
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(gp, orig, spp)
39 GS *gp;
40 SCR *orig, **spp;
42 SCR *sp;
43 size_t len;
45 *spp = NULL;
46 CALLOC_RET(orig, sp, SCR *, 1, sizeof(SCR));
47 *spp = sp;
49 /* INITIALIZED AT SCREEN CREATE. */
50 sp->id = ++gp->id;
51 sp->refcnt = 1;
53 sp->gp = gp; /* All ref the GS structure. */
55 sp->ccnt = 2; /* Anything > 1 */
58 * XXX
59 * sp->defscroll is initialized by the opts_init() code because
60 * we don't have the option information yet.
63 CIRCLEQ_INIT(&sp->tiq);
65 /* PARTIALLY OR COMPLETELY COPIED FROM PREVIOUS SCREEN. */
66 if (orig == NULL) {
67 sp->searchdir = NOTSET;
68 } else {
69 sp->wp = orig->wp;
71 /* Alternate file name. */
72 if (orig->alt_name != NULL &&
73 (sp->alt_name = strdup(orig->alt_name)) == NULL)
74 goto mem;
76 /* Last executed at buffer. */
77 if (F_ISSET(orig, SC_AT_SET)) {
78 F_SET(sp, SC_AT_SET);
79 sp->at_lbuf = orig->at_lbuf;
82 /* Retain searching/substitution information. */
83 sp->searchdir = orig->searchdir == NOTSET ? NOTSET : FORWARD;
84 if (orig->re != NULL && (sp->re =
85 v_wstrdup(sp, orig->re, orig->re_len)) == NULL)
86 goto mem;
87 sp->re_len = orig->re_len;
88 if (orig->subre != NULL && (sp->subre =
89 v_wstrdup(sp, orig->subre, orig->subre_len)) == NULL)
90 goto mem;
91 sp->subre_len = orig->subre_len;
92 if (orig->repl != NULL && (sp->repl =
93 v_wstrdup(sp, orig->repl, orig->repl_len)) == NULL)
94 goto mem;
95 sp->repl_len = orig->repl_len;
96 if (orig->newl_len) {
97 len = orig->newl_len * sizeof(size_t);
98 MALLOC(sp, sp->newl, size_t *, len);
99 if (sp->newl == NULL) {
100 mem: msgq(orig, M_SYSERR, NULL);
101 goto err;
103 sp->newl_len = orig->newl_len;
104 sp->newl_cnt = orig->newl_cnt;
105 memcpy(sp->newl, orig->newl, len);
108 if (opts_copy(orig, sp))
109 goto err;
111 F_SET(sp, F_ISSET(orig, SC_EX | SC_VI));
114 if (ex_screen_copy(orig, sp)) /* Ex. */
115 goto err;
116 if (v_screen_copy(orig, sp)) /* Vi. */
117 goto err;
118 sp->cl_private = 0; /* XXX */
119 conv_init(orig, sp); /* XXX */
121 *spp = sp;
122 return (0);
124 err: screen_end(sp);
125 return (1);
129 * screen_end --
130 * Release a screen, no matter what had (and had not) been
131 * initialized.
133 * PUBLIC: int screen_end __P((SCR *));
136 screen_end(sp)
137 SCR *sp;
139 int rval;
141 /* If multiply referenced, just decrement the count and return. */
142 if (--sp->refcnt != 0)
143 return (0);
146 * Remove the screen from the displayed queue.
148 * If a created screen failed during initialization, it may not
149 * be linked into the chain.
151 if (sp->q.cqe_next != NULL)
152 CIRCLEQ_REMOVE(&sp->wp->scrq, sp, q);
154 /* The screen is no longer real. */
155 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
157 rval = 0;
158 #ifdef HAVE_PERL_INTERP
159 if (perl_screen_end(sp)) /* End perl. */
160 rval = 1;
161 #endif
162 if (v_screen_end(sp)) /* End vi. */
163 rval = 1;
164 if (ex_screen_end(sp)) /* End ex. */
165 rval = 1;
167 /* Free file names. */
168 { char **ap;
169 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
170 for (ap = sp->argv; *ap != NULL; ++ap)
171 free(*ap);
172 free(sp->argv);
176 /* Free any text input. */
177 if (sp->tiq.cqh_first != NULL)
178 text_lfree(&sp->tiq);
180 /* Free alternate file name. */
181 if (sp->alt_name != NULL)
182 free(sp->alt_name);
184 /* Free up search information. */
185 if (sp->re != NULL)
186 free(sp->re);
187 if (F_ISSET(sp, SC_RE_SEARCH))
188 regfree(&sp->re_c);
189 if (sp->subre != NULL)
190 free(sp->subre);
191 if (F_ISSET(sp, SC_RE_SUBST))
192 regfree(&sp->subre_c);
193 if (sp->repl != NULL)
194 free(sp->repl);
195 if (sp->newl != NULL)
196 free(sp->newl);
198 /* Free all the options */
199 opts_free(sp);
201 /* Free the screen itself. */
202 free(sp);
204 return (rval);
208 * screen_next --
209 * Return the next screen in the queue.
211 * PUBLIC: SCR *screen_next __P((SCR *));
213 SCR *
214 screen_next(sp)
215 SCR *sp;
217 GS *gp;
218 WIN *wp;
219 SCR *next;
221 /* Try the display queue, without returning the current screen. */
222 gp = sp->gp;
223 wp = sp->wp;
224 for (next = wp->scrq.cqh_first;
225 next != (void *)&wp->scrq; next = next->q.cqe_next)
226 if (next != sp)
227 break;
228 if (next != (void *)&wp->scrq)
229 return (next);
231 /* Try the hidden queue; if found, move screen to the display queue. */
232 if (gp->hq.cqh_first != (void *)&gp->hq) {
233 next = gp->hq.cqh_first;
234 CIRCLEQ_REMOVE(&gp->hq, next, q);
235 CIRCLEQ_INSERT_HEAD(&wp->scrq, next, q);
236 next->wp = sp->wp;
237 return (next);
239 return (NULL);