protoize most of the function prototypes
[nvi.git] / ex / ex_util.c
blob3410a3e8ba2aa6fca6062442d3b044e9f15238f1
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.32 2001/06/25 15:19:21 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:21 $";
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(SCR *sp, EXCMD *cmdp, int cmd_id, int naddr, db_recno_t lno1, db_recno_t lno2, int force)
39 memset(cmdp, 0, sizeof(EXCMD));
40 cmdp->cmd = &cmds[cmd_id];
41 cmdp->addrcnt = naddr;
42 cmdp->addr1.lno = lno1;
43 cmdp->addr2.lno = lno2;
44 cmdp->addr1.cno = cmdp->addr2.cno = 1;
45 if (force)
46 cmdp->iflags |= E_C_FORCE;
47 (void)argv_init(sp, cmdp);
51 * ex_getline --
52 * Return a line from the file.
54 * PUBLIC: int ex_getline __P((SCR *, FILE *, size_t *));
56 int
57 ex_getline(SCR *sp, FILE *fp, size_t *lenp)
59 EX_PRIVATE *exp;
60 size_t off;
61 int ch;
62 CHAR_T *p;
64 exp = EXP(sp);
65 for (errno = 0, off = 0, p = exp->ibp;;) {
66 if (off * sizeof(CHAR_T) >= exp->ibp_len) {
67 BINC_RETW(sp, exp->ibp, exp->ibp_len, off + 1);
68 p = exp->ibp + off;
70 if ((ch = getc(fp)) == EOF && !feof(fp)) {
71 if (errno == EINTR) {
72 errno = 0;
73 clearerr(fp);
74 continue;
76 return (1);
78 if (ch == EOF || ch == '\n') {
79 if (ch == EOF && !off)
80 return (1);
81 *lenp = off;
82 return (0);
84 *p++ = ch;
85 ++off;
87 /* NOTREACHED */
91 * ex_ncheck --
92 * Check for more files to edit.
94 * PUBLIC: int ex_ncheck __P((SCR *, int));
96 int
97 ex_ncheck(SCR *sp, int force)
99 char **ap;
102 * !!!
103 * Historic practice: quit! or two quit's done in succession
104 * (where ZZ counts as a quit) didn't check for other files.
106 if (!force && sp->ccnt != sp->q_ccnt + 1 &&
107 sp->cargv != NULL && sp->cargv[1] != NULL) {
108 sp->q_ccnt = sp->ccnt;
110 for (ap = sp->cargv + 1; *ap != NULL; ++ap);
111 msgq(sp, M_ERR,
112 "167|%d more files to edit", (ap - sp->cargv) - 1);
114 return (1);
116 return (0);
120 * ex_init --
121 * Init the screen for ex.
123 * PUBLIC: int ex_init __P((SCR *));
126 ex_init(SCR *sp)
128 GS *gp;
130 gp = sp->gp;
132 if (gp->scr_screen(sp, SC_EX))
133 return (1);
134 (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
136 sp->rows = O_VAL(sp, O_LINES);
137 sp->cols = O_VAL(sp, O_COLUMNS);
139 F_CLR(sp, SC_VI);
140 F_SET(sp, SC_EX | SC_SCR_EX);
141 return (0);
145 * ex_emsg --
146 * Display a few common ex and vi error messages.
148 * PUBLIC: void ex_wemsg __P((SCR *, CHAR_T *, exm_t));
150 void
151 ex_wemsg(SCR* sp, CHAR_T *p, exm_t which)
153 char *np;
154 size_t nlen;
156 if (p) INT2CHAR(sp, p, STRLEN(p), np, nlen);
157 else np = NULL;
158 ex_emsg(sp, np, which);
162 * ex_emsg --
163 * Display a few common ex and vi error messages.
165 * PUBLIC: void ex_emsg __P((SCR *, char *, exm_t));
167 void
168 ex_emsg(SCR *sp, char *p, exm_t which)
170 switch (which) {
171 case EXM_EMPTYBUF:
172 msgq(sp, M_ERR, "168|Buffer %s is empty", p);
173 break;
174 case EXM_FILECOUNT:
175 msgq_str(sp, M_ERR, p,
176 "144|%s: expanded into too many file names");
177 break;
178 case EXM_LOCKED:
179 msgq(sp, M_ERR,
180 "Command failed, buffer is locked");
181 break;
182 case EXM_NOCANON:
183 msgq(sp, M_ERR,
184 "283|The %s command requires the ex terminal interface", p);
185 break;
186 case EXM_NOCANON_F:
187 msgq(sp, M_ERR,
188 "272|That form of %s requires the ex terminal interface",
190 break;
191 case EXM_NOFILEYET:
192 if (p == NULL)
193 msgq(sp, M_ERR,
194 "274|Command failed, no file read in yet.");
195 else
196 msgq(sp, M_ERR,
197 "173|The %s command requires that a file have already been read in", p);
198 break;
199 case EXM_NOPREVBUF:
200 msgq(sp, M_ERR, "171|No previous buffer to execute");
201 break;
202 case EXM_NOPREVRE:
203 msgq(sp, M_ERR, "172|No previous regular expression");
204 break;
205 case EXM_NOSUSPEND:
206 msgq(sp, M_ERR, "230|This screen may not be suspended");
207 break;
208 case EXM_SECURE:
209 msgq(sp, M_ERR,
210 "290|The %s command is not supported when the secure edit option is set", p);
211 break;
212 case EXM_SECURE_F:
213 msgq(sp, M_ERR,
214 "284|That form of %s is not supported when the secure edit option is set", p);
215 break;
216 case EXM_USAGE:
217 msgq(sp, M_ERR, "174|Usage: %s", p);
218 break;