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