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