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.
13 static const char sccsid[] = "@(#)screen.c 10.15 (Berkeley) 9/15/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
33 * Do the default initialization of an SCR structure.
35 * PUBLIC: int screen_init __P((GS *, SCR *, SCR **));
38 screen_init(gp, orig, spp)
46 CALLOC_RET(orig, sp, SCR *, 1, sizeof(SCR));
49 /* INITIALIZED AT SCREEN CREATE. */
53 sp->gp = gp; /* All ref the GS structure. */
55 sp->ccnt = 2; /* Anything > 1 */
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. */
67 sp->searchdir = NOTSET;
69 /* Alternate file name. */
70 if (orig->alt_name != NULL &&
71 (sp->alt_name = strdup(orig->alt_name)) == NULL)
74 /* Last executed at buffer. */
75 if (F_ISSET(orig, 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_strdup(sp, orig->re, orig->re_len)) == NULL)
85 sp->re_len = orig->re_len;
86 if (orig->subre != NULL && (sp->subre =
87 v_strdup(sp, orig->subre, orig->subre_len)) == NULL)
89 sp->subre_len = orig->subre_len;
90 if (orig->repl != NULL && (sp->repl =
91 v_strdup(sp, orig->repl, orig->repl_len)) == NULL)
93 sp->repl_len = orig->repl_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);
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))
109 F_SET(sp, F_ISSET(orig, SC_EX | SC_VI));
112 if (ex_screen_copy(orig, sp)) /* Ex. */
114 if (v_screen_copy(orig, sp)) /* Vi. */
126 * Release a screen, no matter what had (and had not) been
129 * PUBLIC: int screen_end __P((SCR *));
137 /* If multiply referenced, just decrement the count and return. */
138 if (--sp->refcnt != 0)
142 * Remove the screen from the displayed queue.
144 * If a created screen failed during initialization, it may not
145 * be linked into the chain.
147 if (sp->q.cqe_next != NULL)
148 CIRCLEQ_REMOVE(&sp->gp->dq, sp, q);
150 /* The screen is no longer real. */
151 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
154 #ifdef HAVE_PERL_INTERP
155 if (perl_screen_end(sp)) /* End perl. */
158 if (v_screen_end(sp)) /* End vi. */
160 if (ex_screen_end(sp)) /* End ex. */
163 /* Free file names. */
165 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
166 for (ap = sp->argv; *ap != NULL; ++ap)
172 /* Free any text input. */
173 if (sp->tiq.cqh_first != NULL)
174 text_lfree(&sp->tiq);
176 /* Free alternate file name. */
177 if (sp->alt_name != NULL)
180 /* Free up search information. */
183 if (F_ISSET(sp, SC_RE_SEARCH))
185 if (sp->subre != NULL)
187 if (F_ISSET(sp, SC_RE_SUBST))
188 regfree(&sp->subre_c);
189 if (sp->repl != NULL)
191 if (sp->newl != NULL)
194 /* Free all the options */
197 /* Free the screen itself. */
205 * Return the next screen in the queue.
207 * PUBLIC: SCR *screen_next __P((SCR *));
216 /* Try the display queue, without returning the current screen. */
218 for (next = gp->dq.cqh_first;
219 next != (void *)&gp->dq; next = next->q.cqe_next)
222 if (next != (void *)&gp->dq)
225 /* Try the hidden queue; if found, move screen to the display queue. */
226 if (gp->hq.cqh_first != (void *)&gp->hq) {
227 next = gp->hq.cqh_first;
228 CIRCLEQ_REMOVE(&gp->hq, next, q);
229 CIRCLEQ_INSERT_HEAD(&gp->dq, next, q);