Sync usage with man page.
[netbsd-mini2440.git] / dist / nvi / ex / ex_util.c
blob33f35ac7e6bda4abfc379b6c23b2b3b93b6f002e
1 /* $NetBSD: ex_util.c,v 1.2 2008/12/05 22:51:42 christos Exp $ */
3 /*-
4 * Copyright (c) 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/stat.h>
22 #include <bitstring.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "../common/common.h"
33 * ex_cinit --
34 * Create an EX command structure.
36 * PUBLIC: void ex_cinit __P((SCR *, EXCMD *, int, int, db_recno_t, db_recno_t, int));
38 void
39 ex_cinit(SCR *sp, EXCMD *cmdp, int cmd_id, int naddr, db_recno_t lno1, db_recno_t lno2, int force)
41 memset(cmdp, 0, sizeof(EXCMD));
42 cmdp->cmd = &cmds[cmd_id];
43 cmdp->addrcnt = naddr;
44 cmdp->addr1.lno = lno1;
45 cmdp->addr2.lno = lno2;
46 cmdp->addr1.cno = cmdp->addr2.cno = 1;
47 if (force)
48 cmdp->iflags |= E_C_FORCE;
49 (void)argv_init(sp, cmdp);
53 * ex_getline --
54 * Return a line from the file.
56 * PUBLIC: int ex_getline __P((SCR *, FILE *, size_t *));
58 int
59 ex_getline(SCR *sp, FILE *fp, size_t *lenp)
61 EX_PRIVATE *exp;
62 size_t off;
63 int ch;
64 char *p;
66 exp = EXP(sp);
67 for (errno = 0, off = 0, p = exp->ibp;;) {
68 if (off >= exp->ibp_len) {
69 BINC_RETC(sp, exp->ibp, exp->ibp_len, off + 1);
70 p = exp->ibp + off;
72 if ((ch = getc(fp)) == EOF && !feof(fp)) {
73 if (errno == EINTR) {
74 errno = 0;
75 clearerr(fp);
76 continue;
78 return (1);
80 if (ch == EOF || ch == '\n') {
81 if (ch == EOF && !off)
82 return (1);
83 *lenp = off;
84 return (0);
86 *p++ = ch;
87 ++off;
89 /* NOTREACHED */
93 * ex_ncheck --
94 * Check for more files to edit.
96 * PUBLIC: int ex_ncheck __P((SCR *, int));
98 int
99 ex_ncheck(SCR *sp, int force)
101 char **ap;
104 * !!!
105 * Historic practice: quit! or two quit's done in succession
106 * (where ZZ counts as a quit) didn't check for other files.
108 if (!force && sp->ccnt != sp->q_ccnt + 1 &&
109 sp->cargv != NULL && sp->cargv[1] != NULL) {
110 sp->q_ccnt = sp->ccnt;
112 for (ap = sp->cargv + 1; *ap != NULL; ++ap);
113 msgq(sp, M_ERR,
114 "167|%zd more files to edit", (ap - sp->cargv) - 1);
116 return (1);
118 return (0);
122 * ex_init --
123 * Init the screen for ex.
125 * PUBLIC: int ex_init __P((SCR *));
128 ex_init(SCR *sp)
130 GS *gp;
132 gp = sp->gp;
134 if (gp->scr_screen(sp, SC_EX))
135 return (1);
136 (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
138 sp->rows = O_VAL(sp, O_LINES);
139 sp->cols = O_VAL(sp, O_COLUMNS);
141 F_CLR(sp, SC_VI);
142 F_SET(sp, SC_EX | SC_SCR_EX);
143 return (0);
147 * ex_emsg --
148 * Display a few common ex and vi error messages.
150 * PUBLIC: void ex_wemsg __P((SCR *, CHAR_T *, exm_t));
152 void
153 ex_wemsg(SCR* sp, const CHAR_T *p, exm_t which)
155 const char *np;
156 size_t nlen;
158 if (p) INT2CHAR(sp, p, STRLEN(p), np, nlen);
159 else np = NULL;
160 ex_emsg(sp, np, which);
164 * ex_emsg --
165 * Display a few common ex and vi error messages.
167 * PUBLIC: void ex_emsg __P((SCR *, char *, exm_t));
169 void
170 ex_emsg(SCR *sp, const char *p, exm_t which)
172 switch (which) {
173 case EXM_EMPTYBUF:
174 msgq(sp, M_ERR, "168|Buffer %s is empty", p);
175 break;
176 case EXM_FILECOUNT:
177 msgq_str(sp, M_ERR, p,
178 "144|%s: expanded into too many file names");
179 break;
180 case EXM_LOCKED:
181 msgq(sp, M_ERR,
182 "Command failed, buffer is locked");
183 break;
184 case EXM_NOCANON:
185 msgq(sp, M_ERR,
186 "283|The %s command requires the ex terminal interface", p);
187 break;
188 case EXM_NOCANON_F:
189 msgq(sp, M_ERR,
190 "272|That form of %s requires the ex terminal interface",
192 break;
193 case EXM_NOFILEYET:
194 if (p == NULL)
195 msgq(sp, M_ERR,
196 "274|Command failed, no file read in yet.");
197 else
198 msgq(sp, M_ERR,
199 "173|The %s command requires that a file have already been read in", p);
200 break;
201 case EXM_NOPREVBUF:
202 msgq(sp, M_ERR, "171|No previous buffer to execute");
203 break;
204 case EXM_NOPREVRE:
205 msgq(sp, M_ERR, "172|No previous regular expression");
206 break;
207 case EXM_NOSUSPEND:
208 msgq(sp, M_ERR, "230|This screen may not be suspended");
209 break;
210 case EXM_SECURE:
211 msgq(sp, M_ERR,
212 "290|The %s command is not supported when the secure edit option is set", p);
213 break;
214 case EXM_SECURE_F:
215 msgq(sp, M_ERR,
216 "284|That form of %s is not supported when the secure edit option is set", p);
217 break;
218 case EXM_USAGE:
219 msgq(sp, M_ERR, "174|Usage: %s", p);
220 break;