include curses library in libs when checking curses functionality
[nvi.git] / common / gs.c
blobedd6a1640aad5295002ffe6307b13563e0c6e5d6
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 LIST_INIT(&gp->seqq);
60 thread_init(gp);
62 return (gp);
66 * gs_new_win
67 * Create new window
68 * PUBLIC: WIN * gs_new_win __P((GS *gp));
71 WIN *
72 gs_new_win(GS *gp)
74 WIN *wp;
76 CALLOC_NOMSG(NULL, wp, WIN *, 1, sizeof(*wp));
77 if (!wp)
78 return NULL;
80 /* Common global structure initialization. */
81 LIST_INIT(&wp->ecq);
82 LIST_INSERT_HEAD(&wp->ecq, &wp->excmd, q);
84 CIRCLEQ_INSERT_TAIL(&gp->dq, wp, q);
85 CIRCLEQ_INIT(&wp->scrq);
87 CIRCLEQ_INIT(&wp->dcb_store.textq);
88 LIST_INIT(&wp->cutq);
90 wp->gp = gp;
92 return wp;
96 * win_end --
97 * Remove window.
99 * PUBLIC: int win_end __P((WIN *wp));
102 win_end(WIN *wp)
104 SCR *sp;
106 CIRCLEQ_REMOVE(&wp->gp->dq, wp, q);
108 while ((sp = wp->scrq.cqh_first) != (void *)&wp->scrq)
109 (void)screen_end(sp);
111 /* Free key input queue. */
112 if (wp->i_event != NULL)
113 free(wp->i_event);
115 /* Free cut buffers. */
116 cut_close(wp);
118 /* Free default buffer storage. */
119 (void)text_lfree(&wp->dcb_store.textq);
121 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
122 /* Free any temporary space. */
123 if (wp->tmp_bp != NULL)
124 free(wp->tmp_bp);
125 #endif
127 return 0;
131 * gs_end --
132 * End the program, discarding screens and most of the global area.
134 * PUBLIC: void gs_end __P((GS *));
136 void
137 gs_end(gp)
138 GS *gp;
140 MSGS *mp;
141 SCR *sp;
142 WIN *wp;
144 /* If there are any remaining screens, kill them off. */
145 if (gp->ccl_sp != NULL) {
146 (void)file_end(gp->ccl_sp, NULL, 1);
147 (void)screen_end(gp->ccl_sp);
149 while ((wp = gp->dq.cqh_first) != (void *)&gp->dq)
150 (void)win_end(wp);
151 while ((sp = gp->hq.cqh_first) != (void *)&gp->hq)
152 (void)screen_end(sp);
154 #ifdef HAVE_PERL_INTERP
155 perl_end(gp);
156 #endif
158 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
159 { FREF *frp;
160 /* Free FREF's. */
161 while ((frp = gp->frefq.cqh_first) != (FREF *)&gp->frefq) {
162 CIRCLEQ_REMOVE(&gp->frefq, frp, q);
163 if (frp->name != NULL)
164 free(frp->name);
165 if (frp->tname != NULL)
166 free(frp->tname);
167 free(frp);
171 /* Free map sequences. */
172 seq_close(gp);
174 /* Close message catalogs. */
175 msg_close(gp);
176 #endif
177 if (gp->env) {
178 gp->env->remove(gp->env, NULL, 0);
180 gp->env->close(gp->env, 0);
184 /* Ring the bell if scheduled. */
185 if (F_ISSET(gp, G_BELLSCHED))
186 (void)fprintf(stderr, "\07"); /* \a */
189 * Flush any remaining messages. If a message is here, it's almost
190 * certainly the message about the event that killed us (although
191 * it's possible that the user is sourcing a file that exits from the
192 * editor).
194 while ((mp = gp->msgq.lh_first) != NULL) {
195 (void)fprintf(stderr, "%s%.*s",
196 mp->mtype == M_ERR ? "ex/vi: " : "", (int)mp->len, mp->buf);
197 LIST_REMOVE(mp, q);
198 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
199 free(mp->buf);
200 free(mp);
201 #endif
204 #if defined(TRACE)
205 /* Close tracing file descriptor. */
206 vtrace_end();
207 #endif
212 * perr --
213 * Print system error.
215 static void
216 perr(name, msg)
217 char *name, *msg;
219 (void)fprintf(stderr, "%s:", name);
220 if (msg != NULL)
221 (void)fprintf(stderr, "%s:", msg);
222 (void)fprintf(stderr, "%s\n", strerror(errno));
223 exit(1);