3 * Sven Verdoolaege. All rights reserved.
5 * See the LICENSE file for redistribution information.
10 #include <sys/types.h>
11 #include <sys/queue.h>
13 #include <bitstring.h>
21 #include "../common/common.h"
23 static void perr
__P((char *, char *));
27 * Create and partially initialize the GS structure.
28 * PUBLIC: GS * gs_init __P((char*));
36 /* Figure out what our name is. */
37 if ((p
= strrchr(name
, '/')) != NULL
)
40 /* Allocate the global structure. */
41 CALLOC_NOMSG(NULL
, gp
, GS
*, 1, sizeof(GS
));
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
);
67 * PUBLIC: WIN * gs_new_win __P((GS *gp));
75 CALLOC_NOMSG(NULL
, wp
, WIN
*, 1, sizeof(*wp
));
79 /* Common global structure initialization. */
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
);
98 * PUBLIC: int win_end __P((WIN *wp));
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
)
118 /* Free cut buffers. */
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
)
135 * End the program, discarding screens and most of the global area.
137 * PUBLIC: void gs_end __P((GS *));
146 /* If there are any remaining screens, kill them off. */
147 while ((wp
= gp
->dq
.cqh_first
) != (void *)&gp
->dq
)
149 while ((sp
= gp
->hq
.cqh_first
) != (void *)&gp
->hq
)
150 (void)screen_end(sp
);
152 #ifdef HAVE_PERL_INTERP
156 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
159 while ((frp
= gp
->frefq
.cqh_first
) != (FREF
*)&gp
->frefq
) {
160 CIRCLEQ_REMOVE(&gp
->frefq
, frp
, q
);
161 if (frp
->name
!= NULL
)
163 if (frp
->tname
!= NULL
)
169 /* Free map sequences. */
172 /* Close message catalogs. */
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
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
);
196 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
203 /* Close tracing file descriptor. */
211 * Print system error.
214 perr(char *name
, char *msg
)
216 (void)fprintf(stderr
, "%s:", name
);
218 (void)fprintf(stderr
, "%s:", msg
);
219 (void)fprintf(stderr
, "%s\n", strerror(errno
));