private version of automake no longer needed
[nvi.git] / ex / ex_util.c
blobaf57d7c9d9bec3036ecf80f92613243d1d627896
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.31 2001/06/10 10:23:45 skimo Exp $ (Berkeley) $Date: 2001/06/10 10:23:45 $";
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_T *p;
71 exp = EXP(sp);
72 for (errno = 0, off = 0, p = exp->ibp;;) {
73 if (off * sizeof(CHAR_T) >= exp->ibp_len) {
74 BINC_RETW(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_wemsg __P((SCR *, CHAR_T *, exm_t));
160 void
161 ex_wemsg(SCR* sp, CHAR_T *p, exm_t which)
163 char *np;
164 size_t nlen;
166 if (p) INT2CHAR(sp, p, STRLEN(p), np, nlen);
167 else np = NULL;
168 ex_emsg(sp, np, which);
172 * ex_emsg --
173 * Display a few common ex and vi error messages.
175 * PUBLIC: void ex_emsg __P((SCR *, char *, exm_t));
177 void
178 ex_emsg(sp, p, which)
179 SCR *sp;
180 char *p;
181 exm_t which;
183 switch (which) {
184 case EXM_EMPTYBUF:
185 msgq(sp, M_ERR, "168|Buffer %s is empty", p);
186 break;
187 case EXM_FILECOUNT:
188 msgq_str(sp, M_ERR, p,
189 "144|%s: expanded into too many file names");
190 break;
191 case EXM_LOCKED:
192 msgq(sp, M_ERR,
193 "Command failed, buffer is locked");
194 break;
195 case EXM_NOCANON:
196 msgq(sp, M_ERR,
197 "283|The %s command requires the ex terminal interface", p);
198 break;
199 case EXM_NOCANON_F:
200 msgq(sp, M_ERR,
201 "272|That form of %s requires the ex terminal interface",
203 break;
204 case EXM_NOFILEYET:
205 if (p == NULL)
206 msgq(sp, M_ERR,
207 "274|Command failed, no file read in yet.");
208 else
209 msgq(sp, M_ERR,
210 "173|The %s command requires that a file have already been read in", p);
211 break;
212 case EXM_NOPREVBUF:
213 msgq(sp, M_ERR, "171|No previous buffer to execute");
214 break;
215 case EXM_NOPREVRE:
216 msgq(sp, M_ERR, "172|No previous regular expression");
217 break;
218 case EXM_NOSUSPEND:
219 msgq(sp, M_ERR, "230|This screen may not be suspended");
220 break;
221 case EXM_SECURE:
222 msgq(sp, M_ERR,
223 "290|The %s command is not supported when the secure edit option is set", p);
224 break;
225 case EXM_SECURE_F:
226 msgq(sp, M_ERR,
227 "284|That form of %s is not supported when the secure edit option is set", p);
228 break;
229 case EXM_USAGE:
230 msgq(sp, M_ERR, "174|Usage: %s", p);
231 break;