tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / common / screen.c
blobe8c8819416a5bef24ea207035855513dbe5ff7b5
1 /* $NetBSD: screen.c,v 1.5 2013/12/01 02:34:54 christos Exp $ */
2 /*-
3 * Copyright (c) 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #ifndef lint
14 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 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "common.h"
30 #include "../vi/vi.h"
32 static int screen_end1(SCR *, int);
34 * screen_init --
35 * Do the default initialization of an SCR structure.
37 * PUBLIC: int screen_init __P((GS *, SCR *, SCR **));
39 int
40 screen_init(GS *gp, SCR *orig, SCR **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 TAILQ_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_end1(sp, 0);
125 return (1);
128 static int
129 screen_end1(SCR *sp, int init)
131 int rval;
133 /* If multiply referenced, just decrement the count and return. */
134 if (--sp->refcnt != 0)
135 return (0);
138 * Remove the screen from the displayed queue.
140 * If a created screen failed during initialization, it may not
141 * be linked into the chain.
143 if (init)
144 TAILQ_REMOVE(&sp->wp->scrq, sp, q);
146 /* The screen is no longer real. */
147 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
149 rval = 0;
150 #ifdef HAVE_PERL_INTERP
151 if (perl_screen_end(sp)) /* End perl. */
152 rval = 1;
153 #endif
154 if (v_screen_end(sp)) /* End vi. */
155 rval = 1;
156 if (ex_screen_end(sp)) /* End ex. */
157 rval = 1;
159 /* Free file names. */
160 { char **ap;
161 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
162 for (ap = sp->argv; *ap != NULL; ++ap)
163 free(*ap);
164 free(sp->argv);
168 /* Free any text input. */
169 if (!TAILQ_EMPTY(&sp->tiq))
170 text_lfree(&sp->tiq);
172 /* Free alternate file name. */
173 if (sp->alt_name != NULL)
174 free(sp->alt_name);
176 /* Free up search information. */
177 if (sp->re != NULL)
178 free(sp->re);
179 if (F_ISSET(sp, SC_RE_SEARCH))
180 regfree(&sp->re_c);
181 if (sp->subre != NULL)
182 free(sp->subre);
183 if (F_ISSET(sp, SC_RE_SUBST))
184 regfree(&sp->subre_c);
185 if (sp->repl != NULL)
186 free(sp->repl);
187 if (sp->newl != NULL)
188 free(sp->newl);
190 /* Free all the options */
191 opts_free(sp);
193 /* Free the screen itself. */
194 free(sp);
196 return (rval);
200 * screen_end --
201 * Release a screen, no matter what had (and had not) been
202 * initialized.
204 * PUBLIC: int screen_end __P((SCR *));
207 screen_end(SCR *sp)
209 return screen_end1(sp, 1);
213 * screen_next --
214 * Return the next screen in the queue.
216 * PUBLIC: SCR *screen_next __P((SCR *));
218 SCR *
219 screen_next(SCR *sp)
221 GS *gp;
222 WIN *wp;
223 SCR *next;
225 /* Try the display queue, without returning the current screen. */
226 gp = sp->gp;
227 wp = sp->wp;
228 TAILQ_FOREACH(next, &wp->scrq, q)
229 if (next != sp)
230 break;
231 if (next != NULL)
232 return (next);
234 /* Try the hidden queue; if found, move screen to the display queue. */
235 if ((next = TAILQ_FIRST(&gp->hq)) != NULL) {
236 TAILQ_REMOVE(&gp->hq, next, q);
237 TAILQ_INSERT_HEAD(&wp->scrq, next, q);
238 next->wp = sp->wp;
239 return (next);
241 return (NULL);