xref: /freebsd/contrib/nvi/ex/ex.c (revision 0fcececbac9880b092aeb56a41a16f1ec8ac1ae6)
1b8ba871bSPeter Wemm /*-
2b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994
3b8ba871bSPeter Wemm  *	The Regents of the University of California.  All rights reserved.
4b8ba871bSPeter Wemm  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5b8ba871bSPeter Wemm  *	Keith Bostic.  All rights reserved.
6b8ba871bSPeter Wemm  *
7b8ba871bSPeter Wemm  * See the LICENSE file for redistribution information.
8b8ba871bSPeter Wemm  */
9b8ba871bSPeter Wemm 
10b8ba871bSPeter Wemm #include "config.h"
11b8ba871bSPeter Wemm 
12b8ba871bSPeter Wemm #include <sys/types.h>
13b8ba871bSPeter Wemm #include <sys/queue.h>
14b8ba871bSPeter Wemm #include <sys/stat.h>
15b8ba871bSPeter Wemm 
16b8ba871bSPeter Wemm #include <bitstring.h>
17b8ba871bSPeter Wemm #include <ctype.h>
18b8ba871bSPeter Wemm #include <errno.h>
19b8ba871bSPeter Wemm #include <fcntl.h>
20b8ba871bSPeter Wemm #include <limits.h>
21b8ba871bSPeter Wemm #include <stdio.h>
22b8ba871bSPeter Wemm #include <stdlib.h>
23b8ba871bSPeter Wemm #include <string.h>
24b8ba871bSPeter Wemm #include <unistd.h>
25b8ba871bSPeter Wemm 
26b8ba871bSPeter Wemm #include "../common/common.h"
27b8ba871bSPeter Wemm #include "../vi/vi.h"
28b8ba871bSPeter Wemm 
29b8ba871bSPeter Wemm #if defined(DEBUG) && defined(COMLOG)
30c271fa92SBaptiste Daroussin static void	ex_comlog(SCR *, EXCMD *);
31b8ba871bSPeter Wemm #endif
32b8ba871bSPeter Wemm static EXCMDLIST const *
33c271fa92SBaptiste Daroussin 		ex_comm_search(CHAR_T *, size_t);
34c271fa92SBaptiste Daroussin static int	ex_discard(SCR *);
35c271fa92SBaptiste Daroussin static int	ex_line(SCR *, EXCMD *, MARK *, int *, int *);
36c271fa92SBaptiste Daroussin static int	ex_load(SCR *);
37c271fa92SBaptiste Daroussin static void	ex_unknown(SCR *, CHAR_T *, size_t);
38b8ba871bSPeter Wemm 
39b8ba871bSPeter Wemm /*
40b8ba871bSPeter Wemm  * ex --
41b8ba871bSPeter Wemm  *	Main ex loop.
42b8ba871bSPeter Wemm  *
43c271fa92SBaptiste Daroussin  * PUBLIC: int ex(SCR **);
44b8ba871bSPeter Wemm  */
45b8ba871bSPeter Wemm int
ex(SCR ** spp)46f0957ccaSPeter Wemm ex(SCR **spp)
47b8ba871bSPeter Wemm {
48b8ba871bSPeter Wemm 	EX_PRIVATE *exp;
49b8ba871bSPeter Wemm 	GS *gp;
50b8ba871bSPeter Wemm 	MSGS *mp;
51b8ba871bSPeter Wemm 	SCR *sp;
52b8ba871bSPeter Wemm 	TEXT *tp;
53b8ba871bSPeter Wemm 	u_int32_t flags;
54b8ba871bSPeter Wemm 
55b8ba871bSPeter Wemm 	sp = *spp;
56b8ba871bSPeter Wemm 	gp = sp->gp;
57b8ba871bSPeter Wemm 	exp = EXP(sp);
58b8ba871bSPeter Wemm 
59b8ba871bSPeter Wemm 	/* Start the ex screen. */
60b8ba871bSPeter Wemm 	if (ex_init(sp))
61b8ba871bSPeter Wemm 		return (1);
62b8ba871bSPeter Wemm 
63b8ba871bSPeter Wemm 	/* Flush any saved messages. */
64f0957ccaSPeter Wemm 	while ((mp = SLIST_FIRST(gp->msgq)) != NULL) {
65b8ba871bSPeter Wemm 		gp->scr_msg(sp, mp->mtype, mp->buf, mp->len);
66f0957ccaSPeter Wemm 		SLIST_REMOVE_HEAD(gp->msgq, q);
67b8ba871bSPeter Wemm 		free(mp->buf);
68b8ba871bSPeter Wemm 		free(mp);
69b8ba871bSPeter Wemm 	}
70b8ba871bSPeter Wemm 
71b8ba871bSPeter Wemm 	/* If reading from a file, errors should have name and line info. */
72b8ba871bSPeter Wemm 	if (F_ISSET(gp, G_SCRIPTED)) {
73b8ba871bSPeter Wemm 		gp->excmd.if_lno = 1;
74b8ba871bSPeter Wemm 		gp->excmd.if_name = "script";
75b8ba871bSPeter Wemm 	}
76b8ba871bSPeter Wemm 
77b8ba871bSPeter Wemm 	/*
78b8ba871bSPeter Wemm 	 * !!!
79b8ba871bSPeter Wemm 	 * Initialize the text flags.  The beautify edit option historically
80b8ba871bSPeter Wemm 	 * applied to ex command input read from a file.  In addition, the
81b8ba871bSPeter Wemm 	 * first time a ^H was discarded from the input, there was a message,
82b8ba871bSPeter Wemm 	 * "^H discarded", that was displayed.  We don't bother.
83b8ba871bSPeter Wemm 	 */
84b8ba871bSPeter Wemm 	LF_INIT(TXT_BACKSLASH | TXT_CNTRLD | TXT_CR);
85b8ba871bSPeter Wemm 	for (;; ++gp->excmd.if_lno) {
86b8ba871bSPeter Wemm 		/* Display status line and flush. */
87b8ba871bSPeter Wemm 		if (F_ISSET(sp, SC_STATUS)) {
88b8ba871bSPeter Wemm 			if (!F_ISSET(sp, SC_EX_SILENT))
89b8ba871bSPeter Wemm 				msgq_status(sp, sp->lno, 0);
90b8ba871bSPeter Wemm 			F_CLR(sp, SC_STATUS);
91b8ba871bSPeter Wemm 		}
92b8ba871bSPeter Wemm 		(void)ex_fflush(sp);
93b8ba871bSPeter Wemm 
94b8ba871bSPeter Wemm 		/* Set the flags the user can reset. */
95b8ba871bSPeter Wemm 		if (O_ISSET(sp, O_BEAUTIFY))
96b8ba871bSPeter Wemm 			LF_SET(TXT_BEAUTIFY);
97b8ba871bSPeter Wemm 		if (O_ISSET(sp, O_PROMPT))
98b8ba871bSPeter Wemm 			LF_SET(TXT_PROMPT);
99b8ba871bSPeter Wemm 
100b8ba871bSPeter Wemm 		/* Clear any current interrupts, and get a command. */
101b8ba871bSPeter Wemm 		CLR_INTERRUPT(sp);
102f0957ccaSPeter Wemm 		if (ex_txt(sp, sp->tiq, ':', flags))
103b8ba871bSPeter Wemm 			return (1);
104b8ba871bSPeter Wemm 		if (INTERRUPTED(sp)) {
105b8ba871bSPeter Wemm 			(void)ex_puts(sp, "\n");
106b8ba871bSPeter Wemm 			(void)ex_fflush(sp);
107b8ba871bSPeter Wemm 			continue;
108b8ba871bSPeter Wemm 		}
109b8ba871bSPeter Wemm 
110b8ba871bSPeter Wemm 		/* Initialize the command structure. */
111b8ba871bSPeter Wemm 		CLEAR_EX_PARSER(&gp->excmd);
112b8ba871bSPeter Wemm 
113b8ba871bSPeter Wemm 		/*
114b8ba871bSPeter Wemm 		 * If the user entered a single carriage return, send
115b8ba871bSPeter Wemm 		 * ex_cmd() a separator -- it discards single newlines.
116b8ba871bSPeter Wemm 		 */
117f0957ccaSPeter Wemm 		tp = TAILQ_FIRST(sp->tiq);
118b8ba871bSPeter Wemm 		if (tp->len == 0) {
119f0957ccaSPeter Wemm 			gp->excmd.cp = L(" ");	/* __TK__ why not |? */
120b8ba871bSPeter Wemm 			gp->excmd.clen = 1;
121b8ba871bSPeter Wemm 		} else {
122b8ba871bSPeter Wemm 			gp->excmd.cp = tp->lb;
123b8ba871bSPeter Wemm 			gp->excmd.clen = tp->len;
124b8ba871bSPeter Wemm 		}
125b8ba871bSPeter Wemm 		F_INIT(&gp->excmd, E_NRSEP);
126b8ba871bSPeter Wemm 
127b8ba871bSPeter Wemm 		if (ex_cmd(sp) && F_ISSET(gp, G_SCRIPTED))
128b8ba871bSPeter Wemm 			return (1);
129b8ba871bSPeter Wemm 
130b8ba871bSPeter Wemm 		if (INTERRUPTED(sp)) {
131b8ba871bSPeter Wemm 			CLR_INTERRUPT(sp);
132b8ba871bSPeter Wemm 			msgq(sp, M_ERR, "170|Interrupted");
133b8ba871bSPeter Wemm 		}
134b8ba871bSPeter Wemm 
135b8ba871bSPeter Wemm 		/*
136b8ba871bSPeter Wemm 		 * If the last command caused a restart, or switched screens
137b8ba871bSPeter Wemm 		 * or into vi, return.
138b8ba871bSPeter Wemm 		 */
139b8ba871bSPeter Wemm 		if (F_ISSET(gp, G_SRESTART) || F_ISSET(sp, SC_SSWITCH | SC_VI)) {
140b8ba871bSPeter Wemm 			*spp = sp;
141b8ba871bSPeter Wemm 			break;
142b8ba871bSPeter Wemm 		}
143b8ba871bSPeter Wemm 
144b8ba871bSPeter Wemm 		/* If the last command switched files, we don't care. */
145b8ba871bSPeter Wemm 		F_CLR(sp, SC_FSWITCH);
146b8ba871bSPeter Wemm 
147b8ba871bSPeter Wemm 		/*
148b8ba871bSPeter Wemm 		 * If we're exiting this screen, move to the next one.  By
149b8ba871bSPeter Wemm 		 * definition, this means returning into vi, so return to the
150b8ba871bSPeter Wemm 		 * main editor loop.  The ordering is careful, don't discard
151b8ba871bSPeter Wemm 		 * the contents of sp until the end.
152b8ba871bSPeter Wemm 		 */
153b8ba871bSPeter Wemm 		if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
154b8ba871bSPeter Wemm 			if (file_end(sp, NULL, F_ISSET(sp, SC_EXIT_FORCE)))
155b8ba871bSPeter Wemm 				return (1);
156b8ba871bSPeter Wemm 			*spp = screen_next(sp);
157*0fcececbSBaptiste Daroussin 			if (*spp) {
158*0fcececbSBaptiste Daroussin 				F_CLR(*spp, SC_SCR_VI);
159*0fcececbSBaptiste Daroussin 				F_SET(*spp, SC_SCR_EX);
160*0fcececbSBaptiste Daroussin 			}
161b8ba871bSPeter Wemm 			return (screen_end(sp));
162b8ba871bSPeter Wemm 		}
163b8ba871bSPeter Wemm 	}
164b8ba871bSPeter Wemm 	return (0);
165b8ba871bSPeter Wemm }
166b8ba871bSPeter Wemm 
167b8ba871bSPeter Wemm /*
168b8ba871bSPeter Wemm  * ex_cmd --
169b8ba871bSPeter Wemm  *	The guts of the ex parser: parse and execute a string containing
170b8ba871bSPeter Wemm  *	ex commands.
171b8ba871bSPeter Wemm  *
172b8ba871bSPeter Wemm  * !!!
173b8ba871bSPeter Wemm  * This code MODIFIES the string that gets passed in, to delete quoting
174b8ba871bSPeter Wemm  * characters, etc.  The string cannot be readonly/text space, nor should
175b8ba871bSPeter Wemm  * you expect to use it again after ex_cmd() returns.
176b8ba871bSPeter Wemm  *
177b8ba871bSPeter Wemm  * !!!
178b8ba871bSPeter Wemm  * For the fun of it, if you want to see if a vi clone got the ex argument
179b8ba871bSPeter Wemm  * parsing right, try:
180b8ba871bSPeter Wemm  *
181b8ba871bSPeter Wemm  *	echo 'foo|bar' > file1; echo 'foo/bar' > file2;
182b8ba871bSPeter Wemm  *	vi
183b8ba871bSPeter Wemm  *	:edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
184b8ba871bSPeter Wemm  *
185b8ba871bSPeter Wemm  * or:	vi
186b8ba871bSPeter Wemm  *	:set|file|append|set|file
187b8ba871bSPeter Wemm  *
188b8ba871bSPeter Wemm  * For extra credit, try them in a startup .exrc file.
189b8ba871bSPeter Wemm  *
190c271fa92SBaptiste Daroussin  * PUBLIC: int ex_cmd(SCR *);
191b8ba871bSPeter Wemm  */
192b8ba871bSPeter Wemm int
ex_cmd(SCR * sp)193f0957ccaSPeter Wemm ex_cmd(SCR *sp)
194b8ba871bSPeter Wemm {
195b8ba871bSPeter Wemm 	enum nresult nret;
196b8ba871bSPeter Wemm 	EX_PRIVATE *exp;
197b8ba871bSPeter Wemm 	EXCMD *ecp;
198b8ba871bSPeter Wemm 	GS *gp;
199b8ba871bSPeter Wemm 	MARK cur;
200b8ba871bSPeter Wemm 	recno_t lno;
201b8ba871bSPeter Wemm 	size_t arg1_len, discard, len;
202b8ba871bSPeter Wemm 	u_int32_t flags;
203b8ba871bSPeter Wemm 	long ltmp;
204b8ba871bSPeter Wemm 	int at_found, gv_found;
205f0957ccaSPeter Wemm 	int cnt, delim, isaddr, namelen;
206b8ba871bSPeter Wemm 	int newscreen, notempty, tmp, vi_address;
207f0957ccaSPeter Wemm 	CHAR_T *arg1, *s, *p, *t;
208f0957ccaSPeter Wemm 	CHAR_T ch = '\0';
209f0957ccaSPeter Wemm 	CHAR_T *n;
210f0957ccaSPeter Wemm 	char *np;
211b8ba871bSPeter Wemm 
212b8ba871bSPeter Wemm 	gp = sp->gp;
213b8ba871bSPeter Wemm 	exp = EXP(sp);
214b8ba871bSPeter Wemm 
215b8ba871bSPeter Wemm 	/*
216b8ba871bSPeter Wemm 	 * We always start running the command on the top of the stack.
217b8ba871bSPeter Wemm 	 * This means that *everything* must be resolved when we leave
218b8ba871bSPeter Wemm 	 * this function for any reason.
219b8ba871bSPeter Wemm 	 */
220f0957ccaSPeter Wemm loop:	ecp = SLIST_FIRST(gp->ecq);
221b8ba871bSPeter Wemm 
222b8ba871bSPeter Wemm 	/* If we're reading a command from a file, set up error information. */
223b8ba871bSPeter Wemm 	if (ecp->if_name != NULL) {
224b8ba871bSPeter Wemm 		gp->if_lno = ecp->if_lno;
225b8ba871bSPeter Wemm 		gp->if_name = ecp->if_name;
226b8ba871bSPeter Wemm 	}
227b8ba871bSPeter Wemm 
228b8ba871bSPeter Wemm 	/*
229b8ba871bSPeter Wemm 	 * If a move to the end of the file is scheduled for this command,
230b8ba871bSPeter Wemm 	 * do it now.
231b8ba871bSPeter Wemm 	 */
232b8ba871bSPeter Wemm 	if (F_ISSET(ecp, E_MOVETOEND)) {
233b8ba871bSPeter Wemm 		if (db_last(sp, &sp->lno))
234b8ba871bSPeter Wemm 			goto rfail;
235b8ba871bSPeter Wemm 		sp->cno = 0;
236b8ba871bSPeter Wemm 		F_CLR(ecp, E_MOVETOEND);
237b8ba871bSPeter Wemm 	}
238b8ba871bSPeter Wemm 
239b8ba871bSPeter Wemm 	/* If we found a newline, increment the count now. */
240b8ba871bSPeter Wemm 	if (F_ISSET(ecp, E_NEWLINE)) {
241b8ba871bSPeter Wemm 		++gp->if_lno;
242b8ba871bSPeter Wemm 		++ecp->if_lno;
243b8ba871bSPeter Wemm 		F_CLR(ecp, E_NEWLINE);
244b8ba871bSPeter Wemm 	}
245b8ba871bSPeter Wemm 
246b8ba871bSPeter Wemm 	/* (Re)initialize the EXCMD structure, preserving some flags. */
247b8ba871bSPeter Wemm 	CLEAR_EX_CMD(ecp);
248b8ba871bSPeter Wemm 
249b8ba871bSPeter Wemm 	/* Initialize the argument structures. */
250b8ba871bSPeter Wemm 	if (argv_init(sp, ecp))
251b8ba871bSPeter Wemm 		goto err;
252b8ba871bSPeter Wemm 
253b8ba871bSPeter Wemm 	/* Initialize +cmd, saved command information. */
254b8ba871bSPeter Wemm 	arg1 = NULL;
255b8ba871bSPeter Wemm 	ecp->save_cmdlen = 0;
256b8ba871bSPeter Wemm 
257b8ba871bSPeter Wemm 	/* Skip <blank>s, empty lines.  */
258b8ba871bSPeter Wemm 	for (notempty = 0; ecp->clen > 0; ++ecp->cp, --ecp->clen)
259b8ba871bSPeter Wemm 		if ((ch = *ecp->cp) == '\n') {
260b8ba871bSPeter Wemm 			++gp->if_lno;
261b8ba871bSPeter Wemm 			++ecp->if_lno;
262f0957ccaSPeter Wemm 		} else if (cmdskip(ch))
263b8ba871bSPeter Wemm 			notempty = 1;
264b8ba871bSPeter Wemm 		else
265b8ba871bSPeter Wemm 			break;
266b8ba871bSPeter Wemm 
267b8ba871bSPeter Wemm 	/*
268b8ba871bSPeter Wemm 	 * !!!
269b8ba871bSPeter Wemm 	 * Permit extra colons at the start of the line.  Historically,
270b8ba871bSPeter Wemm 	 * ex/vi allowed a single extra one.  It's simpler not to count.
271b8ba871bSPeter Wemm 	 * The stripping is done here because, historically, any command
272b8ba871bSPeter Wemm 	 * could have preceding colons, e.g. ":g/pattern/:p" worked.
273b8ba871bSPeter Wemm 	 */
274b8ba871bSPeter Wemm 	if (ecp->clen != 0 && ch == ':') {
275b8ba871bSPeter Wemm 		notempty = 1;
276b8ba871bSPeter Wemm 		while (--ecp->clen > 0 && (ch = *++ecp->cp) == ':');
277b8ba871bSPeter Wemm 	}
278b8ba871bSPeter Wemm 
279b8ba871bSPeter Wemm 	/*
280b8ba871bSPeter Wemm 	 * Command lines that start with a double-quote are comments.
281b8ba871bSPeter Wemm 	 *
282b8ba871bSPeter Wemm 	 * !!!
283b8ba871bSPeter Wemm 	 * Historically, there was no escape or delimiter for a comment, e.g.
284b8ba871bSPeter Wemm 	 * :"foo|set was a single comment and nothing was output.  Since nvi
285b8ba871bSPeter Wemm 	 * permits users to escape <newline> characters into command lines, we
286b8ba871bSPeter Wemm 	 * have to check for that case.
287b8ba871bSPeter Wemm 	 */
288b8ba871bSPeter Wemm 	if (ecp->clen != 0 && ch == '"') {
289b8ba871bSPeter Wemm 		while (--ecp->clen > 0 && *++ecp->cp != '\n');
290b8ba871bSPeter Wemm 		if (*ecp->cp == '\n') {
291b8ba871bSPeter Wemm 			F_SET(ecp, E_NEWLINE);
292b8ba871bSPeter Wemm 			++ecp->cp;
293b8ba871bSPeter Wemm 			--ecp->clen;
294b8ba871bSPeter Wemm 		}
295b8ba871bSPeter Wemm 		goto loop;
296b8ba871bSPeter Wemm 	}
297b8ba871bSPeter Wemm 
298b8ba871bSPeter Wemm 	/* Skip whitespace. */
299b8ba871bSPeter Wemm 	for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
300b8ba871bSPeter Wemm 		ch = *ecp->cp;
301f0957ccaSPeter Wemm 		if (!cmdskip(ch))
302b8ba871bSPeter Wemm 			break;
303b8ba871bSPeter Wemm 	}
304b8ba871bSPeter Wemm 
305b8ba871bSPeter Wemm 	/*
306b8ba871bSPeter Wemm 	 * The last point at which an empty line can mean do nothing.
307b8ba871bSPeter Wemm 	 *
308b8ba871bSPeter Wemm 	 * !!!
309b8ba871bSPeter Wemm 	 * Historically, in ex mode, lines containing only <blank> characters
310b8ba871bSPeter Wemm 	 * were the same as a single <carriage-return>, i.e. a default command.
311b8ba871bSPeter Wemm 	 * In vi mode, they were ignored.  In .exrc files this was a serious
312b8ba871bSPeter Wemm 	 * annoyance, as vi kept trying to treat them as print commands.  We
313b8ba871bSPeter Wemm 	 * ignore backward compatibility in this case, discarding lines that
314b8ba871bSPeter Wemm 	 * contain only <blank> characters from .exrc files.
315b8ba871bSPeter Wemm 	 *
316b8ba871bSPeter Wemm 	 * !!!
317b8ba871bSPeter Wemm 	 * This is where you end up when you're done a command, i.e. clen has
318b8ba871bSPeter Wemm 	 * gone to zero.  Continue if there are more commands to run.
319b8ba871bSPeter Wemm 	 */
320b8ba871bSPeter Wemm 	if (ecp->clen == 0 &&
321b8ba871bSPeter Wemm 	    (!notempty || F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_BLIGNORE))) {
322b8ba871bSPeter Wemm 		if (ex_load(sp))
323b8ba871bSPeter Wemm 			goto rfail;
324f0957ccaSPeter Wemm 		ecp = SLIST_FIRST(gp->ecq);
325b8ba871bSPeter Wemm 		if (ecp->clen == 0)
326b8ba871bSPeter Wemm 			goto rsuccess;
327b8ba871bSPeter Wemm 		goto loop;
328b8ba871bSPeter Wemm 	}
329b8ba871bSPeter Wemm 
330b8ba871bSPeter Wemm 	/*
331b8ba871bSPeter Wemm 	 * Check to see if this is a command for which we may want to move
332b8ba871bSPeter Wemm 	 * the cursor back up to the previous line.  (The command :1<CR>
333b8ba871bSPeter Wemm 	 * wants a <newline> separator, but the command :<CR> wants to erase
334b8ba871bSPeter Wemm 	 * the command line.)  If the line is empty except for <blank>s,
335b8ba871bSPeter Wemm 	 * <carriage-return> or <eof>, we'll probably want to move up.  I
336b8ba871bSPeter Wemm 	 * don't think there's any way to get <blank> characters *after* the
337b8ba871bSPeter Wemm 	 * command character, but this is the ex parser, and I've been wrong
338b8ba871bSPeter Wemm 	 * before.
339b8ba871bSPeter Wemm 	 */
340b8ba871bSPeter Wemm 	if (F_ISSET(ecp, E_NRSEP) &&
341b8ba871bSPeter Wemm 	    ecp->clen != 0 && (ecp->clen != 1 || ecp->cp[0] != '\004'))
342b8ba871bSPeter Wemm 		F_CLR(ecp, E_NRSEP);
343b8ba871bSPeter Wemm 
344b8ba871bSPeter Wemm 	/* Parse command addresses. */
345b8ba871bSPeter Wemm 	if (ex_range(sp, ecp, &tmp))
346b8ba871bSPeter Wemm 		goto rfail;
347b8ba871bSPeter Wemm 	if (tmp)
348b8ba871bSPeter Wemm 		goto err;
349b8ba871bSPeter Wemm 
350b8ba871bSPeter Wemm 	/*
351b8ba871bSPeter Wemm 	 * Skip <blank>s and any more colons (the command :3,5:print
352b8ba871bSPeter Wemm 	 * worked, historically).
353b8ba871bSPeter Wemm 	 */
354b8ba871bSPeter Wemm 	for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
355b8ba871bSPeter Wemm 		ch = *ecp->cp;
356f0957ccaSPeter Wemm 		if (!cmdskip(ch) && ch != ':')
357b8ba871bSPeter Wemm 			break;
358b8ba871bSPeter Wemm 	}
359b8ba871bSPeter Wemm 
360b8ba871bSPeter Wemm 	/*
361b8ba871bSPeter Wemm 	 * If no command, ex does the last specified of p, l, or #, and vi
362b8ba871bSPeter Wemm 	 * moves to the line.  Otherwise, determine the length of the command
363b8ba871bSPeter Wemm 	 * name by looking for the first non-alphabetic character.  (There
364b8ba871bSPeter Wemm 	 * are a few non-alphabetic characters in command names, but they're
365b8ba871bSPeter Wemm 	 * all single character commands.)  This isn't a great test, because
366b8ba871bSPeter Wemm 	 * it means that, for the command ":e +cut.c file", we'll report that
367b8ba871bSPeter Wemm 	 * the command "cut" wasn't known.  However, it makes ":e+35 file" work
368b8ba871bSPeter Wemm 	 * correctly.
369b8ba871bSPeter Wemm 	 *
370b8ba871bSPeter Wemm 	 * !!!
371b8ba871bSPeter Wemm 	 * Historically, lines with multiple adjacent (or <blank> separated)
372b8ba871bSPeter Wemm 	 * command separators were very strange.  For example, the command
373b8ba871bSPeter Wemm 	 * |||<carriage-return>, when the cursor was on line 1, displayed
374b8ba871bSPeter Wemm 	 * lines 2, 3 and 5 of the file.  In addition, the command "   |  "
375b8ba871bSPeter Wemm 	 * would only display the line after the next line, instead of the
376b8ba871bSPeter Wemm 	 * next two lines.  No ideas why.  It worked reasonably when executed
377b8ba871bSPeter Wemm 	 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
378b8ba871bSPeter Wemm 	 * command for each separator.
379b8ba871bSPeter Wemm 	 */
380f0957ccaSPeter Wemm #define	SINGLE_CHAR_COMMANDS	L("\004!#&*<=>@~")
381b8ba871bSPeter Wemm 	newscreen = 0;
382b8ba871bSPeter Wemm 	if (ecp->clen != 0 && ecp->cp[0] != '|' && ecp->cp[0] != '\n') {
383f0957ccaSPeter Wemm 		if (STRCHR(SINGLE_CHAR_COMMANDS, *ecp->cp)) {
384b8ba871bSPeter Wemm 			p = ecp->cp;
385b8ba871bSPeter Wemm 			++ecp->cp;
386b8ba871bSPeter Wemm 			--ecp->clen;
387b8ba871bSPeter Wemm 			namelen = 1;
388b8ba871bSPeter Wemm 		} else {
389b8ba871bSPeter Wemm 			for (p = ecp->cp;
390b8ba871bSPeter Wemm 			    ecp->clen > 0; --ecp->clen, ++ecp->cp)
391110d525eSBaptiste Daroussin 				if (!isazAZ(*ecp->cp))
392b8ba871bSPeter Wemm 					break;
393b8ba871bSPeter Wemm 			if ((namelen = ecp->cp - p) == 0) {
394b8ba871bSPeter Wemm 				msgq(sp, M_ERR, "080|Unknown command name");
395b8ba871bSPeter Wemm 				goto err;
396b8ba871bSPeter Wemm 			}
397b8ba871bSPeter Wemm 		}
398b8ba871bSPeter Wemm 
399b8ba871bSPeter Wemm 		/*
400b8ba871bSPeter Wemm 		 * !!!
401b8ba871bSPeter Wemm 		 * Historic vi permitted flags to immediately follow any
402b8ba871bSPeter Wemm 		 * subset of the 'delete' command, but then did not permit
403b8ba871bSPeter Wemm 		 * further arguments (flag, buffer, count).  Make it work.
404b8ba871bSPeter Wemm 		 * Permit further arguments for the few shreds of dignity
405b8ba871bSPeter Wemm 		 * it offers.
406b8ba871bSPeter Wemm 		 *
407b8ba871bSPeter Wemm 		 * Adding commands that start with 'd', and match "delete"
408b8ba871bSPeter Wemm 		 * up to a l, p, +, - or # character can break this code.
409b8ba871bSPeter Wemm 		 *
410b8ba871bSPeter Wemm 		 * !!!
411b8ba871bSPeter Wemm 		 * Capital letters beginning the command names ex, edit,
412b8ba871bSPeter Wemm 		 * next, previous, tag and visual (in vi mode) indicate the
413b8ba871bSPeter Wemm 		 * command should happen in a new screen.
414b8ba871bSPeter Wemm 		 */
415b8ba871bSPeter Wemm 		switch (p[0]) {
416b8ba871bSPeter Wemm 		case 'd':
417b8ba871bSPeter Wemm 			for (s = p,
418f0957ccaSPeter Wemm 			    n = cmds[C_DELETE].name; *s == *n; ++s, ++n);
419b8ba871bSPeter Wemm 			if (s[0] == 'l' || s[0] == 'p' || s[0] == '+' ||
420b8ba871bSPeter Wemm 			    s[0] == '-' || s[0] == '^' || s[0] == '#') {
421b8ba871bSPeter Wemm 				len = (ecp->cp - p) - (s - p);
422b8ba871bSPeter Wemm 				ecp->cp -= len;
423b8ba871bSPeter Wemm 				ecp->clen += len;
424b8ba871bSPeter Wemm 				ecp->rcmd = cmds[C_DELETE];
425b8ba871bSPeter Wemm 				ecp->rcmd.syntax = "1bca1";
426b8ba871bSPeter Wemm 				ecp->cmd = &ecp->rcmd;
427b8ba871bSPeter Wemm 				goto skip_srch;
428b8ba871bSPeter Wemm 			}
429b8ba871bSPeter Wemm 			break;
430b8ba871bSPeter Wemm 		case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
431b8ba871bSPeter Wemm 			newscreen = 1;
432b8ba871bSPeter Wemm 			p[0] = tolower(p[0]);
433b8ba871bSPeter Wemm 			break;
434b8ba871bSPeter Wemm 		}
435b8ba871bSPeter Wemm 
436b8ba871bSPeter Wemm 		/*
437b8ba871bSPeter Wemm 		 * Search the table for the command.
438b8ba871bSPeter Wemm 		 *
439b8ba871bSPeter Wemm 		 * !!!
440b8ba871bSPeter Wemm 		 * Historic vi permitted the mark to immediately follow the
441b8ba871bSPeter Wemm 		 * 'k' in the 'k' command.  Make it work.
442b8ba871bSPeter Wemm 		 *
443b8ba871bSPeter Wemm 		 * !!!
444b8ba871bSPeter Wemm 		 * Historic vi permitted any flag to follow the s command, e.g.
445b8ba871bSPeter Wemm 		 * "s/e/E/|s|sgc3p" was legal.  Make the command "sgc" work.
446b8ba871bSPeter Wemm 		 * Since the following characters all have to be flags, i.e.
447b8ba871bSPeter Wemm 		 * alphabetics, we can let the s command routine return errors
448b8ba871bSPeter Wemm 		 * if it was some illegal command string.  This code will break
449b8ba871bSPeter Wemm 		 * if an "sg" or similar command is ever added.  The substitute
450b8ba871bSPeter Wemm 		 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
451b8ba871bSPeter Wemm 		 * follows the 's', but we limit the choices here to "cgr" so
452b8ba871bSPeter Wemm 		 * that we get unknown command messages for wrong combinations.
453b8ba871bSPeter Wemm 		 */
454b8ba871bSPeter Wemm 		if ((ecp->cmd = ex_comm_search(p, namelen)) == NULL)
455b8ba871bSPeter Wemm 			switch (p[0]) {
456b8ba871bSPeter Wemm 			case 'k':
457b8ba871bSPeter Wemm 				if (namelen == 2) {
458b8ba871bSPeter Wemm 					ecp->cp -= namelen - 1;
459b8ba871bSPeter Wemm 					ecp->clen += namelen - 1;
460b8ba871bSPeter Wemm 					ecp->cmd = &cmds[C_K];
461b8ba871bSPeter Wemm 					break;
462b8ba871bSPeter Wemm 				}
463b8ba871bSPeter Wemm 				goto unknown;
464b8ba871bSPeter Wemm 			case 's':
465b8ba871bSPeter Wemm 				for (s = p + 1, cnt = namelen; --cnt; ++s)
466b8ba871bSPeter Wemm 					if (s[0] != 'c' &&
467b8ba871bSPeter Wemm 					    s[0] != 'g' && s[0] != 'r')
468b8ba871bSPeter Wemm 						break;
469b8ba871bSPeter Wemm 				if (cnt == 0) {
470b8ba871bSPeter Wemm 					ecp->cp -= namelen - 1;
471b8ba871bSPeter Wemm 					ecp->clen += namelen - 1;
472b8ba871bSPeter Wemm 					ecp->rcmd = cmds[C_SUBSTITUTE];
473b8ba871bSPeter Wemm 					ecp->rcmd.fn = ex_subagain;
474b8ba871bSPeter Wemm 					ecp->cmd = &ecp->rcmd;
475b8ba871bSPeter Wemm 					break;
476b8ba871bSPeter Wemm 				}
477b8ba871bSPeter Wemm 				/* FALLTHROUGH */
478b8ba871bSPeter Wemm 			default:
479b8ba871bSPeter Wemm unknown:			if (newscreen)
480b8ba871bSPeter Wemm 					p[0] = toupper(p[0]);
481b8ba871bSPeter Wemm 				ex_unknown(sp, p, namelen);
482b8ba871bSPeter Wemm 				goto err;
483b8ba871bSPeter Wemm 			}
484b8ba871bSPeter Wemm 
485b8ba871bSPeter Wemm 		/*
486b8ba871bSPeter Wemm 		 * The visual command has a different syntax when called
487b8ba871bSPeter Wemm 		 * from ex than when called from a vi colon command.  FMH.
488b8ba871bSPeter Wemm 		 * Make the change now, before we test for the newscreen
489b8ba871bSPeter Wemm 		 * semantic, so that we're testing the right one.
490b8ba871bSPeter Wemm 		 */
491b8ba871bSPeter Wemm skip_srch:	if (ecp->cmd == &cmds[C_VISUAL_EX] && F_ISSET(sp, SC_VI))
492b8ba871bSPeter Wemm 			ecp->cmd = &cmds[C_VISUAL_VI];
493b8ba871bSPeter Wemm 
494b8ba871bSPeter Wemm 		/*
495b8ba871bSPeter Wemm 		 * !!!
496b8ba871bSPeter Wemm 		 * Historic vi permitted a capital 'P' at the beginning of
497b8ba871bSPeter Wemm 		 * any command that started with 'p'.  Probably wanted the
498b8ba871bSPeter Wemm 		 * P[rint] command for backward compatibility, and the code
499b8ba871bSPeter Wemm 		 * just made Preserve and Put work by accident.  Nvi uses
500b8ba871bSPeter Wemm 		 * Previous to mean previous-in-a-new-screen, so be careful.
501b8ba871bSPeter Wemm 		 */
502b8ba871bSPeter Wemm 		if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN) &&
503b8ba871bSPeter Wemm 		    (ecp->cmd == &cmds[C_PRINT] ||
504b8ba871bSPeter Wemm 		    ecp->cmd == &cmds[C_PRESERVE]))
505b8ba871bSPeter Wemm 			newscreen = 0;
506b8ba871bSPeter Wemm 
507b8ba871bSPeter Wemm 		/* Test for a newscreen associated with this command. */
508b8ba871bSPeter Wemm 		if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN))
509b8ba871bSPeter Wemm 			goto unknown;
510b8ba871bSPeter Wemm 
511b8ba871bSPeter Wemm 		/* Secure means no shell access. */
512b8ba871bSPeter Wemm 		if (F_ISSET(ecp->cmd, E_SECURE) && O_ISSET(sp, O_SECURE)) {
513f0957ccaSPeter Wemm 			ex_wemsg(sp, ecp->cmd->name, EXM_SECURE);
514b8ba871bSPeter Wemm 			goto err;
515b8ba871bSPeter Wemm 		}
516b8ba871bSPeter Wemm 
517b8ba871bSPeter Wemm 		/*
518b8ba871bSPeter Wemm 		 * Multiple < and > characters; another "feature".  Note,
519b8ba871bSPeter Wemm 		 * The string passed to the underlying function may not be
520b8ba871bSPeter Wemm 		 * nul terminated in this case.
521b8ba871bSPeter Wemm 		 */
522b8ba871bSPeter Wemm 		if ((ecp->cmd == &cmds[C_SHIFTL] && *p == '<') ||
523b8ba871bSPeter Wemm 		    (ecp->cmd == &cmds[C_SHIFTR] && *p == '>')) {
524b8ba871bSPeter Wemm 			for (ch = *p;
525b8ba871bSPeter Wemm 			    ecp->clen > 0; --ecp->clen, ++ecp->cp)
526b8ba871bSPeter Wemm 				if (*ecp->cp != ch)
527b8ba871bSPeter Wemm 					break;
528b8ba871bSPeter Wemm 			if (argv_exp0(sp, ecp, p, ecp->cp - p))
529b8ba871bSPeter Wemm 				goto err;
530b8ba871bSPeter Wemm 		}
531b8ba871bSPeter Wemm 
532b8ba871bSPeter Wemm 		/* Set the format style flags for the next command. */
533b8ba871bSPeter Wemm 		if (ecp->cmd == &cmds[C_HASH])
534b8ba871bSPeter Wemm 			exp->fdef = E_C_HASH;
535b8ba871bSPeter Wemm 		else if (ecp->cmd == &cmds[C_LIST])
536b8ba871bSPeter Wemm 			exp->fdef = E_C_LIST;
537b8ba871bSPeter Wemm 		else if (ecp->cmd == &cmds[C_PRINT])
538b8ba871bSPeter Wemm 			exp->fdef = E_C_PRINT;
539b8ba871bSPeter Wemm 		F_CLR(ecp, E_USELASTCMD);
540b8ba871bSPeter Wemm 	} else {
541b8ba871bSPeter Wemm 		/* Print is the default command. */
542b8ba871bSPeter Wemm 		ecp->cmd = &cmds[C_PRINT];
543b8ba871bSPeter Wemm 
544b8ba871bSPeter Wemm 		/* Set the saved format flags. */
545b8ba871bSPeter Wemm 		F_SET(ecp, exp->fdef);
546b8ba871bSPeter Wemm 
547b8ba871bSPeter Wemm 		/*
548b8ba871bSPeter Wemm 		 * !!!
549b8ba871bSPeter Wemm 		 * If no address was specified, and it's not a global command,
550b8ba871bSPeter Wemm 		 * we up the address by one.  (I have no idea why globals are
551b8ba871bSPeter Wemm 		 * exempted, but it's (ahem) historic practice.)
552b8ba871bSPeter Wemm 		 */
553b8ba871bSPeter Wemm 		if (ecp->addrcnt == 0 && !F_ISSET(sp, SC_EX_GLOBAL)) {
554b8ba871bSPeter Wemm 			ecp->addrcnt = 1;
555b8ba871bSPeter Wemm 			ecp->addr1.lno = sp->lno + 1;
556b8ba871bSPeter Wemm 			ecp->addr1.cno = sp->cno;
557b8ba871bSPeter Wemm 		}
558b8ba871bSPeter Wemm 
559b8ba871bSPeter Wemm 		F_SET(ecp, E_USELASTCMD);
560b8ba871bSPeter Wemm 	}
561b8ba871bSPeter Wemm 
562b8ba871bSPeter Wemm 	/*
563b8ba871bSPeter Wemm 	 * !!!
564b8ba871bSPeter Wemm 	 * Historically, the number option applied to both ex and vi.  One
565b8ba871bSPeter Wemm 	 * strangeness was that ex didn't switch display formats until a
566b8ba871bSPeter Wemm 	 * command was entered, e.g. <CR>'s after the set didn't change to
567b8ba871bSPeter Wemm 	 * the new format, but :1p would.
568b8ba871bSPeter Wemm 	 */
569b8ba871bSPeter Wemm 	if (O_ISSET(sp, O_NUMBER)) {
570b8ba871bSPeter Wemm 		F_SET(ecp, E_OPTNUM);
571b8ba871bSPeter Wemm 		FL_SET(ecp->iflags, E_C_HASH);
572b8ba871bSPeter Wemm 	} else
573b8ba871bSPeter Wemm 		F_CLR(ecp, E_OPTNUM);
574b8ba871bSPeter Wemm 
575b8ba871bSPeter Wemm 	/* Check for ex mode legality. */
576b8ba871bSPeter Wemm 	if (F_ISSET(sp, SC_EX) && (F_ISSET(ecp->cmd, E_VIONLY) || newscreen)) {
577f0957ccaSPeter Wemm 		msgq_wstr(sp, M_ERR, ecp->cmd->name,
578f0957ccaSPeter Wemm 		    "082|%s: command not available in ex mode");
579b8ba871bSPeter Wemm 		goto err;
580b8ba871bSPeter Wemm 	}
581b8ba871bSPeter Wemm 
582b8ba871bSPeter Wemm 	/* Add standard command flags. */
583b8ba871bSPeter Wemm 	F_SET(ecp, ecp->cmd->flags);
584b8ba871bSPeter Wemm 	if (!newscreen)
585b8ba871bSPeter Wemm 		F_CLR(ecp, E_NEWSCREEN);
586b8ba871bSPeter Wemm 
587b8ba871bSPeter Wemm 	/*
588b8ba871bSPeter Wemm 	 * There are three normal termination cases for an ex command.  They
589b8ba871bSPeter Wemm 	 * are the end of the string (ecp->clen), or unescaped (by <literal
590b8ba871bSPeter Wemm 	 * next> characters) <newline> or '|' characters.  As we're now past
591b8ba871bSPeter Wemm 	 * possible addresses, we can determine how long the command is, so we
592b8ba871bSPeter Wemm 	 * don't have to look for all the possible terminations.  Naturally,
593b8ba871bSPeter Wemm 	 * there are some exciting special cases:
594b8ba871bSPeter Wemm 	 *
595b8ba871bSPeter Wemm 	 * 1: The bang, global, v and the filter versions of the read and
596b8ba871bSPeter Wemm 	 *    write commands are delimited by <newline>s (they can contain
597b8ba871bSPeter Wemm 	 *    shell pipes).
598b8ba871bSPeter Wemm 	 * 2: The ex, edit, next and visual in vi mode commands all take ex
599b8ba871bSPeter Wemm 	 *    commands as their first arguments.
600b8ba871bSPeter Wemm 	 * 3: The s command takes an RE as its first argument, and wants it
601b8ba871bSPeter Wemm 	 *    to be specially delimited.
602b8ba871bSPeter Wemm 	 *
603b8ba871bSPeter Wemm 	 * Historically, '|' characters in the first argument of the ex, edit,
604b8ba871bSPeter Wemm 	 * next, vi visual, and s commands didn't delimit the command.  And,
605b8ba871bSPeter Wemm 	 * in the filter cases for read and write, and the bang, global and v
606b8ba871bSPeter Wemm 	 * commands, they did not delimit the command at all.
607b8ba871bSPeter Wemm 	 *
608b8ba871bSPeter Wemm 	 * For example, the following commands were legal:
609b8ba871bSPeter Wemm 	 *
610b8ba871bSPeter Wemm 	 *	:edit +25|s/abc/ABC/ file.c
611b8ba871bSPeter Wemm 	 *	:s/|/PIPE/
612b8ba871bSPeter Wemm 	 *	:read !spell % | columnate
613b8ba871bSPeter Wemm 	 *	:global/pattern/p|l
614b8ba871bSPeter Wemm 	 *
615b8ba871bSPeter Wemm 	 * It's not quite as simple as it sounds, however.  The command:
616b8ba871bSPeter Wemm 	 *
617b8ba871bSPeter Wemm 	 *	:s/a/b/|s/c/d|set
618b8ba871bSPeter Wemm 	 *
619b8ba871bSPeter Wemm 	 * was also legal, i.e. the historic ex parser (using the word loosely,
620b8ba871bSPeter Wemm 	 * since "parser" implies some regularity of syntax) delimited the RE's
621b8ba871bSPeter Wemm 	 * based on its delimiter and not anything so irretrievably vulgar as a
622b8ba871bSPeter Wemm 	 * command syntax.
623b8ba871bSPeter Wemm 	 *
624b8ba871bSPeter Wemm 	 * Anyhow, the following code makes this all work.  First, for the
625b8ba871bSPeter Wemm 	 * special cases we move past their special argument(s).  Then, we
626b8ba871bSPeter Wemm 	 * do normal command processing on whatever is left.  Barf-O-Rama.
627b8ba871bSPeter Wemm 	 */
628b8ba871bSPeter Wemm 	discard = 0;		/* Characters discarded from the command. */
629b8ba871bSPeter Wemm 	arg1_len = 0;
630b8ba871bSPeter Wemm 	ecp->save_cmd = ecp->cp;
631b8ba871bSPeter Wemm 	if (ecp->cmd == &cmds[C_EDIT] || ecp->cmd == &cmds[C_EX] ||
632f0957ccaSPeter Wemm 	    ecp->cmd == &cmds[C_NEXT] || ecp->cmd == &cmds[C_VISUAL_VI] ||
633f0957ccaSPeter Wemm 	    ecp->cmd == &cmds[C_VSPLIT]) {
634b8ba871bSPeter Wemm 		/*
635b8ba871bSPeter Wemm 		 * Move to the next non-whitespace character.  A '!'
636b8ba871bSPeter Wemm 		 * immediately following the command is eaten as a
637b8ba871bSPeter Wemm 		 * force flag.
638b8ba871bSPeter Wemm 		 */
639b8ba871bSPeter Wemm 		if (ecp->clen > 0 && *ecp->cp == '!') {
640b8ba871bSPeter Wemm 			++ecp->cp;
641b8ba871bSPeter Wemm 			--ecp->clen;
642b8ba871bSPeter Wemm 			FL_SET(ecp->iflags, E_C_FORCE);
643b8ba871bSPeter Wemm 
644b8ba871bSPeter Wemm 			/* Reset, don't reparse. */
645b8ba871bSPeter Wemm 			ecp->save_cmd = ecp->cp;
646b8ba871bSPeter Wemm 		}
647b8ba871bSPeter Wemm 		for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
648f0957ccaSPeter Wemm 			if (!cmdskip(*ecp->cp))
649b8ba871bSPeter Wemm 				break;
650b8ba871bSPeter Wemm 		/*
651b8ba871bSPeter Wemm 		 * QUOTING NOTE:
652b8ba871bSPeter Wemm 		 *
653b8ba871bSPeter Wemm 		 * The historic implementation ignored all escape characters
654b8ba871bSPeter Wemm 		 * so there was no way to put a space or newline into the +cmd
655b8ba871bSPeter Wemm 		 * field.  We do a simplistic job of fixing it by moving to the
656b8ba871bSPeter Wemm 		 * first whitespace character that isn't escaped.  The escaping
657b8ba871bSPeter Wemm 		 * characters are stripped as no longer useful.
658b8ba871bSPeter Wemm 		 */
659b8ba871bSPeter Wemm 		if (ecp->clen > 0 && *ecp->cp == '+') {
660b8ba871bSPeter Wemm 			++ecp->cp;
661b8ba871bSPeter Wemm 			--ecp->clen;
662b8ba871bSPeter Wemm 			for (arg1 = p = ecp->cp;
663b8ba871bSPeter Wemm 			    ecp->clen > 0; --ecp->clen, ++ecp->cp) {
664b8ba871bSPeter Wemm 				ch = *ecp->cp;
665b8ba871bSPeter Wemm 				if (IS_ESCAPE(sp, ecp, ch) &&
666b8ba871bSPeter Wemm 				    ecp->clen > 1) {
667b8ba871bSPeter Wemm 					++discard;
668b8ba871bSPeter Wemm 					--ecp->clen;
669b8ba871bSPeter Wemm 					ch = *++ecp->cp;
670f0957ccaSPeter Wemm 				} else if (cmdskip(ch))
671b8ba871bSPeter Wemm 					break;
672b8ba871bSPeter Wemm 				*p++ = ch;
673b8ba871bSPeter Wemm 			}
674b8ba871bSPeter Wemm 			arg1_len = ecp->cp - arg1;
675b8ba871bSPeter Wemm 
676b8ba871bSPeter Wemm 			/* Reset, so the first argument isn't reparsed. */
677b8ba871bSPeter Wemm 			ecp->save_cmd = ecp->cp;
678b8ba871bSPeter Wemm 		}
679b8ba871bSPeter Wemm 	} else if (ecp->cmd == &cmds[C_BANG] ||
680b8ba871bSPeter Wemm 	    ecp->cmd == &cmds[C_GLOBAL] || ecp->cmd == &cmds[C_V]) {
681b8ba871bSPeter Wemm 		/*
682b8ba871bSPeter Wemm 		 * QUOTING NOTE:
683b8ba871bSPeter Wemm 		 *
684b8ba871bSPeter Wemm 		 * We use backslashes to escape <newline> characters, although
685b8ba871bSPeter Wemm 		 * this wasn't historic practice for the bang command.  It was
686b8ba871bSPeter Wemm 		 * for the global and v commands, and it's common usage when
687b8ba871bSPeter Wemm 		 * doing text insert during the command.  Escaping characters
688b8ba871bSPeter Wemm 		 * are stripped as no longer useful.
689b8ba871bSPeter Wemm 		 */
690b8ba871bSPeter Wemm 		for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
691b8ba871bSPeter Wemm 			ch = *ecp->cp;
692b8ba871bSPeter Wemm 			if (ch == '\\' && ecp->clen > 1 && ecp->cp[1] == '\n') {
693b8ba871bSPeter Wemm 				++discard;
694b8ba871bSPeter Wemm 				--ecp->clen;
695b8ba871bSPeter Wemm 				ch = *++ecp->cp;
696b8ba871bSPeter Wemm 
697b8ba871bSPeter Wemm 				++gp->if_lno;
698b8ba871bSPeter Wemm 				++ecp->if_lno;
699b8ba871bSPeter Wemm 			} else if (ch == '\n')
700b8ba871bSPeter Wemm 				break;
701b8ba871bSPeter Wemm 			*p++ = ch;
702b8ba871bSPeter Wemm 		}
703b8ba871bSPeter Wemm 	} else if (ecp->cmd == &cmds[C_READ] || ecp->cmd == &cmds[C_WRITE]) {
704b8ba871bSPeter Wemm 		/*
705b8ba871bSPeter Wemm 		 * For write commands, if the next character is a <blank>, and
706b8ba871bSPeter Wemm 		 * the next non-blank character is a '!', it's a filter command
707b8ba871bSPeter Wemm 		 * and we want to eat everything up to the <newline>.  For read
708b8ba871bSPeter Wemm 		 * commands, if the next non-blank character is a '!', it's a
709b8ba871bSPeter Wemm 		 * filter command and we want to eat everything up to the next
710b8ba871bSPeter Wemm 		 * <newline>.  Otherwise, we're done.
711b8ba871bSPeter Wemm 		 */
712b8ba871bSPeter Wemm 		for (tmp = 0; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
713b8ba871bSPeter Wemm 			ch = *ecp->cp;
714f0957ccaSPeter Wemm 			if (cmdskip(ch))
715b8ba871bSPeter Wemm 				tmp = 1;
716b8ba871bSPeter Wemm 			else
717b8ba871bSPeter Wemm 				break;
718b8ba871bSPeter Wemm 		}
719b8ba871bSPeter Wemm 		if (ecp->clen > 0 && ch == '!' &&
720b8ba871bSPeter Wemm 		    (ecp->cmd == &cmds[C_READ] || tmp))
721b8ba871bSPeter Wemm 			for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
722b8ba871bSPeter Wemm 				if (ecp->cp[0] == '\n')
723b8ba871bSPeter Wemm 					break;
724b8ba871bSPeter Wemm 	} else if (ecp->cmd == &cmds[C_SUBSTITUTE]) {
725b8ba871bSPeter Wemm 		/*
726b8ba871bSPeter Wemm 		 * Move to the next non-whitespace character, we'll use it as
727b8ba871bSPeter Wemm 		 * the delimiter.  If the character isn't an alphanumeric or
728b8ba871bSPeter Wemm 		 * a '|', it's the delimiter, so parse it.  Otherwise, we're
729b8ba871bSPeter Wemm 		 * into something like ":s g", so use the special s command.
730b8ba871bSPeter Wemm 		 */
731b8ba871bSPeter Wemm 		for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
732f0957ccaSPeter Wemm 			if (!cmdskip(ecp->cp[0]))
733b8ba871bSPeter Wemm 				break;
734b8ba871bSPeter Wemm 
735110d525eSBaptiste Daroussin 		if (is09azAZ(ecp->cp[0]) || ecp->cp[0] == '|') {
736b8ba871bSPeter Wemm 			ecp->rcmd = cmds[C_SUBSTITUTE];
737b8ba871bSPeter Wemm 			ecp->rcmd.fn = ex_subagain;
738b8ba871bSPeter Wemm 			ecp->cmd = &ecp->rcmd;
739b8ba871bSPeter Wemm 		} else if (ecp->clen > 0) {
740b8ba871bSPeter Wemm 			/*
741b8ba871bSPeter Wemm 			 * QUOTING NOTE:
742b8ba871bSPeter Wemm 			 *
743b8ba871bSPeter Wemm 			 * Backslashes quote delimiter characters for RE's.
744b8ba871bSPeter Wemm 			 * The backslashes are NOT removed since they'll be
745b8ba871bSPeter Wemm 			 * used by the RE code.  Move to the third delimiter
746b8ba871bSPeter Wemm 			 * that's not escaped (or the end of the command).
747b8ba871bSPeter Wemm 			 */
748b8ba871bSPeter Wemm 			delim = *ecp->cp;
749b8ba871bSPeter Wemm 			++ecp->cp;
750b8ba871bSPeter Wemm 			--ecp->clen;
751b8ba871bSPeter Wemm 			for (cnt = 2; ecp->clen > 0 &&
752b8ba871bSPeter Wemm 			    cnt != 0; --ecp->clen, ++ecp->cp)
753b8ba871bSPeter Wemm 				if (ecp->cp[0] == '\\' &&
754b8ba871bSPeter Wemm 				    ecp->clen > 1) {
755b8ba871bSPeter Wemm 					++ecp->cp;
756b8ba871bSPeter Wemm 					--ecp->clen;
757b8ba871bSPeter Wemm 				} else if (ecp->cp[0] == delim)
758b8ba871bSPeter Wemm 					--cnt;
759b8ba871bSPeter Wemm 		}
760b8ba871bSPeter Wemm 	}
761b8ba871bSPeter Wemm 
762b8ba871bSPeter Wemm 	/*
763b8ba871bSPeter Wemm 	 * Use normal quoting and termination rules to find the end of this
764b8ba871bSPeter Wemm 	 * command.
765b8ba871bSPeter Wemm 	 *
766b8ba871bSPeter Wemm 	 * QUOTING NOTE:
767b8ba871bSPeter Wemm 	 *
768b8ba871bSPeter Wemm 	 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
769b8ba871bSPeter Wemm 	 * file.  It was almost certainly a bug, but that's what bug-for-bug
770b8ba871bSPeter Wemm 	 * compatibility means, Grasshopper.  Also, ^V's escape the command
771b8ba871bSPeter Wemm 	 * delimiters.  Literal next quote characters in front of the newlines,
772b8ba871bSPeter Wemm 	 * '|' characters or literal next characters are stripped as they're
773b8ba871bSPeter Wemm 	 * no longer useful.
774b8ba871bSPeter Wemm 	 */
775b8ba871bSPeter Wemm 	vi_address = ecp->clen != 0 && ecp->cp[0] != '\n';
776b8ba871bSPeter Wemm 	for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
777b8ba871bSPeter Wemm 		ch = ecp->cp[0];
778b8ba871bSPeter Wemm 		if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
779f0957ccaSPeter Wemm 			CHAR_T tmp = ecp->cp[1];
780b8ba871bSPeter Wemm 			if (tmp == '\n' || tmp == '|') {
781b8ba871bSPeter Wemm 				if (tmp == '\n') {
782b8ba871bSPeter Wemm 					++gp->if_lno;
783b8ba871bSPeter Wemm 					++ecp->if_lno;
784b8ba871bSPeter Wemm 				}
785b8ba871bSPeter Wemm 				++discard;
786b8ba871bSPeter Wemm 				--ecp->clen;
787b8ba871bSPeter Wemm 				++ecp->cp;
788b8ba871bSPeter Wemm 				ch = tmp;
789b8ba871bSPeter Wemm 			}
790b8ba871bSPeter Wemm 		} else if (ch == '\n' || ch == '|') {
791b8ba871bSPeter Wemm 			if (ch == '\n')
792b8ba871bSPeter Wemm 				F_SET(ecp, E_NEWLINE);
793b8ba871bSPeter Wemm 			--ecp->clen;
794b8ba871bSPeter Wemm 			break;
795b8ba871bSPeter Wemm 		}
796b8ba871bSPeter Wemm 		*p++ = ch;
797b8ba871bSPeter Wemm 	}
798b8ba871bSPeter Wemm 
799b8ba871bSPeter Wemm 	/*
800b8ba871bSPeter Wemm 	 * Save off the next command information, go back to the
801b8ba871bSPeter Wemm 	 * original start of the command.
802b8ba871bSPeter Wemm 	 */
803b8ba871bSPeter Wemm 	p = ecp->cp + 1;
804b8ba871bSPeter Wemm 	ecp->cp = ecp->save_cmd;
805b8ba871bSPeter Wemm 	ecp->save_cmd = p;
806b8ba871bSPeter Wemm 	ecp->save_cmdlen = ecp->clen;
807b8ba871bSPeter Wemm 	ecp->clen = ((ecp->save_cmd - ecp->cp) - 1) - discard;
808b8ba871bSPeter Wemm 
809b8ba871bSPeter Wemm 	/*
810b8ba871bSPeter Wemm 	 * QUOTING NOTE:
811b8ba871bSPeter Wemm 	 *
812b8ba871bSPeter Wemm 	 * The "set tags" command historically used a backslash, not the
813b8ba871bSPeter Wemm 	 * user's literal next character, to escape whitespace.  Handle
814b8ba871bSPeter Wemm 	 * it here instead of complicating the argv_exp3() code.  Note,
815b8ba871bSPeter Wemm 	 * this isn't a particularly complex trap, and if backslashes were
816b8ba871bSPeter Wemm 	 * legal in set commands, this would have to be much more complicated.
817b8ba871bSPeter Wemm 	 */
818755cc40cSBaptiste Daroussin 	if (ecp->cmd == &cmds[C_SET]) {
819b8ba871bSPeter Wemm 		for (p = ecp->cp, len = ecp->clen; len > 0; --len, ++p)
820f0957ccaSPeter Wemm 			if (IS_ESCAPE(sp, ecp, *p) && len > 1) {
821f0957ccaSPeter Wemm 				--len;
822f0957ccaSPeter Wemm 				++p;
823f0957ccaSPeter Wemm 			} else if (*p == '\\')
824b8ba871bSPeter Wemm 				*p = CH_LITERAL;
825755cc40cSBaptiste Daroussin 	}
826b8ba871bSPeter Wemm 
827b8ba871bSPeter Wemm 	/*
828b8ba871bSPeter Wemm 	 * Set the default addresses.  It's an error to specify an address for
829b8ba871bSPeter Wemm 	 * a command that doesn't take them.  If two addresses are specified
830b8ba871bSPeter Wemm 	 * for a command that only takes one, lose the first one.  Two special
831b8ba871bSPeter Wemm 	 * cases here, some commands take 0 or 2 addresses.  For most of them
832b8ba871bSPeter Wemm 	 * (the E_ADDR2_ALL flag), 0 defaults to the entire file.  For one
833b8ba871bSPeter Wemm 	 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
834b8ba871bSPeter Wemm 	 *
835b8ba871bSPeter Wemm 	 * Also, if the file is empty, some commands want to use an address of
836b8ba871bSPeter Wemm 	 * 0, i.e. the entire file is 0 to 0, and the default first address is
837b8ba871bSPeter Wemm 	 * 0.  Otherwise, an entire file is 1 to N and the default line is 1.
838b8ba871bSPeter Wemm 	 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
839b8ba871bSPeter Wemm 	 * case where the 0 address is only valid if it's a default address.
840b8ba871bSPeter Wemm 	 *
841b8ba871bSPeter Wemm 	 * Also, set a flag if we set the default addresses.  Some commands
842b8ba871bSPeter Wemm 	 * (ex: z) care if the user specified an address or if we just used
843b8ba871bSPeter Wemm 	 * the current cursor.
844b8ba871bSPeter Wemm 	 */
845b8ba871bSPeter Wemm 	switch (F_ISSET(ecp, E_ADDR1 | E_ADDR2 | E_ADDR2_ALL | E_ADDR2_NONE)) {
846b8ba871bSPeter Wemm 	case E_ADDR1:				/* One address: */
847b8ba871bSPeter Wemm 		switch (ecp->addrcnt) {
848b8ba871bSPeter Wemm 		case 0:				/* Default cursor/empty file. */
849b8ba871bSPeter Wemm 			ecp->addrcnt = 1;
850b8ba871bSPeter Wemm 			F_SET(ecp, E_ADDR_DEF);
851b8ba871bSPeter Wemm 			if (F_ISSET(ecp, E_ADDR_ZERODEF)) {
852b8ba871bSPeter Wemm 				if (db_last(sp, &lno))
853b8ba871bSPeter Wemm 					goto err;
854b8ba871bSPeter Wemm 				if (lno == 0) {
855b8ba871bSPeter Wemm 					ecp->addr1.lno = 0;
856b8ba871bSPeter Wemm 					F_SET(ecp, E_ADDR_ZERO);
857b8ba871bSPeter Wemm 				} else
858b8ba871bSPeter Wemm 					ecp->addr1.lno = sp->lno;
859b8ba871bSPeter Wemm 			} else
860b8ba871bSPeter Wemm 				ecp->addr1.lno = sp->lno;
861b8ba871bSPeter Wemm 			ecp->addr1.cno = sp->cno;
862b8ba871bSPeter Wemm 			break;
863b8ba871bSPeter Wemm 		case 1:
864b8ba871bSPeter Wemm 			break;
865b8ba871bSPeter Wemm 		case 2:				/* Lose the first address. */
866b8ba871bSPeter Wemm 			ecp->addrcnt = 1;
867b8ba871bSPeter Wemm 			ecp->addr1 = ecp->addr2;
868b8ba871bSPeter Wemm 		}
869b8ba871bSPeter Wemm 		break;
870b8ba871bSPeter Wemm 	case E_ADDR2_NONE:			/* Zero/two addresses: */
871b8ba871bSPeter Wemm 		if (ecp->addrcnt == 0)		/* Default to nothing. */
872b8ba871bSPeter Wemm 			break;
873b8ba871bSPeter Wemm 		goto two_addr;
874b8ba871bSPeter Wemm 	case E_ADDR2_ALL:			/* Zero/two addresses: */
875b8ba871bSPeter Wemm 		if (ecp->addrcnt == 0) {	/* Default entire/empty file. */
876b8ba871bSPeter Wemm 			F_SET(ecp, E_ADDR_DEF);
877b8ba871bSPeter Wemm 			ecp->addrcnt = 2;
878b8ba871bSPeter Wemm 			if (sp->ep == NULL)
879b8ba871bSPeter Wemm 				ecp->addr2.lno = 0;
880b8ba871bSPeter Wemm 			else if (db_last(sp, &ecp->addr2.lno))
881b8ba871bSPeter Wemm 				goto err;
882b8ba871bSPeter Wemm 			if (F_ISSET(ecp, E_ADDR_ZERODEF) &&
883b8ba871bSPeter Wemm 			    ecp->addr2.lno == 0) {
884b8ba871bSPeter Wemm 				ecp->addr1.lno = 0;
885b8ba871bSPeter Wemm 				F_SET(ecp, E_ADDR_ZERO);
886b8ba871bSPeter Wemm 			} else
887b8ba871bSPeter Wemm 				ecp->addr1.lno = 1;
888b8ba871bSPeter Wemm 			ecp->addr1.cno = ecp->addr2.cno = 0;
889b8ba871bSPeter Wemm 			F_SET(ecp, E_ADDR2_ALL);
890b8ba871bSPeter Wemm 			break;
891b8ba871bSPeter Wemm 		}
892b8ba871bSPeter Wemm 		/* FALLTHROUGH */
893b8ba871bSPeter Wemm 	case E_ADDR2:				/* Two addresses: */
894b8ba871bSPeter Wemm two_addr:	switch (ecp->addrcnt) {
895b8ba871bSPeter Wemm 		case 0:				/* Default cursor/empty file. */
896b8ba871bSPeter Wemm 			ecp->addrcnt = 2;
897b8ba871bSPeter Wemm 			F_SET(ecp, E_ADDR_DEF);
898b8ba871bSPeter Wemm 			if (sp->lno == 1 &&
899b8ba871bSPeter Wemm 			    F_ISSET(ecp, E_ADDR_ZERODEF)) {
900b8ba871bSPeter Wemm 				if (db_last(sp, &lno))
901b8ba871bSPeter Wemm 					goto err;
902b8ba871bSPeter Wemm 				if (lno == 0) {
903b8ba871bSPeter Wemm 					ecp->addr1.lno = ecp->addr2.lno = 0;
904b8ba871bSPeter Wemm 					F_SET(ecp, E_ADDR_ZERO);
905b8ba871bSPeter Wemm 				} else
906b8ba871bSPeter Wemm 					ecp->addr1.lno =
907b8ba871bSPeter Wemm 					    ecp->addr2.lno = sp->lno;
908b8ba871bSPeter Wemm 			} else
909b8ba871bSPeter Wemm 				ecp->addr1.lno = ecp->addr2.lno = sp->lno;
910b8ba871bSPeter Wemm 			ecp->addr1.cno = ecp->addr2.cno = sp->cno;
911b8ba871bSPeter Wemm 			break;
912b8ba871bSPeter Wemm 		case 1:				/* Default to first address. */
913b8ba871bSPeter Wemm 			ecp->addrcnt = 2;
914b8ba871bSPeter Wemm 			ecp->addr2 = ecp->addr1;
915b8ba871bSPeter Wemm 			break;
916b8ba871bSPeter Wemm 		case 2:
917b8ba871bSPeter Wemm 			break;
918b8ba871bSPeter Wemm 		}
919b8ba871bSPeter Wemm 		break;
920b8ba871bSPeter Wemm 	default:
921b8ba871bSPeter Wemm 		if (ecp->addrcnt)		/* Error. */
922b8ba871bSPeter Wemm 			goto usage;
923b8ba871bSPeter Wemm 	}
924b8ba871bSPeter Wemm 
925b8ba871bSPeter Wemm 	/*
926b8ba871bSPeter Wemm 	 * !!!
927b8ba871bSPeter Wemm 	 * The ^D scroll command historically scrolled the value of the scroll
928b8ba871bSPeter Wemm 	 * option or to EOF.  It was an error if the cursor was already at EOF.
929b8ba871bSPeter Wemm 	 * (Leading addresses were permitted, but were then ignored.)
930b8ba871bSPeter Wemm 	 */
931b8ba871bSPeter Wemm 	if (ecp->cmd == &cmds[C_SCROLL]) {
932b8ba871bSPeter Wemm 		ecp->addrcnt = 2;
933b8ba871bSPeter Wemm 		ecp->addr1.lno = sp->lno + 1;
934b8ba871bSPeter Wemm 		ecp->addr2.lno = sp->lno + O_VAL(sp, O_SCROLL);
935b8ba871bSPeter Wemm 		ecp->addr1.cno = ecp->addr2.cno = sp->cno;
936b8ba871bSPeter Wemm 		if (db_last(sp, &lno))
937b8ba871bSPeter Wemm 			goto err;
938b8ba871bSPeter Wemm 		if (lno != 0 && lno > sp->lno && ecp->addr2.lno > lno)
939b8ba871bSPeter Wemm 			ecp->addr2.lno = lno;
940b8ba871bSPeter Wemm 	}
941b8ba871bSPeter Wemm 
942b8ba871bSPeter Wemm 	ecp->flagoff = 0;
943f0957ccaSPeter Wemm 	for (np = ecp->cmd->syntax; *np != '\0'; ++np) {
944b8ba871bSPeter Wemm 		/*
945b8ba871bSPeter Wemm 		 * The force flag is sensitive to leading whitespace, i.e.
946b8ba871bSPeter Wemm 		 * "next !" is different from "next!".  Handle it before
947b8ba871bSPeter Wemm 		 * skipping leading <blank>s.
948b8ba871bSPeter Wemm 		 */
949f0957ccaSPeter Wemm 		if (*np == '!') {
950b8ba871bSPeter Wemm 			if (ecp->clen > 0 && *ecp->cp == '!') {
951b8ba871bSPeter Wemm 				++ecp->cp;
952b8ba871bSPeter Wemm 				--ecp->clen;
953b8ba871bSPeter Wemm 				FL_SET(ecp->iflags, E_C_FORCE);
954b8ba871bSPeter Wemm 			}
955b8ba871bSPeter Wemm 			continue;
956b8ba871bSPeter Wemm 		}
957b8ba871bSPeter Wemm 
958b8ba871bSPeter Wemm 		/* Skip leading <blank>s. */
959b8ba871bSPeter Wemm 		for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
960f0957ccaSPeter Wemm 			if (!cmdskip(*ecp->cp))
961b8ba871bSPeter Wemm 				break;
962b8ba871bSPeter Wemm 		if (ecp->clen == 0)
963b8ba871bSPeter Wemm 			break;
964b8ba871bSPeter Wemm 
965f0957ccaSPeter Wemm 		switch (*np) {
966b8ba871bSPeter Wemm 		case '1':				/* +, -, #, l, p */
967b8ba871bSPeter Wemm 			/*
968b8ba871bSPeter Wemm 			 * !!!
969b8ba871bSPeter Wemm 			 * Historically, some flags were ignored depending
970b8ba871bSPeter Wemm 			 * on where they occurred in the command line.  For
971b8ba871bSPeter Wemm 			 * example, in the command, ":3+++p--#", historic vi
972b8ba871bSPeter Wemm 			 * acted on the '#' flag, but ignored the '-' flags.
973b8ba871bSPeter Wemm 			 * It's unambiguous what the flags mean, so we just
974b8ba871bSPeter Wemm 			 * handle them regardless of the stupidity of their
975b8ba871bSPeter Wemm 			 * location.
976b8ba871bSPeter Wemm 			 */
977b8ba871bSPeter Wemm 			for (; ecp->clen; --ecp->clen, ++ecp->cp)
978b8ba871bSPeter Wemm 				switch (*ecp->cp) {
979b8ba871bSPeter Wemm 				case '+':
980b8ba871bSPeter Wemm 					++ecp->flagoff;
981b8ba871bSPeter Wemm 					break;
982b8ba871bSPeter Wemm 				case '-':
983b8ba871bSPeter Wemm 				case '^':
984b8ba871bSPeter Wemm 					--ecp->flagoff;
985b8ba871bSPeter Wemm 					break;
986b8ba871bSPeter Wemm 				case '#':
987b8ba871bSPeter Wemm 					F_CLR(ecp, E_OPTNUM);
988b8ba871bSPeter Wemm 					FL_SET(ecp->iflags, E_C_HASH);
989b8ba871bSPeter Wemm 					exp->fdef |= E_C_HASH;
990b8ba871bSPeter Wemm 					break;
991b8ba871bSPeter Wemm 				case 'l':
992b8ba871bSPeter Wemm 					FL_SET(ecp->iflags, E_C_LIST);
993b8ba871bSPeter Wemm 					exp->fdef |= E_C_LIST;
994b8ba871bSPeter Wemm 					break;
995b8ba871bSPeter Wemm 				case 'p':
996b8ba871bSPeter Wemm 					FL_SET(ecp->iflags, E_C_PRINT);
997b8ba871bSPeter Wemm 					exp->fdef |= E_C_PRINT;
998b8ba871bSPeter Wemm 					break;
999b8ba871bSPeter Wemm 				default:
1000b8ba871bSPeter Wemm 					goto end_case1;
1001b8ba871bSPeter Wemm 				}
1002b8ba871bSPeter Wemm end_case1:		break;
1003b8ba871bSPeter Wemm 		case '2':				/* -, ., +, ^ */
1004b8ba871bSPeter Wemm 		case '3':				/* -, ., +, ^, = */
1005b8ba871bSPeter Wemm 			for (; ecp->clen; --ecp->clen, ++ecp->cp)
1006b8ba871bSPeter Wemm 				switch (*ecp->cp) {
1007b8ba871bSPeter Wemm 				case '-':
1008b8ba871bSPeter Wemm 					FL_SET(ecp->iflags, E_C_DASH);
1009b8ba871bSPeter Wemm 					break;
1010b8ba871bSPeter Wemm 				case '.':
1011b8ba871bSPeter Wemm 					FL_SET(ecp->iflags, E_C_DOT);
1012b8ba871bSPeter Wemm 					break;
1013b8ba871bSPeter Wemm 				case '+':
1014b8ba871bSPeter Wemm 					FL_SET(ecp->iflags, E_C_PLUS);
1015b8ba871bSPeter Wemm 					break;
1016b8ba871bSPeter Wemm 				case '^':
1017b8ba871bSPeter Wemm 					FL_SET(ecp->iflags, E_C_CARAT);
1018b8ba871bSPeter Wemm 					break;
1019b8ba871bSPeter Wemm 				case '=':
1020f0957ccaSPeter Wemm 					if (*np == '3') {
1021b8ba871bSPeter Wemm 						FL_SET(ecp->iflags, E_C_EQUAL);
1022b8ba871bSPeter Wemm 						break;
1023b8ba871bSPeter Wemm 					}
1024b8ba871bSPeter Wemm 					/* FALLTHROUGH */
1025b8ba871bSPeter Wemm 				default:
1026b8ba871bSPeter Wemm 					goto end_case23;
1027b8ba871bSPeter Wemm 				}
1028b8ba871bSPeter Wemm end_case23:		break;
1029b8ba871bSPeter Wemm 		case 'b':				/* buffer */
1030b8ba871bSPeter Wemm 			/*
1031b8ba871bSPeter Wemm 			 * !!!
1032b8ba871bSPeter Wemm 			 * Historically, "d #" was a delete with a flag, not a
1033b8ba871bSPeter Wemm 			 * delete into the '#' buffer.  If the current command
1034b8ba871bSPeter Wemm 			 * permits a flag, don't use one as a buffer.  However,
1035b8ba871bSPeter Wemm 			 * the 'l' and 'p' flags were legal buffer names in the
1036b8ba871bSPeter Wemm 			 * historic ex, and were used as buffers, not flags.
1037b8ba871bSPeter Wemm 			 */
1038b8ba871bSPeter Wemm 			if ((ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1039b8ba871bSPeter Wemm 			    ecp->cp[0] == '^' || ecp->cp[0] == '#') &&
1040f0957ccaSPeter Wemm 			    strchr(np, '1') != NULL)
1041b8ba871bSPeter Wemm 				break;
1042b8ba871bSPeter Wemm 			/*
1043b8ba871bSPeter Wemm 			 * !!!
1044b8ba871bSPeter Wemm 			 * Digits can't be buffer names in ex commands, or the
1045b8ba871bSPeter Wemm 			 * command "d2" would be a delete into buffer '2', and
1046b8ba871bSPeter Wemm 			 * not a two-line deletion.
1047b8ba871bSPeter Wemm 			 */
1048f0957ccaSPeter Wemm 			if (!ISDIGIT(ecp->cp[0])) {
1049b8ba871bSPeter Wemm 				ecp->buffer = *ecp->cp;
1050b8ba871bSPeter Wemm 				++ecp->cp;
1051b8ba871bSPeter Wemm 				--ecp->clen;
1052b8ba871bSPeter Wemm 				FL_SET(ecp->iflags, E_C_BUFFER);
1053b8ba871bSPeter Wemm 			}
1054b8ba871bSPeter Wemm 			break;
1055b8ba871bSPeter Wemm 		case 'c':				/* count [01+a] */
1056f0957ccaSPeter Wemm 			++np;
1057b8ba871bSPeter Wemm 			/* Validate any signed value. */
1058f0957ccaSPeter Wemm 			if (!ISDIGIT(*ecp->cp) && (*np != '+' ||
1059b8ba871bSPeter Wemm 			    (*ecp->cp != '+' && *ecp->cp != '-')))
1060b8ba871bSPeter Wemm 				break;
1061b8ba871bSPeter Wemm 			/* If a signed value, set appropriate flags. */
1062b8ba871bSPeter Wemm 			if (*ecp->cp == '-')
1063b8ba871bSPeter Wemm 				FL_SET(ecp->iflags, E_C_COUNT_NEG);
1064b8ba871bSPeter Wemm 			else if (*ecp->cp == '+')
1065b8ba871bSPeter Wemm 				FL_SET(ecp->iflags, E_C_COUNT_POS);
1066b8ba871bSPeter Wemm 			if ((nret =
1067b8ba871bSPeter Wemm 			    nget_slong(&ltmp, ecp->cp, &t, 10)) != NUM_OK) {
1068b8ba871bSPeter Wemm 				ex_badaddr(sp, NULL, A_NOTSET, nret);
1069b8ba871bSPeter Wemm 				goto err;
1070b8ba871bSPeter Wemm 			}
1071f0957ccaSPeter Wemm 			if (ltmp == 0 && *np != '0') {
1072b8ba871bSPeter Wemm 				msgq(sp, M_ERR, "083|Count may not be zero");
1073b8ba871bSPeter Wemm 				goto err;
1074b8ba871bSPeter Wemm 			}
1075b8ba871bSPeter Wemm 			ecp->clen -= (t - ecp->cp);
1076b8ba871bSPeter Wemm 			ecp->cp = t;
1077b8ba871bSPeter Wemm 
1078b8ba871bSPeter Wemm 			/*
1079b8ba871bSPeter Wemm 			 * Counts as address offsets occur in commands taking
1080b8ba871bSPeter Wemm 			 * two addresses.  Historic vi practice was to use
1081b8ba871bSPeter Wemm 			 * the count as an offset from the *second* address.
1082b8ba871bSPeter Wemm 			 *
1083b8ba871bSPeter Wemm 			 * Set a count flag; some underlying commands (see
1084b8ba871bSPeter Wemm 			 * join) do different things with counts than with
1085b8ba871bSPeter Wemm 			 * line addresses.
1086b8ba871bSPeter Wemm 			 */
1087f0957ccaSPeter Wemm 			if (*np == 'a') {
1088b8ba871bSPeter Wemm 				ecp->addr1 = ecp->addr2;
1089b8ba871bSPeter Wemm 				ecp->addr2.lno = ecp->addr1.lno + ltmp - 1;
1090b8ba871bSPeter Wemm 			} else
1091b8ba871bSPeter Wemm 				ecp->count = ltmp;
1092b8ba871bSPeter Wemm 			FL_SET(ecp->iflags, E_C_COUNT);
1093b8ba871bSPeter Wemm 			break;
1094b8ba871bSPeter Wemm 		case 'f':				/* file */
1095b8ba871bSPeter Wemm 			if (argv_exp2(sp, ecp, ecp->cp, ecp->clen))
1096b8ba871bSPeter Wemm 				goto err;
1097b8ba871bSPeter Wemm 			goto arg_cnt_chk;
1098b8ba871bSPeter Wemm 		case 'l':				/* line */
1099b8ba871bSPeter Wemm 			/*
1100b8ba871bSPeter Wemm 			 * Get a line specification.
1101b8ba871bSPeter Wemm 			 *
1102b8ba871bSPeter Wemm 			 * If the line was a search expression, we may have
1103b8ba871bSPeter Wemm 			 * changed state during the call, and we're now
1104b8ba871bSPeter Wemm 			 * searching the file.  Push ourselves onto the state
1105b8ba871bSPeter Wemm 			 * stack.
1106b8ba871bSPeter Wemm 			 */
1107b8ba871bSPeter Wemm 			if (ex_line(sp, ecp, &cur, &isaddr, &tmp))
1108b8ba871bSPeter Wemm 				goto rfail;
1109b8ba871bSPeter Wemm 			if (tmp)
1110b8ba871bSPeter Wemm 				goto err;
1111b8ba871bSPeter Wemm 
1112b8ba871bSPeter Wemm 			/* Line specifications are always required. */
1113b8ba871bSPeter Wemm 			if (!isaddr) {
1114f0957ccaSPeter Wemm 				msgq_wstr(sp, M_ERR, ecp->cp,
1115b8ba871bSPeter Wemm 				     "084|%s: bad line specification");
1116b8ba871bSPeter Wemm 				goto err;
1117b8ba871bSPeter Wemm 			}
1118b8ba871bSPeter Wemm 			/*
1119b8ba871bSPeter Wemm 			 * The target line should exist for these commands,
1120b8ba871bSPeter Wemm 			 * but 0 is legal for them as well.
1121b8ba871bSPeter Wemm 			 */
1122b8ba871bSPeter Wemm 			if (cur.lno != 0 && !db_exist(sp, cur.lno)) {
1123b8ba871bSPeter Wemm 				ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1124b8ba871bSPeter Wemm 				goto err;
1125b8ba871bSPeter Wemm 			}
1126b8ba871bSPeter Wemm 			ecp->lineno = cur.lno;
1127b8ba871bSPeter Wemm 			break;
1128b8ba871bSPeter Wemm 		case 'S':				/* string, file exp. */
1129b8ba871bSPeter Wemm 			if (ecp->clen != 0) {
1130b8ba871bSPeter Wemm 				if (argv_exp1(sp, ecp, ecp->cp,
1131b8ba871bSPeter Wemm 				    ecp->clen, ecp->cmd == &cmds[C_BANG]))
1132b8ba871bSPeter Wemm 					goto err;
1133b8ba871bSPeter Wemm 				goto addr_verify;
1134b8ba871bSPeter Wemm 			}
1135b8ba871bSPeter Wemm 			/* FALLTHROUGH */
1136b8ba871bSPeter Wemm 		case 's':				/* string */
1137b8ba871bSPeter Wemm 			if (argv_exp0(sp, ecp, ecp->cp, ecp->clen))
1138b8ba871bSPeter Wemm 				goto err;
1139b8ba871bSPeter Wemm 			goto addr_verify;
1140b8ba871bSPeter Wemm 		case 'W':				/* word string */
1141b8ba871bSPeter Wemm 			/*
1142b8ba871bSPeter Wemm 			 * QUOTING NOTE:
1143b8ba871bSPeter Wemm 			 *
1144b8ba871bSPeter Wemm 			 * Literal next characters escape the following
1145b8ba871bSPeter Wemm 			 * character.  Quoting characters are stripped here
1146b8ba871bSPeter Wemm 			 * since they are no longer useful.
1147b8ba871bSPeter Wemm 			 *
1148b8ba871bSPeter Wemm 			 * First there was the word.
1149b8ba871bSPeter Wemm 			 */
1150b8ba871bSPeter Wemm 			for (p = t = ecp->cp;
1151b8ba871bSPeter Wemm 			    ecp->clen > 0; --ecp->clen, ++ecp->cp) {
1152b8ba871bSPeter Wemm 				ch = *ecp->cp;
1153b8ba871bSPeter Wemm 				if (IS_ESCAPE(sp,
1154b8ba871bSPeter Wemm 				    ecp, ch) && ecp->clen > 1) {
1155b8ba871bSPeter Wemm 					--ecp->clen;
1156b8ba871bSPeter Wemm 					*p++ = *++ecp->cp;
1157f0957ccaSPeter Wemm 				} else if (cmdskip(ch)) {
1158b8ba871bSPeter Wemm 					++ecp->cp;
1159b8ba871bSPeter Wemm 					--ecp->clen;
1160b8ba871bSPeter Wemm 					break;
1161b8ba871bSPeter Wemm 				} else
1162b8ba871bSPeter Wemm 					*p++ = ch;
1163b8ba871bSPeter Wemm 			}
1164b8ba871bSPeter Wemm 			if (argv_exp0(sp, ecp, t, p - t))
1165b8ba871bSPeter Wemm 				goto err;
1166b8ba871bSPeter Wemm 
1167b8ba871bSPeter Wemm 			/* Delete intervening whitespace. */
1168b8ba871bSPeter Wemm 			for (; ecp->clen > 0;
1169b8ba871bSPeter Wemm 			    --ecp->clen, ++ecp->cp) {
1170b8ba871bSPeter Wemm 				ch = *ecp->cp;
1171f0957ccaSPeter Wemm 				if (!cmdskip(ch))
1172b8ba871bSPeter Wemm 					break;
1173b8ba871bSPeter Wemm 			}
1174b8ba871bSPeter Wemm 			if (ecp->clen == 0)
1175b8ba871bSPeter Wemm 				goto usage;
1176b8ba871bSPeter Wemm 
1177b8ba871bSPeter Wemm 			/* Followed by the string. */
1178b8ba871bSPeter Wemm 			for (p = t = ecp->cp; ecp->clen > 0;
1179b8ba871bSPeter Wemm 			    --ecp->clen, ++ecp->cp, ++p) {
1180b8ba871bSPeter Wemm 				ch = *ecp->cp;
1181b8ba871bSPeter Wemm 				if (IS_ESCAPE(sp,
1182b8ba871bSPeter Wemm 				    ecp, ch) && ecp->clen > 1) {
1183b8ba871bSPeter Wemm 					--ecp->clen;
1184b8ba871bSPeter Wemm 					*p = *++ecp->cp;
1185b8ba871bSPeter Wemm 				} else
1186b8ba871bSPeter Wemm 					*p = ch;
1187b8ba871bSPeter Wemm 			}
1188b8ba871bSPeter Wemm 			if (argv_exp0(sp, ecp, t, p - t))
1189b8ba871bSPeter Wemm 				goto err;
1190b8ba871bSPeter Wemm 			goto addr_verify;
1191b8ba871bSPeter Wemm 		case 'w':				/* word */
1192b8ba871bSPeter Wemm 			if (argv_exp3(sp, ecp, ecp->cp, ecp->clen))
1193b8ba871bSPeter Wemm 				goto err;
1194f0957ccaSPeter Wemm arg_cnt_chk:		if (*++np != 'N') {		/* N */
1195b8ba871bSPeter Wemm 				/*
1196b8ba871bSPeter Wemm 				 * If a number is specified, must either be
1197b8ba871bSPeter Wemm 				 * 0 or that number, if optional, and that
1198b8ba871bSPeter Wemm 				 * number, if required.
1199b8ba871bSPeter Wemm 				 */
1200f0957ccaSPeter Wemm 				tmp = *np - '0';
1201f0957ccaSPeter Wemm 				if ((*++np != 'o' || exp->argsoff != 0) &&
1202b8ba871bSPeter Wemm 				    exp->argsoff != tmp)
1203b8ba871bSPeter Wemm 					goto usage;
1204b8ba871bSPeter Wemm 			}
1205b8ba871bSPeter Wemm 			goto addr_verify;
1206f0957ccaSPeter Wemm 		default: {
1207f0957ccaSPeter Wemm 			size_t nlen;
1208f0957ccaSPeter Wemm 			char *nstr;
1209f0957ccaSPeter Wemm 
1210f0957ccaSPeter Wemm 			INT2CHAR(sp, ecp->cmd->name, STRLEN(ecp->cmd->name) + 1,
1211f0957ccaSPeter Wemm 			    nstr, nlen);
1212b8ba871bSPeter Wemm 			msgq(sp, M_ERR,
1213b8ba871bSPeter Wemm 			    "085|Internal syntax table error (%s: %s)",
1214f0957ccaSPeter Wemm 			    nstr, KEY_NAME(sp, *np));
1215f0957ccaSPeter Wemm 		}
1216b8ba871bSPeter Wemm 		}
1217b8ba871bSPeter Wemm 	}
1218b8ba871bSPeter Wemm 
1219b8ba871bSPeter Wemm 	/* Skip trailing whitespace. */
1220b8ba871bSPeter Wemm 	for (; ecp->clen > 0; --ecp->clen) {
1221b8ba871bSPeter Wemm 		ch = *ecp->cp++;
1222f0957ccaSPeter Wemm 		if (!cmdskip(ch))
1223b8ba871bSPeter Wemm 			break;
1224b8ba871bSPeter Wemm 	}
1225b8ba871bSPeter Wemm 
1226b8ba871bSPeter Wemm 	/*
1227b8ba871bSPeter Wemm 	 * There shouldn't be anything left, and no more required fields,
1228b8ba871bSPeter Wemm 	 * i.e neither 'l' or 'r' in the syntax string.
1229b8ba871bSPeter Wemm 	 */
1230f0957ccaSPeter Wemm 	if (ecp->clen != 0 || strpbrk(np, "lr")) {
1231b8ba871bSPeter Wemm usage:		msgq(sp, M_ERR, "086|Usage: %s", ecp->cmd->usage);
1232b8ba871bSPeter Wemm 		goto err;
1233b8ba871bSPeter Wemm 	}
1234b8ba871bSPeter Wemm 
1235b8ba871bSPeter Wemm 	/*
1236b8ba871bSPeter Wemm 	 * Verify that the addresses are legal.  Check the addresses here,
1237b8ba871bSPeter Wemm 	 * because this is a place where all ex addresses pass through.
1238b8ba871bSPeter Wemm 	 * (They don't all pass through ex_line(), for instance.)  We're
1239b8ba871bSPeter Wemm 	 * assuming that any non-existent line doesn't exist because it's
1240b8ba871bSPeter Wemm 	 * past the end-of-file.  That's a pretty good guess.
1241b8ba871bSPeter Wemm 	 *
1242b8ba871bSPeter Wemm 	 * If it's a "default vi command", an address of zero is okay.
1243b8ba871bSPeter Wemm 	 */
1244b8ba871bSPeter Wemm addr_verify:
1245b8ba871bSPeter Wemm 	switch (ecp->addrcnt) {
1246b8ba871bSPeter Wemm 	case 2:
1247b8ba871bSPeter Wemm 		/*
1248b8ba871bSPeter Wemm 		 * Historic ex/vi permitted commands with counts to go past
1249b8ba871bSPeter Wemm 		 * EOF.  So, for example, if the file only had 5 lines, the
1250b8ba871bSPeter Wemm 		 * ex command "1,6>" would fail, but the command ">300"
1251b8ba871bSPeter Wemm 		 * would succeed.  Since we don't want to have to make all
1252b8ba871bSPeter Wemm 		 * of the underlying commands handle random line numbers,
1253b8ba871bSPeter Wemm 		 * fix it here.
1254b8ba871bSPeter Wemm 		 */
1255b8ba871bSPeter Wemm 		if (ecp->addr2.lno == 0) {
1256b8ba871bSPeter Wemm 			if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1257b8ba871bSPeter Wemm 			    (F_ISSET(sp, SC_EX) ||
1258b8ba871bSPeter Wemm 			    !F_ISSET(ecp, E_USELASTCMD))) {
1259b8ba871bSPeter Wemm 				ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1260b8ba871bSPeter Wemm 				goto err;
1261b8ba871bSPeter Wemm 			}
1262755cc40cSBaptiste Daroussin 		} else if (!db_exist(sp, ecp->addr2.lno)) {
1263b8ba871bSPeter Wemm 			if (FL_ISSET(ecp->iflags, E_C_COUNT)) {
1264b8ba871bSPeter Wemm 				if (db_last(sp, &lno))
1265b8ba871bSPeter Wemm 					goto err;
1266b8ba871bSPeter Wemm 				ecp->addr2.lno = lno;
1267b8ba871bSPeter Wemm 			} else {
1268b8ba871bSPeter Wemm 				ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1269b8ba871bSPeter Wemm 				goto err;
1270b8ba871bSPeter Wemm 			}
1271755cc40cSBaptiste Daroussin 		}
1272b8ba871bSPeter Wemm 		/* FALLTHROUGH */
1273b8ba871bSPeter Wemm 	case 1:
1274b8ba871bSPeter Wemm 		if (ecp->addr1.lno == 0) {
1275b8ba871bSPeter Wemm 			if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1276b8ba871bSPeter Wemm 			    (F_ISSET(sp, SC_EX) ||
1277b8ba871bSPeter Wemm 			    !F_ISSET(ecp, E_USELASTCMD))) {
1278b8ba871bSPeter Wemm 				ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1279b8ba871bSPeter Wemm 				goto err;
1280b8ba871bSPeter Wemm 			}
1281b8ba871bSPeter Wemm 		} else if (!db_exist(sp, ecp->addr1.lno)) {
1282b8ba871bSPeter Wemm 			ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1283b8ba871bSPeter Wemm 			goto err;
1284b8ba871bSPeter Wemm 		}
1285b8ba871bSPeter Wemm 		break;
1286b8ba871bSPeter Wemm 	}
1287b8ba871bSPeter Wemm 
1288b8ba871bSPeter Wemm 	/*
1289b8ba871bSPeter Wemm 	 * If doing a default command and there's nothing left on the line,
1290b8ba871bSPeter Wemm 	 * vi just moves to the line.  For example, ":3" and ":'a,'b" just
1291b8ba871bSPeter Wemm 	 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1292b8ba871bSPeter Wemm 	 *
1293b8ba871bSPeter Wemm 	 * !!!
1294b8ba871bSPeter Wemm 	 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1295b8ba871bSPeter Wemm 	 * the line.
1296b8ba871bSPeter Wemm 	 *
1297b8ba871bSPeter Wemm 	 * !!!
1298b8ba871bSPeter Wemm 	 * This is done before the absolute mark gets set; historically,
1299b8ba871bSPeter Wemm 	 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1300b8ba871bSPeter Wemm 	 */
1301b8ba871bSPeter Wemm 	if ((F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_NOPRDEF)) &&
1302b8ba871bSPeter Wemm 	    F_ISSET(ecp, E_USELASTCMD) && vi_address == 0) {
1303b8ba871bSPeter Wemm 		switch (ecp->addrcnt) {
1304b8ba871bSPeter Wemm 		case 2:
1305b8ba871bSPeter Wemm 			if (sp->lno !=
1306b8ba871bSPeter Wemm 			    (ecp->addr2.lno ? ecp->addr2.lno : 1)) {
1307b8ba871bSPeter Wemm 				sp->lno =
1308b8ba871bSPeter Wemm 				    ecp->addr2.lno ? ecp->addr2.lno : 1;
1309b8ba871bSPeter Wemm 				sp->cno = 0;
1310b8ba871bSPeter Wemm 				(void)nonblank(sp, sp->lno, &sp->cno);
1311b8ba871bSPeter Wemm 			}
1312b8ba871bSPeter Wemm 			break;
1313b8ba871bSPeter Wemm 		case 1:
1314b8ba871bSPeter Wemm 			if (sp->lno !=
1315b8ba871bSPeter Wemm 			    (ecp->addr1.lno ? ecp->addr1.lno : 1)) {
1316b8ba871bSPeter Wemm 				sp->lno =
1317b8ba871bSPeter Wemm 				    ecp->addr1.lno ? ecp->addr1.lno : 1;
1318b8ba871bSPeter Wemm 				sp->cno = 0;
1319b8ba871bSPeter Wemm 				(void)nonblank(sp, sp->lno, &sp->cno);
1320b8ba871bSPeter Wemm 			}
1321b8ba871bSPeter Wemm 			break;
1322b8ba871bSPeter Wemm 		}
1323b8ba871bSPeter Wemm 		ecp->cp = ecp->save_cmd;
1324b8ba871bSPeter Wemm 		ecp->clen = ecp->save_cmdlen;
1325b8ba871bSPeter Wemm 		goto loop;
1326b8ba871bSPeter Wemm 	}
1327b8ba871bSPeter Wemm 
1328b8ba871bSPeter Wemm 	/*
1329b8ba871bSPeter Wemm 	 * Set the absolute mark -- we have to set it for vi here, in case
1330b8ba871bSPeter Wemm 	 * it's a compound command, e.g. ":5p|6" should set the absolute
1331b8ba871bSPeter Wemm 	 * mark for vi.
1332b8ba871bSPeter Wemm 	 */
1333b8ba871bSPeter Wemm 	if (F_ISSET(ecp, E_ABSMARK)) {
1334b8ba871bSPeter Wemm 		cur.lno = sp->lno;
1335b8ba871bSPeter Wemm 		cur.cno = sp->cno;
1336b8ba871bSPeter Wemm 		F_CLR(ecp, E_ABSMARK);
1337b8ba871bSPeter Wemm 		if (mark_set(sp, ABSMARK1, &cur, 1))
1338b8ba871bSPeter Wemm 			goto err;
1339b8ba871bSPeter Wemm 	}
1340b8ba871bSPeter Wemm 
1341b8ba871bSPeter Wemm #if defined(DEBUG) && defined(COMLOG)
1342b8ba871bSPeter Wemm 	ex_comlog(sp, ecp);
1343b8ba871bSPeter Wemm #endif
1344b8ba871bSPeter Wemm 	/* Increment the command count if not called from vi. */
1345b8ba871bSPeter Wemm 	if (F_ISSET(sp, SC_EX))
1346b8ba871bSPeter Wemm 		++sp->ccnt;
1347b8ba871bSPeter Wemm 
1348b8ba871bSPeter Wemm 	/*
1349b8ba871bSPeter Wemm 	 * If file state available, and not doing a global command,
1350b8ba871bSPeter Wemm 	 * log the start of an action.
1351b8ba871bSPeter Wemm 	 */
1352b8ba871bSPeter Wemm 	if (sp->ep != NULL && !F_ISSET(sp, SC_EX_GLOBAL))
1353b8ba871bSPeter Wemm 		(void)log_cursor(sp);
1354b8ba871bSPeter Wemm 
1355b8ba871bSPeter Wemm 	/*
1356b8ba871bSPeter Wemm 	 * !!!
1357b8ba871bSPeter Wemm 	 * There are two special commands for the purposes of this code: the
1358b8ba871bSPeter Wemm 	 * default command (<carriage-return>) or the scrolling commands (^D
1359b8ba871bSPeter Wemm 	 * and <EOF>) as the first non-<blank> characters  in the line.
1360b8ba871bSPeter Wemm 	 *
1361b8ba871bSPeter Wemm 	 * If this is the first command in the command line, we received the
1362b8ba871bSPeter Wemm 	 * command from the ex command loop and we're talking to a tty, and
1363b8ba871bSPeter Wemm 	 * and there's nothing else on the command line, and it's one of the
1364b8ba871bSPeter Wemm 	 * special commands, we move back up to the previous line, and erase
1365b8ba871bSPeter Wemm 	 * the prompt character with the output.  Since ex runs in canonical
1366b8ba871bSPeter Wemm 	 * mode, we don't have to do anything else, a <newline> has already
1367b8ba871bSPeter Wemm 	 * been echoed by the tty driver.  It's OK if vi calls us -- we won't
1368b8ba871bSPeter Wemm 	 * be in ex mode so we'll do nothing.
1369b8ba871bSPeter Wemm 	 */
1370b8ba871bSPeter Wemm 	if (F_ISSET(ecp, E_NRSEP)) {
1371b8ba871bSPeter Wemm 		if (sp->ep != NULL &&
1372b8ba871bSPeter Wemm 		    F_ISSET(sp, SC_EX) && !F_ISSET(gp, G_SCRIPTED) &&
1373b8ba871bSPeter Wemm 		    (F_ISSET(ecp, E_USELASTCMD) || ecp->cmd == &cmds[C_SCROLL]))
1374b8ba871bSPeter Wemm 			gp->scr_ex_adjust(sp, EX_TERM_SCROLL);
1375b8ba871bSPeter Wemm 		F_CLR(ecp, E_NRSEP);
1376b8ba871bSPeter Wemm 	}
1377b8ba871bSPeter Wemm 
1378b8ba871bSPeter Wemm 	/*
1379b8ba871bSPeter Wemm 	 * Call the underlying function for the ex command.
1380b8ba871bSPeter Wemm 	 *
1381b8ba871bSPeter Wemm 	 * XXX
1382b8ba871bSPeter Wemm 	 * Interrupts behave like errors, for now.
1383b8ba871bSPeter Wemm 	 */
1384b8ba871bSPeter Wemm 	if (ecp->cmd->fn(sp, ecp) || INTERRUPTED(sp)) {
1385b8ba871bSPeter Wemm 		if (F_ISSET(gp, G_SCRIPTED))
1386b8ba871bSPeter Wemm 			F_SET(sp, SC_EXIT_FORCE);
1387b8ba871bSPeter Wemm 		goto err;
1388b8ba871bSPeter Wemm 	}
1389b8ba871bSPeter Wemm 
1390b8ba871bSPeter Wemm #ifdef DEBUG
1391b8ba871bSPeter Wemm 	/* Make sure no function left global temporary space locked. */
1392b8ba871bSPeter Wemm 	if (F_ISSET(gp, G_TMP_INUSE)) {
1393b8ba871bSPeter Wemm 		F_CLR(gp, G_TMP_INUSE);
1394f0957ccaSPeter Wemm 		msgq_wstr(sp, M_ERR, ecp->cmd->name,
1395f0957ccaSPeter Wemm 		    "087|%s: temporary buffer not released");
1396b8ba871bSPeter Wemm 	}
1397b8ba871bSPeter Wemm #endif
1398b8ba871bSPeter Wemm 	/*
1399b8ba871bSPeter Wemm 	 * Ex displayed the number of lines modified immediately after each
1400b8ba871bSPeter Wemm 	 * command, so the command "1,10d|1,10d" would display:
1401b8ba871bSPeter Wemm 	 *
1402b8ba871bSPeter Wemm 	 *	10 lines deleted
1403b8ba871bSPeter Wemm 	 *	10 lines deleted
1404b8ba871bSPeter Wemm 	 *	<autoprint line>
1405b8ba871bSPeter Wemm 	 *
1406b8ba871bSPeter Wemm 	 * Executing ex commands from vi only reported the final modified
1407b8ba871bSPeter Wemm 	 * lines message -- that's wrong enough that we don't match it.
1408b8ba871bSPeter Wemm 	 */
1409b8ba871bSPeter Wemm 	if (F_ISSET(sp, SC_EX))
1410b8ba871bSPeter Wemm 		mod_rpt(sp);
1411b8ba871bSPeter Wemm 
1412b8ba871bSPeter Wemm 	/*
1413b8ba871bSPeter Wemm 	 * Integrate any offset parsed by the underlying command, and make
1414b8ba871bSPeter Wemm 	 * sure the referenced line exists.
1415b8ba871bSPeter Wemm 	 *
1416b8ba871bSPeter Wemm 	 * XXX
1417b8ba871bSPeter Wemm 	 * May not match historic practice (which I've never been able to
1418b8ba871bSPeter Wemm 	 * completely figure out.)  For example, the '=' command from vi
1419b8ba871bSPeter Wemm 	 * mode often got the offset wrong, and complained it was too large,
1420b8ba871bSPeter Wemm 	 * but didn't seem to have a problem with the cursor.  If anyone
1421b8ba871bSPeter Wemm 	 * complains, ask them how it's supposed to work, they might know.
1422b8ba871bSPeter Wemm 	 */
1423b8ba871bSPeter Wemm 	if (sp->ep != NULL && ecp->flagoff) {
1424b8ba871bSPeter Wemm 		if (ecp->flagoff < 0) {
1425b8ba871bSPeter Wemm 			if (sp->lno <= -ecp->flagoff) {
1426b8ba871bSPeter Wemm 				msgq(sp, M_ERR,
1427b8ba871bSPeter Wemm 				    "088|Flag offset to before line 1");
1428b8ba871bSPeter Wemm 				goto err;
1429b8ba871bSPeter Wemm 			}
1430b8ba871bSPeter Wemm 		} else {
1431b8ba871bSPeter Wemm 			if (!NPFITS(MAX_REC_NUMBER, sp->lno, ecp->flagoff)) {
1432b8ba871bSPeter Wemm 				ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1433b8ba871bSPeter Wemm 				goto err;
1434b8ba871bSPeter Wemm 			}
1435b8ba871bSPeter Wemm 			if (!db_exist(sp, sp->lno + ecp->flagoff)) {
1436b8ba871bSPeter Wemm 				msgq(sp, M_ERR,
1437b8ba871bSPeter Wemm 				    "089|Flag offset past end-of-file");
1438b8ba871bSPeter Wemm 				goto err;
1439b8ba871bSPeter Wemm 			}
1440b8ba871bSPeter Wemm 		}
1441b8ba871bSPeter Wemm 		sp->lno += ecp->flagoff;
1442b8ba871bSPeter Wemm 	}
1443b8ba871bSPeter Wemm 
1444b8ba871bSPeter Wemm 	/*
1445b8ba871bSPeter Wemm 	 * If the command executed successfully, we may want to display a line
1446b8ba871bSPeter Wemm 	 * based on the autoprint option or an explicit print flag.  (Make sure
1447b8ba871bSPeter Wemm 	 * that there's a line to display.)  Also, the autoprint edit option is
1448b8ba871bSPeter Wemm 	 * turned off for the duration of global commands.
1449b8ba871bSPeter Wemm 	 */
1450b8ba871bSPeter Wemm 	if (F_ISSET(sp, SC_EX) && sp->ep != NULL && sp->lno != 0) {
1451b8ba871bSPeter Wemm 		/*
1452b8ba871bSPeter Wemm 		 * The print commands have already handled the `print' flags.
1453b8ba871bSPeter Wemm 		 * If so, clear them.
1454b8ba871bSPeter Wemm 		 */
1455b8ba871bSPeter Wemm 		if (FL_ISSET(ecp->iflags, E_CLRFLAG))
1456b8ba871bSPeter Wemm 			FL_CLR(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT);
1457b8ba871bSPeter Wemm 
1458b8ba871bSPeter Wemm 		/* If hash set only because of the number option, discard it. */
1459b8ba871bSPeter Wemm 		if (F_ISSET(ecp, E_OPTNUM))
1460b8ba871bSPeter Wemm 			FL_CLR(ecp->iflags, E_C_HASH);
1461b8ba871bSPeter Wemm 
1462b8ba871bSPeter Wemm 		/*
1463b8ba871bSPeter Wemm 		 * If there was an explicit flag to display the new cursor line,
1464b8ba871bSPeter Wemm 		 * or autoprint is set and a change was made, display the line.
1465b8ba871bSPeter Wemm 		 * If any print flags were set use them, else default to print.
1466b8ba871bSPeter Wemm 		 */
1467b8ba871bSPeter Wemm 		LF_INIT(FL_ISSET(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT));
1468b8ba871bSPeter Wemm 		if (!LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT | E_NOAUTO) &&
1469b8ba871bSPeter Wemm 		    !F_ISSET(sp, SC_EX_GLOBAL) &&
1470*0fcececbSBaptiste Daroussin 		    O_ISSET(sp, O_AUTOPRINT) && F_ISSET(ecp, E_AUTOPRINT)) {
1471*0fcececbSBaptiste Daroussin 			/* Honor the number option if autoprint is set. */
1472*0fcececbSBaptiste Daroussin 			if (F_ISSET(ecp, E_OPTNUM))
1473*0fcececbSBaptiste Daroussin 				LF_INIT(E_C_HASH);
1474*0fcececbSBaptiste Daroussin 			else
1475b8ba871bSPeter Wemm 				LF_INIT(E_C_PRINT);
1476*0fcececbSBaptiste Daroussin 		}
1477b8ba871bSPeter Wemm 
1478b8ba871bSPeter Wemm 		if (LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT)) {
1479b8ba871bSPeter Wemm 			cur.lno = sp->lno;
1480b8ba871bSPeter Wemm 			cur.cno = 0;
1481b8ba871bSPeter Wemm 			(void)ex_print(sp, ecp, &cur, &cur, flags);
1482b8ba871bSPeter Wemm 		}
1483b8ba871bSPeter Wemm 	}
1484b8ba871bSPeter Wemm 
1485b8ba871bSPeter Wemm 	/*
1486b8ba871bSPeter Wemm 	 * If the command had an associated "+cmd", it has to be executed
1487b8ba871bSPeter Wemm 	 * before we finish executing any more of this ex command.  For
1488b8ba871bSPeter Wemm 	 * example, consider a .exrc file that contains the following lines:
1489b8ba871bSPeter Wemm 	 *
1490b8ba871bSPeter Wemm 	 *	:set all
1491b8ba871bSPeter Wemm 	 *	:edit +25 file.c|s/abc/ABC/|1
1492b8ba871bSPeter Wemm 	 *	:3,5 print
1493b8ba871bSPeter Wemm 	 *
1494b8ba871bSPeter Wemm 	 * This can happen more than once -- the historic vi simply hung or
1495b8ba871bSPeter Wemm 	 * dropped core, of course.  Prepend the + command back into the
1496b8ba871bSPeter Wemm 	 * current command and continue.  We may have to add an additional
1497b8ba871bSPeter Wemm 	 * <literal next> character.  We know that it will fit because we
1498b8ba871bSPeter Wemm 	 * discarded at least one space and the + character.
1499b8ba871bSPeter Wemm 	 */
1500b8ba871bSPeter Wemm 	if (arg1_len != 0) {
1501b8ba871bSPeter Wemm 		/*
1502b8ba871bSPeter Wemm 		 * If the last character of the + command was a <literal next>
1503b8ba871bSPeter Wemm 		 * character, it would be treated differently because of the
1504b8ba871bSPeter Wemm 		 * append.  Quote it, if necessary.
1505b8ba871bSPeter Wemm 		 */
1506b8ba871bSPeter Wemm 		if (IS_ESCAPE(sp, ecp, arg1[arg1_len - 1])) {
1507b8ba871bSPeter Wemm 			*--ecp->save_cmd = CH_LITERAL;
1508b8ba871bSPeter Wemm 			++ecp->save_cmdlen;
1509b8ba871bSPeter Wemm 		}
1510b8ba871bSPeter Wemm 
1511b8ba871bSPeter Wemm 		ecp->save_cmd -= arg1_len;
1512b8ba871bSPeter Wemm 		ecp->save_cmdlen += arg1_len;
1513110d525eSBaptiste Daroussin 		MEMMOVE(ecp->save_cmd, arg1, arg1_len);
1514b8ba871bSPeter Wemm 
1515b8ba871bSPeter Wemm 		/*
1516b8ba871bSPeter Wemm 		 * Any commands executed from a +cmd are executed starting at
1517b8ba871bSPeter Wemm 		 * the first column of the last line of the file -- NOT the
1518b8ba871bSPeter Wemm 		 * first nonblank.)  The main file startup code doesn't know
1519b8ba871bSPeter Wemm 		 * that a +cmd was set, however, so it may have put us at the
1520b8ba871bSPeter Wemm 		 * top of the file.  (Note, this is safe because we must have
1521b8ba871bSPeter Wemm 		 * switched files to get here.)
1522b8ba871bSPeter Wemm 		 */
1523b8ba871bSPeter Wemm 		F_SET(ecp, E_MOVETOEND);
1524b8ba871bSPeter Wemm 	}
1525b8ba871bSPeter Wemm 
1526b8ba871bSPeter Wemm 	/* Update the current command. */
1527b8ba871bSPeter Wemm 	ecp->cp = ecp->save_cmd;
1528b8ba871bSPeter Wemm 	ecp->clen = ecp->save_cmdlen;
1529b8ba871bSPeter Wemm 
1530b8ba871bSPeter Wemm 	/*
1531b8ba871bSPeter Wemm 	 * !!!
1532b8ba871bSPeter Wemm 	 * If we've changed screens or underlying files, any pending global or
1533b8ba871bSPeter Wemm 	 * v command, or @ buffer that has associated addresses, has to be
1534b8ba871bSPeter Wemm 	 * discarded.  This is historic practice for globals, and necessary for
1535b8ba871bSPeter Wemm 	 * @ buffers that had associated addresses.
1536b8ba871bSPeter Wemm 	 *
1537b8ba871bSPeter Wemm 	 * Otherwise, if we've changed underlying files, it's not a problem,
1538b8ba871bSPeter Wemm 	 * we continue with the rest of the ex command(s), operating on the
1539b8ba871bSPeter Wemm 	 * new file.  However, if we switch screens (either by exiting or by
1540b8ba871bSPeter Wemm 	 * an explicit command), we have no way of knowing where to put output
1541b8ba871bSPeter Wemm 	 * messages, and, since we don't control screens here, we could screw
1542b8ba871bSPeter Wemm 	 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1543b8ba871bSPeter Wemm 	 * times).  So, return and continue after we've got a new screen.
1544b8ba871bSPeter Wemm 	 */
1545b8ba871bSPeter Wemm 	if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH)) {
1546b8ba871bSPeter Wemm 		at_found = gv_found = 0;
1547f0957ccaSPeter Wemm 		SLIST_FOREACH(ecp, sp->gp->ecq, q)
1548b8ba871bSPeter Wemm 			switch (ecp->agv_flags) {
1549b8ba871bSPeter Wemm 			case 0:
1550b8ba871bSPeter Wemm 			case AGV_AT_NORANGE:
1551b8ba871bSPeter Wemm 				break;
1552b8ba871bSPeter Wemm 			case AGV_AT:
1553b8ba871bSPeter Wemm 				if (!at_found) {
1554b8ba871bSPeter Wemm 					at_found = 1;
1555b8ba871bSPeter Wemm 					msgq(sp, M_ERR,
1556b8ba871bSPeter Wemm 		"090|@ with range running when the file/screen changed");
1557b8ba871bSPeter Wemm 				}
1558b8ba871bSPeter Wemm 				break;
1559b8ba871bSPeter Wemm 			case AGV_GLOBAL:
1560b8ba871bSPeter Wemm 			case AGV_V:
1561b8ba871bSPeter Wemm 				if (!gv_found) {
1562b8ba871bSPeter Wemm 					gv_found = 1;
1563b8ba871bSPeter Wemm 					msgq(sp, M_ERR,
1564b8ba871bSPeter Wemm 		"091|Global/v command running when the file/screen changed");
1565b8ba871bSPeter Wemm 				}
1566b8ba871bSPeter Wemm 				break;
1567b8ba871bSPeter Wemm 			default:
1568b8ba871bSPeter Wemm 				abort();
1569b8ba871bSPeter Wemm 			}
1570b8ba871bSPeter Wemm 		if (at_found || gv_found)
1571b8ba871bSPeter Wemm 			goto discard;
1572b8ba871bSPeter Wemm 		if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_SSWITCH))
1573b8ba871bSPeter Wemm 			goto rsuccess;
1574b8ba871bSPeter Wemm 	}
1575b8ba871bSPeter Wemm 
1576b8ba871bSPeter Wemm 	goto loop;
1577b8ba871bSPeter Wemm 	/* NOTREACHED */
1578b8ba871bSPeter Wemm 
1579b8ba871bSPeter Wemm err:	/*
1580b8ba871bSPeter Wemm 	 * On command failure, we discard keys and pending commands remaining,
1581b8ba871bSPeter Wemm 	 * as well as any keys that were mapped and waiting.  The save_cmdlen
1582b8ba871bSPeter Wemm 	 * test is not necessarily correct.  If we fail early enough we don't
1583b8ba871bSPeter Wemm 	 * know if the entire string was a single command or not.  Guess, as
1584b8ba871bSPeter Wemm 	 * it's useful to know if commands other than the current one are being
1585b8ba871bSPeter Wemm 	 * discarded.
1586b8ba871bSPeter Wemm 	 */
1587b8ba871bSPeter Wemm 	if (ecp->save_cmdlen == 0)
1588b8ba871bSPeter Wemm 		for (; ecp->clen; --ecp->clen) {
1589b8ba871bSPeter Wemm 			ch = *ecp->cp++;
1590b8ba871bSPeter Wemm 			if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
1591b8ba871bSPeter Wemm 				--ecp->clen;
1592b8ba871bSPeter Wemm 				++ecp->cp;
1593b8ba871bSPeter Wemm 			} else if (ch == '\n' || ch == '|') {
1594b8ba871bSPeter Wemm 				if (ecp->clen > 1)
1595b8ba871bSPeter Wemm 					ecp->save_cmdlen = 1;
1596b8ba871bSPeter Wemm 				break;
1597b8ba871bSPeter Wemm 			}
1598b8ba871bSPeter Wemm 		}
1599f0957ccaSPeter Wemm 	if (ecp->save_cmdlen != 0 || SLIST_FIRST(gp->ecq) != &gp->excmd) {
1600b8ba871bSPeter Wemm discard:	msgq(sp, M_BERR,
1601b8ba871bSPeter Wemm 		    "092|Ex command failed: pending commands discarded");
1602b8ba871bSPeter Wemm 		ex_discard(sp);
1603b8ba871bSPeter Wemm 	}
1604b8ba871bSPeter Wemm 	if (v_event_flush(sp, CH_MAPPED))
1605b8ba871bSPeter Wemm 		msgq(sp, M_BERR,
1606b8ba871bSPeter Wemm 		    "093|Ex command failed: mapped keys discarded");
1607b8ba871bSPeter Wemm 
1608b8ba871bSPeter Wemm rfail:	tmp = 1;
1609b8ba871bSPeter Wemm 	if (0)
1610b8ba871bSPeter Wemm rsuccess:	tmp = 0;
1611b8ba871bSPeter Wemm 
1612b8ba871bSPeter Wemm 	/* Turn off any file name error information. */
1613b8ba871bSPeter Wemm 	gp->if_name = NULL;
1614b8ba871bSPeter Wemm 
1615b8ba871bSPeter Wemm 	/* Turn off the global bit. */
1616b8ba871bSPeter Wemm 	F_CLR(sp, SC_EX_GLOBAL);
1617b8ba871bSPeter Wemm 
1618b8ba871bSPeter Wemm 	return (tmp);
1619b8ba871bSPeter Wemm }
1620b8ba871bSPeter Wemm 
1621b8ba871bSPeter Wemm /*
1622b8ba871bSPeter Wemm  * ex_range --
1623b8ba871bSPeter Wemm  *	Get a line range for ex commands, or perform a vi ex address search.
1624b8ba871bSPeter Wemm  *
1625c271fa92SBaptiste Daroussin  * PUBLIC: int ex_range(SCR *, EXCMD *, int *);
1626b8ba871bSPeter Wemm  */
1627b8ba871bSPeter Wemm int
ex_range(SCR * sp,EXCMD * ecp,int * errp)1628f0957ccaSPeter Wemm ex_range(SCR *sp, EXCMD *ecp, int *errp)
1629b8ba871bSPeter Wemm {
1630b8ba871bSPeter Wemm 	enum { ADDR_FOUND, ADDR_NEED, ADDR_NONE } addr;
1631b8ba871bSPeter Wemm 	GS *gp;
1632b8ba871bSPeter Wemm 	EX_PRIVATE *exp;
1633b8ba871bSPeter Wemm 	MARK m;
1634b8ba871bSPeter Wemm 	int isaddr;
1635b8ba871bSPeter Wemm 
1636b8ba871bSPeter Wemm 	*errp = 0;
1637b8ba871bSPeter Wemm 
1638b8ba871bSPeter Wemm 	/*
1639b8ba871bSPeter Wemm 	 * Parse comma or semi-colon delimited line specs.
1640b8ba871bSPeter Wemm 	 *
1641b8ba871bSPeter Wemm 	 * Semi-colon delimiters update the current address to be the last
1642b8ba871bSPeter Wemm 	 * address.  For example, the command
1643b8ba871bSPeter Wemm 	 *
1644b8ba871bSPeter Wemm 	 *	:3;/pattern/ecp->cp
1645b8ba871bSPeter Wemm 	 *
1646b8ba871bSPeter Wemm 	 * will search for pattern from line 3.  In addition, if ecp->cp
1647b8ba871bSPeter Wemm 	 * is not a valid command, the current line will be left at 3, not
1648b8ba871bSPeter Wemm 	 * at the original address.
1649b8ba871bSPeter Wemm 	 *
1650b8ba871bSPeter Wemm 	 * Extra addresses are discarded, starting with the first.
1651b8ba871bSPeter Wemm 	 *
1652b8ba871bSPeter Wemm 	 * !!!
1653b8ba871bSPeter Wemm 	 * If any addresses are missing, they default to the current line.
1654b8ba871bSPeter Wemm 	 * This was historically true for both leading and trailing comma
1655b8ba871bSPeter Wemm 	 * delimited addresses as well as for trailing semicolon delimited
1656b8ba871bSPeter Wemm 	 * addresses.  For consistency, we make it true for leading semicolon
1657b8ba871bSPeter Wemm 	 * addresses as well.
1658b8ba871bSPeter Wemm 	 */
1659b8ba871bSPeter Wemm 	gp = sp->gp;
1660b8ba871bSPeter Wemm 	exp = EXP(sp);
1661b8ba871bSPeter Wemm 	for (addr = ADDR_NONE, ecp->addrcnt = 0; ecp->clen > 0;)
1662b8ba871bSPeter Wemm 		switch (*ecp->cp) {
1663b8ba871bSPeter Wemm 		case '%':		/* Entire file. */
1664b8ba871bSPeter Wemm 			/* Vi ex address searches didn't permit % signs. */
1665b8ba871bSPeter Wemm 			if (F_ISSET(ecp, E_VISEARCH))
1666b8ba871bSPeter Wemm 				goto ret;
1667b8ba871bSPeter Wemm 
1668b8ba871bSPeter Wemm 			/* It's an error if the file is empty. */
1669b8ba871bSPeter Wemm 			if (sp->ep == NULL) {
1670b8ba871bSPeter Wemm 				ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1671b8ba871bSPeter Wemm 				*errp = 1;
1672b8ba871bSPeter Wemm 				return (0);
1673b8ba871bSPeter Wemm 			}
1674b8ba871bSPeter Wemm 			/*
1675b8ba871bSPeter Wemm 			 * !!!
1676b8ba871bSPeter Wemm 			 * A percent character addresses all of the lines in
1677b8ba871bSPeter Wemm 			 * the file.  Historically, it couldn't be followed by
1678b8ba871bSPeter Wemm 			 * any other address.  We do it as a text substitution
1679b8ba871bSPeter Wemm 			 * for simplicity.  POSIX 1003.2 is expected to follow
1680b8ba871bSPeter Wemm 			 * this practice.
1681b8ba871bSPeter Wemm 			 *
1682b8ba871bSPeter Wemm 			 * If it's an empty file, the first line is 0, not 1.
1683b8ba871bSPeter Wemm 			 */
1684b8ba871bSPeter Wemm 			if (addr == ADDR_FOUND) {
1685b8ba871bSPeter Wemm 				ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1686b8ba871bSPeter Wemm 				*errp = 1;
1687b8ba871bSPeter Wemm 				return (0);
1688b8ba871bSPeter Wemm 			}
1689b8ba871bSPeter Wemm 			if (db_last(sp, &ecp->addr2.lno))
1690b8ba871bSPeter Wemm 				return (1);
1691b8ba871bSPeter Wemm 			ecp->addr1.lno = ecp->addr2.lno == 0 ? 0 : 1;
1692b8ba871bSPeter Wemm 			ecp->addr1.cno = ecp->addr2.cno = 0;
1693b8ba871bSPeter Wemm 			ecp->addrcnt = 2;
1694b8ba871bSPeter Wemm 			addr = ADDR_FOUND;
1695b8ba871bSPeter Wemm 			++ecp->cp;
1696b8ba871bSPeter Wemm 			--ecp->clen;
1697b8ba871bSPeter Wemm 			break;
1698b8ba871bSPeter Wemm 		case ',':	       /* Comma delimiter. */
1699b8ba871bSPeter Wemm 			/* Vi ex address searches didn't permit commas. */
1700b8ba871bSPeter Wemm 			if (F_ISSET(ecp, E_VISEARCH))
1701b8ba871bSPeter Wemm 				goto ret;
1702b8ba871bSPeter Wemm 			/* FALLTHROUGH */
1703b8ba871bSPeter Wemm 		case ';':	       /* Semi-colon delimiter. */
1704b8ba871bSPeter Wemm 			if (sp->ep == NULL) {
1705b8ba871bSPeter Wemm 				ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1706b8ba871bSPeter Wemm 				*errp = 1;
1707b8ba871bSPeter Wemm 				return (0);
1708b8ba871bSPeter Wemm 			}
1709b8ba871bSPeter Wemm 			if (addr != ADDR_FOUND)
1710b8ba871bSPeter Wemm 				switch (ecp->addrcnt) {
1711b8ba871bSPeter Wemm 				case 0:
1712b8ba871bSPeter Wemm 					ecp->addr1.lno = sp->lno;
1713b8ba871bSPeter Wemm 					ecp->addr1.cno = sp->cno;
1714b8ba871bSPeter Wemm 					ecp->addrcnt = 1;
1715b8ba871bSPeter Wemm 					break;
1716b8ba871bSPeter Wemm 				case 2:
1717b8ba871bSPeter Wemm 					ecp->addr1 = ecp->addr2;
1718b8ba871bSPeter Wemm 					/* FALLTHROUGH */
1719b8ba871bSPeter Wemm 				case 1:
1720b8ba871bSPeter Wemm 					ecp->addr2.lno = sp->lno;
1721b8ba871bSPeter Wemm 					ecp->addr2.cno = sp->cno;
1722b8ba871bSPeter Wemm 					ecp->addrcnt = 2;
1723b8ba871bSPeter Wemm 					break;
1724b8ba871bSPeter Wemm 				}
1725b8ba871bSPeter Wemm 			if (*ecp->cp == ';')
1726b8ba871bSPeter Wemm 				switch (ecp->addrcnt) {
1727b8ba871bSPeter Wemm 				case 0:
1728b8ba871bSPeter Wemm 					abort();
1729b8ba871bSPeter Wemm 					/* NOTREACHED */
1730b8ba871bSPeter Wemm 				case 1:
1731b8ba871bSPeter Wemm 					sp->lno = ecp->addr1.lno;
1732b8ba871bSPeter Wemm 					sp->cno = ecp->addr1.cno;
1733b8ba871bSPeter Wemm 					break;
1734b8ba871bSPeter Wemm 				case 2:
1735b8ba871bSPeter Wemm 					sp->lno = ecp->addr2.lno;
1736b8ba871bSPeter Wemm 					sp->cno = ecp->addr2.cno;
1737b8ba871bSPeter Wemm 					break;
1738b8ba871bSPeter Wemm 				}
1739b8ba871bSPeter Wemm 			addr = ADDR_NEED;
1740b8ba871bSPeter Wemm 			/* FALLTHROUGH */
1741b8ba871bSPeter Wemm 		case ' ':		/* Whitespace. */
1742b8ba871bSPeter Wemm 		case '\t':		/* Whitespace. */
1743b8ba871bSPeter Wemm 			++ecp->cp;
1744b8ba871bSPeter Wemm 			--ecp->clen;
1745b8ba871bSPeter Wemm 			break;
1746b8ba871bSPeter Wemm 		default:
1747b8ba871bSPeter Wemm 			/* Get a line specification. */
1748b8ba871bSPeter Wemm 			if (ex_line(sp, ecp, &m, &isaddr, errp))
1749b8ba871bSPeter Wemm 				return (1);
1750b8ba871bSPeter Wemm 			if (*errp)
1751b8ba871bSPeter Wemm 				return (0);
1752b8ba871bSPeter Wemm 			if (!isaddr)
1753b8ba871bSPeter Wemm 				goto ret;
1754b8ba871bSPeter Wemm 			if (addr == ADDR_FOUND) {
1755b8ba871bSPeter Wemm 				ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1756b8ba871bSPeter Wemm 				*errp = 1;
1757b8ba871bSPeter Wemm 				return (0);
1758b8ba871bSPeter Wemm 			}
1759b8ba871bSPeter Wemm 			switch (ecp->addrcnt) {
1760b8ba871bSPeter Wemm 			case 0:
1761b8ba871bSPeter Wemm 				ecp->addr1 = m;
1762b8ba871bSPeter Wemm 				ecp->addrcnt = 1;
1763b8ba871bSPeter Wemm 				break;
1764b8ba871bSPeter Wemm 			case 1:
1765b8ba871bSPeter Wemm 				ecp->addr2 = m;
1766b8ba871bSPeter Wemm 				ecp->addrcnt = 2;
1767b8ba871bSPeter Wemm 				break;
1768b8ba871bSPeter Wemm 			case 2:
1769b8ba871bSPeter Wemm 				ecp->addr1 = ecp->addr2;
1770b8ba871bSPeter Wemm 				ecp->addr2 = m;
1771b8ba871bSPeter Wemm 				break;
1772b8ba871bSPeter Wemm 			}
1773b8ba871bSPeter Wemm 			addr = ADDR_FOUND;
1774b8ba871bSPeter Wemm 			break;
1775b8ba871bSPeter Wemm 		}
1776b8ba871bSPeter Wemm 
1777b8ba871bSPeter Wemm 	/*
1778b8ba871bSPeter Wemm 	 * !!!
1779b8ba871bSPeter Wemm 	 * Vi ex address searches are indifferent to order or trailing
1780b8ba871bSPeter Wemm 	 * semi-colons.
1781b8ba871bSPeter Wemm 	 */
1782b8ba871bSPeter Wemm ret:	if (F_ISSET(ecp, E_VISEARCH))
1783b8ba871bSPeter Wemm 		return (0);
1784b8ba871bSPeter Wemm 
1785b8ba871bSPeter Wemm 	if (addr == ADDR_NEED)
1786b8ba871bSPeter Wemm 		switch (ecp->addrcnt) {
1787b8ba871bSPeter Wemm 		case 0:
1788b8ba871bSPeter Wemm 			ecp->addr1.lno = sp->lno;
1789b8ba871bSPeter Wemm 			ecp->addr1.cno = sp->cno;
1790b8ba871bSPeter Wemm 			ecp->addrcnt = 1;
1791b8ba871bSPeter Wemm 			break;
1792b8ba871bSPeter Wemm 		case 2:
1793b8ba871bSPeter Wemm 			ecp->addr1 = ecp->addr2;
1794b8ba871bSPeter Wemm 			/* FALLTHROUGH */
1795b8ba871bSPeter Wemm 		case 1:
1796b8ba871bSPeter Wemm 			ecp->addr2.lno = sp->lno;
1797b8ba871bSPeter Wemm 			ecp->addr2.cno = sp->cno;
1798b8ba871bSPeter Wemm 			ecp->addrcnt = 2;
1799b8ba871bSPeter Wemm 			break;
1800b8ba871bSPeter Wemm 		}
1801b8ba871bSPeter Wemm 
1802b8ba871bSPeter Wemm 	if (ecp->addrcnt == 2 && ecp->addr2.lno < ecp->addr1.lno) {
1803b8ba871bSPeter Wemm 		msgq(sp, M_ERR,
1804b8ba871bSPeter Wemm 		    "094|The second address is smaller than the first");
1805b8ba871bSPeter Wemm 		*errp = 1;
1806b8ba871bSPeter Wemm 	}
1807b8ba871bSPeter Wemm 	return (0);
1808b8ba871bSPeter Wemm }
1809b8ba871bSPeter Wemm 
1810b8ba871bSPeter Wemm /*
1811b8ba871bSPeter Wemm  * ex_line --
1812b8ba871bSPeter Wemm  *	Get a single line address specifier.
1813b8ba871bSPeter Wemm  *
1814b8ba871bSPeter Wemm  * The way the "previous context" mark worked was that any "non-relative"
1815b8ba871bSPeter Wemm  * motion set it.  While ex/vi wasn't totally consistent about this, ANY
1816b8ba871bSPeter Wemm  * numeric address, search pattern, '$', or mark reference in an address
1817b8ba871bSPeter Wemm  * was considered non-relative, and set the value.  Which should explain
1818b8ba871bSPeter Wemm  * why we're hacking marks down here.  The problem was that the mark was
1819b8ba871bSPeter Wemm  * only set if the command was called, i.e. we have to set a flag and test
1820b8ba871bSPeter Wemm  * it later.
1821b8ba871bSPeter Wemm  *
1822b8ba871bSPeter Wemm  * XXX
1823b8ba871bSPeter Wemm  * This is probably still not exactly historic practice, although I think
1824b8ba871bSPeter Wemm  * it's fairly close.
1825b8ba871bSPeter Wemm  */
1826b8ba871bSPeter Wemm static int
ex_line(SCR * sp,EXCMD * ecp,MARK * mp,int * isaddrp,int * errp)1827f0957ccaSPeter Wemm ex_line(SCR *sp, EXCMD *ecp, MARK *mp, int *isaddrp, int *errp)
1828b8ba871bSPeter Wemm {
1829b8ba871bSPeter Wemm 	enum nresult nret;
1830b8ba871bSPeter Wemm 	EX_PRIVATE *exp;
1831b8ba871bSPeter Wemm 	GS *gp;
1832b8ba871bSPeter Wemm 	long total, val;
1833b8ba871bSPeter Wemm 	int isneg;
1834c271fa92SBaptiste Daroussin 	int (*sf)(SCR *, MARK *, MARK *, CHAR_T *, size_t, CHAR_T **, u_int);
1835f0957ccaSPeter Wemm 	CHAR_T *endp;
1836b8ba871bSPeter Wemm 
1837b8ba871bSPeter Wemm 	gp = sp->gp;
1838b8ba871bSPeter Wemm 	exp = EXP(sp);
1839b8ba871bSPeter Wemm 
1840b8ba871bSPeter Wemm 	*isaddrp = *errp = 0;
1841b8ba871bSPeter Wemm 	F_CLR(ecp, E_DELTA);
1842b8ba871bSPeter Wemm 
1843b8ba871bSPeter Wemm 	/* No addresses permitted until a file has been read in. */
1844f0957ccaSPeter Wemm 	if (sp->ep == NULL && STRCHR(L("$0123456789'\\/?.+-^"), *ecp->cp)) {
1845b8ba871bSPeter Wemm 		ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1846b8ba871bSPeter Wemm 		*errp = 1;
1847b8ba871bSPeter Wemm 		return (0);
1848b8ba871bSPeter Wemm 	}
1849b8ba871bSPeter Wemm 
1850b8ba871bSPeter Wemm 	switch (*ecp->cp) {
1851b8ba871bSPeter Wemm 	case '$':				/* Last line in the file. */
1852b8ba871bSPeter Wemm 		*isaddrp = 1;
1853b8ba871bSPeter Wemm 		F_SET(ecp, E_ABSMARK);
1854b8ba871bSPeter Wemm 
1855b8ba871bSPeter Wemm 		mp->cno = 0;
1856b8ba871bSPeter Wemm 		if (db_last(sp, &mp->lno))
1857b8ba871bSPeter Wemm 			return (1);
1858b8ba871bSPeter Wemm 		++ecp->cp;
1859b8ba871bSPeter Wemm 		--ecp->clen;
1860b8ba871bSPeter Wemm 		break;				/* Absolute line number. */
1861b8ba871bSPeter Wemm 	case '0': case '1': case '2': case '3': case '4':
1862b8ba871bSPeter Wemm 	case '5': case '6': case '7': case '8': case '9':
1863b8ba871bSPeter Wemm 		*isaddrp = 1;
1864b8ba871bSPeter Wemm 		F_SET(ecp, E_ABSMARK);
1865b8ba871bSPeter Wemm 
1866b8ba871bSPeter Wemm 		if ((nret = nget_slong(&val, ecp->cp, &endp, 10)) != NUM_OK) {
1867b8ba871bSPeter Wemm 			ex_badaddr(sp, NULL, A_NOTSET, nret);
1868b8ba871bSPeter Wemm 			*errp = 1;
1869b8ba871bSPeter Wemm 			return (0);
1870b8ba871bSPeter Wemm 		}
1871b8ba871bSPeter Wemm 		if (!NPFITS(MAX_REC_NUMBER, 0, val)) {
1872b8ba871bSPeter Wemm 			ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1873b8ba871bSPeter Wemm 			*errp = 1;
1874b8ba871bSPeter Wemm 			return (0);
1875b8ba871bSPeter Wemm 		}
1876b8ba871bSPeter Wemm 		mp->lno = val;
1877b8ba871bSPeter Wemm 		mp->cno = 0;
1878b8ba871bSPeter Wemm 		ecp->clen -= (endp - ecp->cp);
1879b8ba871bSPeter Wemm 		ecp->cp = endp;
1880b8ba871bSPeter Wemm 		break;
1881b8ba871bSPeter Wemm 	case '\'':				/* Use a mark. */
1882b8ba871bSPeter Wemm 		*isaddrp = 1;
1883b8ba871bSPeter Wemm 		F_SET(ecp, E_ABSMARK);
1884b8ba871bSPeter Wemm 
1885b8ba871bSPeter Wemm 		if (ecp->clen == 1) {
1886b8ba871bSPeter Wemm 			msgq(sp, M_ERR, "095|No mark name supplied");
1887b8ba871bSPeter Wemm 			*errp = 1;
1888b8ba871bSPeter Wemm 			return (0);
1889b8ba871bSPeter Wemm 		}
1890b8ba871bSPeter Wemm 		if (mark_get(sp, ecp->cp[1], mp, M_ERR)) {
1891b8ba871bSPeter Wemm 			*errp = 1;
1892b8ba871bSPeter Wemm 			return (0);
1893b8ba871bSPeter Wemm 		}
1894b8ba871bSPeter Wemm 		ecp->cp += 2;
1895b8ba871bSPeter Wemm 		ecp->clen -= 2;
1896b8ba871bSPeter Wemm 		break;
1897b8ba871bSPeter Wemm 	case '\\':				/* Search: forward/backward. */
1898b8ba871bSPeter Wemm 		/*
1899b8ba871bSPeter Wemm 		 * !!!
1900b8ba871bSPeter Wemm 		 * I can't find any difference between // and \/ or between
1901b8ba871bSPeter Wemm 		 * ?? and \?.  Mark Horton doesn't remember there being any
1902b8ba871bSPeter Wemm 		 * difference.  C'est la vie.
1903b8ba871bSPeter Wemm 		 */
1904b8ba871bSPeter Wemm 		if (ecp->clen < 2 ||
1905f0957ccaSPeter Wemm 		    (ecp->cp[1] != '/' && ecp->cp[1] != '?')) {
1906b8ba871bSPeter Wemm 			msgq(sp, M_ERR, "096|\\ not followed by / or ?");
1907b8ba871bSPeter Wemm 			*errp = 1;
1908b8ba871bSPeter Wemm 			return (0);
1909b8ba871bSPeter Wemm 		}
1910b8ba871bSPeter Wemm 		++ecp->cp;
1911b8ba871bSPeter Wemm 		--ecp->clen;
1912b8ba871bSPeter Wemm 		sf = ecp->cp[0] == '/' ? f_search : b_search;
1913b8ba871bSPeter Wemm 		goto search;
1914b8ba871bSPeter Wemm 	case '/':				/* Search forward. */
1915b8ba871bSPeter Wemm 		sf = f_search;
1916b8ba871bSPeter Wemm 		goto search;
1917b8ba871bSPeter Wemm 	case '?':				/* Search backward. */
1918b8ba871bSPeter Wemm 		sf = b_search;
1919b8ba871bSPeter Wemm 
1920b8ba871bSPeter Wemm search:		mp->lno = sp->lno;
1921b8ba871bSPeter Wemm 		mp->cno = sp->cno;
1922b8ba871bSPeter Wemm 		if (sf(sp, mp, mp, ecp->cp, ecp->clen, &endp,
1923b8ba871bSPeter Wemm 		    SEARCH_MSG | SEARCH_PARSE | SEARCH_SET |
1924b8ba871bSPeter Wemm 		    (F_ISSET(ecp, E_SEARCH_WMSG) ? SEARCH_WMSG : 0))) {
1925b8ba871bSPeter Wemm 			*errp = 1;
1926b8ba871bSPeter Wemm 			return (0);
1927b8ba871bSPeter Wemm 		}
1928b8ba871bSPeter Wemm 
1929b8ba871bSPeter Wemm 		/* Fix up the command pointers. */
1930b8ba871bSPeter Wemm 		ecp->clen -= (endp - ecp->cp);
1931b8ba871bSPeter Wemm 		ecp->cp = endp;
1932b8ba871bSPeter Wemm 
1933b8ba871bSPeter Wemm 		*isaddrp = 1;
1934b8ba871bSPeter Wemm 		F_SET(ecp, E_ABSMARK);
1935b8ba871bSPeter Wemm 		break;
1936b8ba871bSPeter Wemm 	case '.':				/* Current position. */
1937b8ba871bSPeter Wemm 		*isaddrp = 1;
1938b8ba871bSPeter Wemm 		mp->cno = sp->cno;
1939b8ba871bSPeter Wemm 
1940b8ba871bSPeter Wemm 		/* If an empty file, then '.' is 0, not 1. */
1941b8ba871bSPeter Wemm 		if (sp->lno == 1) {
1942b8ba871bSPeter Wemm 			if (db_last(sp, &mp->lno))
1943b8ba871bSPeter Wemm 				return (1);
1944b8ba871bSPeter Wemm 			if (mp->lno != 0)
1945b8ba871bSPeter Wemm 				mp->lno = 1;
1946b8ba871bSPeter Wemm 		} else
1947b8ba871bSPeter Wemm 			mp->lno = sp->lno;
1948b8ba871bSPeter Wemm 
1949b8ba871bSPeter Wemm 		/*
1950b8ba871bSPeter Wemm 		 * !!!
1951b8ba871bSPeter Wemm 		 * Historically, .<number> was the same as .+<number>, i.e.
1952b8ba871bSPeter Wemm 		 * the '+' could be omitted.  (This feature is found in ed
1953b8ba871bSPeter Wemm 		 * as well.)
1954b8ba871bSPeter Wemm 		 */
1955f0957ccaSPeter Wemm 		if (ecp->clen > 1 && ISDIGIT(ecp->cp[1]))
1956b8ba871bSPeter Wemm 			*ecp->cp = '+';
1957b8ba871bSPeter Wemm 		else {
1958b8ba871bSPeter Wemm 			++ecp->cp;
1959b8ba871bSPeter Wemm 			--ecp->clen;
1960b8ba871bSPeter Wemm 		}
1961b8ba871bSPeter Wemm 		break;
1962b8ba871bSPeter Wemm 	}
1963b8ba871bSPeter Wemm 
1964b8ba871bSPeter Wemm 	/* Skip trailing <blank>s. */
1965b8ba871bSPeter Wemm 	for (; ecp->clen > 0 &&
1966f0957ccaSPeter Wemm 	    cmdskip(ecp->cp[0]); ++ecp->cp, --ecp->clen);
1967b8ba871bSPeter Wemm 
1968b8ba871bSPeter Wemm 	/*
1969b8ba871bSPeter Wemm 	 * Evaluate any offset.  If no address yet found, the offset
1970b8ba871bSPeter Wemm 	 * is relative to ".".
1971b8ba871bSPeter Wemm 	 */
1972b8ba871bSPeter Wemm 	total = 0;
1973f0957ccaSPeter Wemm 	if (ecp->clen != 0 && (ISDIGIT(ecp->cp[0]) ||
1974b8ba871bSPeter Wemm 	    ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1975b8ba871bSPeter Wemm 	    ecp->cp[0] == '^')) {
1976b8ba871bSPeter Wemm 		if (!*isaddrp) {
1977b8ba871bSPeter Wemm 			*isaddrp = 1;
1978b8ba871bSPeter Wemm 			mp->lno = sp->lno;
1979b8ba871bSPeter Wemm 			mp->cno = sp->cno;
1980b8ba871bSPeter Wemm 		}
1981b8ba871bSPeter Wemm 		/*
1982b8ba871bSPeter Wemm 		 * Evaluate an offset, defined as:
1983b8ba871bSPeter Wemm 		 *
1984b8ba871bSPeter Wemm 		 *		[+-^<blank>]*[<blank>]*[0-9]*
1985b8ba871bSPeter Wemm 		 *
1986b8ba871bSPeter Wemm 		 * The rough translation is any number of signs, optionally
1987b8ba871bSPeter Wemm 		 * followed by numbers, or a number by itself, all <blank>
1988b8ba871bSPeter Wemm 		 * separated.
1989b8ba871bSPeter Wemm 		 *
1990b8ba871bSPeter Wemm 		 * !!!
1991b8ba871bSPeter Wemm 		 * All address offsets were additive, e.g. "2 2 3p" was the
1992b8ba871bSPeter Wemm 		 * same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2".
1993b8ba871bSPeter Wemm 		 * Note, however, "2 /ZZZ/" was an error.  It was also legal
1994b8ba871bSPeter Wemm 		 * to insert signs without numbers, so "3 - 2" was legal, and
1995b8ba871bSPeter Wemm 		 * equal to 4.
1996b8ba871bSPeter Wemm 		 *
1997b8ba871bSPeter Wemm 		 * !!!
1998b8ba871bSPeter Wemm 		 * Offsets were historically permitted for any line address,
1999b8ba871bSPeter Wemm 		 * e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after
2000b8ba871bSPeter Wemm 		 * line 8.
2001b8ba871bSPeter Wemm 		 *
2002b8ba871bSPeter Wemm 		 * !!!
2003b8ba871bSPeter Wemm 		 * Offsets were historically permitted for search commands,
2004b8ba871bSPeter Wemm 		 * and handled as addresses: "/pattern/2 2 2" was legal, and
2005b8ba871bSPeter Wemm 		 * referenced the 6th line after pattern.
2006b8ba871bSPeter Wemm 		 */
2007b8ba871bSPeter Wemm 		F_SET(ecp, E_DELTA);
2008b8ba871bSPeter Wemm 		for (;;) {
2009f0957ccaSPeter Wemm 			for (; ecp->clen > 0 && cmdskip(ecp->cp[0]);
2010b8ba871bSPeter Wemm 			    ++ecp->cp, --ecp->clen);
2011f0957ccaSPeter Wemm 			if (ecp->clen == 0 || (!ISDIGIT(ecp->cp[0]) &&
2012b8ba871bSPeter Wemm 			    ecp->cp[0] != '+' && ecp->cp[0] != '-' &&
2013f0957ccaSPeter Wemm 			    ecp->cp[0] != '^'))
2014b8ba871bSPeter Wemm 				break;
2015f0957ccaSPeter Wemm 			if (!ISDIGIT(ecp->cp[0]) &&
2016f0957ccaSPeter Wemm 			    !ISDIGIT(ecp->cp[1])) {
2017b8ba871bSPeter Wemm 				total += ecp->cp[0] == '+' ? 1 : -1;
2018b8ba871bSPeter Wemm 				--ecp->clen;
2019b8ba871bSPeter Wemm 				++ecp->cp;
2020b8ba871bSPeter Wemm 			} else {
2021b8ba871bSPeter Wemm 				if (ecp->cp[0] == '-' ||
2022b8ba871bSPeter Wemm 				    ecp->cp[0] == '^') {
2023b8ba871bSPeter Wemm 					++ecp->cp;
2024b8ba871bSPeter Wemm 					--ecp->clen;
2025b8ba871bSPeter Wemm 					isneg = 1;
2026b8ba871bSPeter Wemm 				} else
2027b8ba871bSPeter Wemm 					isneg = 0;
2028b8ba871bSPeter Wemm 
2029b8ba871bSPeter Wemm 				/* Get a signed long, add it to the total. */
2030b8ba871bSPeter Wemm 				if ((nret = nget_slong(&val,
2031b8ba871bSPeter Wemm 				    ecp->cp, &endp, 10)) != NUM_OK ||
2032b8ba871bSPeter Wemm 				    (nret = NADD_SLONG(sp,
2033b8ba871bSPeter Wemm 				    total, val)) != NUM_OK) {
2034b8ba871bSPeter Wemm 					ex_badaddr(sp, NULL, A_NOTSET, nret);
2035b8ba871bSPeter Wemm 					*errp = 1;
2036b8ba871bSPeter Wemm 					return (0);
2037b8ba871bSPeter Wemm 				}
2038b8ba871bSPeter Wemm 				total += isneg ? -val : val;
2039b8ba871bSPeter Wemm 				ecp->clen -= (endp - ecp->cp);
2040b8ba871bSPeter Wemm 				ecp->cp = endp;
2041b8ba871bSPeter Wemm 			}
2042b8ba871bSPeter Wemm 		}
2043b8ba871bSPeter Wemm 	}
2044b8ba871bSPeter Wemm 
2045b8ba871bSPeter Wemm 	/*
2046b8ba871bSPeter Wemm 	 * Any value less than 0 is an error.  Make sure that the new value
2047b8ba871bSPeter Wemm 	 * will fit into a recno_t.
2048b8ba871bSPeter Wemm 	 */
2049b8ba871bSPeter Wemm 	if (*isaddrp && total != 0) {
2050b8ba871bSPeter Wemm 		if (total < 0) {
2051b8ba871bSPeter Wemm 			if (-total > mp->lno) {
2052b8ba871bSPeter Wemm 				msgq(sp, M_ERR,
2053b8ba871bSPeter Wemm 			    "097|Reference to a line number less than 0");
2054b8ba871bSPeter Wemm 				*errp = 1;
2055b8ba871bSPeter Wemm 				return (0);
2056b8ba871bSPeter Wemm 			}
2057b8ba871bSPeter Wemm 		} else
2058b8ba871bSPeter Wemm 			if (!NPFITS(MAX_REC_NUMBER, mp->lno, total)) {
2059b8ba871bSPeter Wemm 				ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
2060b8ba871bSPeter Wemm 				*errp = 1;
2061b8ba871bSPeter Wemm 				return (0);
2062b8ba871bSPeter Wemm 			}
2063b8ba871bSPeter Wemm 		mp->lno += total;
2064b8ba871bSPeter Wemm 	}
2065b8ba871bSPeter Wemm 	return (0);
2066b8ba871bSPeter Wemm }
2067b8ba871bSPeter Wemm 
2068b8ba871bSPeter Wemm 
2069b8ba871bSPeter Wemm /*
2070b8ba871bSPeter Wemm  * ex_load --
2071b8ba871bSPeter Wemm  *	Load up the next command, which may be an @ buffer or global command.
2072b8ba871bSPeter Wemm  */
2073b8ba871bSPeter Wemm static int
ex_load(SCR * sp)2074f0957ccaSPeter Wemm ex_load(SCR *sp)
2075b8ba871bSPeter Wemm {
2076b8ba871bSPeter Wemm 	GS *gp;
2077b8ba871bSPeter Wemm 	EXCMD *ecp;
2078b8ba871bSPeter Wemm 	RANGE *rp;
2079b8ba871bSPeter Wemm 
2080b8ba871bSPeter Wemm 	F_CLR(sp, SC_EX_GLOBAL);
2081b8ba871bSPeter Wemm 
2082b8ba871bSPeter Wemm 	/*
2083b8ba871bSPeter Wemm 	 * Lose any exhausted commands.  We know that the first command
2084b8ba871bSPeter Wemm 	 * can't be an AGV command, which makes things a bit easier.
2085b8ba871bSPeter Wemm 	 */
2086b8ba871bSPeter Wemm 	for (gp = sp->gp;;) {
2087f0957ccaSPeter Wemm 		ecp = SLIST_FIRST(gp->ecq);
2088f0957ccaSPeter Wemm 
2089f0957ccaSPeter Wemm 		/* Discard the allocated source name as requested. */
2090f0957ccaSPeter Wemm 		if (F_ISSET(ecp, E_NAMEDISCARD))
2091f0957ccaSPeter Wemm 			free(ecp->if_name);
2092f0957ccaSPeter Wemm 
2093b8ba871bSPeter Wemm 		/*
2094b8ba871bSPeter Wemm 		 * If we're back to the original structure, leave it around,
2095f0957ccaSPeter Wemm 		 * since we've returned to the beginning of the command stack.
2096b8ba871bSPeter Wemm 		 */
2097f0957ccaSPeter Wemm 		if (ecp == &gp->excmd) {
2098b8ba871bSPeter Wemm 			ecp->if_name = NULL;
2099b8ba871bSPeter Wemm 			return (0);
2100b8ba871bSPeter Wemm 		}
2101b8ba871bSPeter Wemm 
2102b8ba871bSPeter Wemm 		/*
2103b8ba871bSPeter Wemm 		 * ecp->clen will be 0 for the first discarded command, but
2104b8ba871bSPeter Wemm 		 * may not be 0 for subsequent ones, e.g. if the original
2105b8ba871bSPeter Wemm 		 * command was ":g/xx/@a|s/b/c/", then when we discard the
2106b8ba871bSPeter Wemm 		 * command pushed on the stack by the @a, we have to resume
2107b8ba871bSPeter Wemm 		 * the global command which included the substitute command.
2108b8ba871bSPeter Wemm 		 */
2109b8ba871bSPeter Wemm 		if (ecp->clen != 0)
2110b8ba871bSPeter Wemm 			return (0);
2111b8ba871bSPeter Wemm 
2112b8ba871bSPeter Wemm 		/*
2113b8ba871bSPeter Wemm 		 * If it's an @, global or v command, we may need to continue
2114b8ba871bSPeter Wemm 		 * the command on a different line.
2115b8ba871bSPeter Wemm 		 */
2116b8ba871bSPeter Wemm 		if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2117b8ba871bSPeter Wemm 			/* Discard any exhausted ranges. */
2118f0957ccaSPeter Wemm 			while ((rp = TAILQ_FIRST(ecp->rq)) != NULL)
2119b8ba871bSPeter Wemm 				if (rp->start > rp->stop) {
2120f0957ccaSPeter Wemm 					TAILQ_REMOVE(ecp->rq, rp, q);
2121b8ba871bSPeter Wemm 					free(rp);
2122b8ba871bSPeter Wemm 				} else
2123b8ba871bSPeter Wemm 					break;
2124b8ba871bSPeter Wemm 
2125b8ba871bSPeter Wemm 			/* If there's another range, continue with it. */
2126f0957ccaSPeter Wemm 			if (rp != NULL)
2127b8ba871bSPeter Wemm 				break;
2128b8ba871bSPeter Wemm 
2129b8ba871bSPeter Wemm 			/* If it's a global/v command, fix up the last line. */
2130b8ba871bSPeter Wemm 			if (FL_ISSET(ecp->agv_flags,
2131755cc40cSBaptiste Daroussin 			    AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO) {
2132b8ba871bSPeter Wemm 				if (db_exist(sp, ecp->range_lno))
2133b8ba871bSPeter Wemm 					sp->lno = ecp->range_lno;
2134b8ba871bSPeter Wemm 				else {
2135b8ba871bSPeter Wemm 					if (db_last(sp, &sp->lno))
2136b8ba871bSPeter Wemm 						return (1);
2137b8ba871bSPeter Wemm 					if (sp->lno == 0)
2138b8ba871bSPeter Wemm 						sp->lno = 1;
2139b8ba871bSPeter Wemm 				}
2140755cc40cSBaptiste Daroussin 			}
2141b8ba871bSPeter Wemm 			free(ecp->o_cp);
2142b8ba871bSPeter Wemm 		}
2143b8ba871bSPeter Wemm 
2144b8ba871bSPeter Wemm 		/* Discard the EXCMD. */
2145f0957ccaSPeter Wemm 		SLIST_REMOVE_HEAD(gp->ecq, q);
2146b8ba871bSPeter Wemm 		free(ecp);
2147b8ba871bSPeter Wemm 	}
2148b8ba871bSPeter Wemm 
2149b8ba871bSPeter Wemm 	/*
2150b8ba871bSPeter Wemm 	 * We only get here if it's an active @, global or v command.  Set
2151b8ba871bSPeter Wemm 	 * the current line number, and get a new copy of the command for
2152b8ba871bSPeter Wemm 	 * the parser.  Note, the original pointer almost certainly moved,
2153b8ba871bSPeter Wemm 	 * so we have play games.
2154b8ba871bSPeter Wemm 	 */
2155b8ba871bSPeter Wemm 	ecp->cp = ecp->o_cp;
2156f0957ccaSPeter Wemm 	MEMCPY(ecp->cp, ecp->cp + ecp->o_clen, ecp->o_clen);
2157b8ba871bSPeter Wemm 	ecp->clen = ecp->o_clen;
2158b8ba871bSPeter Wemm 	ecp->range_lno = sp->lno = rp->start++;
2159b8ba871bSPeter Wemm 
2160b8ba871bSPeter Wemm 	if (FL_ISSET(ecp->agv_flags, AGV_GLOBAL | AGV_V))
2161b8ba871bSPeter Wemm 		F_SET(sp, SC_EX_GLOBAL);
2162b8ba871bSPeter Wemm 	return (0);
2163b8ba871bSPeter Wemm }
2164b8ba871bSPeter Wemm 
2165b8ba871bSPeter Wemm /*
2166b8ba871bSPeter Wemm  * ex_discard --
2167b8ba871bSPeter Wemm  *	Discard any pending ex commands.
2168b8ba871bSPeter Wemm  */
2169b8ba871bSPeter Wemm static int
ex_discard(SCR * sp)2170f0957ccaSPeter Wemm ex_discard(SCR *sp)
2171b8ba871bSPeter Wemm {
2172b8ba871bSPeter Wemm 	GS *gp;
2173b8ba871bSPeter Wemm 	EXCMD *ecp;
2174b8ba871bSPeter Wemm 	RANGE *rp;
2175b8ba871bSPeter Wemm 
2176b8ba871bSPeter Wemm 	/*
2177b8ba871bSPeter Wemm 	 * We know the first command can't be an AGV command, so we don't
2178b8ba871bSPeter Wemm 	 * process it specially.  We do, however, nail the command itself.
2179b8ba871bSPeter Wemm 	 */
2180f0957ccaSPeter Wemm 	for (gp = sp->gp;;) {
2181f0957ccaSPeter Wemm 		ecp = SLIST_FIRST(gp->ecq);
2182f0957ccaSPeter Wemm 		if (F_ISSET(ecp, E_NAMEDISCARD))
2183f0957ccaSPeter Wemm 			free(ecp->if_name);
2184f0957ccaSPeter Wemm 		/* Reset the last command without dropping it. */
2185f0957ccaSPeter Wemm 		if (ecp == &gp->excmd)
2186f0957ccaSPeter Wemm 			break;
2187b8ba871bSPeter Wemm 		if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2188f0957ccaSPeter Wemm 			while ((rp = TAILQ_FIRST(ecp->rq)) != NULL) {
2189f0957ccaSPeter Wemm 				TAILQ_REMOVE(ecp->rq, rp, q);
2190b8ba871bSPeter Wemm 				free(rp);
2191b8ba871bSPeter Wemm 			}
2192b8ba871bSPeter Wemm 			free(ecp->o_cp);
2193b8ba871bSPeter Wemm 		}
2194f0957ccaSPeter Wemm 		SLIST_REMOVE_HEAD(gp->ecq, q);
2195b8ba871bSPeter Wemm 		free(ecp);
2196b8ba871bSPeter Wemm 	}
2197f0957ccaSPeter Wemm 
2198f0957ccaSPeter Wemm 	ecp->if_name = NULL;
2199f0957ccaSPeter Wemm 	ecp->clen = 0;
2200b8ba871bSPeter Wemm 	return (0);
2201b8ba871bSPeter Wemm }
2202b8ba871bSPeter Wemm 
2203b8ba871bSPeter Wemm /*
2204b8ba871bSPeter Wemm  * ex_unknown --
2205b8ba871bSPeter Wemm  *	Display an unknown command name.
2206b8ba871bSPeter Wemm  */
2207b8ba871bSPeter Wemm static void
ex_unknown(SCR * sp,CHAR_T * cmd,size_t len)2208f0957ccaSPeter Wemm ex_unknown(SCR *sp, CHAR_T *cmd, size_t len)
2209b8ba871bSPeter Wemm {
2210b8ba871bSPeter Wemm 	size_t blen;
2211f0957ccaSPeter Wemm 	CHAR_T *bp;
2212b8ba871bSPeter Wemm 
2213f0957ccaSPeter Wemm 	GET_SPACE_GOTOW(sp, bp, blen, len + 1);
2214b8ba871bSPeter Wemm 	bp[len] = '\0';
2215f0957ccaSPeter Wemm 	MEMCPY(bp, cmd, len);
2216f0957ccaSPeter Wemm 	msgq_wstr(sp, M_ERR, bp, "098|The %s command is unknown");
2217f0957ccaSPeter Wemm 	FREE_SPACEW(sp, bp, blen);
2218b8ba871bSPeter Wemm 
2219b8ba871bSPeter Wemm alloc_err:
2220b8ba871bSPeter Wemm 	return;
2221b8ba871bSPeter Wemm }
2222b8ba871bSPeter Wemm 
2223b8ba871bSPeter Wemm /*
2224b8ba871bSPeter Wemm  * ex_is_abbrev -
2225b8ba871bSPeter Wemm  *	The vi text input routine needs to know if ex thinks this is an
2226b8ba871bSPeter Wemm  *	[un]abbreviate command, so it can turn off abbreviations.  See
2227b8ba871bSPeter Wemm  *	the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2228b8ba871bSPeter Wemm  *
2229c271fa92SBaptiste Daroussin  * PUBLIC: int ex_is_abbrev(CHAR_T *, size_t);
2230b8ba871bSPeter Wemm  */
2231b8ba871bSPeter Wemm int
ex_is_abbrev(CHAR_T * name,size_t len)2232f0957ccaSPeter Wemm ex_is_abbrev(CHAR_T *name, size_t len)
2233b8ba871bSPeter Wemm {
2234b8ba871bSPeter Wemm 	EXCMDLIST const *cp;
2235b8ba871bSPeter Wemm 
2236b8ba871bSPeter Wemm 	return ((cp = ex_comm_search(name, len)) != NULL &&
2237b8ba871bSPeter Wemm 	    (cp == &cmds[C_ABBR] || cp == &cmds[C_UNABBREVIATE]));
2238b8ba871bSPeter Wemm }
2239b8ba871bSPeter Wemm 
2240b8ba871bSPeter Wemm /*
2241b8ba871bSPeter Wemm  * ex_is_unmap -
2242b8ba871bSPeter Wemm  *	The vi text input routine needs to know if ex thinks this is an
2243b8ba871bSPeter Wemm  *	unmap command, so it can turn off input mapping.  See the usual
2244b8ba871bSPeter Wemm  *	ranting in the vi/v_txt_ev.c:txt_unmap() routine.
2245b8ba871bSPeter Wemm  *
2246c271fa92SBaptiste Daroussin  * PUBLIC: int ex_is_unmap(CHAR_T *, size_t);
2247b8ba871bSPeter Wemm  */
2248b8ba871bSPeter Wemm int
ex_is_unmap(CHAR_T * name,size_t len)2249f0957ccaSPeter Wemm ex_is_unmap(CHAR_T *name, size_t len)
2250b8ba871bSPeter Wemm {
2251b8ba871bSPeter Wemm 	EXCMDLIST const *cp;
2252b8ba871bSPeter Wemm 
2253b8ba871bSPeter Wemm 	/*
2254b8ba871bSPeter Wemm 	 * The command the vi input routines are really interested in
2255b8ba871bSPeter Wemm 	 * is "unmap!", not just unmap.
2256b8ba871bSPeter Wemm 	 */
2257b8ba871bSPeter Wemm 	if (name[len - 1] != '!')
2258b8ba871bSPeter Wemm 		return (0);
2259b8ba871bSPeter Wemm 	--len;
2260b8ba871bSPeter Wemm 	return ((cp = ex_comm_search(name, len)) != NULL &&
2261b8ba871bSPeter Wemm 	    cp == &cmds[C_UNMAP]);
2262b8ba871bSPeter Wemm }
2263b8ba871bSPeter Wemm 
2264b8ba871bSPeter Wemm /*
2265b8ba871bSPeter Wemm  * ex_comm_search --
2266b8ba871bSPeter Wemm  *	Search for a command name.
2267b8ba871bSPeter Wemm  */
2268b8ba871bSPeter Wemm static EXCMDLIST const *
ex_comm_search(CHAR_T * name,size_t len)2269f0957ccaSPeter Wemm ex_comm_search(CHAR_T *name, size_t len)
2270b8ba871bSPeter Wemm {
2271b8ba871bSPeter Wemm 	EXCMDLIST const *cp;
2272b8ba871bSPeter Wemm 
2273b8ba871bSPeter Wemm 	for (cp = cmds; cp->name != NULL; ++cp) {
2274b8ba871bSPeter Wemm 		if (cp->name[0] > name[0])
2275b8ba871bSPeter Wemm 			return (NULL);
2276b8ba871bSPeter Wemm 		if (cp->name[0] != name[0])
2277b8ba871bSPeter Wemm 			continue;
22786680e5a5SBaptiste Daroussin 		if (STRLEN(cp->name) >= len &&
22796680e5a5SBaptiste Daroussin 			!MEMCMP(name, cp->name, len))
2280b8ba871bSPeter Wemm 			return (cp);
2281b8ba871bSPeter Wemm 	}
2282b8ba871bSPeter Wemm 	return (NULL);
2283b8ba871bSPeter Wemm }
2284b8ba871bSPeter Wemm 
2285b8ba871bSPeter Wemm /*
2286b8ba871bSPeter Wemm  * ex_badaddr --
2287b8ba871bSPeter Wemm  *	Display a bad address message.
2288b8ba871bSPeter Wemm  *
2289b8ba871bSPeter Wemm  * PUBLIC: void ex_badaddr
2290c271fa92SBaptiste Daroussin  * PUBLIC:   (SCR *, EXCMDLIST const *, enum badaddr, enum nresult);
2291b8ba871bSPeter Wemm  */
2292b8ba871bSPeter Wemm void
ex_badaddr(SCR * sp,const EXCMDLIST * cp,enum badaddr ba,enum nresult nret)2293f0957ccaSPeter Wemm ex_badaddr(SCR *sp, const EXCMDLIST *cp, enum badaddr ba, enum nresult nret)
2294b8ba871bSPeter Wemm {
2295b8ba871bSPeter Wemm 	recno_t lno;
2296b8ba871bSPeter Wemm 
2297b8ba871bSPeter Wemm 	switch (nret) {
2298b8ba871bSPeter Wemm 	case NUM_OK:
2299b8ba871bSPeter Wemm 		break;
2300b8ba871bSPeter Wemm 	case NUM_ERR:
2301b8ba871bSPeter Wemm 		msgq(sp, M_SYSERR, NULL);
2302b8ba871bSPeter Wemm 		return;
2303b8ba871bSPeter Wemm 	case NUM_OVER:
2304b8ba871bSPeter Wemm 		msgq(sp, M_ERR, "099|Address value overflow");
2305b8ba871bSPeter Wemm 		return;
2306b8ba871bSPeter Wemm 	case NUM_UNDER:
2307b8ba871bSPeter Wemm 		msgq(sp, M_ERR, "100|Address value underflow");
2308b8ba871bSPeter Wemm 		return;
2309b8ba871bSPeter Wemm 	}
2310b8ba871bSPeter Wemm 
2311b8ba871bSPeter Wemm 	/*
2312b8ba871bSPeter Wemm 	 * When encountering an address error, tell the user if there's no
2313b8ba871bSPeter Wemm 	 * underlying file, that's the real problem.
2314b8ba871bSPeter Wemm 	 */
2315b8ba871bSPeter Wemm 	if (sp->ep == NULL) {
2316f0957ccaSPeter Wemm 		ex_wemsg(sp, cp ? cp->name : NULL, EXM_NOFILEYET);
2317b8ba871bSPeter Wemm 		return;
2318b8ba871bSPeter Wemm 	}
2319b8ba871bSPeter Wemm 
2320b8ba871bSPeter Wemm 	switch (ba) {
2321b8ba871bSPeter Wemm 	case A_COMBO:
2322b8ba871bSPeter Wemm 		msgq(sp, M_ERR, "101|Illegal address combination");
2323b8ba871bSPeter Wemm 		break;
2324b8ba871bSPeter Wemm 	case A_EOF:
2325b8ba871bSPeter Wemm 		if (db_last(sp, &lno))
2326b8ba871bSPeter Wemm 			return;
2327b8ba871bSPeter Wemm 		if (lno != 0) {
2328b8ba871bSPeter Wemm 			msgq(sp, M_ERR,
2329b8ba871bSPeter Wemm 			    "102|Illegal address: only %lu lines in the file",
2330f0957ccaSPeter Wemm 			    (u_long)lno);
2331b8ba871bSPeter Wemm 			break;
2332b8ba871bSPeter Wemm 		}
2333b8ba871bSPeter Wemm 		/* FALLTHROUGH */
2334b8ba871bSPeter Wemm 	case A_EMPTY:
2335b8ba871bSPeter Wemm 		msgq(sp, M_ERR, "103|Illegal address: the file is empty");
2336b8ba871bSPeter Wemm 		break;
2337b8ba871bSPeter Wemm 	case A_NOTSET:
2338b8ba871bSPeter Wemm 		abort();
2339b8ba871bSPeter Wemm 		/* NOTREACHED */
2340b8ba871bSPeter Wemm 	case A_ZERO:
2341f0957ccaSPeter Wemm 		msgq_wstr(sp, M_ERR, cp->name,
2342f0957ccaSPeter Wemm 		    "104|The %s command doesn't permit an address of 0");
2343b8ba871bSPeter Wemm 		break;
2344b8ba871bSPeter Wemm 	}
2345b8ba871bSPeter Wemm 	return;
2346b8ba871bSPeter Wemm }
2347b8ba871bSPeter Wemm 
2348b8ba871bSPeter Wemm #if defined(DEBUG) && defined(COMLOG)
2349b8ba871bSPeter Wemm /*
2350b8ba871bSPeter Wemm  * ex_comlog --
2351b8ba871bSPeter Wemm  *	Log ex commands.
2352b8ba871bSPeter Wemm  */
2353b8ba871bSPeter Wemm static void
ex_comlog(sp,ecp)2354b8ba871bSPeter Wemm ex_comlog(sp, ecp)
2355b8ba871bSPeter Wemm 	SCR *sp;
2356b8ba871bSPeter Wemm 	EXCMD *ecp;
2357b8ba871bSPeter Wemm {
2358f0957ccaSPeter Wemm 	TRACE(sp, "ecmd: "WS, ecp->cmd->name);
2359b8ba871bSPeter Wemm 	if (ecp->addrcnt > 0) {
2360b8ba871bSPeter Wemm 		TRACE(sp, " a1 %d", ecp->addr1.lno);
2361b8ba871bSPeter Wemm 		if (ecp->addrcnt > 1)
2362b8ba871bSPeter Wemm 			TRACE(sp, " a2: %d", ecp->addr2.lno);
2363b8ba871bSPeter Wemm 	}
2364b8ba871bSPeter Wemm 	if (ecp->lineno)
2365b8ba871bSPeter Wemm 		TRACE(sp, " line %d", ecp->lineno);
2366b8ba871bSPeter Wemm 	if (ecp->flags)
2367b8ba871bSPeter Wemm 		TRACE(sp, " flags 0x%x", ecp->flags);
2368f0957ccaSPeter Wemm 	if (FL_ISSET(ecp->iflags, E_C_BUFFER))
2369f0957ccaSPeter Wemm 		TRACE(sp, " buffer "WC, ecp->buffer);
2370f0957ccaSPeter Wemm 	if (ecp->argc) {
2371f0957ccaSPeter Wemm 		int cnt;
2372b8ba871bSPeter Wemm 		for (cnt = 0; cnt < ecp->argc; ++cnt)
2373f0957ccaSPeter Wemm 			TRACE(sp, " arg %d: {"WS"}", cnt, ecp->argv[cnt]->bp);
2374f0957ccaSPeter Wemm 	}
2375b8ba871bSPeter Wemm 	TRACE(sp, "\n");
2376b8ba871bSPeter Wemm }
2377b8ba871bSPeter Wemm #endif
2378