ex_util.c revision 19305
1205859Sjoel/*-
2162874Snetchild * Copyright (c) 1993, 1994
3159687Snetchild *	The Regents of the University of California.  All rights reserved.
4159687Snetchild * Copyright (c) 1993, 1994, 1995, 1996
5159687Snetchild *	Keith Bostic.  All rights reserved.
6159687Snetchild *
7159687Snetchild * See the LICENSE file for redistribution information.
8159687Snetchild */
9159687Snetchild
10159687Snetchild#include "config.h"
11159687Snetchild
12159687Snetchild#ifndef lint
13159687Snetchildstatic const char sccsid[] = "@(#)ex_util.c	10.23 (Berkeley) 6/19/96";
14159687Snetchild#endif /* not lint */
15159687Snetchild
16159687Snetchild#include <sys/types.h>
17159687Snetchild#include <sys/queue.h>
18159687Snetchild#include <sys/stat.h>
19159687Snetchild
20159687Snetchild#include <bitstring.h>
21159687Snetchild#include <errno.h>
22159687Snetchild#include <limits.h>
23159687Snetchild#include <stdio.h>
24159687Snetchild#include <stdlib.h>
25159687Snetchild#include <string.h>
26159687Snetchild#include <unistd.h>
27159687Snetchild
28159687Snetchild#include "../common/common.h"
29159687Snetchild
30159687Snetchild/*
31162874Snetchild * ex_cinit --
32162874Snetchild *	Create an EX command structure.
33162874Snetchild *
34162874Snetchild * PUBLIC: void ex_cinit __P((EXCMD *,
35162874Snetchild * PUBLIC:    int, int, recno_t, recno_t, int, ARGS **));
36170031Sjoel */
37159687Snetchildvoid
38159687Snetchildex_cinit(cmdp, cmd_id, naddr, lno1, lno2, force, ap)
39159687Snetchild	EXCMD *cmdp;
40159687Snetchild	int cmd_id, force, naddr;
41159687Snetchild	recno_t lno1, lno2;
42159687Snetchild	ARGS **ap;
43159687Snetchild{
44159687Snetchild	memset(cmdp, 0, sizeof(EXCMD));
45159687Snetchild	cmdp->cmd = &cmds[cmd_id];
46159687Snetchild	cmdp->addrcnt = naddr;
47159687Snetchild	cmdp->addr1.lno = lno1;
48159687Snetchild	cmdp->addr2.lno = lno2;
49159687Snetchild	cmdp->addr1.cno = cmdp->addr2.cno = 1;
50159687Snetchild	if (force)
51159687Snetchild		cmdp->iflags |= E_C_FORCE;
52159687Snetchild	cmdp->argc = 0;
53159687Snetchild	if ((cmdp->argv = ap) != NULL)
54159687Snetchild		cmdp->argv[0] = NULL;
55159687Snetchild}
56159687Snetchild
57159687Snetchild/*
58159687Snetchild * ex_cadd --
59159687Snetchild *	Add an argument to an EX command structure.
60159687Snetchild *
61159687Snetchild * PUBLIC: void ex_cadd __P((EXCMD *, ARGS *, char *, size_t));
62159687Snetchild */
63159687Snetchildvoid
64159687Snetchildex_cadd(cmdp, ap, arg, len)
65159687Snetchild	EXCMD *cmdp;
66159687Snetchild	ARGS *ap;
67159687Snetchild	char *arg;
68159687Snetchild	size_t len;
69159687Snetchild{
70159687Snetchild	cmdp->argv[cmdp->argc] = ap;
71159687Snetchild	ap->bp = arg;
72159687Snetchild	ap->len = len;
73159687Snetchild	cmdp->argv[++cmdp->argc] = NULL;
74159687Snetchild}
75159687Snetchild
76159687Snetchild/*
77159687Snetchild * ex_getline --
78159687Snetchild *	Return a line from the file.
79159687Snetchild *
80159687Snetchild * PUBLIC: int ex_getline __P((SCR *, FILE *, size_t *));
81159687Snetchild */
82159687Snetchildint
83159687Snetchildex_getline(sp, fp, lenp)
84162874Snetchild	SCR *sp;
85162874Snetchild	FILE *fp;
86162874Snetchild	size_t *lenp;
87162874Snetchild{
88162874Snetchild	EX_PRIVATE *exp;
89162874Snetchild	size_t off;
90162874Snetchild	int ch;
91162874Snetchild	char *p;
92162874Snetchild
93162874Snetchild	exp = EXP(sp);
94162874Snetchild	for (errno = 0, off = 0, p = exp->ibp;;) {
95159687Snetchild		if (off >= exp->ibp_len) {
96162874Snetchild			BINC_RET(sp, exp->ibp, exp->ibp_len, off + 1);
97162874Snetchild			p = exp->ibp + off;
98162874Snetchild		}
99162874Snetchild		if ((ch = getc(fp)) == EOF && !feof(fp)) {
100159687Snetchild			if (errno == EINTR) {
101162874Snetchild				errno = 0;
102162874Snetchild				clearerr(fp);
103162874Snetchild				continue;
104162874Snetchild			}
105170031Sjoel			return (1);
106170031Sjoel		}
107170031Sjoel		if (ch == EOF || ch == '\n') {
108170031Sjoel			if (ch == EOF && !off)
109162874Snetchild				return (1);
110162874Snetchild			*lenp = off;
111162874Snetchild			return (0);
112162874Snetchild		}
113162874Snetchild		*p++ = ch;
114162874Snetchild		++off;
115162874Snetchild	}
116162874Snetchild	/* NOTREACHED */
117162874Snetchild}
118162874Snetchild
119162874Snetchild/*
120162874Snetchild * ex_ncheck --
121162874Snetchild *	Check for more files to edit.
122 *
123 * PUBLIC: int ex_ncheck __P((SCR *, int));
124 */
125int
126ex_ncheck(sp, force)
127	SCR *sp;
128	int force;
129{
130	char **ap;
131
132	/*
133	 * !!!
134	 * Historic practice: quit! or two quit's done in succession
135	 * (where ZZ counts as a quit) didn't check for other files.
136	 */
137	if (!force && sp->ccnt != sp->q_ccnt + 1 &&
138	    sp->cargv != NULL && sp->cargv[1] != NULL) {
139		sp->q_ccnt = sp->ccnt;
140
141		for (ap = sp->cargv + 1; *ap != NULL; ++ap);
142		msgq(sp, M_ERR,
143		    "167|%d more files to edit", (ap - sp->cargv) - 1);
144
145		return (1);
146	}
147	return (0);
148}
149
150/*
151 * ex_init --
152 *	Init the screen for ex.
153 *
154 * PUBLIC: int ex_init __P((SCR *));
155 */
156int
157ex_init(sp)
158	SCR *sp;
159{
160	GS *gp;
161
162	gp = sp->gp;
163
164	if (gp->scr_screen(sp, SC_EX))
165		return (1);
166	(void)gp->scr_attr(sp, SA_ALTERNATE, 0);
167
168	sp->rows = O_VAL(sp, O_LINES);
169	sp->cols = O_VAL(sp, O_COLUMNS);
170
171	F_CLR(sp, SC_VI);
172	F_SET(sp, SC_EX | SC_SCR_EX);
173	return (0);
174}
175
176/*
177 * ex_emsg --
178 *	Display a few common ex and vi error messages.
179 *
180 * PUBLIC: void ex_emsg __P((SCR *, char *, exm_t));
181 */
182void
183ex_emsg(sp, p, which)
184	SCR *sp;
185	char *p;
186	exm_t which;
187{
188	switch (which) {
189	case EXM_EMPTYBUF:
190		msgq(sp, M_ERR, "168|Buffer %s is empty", p);
191		break;
192	case EXM_FILECOUNT:
193		 msgq_str(sp, M_ERR, p,
194		     "144|%s: expanded into too many file names");
195		break;
196	case EXM_NOCANON:
197		msgq(sp, M_ERR,
198		    "283|The %s command requires the ex terminal interface", p);
199		break;
200	case EXM_NOCANON_F:
201		msgq(sp, M_ERR,
202		    "272|That form of %s requires the ex terminal interface",
203		    p);
204		break;
205	case EXM_NOFILEYET:
206		if (p == NULL)
207			msgq(sp, M_ERR,
208			    "274|Command failed, no file read in yet.");
209		else
210			msgq(sp, M_ERR,
211	"173|The %s command requires that a file have already been read in", p);
212		break;
213	case EXM_NOPREVBUF:
214		msgq(sp, M_ERR, "171|No previous buffer to execute");
215		break;
216	case EXM_NOPREVRE:
217		msgq(sp, M_ERR, "172|No previous regular expression");
218		break;
219	case EXM_NOSUSPEND:
220		msgq(sp, M_ERR, "230|This screen may not be suspended");
221		break;
222	case EXM_SECURE:
223		msgq(sp, M_ERR,
224"290|The %s command is not supported when the secure edit option is set", p);
225		break;
226	case EXM_SECURE_F:
227		msgq(sp, M_ERR,
228"284|That form of %s is not supported when the secure edit option is set", p);
229		break;
230	case EXM_USAGE:
231		msgq(sp, M_ERR, "174|Usage: %s", p);
232		break;
233	}
234}
235