Import nvi2 version 2.1.3 to vendor branch
[dragonfly.git] / contrib / nvi2 / ex / ex_util.c
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.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9
10 #include "config.h"
11
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_util.c,v 10.32 2001/06/25 15:19:21 skimo Exp $";
14 #endif /* not lint */
15
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19
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>
27
28 #include "../common/common.h"
29
30 /*
31  * ex_cinit --
32  *      Create an EX command structure.
33  *
34  * PUBLIC: void ex_cinit(SCR *, EXCMD *, int, int, recno_t, recno_t, int);
35  */
36 void
37 ex_cinit(SCR *sp, EXCMD *cmdp, int cmd_id, int naddr, recno_t lno1, recno_t lno2, int force)
38 {
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);
48 }
49
50 /*
51  * ex_getline --
52  *      Return a line from the file.
53  *
54  * PUBLIC: int ex_getline(SCR *, FILE *, size_t *);
55  */
56 int
57 ex_getline(SCR *sp, FILE *fp, size_t *lenp)
58 {
59         EX_PRIVATE *exp;
60         size_t off;
61         int ch;
62         char *p;
63
64         exp = EXP(sp);
65         for (errno = 0, off = 0, p = exp->ibp;;) {
66                 if (off >= exp->ibp_len) {
67                         BINC_RETC(sp, exp->ibp, exp->ibp_len, off + 1);
68                         p = exp->ibp + off;
69                 }
70                 if ((ch = getc(fp)) == EOF && !feof(fp)) {
71                         if (errno == EINTR) {
72                                 errno = 0;
73                                 clearerr(fp);
74                                 continue;
75                         }
76                         return (1);
77                 }
78                 if (ch == EOF || ch == '\n') {
79                         if (ch == EOF && !off)
80                                 return (1);
81                         *lenp = off;
82                         return (0);
83                 }
84                 *p++ = ch;
85                 ++off;
86         }
87         /* NOTREACHED */
88 }
89
90 /*
91  * ex_ncheck --
92  *      Check for more files to edit.
93  *
94  * PUBLIC: int ex_ncheck(SCR *, int);
95  */
96 int
97 ex_ncheck(SCR *sp, int force)
98 {
99         char **ap;
100
101         /*
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.
105          */
106         if (!force && sp->ccnt != sp->q_ccnt + 1 &&
107             sp->cargv != NULL && sp->cargv[1] != NULL) {
108                 sp->q_ccnt = sp->ccnt;
109
110                 for (ap = sp->cargv + 1; *ap != NULL; ++ap);
111                 msgq(sp, M_ERR,
112                     "167|%d more files to edit", (int)(ap - sp->cargv) - 1);
113
114                 return (1);
115         }
116         return (0);
117 }
118
119 /*
120  * ex_init --
121  *      Init the screen for ex.
122  *
123  * PUBLIC: int ex_init(SCR *);
124  */
125 int
126 ex_init(SCR *sp)
127 {
128         GS *gp;
129
130         gp = sp->gp;
131
132         if (gp->scr_screen(sp, SC_EX))
133                 return (1);
134         (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
135
136         sp->rows = O_VAL(sp, O_LINES);
137         sp->cols = O_VAL(sp, O_COLUMNS);
138
139         F_CLR(sp, SC_VI);
140         F_SET(sp, SC_EX | SC_SCR_EX);
141         return (0);
142 }
143
144 /*
145  * ex_emsg --
146  *      Display a few common ex and vi error messages.
147  *
148  * PUBLIC: void ex_wemsg(SCR *, CHAR_T *, exm_t);
149  */
150 void
151 ex_wemsg(SCR* sp, CHAR_T *p, exm_t which)
152 {
153         char *np;
154         size_t nlen;
155
156         if (p) INT2CHAR(sp, p, STRLEN(p), np, nlen);
157         else np = NULL;
158         ex_emsg(sp, np, which);
159 }
160
161 /*
162  * ex_emsg --
163  *      Display a few common ex and vi error messages.
164  *
165  * PUBLIC: void ex_emsg(SCR *, char *, exm_t);
166  */
167 void
168 ex_emsg(SCR *sp, char *p, exm_t which)
169 {
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_NOCANON:
179                 msgq(sp, M_ERR,
180                     "283|The %s command requires the ex terminal interface", p);
181                 break;
182         case EXM_NOCANON_F:
183                 msgq(sp, M_ERR,
184                     "272|That form of %s requires the ex terminal interface",
185                     p);
186                 break;
187         case EXM_NOFILEYET:
188                 if (p == NULL)
189                         msgq(sp, M_ERR,
190                             "274|Command failed, no file read in yet.");
191                 else
192                         msgq(sp, M_ERR,
193         "173|The %s command requires that a file have already been read in", p);
194                 break;
195         case EXM_NOPREVBUF:
196                 msgq(sp, M_ERR, "171|No previous buffer to execute");
197                 break;
198         case EXM_NOPREVRE:
199                 msgq(sp, M_ERR, "172|No previous regular expression");
200                 break;
201         case EXM_NOSUSPEND:
202                 msgq(sp, M_ERR, "230|This screen may not be suspended");
203                 break;
204         case EXM_SECURE:
205                 msgq(sp, M_ERR,
206 "290|The %s command is not supported when the secure edit option is set", p);
207                 break;
208         case EXM_SECURE_F:
209                 msgq(sp, M_ERR,
210 "284|That form of %s is not supported when the secure edit option is set", p);
211                 break;
212         case EXM_USAGE:
213                 msgq(sp, M_ERR, "174|Usage: %s", p);
214                 break;
215         }
216 }