only clean out gs when last window goes
[nvi.git] / ex / ex_util.c
blobd1728dd2db53c8fabf75e10ec7f17bfcc46f1cee
1 /*-
2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_util.c,v 10.26 2000/04/21 19:00:38 skimo Exp $ (Berkeley) $Date: 2000/04/21 19:00:38 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <bitstring.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "../common/common.h"
31 * ex_cinit --
32 * Create an EX command structure.
34 * PUBLIC: void ex_cinit __P((SCR *, EXCMD *, int, int, db_recno_t, db_recno_t, int));
36 void
37 ex_cinit(sp, cmdp, cmd_id, naddr, lno1, lno2, force)
38 SCR *sp;
39 EXCMD *cmdp;
40 int cmd_id, force, naddr;
41 db_recno_t lno1, lno2;
43 memset(cmdp, 0, sizeof(EXCMD));
44 cmdp->cmd = &cmds[cmd_id];
45 cmdp->addrcnt = naddr;
46 cmdp->addr1.lno = lno1;
47 cmdp->addr2.lno = lno2;
48 cmdp->addr1.cno = cmdp->addr2.cno = 1;
49 if (force)
50 cmdp->iflags |= E_C_FORCE;
51 (void)argv_init(sp, cmdp);
55 * ex_getline --
56 * Return a line from the file.
58 * PUBLIC: int ex_getline __P((SCR *, FILE *, size_t *));
60 int
61 ex_getline(sp, fp, lenp)
62 SCR *sp;
63 FILE *fp;
64 size_t *lenp;
66 EX_PRIVATE *exp;
67 size_t off;
68 int ch;
69 char *p;
71 exp = EXP(sp);
72 for (errno = 0, off = 0, p = exp->ibp;;) {
73 if (off >= exp->ibp_len) {
74 BINC_RET(sp, exp->ibp, exp->ibp_len, off + 1);
75 p = exp->ibp + off;
77 if ((ch = getc(fp)) == EOF && !feof(fp)) {
78 if (errno == EINTR) {
79 errno = 0;
80 clearerr(fp);
81 continue;
83 return (1);
85 if (ch == EOF || ch == '\n') {
86 if (ch == EOF && !off)
87 return (1);
88 *lenp = off;
89 return (0);
91 *p++ = ch;
92 ++off;
94 /* NOTREACHED */
98 * ex_ncheck --
99 * Check for more files to edit.
101 * PUBLIC: int ex_ncheck __P((SCR *, int));
104 ex_ncheck(sp, force)
105 SCR *sp;
106 int force;
108 char **ap;
111 * !!!
112 * Historic practice: quit! or two quit's done in succession
113 * (where ZZ counts as a quit) didn't check for other files.
115 if (!force && sp->ccnt != sp->q_ccnt + 1 &&
116 sp->cargv != NULL && sp->cargv[1] != NULL) {
117 sp->q_ccnt = sp->ccnt;
119 for (ap = sp->cargv + 1; *ap != NULL; ++ap);
120 msgq(sp, M_ERR,
121 "167|%d more files to edit", (ap - sp->cargv) - 1);
123 return (1);
125 return (0);
129 * ex_init --
130 * Init the screen for ex.
132 * PUBLIC: int ex_init __P((SCR *));
135 ex_init(sp)
136 SCR *sp;
138 GS *gp;
140 gp = sp->gp;
142 if (gp->scr_screen(sp, SC_EX))
143 return (1);
144 (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
146 sp->rows = O_VAL(sp, O_LINES);
147 sp->cols = O_VAL(sp, O_COLUMNS);
149 F_CLR(sp, SC_VI);
150 F_SET(sp, SC_EX | SC_SCR_EX);
151 return (0);
155 * ex_emsg --
156 * Display a few common ex and vi error messages.
158 * PUBLIC: void ex_emsg __P((SCR *, char *, exm_t));
160 void
161 ex_emsg(sp, p, which)
162 SCR *sp;
163 char *p;
164 exm_t which;
166 switch (which) {
167 case EXM_EMPTYBUF:
168 msgq(sp, M_ERR, "168|Buffer %s is empty", p);
169 break;
170 case EXM_FILECOUNT:
171 msgq_str(sp, M_ERR, p,
172 "144|%s: expanded into too many file names");
173 break;
174 case EXM_NOCANON:
175 msgq(sp, M_ERR,
176 "283|The %s command requires the ex terminal interface", p);
177 break;
178 case EXM_NOCANON_F:
179 msgq(sp, M_ERR,
180 "272|That form of %s requires the ex terminal interface",
182 break;
183 case EXM_NOFILEYET:
184 if (p == NULL)
185 msgq(sp, M_ERR,
186 "274|Command failed, no file read in yet.");
187 else
188 msgq(sp, M_ERR,
189 "173|The %s command requires that a file have already been read in", p);
190 break;
191 case EXM_NOPREVBUF:
192 msgq(sp, M_ERR, "171|No previous buffer to execute");
193 break;
194 case EXM_NOPREVRE:
195 msgq(sp, M_ERR, "172|No previous regular expression");
196 break;
197 case EXM_NOSUSPEND:
198 msgq(sp, M_ERR, "230|This screen may not be suspended");
199 break;
200 case EXM_SECURE:
201 msgq(sp, M_ERR,
202 "290|The %s command is not supported when the secure edit option is set", p);
203 break;
204 case EXM_SECURE_F:
205 msgq(sp, M_ERR,
206 "284|That form of %s is not supported when the secure edit option is set", p);
207 break;
208 case EXM_USAGE:
209 msgq(sp, M_ERR, "174|Usage: %s", p);
210 break;