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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
29 * Do the default initialization of an SCR structure.
31 * PUBLIC: int screen_init(GS *, SCR *, SCR **);
34 screen_init(GS *gp, SCR *orig, SCR **spp)
40 CALLOC_RET(orig, sp, 1, sizeof(SCR));
43 /* INITIALIZED AT SCREEN CREATE. */
47 sp->gp = gp; /* All ref the GS structure. */
49 sp->ccnt = 2; /* Anything > 1 */
53 * sp->defscroll is initialized by the opts_init() code because
54 * we don't have the option information yet.
59 /* PARTIALLY OR COMPLETELY COPIED FROM PREVIOUS SCREEN. */
61 sp->searchdir = NOTSET;
63 /* Alternate file name. */
64 if (orig->alt_name != NULL &&
65 (sp->alt_name = strdup(orig->alt_name)) == NULL)
68 /* Last executed at buffer. */
69 if (F_ISSET(orig, SC_AT_SET)) {
71 sp->at_lbuf = orig->at_lbuf;
74 /* Retain searching/substitution information. */
75 sp->searchdir = orig->searchdir == NOTSET ? NOTSET : FORWARD;
76 if (orig->re != NULL && (sp->re =
77 v_wstrdup(sp, orig->re, orig->re_len)) == NULL)
79 sp->re_len = orig->re_len;
80 if (orig->subre != NULL && (sp->subre =
81 v_wstrdup(sp, orig->subre, orig->subre_len)) == NULL)
83 sp->subre_len = orig->subre_len;
84 if (orig->repl != NULL && (sp->repl =
85 v_wstrdup(sp, orig->repl, orig->repl_len)) == NULL)
87 sp->repl_len = orig->repl_len;
89 len = orig->newl_len * sizeof(size_t);
90 MALLOC(sp, sp->newl, len);
91 if (sp->newl == NULL) {
92 mem: msgq(orig, M_SYSERR, NULL);
95 sp->newl_len = orig->newl_len;
96 sp->newl_cnt = orig->newl_cnt;
97 memcpy(sp->newl, orig->newl, len);
100 if (opts_copy(orig, sp))
103 F_SET(sp, F_ISSET(orig, SC_EX | SC_VI));
106 if (ex_screen_copy(orig, sp)) /* Ex. */
108 if (v_screen_copy(orig, sp)) /* Vi. */
110 sp->cl_private = 0; /* XXX */
111 conv_init(orig, sp); /* XXX */
122 * Release a screen, no matter what had (and had not) been
125 * PUBLIC: int screen_end(SCR *);
132 /* If multiply referenced, just decrement the count and return. */
133 if (--sp->refcnt != 0)
137 * Remove the screen from the displayed queue.
139 * If a created screen failed during initialization, it may not
140 * be linked into the chain.
142 if (TAILQ_ENTRY_ISVALID(sp, q))
143 TAILQ_REMOVE(sp->gp->dq, sp, q);
145 /* The screen is no longer real. */
146 F_CLR(sp, SC_SCR_EX | SC_SCR_VI);
149 if (v_screen_end(sp)) /* End vi. */
151 if (ex_screen_end(sp)) /* End ex. */
154 /* Free file names. */
156 if (!F_ISSET(sp, SC_ARGNOFREE) && sp->argv != NULL) {
157 for (ap = sp->argv; *ap != NULL; ++ap)
163 /* Free any text input. */
164 if (!TAILQ_EMPTY(sp->tiq))
167 /* Free alternate file name. */
170 /* Free up search information. */
172 if (F_ISSET(sp, SC_RE_SEARCH))
175 if (F_ISSET(sp, SC_RE_SUBST))
176 regfree(&sp->subre_c);
180 /* Free the iconv environment */
183 /* Free all the options */
186 /* Free the screen itself. */
194 * Return the next screen in the queue.
196 * PUBLIC: SCR *screen_next(SCR *);
204 /* Try the display queue, without returning the current screen. */
206 TAILQ_FOREACH(next, gp->dq, q)
212 /* Try the hidden queue; if found, move screen to the display queue. */
213 if (!TAILQ_EMPTY(gp->hq)) {
214 next = TAILQ_FIRST(gp->hq);
215 TAILQ_REMOVE(gp->hq, next, q);
216 TAILQ_INSERT_HEAD(gp->dq, next, q);