Use usermem for getting from db.
[nvi.git] / common / gs.c
blob822e0df03b647b712e051f124477c65f653b3be0
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 thread_init(gp);
54 return (gp);
58 * gs_new_win
59 * Create new window
60 * PUBLIC: WIN * gs_new_win __P((GS *gp));
63 WIN *
64 gs_new_win(GS *gp)
66 WIN *wp;
68 CALLOC_NOMSG(NULL, wp, WIN *, 1, sizeof(*wp));
69 if (!wp)
70 return NULL;
72 /* Common global structure initialization. */
73 LIST_INIT(&wp->ecq);
74 LIST_INSERT_HEAD(&wp->ecq, &wp->excmd, q);
76 CIRCLEQ_INSERT_TAIL(&gp->dq, wp, q);
77 CIRCLEQ_INIT(&wp->scrq);
79 wp->gp = gp;
81 return wp;
85 * win_end --
86 * Remove window.
88 * PUBLIC: int win_end __P((WIN *wp));
90 int
91 win_end(WIN *wp)
93 SCR *sp;
95 CIRCLEQ_REMOVE(&wp->gp->dq, wp, q);
97 while ((sp = wp->scrq.cqh_first) != (void *)&wp->scrq)
98 (void)screen_end(sp);
100 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
101 /* Free any temporary space. */
102 if (wp->tmp_bp != NULL)
103 free(wp->tmp_bp);
104 #endif
106 return 0;
110 * gs_end --
111 * End the program, discarding screens and most of the global area.
113 * PUBLIC: void gs_end __P((GS *));
115 void
116 gs_end(gp)
117 GS *gp;
119 MSGS *mp;
120 SCR *sp;
121 WIN *wp;
123 /* If there are any remaining screens, kill them off. */
124 if (gp->ccl_sp != NULL) {
125 (void)file_end(gp->ccl_sp, NULL, 1);
126 (void)screen_end(gp->ccl_sp);
128 while ((wp = gp->dq.cqh_first) != (void *)&gp->dq)
129 (void)win_end(wp);
130 while ((sp = gp->hq.cqh_first) != (void *)&gp->hq)
131 (void)screen_end(sp);
133 #ifdef HAVE_PERL_INTERP
134 perl_end(gp);
135 #endif
137 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
138 { FREF *frp;
139 /* Free FREF's. */
140 while ((frp = gp->frefq.cqh_first) != (FREF *)&gp->frefq) {
141 CIRCLEQ_REMOVE(&gp->frefq, frp, q);
142 if (frp->name != NULL)
143 free(frp->name);
144 if (frp->tname != NULL)
145 free(frp->tname);
146 free(frp);
150 /* Free key input queue. */
151 if (gp->i_event != NULL)
152 free(gp->i_event);
154 /* Free cut buffers. */
155 cut_close(gp);
157 /* Free map sequences. */
158 seq_close(gp);
160 /* Free default buffer storage. */
161 (void)text_lfree(&gp->dcb_store.textq);
163 /* Close message catalogs. */
164 msg_close(gp);
165 #endif
166 if (gp->env) {
167 gp->env->remove(gp->env, NULL, 0);
169 gp->env->close(gp->env, 0);
173 /* Ring the bell if scheduled. */
174 if (F_ISSET(gp, G_BELLSCHED))
175 (void)fprintf(stderr, "\07"); /* \a */
178 * Flush any remaining messages. If a message is here, it's almost
179 * certainly the message about the event that killed us (although
180 * it's possible that the user is sourcing a file that exits from the
181 * editor).
183 while ((mp = gp->msgq.lh_first) != NULL) {
184 (void)fprintf(stderr, "%s%.*s",
185 mp->mtype == M_ERR ? "ex/vi: " : "", (int)mp->len, mp->buf);
186 LIST_REMOVE(mp, q);
187 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
188 free(mp->buf);
189 free(mp);
190 #endif
193 #if defined(TRACE)
194 /* Close tracing file descriptor. */
195 vtrace_end();
196 #endif
201 * perr --
202 * Print system error.
204 static void
205 perr(name, msg)
206 char *name, *msg;
208 (void)fprintf(stderr, "%s:", name);
209 if (msg != NULL)
210 (void)fprintf(stderr, "%s:", msg);
211 (void)fprintf(stderr, "%s\n", strerror(errno));
212 exit(1);