ifconfig(8): Fix IPv6 CIDR parsing error for wgaip (wg allowed-ip)
[dragonfly.git] / contrib / nvi2 / ex / ex_util.c
blobf3aee410f8ad9d8438a6f3d8fcfe0aa5cb7a19d6
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 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/stat.h>
16 #include <bitstring.h>
17 #include <errno.h>
18 #include <limits.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
24 #include "../common/common.h"
27 * ex_cinit --
28 * Create an EX command structure.
30 * PUBLIC: void ex_cinit(SCR *, EXCMD *, int, int, recno_t, recno_t, int);
32 void
33 ex_cinit(SCR *sp, EXCMD *cmdp, int cmd_id, int naddr, recno_t lno1, recno_t lno2, int force)
35 memset(cmdp, 0, sizeof(EXCMD));
36 cmdp->cmd = &cmds[cmd_id];
37 cmdp->addrcnt = naddr;
38 cmdp->addr1.lno = lno1;
39 cmdp->addr2.lno = lno2;
40 cmdp->addr1.cno = cmdp->addr2.cno = 1;
41 if (force)
42 cmdp->iflags |= E_C_FORCE;
43 (void)argv_init(sp, cmdp);
47 * ex_getline --
48 * Return a line from the file.
50 * PUBLIC: int ex_getline(SCR *, FILE *, size_t *);
52 int
53 ex_getline(SCR *sp, FILE *fp, size_t *lenp)
55 EX_PRIVATE *exp;
56 size_t off;
57 int ch;
58 char *p;
60 exp = EXP(sp);
61 for (errno = 0, off = 0, p = exp->ibp;;) {
62 if (off >= exp->ibp_len) {
63 BINC_RETC(sp, exp->ibp, exp->ibp_len, off + 1);
64 p = exp->ibp + off;
66 if ((ch = getc(fp)) == EOF && !feof(fp)) {
67 if (errno == EINTR) {
68 errno = 0;
69 clearerr(fp);
70 continue;
72 return (1);
74 if (ch == EOF || ch == '\n') {
75 if (ch == EOF && !off)
76 return (1);
77 *lenp = off;
78 return (0);
80 *p++ = ch;
81 ++off;
83 /* NOTREACHED */
87 * ex_ncheck --
88 * Check for more files to edit.
90 * PUBLIC: int ex_ncheck(SCR *, int);
92 int
93 ex_ncheck(SCR *sp, int force)
95 char **ap;
98 * !!!
99 * Historic practice: quit! or two quit's done in succession
100 * (where ZZ counts as a quit) didn't check for other files.
102 if (!force && sp->ccnt != sp->q_ccnt + 1 &&
103 sp->cargv != NULL && sp->cargv[1] != NULL) {
104 sp->q_ccnt = sp->ccnt;
106 for (ap = sp->cargv + 1; *ap != NULL; ++ap);
107 msgq(sp, M_ERR,
108 "167|%d more files to edit", (int)(ap - sp->cargv) - 1);
110 return (1);
112 return (0);
116 * ex_init --
117 * Init the screen for ex.
119 * PUBLIC: int ex_init(SCR *);
122 ex_init(SCR *sp)
124 GS *gp;
126 gp = sp->gp;
128 if (gp->scr_screen(sp, SC_EX))
129 return (1);
130 (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
132 sp->rows = O_VAL(sp, O_LINES);
133 sp->cols = O_VAL(sp, O_COLUMNS);
135 F_CLR(sp, SC_VI);
136 F_SET(sp, SC_EX | SC_SCR_EX);
137 return (0);
141 * ex_emsg --
142 * Display a few common ex and vi error messages.
144 * PUBLIC: void ex_wemsg(SCR *, CHAR_T *, exm_t);
146 void
147 ex_wemsg(SCR* sp, CHAR_T *p, exm_t which)
149 char *np;
150 size_t nlen;
152 if (p) INT2CHAR(sp, p, STRLEN(p), np, nlen);
153 else np = NULL;
154 ex_emsg(sp, np, which);
158 * ex_emsg --
159 * Display a few common ex and vi error messages.
161 * PUBLIC: void ex_emsg(SCR *, char *, exm_t);
163 void
164 ex_emsg(SCR *sp, char *p, 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;