move ccl from global structure to win
[nvi.git] / common / gs.c
blob363806f5a8ded496eea4c7a2621b57efa3da3eed
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(char *name)
33 GS *gp;
34 char *p;
36 /* Figure out what our name is. */
37 if ((p = strrchr(name, '/')) != NULL)
38 name = p + 1;
40 /* Allocate the global structure. */
41 CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS));
42 if (gp == NULL)
43 perr(name, NULL);
45 gp->progname = name;
47 /* Common global structure initialization. */
48 /* others will need to be copied from main.c */
49 CIRCLEQ_INIT(&gp->dq);
51 CIRCLEQ_INIT(&gp->hq);
52 gp->noprint = DEFAULT_NOPRINT;
54 /* Structures shared by screens so stored in the GS structure. */
55 CIRCLEQ_INIT(&gp->frefq);
56 CIRCLEQ_INIT(&gp->exfq);
57 LIST_INIT(&gp->seqq);
59 thread_init(gp);
61 return (gp);
65 * gs_new_win
66 * Create new window
67 * PUBLIC: WIN * gs_new_win __P((GS *gp));
70 WIN *
71 gs_new_win(GS *gp)
73 WIN *wp;
75 CALLOC_NOMSG(NULL, wp, WIN *, 1, sizeof(*wp));
76 if (!wp)
77 return NULL;
79 /* Common global structure initialization. */
80 LIST_INIT(&wp->ecq);
81 LIST_INSERT_HEAD(&wp->ecq, &wp->excmd, q);
83 CIRCLEQ_INSERT_TAIL(&gp->dq, wp, q);
84 CIRCLEQ_INIT(&wp->scrq);
86 CIRCLEQ_INIT(&wp->dcb_store.textq);
87 LIST_INIT(&wp->cutq);
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 if (wp->ccl_sp != NULL) {
108 (void)file_end(wp->ccl_sp, NULL, 1);
109 (void)screen_end(wp->ccl_sp);
111 while ((sp = wp->scrq.cqh_first) != (void *)&wp->scrq)
112 (void)screen_end(sp);
114 /* Free key input queue. */
115 if (wp->i_event != NULL)
116 free(wp->i_event);
118 /* Free cut buffers. */
119 cut_close(wp);
121 /* Free default buffer storage. */
122 (void)text_lfree(&wp->dcb_store.textq);
124 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
125 /* Free any temporary space. */
126 if (wp->tmp_bp != NULL)
127 free(wp->tmp_bp);
128 #endif
130 return 0;
134 * gs_end --
135 * End the program, discarding screens and most of the global area.
137 * PUBLIC: void gs_end __P((GS *));
139 void
140 gs_end(GS *gp)
142 MSGS *mp;
143 SCR *sp;
144 WIN *wp;
146 /* If there are any remaining screens, kill them off. */
147 while ((wp = gp->dq.cqh_first) != (void *)&gp->dq)
148 (void)win_end(wp);
149 while ((sp = gp->hq.cqh_first) != (void *)&gp->hq)
150 (void)screen_end(sp);
152 #ifdef HAVE_PERL_INTERP
153 perl_end(gp);
154 #endif
156 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
157 { FREF *frp;
158 /* Free FREF's. */
159 while ((frp = gp->frefq.cqh_first) != (FREF *)&gp->frefq) {
160 CIRCLEQ_REMOVE(&gp->frefq, frp, q);
161 if (frp->name != NULL)
162 free(frp->name);
163 if (frp->tname != NULL)
164 free(frp->tname);
165 free(frp);
169 /* Free map sequences. */
170 seq_close(gp);
172 /* Close message catalogs. */
173 msg_close(gp);
174 #endif
175 if (gp->env) {
176 gp->env->remove(gp->env, NULL, 0);
178 gp->env->close(gp->env, 0);
182 /* Ring the bell if scheduled. */
183 if (F_ISSET(gp, G_BELLSCHED))
184 (void)fprintf(stderr, "\07"); /* \a */
187 * Flush any remaining messages. If a message is here, it's almost
188 * certainly the message about the event that killed us (although
189 * it's possible that the user is sourcing a file that exits from the
190 * editor).
192 while ((mp = gp->msgq.lh_first) != NULL) {
193 (void)fprintf(stderr, "%s%.*s",
194 mp->mtype == M_ERR ? "ex/vi: " : "", (int)mp->len, mp->buf);
195 LIST_REMOVE(mp, q);
196 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
197 free(mp->buf);
198 free(mp);
199 #endif
202 #if defined(TRACE)
203 /* Close tracing file descriptor. */
204 vtrace_end();
205 #endif
210 * perr --
211 * Print system error.
213 static void
214 perr(char *name, char *msg)
216 (void)fprintf(stderr, "%s:", name);
217 if (msg != NULL)
218 (void)fprintf(stderr, "%s:", msg);
219 (void)fprintf(stderr, "%s\n", strerror(errno));
220 exit(1);