use DB_THREAD
[nvi.git] / common / gs.c
blob7ef5e04ee9560b93cea528e8defd99eb0cf11a8f
1 /*-
2 * Copyright (c) 2000
3 * Sven Verdoolaege. All rights reserved.
5 * See the LICENSE file for redistribution information.
6 */
8 #include "config.h"
10 #include <sys/types.h>
11 #include <sys/queue.h>
13 #include <bitstring.h>
14 #include <ctype.h>
15 #include <errno.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
21 #include "../common/common.h"
23 static void perr __P((char *, char *));
26 * gs_init --
27 * Create and partially initialize the GS structure.
28 * PUBLIC: GS * gs_init __P((char*));
30 GS *
31 gs_init(name)
32 char *name;
34 GS *gp;
35 char *p;
37 /* Figure out what our name is. */
38 if ((p = strrchr(name, '/')) != NULL)
39 name = p + 1;
41 /* Allocate the global structure. */
42 CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS));
43 if (gp == NULL)
44 perr(name, NULL);
46 gp->progname = name;
48 /* Common global structure initialization. */
49 /* others will need to be copied from main.c */
50 CIRCLEQ_INIT(&gp->dq);
52 CIRCLEQ_INIT(&gp->hq);
53 gp->noprint = DEFAULT_NOPRINT;
55 /* Structures shared by screens so stored in the GS structure. */
56 CIRCLEQ_INIT(&gp->frefq);
57 CIRCLEQ_INIT(&gp->exfq);
58 CIRCLEQ_INIT(&gp->dcb_store.textq);
59 LIST_INIT(&gp->cutq);
60 LIST_INIT(&gp->seqq);
62 thread_init(gp);
64 return (gp);
68 * gs_new_win
69 * Create new window
70 * PUBLIC: WIN * gs_new_win __P((GS *gp));
73 WIN *
74 gs_new_win(GS *gp)
76 WIN *wp;
78 CALLOC_NOMSG(NULL, wp, WIN *, 1, sizeof(*wp));
79 if (!wp)
80 return NULL;
82 /* Common global structure initialization. */
83 LIST_INIT(&wp->ecq);
84 LIST_INSERT_HEAD(&wp->ecq, &wp->excmd, q);
86 CIRCLEQ_INSERT_TAIL(&gp->dq, wp, q);
87 CIRCLEQ_INIT(&wp->scrq);
89 wp->gp = gp;
91 return wp;
95 * win_end --
96 * Remove window.
98 * PUBLIC: int win_end __P((WIN *wp));
101 win_end(WIN *wp)
103 SCR *sp;
105 CIRCLEQ_REMOVE(&wp->gp->dq, wp, q);
107 while ((sp = wp->scrq.cqh_first) != (void *)&wp->scrq)
108 (void)screen_end(sp);
110 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
111 /* Free any temporary space. */
112 if (wp->tmp_bp != NULL)
113 free(wp->tmp_bp);
114 #endif
116 return 0;
120 * gs_end --
121 * End the program, discarding screens and most of the global area.
123 * PUBLIC: void gs_end __P((GS *));
125 void
126 gs_end(gp)
127 GS *gp;
129 MSGS *mp;
130 SCR *sp;
131 WIN *wp;
133 /* If there are any remaining screens, kill them off. */
134 if (gp->ccl_sp != NULL) {
135 (void)file_end(gp->ccl_sp, NULL, 1);
136 (void)screen_end(gp->ccl_sp);
138 while ((wp = gp->dq.cqh_first) != (void *)&gp->dq)
139 (void)win_end(wp);
140 while ((sp = gp->hq.cqh_first) != (void *)&gp->hq)
141 (void)screen_end(sp);
143 #ifdef HAVE_PERL_INTERP
144 perl_end(gp);
145 #endif
147 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
148 { FREF *frp;
149 /* Free FREF's. */
150 while ((frp = gp->frefq.cqh_first) != (FREF *)&gp->frefq) {
151 CIRCLEQ_REMOVE(&gp->frefq, frp, q);
152 if (frp->name != NULL)
153 free(frp->name);
154 if (frp->tname != NULL)
155 free(frp->tname);
156 free(frp);
160 /* Free key input queue. */
161 if (gp->i_event != NULL)
162 free(gp->i_event);
164 /* Free cut buffers. */
165 cut_close(gp);
167 /* Free map sequences. */
168 seq_close(gp);
170 /* Free default buffer storage. */
171 (void)text_lfree(&gp->dcb_store.textq);
173 /* Close message catalogs. */
174 msg_close(gp);
175 #endif
176 if (gp->env) {
177 gp->env->remove(gp->env, NULL, 0);
179 gp->env->close(gp->env, 0);
183 /* Ring the bell if scheduled. */
184 if (F_ISSET(gp, G_BELLSCHED))
185 (void)fprintf(stderr, "\07"); /* \a */
188 * Flush any remaining messages. If a message is here, it's almost
189 * certainly the message about the event that killed us (although
190 * it's possible that the user is sourcing a file that exits from the
191 * editor).
193 while ((mp = gp->msgq.lh_first) != NULL) {
194 (void)fprintf(stderr, "%s%.*s",
195 mp->mtype == M_ERR ? "ex/vi: " : "", (int)mp->len, mp->buf);
196 LIST_REMOVE(mp, q);
197 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
198 free(mp->buf);
199 free(mp);
200 #endif
203 #if defined(TRACE)
204 /* Close tracing file descriptor. */
205 vtrace_end();
206 #endif
211 * perr --
212 * Print system error.
214 static void
215 perr(name, msg)
216 char *name, *msg;
218 (void)fprintf(stderr, "%s:", name);
219 if (msg != NULL)
220 (void)fprintf(stderr, "%s:", msg);
221 (void)fprintf(stderr, "%s\n", strerror(errno));
222 exit(1);