1 /* $NetBSD: gs.c,v 1.4 2013/12/01 02:34:54 christos Exp $ */
4 * Sven Verdoolaege. All rights reserved.
6 * See the LICENSE file for redistribution information.
11 #include <sys/types.h>
12 #include <sys/queue.h>
14 #include <bitstring.h>
22 #include "../common/common.h"
23 #ifdef USE_PERL_INTERP
24 #include "perl_api_extern.h"
27 __dead
static void perr
__P((char *, char *));
31 * Create and partially initialize the GS structure.
32 * PUBLIC: GS * gs_init __P((char*));
40 /* Figure out what our name is. */
41 if ((p
= strrchr(name
, '/')) != NULL
)
44 /* Allocate the global structure. */
45 CALLOC_NOMSG(NULL
, gp
, GS
*, 1, sizeof(GS
));
51 /* Common global structure initialization. */
52 /* others will need to be copied from main.c */
56 gp
->noprint
= DEFAULT_NOPRINT
;
58 /* Structures shared by screens so stored in the GS structure. */
59 TAILQ_INIT(&gp
->frefq
);
60 TAILQ_INIT(&gp
->exfq
);
71 * PUBLIC: WIN * gs_new_win __P((GS *gp));
79 CALLOC_NOMSG(NULL
, wp
, WIN
*, 1, sizeof(*wp
));
83 /* Common global structure initialization. */
85 LIST_INSERT_HEAD(&wp
->ecq
, &wp
->excmd
, q
);
87 TAILQ_INSERT_TAIL(&gp
->dq
, wp
, q
);
88 TAILQ_INIT(&wp
->scrq
);
90 TAILQ_INIT(&wp
->dcb_store
.textq
);
102 * PUBLIC: int win_end __P((WIN *wp));
109 TAILQ_REMOVE(&wp
->gp
->dq
, wp
, q
);
111 if (wp
->ccl_sp
!= NULL
) {
112 (void)file_end(wp
->ccl_sp
, NULL
, 1);
113 (void)screen_end(wp
->ccl_sp
);
115 while ((sp
= TAILQ_FIRST(&wp
->scrq
)) != NULL
)
116 (void)screen_end(sp
);
118 /* Free key input queue. */
119 if (wp
->i_event
!= NULL
)
122 /* Free cut buffers. */
125 /* Free default buffer storage. */
126 (void)text_lfree(&wp
->dcb_store
.textq
);
128 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
129 /* Free any temporary space. */
130 if (wp
->tmp_bp
!= NULL
)
139 * End the program, discarding screens and most of the global area.
141 * PUBLIC: void gs_end __P((GS *));
150 /* If there are any remaining screens, kill them off. */
151 while ((wp
= TAILQ_FIRST(&gp
->dq
)) != NULL
)
153 while ((sp
= TAILQ_FIRST(&gp
->hq
)) != NULL
)
154 (void)screen_end(sp
);
156 #ifdef HAVE_PERL_INTERP
160 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
163 while ((frp
= TAILQ_FIRST(&gp
->frefq
)) != NULL
) {
164 TAILQ_REMOVE(&gp
->frefq
, frp
, q
);
165 if (frp
->name
!= NULL
)
167 if (frp
->tname
!= NULL
)
173 /* Free map sequences. */
176 /* Close message catalogs. */
180 /* Ring the bell if scheduled. */
181 if (F_ISSET(gp
, G_BELLSCHED
))
182 (void)fprintf(stderr
, "\07"); /* \a */
185 * Flush any remaining messages. If a message is here, it's almost
186 * certainly the message about the event that killed us (although
187 * it's possible that the user is sourcing a file that exits from the
190 while ((mp
= LIST_FIRST(&gp
->msgq
)) != NULL
) {
191 (void)fprintf(stderr
, "%s%.*s",
192 mp
->mtype
== M_ERR
? "ex/vi: " : "", (int)mp
->len
, mp
->buf
);
194 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
201 /* Close tracing file descriptor. */
209 * Print system error.
212 perr(char *name
, char *msg
)
214 (void)fprintf(stderr
, "%s:", name
);
216 (void)fprintf(stderr
, "%s:", msg
);
217 (void)fprintf(stderr
, "%s\n", strerror(errno
));