Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / common / screen.c
blob17253a755d2a026dc9648ae9e9900674d37dda29
1 /* $NetBSD$ */
3 /*-
4 * Copyright (c) 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "common.h"
31 #include "../vi/vi.h"
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 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(SCR *sp)
138 int rval;
140 /* If multiply referenced, just decrement the count and return. */
141 if (--sp->refcnt != 0)
142 return (0);
145 * Remove the screen from the displayed queue.
147 * If a created screen failed during initialization, it may not
148 * be linked into the chain.
150 if (sp->q.cqe_next != NULL)
151 CIRCLEQ_REMOVE(&sp->wp->scrq, sp, q);
153 /* The screen is no longer real. */
154 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
156 rval = 0;
157 #ifdef HAVE_PERL_INTERP
158 if (perl_screen_end(sp)) /* End perl. */
159 rval = 1;
160 #endif
161 if (v_screen_end(sp)) /* End vi. */
162 rval = 1;
163 if (ex_screen_end(sp)) /* End ex. */
164 rval = 1;
166 /* Free file names. */
167 { char **ap;
168 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
169 for (ap = sp->argv; *ap != NULL; ++ap)
170 free(*ap);
171 free(sp->argv);
175 /* Free any text input. */
176 if (sp->tiq.cqh_first != NULL)
177 text_lfree(&sp->tiq);
179 /* Free alternate file name. */
180 if (sp->alt_name != NULL)
181 free(sp->alt_name);
183 /* Free up search information. */
184 if (sp->re != NULL)
185 free(sp->re);
186 if (F_ISSET(sp, SC_RE_SEARCH))
187 regfree(&sp->re_c);
188 if (sp->subre != NULL)
189 free(sp->subre);
190 if (F_ISSET(sp, SC_RE_SUBST))
191 regfree(&sp->subre_c);
192 if (sp->repl != NULL)
193 free(sp->repl);
194 if (sp->newl != NULL)
195 free(sp->newl);
197 /* Free all the options */
198 opts_free(sp);
200 /* Free the screen itself. */
201 free(sp);
203 return (rval);
207 * screen_next --
208 * Return the next screen in the queue.
210 * PUBLIC: SCR *screen_next __P((SCR *));
212 SCR *
213 screen_next(SCR *sp)
215 GS *gp;
216 WIN *wp;
217 SCR *next;
219 /* Try the display queue, without returning the current screen. */
220 gp = sp->gp;
221 wp = sp->wp;
222 for (next = wp->scrq.cqh_first;
223 next != (void *)&wp->scrq; next = next->q.cqe_next)
224 if (next != sp)
225 break;
226 if (next != (void *)&wp->scrq)
227 return (next);
229 /* Try the hidden queue; if found, move screen to the display queue. */
230 if (gp->hq.cqh_first != (void *)&gp->hq) {
231 next = gp->hq.cqh_first;
232 CIRCLEQ_REMOVE(&gp->hq, next, q);
233 CIRCLEQ_INSERT_HEAD(&wp->scrq, next, q);
234 next->wp = sp->wp;
235 return (next);
237 return (NULL);