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
[] = "$Id: screen.c,v 10.25 2011/12/04 04:06:45 zy Exp $";
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(GS *, SCR *, SCR **);
47 CALLOC_RET(orig
, sp
, SCR
*, 1, sizeof(SCR
));
50 /* INITIALIZED AT SCREEN CREATE. */
54 sp
->gp
= gp
; /* All ref the GS structure. */
56 sp
->ccnt
= 2; /* Anything > 1 */
60 * sp->defscroll is initialized by the opts_init() code because
61 * we don't have the option information yet.
66 /* PARTIALLY OR COMPLETELY COPIED FROM PREVIOUS SCREEN. */
68 sp
->searchdir
= NOTSET
;
70 /* Alternate file name. */
71 if (orig
->alt_name
!= NULL
&&
72 (sp
->alt_name
= strdup(orig
->alt_name
)) == NULL
)
75 /* Last executed at buffer. */
76 if (F_ISSET(orig
, SC_AT_SET
)) {
78 sp
->at_lbuf
= orig
->at_lbuf
;
81 /* Retain searching/substitution information. */
82 sp
->searchdir
= orig
->searchdir
== NOTSET
? NOTSET
: FORWARD
;
83 if (orig
->re
!= NULL
&& (sp
->re
=
84 v_wstrdup(sp
, orig
->re
, orig
->re_len
)) == NULL
)
86 sp
->re_len
= orig
->re_len
;
87 if (orig
->subre
!= NULL
&& (sp
->subre
=
88 v_wstrdup(sp
, orig
->subre
, orig
->subre_len
)) == NULL
)
90 sp
->subre_len
= orig
->subre_len
;
91 if (orig
->repl
!= NULL
&& (sp
->repl
=
92 v_wstrdup(sp
, orig
->repl
, orig
->repl_len
)) == NULL
)
94 sp
->repl_len
= orig
->repl_len
;
96 len
= orig
->newl_len
* sizeof(size_t);
97 MALLOC(sp
, sp
->newl
, size_t *, len
);
98 if (sp
->newl
== NULL
) {
99 mem
: msgq(orig
, M_SYSERR
, NULL
);
102 sp
->newl_len
= orig
->newl_len
;
103 sp
->newl_cnt
= orig
->newl_cnt
;
104 memcpy(sp
->newl
, orig
->newl
, len
);
107 if (opts_copy(orig
, sp
))
110 F_SET(sp
, F_ISSET(orig
, SC_EX
| SC_VI
));
113 if (ex_screen_copy(orig
, sp
)) /* Ex. */
115 if (v_screen_copy(orig
, sp
)) /* Vi. */
117 sp
->cl_private
= 0; /* XXX */
118 conv_init(orig
, sp
); /* XXX */
129 * Release a screen, no matter what had (and had not) been
132 * PUBLIC: int screen_end(SCR *);
139 /* If multiply referenced, just decrement the count and return. */
140 if (--sp
->refcnt
!= 0)
144 * Remove the screen from the displayed queue.
146 * If a created screen failed during initialization, it may not
147 * be linked into the chain.
149 if (TAILQ_ENTRY_ISVALID(sp
, q
))
150 TAILQ_REMOVE(sp
->gp
->dq
, sp
, q
);
152 /* The screen is no longer real. */
153 F_CLR(sp
, SC_SCR_EX
| SC_SCR_VI
);
156 if (v_screen_end(sp
)) /* End vi. */
158 if (ex_screen_end(sp
)) /* End ex. */
161 /* Free file names. */
163 if (!F_ISSET(sp
, SC_ARGNOFREE
) && sp
->argv
!= NULL
) {
164 for (ap
= sp
->argv
; *ap
!= NULL
; ++ap
)
170 /* Free any text input. */
171 if (!TAILQ_EMPTY(sp
->tiq
))
174 /* Free alternate file name. */
175 if (sp
->alt_name
!= NULL
)
178 /* Free up search information. */
181 if (F_ISSET(sp
, SC_RE_SEARCH
))
183 if (sp
->subre
!= NULL
)
185 if (F_ISSET(sp
, SC_RE_SUBST
))
186 regfree(&sp
->subre_c
);
187 if (sp
->repl
!= NULL
)
189 if (sp
->newl
!= NULL
)
192 /* Free the iconv environment */
195 /* Free all the options */
198 /* Free the screen itself. */
206 * Return the next screen in the queue.
208 * PUBLIC: SCR *screen_next(SCR *);
216 /* Try the display queue, without returning the current screen. */
218 TAILQ_FOREACH(next
, gp
->dq
, q
)
224 /* Try the hidden queue; if found, move screen to the display queue. */
225 if (!TAILQ_EMPTY(gp
->hq
)) {
226 next
= TAILQ_FIRST(gp
->hq
);
227 TAILQ_REMOVE(gp
->hq
, next
, q
);
228 TAILQ_INSERT_HEAD(gp
->dq
, next
, q
);