xref: /openbsd/usr.bin/vi/ex/ex_util.c (revision 4cfece93)
1 /*	$OpenBSD: ex_util.c,v 1.9 2016/01/06 22:28:52 millert Exp $	*/
2 
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.
8  *
9  * See the LICENSE file for redistribution information.
10  */
11 
12 #include "config.h"
13 
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #include <sys/stat.h>
17 
18 #include <bitstring.h>
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include "../common/common.h"
27 
28 /*
29  * ex_cinit --
30  *	Create an EX command structure.
31  *
32  * PUBLIC: void ex_cinit(EXCMD *, int, int, recno_t, recno_t, int, ARGS **);
33  */
34 void
35 ex_cinit(EXCMD *cmdp, int cmd_id, int naddr, recno_t lno1, recno_t lno2,
36     int force, ARGS **ap)
37 {
38 	memset(cmdp, 0, sizeof(EXCMD));
39 	cmdp->cmd = &cmds[cmd_id];
40 	cmdp->addrcnt = naddr;
41 	cmdp->addr1.lno = lno1;
42 	cmdp->addr2.lno = lno2;
43 	cmdp->addr1.cno = cmdp->addr2.cno = 1;
44 	if (force)
45 		cmdp->iflags |= E_C_FORCE;
46 	cmdp->argc = 0;
47 	if ((cmdp->argv = ap) != NULL)
48 		cmdp->argv[0] = NULL;
49 }
50 
51 /*
52  * ex_cadd --
53  *	Add an argument to an EX command structure.
54  *
55  * PUBLIC: void ex_cadd(EXCMD *, ARGS *, char *, size_t);
56  */
57 void
58 ex_cadd(EXCMD *cmdp, ARGS *ap, char *arg, size_t len)
59 {
60 	cmdp->argv[cmdp->argc] = ap;
61 	ap->bp = arg;
62 	ap->len = len;
63 	cmdp->argv[++cmdp->argc] = NULL;
64 }
65 
66 /*
67  * ex_getline --
68  *	Return a line from the file.
69  *
70  * PUBLIC: int ex_getline(SCR *, FILE *, size_t *);
71  */
72 int
73 ex_getline(SCR *sp, FILE *fp, size_t *lenp)
74 {
75 	EX_PRIVATE *exp;
76 	size_t off;
77 	int ch;
78 	char *p;
79 
80 	exp = EXP(sp);
81 	for (errno = 0, off = 0, p = exp->ibp;;) {
82 		if (off >= exp->ibp_len) {
83 			BINC_RET(sp, exp->ibp, exp->ibp_len, off + 1);
84 			p = exp->ibp + off;
85 		}
86 		if ((ch = getc(fp)) == EOF && !feof(fp)) {
87 			if (errno == EINTR) {
88 				errno = 0;
89 				clearerr(fp);
90 				continue;
91 			}
92 			return (1);
93 		}
94 		if (ch == EOF || ch == '\n') {
95 			if (ch == EOF && !off)
96 				return (1);
97 			*lenp = off;
98 			return (0);
99 		}
100 		*p++ = ch;
101 		++off;
102 	}
103 	/* NOTREACHED */
104 }
105 
106 /*
107  * ex_ncheck --
108  *	Check for more files to edit.
109  *
110  * PUBLIC: int ex_ncheck(SCR *, int);
111  */
112 int
113 ex_ncheck(SCR *sp, int force)
114 {
115 	char **ap;
116 
117 	/*
118 	 * !!!
119 	 * Historic practice: quit! or two quit's done in succession
120 	 * (where ZZ counts as a quit) didn't check for other files.
121 	 */
122 	if (!force && sp->ccnt != sp->q_ccnt + 1 &&
123 	    sp->cargv != NULL && sp->cargv[1] != NULL) {
124 		sp->q_ccnt = sp->ccnt;
125 
126 		for (ap = sp->cargv + 1; *ap != NULL; ++ap);
127 		msgq(sp, M_ERR,
128 		    "%d more files to edit", (ap - sp->cargv) - 1);
129 
130 		return (1);
131 	}
132 	return (0);
133 }
134 
135 /*
136  * ex_init --
137  *	Init the screen for ex.
138  *
139  * PUBLIC: int ex_init(SCR *);
140  */
141 int
142 ex_init(SCR *sp)
143 {
144 	GS *gp;
145 
146 	gp = sp->gp;
147 
148 	if (gp->scr_screen(sp, SC_EX))
149 		return (1);
150 	(void)gp->scr_attr(sp, SA_ALTERNATE, 0);
151 
152 	sp->rows = O_VAL(sp, O_LINES);
153 	sp->cols = O_VAL(sp, O_COLUMNS);
154 
155 	F_CLR(sp, SC_VI);
156 	F_SET(sp, SC_EX | SC_SCR_EX);
157 	return (0);
158 }
159 
160 /*
161  * ex_emsg --
162  *	Display a few common ex and vi error messages.
163  *
164  * PUBLIC: void ex_emsg(SCR *, char *, exm_t);
165  */
166 void
167 ex_emsg(SCR *sp, char *p, exm_t which)
168 {
169 	switch (which) {
170 	case EXM_EMPTYBUF:
171 		msgq(sp, M_ERR, "Buffer %s is empty", p);
172 		break;
173 	case EXM_FILECOUNT:
174 		 msgq_str(sp, M_ERR, p,
175 		     "%s: expanded into too many file names");
176 		break;
177 	case EXM_NOCANON:
178 		msgq(sp, M_ERR,
179 		    "The %s command requires the ex terminal interface", p);
180 		break;
181 	case EXM_NOCANON_F:
182 		msgq(sp, M_ERR,
183 		    "That form of %s requires the ex terminal interface",
184 		    p);
185 		break;
186 	case EXM_NOFILEYET:
187 		if (p == NULL)
188 			msgq(sp, M_ERR,
189 			    "Command failed, no file read in yet.");
190 		else
191 			msgq(sp, M_ERR,
192 	"The %s command requires that a file have already been read in", p);
193 		break;
194 	case EXM_NOPREVBUF:
195 		msgq(sp, M_ERR, "No previous buffer to execute");
196 		break;
197 	case EXM_NOPREVRE:
198 		msgq(sp, M_ERR, "No previous regular expression");
199 		break;
200 	case EXM_NOSUSPEND:
201 		msgq(sp, M_ERR, "This screen may not be suspended");
202 		break;
203 	case EXM_SECURE:
204 		msgq(sp, M_ERR,
205 "The %s command is not supported when the secure edit option is set", p);
206 		break;
207 	case EXM_SECURE_F:
208 		msgq(sp, M_ERR,
209 "That form of %s is not supported when the secure edit option is set", p);
210 		break;
211 	case EXM_USAGE:
212 		msgq(sp, M_ERR, "Usage: %s", p);
213 		break;
214 	}
215 }
216