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