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.22 2001/06/25 15:19:12 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:12 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
30 #include "../perl_api/extern.h"
34 * Do the default initialization of an SCR structure.
36 * PUBLIC: int screen_init __P((GS *, SCR *, SCR **));
39 screen_init(GS
*gp
, SCR
*orig
, SCR
**spp
)
45 CALLOC_RET(orig
, sp
, SCR
*, 1, sizeof(SCR
));
48 /* INITIALIZED AT SCREEN CREATE. */
52 sp
->gp
= gp
; /* All ref the GS structure. */
54 sp
->ccnt
= 2; /* Anything > 1 */
58 * sp->defscroll is initialized by the opts_init() code because
59 * we don't have the option information yet.
62 CIRCLEQ_INIT(&sp
->tiq
);
64 /* PARTIALLY OR COMPLETELY COPIED FROM PREVIOUS SCREEN. */
66 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 __P((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 (sp
->q
.cqe_next
!= NULL
)
150 CIRCLEQ_REMOVE(&sp
->wp
->scrq
, sp
, q
);
152 /* The screen is no longer real. */
153 F_CLR(sp
, SC_SCR_EX
| SC_SCR_VI
);
156 #ifdef HAVE_PERL_INTERP
157 if (perl_screen_end(sp
)) /* End perl. */
160 if (v_screen_end(sp
)) /* End vi. */
162 if (ex_screen_end(sp
)) /* End ex. */
165 /* Free file names. */
167 if (!F_ISSET(sp
, SC_ARGNOFREE
) && sp
->argv
!= NULL
) {
168 for (ap
= sp
->argv
; *ap
!= NULL
; ++ap
)
174 /* Free any text input. */
175 if (sp
->tiq
.cqh_first
!= NULL
)
176 text_lfree(&sp
->tiq
);
178 /* Free alternate file name. */
179 if (sp
->alt_name
!= NULL
)
182 /* Free up search information. */
185 if (F_ISSET(sp
, SC_RE_SEARCH
))
187 if (sp
->subre
!= NULL
)
189 if (F_ISSET(sp
, SC_RE_SUBST
))
190 regfree(&sp
->subre_c
);
191 if (sp
->repl
!= NULL
)
193 if (sp
->newl
!= NULL
)
196 /* Free all the options */
199 /* Free the screen itself. */
207 * Return the next screen in the queue.
209 * PUBLIC: SCR *screen_next __P((SCR *));
218 /* Try the display queue, without returning the current screen. */
221 for (next
= wp
->scrq
.cqh_first
;
222 next
!= (void *)&wp
->scrq
; next
= next
->q
.cqe_next
)
225 if (next
!= (void *)&wp
->scrq
)
228 /* Try the hidden queue; if found, move screen to the display queue. */
229 if (gp
->hq
.cqh_first
!= (void *)&gp
->hq
) {
230 next
= gp
->hq
.cqh_first
;
231 CIRCLEQ_REMOVE(&gp
->hq
, next
, q
);
232 CIRCLEQ_INSERT_HEAD(&wp
->scrq
, next
, q
);