1*0a6a1f1dSLionel Sambuc /* $NetBSD: ex.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
584d9c625SLionel Sambuc * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc * Keith Bostic. All rights reserved.
784d9c625SLionel Sambuc *
884d9c625SLionel Sambuc * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc */
1084d9c625SLionel Sambuc
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc 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 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc #include <sys/stat.h>
2584d9c625SLionel Sambuc #include <sys/time.h>
2684d9c625SLionel Sambuc
2784d9c625SLionel Sambuc #include <bitstring.h>
2884d9c625SLionel Sambuc #include <ctype.h>
2984d9c625SLionel Sambuc #include <errno.h>
3084d9c625SLionel Sambuc #include <fcntl.h>
3184d9c625SLionel Sambuc #include <limits.h>
3284d9c625SLionel Sambuc #include <stdio.h>
3384d9c625SLionel Sambuc #include <stdlib.h>
3484d9c625SLionel Sambuc #include <string.h>
3584d9c625SLionel Sambuc #include <unistd.h>
3684d9c625SLionel Sambuc
3784d9c625SLionel Sambuc #include "../common/common.h"
3884d9c625SLionel Sambuc #include "../common/multibyte.h"
3984d9c625SLionel Sambuc #include "../vi/vi.h"
4084d9c625SLionel Sambuc
4184d9c625SLionel Sambuc #if defined(DEBUG) && defined(COMLOG)
4284d9c625SLionel Sambuc static void ex_comlog __P((SCR *, EXCMD *));
4384d9c625SLionel Sambuc #endif
4484d9c625SLionel Sambuc static EXCMDLIST const *
4584d9c625SLionel Sambuc ex_comm_search __P((SCR *, CHAR_T *, size_t));
4684d9c625SLionel Sambuc static int ex_discard __P((SCR *));
4784d9c625SLionel Sambuc static int ex_line __P((SCR *, EXCMD *, MARK *, int *, int *));
4884d9c625SLionel Sambuc static int ex_load __P((SCR *));
4984d9c625SLionel Sambuc static void ex_unknown __P((SCR *, CHAR_T *, size_t));
5084d9c625SLionel Sambuc
5184d9c625SLionel Sambuc /*
5284d9c625SLionel Sambuc * ex --
5384d9c625SLionel Sambuc * Main ex loop.
5484d9c625SLionel Sambuc *
5584d9c625SLionel Sambuc * PUBLIC: int ex __P((SCR **));
5684d9c625SLionel Sambuc */
5784d9c625SLionel Sambuc int
ex(SCR ** spp)5884d9c625SLionel Sambuc ex(SCR **spp)
5984d9c625SLionel Sambuc {
6084d9c625SLionel Sambuc GS *gp;
6184d9c625SLionel Sambuc WIN *wp;
6284d9c625SLionel Sambuc MSGS *mp;
6384d9c625SLionel Sambuc SCR *sp;
6484d9c625SLionel Sambuc TEXT *tp;
6584d9c625SLionel Sambuc u_int32_t flags;
6684d9c625SLionel Sambuc
6784d9c625SLionel Sambuc sp = *spp;
6884d9c625SLionel Sambuc wp = sp->wp;
6984d9c625SLionel Sambuc gp = sp->gp;
7084d9c625SLionel Sambuc
7184d9c625SLionel Sambuc /* Start the ex screen. */
7284d9c625SLionel Sambuc if (ex_init(sp))
7384d9c625SLionel Sambuc return (1);
7484d9c625SLionel Sambuc
7584d9c625SLionel Sambuc /* Flush any saved messages. */
7684d9c625SLionel Sambuc while ((mp = LIST_FIRST(&gp->msgq)) != NULL) {
7784d9c625SLionel Sambuc wp->scr_msg(sp, mp->mtype, mp->buf, mp->len);
7884d9c625SLionel Sambuc LIST_REMOVE(mp, q);
7984d9c625SLionel Sambuc free(mp->buf);
8084d9c625SLionel Sambuc free(mp);
8184d9c625SLionel Sambuc }
8284d9c625SLionel Sambuc
8384d9c625SLionel Sambuc /* If reading from a file, errors should have name and line info. */
8484d9c625SLionel Sambuc if (F_ISSET(gp, G_SCRIPTED)) {
8584d9c625SLionel Sambuc wp->excmd.if_lno = 1;
8684d9c625SLionel Sambuc wp->excmd.if_name = strdup("script");
8784d9c625SLionel Sambuc }
8884d9c625SLionel Sambuc
8984d9c625SLionel Sambuc /*
9084d9c625SLionel Sambuc * !!!
9184d9c625SLionel Sambuc * Initialize the text flags. The beautify edit option historically
9284d9c625SLionel Sambuc * applied to ex command input read from a file. In addition, the
9384d9c625SLionel Sambuc * first time a ^H was discarded from the input, there was a message,
9484d9c625SLionel Sambuc * "^H discarded", that was displayed. We don't bother.
9584d9c625SLionel Sambuc */
9684d9c625SLionel Sambuc LF_INIT(TXT_BACKSLASH | TXT_CNTRLD | TXT_CR);
9784d9c625SLionel Sambuc for (;; ++wp->excmd.if_lno) {
9884d9c625SLionel Sambuc /* Display status line and flush. */
9984d9c625SLionel Sambuc if (F_ISSET(sp, SC_STATUS)) {
10084d9c625SLionel Sambuc if (!F_ISSET(sp, SC_EX_SILENT))
10184d9c625SLionel Sambuc msgq_status(sp, sp->lno, 0);
10284d9c625SLionel Sambuc F_CLR(sp, SC_STATUS);
10384d9c625SLionel Sambuc }
10484d9c625SLionel Sambuc (void)ex_fflush(sp);
10584d9c625SLionel Sambuc
10684d9c625SLionel Sambuc /* Set the flags the user can reset. */
10784d9c625SLionel Sambuc if (O_ISSET(sp, O_BEAUTIFY))
10884d9c625SLionel Sambuc LF_SET(TXT_BEAUTIFY);
10984d9c625SLionel Sambuc if (O_ISSET(sp, O_PROMPT))
11084d9c625SLionel Sambuc LF_SET(TXT_PROMPT);
11184d9c625SLionel Sambuc
11284d9c625SLionel Sambuc /* Clear any current interrupts, and get a command. */
11384d9c625SLionel Sambuc CLR_INTERRUPT(sp);
11484d9c625SLionel Sambuc if (ex_txt(sp, &sp->tiq, ':', flags))
11584d9c625SLionel Sambuc return (1);
11684d9c625SLionel Sambuc if (INTERRUPTED(sp)) {
11784d9c625SLionel Sambuc (void)ex_puts(sp, "\n");
11884d9c625SLionel Sambuc (void)ex_fflush(sp);
11984d9c625SLionel Sambuc continue;
12084d9c625SLionel Sambuc }
12184d9c625SLionel Sambuc
12284d9c625SLionel Sambuc /* Initialize the command structure. */
12384d9c625SLionel Sambuc CLEAR_EX_PARSER(&wp->excmd);
12484d9c625SLionel Sambuc
12584d9c625SLionel Sambuc /*
12684d9c625SLionel Sambuc * If the user entered a single carriage return, send
12784d9c625SLionel Sambuc * ex_cmd() a separator -- it discards single newlines.
12884d9c625SLionel Sambuc */
12984d9c625SLionel Sambuc tp = TAILQ_FIRST(&sp->tiq);
13084d9c625SLionel Sambuc if (tp->len == 0) {
13184d9c625SLionel Sambuc static CHAR_T space = ' ';
13284d9c625SLionel Sambuc wp->excmd.cp = &space; /* __TK__ why not |? */
13384d9c625SLionel Sambuc wp->excmd.clen = 1;
13484d9c625SLionel Sambuc } else {
13584d9c625SLionel Sambuc wp->excmd.cp = tp->lb;
13684d9c625SLionel Sambuc wp->excmd.clen = tp->len;
13784d9c625SLionel Sambuc }
13884d9c625SLionel Sambuc F_INIT(&wp->excmd, E_NRSEP);
13984d9c625SLionel Sambuc
14084d9c625SLionel Sambuc if (ex_cmd(sp) && F_ISSET(gp, G_SCRIPTED))
14184d9c625SLionel Sambuc return (1);
14284d9c625SLionel Sambuc
14384d9c625SLionel Sambuc if (INTERRUPTED(sp)) {
14484d9c625SLionel Sambuc CLR_INTERRUPT(sp);
14584d9c625SLionel Sambuc msgq(sp, M_ERR, "170|Interrupted");
14684d9c625SLionel Sambuc }
14784d9c625SLionel Sambuc
14884d9c625SLionel Sambuc /*
14984d9c625SLionel Sambuc * If the last command caused a restart, or switched screens
15084d9c625SLionel Sambuc * or into vi, return.
15184d9c625SLionel Sambuc */
15284d9c625SLionel Sambuc if (F_ISSET(gp, G_SRESTART) || F_ISSET(sp, SC_SSWITCH | SC_VI)) {
15384d9c625SLionel Sambuc *spp = sp;
15484d9c625SLionel Sambuc break;
15584d9c625SLionel Sambuc }
15684d9c625SLionel Sambuc
15784d9c625SLionel Sambuc /* If the last command switched files, we don't care. */
15884d9c625SLionel Sambuc F_CLR(sp, SC_FSWITCH);
15984d9c625SLionel Sambuc
16084d9c625SLionel Sambuc /*
16184d9c625SLionel Sambuc * If we're exiting this screen, move to the next one. By
16284d9c625SLionel Sambuc * definition, this means returning into vi, so return to the
16384d9c625SLionel Sambuc * main editor loop. The ordering is careful, don't discard
16484d9c625SLionel Sambuc * the contents of sp until the end.
16584d9c625SLionel Sambuc */
16684d9c625SLionel Sambuc if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
16784d9c625SLionel Sambuc if (file_end(sp, NULL, F_ISSET(sp, SC_EXIT_FORCE)))
16884d9c625SLionel Sambuc return (1);
16984d9c625SLionel Sambuc *spp = screen_next(sp);
17084d9c625SLionel Sambuc return (screen_end(sp));
17184d9c625SLionel Sambuc }
17284d9c625SLionel Sambuc }
17384d9c625SLionel Sambuc return (0);
17484d9c625SLionel Sambuc }
17584d9c625SLionel Sambuc
17684d9c625SLionel Sambuc /*
17784d9c625SLionel Sambuc * ex_cmd --
17884d9c625SLionel Sambuc * The guts of the ex parser: parse and execute a string containing
17984d9c625SLionel Sambuc * ex commands.
18084d9c625SLionel Sambuc *
18184d9c625SLionel Sambuc * !!!
18284d9c625SLionel Sambuc * This code MODIFIES the string that gets passed in, to delete quoting
18384d9c625SLionel Sambuc * characters, etc. The string cannot be readonly/text space, nor should
18484d9c625SLionel Sambuc * you expect to use it again after ex_cmd() returns.
18584d9c625SLionel Sambuc *
18684d9c625SLionel Sambuc * !!!
18784d9c625SLionel Sambuc * For the fun of it, if you want to see if a vi clone got the ex argument
18884d9c625SLionel Sambuc * parsing right, try:
18984d9c625SLionel Sambuc *
19084d9c625SLionel Sambuc * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
19184d9c625SLionel Sambuc * vi
19284d9c625SLionel Sambuc * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
19384d9c625SLionel Sambuc *
19484d9c625SLionel Sambuc * or: vi
19584d9c625SLionel Sambuc * :set|file|append|set|file
19684d9c625SLionel Sambuc *
19784d9c625SLionel Sambuc * For extra credit, try them in a startup .exrc file.
19884d9c625SLionel Sambuc *
19984d9c625SLionel Sambuc * PUBLIC: int ex_cmd __P((SCR *));
20084d9c625SLionel Sambuc */
20184d9c625SLionel Sambuc int
ex_cmd(SCR * sp)20284d9c625SLionel Sambuc ex_cmd(SCR *sp)
20384d9c625SLionel Sambuc {
20484d9c625SLionel Sambuc enum nresult nret;
20584d9c625SLionel Sambuc EX_PRIVATE *exp;
20684d9c625SLionel Sambuc EXCMD *ecp;
20784d9c625SLionel Sambuc GS *gp;
20884d9c625SLionel Sambuc WIN *wp;
20984d9c625SLionel Sambuc MARK cur;
21084d9c625SLionel Sambuc db_recno_t lno;
21184d9c625SLionel Sambuc size_t arg1_len, discard, len;
21284d9c625SLionel Sambuc u_int32_t flags;
21384d9c625SLionel Sambuc long ltmp;
21484d9c625SLionel Sambuc int at_found, gv_found;
21584d9c625SLionel Sambuc int cnt, delim, isaddr, namelen;
21684d9c625SLionel Sambuc int newscreen, notempty, tmp, vi_address;
21784d9c625SLionel Sambuc CHAR_T *arg1, *s, *p, *t;
21884d9c625SLionel Sambuc ARG_CHAR_T ch;
21984d9c625SLionel Sambuc const CHAR_T *n;
22084d9c625SLionel Sambuc const char *np;
22184d9c625SLionel Sambuc
22284d9c625SLionel Sambuc gp = sp->gp;
22384d9c625SLionel Sambuc wp = sp->wp;
22484d9c625SLionel Sambuc exp = EXP(sp);
22584d9c625SLionel Sambuc ch = '\0'; /* XXX: gcc -O1 -Wuninitialized */
22684d9c625SLionel Sambuc
22784d9c625SLionel Sambuc /*
22884d9c625SLionel Sambuc * We always start running the command on the top of the stack.
22984d9c625SLionel Sambuc * This means that *everything* must be resolved when we leave
23084d9c625SLionel Sambuc * this function for any reason.
23184d9c625SLionel Sambuc */
23284d9c625SLionel Sambuc loop: ecp = LIST_FIRST(&wp->ecq);
23384d9c625SLionel Sambuc
23484d9c625SLionel Sambuc /* If we're reading a command from a file, set up error information. */
23584d9c625SLionel Sambuc if (ecp->if_name != NULL) {
23684d9c625SLionel Sambuc wp->if_lno = ecp->if_lno;
23784d9c625SLionel Sambuc wp->if_name = ecp->if_name;
23884d9c625SLionel Sambuc }
23984d9c625SLionel Sambuc
24084d9c625SLionel Sambuc /*
24184d9c625SLionel Sambuc * If a move to the end of the file is scheduled for this command,
24284d9c625SLionel Sambuc * do it now.
24384d9c625SLionel Sambuc */
24484d9c625SLionel Sambuc if (F_ISSET(ecp, E_MOVETOEND)) {
24584d9c625SLionel Sambuc if (db_last(sp, &sp->lno))
24684d9c625SLionel Sambuc goto rfail;
24784d9c625SLionel Sambuc sp->cno = 0;
24884d9c625SLionel Sambuc F_CLR(ecp, E_MOVETOEND);
24984d9c625SLionel Sambuc }
25084d9c625SLionel Sambuc
25184d9c625SLionel Sambuc /* If we found a newline, increment the count now. */
25284d9c625SLionel Sambuc if (F_ISSET(ecp, E_NEWLINE)) {
25384d9c625SLionel Sambuc ++wp->if_lno;
25484d9c625SLionel Sambuc ++ecp->if_lno;
25584d9c625SLionel Sambuc F_CLR(ecp, E_NEWLINE);
25684d9c625SLionel Sambuc }
25784d9c625SLionel Sambuc
25884d9c625SLionel Sambuc /* (Re)initialize the EXCMD structure, preserving some flags. */
25984d9c625SLionel Sambuc CLEAR_EX_CMD(ecp);
26084d9c625SLionel Sambuc
26184d9c625SLionel Sambuc /* Initialize the argument structures. */
26284d9c625SLionel Sambuc if (argv_init(sp, ecp))
26384d9c625SLionel Sambuc goto err;
26484d9c625SLionel Sambuc
26584d9c625SLionel Sambuc /* Initialize +cmd, saved command information. */
26684d9c625SLionel Sambuc arg1 = NULL;
26784d9c625SLionel Sambuc ecp->save_cmdlen = 0;
26884d9c625SLionel Sambuc
26984d9c625SLionel Sambuc /* Skip <blank>s, empty lines. */
27084d9c625SLionel Sambuc for (notempty = 0; ecp->clen > 0; ++ecp->cp, --ecp->clen)
27184d9c625SLionel Sambuc if ((ch = (UCHAR_T)*ecp->cp) == '\n') {
27284d9c625SLionel Sambuc ++wp->if_lno;
27384d9c625SLionel Sambuc ++ecp->if_lno;
27484d9c625SLionel Sambuc } else if (ISBLANK(ch))
27584d9c625SLionel Sambuc notempty = 1;
27684d9c625SLionel Sambuc else
27784d9c625SLionel Sambuc break;
27884d9c625SLionel Sambuc
27984d9c625SLionel Sambuc /*
28084d9c625SLionel Sambuc * !!!
28184d9c625SLionel Sambuc * Permit extra colons at the start of the line. Historically,
28284d9c625SLionel Sambuc * ex/vi allowed a single extra one. It's simpler not to count.
28384d9c625SLionel Sambuc * The stripping is done here because, historically, any command
28484d9c625SLionel Sambuc * could have preceding colons, e.g. ":g/pattern/:p" worked.
28584d9c625SLionel Sambuc */
28684d9c625SLionel Sambuc if (ecp->clen != 0 && ch == ':') {
28784d9c625SLionel Sambuc notempty = 1;
28884d9c625SLionel Sambuc while (--ecp->clen > 0 && (ch = (UCHAR_T)*++ecp->cp) == ':');
28984d9c625SLionel Sambuc }
29084d9c625SLionel Sambuc
29184d9c625SLionel Sambuc /*
29284d9c625SLionel Sambuc * Command lines that start with a double-quote are comments.
29384d9c625SLionel Sambuc *
29484d9c625SLionel Sambuc * !!!
29584d9c625SLionel Sambuc * Historically, there was no escape or delimiter for a comment, e.g.
29684d9c625SLionel Sambuc * :"foo|set was a single comment and nothing was output. Since nvi
29784d9c625SLionel Sambuc * permits users to escape <newline> characters into command lines, we
29884d9c625SLionel Sambuc * have to check for that case.
29984d9c625SLionel Sambuc */
30084d9c625SLionel Sambuc if (ecp->clen != 0 && ch == '"') {
30184d9c625SLionel Sambuc while (--ecp->clen > 0 && *++ecp->cp != '\n');
30284d9c625SLionel Sambuc if (*ecp->cp == '\n') {
30384d9c625SLionel Sambuc F_SET(ecp, E_NEWLINE);
30484d9c625SLionel Sambuc ++ecp->cp;
30584d9c625SLionel Sambuc --ecp->clen;
30684d9c625SLionel Sambuc }
30784d9c625SLionel Sambuc goto loop;
30884d9c625SLionel Sambuc }
30984d9c625SLionel Sambuc
31084d9c625SLionel Sambuc /* Skip whitespace. */
31184d9c625SLionel Sambuc for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
31284d9c625SLionel Sambuc ch = (UCHAR_T)*ecp->cp;
31384d9c625SLionel Sambuc if (!ISBLANK(ch))
31484d9c625SLionel Sambuc break;
31584d9c625SLionel Sambuc }
31684d9c625SLionel Sambuc
31784d9c625SLionel Sambuc /*
31884d9c625SLionel Sambuc * The last point at which an empty line can mean do nothing.
31984d9c625SLionel Sambuc *
32084d9c625SLionel Sambuc * !!!
32184d9c625SLionel Sambuc * Historically, in ex mode, lines containing only <blank> characters
32284d9c625SLionel Sambuc * were the same as a single <carriage-return>, i.e. a default command.
32384d9c625SLionel Sambuc * In vi mode, they were ignored. In .exrc files this was a serious
32484d9c625SLionel Sambuc * annoyance, as vi kept trying to treat them as print commands. We
32584d9c625SLionel Sambuc * ignore backward compatibility in this case, discarding lines that
32684d9c625SLionel Sambuc * contain only <blank> characters from .exrc files.
32784d9c625SLionel Sambuc *
32884d9c625SLionel Sambuc * !!!
32984d9c625SLionel Sambuc * This is where you end up when you're done a command, i.e. clen has
33084d9c625SLionel Sambuc * gone to zero. Continue if there are more commands to run.
33184d9c625SLionel Sambuc */
33284d9c625SLionel Sambuc if (ecp->clen == 0 &&
33384d9c625SLionel Sambuc (!notempty || F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_BLIGNORE))) {
33484d9c625SLionel Sambuc if (ex_load(sp))
33584d9c625SLionel Sambuc goto rfail;
33684d9c625SLionel Sambuc ecp = LIST_FIRST(&wp->ecq);
33784d9c625SLionel Sambuc if (ecp->clen == 0)
33884d9c625SLionel Sambuc goto rsuccess;
33984d9c625SLionel Sambuc goto loop;
34084d9c625SLionel Sambuc }
34184d9c625SLionel Sambuc
34284d9c625SLionel Sambuc /*
34384d9c625SLionel Sambuc * Check to see if this is a command for which we may want to move
34484d9c625SLionel Sambuc * the cursor back up to the previous line. (The command :1<CR>
34584d9c625SLionel Sambuc * wants a <newline> separator, but the command :<CR> wants to erase
34684d9c625SLionel Sambuc * the command line.) If the line is empty except for <blank>s,
34784d9c625SLionel Sambuc * <carriage-return> or <eof>, we'll probably want to move up. I
34884d9c625SLionel Sambuc * don't think there's any way to get <blank> characters *after* the
34984d9c625SLionel Sambuc * command character, but this is the ex parser, and I've been wrong
35084d9c625SLionel Sambuc * before.
35184d9c625SLionel Sambuc */
35284d9c625SLionel Sambuc if (F_ISSET(ecp, E_NRSEP) &&
35384d9c625SLionel Sambuc ecp->clen != 0 && (ecp->clen != 1 || ecp->cp[0] != '\004'))
35484d9c625SLionel Sambuc F_CLR(ecp, E_NRSEP);
35584d9c625SLionel Sambuc
35684d9c625SLionel Sambuc /* Parse command addresses. */
35784d9c625SLionel Sambuc if (ex_range(sp, ecp, &tmp))
35884d9c625SLionel Sambuc goto rfail;
35984d9c625SLionel Sambuc if (tmp)
36084d9c625SLionel Sambuc goto err;
36184d9c625SLionel Sambuc
36284d9c625SLionel Sambuc /*
36384d9c625SLionel Sambuc * Skip <blank>s and any more colons (the command :3,5:print
36484d9c625SLionel Sambuc * worked, historically).
36584d9c625SLionel Sambuc */
36684d9c625SLionel Sambuc for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
36784d9c625SLionel Sambuc ch = (UCHAR_T)*ecp->cp;
36884d9c625SLionel Sambuc if (!ISBLANK(ch) && ch != ':')
36984d9c625SLionel Sambuc break;
37084d9c625SLionel Sambuc }
37184d9c625SLionel Sambuc
37284d9c625SLionel Sambuc /*
37384d9c625SLionel Sambuc * If no command, ex does the last specified of p, l, or #, and vi
37484d9c625SLionel Sambuc * moves to the line. Otherwise, determine the length of the command
37584d9c625SLionel Sambuc * name by looking for the first non-alphabetic character. (There
37684d9c625SLionel Sambuc * are a few non-alphabetic characters in command names, but they're
37784d9c625SLionel Sambuc * all single character commands.) This isn't a great test, because
37884d9c625SLionel Sambuc * it means that, for the command ":e +cut.c file", we'll report that
37984d9c625SLionel Sambuc * the command "cut" wasn't known. However, it makes ":e+35 file" work
38084d9c625SLionel Sambuc * correctly.
38184d9c625SLionel Sambuc *
38284d9c625SLionel Sambuc * !!!
38384d9c625SLionel Sambuc * Historically, lines with multiple adjacent (or <blank> separated)
38484d9c625SLionel Sambuc * command separators were very strange. For example, the command
38584d9c625SLionel Sambuc * |||<carriage-return>, when the cursor was on line 1, displayed
38684d9c625SLionel Sambuc * lines 2, 3 and 5 of the file. In addition, the command " | "
38784d9c625SLionel Sambuc * would only display the line after the next line, instead of the
38884d9c625SLionel Sambuc * next two lines. No ideas why. It worked reasonably when executed
38984d9c625SLionel Sambuc * from vi mode, and displayed lines 2, 3, and 4, so we do a default
39084d9c625SLionel Sambuc * command for each separator.
39184d9c625SLionel Sambuc */
39284d9c625SLionel Sambuc #define SINGLE_CHAR_COMMANDS "\004!#&*<=>@~"
39384d9c625SLionel Sambuc newscreen = 0;
39484d9c625SLionel Sambuc if (ecp->clen != 0 && ecp->cp[0] != '|' && ecp->cp[0] != '\n') {
39584d9c625SLionel Sambuc if (strchr(SINGLE_CHAR_COMMANDS, *ecp->cp)) {
39684d9c625SLionel Sambuc p = ecp->cp;
39784d9c625SLionel Sambuc ++ecp->cp;
39884d9c625SLionel Sambuc --ecp->clen;
39984d9c625SLionel Sambuc namelen = 1;
40084d9c625SLionel Sambuc } else {
40184d9c625SLionel Sambuc for (p = ecp->cp;
40284d9c625SLionel Sambuc ecp->clen > 0; --ecp->clen, ++ecp->cp)
40384d9c625SLionel Sambuc if (!ISALPHA((UCHAR_T)*ecp->cp))
40484d9c625SLionel Sambuc break;
40584d9c625SLionel Sambuc if ((namelen = ecp->cp - p) == 0) {
40684d9c625SLionel Sambuc msgq(sp, M_ERR, "080|Unknown command name");
40784d9c625SLionel Sambuc goto err;
40884d9c625SLionel Sambuc }
40984d9c625SLionel Sambuc }
41084d9c625SLionel Sambuc
41184d9c625SLionel Sambuc /*
41284d9c625SLionel Sambuc * !!!
41384d9c625SLionel Sambuc * Historic vi permitted flags to immediately follow any
41484d9c625SLionel Sambuc * subset of the 'delete' command, but then did not permit
41584d9c625SLionel Sambuc * further arguments (flag, buffer, count). Make it work.
41684d9c625SLionel Sambuc * Permit further arguments for the few shreds of dignity
41784d9c625SLionel Sambuc * it offers.
41884d9c625SLionel Sambuc *
41984d9c625SLionel Sambuc * Adding commands that start with 'd', and match "delete"
42084d9c625SLionel Sambuc * up to a l, p, +, - or # character can break this code.
42184d9c625SLionel Sambuc *
42284d9c625SLionel Sambuc * !!!
42384d9c625SLionel Sambuc * Capital letters beginning the command names ex, edit,
42484d9c625SLionel Sambuc * next, previous, tag and visual (in vi mode) indicate the
42584d9c625SLionel Sambuc * command should happen in a new screen.
42684d9c625SLionel Sambuc */
42784d9c625SLionel Sambuc switch (p[0]) {
42884d9c625SLionel Sambuc case 'd':
42984d9c625SLionel Sambuc for (s = p,
43084d9c625SLionel Sambuc n = cmds[C_DELETE].name; *s == *n; ++s, ++n);
43184d9c625SLionel Sambuc if (s[0] == 'l' || s[0] == 'p' || s[0] == '+' ||
43284d9c625SLionel Sambuc s[0] == '-' || s[0] == '^' || s[0] == '#') {
43384d9c625SLionel Sambuc len = (ecp->cp - p) - (s - p);
43484d9c625SLionel Sambuc ecp->cp -= len;
43584d9c625SLionel Sambuc ecp->clen += len;
43684d9c625SLionel Sambuc ecp->rcmd = cmds[C_DELETE];
43784d9c625SLionel Sambuc ecp->rcmd.syntax = "1bca1";
43884d9c625SLionel Sambuc ecp->cmd = &ecp->rcmd;
43984d9c625SLionel Sambuc goto skip_srch;
44084d9c625SLionel Sambuc }
44184d9c625SLionel Sambuc break;
44284d9c625SLionel Sambuc case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
44384d9c625SLionel Sambuc newscreen = 1;
44484d9c625SLionel Sambuc p[0] = TOLOWER(p[0]);
44584d9c625SLionel Sambuc break;
44684d9c625SLionel Sambuc }
44784d9c625SLionel Sambuc
44884d9c625SLionel Sambuc /*
44984d9c625SLionel Sambuc * Search the table for the command.
45084d9c625SLionel Sambuc *
45184d9c625SLionel Sambuc * !!!
45284d9c625SLionel Sambuc * Historic vi permitted the mark to immediately follow the
45384d9c625SLionel Sambuc * 'k' in the 'k' command. Make it work.
45484d9c625SLionel Sambuc *
45584d9c625SLionel Sambuc * !!!
45684d9c625SLionel Sambuc * Historic vi permitted any flag to follow the s command, e.g.
45784d9c625SLionel Sambuc * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
45884d9c625SLionel Sambuc * Since the following characters all have to be flags, i.e.
45984d9c625SLionel Sambuc * alphabetics, we can let the s command routine return errors
46084d9c625SLionel Sambuc * if it was some illegal command string. This code will break
46184d9c625SLionel Sambuc * if an "sg" or similar command is ever added. The substitute
46284d9c625SLionel Sambuc * code doesn't care if it's a "cgr" flag or a "#lp" flag that
46384d9c625SLionel Sambuc * follows the 's', but we limit the choices here to "cgr" so
46484d9c625SLionel Sambuc * that we get unknown command messages for wrong combinations.
46584d9c625SLionel Sambuc */
46684d9c625SLionel Sambuc if ((ecp->cmd = ex_comm_search(sp, p, namelen)) == NULL)
46784d9c625SLionel Sambuc switch (p[0]) {
46884d9c625SLionel Sambuc case 'k':
46984d9c625SLionel Sambuc if (namelen == 2) {
47084d9c625SLionel Sambuc ecp->cp -= namelen - 1;
47184d9c625SLionel Sambuc ecp->clen += namelen - 1;
47284d9c625SLionel Sambuc ecp->cmd = &cmds[C_K];
47384d9c625SLionel Sambuc break;
47484d9c625SLionel Sambuc }
47584d9c625SLionel Sambuc goto unknown;
47684d9c625SLionel Sambuc case 's':
47784d9c625SLionel Sambuc for (s = p + 1, cnt = namelen; --cnt; ++s)
47884d9c625SLionel Sambuc if (s[0] != 'c' &&
47984d9c625SLionel Sambuc s[0] != 'g' && s[0] != 'r')
48084d9c625SLionel Sambuc break;
48184d9c625SLionel Sambuc if (cnt == 0) {
48284d9c625SLionel Sambuc ecp->cp -= namelen - 1;
48384d9c625SLionel Sambuc ecp->clen += namelen - 1;
48484d9c625SLionel Sambuc ecp->rcmd = cmds[C_SUBSTITUTE];
48584d9c625SLionel Sambuc ecp->rcmd.fn = ex_subagain;
48684d9c625SLionel Sambuc ecp->cmd = &ecp->rcmd;
48784d9c625SLionel Sambuc break;
48884d9c625SLionel Sambuc }
48984d9c625SLionel Sambuc /* FALLTHROUGH */
49084d9c625SLionel Sambuc default:
49184d9c625SLionel Sambuc unknown: if (newscreen)
49284d9c625SLionel Sambuc p[0] = TOUPPER((UCHAR_T)p[0]);
49384d9c625SLionel Sambuc ex_unknown(sp, p, namelen);
49484d9c625SLionel Sambuc goto err;
49584d9c625SLionel Sambuc }
49684d9c625SLionel Sambuc
49784d9c625SLionel Sambuc /*
49884d9c625SLionel Sambuc * The visual command has a different syntax when called
49984d9c625SLionel Sambuc * from ex than when called from a vi colon command. FMH.
50084d9c625SLionel Sambuc * Make the change now, before we test for the newscreen
50184d9c625SLionel Sambuc * semantic, so that we're testing the right one.
50284d9c625SLionel Sambuc */
50384d9c625SLionel Sambuc skip_srch: if (ecp->cmd == &cmds[C_VISUAL_EX] && F_ISSET(sp, SC_VI))
50484d9c625SLionel Sambuc ecp->cmd = &cmds[C_VISUAL_VI];
50584d9c625SLionel Sambuc
50684d9c625SLionel Sambuc /*
50784d9c625SLionel Sambuc * !!!
50884d9c625SLionel Sambuc * Historic vi permitted a capital 'P' at the beginning of
50984d9c625SLionel Sambuc * any command that started with 'p'. Probably wanted the
51084d9c625SLionel Sambuc * P[rint] command for backward compatibility, and the code
51184d9c625SLionel Sambuc * just made Preserve and Put work by accident. Nvi uses
51284d9c625SLionel Sambuc * Previous to mean previous-in-a-new-screen, so be careful.
51384d9c625SLionel Sambuc */
51484d9c625SLionel Sambuc if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN) &&
51584d9c625SLionel Sambuc (ecp->cmd == &cmds[C_PRINT] ||
51684d9c625SLionel Sambuc ecp->cmd == &cmds[C_PRESERVE]))
51784d9c625SLionel Sambuc newscreen = 0;
51884d9c625SLionel Sambuc
51984d9c625SLionel Sambuc /* Test for a newscreen associated with this command. */
52084d9c625SLionel Sambuc if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN))
52184d9c625SLionel Sambuc goto unknown;
52284d9c625SLionel Sambuc
52384d9c625SLionel Sambuc /* Secure means no shell access. */
52484d9c625SLionel Sambuc if (F_ISSET(ecp->cmd, E_SECURE) && O_ISSET(sp, O_SECURE)) {
52584d9c625SLionel Sambuc ex_wemsg(sp, ecp->cmd->name, EXM_SECURE);
52684d9c625SLionel Sambuc goto err;
52784d9c625SLionel Sambuc }
52884d9c625SLionel Sambuc
52984d9c625SLionel Sambuc /*
53084d9c625SLionel Sambuc * Multiple < and > characters; another "feature". Note,
53184d9c625SLionel Sambuc * The string passed to the underlying function may not be
53284d9c625SLionel Sambuc * nul terminated in this case.
53384d9c625SLionel Sambuc */
53484d9c625SLionel Sambuc if ((ecp->cmd == &cmds[C_SHIFTL] && *p == '<') ||
53584d9c625SLionel Sambuc (ecp->cmd == &cmds[C_SHIFTR] && *p == '>')) {
53684d9c625SLionel Sambuc for (ch = (UCHAR_T)*p;
53784d9c625SLionel Sambuc ecp->clen > 0; --ecp->clen, ++ecp->cp)
53884d9c625SLionel Sambuc if (*ecp->cp != ch)
53984d9c625SLionel Sambuc break;
54084d9c625SLionel Sambuc if (argv_exp0(sp, ecp, p, ecp->cp - p))
54184d9c625SLionel Sambuc goto err;
54284d9c625SLionel Sambuc }
54384d9c625SLionel Sambuc
54484d9c625SLionel Sambuc /* Set the format style flags for the next command. */
54584d9c625SLionel Sambuc if (ecp->cmd == &cmds[C_HASH])
54684d9c625SLionel Sambuc exp->fdef = E_C_HASH;
54784d9c625SLionel Sambuc else if (ecp->cmd == &cmds[C_LIST])
54884d9c625SLionel Sambuc exp->fdef = E_C_LIST;
54984d9c625SLionel Sambuc else if (ecp->cmd == &cmds[C_PRINT])
55084d9c625SLionel Sambuc exp->fdef = E_C_PRINT;
55184d9c625SLionel Sambuc F_CLR(ecp, E_USELASTCMD);
55284d9c625SLionel Sambuc } else {
55384d9c625SLionel Sambuc /* Print is the default command. */
55484d9c625SLionel Sambuc ecp->cmd = &cmds[C_PRINT];
55584d9c625SLionel Sambuc
55684d9c625SLionel Sambuc /* Set the saved format flags. */
55784d9c625SLionel Sambuc F_SET(ecp, exp->fdef);
55884d9c625SLionel Sambuc
55984d9c625SLionel Sambuc /*
56084d9c625SLionel Sambuc * !!!
56184d9c625SLionel Sambuc * If no address was specified, and it's not a global command,
56284d9c625SLionel Sambuc * we up the address by one. (I have no idea why globals are
56384d9c625SLionel Sambuc * exempted, but it's (ahem) historic practice.)
56484d9c625SLionel Sambuc */
56584d9c625SLionel Sambuc if (ecp->addrcnt == 0 && !F_ISSET(sp, SC_EX_GLOBAL)) {
56684d9c625SLionel Sambuc ecp->addrcnt = 1;
56784d9c625SLionel Sambuc ecp->addr1.lno = sp->lno + 1;
56884d9c625SLionel Sambuc ecp->addr1.cno = sp->cno;
56984d9c625SLionel Sambuc }
57084d9c625SLionel Sambuc
57184d9c625SLionel Sambuc F_SET(ecp, E_USELASTCMD);
57284d9c625SLionel Sambuc }
57384d9c625SLionel Sambuc
57484d9c625SLionel Sambuc /*
57584d9c625SLionel Sambuc * !!!
57684d9c625SLionel Sambuc * Historically, the number option applied to both ex and vi. One
57784d9c625SLionel Sambuc * strangeness was that ex didn't switch display formats until a
57884d9c625SLionel Sambuc * command was entered, e.g. <CR>'s after the set didn't change to
57984d9c625SLionel Sambuc * the new format, but :1p would.
58084d9c625SLionel Sambuc */
58184d9c625SLionel Sambuc if (O_ISSET(sp, O_NUMBER)) {
58284d9c625SLionel Sambuc F_SET(ecp, E_OPTNUM);
58384d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_HASH);
58484d9c625SLionel Sambuc } else
58584d9c625SLionel Sambuc F_CLR(ecp, E_OPTNUM);
58684d9c625SLionel Sambuc
58784d9c625SLionel Sambuc /* Check for ex mode legality. */
58884d9c625SLionel Sambuc if (F_ISSET(sp, SC_EX) && (F_ISSET(ecp->cmd, E_VIONLY) || newscreen)) {
58984d9c625SLionel Sambuc msgq_wstr(sp, M_ERR, ecp->cmd->name,
59084d9c625SLionel Sambuc "082|%s: command not available in ex mode");
59184d9c625SLionel Sambuc goto err;
59284d9c625SLionel Sambuc }
59384d9c625SLionel Sambuc
59484d9c625SLionel Sambuc /* Add standard command flags. */
59584d9c625SLionel Sambuc F_SET(ecp, ecp->cmd->flags);
59684d9c625SLionel Sambuc if (!newscreen)
59784d9c625SLionel Sambuc F_CLR(ecp, E_NEWSCREEN);
59884d9c625SLionel Sambuc
59984d9c625SLionel Sambuc /*
60084d9c625SLionel Sambuc * There are three normal termination cases for an ex command. They
60184d9c625SLionel Sambuc * are the end of the string (ecp->clen), or unescaped (by <literal
60284d9c625SLionel Sambuc * next> characters) <newline> or '|' characters. As we're now past
60384d9c625SLionel Sambuc * possible addresses, we can determine how long the command is, so we
60484d9c625SLionel Sambuc * don't have to look for all the possible terminations. Naturally,
60584d9c625SLionel Sambuc * there are some exciting special cases:
60684d9c625SLionel Sambuc *
60784d9c625SLionel Sambuc * 1: The bang, global, v and the filter versions of the read and
60884d9c625SLionel Sambuc * write commands are delimited by <newline>s (they can contain
60984d9c625SLionel Sambuc * shell pipes).
61084d9c625SLionel Sambuc * 2: The ex, edit, next and visual in vi mode commands all take ex
61184d9c625SLionel Sambuc * commands as their first arguments.
61284d9c625SLionel Sambuc * 3: The s command takes an RE as its first argument, and wants it
61384d9c625SLionel Sambuc * to be specially delimited.
61484d9c625SLionel Sambuc *
61584d9c625SLionel Sambuc * Historically, '|' characters in the first argument of the ex, edit,
61684d9c625SLionel Sambuc * next, vi visual, and s commands didn't delimit the command. And,
61784d9c625SLionel Sambuc * in the filter cases for read and write, and the bang, global and v
61884d9c625SLionel Sambuc * commands, they did not delimit the command at all.
61984d9c625SLionel Sambuc *
62084d9c625SLionel Sambuc * For example, the following commands were legal:
62184d9c625SLionel Sambuc *
62284d9c625SLionel Sambuc * :edit +25|s/abc/ABC/ file.c
62384d9c625SLionel Sambuc * :s/|/PIPE/
62484d9c625SLionel Sambuc * :read !spell % | columnate
62584d9c625SLionel Sambuc * :global/pattern/p|l
62684d9c625SLionel Sambuc *
62784d9c625SLionel Sambuc * It's not quite as simple as it sounds, however. The command:
62884d9c625SLionel Sambuc *
62984d9c625SLionel Sambuc * :s/a/b/|s/c/d|set
63084d9c625SLionel Sambuc *
63184d9c625SLionel Sambuc * was also legal, i.e. the historic ex parser (using the word loosely,
63284d9c625SLionel Sambuc * since "parser" implies some regularity of syntax) delimited the RE's
63384d9c625SLionel Sambuc * based on its delimiter and not anything so irretrievably vulgar as a
63484d9c625SLionel Sambuc * command syntax.
63584d9c625SLionel Sambuc *
63684d9c625SLionel Sambuc * Anyhow, the following code makes this all work. First, for the
63784d9c625SLionel Sambuc * special cases we move past their special argument(s). Then, we
63884d9c625SLionel Sambuc * do normal command processing on whatever is left. Barf-O-Rama.
63984d9c625SLionel Sambuc */
64084d9c625SLionel Sambuc discard = 0; /* Characters discarded from the command. */
64184d9c625SLionel Sambuc arg1_len = 0;
64284d9c625SLionel Sambuc ecp->save_cmd = ecp->cp;
64384d9c625SLionel Sambuc if (ecp->cmd == &cmds[C_EDIT] || ecp->cmd == &cmds[C_EX] ||
64484d9c625SLionel Sambuc ecp->cmd == &cmds[C_NEXT] || ecp->cmd == &cmds[C_VISUAL_VI] ||
64584d9c625SLionel Sambuc ecp->cmd == &cmds[C_VSPLIT]) {
64684d9c625SLionel Sambuc /*
64784d9c625SLionel Sambuc * Move to the next non-whitespace character. A '!'
64884d9c625SLionel Sambuc * immediately following the command is eaten as a
64984d9c625SLionel Sambuc * force flag.
65084d9c625SLionel Sambuc */
65184d9c625SLionel Sambuc if (ecp->clen > 0 && *ecp->cp == '!') {
65284d9c625SLionel Sambuc ++ecp->cp;
65384d9c625SLionel Sambuc --ecp->clen;
65484d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_FORCE);
65584d9c625SLionel Sambuc
65684d9c625SLionel Sambuc /* Reset, don't reparse. */
65784d9c625SLionel Sambuc ecp->save_cmd = ecp->cp;
65884d9c625SLionel Sambuc }
65984d9c625SLionel Sambuc for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
66084d9c625SLionel Sambuc if (!ISBLANK(*ecp->cp))
66184d9c625SLionel Sambuc break;
66284d9c625SLionel Sambuc /*
66384d9c625SLionel Sambuc * QUOTING NOTE:
66484d9c625SLionel Sambuc *
66584d9c625SLionel Sambuc * The historic implementation ignored all escape characters
66684d9c625SLionel Sambuc * so there was no way to put a space or newline into the +cmd
66784d9c625SLionel Sambuc * field. We do a simplistic job of fixing it by moving to the
66884d9c625SLionel Sambuc * first whitespace character that isn't escaped. The escaping
66984d9c625SLionel Sambuc * characters are stripped as no longer useful.
67084d9c625SLionel Sambuc */
67184d9c625SLionel Sambuc if (ecp->clen > 0 && *ecp->cp == '+') {
67284d9c625SLionel Sambuc ++ecp->cp;
67384d9c625SLionel Sambuc --ecp->clen;
67484d9c625SLionel Sambuc for (arg1 = p = ecp->cp;
67584d9c625SLionel Sambuc ecp->clen > 0; --ecp->clen, ++ecp->cp) {
67684d9c625SLionel Sambuc ch = (UCHAR_T)*ecp->cp;
67784d9c625SLionel Sambuc if (IS_ESCAPE(sp, ecp, ch) &&
67884d9c625SLionel Sambuc ecp->clen > 1) {
67984d9c625SLionel Sambuc ++discard;
68084d9c625SLionel Sambuc --ecp->clen;
68184d9c625SLionel Sambuc ch = (UCHAR_T)*++ecp->cp;
68284d9c625SLionel Sambuc } else if (ISBLANK(ch))
68384d9c625SLionel Sambuc break;
68484d9c625SLionel Sambuc *p++ = ch;
68584d9c625SLionel Sambuc }
68684d9c625SLionel Sambuc arg1_len = ecp->cp - arg1;
68784d9c625SLionel Sambuc
68884d9c625SLionel Sambuc /* Reset, so the first argument isn't reparsed. */
68984d9c625SLionel Sambuc ecp->save_cmd = ecp->cp;
69084d9c625SLionel Sambuc }
69184d9c625SLionel Sambuc } else if (ecp->cmd == &cmds[C_BANG] ||
69284d9c625SLionel Sambuc ecp->cmd == &cmds[C_GLOBAL] || ecp->cmd == &cmds[C_V]) {
69384d9c625SLionel Sambuc /*
69484d9c625SLionel Sambuc * QUOTING NOTE:
69584d9c625SLionel Sambuc *
69684d9c625SLionel Sambuc * We use backslashes to escape <newline> characters, although
69784d9c625SLionel Sambuc * this wasn't historic practice for the bang command. It was
69884d9c625SLionel Sambuc * for the global and v commands, and it's common usage when
69984d9c625SLionel Sambuc * doing text insert during the command. Escaping characters
70084d9c625SLionel Sambuc * are stripped as no longer useful.
70184d9c625SLionel Sambuc */
70284d9c625SLionel Sambuc for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
70384d9c625SLionel Sambuc ch = (UCHAR_T)*ecp->cp;
70484d9c625SLionel Sambuc if (ch == '\\' && ecp->clen > 1 && ecp->cp[1] == '\n') {
70584d9c625SLionel Sambuc ++discard;
70684d9c625SLionel Sambuc --ecp->clen;
70784d9c625SLionel Sambuc ch = (UCHAR_T)*++ecp->cp;
70884d9c625SLionel Sambuc
70984d9c625SLionel Sambuc ++wp->if_lno;
71084d9c625SLionel Sambuc ++ecp->if_lno;
71184d9c625SLionel Sambuc } else if (ch == '\n')
71284d9c625SLionel Sambuc break;
71384d9c625SLionel Sambuc *p++ = ch;
71484d9c625SLionel Sambuc }
71584d9c625SLionel Sambuc } else if (ecp->cmd == &cmds[C_READ] || ecp->cmd == &cmds[C_WRITE]) {
71684d9c625SLionel Sambuc /*
71784d9c625SLionel Sambuc * For write commands, if the next character is a <blank>, and
71884d9c625SLionel Sambuc * the next non-blank character is a '!', it's a filter command
71984d9c625SLionel Sambuc * and we want to eat everything up to the <newline>. For read
72084d9c625SLionel Sambuc * commands, if the next non-blank character is a '!', it's a
72184d9c625SLionel Sambuc * filter command and we want to eat everything up to the next
72284d9c625SLionel Sambuc * <newline>. Otherwise, we're done.
72384d9c625SLionel Sambuc */
72484d9c625SLionel Sambuc for (tmp = 0; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
72584d9c625SLionel Sambuc ch = (UCHAR_T)*ecp->cp;
72684d9c625SLionel Sambuc if (ISBLANK(ch))
72784d9c625SLionel Sambuc tmp = 1;
72884d9c625SLionel Sambuc else
72984d9c625SLionel Sambuc break;
73084d9c625SLionel Sambuc }
73184d9c625SLionel Sambuc if (ecp->clen > 0 && ch == '!' &&
73284d9c625SLionel Sambuc (ecp->cmd == &cmds[C_READ] || tmp))
73384d9c625SLionel Sambuc for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
73484d9c625SLionel Sambuc if (ecp->cp[0] == '\n')
73584d9c625SLionel Sambuc break;
73684d9c625SLionel Sambuc } else if (ecp->cmd == &cmds[C_SUBSTITUTE]) {
73784d9c625SLionel Sambuc /*
73884d9c625SLionel Sambuc * Move to the next non-whitespace character, we'll use it as
73984d9c625SLionel Sambuc * the delimiter. If the character isn't an alphanumeric or
74084d9c625SLionel Sambuc * a '|', it's the delimiter, so parse it. Otherwise, we're
74184d9c625SLionel Sambuc * into something like ":s g", so use the special s command.
74284d9c625SLionel Sambuc */
74384d9c625SLionel Sambuc for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
74484d9c625SLionel Sambuc if (!ISBLANK(ecp->cp[0]))
74584d9c625SLionel Sambuc break;
74684d9c625SLionel Sambuc
74784d9c625SLionel Sambuc if (ISALNUM((UCHAR_T)ecp->cp[0]) || ecp->cp[0] == '|') {
74884d9c625SLionel Sambuc ecp->rcmd = cmds[C_SUBSTITUTE];
74984d9c625SLionel Sambuc ecp->rcmd.fn = ex_subagain;
75084d9c625SLionel Sambuc ecp->cmd = &ecp->rcmd;
75184d9c625SLionel Sambuc } else if (ecp->clen > 0) {
75284d9c625SLionel Sambuc /*
75384d9c625SLionel Sambuc * QUOTING NOTE:
75484d9c625SLionel Sambuc *
75584d9c625SLionel Sambuc * Backslashes quote delimiter characters for RE's.
75684d9c625SLionel Sambuc * The backslashes are NOT removed since they'll be
75784d9c625SLionel Sambuc * used by the RE code. Move to the third delimiter
75884d9c625SLionel Sambuc * that's not escaped (or the end of the command).
75984d9c625SLionel Sambuc */
76084d9c625SLionel Sambuc delim = *ecp->cp;
76184d9c625SLionel Sambuc ++ecp->cp;
76284d9c625SLionel Sambuc --ecp->clen;
76384d9c625SLionel Sambuc for (cnt = 2; ecp->clen > 0 &&
76484d9c625SLionel Sambuc cnt != 0; --ecp->clen, ++ecp->cp)
76584d9c625SLionel Sambuc if (ecp->cp[0] == '\\' &&
76684d9c625SLionel Sambuc ecp->clen > 1) {
76784d9c625SLionel Sambuc ++ecp->cp;
76884d9c625SLionel Sambuc --ecp->clen;
76984d9c625SLionel Sambuc } else if (ecp->cp[0] == delim)
77084d9c625SLionel Sambuc --cnt;
77184d9c625SLionel Sambuc }
77284d9c625SLionel Sambuc }
77384d9c625SLionel Sambuc
77484d9c625SLionel Sambuc /*
77584d9c625SLionel Sambuc * Use normal quoting and termination rules to find the end of this
77684d9c625SLionel Sambuc * command.
77784d9c625SLionel Sambuc *
77884d9c625SLionel Sambuc * QUOTING NOTE:
77984d9c625SLionel Sambuc *
78084d9c625SLionel Sambuc * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
78184d9c625SLionel Sambuc * file. It was almost certainly a bug, but that's what bug-for-bug
78284d9c625SLionel Sambuc * compatibility means, Grasshopper. Also, ^V's escape the command
78384d9c625SLionel Sambuc * delimiters. Literal next quote characters in front of the newlines,
78484d9c625SLionel Sambuc * '|' characters or literal next characters are stripped as they're
78584d9c625SLionel Sambuc * no longer useful.
78684d9c625SLionel Sambuc */
78784d9c625SLionel Sambuc vi_address = ecp->clen != 0 && ecp->cp[0] != '\n';
78884d9c625SLionel Sambuc for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
78984d9c625SLionel Sambuc ch = (UCHAR_T)ecp->cp[0];
79084d9c625SLionel Sambuc if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
79184d9c625SLionel Sambuc ARG_CHAR_T tmp1 = (UCHAR_T)ecp->cp[1];
79284d9c625SLionel Sambuc if (tmp1 == '\n' || tmp1 == '|') {
79384d9c625SLionel Sambuc if (tmp1 == '\n') {
79484d9c625SLionel Sambuc ++wp->if_lno;
79584d9c625SLionel Sambuc ++ecp->if_lno;
79684d9c625SLionel Sambuc }
79784d9c625SLionel Sambuc ++discard;
79884d9c625SLionel Sambuc --ecp->clen;
79984d9c625SLionel Sambuc ++ecp->cp;
80084d9c625SLionel Sambuc ch = tmp1;
80184d9c625SLionel Sambuc }
80284d9c625SLionel Sambuc } else if (ch == '\n' || ch == '|') {
80384d9c625SLionel Sambuc if (ch == '\n')
80484d9c625SLionel Sambuc F_SET(ecp, E_NEWLINE);
80584d9c625SLionel Sambuc --ecp->clen;
80684d9c625SLionel Sambuc break;
80784d9c625SLionel Sambuc }
80884d9c625SLionel Sambuc *p++ = ch;
80984d9c625SLionel Sambuc }
81084d9c625SLionel Sambuc
81184d9c625SLionel Sambuc /*
81284d9c625SLionel Sambuc * Save off the next command information, go back to the
81384d9c625SLionel Sambuc * original start of the command.
81484d9c625SLionel Sambuc */
81584d9c625SLionel Sambuc p = ecp->cp + 1;
81684d9c625SLionel Sambuc ecp->cp = ecp->save_cmd;
81784d9c625SLionel Sambuc ecp->save_cmd = p;
81884d9c625SLionel Sambuc ecp->save_cmdlen = ecp->clen;
81984d9c625SLionel Sambuc ecp->clen = ((ecp->save_cmd - ecp->cp) - 1) - discard;
82084d9c625SLionel Sambuc
82184d9c625SLionel Sambuc /*
82284d9c625SLionel Sambuc * QUOTING NOTE:
82384d9c625SLionel Sambuc *
82484d9c625SLionel Sambuc * The "set tags" command historically used a backslash, not the
82584d9c625SLionel Sambuc * user's literal next character, to escape whitespace. Handle
82684d9c625SLionel Sambuc * it here instead of complicating the argv_exp3() code. Note,
82784d9c625SLionel Sambuc * this isn't a particularly complex trap, and if backslashes were
82884d9c625SLionel Sambuc * legal in set commands, this would have to be much more complicated.
82984d9c625SLionel Sambuc */
83084d9c625SLionel Sambuc if (ecp->cmd == &cmds[C_SET])
83184d9c625SLionel Sambuc for (p = ecp->cp, len = ecp->clen; len > 0; --len, ++p)
83284d9c625SLionel Sambuc if (*p == '\\')
83384d9c625SLionel Sambuc *p = CH_LITERAL;
83484d9c625SLionel Sambuc
83584d9c625SLionel Sambuc /*
83684d9c625SLionel Sambuc * Set the default addresses. It's an error to specify an address for
83784d9c625SLionel Sambuc * a command that doesn't take them. If two addresses are specified
83884d9c625SLionel Sambuc * for a command that only takes one, lose the first one. Two special
83984d9c625SLionel Sambuc * cases here, some commands take 0 or 2 addresses. For most of them
84084d9c625SLionel Sambuc * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
84184d9c625SLionel Sambuc * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
84284d9c625SLionel Sambuc *
84384d9c625SLionel Sambuc * Also, if the file is empty, some commands want to use an address of
84484d9c625SLionel Sambuc * 0, i.e. the entire file is 0 to 0, and the default first address is
84584d9c625SLionel Sambuc * 0. Otherwise, an entire file is 1 to N and the default line is 1.
84684d9c625SLionel Sambuc * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
84784d9c625SLionel Sambuc * case where the 0 address is only valid if it's a default address.
84884d9c625SLionel Sambuc *
84984d9c625SLionel Sambuc * Also, set a flag if we set the default addresses. Some commands
85084d9c625SLionel Sambuc * (ex: z) care if the user specified an address or if we just used
85184d9c625SLionel Sambuc * the current cursor.
85284d9c625SLionel Sambuc */
85384d9c625SLionel Sambuc switch (F_ISSET(ecp, E_ADDR1 | E_ADDR2 | E_ADDR2_ALL | E_ADDR2_NONE)) {
85484d9c625SLionel Sambuc case E_ADDR1: /* One address: */
85584d9c625SLionel Sambuc switch (ecp->addrcnt) {
85684d9c625SLionel Sambuc case 0: /* Default cursor/empty file. */
85784d9c625SLionel Sambuc ecp->addrcnt = 1;
85884d9c625SLionel Sambuc F_SET(ecp, E_ADDR_DEF);
85984d9c625SLionel Sambuc if (F_ISSET(ecp, E_ADDR_ZERODEF)) {
86084d9c625SLionel Sambuc if (db_last(sp, &lno))
86184d9c625SLionel Sambuc goto err;
86284d9c625SLionel Sambuc if (lno == 0) {
86384d9c625SLionel Sambuc ecp->addr1.lno = 0;
86484d9c625SLionel Sambuc F_SET(ecp, E_ADDR_ZERO);
86584d9c625SLionel Sambuc } else
86684d9c625SLionel Sambuc ecp->addr1.lno = sp->lno;
86784d9c625SLionel Sambuc } else
86884d9c625SLionel Sambuc ecp->addr1.lno = sp->lno;
86984d9c625SLionel Sambuc ecp->addr1.cno = sp->cno;
87084d9c625SLionel Sambuc break;
87184d9c625SLionel Sambuc case 1:
87284d9c625SLionel Sambuc break;
87384d9c625SLionel Sambuc case 2: /* Lose the first address. */
87484d9c625SLionel Sambuc ecp->addrcnt = 1;
87584d9c625SLionel Sambuc ecp->addr1 = ecp->addr2;
87684d9c625SLionel Sambuc }
87784d9c625SLionel Sambuc break;
87884d9c625SLionel Sambuc case E_ADDR2_NONE: /* Zero/two addresses: */
87984d9c625SLionel Sambuc if (ecp->addrcnt == 0) /* Default to nothing. */
88084d9c625SLionel Sambuc break;
88184d9c625SLionel Sambuc goto two_addr;
88284d9c625SLionel Sambuc case E_ADDR2_ALL: /* Zero/two addresses: */
88384d9c625SLionel Sambuc if (ecp->addrcnt == 0) { /* Default entire/empty file. */
88484d9c625SLionel Sambuc F_SET(ecp, E_ADDR_DEF);
88584d9c625SLionel Sambuc ecp->addrcnt = 2;
88684d9c625SLionel Sambuc if (sp->ep == NULL)
88784d9c625SLionel Sambuc ecp->addr2.lno = 0;
88884d9c625SLionel Sambuc else if (db_last(sp, &ecp->addr2.lno))
88984d9c625SLionel Sambuc goto err;
89084d9c625SLionel Sambuc if (F_ISSET(ecp, E_ADDR_ZERODEF) &&
89184d9c625SLionel Sambuc ecp->addr2.lno == 0) {
89284d9c625SLionel Sambuc ecp->addr1.lno = 0;
89384d9c625SLionel Sambuc F_SET(ecp, E_ADDR_ZERO);
89484d9c625SLionel Sambuc } else
89584d9c625SLionel Sambuc ecp->addr1.lno = 1;
89684d9c625SLionel Sambuc ecp->addr1.cno = ecp->addr2.cno = 0;
89784d9c625SLionel Sambuc F_SET(ecp, E_ADDR2_ALL);
89884d9c625SLionel Sambuc break;
89984d9c625SLionel Sambuc }
90084d9c625SLionel Sambuc /* FALLTHROUGH */
90184d9c625SLionel Sambuc case E_ADDR2: /* Two addresses: */
90284d9c625SLionel Sambuc two_addr: switch (ecp->addrcnt) {
90384d9c625SLionel Sambuc case 0: /* Default cursor/empty file. */
90484d9c625SLionel Sambuc ecp->addrcnt = 2;
90584d9c625SLionel Sambuc F_SET(ecp, E_ADDR_DEF);
90684d9c625SLionel Sambuc if (sp->lno == 1 &&
90784d9c625SLionel Sambuc F_ISSET(ecp, E_ADDR_ZERODEF)) {
90884d9c625SLionel Sambuc if (db_last(sp, &lno))
90984d9c625SLionel Sambuc goto err;
91084d9c625SLionel Sambuc if (lno == 0) {
91184d9c625SLionel Sambuc ecp->addr1.lno = ecp->addr2.lno = 0;
91284d9c625SLionel Sambuc F_SET(ecp, E_ADDR_ZERO);
91384d9c625SLionel Sambuc } else
91484d9c625SLionel Sambuc ecp->addr1.lno =
91584d9c625SLionel Sambuc ecp->addr2.lno = sp->lno;
91684d9c625SLionel Sambuc } else
91784d9c625SLionel Sambuc ecp->addr1.lno = ecp->addr2.lno = sp->lno;
91884d9c625SLionel Sambuc ecp->addr1.cno = ecp->addr2.cno = sp->cno;
91984d9c625SLionel Sambuc break;
92084d9c625SLionel Sambuc case 1: /* Default to first address. */
92184d9c625SLionel Sambuc //ecp->addrcnt = 2; /* XXX Was this needed ??? */
92284d9c625SLionel Sambuc ecp->addr2 = ecp->addr1;
92384d9c625SLionel Sambuc break;
92484d9c625SLionel Sambuc case 2:
92584d9c625SLionel Sambuc break;
92684d9c625SLionel Sambuc }
92784d9c625SLionel Sambuc break;
92884d9c625SLionel Sambuc default:
92984d9c625SLionel Sambuc if (ecp->addrcnt) /* Error. */
93084d9c625SLionel Sambuc goto usage;
93184d9c625SLionel Sambuc }
93284d9c625SLionel Sambuc
93384d9c625SLionel Sambuc /*
93484d9c625SLionel Sambuc * !!!
93584d9c625SLionel Sambuc * The ^D scroll command historically scrolled the value of the scroll
93684d9c625SLionel Sambuc * option or to EOF. It was an error if the cursor was already at EOF.
93784d9c625SLionel Sambuc * (Leading addresses were permitted, but were then ignored.)
93884d9c625SLionel Sambuc */
93984d9c625SLionel Sambuc if (ecp->cmd == &cmds[C_SCROLL]) {
94084d9c625SLionel Sambuc ecp->addrcnt = 2;
94184d9c625SLionel Sambuc ecp->addr1.lno = sp->lno + 1;
94284d9c625SLionel Sambuc ecp->addr2.lno = sp->lno + O_VAL(sp, O_SCROLL);
94384d9c625SLionel Sambuc ecp->addr1.cno = ecp->addr2.cno = sp->cno;
94484d9c625SLionel Sambuc if (db_last(sp, &lno))
94584d9c625SLionel Sambuc goto err;
94684d9c625SLionel Sambuc if (lno != 0 && lno > sp->lno && ecp->addr2.lno > lno)
94784d9c625SLionel Sambuc ecp->addr2.lno = lno;
94884d9c625SLionel Sambuc }
94984d9c625SLionel Sambuc
95084d9c625SLionel Sambuc ecp->flagoff = 0;
95184d9c625SLionel Sambuc for (np = ecp->cmd->syntax; *np != '\0'; ++np) {
95284d9c625SLionel Sambuc /*
95384d9c625SLionel Sambuc * The force flag is sensitive to leading whitespace, i.e.
95484d9c625SLionel Sambuc * "next !" is different from "next!". Handle it before
95584d9c625SLionel Sambuc * skipping leading <blank>s.
95684d9c625SLionel Sambuc */
95784d9c625SLionel Sambuc if (*np == '!') {
95884d9c625SLionel Sambuc if (ecp->clen > 0 && *ecp->cp == '!') {
95984d9c625SLionel Sambuc ++ecp->cp;
96084d9c625SLionel Sambuc --ecp->clen;
96184d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_FORCE);
96284d9c625SLionel Sambuc }
96384d9c625SLionel Sambuc continue;
96484d9c625SLionel Sambuc }
96584d9c625SLionel Sambuc
96684d9c625SLionel Sambuc /* Skip leading <blank>s. */
96784d9c625SLionel Sambuc for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
96884d9c625SLionel Sambuc if (!ISBLANK(*ecp->cp))
96984d9c625SLionel Sambuc break;
97084d9c625SLionel Sambuc if (ecp->clen == 0)
97184d9c625SLionel Sambuc break;
97284d9c625SLionel Sambuc
97384d9c625SLionel Sambuc switch (*np) {
97484d9c625SLionel Sambuc case '1': /* +, -, #, l, p */
97584d9c625SLionel Sambuc /*
97684d9c625SLionel Sambuc * !!!
97784d9c625SLionel Sambuc * Historically, some flags were ignored depending
97884d9c625SLionel Sambuc * on where they occurred in the command line. For
97984d9c625SLionel Sambuc * example, in the command, ":3+++p--#", historic vi
98084d9c625SLionel Sambuc * acted on the '#' flag, but ignored the '-' flags.
98184d9c625SLionel Sambuc * It's unambiguous what the flags mean, so we just
98284d9c625SLionel Sambuc * handle them regardless of the stupidity of their
98384d9c625SLionel Sambuc * location.
98484d9c625SLionel Sambuc */
98584d9c625SLionel Sambuc for (; ecp->clen; --ecp->clen, ++ecp->cp)
98684d9c625SLionel Sambuc switch (*ecp->cp) {
98784d9c625SLionel Sambuc case '+':
98884d9c625SLionel Sambuc ++ecp->flagoff;
98984d9c625SLionel Sambuc break;
99084d9c625SLionel Sambuc case '-':
99184d9c625SLionel Sambuc case '^':
99284d9c625SLionel Sambuc --ecp->flagoff;
99384d9c625SLionel Sambuc break;
99484d9c625SLionel Sambuc case '#':
99584d9c625SLionel Sambuc F_CLR(ecp, E_OPTNUM);
99684d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_HASH);
99784d9c625SLionel Sambuc exp->fdef |= E_C_HASH;
99884d9c625SLionel Sambuc break;
99984d9c625SLionel Sambuc case 'l':
100084d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_LIST);
100184d9c625SLionel Sambuc exp->fdef |= E_C_LIST;
100284d9c625SLionel Sambuc break;
100384d9c625SLionel Sambuc case 'p':
100484d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_PRINT);
100584d9c625SLionel Sambuc exp->fdef |= E_C_PRINT;
100684d9c625SLionel Sambuc break;
100784d9c625SLionel Sambuc default:
100884d9c625SLionel Sambuc goto end_case1;
100984d9c625SLionel Sambuc }
101084d9c625SLionel Sambuc end_case1: break;
101184d9c625SLionel Sambuc case '2': /* -, ., +, ^ */
101284d9c625SLionel Sambuc case '3': /* -, ., +, ^, = */
101384d9c625SLionel Sambuc for (; ecp->clen; --ecp->clen, ++ecp->cp)
101484d9c625SLionel Sambuc switch (*ecp->cp) {
101584d9c625SLionel Sambuc case '-':
101684d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_DASH);
101784d9c625SLionel Sambuc break;
101884d9c625SLionel Sambuc case '.':
101984d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_DOT);
102084d9c625SLionel Sambuc break;
102184d9c625SLionel Sambuc case '+':
102284d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_PLUS);
102384d9c625SLionel Sambuc break;
102484d9c625SLionel Sambuc case '^':
102584d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_CARAT);
102684d9c625SLionel Sambuc break;
102784d9c625SLionel Sambuc case '=':
102884d9c625SLionel Sambuc if (*np == '3') {
102984d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_EQUAL);
103084d9c625SLionel Sambuc break;
103184d9c625SLionel Sambuc }
103284d9c625SLionel Sambuc /* FALLTHROUGH */
103384d9c625SLionel Sambuc default:
103484d9c625SLionel Sambuc goto end_case23;
103584d9c625SLionel Sambuc }
103684d9c625SLionel Sambuc end_case23: break;
103784d9c625SLionel Sambuc case 'b': /* buffer */
103884d9c625SLionel Sambuc /*
103984d9c625SLionel Sambuc * !!!
104084d9c625SLionel Sambuc * Historically, "d #" was a delete with a flag, not a
104184d9c625SLionel Sambuc * delete into the '#' buffer. If the current command
104284d9c625SLionel Sambuc * permits a flag, don't use one as a buffer. However,
104384d9c625SLionel Sambuc * the 'l' and 'p' flags were legal buffer names in the
104484d9c625SLionel Sambuc * historic ex, and were used as buffers, not flags.
104584d9c625SLionel Sambuc */
104684d9c625SLionel Sambuc if ((ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
104784d9c625SLionel Sambuc ecp->cp[0] == '^' || ecp->cp[0] == '#') &&
104884d9c625SLionel Sambuc strchr(np, '1') != NULL)
104984d9c625SLionel Sambuc break;
105084d9c625SLionel Sambuc /*
105184d9c625SLionel Sambuc * !!!
105284d9c625SLionel Sambuc * Digits can't be buffer names in ex commands, or the
105384d9c625SLionel Sambuc * command "d2" would be a delete into buffer '2', and
105484d9c625SLionel Sambuc * not a two-line deletion.
105584d9c625SLionel Sambuc */
105684d9c625SLionel Sambuc if (!ISDIGIT((UCHAR_T)ecp->cp[0])) {
105784d9c625SLionel Sambuc ecp->buffer = (UCHAR_T)*ecp->cp;
105884d9c625SLionel Sambuc ++ecp->cp;
105984d9c625SLionel Sambuc --ecp->clen;
106084d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_BUFFER);
106184d9c625SLionel Sambuc }
106284d9c625SLionel Sambuc break;
106384d9c625SLionel Sambuc case 'c': /* count [01+a] */
106484d9c625SLionel Sambuc ++np;
106584d9c625SLionel Sambuc /* Validate any signed value. */
106684d9c625SLionel Sambuc if (!ISDIGIT((UCHAR_T)*ecp->cp) && (*np != '+' ||
106784d9c625SLionel Sambuc (*ecp->cp != '+' && *ecp->cp != '-')))
106884d9c625SLionel Sambuc break;
106984d9c625SLionel Sambuc /* If a signed value, set appropriate flags. */
107084d9c625SLionel Sambuc if (*ecp->cp == '-')
107184d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_COUNT_NEG);
107284d9c625SLionel Sambuc else if (*ecp->cp == '+')
107384d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_COUNT_POS);
107484d9c625SLionel Sambuc if ((nret =
107584d9c625SLionel Sambuc nget_slong(sp, <mp, ecp->cp, &t, 10)) != NUM_OK) {
107684d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_NOTSET, nret);
107784d9c625SLionel Sambuc goto err;
107884d9c625SLionel Sambuc }
107984d9c625SLionel Sambuc if (ltmp == 0 && *np != '0') {
108084d9c625SLionel Sambuc msgq(sp, M_ERR, "083|Count may not be zero");
108184d9c625SLionel Sambuc goto err;
108284d9c625SLionel Sambuc }
108384d9c625SLionel Sambuc ecp->clen -= (t - ecp->cp);
108484d9c625SLionel Sambuc ecp->cp = t;
108584d9c625SLionel Sambuc
108684d9c625SLionel Sambuc /*
108784d9c625SLionel Sambuc * Counts as address offsets occur in commands taking
108884d9c625SLionel Sambuc * two addresses. Historic vi practice was to use
108984d9c625SLionel Sambuc * the count as an offset from the *second* address.
109084d9c625SLionel Sambuc *
109184d9c625SLionel Sambuc * Set a count flag; some underlying commands (see
109284d9c625SLionel Sambuc * join) do different things with counts than with
109384d9c625SLionel Sambuc * line addresses.
109484d9c625SLionel Sambuc */
109584d9c625SLionel Sambuc if (*np == 'a') {
109684d9c625SLionel Sambuc ecp->addr1 = ecp->addr2;
109784d9c625SLionel Sambuc ecp->addr2.lno = ecp->addr1.lno + ltmp - 1;
109884d9c625SLionel Sambuc } else
109984d9c625SLionel Sambuc ecp->count = ltmp;
110084d9c625SLionel Sambuc FL_SET(ecp->iflags, E_C_COUNT);
110184d9c625SLionel Sambuc break;
110284d9c625SLionel Sambuc case 'f': /* file */
110384d9c625SLionel Sambuc if (argv_exp2(sp, ecp, ecp->cp, ecp->clen))
110484d9c625SLionel Sambuc goto err;
110584d9c625SLionel Sambuc goto arg_cnt_chk;
110684d9c625SLionel Sambuc case 'l': /* line */
110784d9c625SLionel Sambuc /*
110884d9c625SLionel Sambuc * Get a line specification.
110984d9c625SLionel Sambuc *
111084d9c625SLionel Sambuc * If the line was a search expression, we may have
111184d9c625SLionel Sambuc * changed state during the call, and we're now
111284d9c625SLionel Sambuc * searching the file. Push ourselves onto the state
111384d9c625SLionel Sambuc * stack.
111484d9c625SLionel Sambuc */
111584d9c625SLionel Sambuc if (ex_line(sp, ecp, &cur, &isaddr, &tmp))
111684d9c625SLionel Sambuc goto rfail;
111784d9c625SLionel Sambuc if (tmp)
111884d9c625SLionel Sambuc goto err;
111984d9c625SLionel Sambuc
112084d9c625SLionel Sambuc /* Line specifications are always required. */
112184d9c625SLionel Sambuc if (!isaddr) {
112284d9c625SLionel Sambuc msgq_wstr(sp, M_ERR, ecp->cp,
112384d9c625SLionel Sambuc "084|%s: bad line specification");
112484d9c625SLionel Sambuc goto err;
112584d9c625SLionel Sambuc }
112684d9c625SLionel Sambuc /*
112784d9c625SLionel Sambuc * The target line should exist for these commands,
112884d9c625SLionel Sambuc * but 0 is legal for them as well.
112984d9c625SLionel Sambuc */
113084d9c625SLionel Sambuc if (cur.lno != 0 && !db_exist(sp, cur.lno)) {
113184d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_EOF, NUM_OK);
113284d9c625SLionel Sambuc goto err;
113384d9c625SLionel Sambuc }
113484d9c625SLionel Sambuc ecp->lineno = cur.lno;
113584d9c625SLionel Sambuc break;
113684d9c625SLionel Sambuc case 'S': /* string, file exp. */
113784d9c625SLionel Sambuc if (ecp->clen != 0) {
113884d9c625SLionel Sambuc if (argv_exp1(sp, ecp, ecp->cp,
113984d9c625SLionel Sambuc ecp->clen, ecp->cmd == &cmds[C_BANG]))
114084d9c625SLionel Sambuc goto err;
114184d9c625SLionel Sambuc goto addr_verify;
114284d9c625SLionel Sambuc }
114384d9c625SLionel Sambuc /* FALLTHROUGH */
114484d9c625SLionel Sambuc case 's': /* string */
114584d9c625SLionel Sambuc if (argv_exp0(sp, ecp, ecp->cp, ecp->clen))
114684d9c625SLionel Sambuc goto err;
114784d9c625SLionel Sambuc goto addr_verify;
114884d9c625SLionel Sambuc case 'W': /* word string */
114984d9c625SLionel Sambuc /*
115084d9c625SLionel Sambuc * QUOTING NOTE:
115184d9c625SLionel Sambuc *
115284d9c625SLionel Sambuc * Literal next characters escape the following
115384d9c625SLionel Sambuc * character. Quoting characters are stripped here
115484d9c625SLionel Sambuc * since they are no longer useful.
115584d9c625SLionel Sambuc *
115684d9c625SLionel Sambuc * First there was the word.
115784d9c625SLionel Sambuc */
115884d9c625SLionel Sambuc for (p = t = ecp->cp;
115984d9c625SLionel Sambuc ecp->clen > 0; --ecp->clen, ++ecp->cp) {
116084d9c625SLionel Sambuc ch = (UCHAR_T)*ecp->cp;
116184d9c625SLionel Sambuc if (IS_ESCAPE(sp,
116284d9c625SLionel Sambuc ecp, ch) && ecp->clen > 1) {
116384d9c625SLionel Sambuc --ecp->clen;
116484d9c625SLionel Sambuc *p++ = *++ecp->cp;
116584d9c625SLionel Sambuc } else if (ISBLANK(ch)) {
116684d9c625SLionel Sambuc ++ecp->cp;
116784d9c625SLionel Sambuc --ecp->clen;
116884d9c625SLionel Sambuc break;
116984d9c625SLionel Sambuc } else
117084d9c625SLionel Sambuc *p++ = ch;
117184d9c625SLionel Sambuc }
117284d9c625SLionel Sambuc if (argv_exp0(sp, ecp, t, p - t))
117384d9c625SLionel Sambuc goto err;
117484d9c625SLionel Sambuc
117584d9c625SLionel Sambuc /* Delete intervening whitespace. */
117684d9c625SLionel Sambuc for (; ecp->clen > 0;
117784d9c625SLionel Sambuc --ecp->clen, ++ecp->cp) {
117884d9c625SLionel Sambuc ch = (UCHAR_T)*ecp->cp;
117984d9c625SLionel Sambuc if (!ISBLANK(ch))
118084d9c625SLionel Sambuc break;
118184d9c625SLionel Sambuc }
118284d9c625SLionel Sambuc if (ecp->clen == 0)
118384d9c625SLionel Sambuc goto usage;
118484d9c625SLionel Sambuc
118584d9c625SLionel Sambuc /* Followed by the string. */
118684d9c625SLionel Sambuc for (p = t = ecp->cp; ecp->clen > 0;
118784d9c625SLionel Sambuc --ecp->clen, ++ecp->cp, ++p) {
118884d9c625SLionel Sambuc ch = (UCHAR_T)*ecp->cp;
118984d9c625SLionel Sambuc if (IS_ESCAPE(sp,
119084d9c625SLionel Sambuc ecp, ch) && ecp->clen > 1) {
119184d9c625SLionel Sambuc --ecp->clen;
119284d9c625SLionel Sambuc *p = *++ecp->cp;
119384d9c625SLionel Sambuc } else
119484d9c625SLionel Sambuc *p = ch;
119584d9c625SLionel Sambuc }
119684d9c625SLionel Sambuc if (argv_exp0(sp, ecp, t, p - t))
119784d9c625SLionel Sambuc goto err;
119884d9c625SLionel Sambuc goto addr_verify;
119984d9c625SLionel Sambuc case 'w': /* word */
120084d9c625SLionel Sambuc if (argv_exp3(sp, ecp, ecp->cp, ecp->clen))
120184d9c625SLionel Sambuc goto err;
120284d9c625SLionel Sambuc arg_cnt_chk: if (*++np != 'N') { /* N */
120384d9c625SLionel Sambuc /*
120484d9c625SLionel Sambuc * If a number is specified, must either be
120584d9c625SLionel Sambuc * 0 or that number, if optional, and that
120684d9c625SLionel Sambuc * number, if required.
120784d9c625SLionel Sambuc */
120884d9c625SLionel Sambuc tmp = *np - '0';
120984d9c625SLionel Sambuc if ((*++np != 'o' || exp->argsoff != 0) &&
121084d9c625SLionel Sambuc exp->argsoff != tmp)
121184d9c625SLionel Sambuc goto usage;
121284d9c625SLionel Sambuc }
121384d9c625SLionel Sambuc goto addr_verify;
121484d9c625SLionel Sambuc default: {
121584d9c625SLionel Sambuc const char *nstr;
121684d9c625SLionel Sambuc size_t nlen;
121784d9c625SLionel Sambuc INT2CHAR(sp, ecp->cmd->name, STRLEN(ecp->cmd->name) + 1,
121884d9c625SLionel Sambuc nstr, nlen);
121984d9c625SLionel Sambuc msgq(sp, M_ERR,
122084d9c625SLionel Sambuc "085|Internal syntax table error (%s: %s)",
122184d9c625SLionel Sambuc nstr, KEY_NAME(sp, *np));
122284d9c625SLionel Sambuc }
122384d9c625SLionel Sambuc }
122484d9c625SLionel Sambuc }
122584d9c625SLionel Sambuc
122684d9c625SLionel Sambuc /* Skip trailing whitespace. */
122784d9c625SLionel Sambuc for (; ecp->clen > 0; --ecp->clen) {
122884d9c625SLionel Sambuc ch = (UCHAR_T)*ecp->cp++;
122984d9c625SLionel Sambuc if (!ISBLANK(ch))
123084d9c625SLionel Sambuc break;
123184d9c625SLionel Sambuc }
123284d9c625SLionel Sambuc
123384d9c625SLionel Sambuc /*
123484d9c625SLionel Sambuc * There shouldn't be anything left, and no more required fields,
123584d9c625SLionel Sambuc * i.e neither 'l' or 'r' in the syntax string.
123684d9c625SLionel Sambuc */
123784d9c625SLionel Sambuc if (ecp->clen != 0 || strpbrk(np, "lr")) {
123884d9c625SLionel Sambuc usage: msgq(sp, M_ERR, "086|Usage: %s", ecp->cmd->usage);
123984d9c625SLionel Sambuc goto err;
124084d9c625SLionel Sambuc }
124184d9c625SLionel Sambuc
124284d9c625SLionel Sambuc /*
124384d9c625SLionel Sambuc * Verify that the addresses are legal. Check the addresses here,
124484d9c625SLionel Sambuc * because this is a place where all ex addresses pass through.
124584d9c625SLionel Sambuc * (They don't all pass through ex_line(), for instance.) We're
124684d9c625SLionel Sambuc * assuming that any non-existent line doesn't exist because it's
124784d9c625SLionel Sambuc * past the end-of-file. That's a pretty good guess.
124884d9c625SLionel Sambuc *
124984d9c625SLionel Sambuc * If it's a "default vi command", an address of zero is okay.
125084d9c625SLionel Sambuc */
125184d9c625SLionel Sambuc addr_verify:
125284d9c625SLionel Sambuc switch (ecp->addrcnt) {
125384d9c625SLionel Sambuc case 2:
125484d9c625SLionel Sambuc /*
125584d9c625SLionel Sambuc * Historic ex/vi permitted commands with counts to go past
125684d9c625SLionel Sambuc * EOF. So, for example, if the file only had 5 lines, the
125784d9c625SLionel Sambuc * ex command "1,6>" would fail, but the command ">300"
125884d9c625SLionel Sambuc * would succeed. Since we don't want to have to make all
125984d9c625SLionel Sambuc * of the underlying commands handle random line numbers,
126084d9c625SLionel Sambuc * fix it here.
126184d9c625SLionel Sambuc */
126284d9c625SLionel Sambuc if (ecp->addr2.lno == 0) {
126384d9c625SLionel Sambuc if (!F_ISSET(ecp, E_ADDR_ZERO) &&
126484d9c625SLionel Sambuc (F_ISSET(sp, SC_EX) ||
126584d9c625SLionel Sambuc !F_ISSET(ecp, E_USELASTCMD))) {
126684d9c625SLionel Sambuc ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
126784d9c625SLionel Sambuc goto err;
126884d9c625SLionel Sambuc }
126984d9c625SLionel Sambuc } else if (!db_exist(sp, ecp->addr2.lno)) {
127084d9c625SLionel Sambuc if (FL_ISSET(ecp->iflags, E_C_COUNT)) {
127184d9c625SLionel Sambuc if (db_last(sp, &lno))
127284d9c625SLionel Sambuc goto err;
127384d9c625SLionel Sambuc ecp->addr2.lno = lno;
127484d9c625SLionel Sambuc } else {
127584d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_EOF, NUM_OK);
127684d9c625SLionel Sambuc goto err;
127784d9c625SLionel Sambuc }
127884d9c625SLionel Sambuc }
127984d9c625SLionel Sambuc /* FALLTHROUGH */
128084d9c625SLionel Sambuc case 1:
128184d9c625SLionel Sambuc if (ecp->addr1.lno == 0) {
128284d9c625SLionel Sambuc if (!F_ISSET(ecp, E_ADDR_ZERO) &&
128384d9c625SLionel Sambuc (F_ISSET(sp, SC_EX) ||
128484d9c625SLionel Sambuc !F_ISSET(ecp, E_USELASTCMD))) {
128584d9c625SLionel Sambuc ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
128684d9c625SLionel Sambuc goto err;
128784d9c625SLionel Sambuc }
128884d9c625SLionel Sambuc } else if (!db_exist(sp, ecp->addr1.lno)) {
128984d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_EOF, NUM_OK);
129084d9c625SLionel Sambuc goto err;
129184d9c625SLionel Sambuc }
129284d9c625SLionel Sambuc break;
129384d9c625SLionel Sambuc }
129484d9c625SLionel Sambuc
129584d9c625SLionel Sambuc /*
129684d9c625SLionel Sambuc * If doing a default command and there's nothing left on the line,
129784d9c625SLionel Sambuc * vi just moves to the line. For example, ":3" and ":'a,'b" just
129884d9c625SLionel Sambuc * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
129984d9c625SLionel Sambuc *
130084d9c625SLionel Sambuc * !!!
130184d9c625SLionel Sambuc * In addition, IF THE LINE CHANGES, move to the first nonblank of
130284d9c625SLionel Sambuc * the line.
130384d9c625SLionel Sambuc *
130484d9c625SLionel Sambuc * !!!
130584d9c625SLionel Sambuc * This is done before the absolute mark gets set; historically,
130684d9c625SLionel Sambuc * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
130784d9c625SLionel Sambuc */
130884d9c625SLionel Sambuc if ((F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_NOPRDEF)) &&
130984d9c625SLionel Sambuc F_ISSET(ecp, E_USELASTCMD) && vi_address == 0) {
131084d9c625SLionel Sambuc switch (ecp->addrcnt) {
131184d9c625SLionel Sambuc case 2:
131284d9c625SLionel Sambuc if (sp->lno !=
131384d9c625SLionel Sambuc (ecp->addr2.lno ? ecp->addr2.lno : 1)) {
131484d9c625SLionel Sambuc sp->lno =
131584d9c625SLionel Sambuc ecp->addr2.lno ? ecp->addr2.lno : 1;
131684d9c625SLionel Sambuc sp->cno = 0;
131784d9c625SLionel Sambuc (void)nonblank(sp, sp->lno, &sp->cno);
131884d9c625SLionel Sambuc }
131984d9c625SLionel Sambuc break;
132084d9c625SLionel Sambuc case 1:
132184d9c625SLionel Sambuc if (sp->lno !=
132284d9c625SLionel Sambuc (ecp->addr1.lno ? ecp->addr1.lno : 1)) {
132384d9c625SLionel Sambuc sp->lno =
132484d9c625SLionel Sambuc ecp->addr1.lno ? ecp->addr1.lno : 1;
132584d9c625SLionel Sambuc sp->cno = 0;
132684d9c625SLionel Sambuc (void)nonblank(sp, sp->lno, &sp->cno);
132784d9c625SLionel Sambuc }
132884d9c625SLionel Sambuc break;
132984d9c625SLionel Sambuc }
133084d9c625SLionel Sambuc ecp->cp = ecp->save_cmd;
133184d9c625SLionel Sambuc ecp->clen = ecp->save_cmdlen;
133284d9c625SLionel Sambuc goto loop;
133384d9c625SLionel Sambuc }
133484d9c625SLionel Sambuc
133584d9c625SLionel Sambuc /*
133684d9c625SLionel Sambuc * Set the absolute mark -- we have to set it for vi here, in case
133784d9c625SLionel Sambuc * it's a compound command, e.g. ":5p|6" should set the absolute
133884d9c625SLionel Sambuc * mark for vi.
133984d9c625SLionel Sambuc */
134084d9c625SLionel Sambuc if (F_ISSET(ecp, E_ABSMARK)) {
134184d9c625SLionel Sambuc cur.lno = sp->lno;
134284d9c625SLionel Sambuc cur.cno = sp->cno;
134384d9c625SLionel Sambuc F_CLR(ecp, E_ABSMARK);
134484d9c625SLionel Sambuc if (mark_set(sp, ABSMARK1, &cur, 1))
134584d9c625SLionel Sambuc goto err;
134684d9c625SLionel Sambuc }
134784d9c625SLionel Sambuc
134884d9c625SLionel Sambuc #if defined(DEBUG) && defined(COMLOG)
134984d9c625SLionel Sambuc ex_comlog(sp, ecp);
135084d9c625SLionel Sambuc #endif
135184d9c625SLionel Sambuc /* Increment the command count if not called from vi. */
135284d9c625SLionel Sambuc if (F_ISSET(sp, SC_EX))
135384d9c625SLionel Sambuc ++sp->ccnt;
135484d9c625SLionel Sambuc
135584d9c625SLionel Sambuc /*
135684d9c625SLionel Sambuc * If file state available, and not doing a global command,
135784d9c625SLionel Sambuc * log the start of an action.
135884d9c625SLionel Sambuc */
135984d9c625SLionel Sambuc if (sp->ep != NULL && !F_ISSET(sp, SC_EX_GLOBAL))
136084d9c625SLionel Sambuc (void)log_cursor(sp);
136184d9c625SLionel Sambuc
136284d9c625SLionel Sambuc /*
136384d9c625SLionel Sambuc * !!!
136484d9c625SLionel Sambuc * There are two special commands for the purposes of this code: the
136584d9c625SLionel Sambuc * default command (<carriage-return>) or the scrolling commands (^D
136684d9c625SLionel Sambuc * and <EOF>) as the first non-<blank> characters in the line.
136784d9c625SLionel Sambuc *
136884d9c625SLionel Sambuc * If this is the first command in the command line, we received the
136984d9c625SLionel Sambuc * command from the ex command loop and we're talking to a tty, and
137084d9c625SLionel Sambuc * and there's nothing else on the command line, and it's one of the
137184d9c625SLionel Sambuc * special commands, we move back up to the previous line, and erase
137284d9c625SLionel Sambuc * the prompt character with the output. Since ex runs in canonical
137384d9c625SLionel Sambuc * mode, we don't have to do anything else, a <newline> has already
137484d9c625SLionel Sambuc * been echoed by the tty driver. It's OK if vi calls us -- we won't
137584d9c625SLionel Sambuc * be in ex mode so we'll do nothing.
137684d9c625SLionel Sambuc */
137784d9c625SLionel Sambuc if (F_ISSET(ecp, E_NRSEP)) {
137884d9c625SLionel Sambuc if (sp->ep != NULL &&
137984d9c625SLionel Sambuc F_ISSET(sp, SC_EX) && !F_ISSET(gp, G_SCRIPTED) &&
138084d9c625SLionel Sambuc (F_ISSET(ecp, E_USELASTCMD) || ecp->cmd == &cmds[C_SCROLL]))
138184d9c625SLionel Sambuc gp->scr_ex_adjust(sp, EX_TERM_SCROLL);
138284d9c625SLionel Sambuc F_CLR(ecp, E_NRSEP);
138384d9c625SLionel Sambuc }
138484d9c625SLionel Sambuc
138584d9c625SLionel Sambuc /*
138684d9c625SLionel Sambuc * Call the underlying function for the ex command.
138784d9c625SLionel Sambuc *
138884d9c625SLionel Sambuc * XXX
138984d9c625SLionel Sambuc * Interrupts behave like errors, for now.
139084d9c625SLionel Sambuc */
139184d9c625SLionel Sambuc if (ecp->cmd->fn(sp, ecp) || INTERRUPTED(sp)) {
139284d9c625SLionel Sambuc if (F_ISSET(gp, G_SCRIPTED))
139384d9c625SLionel Sambuc F_SET(sp, SC_EXIT_FORCE);
139484d9c625SLionel Sambuc goto err;
139584d9c625SLionel Sambuc }
139684d9c625SLionel Sambuc
139784d9c625SLionel Sambuc #ifdef DEBUG
139884d9c625SLionel Sambuc /* Make sure no function left global temporary space locked. */
139984d9c625SLionel Sambuc if (F_ISSET(wp, W_TMP_INUSE)) {
140084d9c625SLionel Sambuc F_CLR(wp, W_TMP_INUSE);
140184d9c625SLionel Sambuc msgq(sp, M_ERR, "087|%s: temporary buffer not released",
140284d9c625SLionel Sambuc ecp->cmd->name);
140384d9c625SLionel Sambuc }
140484d9c625SLionel Sambuc #endif
140584d9c625SLionel Sambuc /*
140684d9c625SLionel Sambuc * Ex displayed the number of lines modified immediately after each
140784d9c625SLionel Sambuc * command, so the command "1,10d|1,10d" would display:
140884d9c625SLionel Sambuc *
140984d9c625SLionel Sambuc * 10 lines deleted
141084d9c625SLionel Sambuc * 10 lines deleted
141184d9c625SLionel Sambuc * <autoprint line>
141284d9c625SLionel Sambuc *
141384d9c625SLionel Sambuc * Executing ex commands from vi only reported the final modified
141484d9c625SLionel Sambuc * lines message -- that's wrong enough that we don't match it.
141584d9c625SLionel Sambuc */
141684d9c625SLionel Sambuc if (F_ISSET(sp, SC_EX))
141784d9c625SLionel Sambuc mod_rpt(sp);
141884d9c625SLionel Sambuc
141984d9c625SLionel Sambuc /*
142084d9c625SLionel Sambuc * Integrate any offset parsed by the underlying command, and make
142184d9c625SLionel Sambuc * sure the referenced line exists.
142284d9c625SLionel Sambuc *
142384d9c625SLionel Sambuc * XXX
142484d9c625SLionel Sambuc * May not match historic practice (which I've never been able to
142584d9c625SLionel Sambuc * completely figure out.) For example, the '=' command from vi
142684d9c625SLionel Sambuc * mode often got the offset wrong, and complained it was too large,
142784d9c625SLionel Sambuc * but didn't seem to have a problem with the cursor. If anyone
142884d9c625SLionel Sambuc * complains, ask them how it's supposed to work, they might know.
142984d9c625SLionel Sambuc */
143084d9c625SLionel Sambuc if (sp->ep != NULL && ecp->flagoff) {
143184d9c625SLionel Sambuc if (ecp->flagoff < 0) {
143284d9c625SLionel Sambuc if (sp->lno <= (db_recno_t)(-ecp->flagoff)) {
143384d9c625SLionel Sambuc msgq(sp, M_ERR,
143484d9c625SLionel Sambuc "088|Flag offset to before line 1");
143584d9c625SLionel Sambuc goto err;
143684d9c625SLionel Sambuc }
143784d9c625SLionel Sambuc } else {
143884d9c625SLionel Sambuc if (!NPFITS(DB_MAX_RECORDS, sp->lno, (db_recno_t)ecp->flagoff)) {
143984d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
144084d9c625SLionel Sambuc goto err;
144184d9c625SLionel Sambuc }
144284d9c625SLionel Sambuc if (!db_exist(sp, sp->lno + ecp->flagoff)) {
144384d9c625SLionel Sambuc msgq(sp, M_ERR,
144484d9c625SLionel Sambuc "089|Flag offset past end-of-file");
144584d9c625SLionel Sambuc goto err;
144684d9c625SLionel Sambuc }
144784d9c625SLionel Sambuc }
144884d9c625SLionel Sambuc sp->lno += ecp->flagoff;
144984d9c625SLionel Sambuc }
145084d9c625SLionel Sambuc
145184d9c625SLionel Sambuc /*
145284d9c625SLionel Sambuc * If the command executed successfully, we may want to display a line
145384d9c625SLionel Sambuc * based on the autoprint option or an explicit print flag. (Make sure
145484d9c625SLionel Sambuc * that there's a line to display.) Also, the autoprint edit option is
145584d9c625SLionel Sambuc * turned off for the duration of global commands.
145684d9c625SLionel Sambuc */
145784d9c625SLionel Sambuc if (F_ISSET(sp, SC_EX) && sp->ep != NULL && sp->lno != 0) {
145884d9c625SLionel Sambuc /*
145984d9c625SLionel Sambuc * The print commands have already handled the `print' flags.
146084d9c625SLionel Sambuc * If so, clear them.
146184d9c625SLionel Sambuc */
146284d9c625SLionel Sambuc if (FL_ISSET(ecp->iflags, E_CLRFLAG))
146384d9c625SLionel Sambuc FL_CLR(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT);
146484d9c625SLionel Sambuc
146584d9c625SLionel Sambuc /* If hash set only because of the number option, discard it. */
146684d9c625SLionel Sambuc if (F_ISSET(ecp, E_OPTNUM))
146784d9c625SLionel Sambuc FL_CLR(ecp->iflags, E_C_HASH);
146884d9c625SLionel Sambuc
146984d9c625SLionel Sambuc /*
147084d9c625SLionel Sambuc * If there was an explicit flag to display the new cursor line,
147184d9c625SLionel Sambuc * or autoprint is set and a change was made, display the line.
147284d9c625SLionel Sambuc * If any print flags were set use them, else default to print.
147384d9c625SLionel Sambuc */
147484d9c625SLionel Sambuc LF_INIT(FL_ISSET(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT));
147584d9c625SLionel Sambuc if (!LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT | E_NOAUTO) &&
147684d9c625SLionel Sambuc !F_ISSET(sp, SC_EX_GLOBAL) &&
147784d9c625SLionel Sambuc O_ISSET(sp, O_AUTOPRINT) && F_ISSET(ecp, E_AUTOPRINT))
147884d9c625SLionel Sambuc LF_INIT(E_C_PRINT);
147984d9c625SLionel Sambuc
148084d9c625SLionel Sambuc if (LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT)) {
148184d9c625SLionel Sambuc cur.lno = sp->lno;
148284d9c625SLionel Sambuc cur.cno = 0;
148384d9c625SLionel Sambuc (void)ex_print(sp, ecp, &cur, &cur, flags);
148484d9c625SLionel Sambuc }
148584d9c625SLionel Sambuc }
148684d9c625SLionel Sambuc
148784d9c625SLionel Sambuc /*
148884d9c625SLionel Sambuc * If the command had an associated "+cmd", it has to be executed
148984d9c625SLionel Sambuc * before we finish executing any more of this ex command. For
149084d9c625SLionel Sambuc * example, consider a .exrc file that contains the following lines:
149184d9c625SLionel Sambuc *
149284d9c625SLionel Sambuc * :set all
149384d9c625SLionel Sambuc * :edit +25 file.c|s/abc/ABC/|1
149484d9c625SLionel Sambuc * :3,5 print
149584d9c625SLionel Sambuc *
149684d9c625SLionel Sambuc * This can happen more than once -- the historic vi simply hung or
149784d9c625SLionel Sambuc * dropped core, of course. Prepend the + command back into the
149884d9c625SLionel Sambuc * current command and continue. We may have to add an additional
149984d9c625SLionel Sambuc * <literal next> character. We know that it will fit because we
150084d9c625SLionel Sambuc * discarded at least one space and the + character.
150184d9c625SLionel Sambuc */
150284d9c625SLionel Sambuc if (arg1_len != 0) {
150384d9c625SLionel Sambuc /*
150484d9c625SLionel Sambuc * If the last character of the + command was a <literal next>
150584d9c625SLionel Sambuc * character, it would be treated differently because of the
150684d9c625SLionel Sambuc * append. Quote it, if necessary.
150784d9c625SLionel Sambuc */
150884d9c625SLionel Sambuc if (IS_ESCAPE(sp, ecp, arg1[arg1_len - 1])) {
150984d9c625SLionel Sambuc *--ecp->save_cmd = CH_LITERAL;
151084d9c625SLionel Sambuc ++ecp->save_cmdlen;
151184d9c625SLionel Sambuc }
151284d9c625SLionel Sambuc
151384d9c625SLionel Sambuc ecp->save_cmd -= arg1_len;
151484d9c625SLionel Sambuc ecp->save_cmdlen += arg1_len;
151584d9c625SLionel Sambuc MEMCPYW(ecp->save_cmd, arg1, arg1_len);
151684d9c625SLionel Sambuc
151784d9c625SLionel Sambuc /*
151884d9c625SLionel Sambuc * Any commands executed from a +cmd are executed starting at
151984d9c625SLionel Sambuc * the first column of the last line of the file -- NOT the
152084d9c625SLionel Sambuc * first nonblank.) The main file startup code doesn't know
152184d9c625SLionel Sambuc * that a +cmd was set, however, so it may have put us at the
152284d9c625SLionel Sambuc * top of the file. (Note, this is safe because we must have
152384d9c625SLionel Sambuc * switched files to get here.)
152484d9c625SLionel Sambuc */
152584d9c625SLionel Sambuc F_SET(ecp, E_MOVETOEND);
152684d9c625SLionel Sambuc }
152784d9c625SLionel Sambuc
152884d9c625SLionel Sambuc /* Update the current command. */
152984d9c625SLionel Sambuc ecp->cp = ecp->save_cmd;
153084d9c625SLionel Sambuc ecp->clen = ecp->save_cmdlen;
153184d9c625SLionel Sambuc
153284d9c625SLionel Sambuc /*
153384d9c625SLionel Sambuc * !!!
153484d9c625SLionel Sambuc * If we've changed screens or underlying files, any pending global or
153584d9c625SLionel Sambuc * v command, or @ buffer that has associated addresses, has to be
153684d9c625SLionel Sambuc * discarded. This is historic practice for globals, and necessary for
153784d9c625SLionel Sambuc * @ buffers that had associated addresses.
153884d9c625SLionel Sambuc *
153984d9c625SLionel Sambuc * Otherwise, if we've changed underlying files, it's not a problem,
154084d9c625SLionel Sambuc * we continue with the rest of the ex command(s), operating on the
154184d9c625SLionel Sambuc * new file. However, if we switch screens (either by exiting or by
154284d9c625SLionel Sambuc * an explicit command), we have no way of knowing where to put output
154384d9c625SLionel Sambuc * messages, and, since we don't control screens here, we could screw
154484d9c625SLionel Sambuc * up the upper layers, (e.g. we could exit/reenter a screen multiple
154584d9c625SLionel Sambuc * times). So, return and continue after we've got a new screen.
154684d9c625SLionel Sambuc */
154784d9c625SLionel Sambuc if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH)) {
154884d9c625SLionel Sambuc at_found = gv_found = 0;
154984d9c625SLionel Sambuc LIST_FOREACH(ecp, &wp->ecq, q)
155084d9c625SLionel Sambuc switch (ecp->agv_flags) {
155184d9c625SLionel Sambuc case 0:
155284d9c625SLionel Sambuc case AGV_AT_NORANGE:
155384d9c625SLionel Sambuc break;
155484d9c625SLionel Sambuc case AGV_AT:
155584d9c625SLionel Sambuc if (!at_found) {
155684d9c625SLionel Sambuc at_found = 1;
155784d9c625SLionel Sambuc msgq(sp, M_ERR,
155884d9c625SLionel Sambuc "090|@ with range running when the file/screen changed");
155984d9c625SLionel Sambuc }
156084d9c625SLionel Sambuc break;
156184d9c625SLionel Sambuc case AGV_GLOBAL:
156284d9c625SLionel Sambuc case AGV_V:
156384d9c625SLionel Sambuc if (!gv_found) {
156484d9c625SLionel Sambuc gv_found = 1;
156584d9c625SLionel Sambuc msgq(sp, M_ERR,
156684d9c625SLionel Sambuc "091|Global/v command running when the file/screen changed");
156784d9c625SLionel Sambuc }
156884d9c625SLionel Sambuc break;
156984d9c625SLionel Sambuc default:
157084d9c625SLionel Sambuc abort();
157184d9c625SLionel Sambuc }
157284d9c625SLionel Sambuc if (at_found || gv_found)
157384d9c625SLionel Sambuc goto discard;
157484d9c625SLionel Sambuc if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_SSWITCH))
157584d9c625SLionel Sambuc goto rsuccess;
157684d9c625SLionel Sambuc }
157784d9c625SLionel Sambuc
157884d9c625SLionel Sambuc goto loop;
157984d9c625SLionel Sambuc /* NOTREACHED */
158084d9c625SLionel Sambuc
158184d9c625SLionel Sambuc err: /*
158284d9c625SLionel Sambuc * On command failure, we discard keys and pending commands remaining,
158384d9c625SLionel Sambuc * as well as any keys that were mapped and waiting. The save_cmdlen
158484d9c625SLionel Sambuc * test is not necessarily correct. If we fail early enough we don't
158584d9c625SLionel Sambuc * know if the entire string was a single command or not. Guess, as
158684d9c625SLionel Sambuc * it's useful to know if commands other than the current one are being
158784d9c625SLionel Sambuc * discarded.
158884d9c625SLionel Sambuc */
158984d9c625SLionel Sambuc if (ecp->save_cmdlen == 0)
159084d9c625SLionel Sambuc for (; ecp->clen; --ecp->clen) {
159184d9c625SLionel Sambuc ch = (UCHAR_T)*ecp->cp++;
159284d9c625SLionel Sambuc if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
159384d9c625SLionel Sambuc --ecp->clen;
159484d9c625SLionel Sambuc ++ecp->cp;
159584d9c625SLionel Sambuc } else if (ch == '\n' || ch == '|') {
159684d9c625SLionel Sambuc if (ecp->clen > 1)
159784d9c625SLionel Sambuc ecp->save_cmdlen = 1;
159884d9c625SLionel Sambuc break;
159984d9c625SLionel Sambuc }
160084d9c625SLionel Sambuc }
160184d9c625SLionel Sambuc if (ecp->save_cmdlen != 0 || LIST_FIRST(&wp->ecq) != &wp->excmd) {
160284d9c625SLionel Sambuc discard: msgq(sp, M_BERR,
160384d9c625SLionel Sambuc "092|Ex command failed: pending commands discarded");
160484d9c625SLionel Sambuc ex_discard(sp);
160584d9c625SLionel Sambuc }
160684d9c625SLionel Sambuc if (v_event_flush(sp, CH_MAPPED))
160784d9c625SLionel Sambuc msgq(sp, M_BERR,
160884d9c625SLionel Sambuc "093|Ex command failed: mapped keys discarded");
160984d9c625SLionel Sambuc
161084d9c625SLionel Sambuc rfail: tmp = 1;
161184d9c625SLionel Sambuc if (0)
161284d9c625SLionel Sambuc rsuccess: tmp = 0;
161384d9c625SLionel Sambuc
161484d9c625SLionel Sambuc /* Turn off any file name error information. */
161584d9c625SLionel Sambuc wp->if_name = NULL;
161684d9c625SLionel Sambuc
161784d9c625SLionel Sambuc /* Turn off the global bit. */
161884d9c625SLionel Sambuc F_CLR(sp, SC_EX_GLOBAL);
161984d9c625SLionel Sambuc
162084d9c625SLionel Sambuc return (tmp);
162184d9c625SLionel Sambuc }
162284d9c625SLionel Sambuc
162384d9c625SLionel Sambuc /*
162484d9c625SLionel Sambuc * ex_range --
162584d9c625SLionel Sambuc * Get a line range for ex commands, or perform a vi ex address search.
162684d9c625SLionel Sambuc *
162784d9c625SLionel Sambuc * PUBLIC: int ex_range __P((SCR *, EXCMD *, int *));
162884d9c625SLionel Sambuc */
162984d9c625SLionel Sambuc int
ex_range(SCR * sp,EXCMD * ecp,int * errp)163084d9c625SLionel Sambuc ex_range(SCR *sp, EXCMD *ecp, int *errp)
163184d9c625SLionel Sambuc {
163284d9c625SLionel Sambuc enum { ADDR_FOUND, ADDR_NEED, ADDR_NONE } addr;
163384d9c625SLionel Sambuc MARK m;
163484d9c625SLionel Sambuc int isaddr;
163584d9c625SLionel Sambuc
163684d9c625SLionel Sambuc *errp = 0;
163784d9c625SLionel Sambuc
163884d9c625SLionel Sambuc /*
163984d9c625SLionel Sambuc * Parse comma or semi-colon delimited line specs.
164084d9c625SLionel Sambuc *
164184d9c625SLionel Sambuc * Semi-colon delimiters update the current address to be the last
164284d9c625SLionel Sambuc * address. For example, the command
164384d9c625SLionel Sambuc *
164484d9c625SLionel Sambuc * :3;/pattern/ecp->cp
164584d9c625SLionel Sambuc *
164684d9c625SLionel Sambuc * will search for pattern from line 3. In addition, if ecp->cp
164784d9c625SLionel Sambuc * is not a valid command, the current line will be left at 3, not
164884d9c625SLionel Sambuc * at the original address.
164984d9c625SLionel Sambuc *
165084d9c625SLionel Sambuc * Extra addresses are discarded, starting with the first.
165184d9c625SLionel Sambuc *
165284d9c625SLionel Sambuc * !!!
165384d9c625SLionel Sambuc * If any addresses are missing, they default to the current line.
165484d9c625SLionel Sambuc * This was historically true for both leading and trailing comma
165584d9c625SLionel Sambuc * delimited addresses as well as for trailing semicolon delimited
165684d9c625SLionel Sambuc * addresses. For consistency, we make it true for leading semicolon
165784d9c625SLionel Sambuc * addresses as well.
165884d9c625SLionel Sambuc */
165984d9c625SLionel Sambuc for (addr = ADDR_NONE, ecp->addrcnt = 0; ecp->clen > 0;)
166084d9c625SLionel Sambuc switch (*ecp->cp) {
166184d9c625SLionel Sambuc case '%': /* Entire file. */
166284d9c625SLionel Sambuc /* Vi ex address searches didn't permit % signs. */
166384d9c625SLionel Sambuc if (F_ISSET(ecp, E_VISEARCH))
166484d9c625SLionel Sambuc goto ret;
166584d9c625SLionel Sambuc
166684d9c625SLionel Sambuc /* It's an error if the file is empty. */
166784d9c625SLionel Sambuc if (sp->ep == NULL) {
166884d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
166984d9c625SLionel Sambuc *errp = 1;
167084d9c625SLionel Sambuc return (0);
167184d9c625SLionel Sambuc }
167284d9c625SLionel Sambuc /*
167384d9c625SLionel Sambuc * !!!
167484d9c625SLionel Sambuc * A percent character addresses all of the lines in
167584d9c625SLionel Sambuc * the file. Historically, it couldn't be followed by
167684d9c625SLionel Sambuc * any other address. We do it as a text substitution
167784d9c625SLionel Sambuc * for simplicity. POSIX 1003.2 is expected to follow
167884d9c625SLionel Sambuc * this practice.
167984d9c625SLionel Sambuc *
168084d9c625SLionel Sambuc * If it's an empty file, the first line is 0, not 1.
168184d9c625SLionel Sambuc */
168284d9c625SLionel Sambuc if (addr == ADDR_FOUND) {
168384d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
168484d9c625SLionel Sambuc *errp = 1;
168584d9c625SLionel Sambuc return (0);
168684d9c625SLionel Sambuc }
168784d9c625SLionel Sambuc if (db_last(sp, &ecp->addr2.lno))
168884d9c625SLionel Sambuc return (1);
168984d9c625SLionel Sambuc ecp->addr1.lno = ecp->addr2.lno == 0 ? 0 : 1;
169084d9c625SLionel Sambuc ecp->addr1.cno = ecp->addr2.cno = 0;
169184d9c625SLionel Sambuc ecp->addrcnt = 2;
169284d9c625SLionel Sambuc addr = ADDR_FOUND;
169384d9c625SLionel Sambuc ++ecp->cp;
169484d9c625SLionel Sambuc --ecp->clen;
169584d9c625SLionel Sambuc break;
169684d9c625SLionel Sambuc case ',': /* Comma delimiter. */
169784d9c625SLionel Sambuc /* Vi ex address searches didn't permit commas. */
169884d9c625SLionel Sambuc if (F_ISSET(ecp, E_VISEARCH))
169984d9c625SLionel Sambuc goto ret;
170084d9c625SLionel Sambuc /* FALLTHROUGH */
170184d9c625SLionel Sambuc case ';': /* Semi-colon delimiter. */
170284d9c625SLionel Sambuc if (sp->ep == NULL) {
170384d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
170484d9c625SLionel Sambuc *errp = 1;
170584d9c625SLionel Sambuc return (0);
170684d9c625SLionel Sambuc }
170784d9c625SLionel Sambuc if (addr != ADDR_FOUND)
170884d9c625SLionel Sambuc switch (ecp->addrcnt) {
170984d9c625SLionel Sambuc case 0:
171084d9c625SLionel Sambuc ecp->addr1.lno = sp->lno;
171184d9c625SLionel Sambuc ecp->addr1.cno = sp->cno;
171284d9c625SLionel Sambuc ecp->addrcnt = 1;
171384d9c625SLionel Sambuc break;
171484d9c625SLionel Sambuc case 2:
171584d9c625SLionel Sambuc ecp->addr1 = ecp->addr2;
171684d9c625SLionel Sambuc /* FALLTHROUGH */
171784d9c625SLionel Sambuc case 1:
171884d9c625SLionel Sambuc ecp->addr2.lno = sp->lno;
171984d9c625SLionel Sambuc ecp->addr2.cno = sp->cno;
172084d9c625SLionel Sambuc ecp->addrcnt = 2;
172184d9c625SLionel Sambuc break;
172284d9c625SLionel Sambuc }
172384d9c625SLionel Sambuc if (*ecp->cp == ';')
172484d9c625SLionel Sambuc switch (ecp->addrcnt) {
172584d9c625SLionel Sambuc case 0:
172684d9c625SLionel Sambuc abort();
172784d9c625SLionel Sambuc /* NOTREACHED */
172884d9c625SLionel Sambuc case 1:
172984d9c625SLionel Sambuc sp->lno = ecp->addr1.lno;
173084d9c625SLionel Sambuc sp->cno = ecp->addr1.cno;
173184d9c625SLionel Sambuc break;
173284d9c625SLionel Sambuc case 2:
173384d9c625SLionel Sambuc sp->lno = ecp->addr2.lno;
173484d9c625SLionel Sambuc sp->cno = ecp->addr2.cno;
173584d9c625SLionel Sambuc break;
173684d9c625SLionel Sambuc }
173784d9c625SLionel Sambuc addr = ADDR_NEED;
173884d9c625SLionel Sambuc /* FALLTHROUGH */
173984d9c625SLionel Sambuc case ' ': /* Whitespace. */
174084d9c625SLionel Sambuc case '\t': /* Whitespace. */
174184d9c625SLionel Sambuc ++ecp->cp;
174284d9c625SLionel Sambuc --ecp->clen;
174384d9c625SLionel Sambuc break;
174484d9c625SLionel Sambuc default:
174584d9c625SLionel Sambuc /* Get a line specification. */
174684d9c625SLionel Sambuc if (ex_line(sp, ecp, &m, &isaddr, errp))
174784d9c625SLionel Sambuc return (1);
174884d9c625SLionel Sambuc if (*errp)
174984d9c625SLionel Sambuc return (0);
175084d9c625SLionel Sambuc if (!isaddr)
175184d9c625SLionel Sambuc goto ret;
175284d9c625SLionel Sambuc if (addr == ADDR_FOUND) {
175384d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
175484d9c625SLionel Sambuc *errp = 1;
175584d9c625SLionel Sambuc return (0);
175684d9c625SLionel Sambuc }
175784d9c625SLionel Sambuc switch (ecp->addrcnt) {
175884d9c625SLionel Sambuc case 0:
175984d9c625SLionel Sambuc ecp->addr1 = m;
176084d9c625SLionel Sambuc ecp->addrcnt = 1;
176184d9c625SLionel Sambuc break;
176284d9c625SLionel Sambuc case 1:
176384d9c625SLionel Sambuc ecp->addr2 = m;
176484d9c625SLionel Sambuc ecp->addrcnt = 2;
176584d9c625SLionel Sambuc break;
176684d9c625SLionel Sambuc case 2:
176784d9c625SLionel Sambuc ecp->addr1 = ecp->addr2;
176884d9c625SLionel Sambuc ecp->addr2 = m;
176984d9c625SLionel Sambuc break;
177084d9c625SLionel Sambuc }
177184d9c625SLionel Sambuc addr = ADDR_FOUND;
177284d9c625SLionel Sambuc break;
177384d9c625SLionel Sambuc }
177484d9c625SLionel Sambuc
177584d9c625SLionel Sambuc /*
177684d9c625SLionel Sambuc * !!!
177784d9c625SLionel Sambuc * Vi ex address searches are indifferent to order or trailing
177884d9c625SLionel Sambuc * semi-colons.
177984d9c625SLionel Sambuc */
178084d9c625SLionel Sambuc ret: if (F_ISSET(ecp, E_VISEARCH))
178184d9c625SLionel Sambuc return (0);
178284d9c625SLionel Sambuc
178384d9c625SLionel Sambuc if (addr == ADDR_NEED)
178484d9c625SLionel Sambuc switch (ecp->addrcnt) {
178584d9c625SLionel Sambuc case 0:
178684d9c625SLionel Sambuc ecp->addr1.lno = sp->lno;
178784d9c625SLionel Sambuc ecp->addr1.cno = sp->cno;
178884d9c625SLionel Sambuc ecp->addrcnt = 1;
178984d9c625SLionel Sambuc break;
179084d9c625SLionel Sambuc case 2:
179184d9c625SLionel Sambuc ecp->addr1 = ecp->addr2;
179284d9c625SLionel Sambuc /* FALLTHROUGH */
179384d9c625SLionel Sambuc case 1:
179484d9c625SLionel Sambuc ecp->addr2.lno = sp->lno;
179584d9c625SLionel Sambuc ecp->addr2.cno = sp->cno;
179684d9c625SLionel Sambuc ecp->addrcnt = 2;
179784d9c625SLionel Sambuc break;
179884d9c625SLionel Sambuc }
179984d9c625SLionel Sambuc
180084d9c625SLionel Sambuc if (ecp->addrcnt == 2 && ecp->addr2.lno < ecp->addr1.lno) {
180184d9c625SLionel Sambuc msgq(sp, M_ERR,
180284d9c625SLionel Sambuc "094|The second address is smaller than the first");
180384d9c625SLionel Sambuc *errp = 1;
180484d9c625SLionel Sambuc }
180584d9c625SLionel Sambuc return (0);
180684d9c625SLionel Sambuc }
180784d9c625SLionel Sambuc
180884d9c625SLionel Sambuc /*
180984d9c625SLionel Sambuc * ex_line --
181084d9c625SLionel Sambuc * Get a single line address specifier.
181184d9c625SLionel Sambuc *
181284d9c625SLionel Sambuc * The way the "previous context" mark worked was that any "non-relative"
181384d9c625SLionel Sambuc * motion set it. While ex/vi wasn't totally consistent about this, ANY
181484d9c625SLionel Sambuc * numeric address, search pattern, '$', or mark reference in an address
181584d9c625SLionel Sambuc * was considered non-relative, and set the value. Which should explain
181684d9c625SLionel Sambuc * why we're hacking marks down here. The problem was that the mark was
181784d9c625SLionel Sambuc * only set if the command was called, i.e. we have to set a flag and test
181884d9c625SLionel Sambuc * it later.
181984d9c625SLionel Sambuc *
182084d9c625SLionel Sambuc * XXX
182184d9c625SLionel Sambuc * This is probably still not exactly historic practice, although I think
182284d9c625SLionel Sambuc * it's fairly close.
182384d9c625SLionel Sambuc */
182484d9c625SLionel Sambuc static int
ex_line(SCR * sp,EXCMD * ecp,MARK * mp,int * isaddrp,int * errp)182584d9c625SLionel Sambuc ex_line(SCR *sp, EXCMD *ecp, MARK *mp, int *isaddrp, int *errp)
182684d9c625SLionel Sambuc {
182784d9c625SLionel Sambuc enum nresult nret;
182884d9c625SLionel Sambuc long total, val;
182984d9c625SLionel Sambuc unsigned long uval;
183084d9c625SLionel Sambuc int isneg;
183184d9c625SLionel Sambuc int (*sf) __P((SCR *, MARK *, MARK *, CHAR_T *, size_t, CHAR_T **, u_int));
183284d9c625SLionel Sambuc CHAR_T *endp;
183384d9c625SLionel Sambuc
183484d9c625SLionel Sambuc *isaddrp = *errp = 0;
183584d9c625SLionel Sambuc F_CLR(ecp, E_DELTA);
183684d9c625SLionel Sambuc
183784d9c625SLionel Sambuc /* No addresses permitted until a file has been read in. */
183884d9c625SLionel Sambuc if (sp->ep == NULL && STRCHR(L("$0123456789'\\/?.+-^"), *ecp->cp)) {
183984d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
184084d9c625SLionel Sambuc *errp = 1;
184184d9c625SLionel Sambuc return (0);
184284d9c625SLionel Sambuc }
184384d9c625SLionel Sambuc
184484d9c625SLionel Sambuc switch (*ecp->cp) {
184584d9c625SLionel Sambuc case '$': /* Last line in the file. */
184684d9c625SLionel Sambuc *isaddrp = 1;
184784d9c625SLionel Sambuc F_SET(ecp, E_ABSMARK);
184884d9c625SLionel Sambuc
184984d9c625SLionel Sambuc mp->cno = 0;
185084d9c625SLionel Sambuc if (db_last(sp, &mp->lno))
185184d9c625SLionel Sambuc return (1);
185284d9c625SLionel Sambuc ++ecp->cp;
185384d9c625SLionel Sambuc --ecp->clen;
185484d9c625SLionel Sambuc break; /* Absolute line number. */
185584d9c625SLionel Sambuc case '0': case '1': case '2': case '3': case '4':
185684d9c625SLionel Sambuc case '5': case '6': case '7': case '8': case '9':
185784d9c625SLionel Sambuc *isaddrp = 1;
185884d9c625SLionel Sambuc F_SET(ecp, E_ABSMARK);
185984d9c625SLionel Sambuc
186084d9c625SLionel Sambuc if ((nret = nget_uslong(sp, &uval, ecp->cp, &endp, 10)) != NUM_OK) {
186184d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_NOTSET, nret);
186284d9c625SLionel Sambuc *errp = 1;
186384d9c625SLionel Sambuc return (0);
186484d9c625SLionel Sambuc }
186584d9c625SLionel Sambuc if (!NPFITS(DB_MAX_RECORDS, 0, uval)) {
186684d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
186784d9c625SLionel Sambuc *errp = 1;
186884d9c625SLionel Sambuc return (0);
186984d9c625SLionel Sambuc }
187084d9c625SLionel Sambuc mp->lno = uval;
187184d9c625SLionel Sambuc mp->cno = 0;
187284d9c625SLionel Sambuc ecp->clen -= (endp - ecp->cp);
187384d9c625SLionel Sambuc ecp->cp = endp;
187484d9c625SLionel Sambuc break;
187584d9c625SLionel Sambuc case '\'': /* Use a mark. */
187684d9c625SLionel Sambuc *isaddrp = 1;
187784d9c625SLionel Sambuc F_SET(ecp, E_ABSMARK);
187884d9c625SLionel Sambuc
187984d9c625SLionel Sambuc if (ecp->clen == 1) {
188084d9c625SLionel Sambuc msgq(sp, M_ERR, "095|No mark name supplied");
188184d9c625SLionel Sambuc *errp = 1;
188284d9c625SLionel Sambuc return (0);
188384d9c625SLionel Sambuc }
188484d9c625SLionel Sambuc if (mark_get(sp, ecp->cp[1], mp, M_ERR)) {
188584d9c625SLionel Sambuc *errp = 1;
188684d9c625SLionel Sambuc return (0);
188784d9c625SLionel Sambuc }
188884d9c625SLionel Sambuc ecp->cp += 2;
188984d9c625SLionel Sambuc ecp->clen -= 2;
189084d9c625SLionel Sambuc break;
189184d9c625SLionel Sambuc case '\\': /* Search: forward/backward. */
189284d9c625SLionel Sambuc /*
189384d9c625SLionel Sambuc * !!!
189484d9c625SLionel Sambuc * I can't find any difference between // and \/ or between
189584d9c625SLionel Sambuc * ?? and \?. Mark Horton doesn't remember there being any
189684d9c625SLionel Sambuc * difference. C'est la vie.
189784d9c625SLionel Sambuc */
189884d9c625SLionel Sambuc if (ecp->clen < 2 ||
189984d9c625SLionel Sambuc (ecp->cp[1] != '/' && ecp->cp[1] != '?')) {
190084d9c625SLionel Sambuc msgq(sp, M_ERR, "096|\\ not followed by / or ?");
190184d9c625SLionel Sambuc *errp = 1;
190284d9c625SLionel Sambuc return (0);
190384d9c625SLionel Sambuc }
190484d9c625SLionel Sambuc ++ecp->cp;
190584d9c625SLionel Sambuc --ecp->clen;
190684d9c625SLionel Sambuc sf = ecp->cp[0] == '/' ? f_search : b_search;
190784d9c625SLionel Sambuc goto search;
190884d9c625SLionel Sambuc case '/': /* Search forward. */
190984d9c625SLionel Sambuc sf = f_search;
191084d9c625SLionel Sambuc goto search;
191184d9c625SLionel Sambuc case '?': /* Search backward. */
191284d9c625SLionel Sambuc sf = b_search;
191384d9c625SLionel Sambuc
191484d9c625SLionel Sambuc search: mp->lno = sp->lno;
191584d9c625SLionel Sambuc mp->cno = sp->cno;
191684d9c625SLionel Sambuc if (sf(sp, mp, mp, ecp->cp, ecp->clen, &endp,
191784d9c625SLionel Sambuc SEARCH_MSG | SEARCH_PARSE | SEARCH_SET |
191884d9c625SLionel Sambuc (F_ISSET(ecp, E_SEARCH_WMSG) ? SEARCH_WMSG : 0))) {
191984d9c625SLionel Sambuc *errp = 1;
192084d9c625SLionel Sambuc return (0);
192184d9c625SLionel Sambuc }
192284d9c625SLionel Sambuc
192384d9c625SLionel Sambuc /* Fix up the command pointers. */
192484d9c625SLionel Sambuc ecp->clen -= (endp - ecp->cp);
192584d9c625SLionel Sambuc ecp->cp = endp;
192684d9c625SLionel Sambuc
192784d9c625SLionel Sambuc *isaddrp = 1;
192884d9c625SLionel Sambuc F_SET(ecp, E_ABSMARK);
192984d9c625SLionel Sambuc break;
193084d9c625SLionel Sambuc case '.': /* Current position. */
193184d9c625SLionel Sambuc *isaddrp = 1;
193284d9c625SLionel Sambuc mp->cno = sp->cno;
193384d9c625SLionel Sambuc
193484d9c625SLionel Sambuc /* If an empty file, then '.' is 0, not 1. */
193584d9c625SLionel Sambuc if (sp->lno == 1) {
193684d9c625SLionel Sambuc if (db_last(sp, &mp->lno))
193784d9c625SLionel Sambuc return (1);
193884d9c625SLionel Sambuc if (mp->lno != 0)
193984d9c625SLionel Sambuc mp->lno = 1;
194084d9c625SLionel Sambuc } else
194184d9c625SLionel Sambuc mp->lno = sp->lno;
194284d9c625SLionel Sambuc
194384d9c625SLionel Sambuc /*
194484d9c625SLionel Sambuc * !!!
194584d9c625SLionel Sambuc * Historically, .<number> was the same as .+<number>, i.e.
194684d9c625SLionel Sambuc * the '+' could be omitted. (This feature is found in ed
194784d9c625SLionel Sambuc * as well.)
194884d9c625SLionel Sambuc */
194984d9c625SLionel Sambuc if (ecp->clen > 1 && ISDIGIT((UCHAR_T)ecp->cp[1]))
195084d9c625SLionel Sambuc *ecp->cp = '+';
195184d9c625SLionel Sambuc else {
195284d9c625SLionel Sambuc ++ecp->cp;
195384d9c625SLionel Sambuc --ecp->clen;
195484d9c625SLionel Sambuc }
195584d9c625SLionel Sambuc break;
195684d9c625SLionel Sambuc }
195784d9c625SLionel Sambuc
195884d9c625SLionel Sambuc /* Skip trailing <blank>s. */
195984d9c625SLionel Sambuc for (; ecp->clen > 0 &&
196084d9c625SLionel Sambuc ISBLANK((UCHAR_T)ecp->cp[0]); ++ecp->cp, --ecp->clen);
196184d9c625SLionel Sambuc
196284d9c625SLionel Sambuc /*
196384d9c625SLionel Sambuc * Evaluate any offset. If no address yet found, the offset
196484d9c625SLionel Sambuc * is relative to ".".
196584d9c625SLionel Sambuc */
196684d9c625SLionel Sambuc total = 0;
196784d9c625SLionel Sambuc if (ecp->clen != 0 && (ISDIGIT((UCHAR_T)ecp->cp[0]) ||
196884d9c625SLionel Sambuc ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
196984d9c625SLionel Sambuc ecp->cp[0] == '^')) {
197084d9c625SLionel Sambuc if (!*isaddrp) {
197184d9c625SLionel Sambuc *isaddrp = 1;
197284d9c625SLionel Sambuc mp->lno = sp->lno;
197384d9c625SLionel Sambuc mp->cno = sp->cno;
197484d9c625SLionel Sambuc }
197584d9c625SLionel Sambuc /*
197684d9c625SLionel Sambuc * Evaluate an offset, defined as:
197784d9c625SLionel Sambuc *
197884d9c625SLionel Sambuc * [+-^<blank>]*[<blank>]*[0-9]*
197984d9c625SLionel Sambuc *
198084d9c625SLionel Sambuc * The rough translation is any number of signs, optionally
198184d9c625SLionel Sambuc * followed by numbers, or a number by itself, all <blank>
198284d9c625SLionel Sambuc * separated.
198384d9c625SLionel Sambuc *
198484d9c625SLionel Sambuc * !!!
198584d9c625SLionel Sambuc * All address offsets were additive, e.g. "2 2 3p" was the
198684d9c625SLionel Sambuc * same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2".
198784d9c625SLionel Sambuc * Note, however, "2 /ZZZ/" was an error. It was also legal
198884d9c625SLionel Sambuc * to insert signs without numbers, so "3 - 2" was legal, and
198984d9c625SLionel Sambuc * equal to 4.
199084d9c625SLionel Sambuc *
199184d9c625SLionel Sambuc * !!!
199284d9c625SLionel Sambuc * Offsets were historically permitted for any line address,
199384d9c625SLionel Sambuc * e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after
199484d9c625SLionel Sambuc * line 8.
199584d9c625SLionel Sambuc *
199684d9c625SLionel Sambuc * !!!
199784d9c625SLionel Sambuc * Offsets were historically permitted for search commands,
199884d9c625SLionel Sambuc * and handled as addresses: "/pattern/2 2 2" was legal, and
199984d9c625SLionel Sambuc * referenced the 6th line after pattern.
200084d9c625SLionel Sambuc */
200184d9c625SLionel Sambuc F_SET(ecp, E_DELTA);
200284d9c625SLionel Sambuc for (;;) {
200384d9c625SLionel Sambuc for (; ecp->clen > 0 && ISBLANK((UCHAR_T)ecp->cp[0]);
200484d9c625SLionel Sambuc ++ecp->cp, --ecp->clen);
200584d9c625SLionel Sambuc if (ecp->clen == 0 || (!ISDIGIT((UCHAR_T)ecp->cp[0]) &&
200684d9c625SLionel Sambuc ecp->cp[0] != '+' && ecp->cp[0] != '-' &&
200784d9c625SLionel Sambuc ecp->cp[0] != '^'))
200884d9c625SLionel Sambuc break;
200984d9c625SLionel Sambuc if (!ISDIGIT((UCHAR_T)ecp->cp[0]) &&
201084d9c625SLionel Sambuc !ISDIGIT((UCHAR_T)ecp->cp[1])) {
201184d9c625SLionel Sambuc total += ecp->cp[0] == '+' ? 1 : -1;
201284d9c625SLionel Sambuc --ecp->clen;
201384d9c625SLionel Sambuc ++ecp->cp;
201484d9c625SLionel Sambuc } else {
201584d9c625SLionel Sambuc if (ecp->cp[0] == '-' ||
201684d9c625SLionel Sambuc ecp->cp[0] == '^') {
201784d9c625SLionel Sambuc ++ecp->cp;
201884d9c625SLionel Sambuc --ecp->clen;
201984d9c625SLionel Sambuc isneg = 1;
202084d9c625SLionel Sambuc } else
202184d9c625SLionel Sambuc isneg = 0;
202284d9c625SLionel Sambuc
202384d9c625SLionel Sambuc /* Get a signed long, add it to the total. */
202484d9c625SLionel Sambuc if ((nret = nget_slong(sp, &val,
202584d9c625SLionel Sambuc ecp->cp, &endp, 10)) != NUM_OK ||
202684d9c625SLionel Sambuc (nret = NADD_SLONG(sp,
202784d9c625SLionel Sambuc total, val)) != NUM_OK) {
202884d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_NOTSET, nret);
202984d9c625SLionel Sambuc *errp = 1;
203084d9c625SLionel Sambuc return (0);
203184d9c625SLionel Sambuc }
203284d9c625SLionel Sambuc total += isneg ? -val : val;
203384d9c625SLionel Sambuc ecp->clen -= (endp - ecp->cp);
203484d9c625SLionel Sambuc ecp->cp = endp;
203584d9c625SLionel Sambuc }
203684d9c625SLionel Sambuc }
203784d9c625SLionel Sambuc }
203884d9c625SLionel Sambuc
203984d9c625SLionel Sambuc /*
204084d9c625SLionel Sambuc * Any value less than 0 is an error. Make sure that the new value
204184d9c625SLionel Sambuc * will fit into a db_recno_t.
204284d9c625SLionel Sambuc */
204384d9c625SLionel Sambuc if (*isaddrp && total != 0) {
204484d9c625SLionel Sambuc if (total < 0) {
204584d9c625SLionel Sambuc if ((db_recno_t)-total > mp->lno) {
204684d9c625SLionel Sambuc msgq(sp, M_ERR,
204784d9c625SLionel Sambuc "097|Reference to a line number less than 0");
204884d9c625SLionel Sambuc *errp = 1;
204984d9c625SLionel Sambuc return (0);
205084d9c625SLionel Sambuc }
205184d9c625SLionel Sambuc } else
205284d9c625SLionel Sambuc if (!NPFITS(DB_MAX_RECORDS, mp->lno, (unsigned long)total)) {
205384d9c625SLionel Sambuc ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
205484d9c625SLionel Sambuc *errp = 1;
205584d9c625SLionel Sambuc return (0);
205684d9c625SLionel Sambuc }
205784d9c625SLionel Sambuc mp->lno += total;
205884d9c625SLionel Sambuc }
205984d9c625SLionel Sambuc return (0);
206084d9c625SLionel Sambuc }
206184d9c625SLionel Sambuc
206284d9c625SLionel Sambuc
206384d9c625SLionel Sambuc /*
206484d9c625SLionel Sambuc * ex_load --
206584d9c625SLionel Sambuc * Load up the next command, which may be an @ buffer or global command.
206684d9c625SLionel Sambuc */
206784d9c625SLionel Sambuc static int
ex_load(SCR * sp)206884d9c625SLionel Sambuc ex_load(SCR *sp)
206984d9c625SLionel Sambuc {
207084d9c625SLionel Sambuc WIN *wp;
207184d9c625SLionel Sambuc EXCMD *ecp;
207284d9c625SLionel Sambuc RANGE *rp;
207384d9c625SLionel Sambuc
207484d9c625SLionel Sambuc F_CLR(sp, SC_EX_GLOBAL);
207584d9c625SLionel Sambuc
207684d9c625SLionel Sambuc /*
207784d9c625SLionel Sambuc * Lose any exhausted commands. We know that the first command
207884d9c625SLionel Sambuc * can't be an AGV command, which makes things a bit easier.
207984d9c625SLionel Sambuc */
208084d9c625SLionel Sambuc for (wp = sp->wp;;) {
208184d9c625SLionel Sambuc /*
208284d9c625SLionel Sambuc * If we're back to the original structure, leave it around,
208384d9c625SLionel Sambuc * but discard any allocated source name, we've returned to
208484d9c625SLionel Sambuc * the beginning of the command stack.
208584d9c625SLionel Sambuc */
208684d9c625SLionel Sambuc if ((ecp = LIST_FIRST(&wp->ecq)) == &wp->excmd) {
208784d9c625SLionel Sambuc if (F_ISSET(ecp, E_NAMEDISCARD)) {
208884d9c625SLionel Sambuc free(ecp->if_name);
208984d9c625SLionel Sambuc ecp->if_name = NULL;
209084d9c625SLionel Sambuc }
209184d9c625SLionel Sambuc return (0);
209284d9c625SLionel Sambuc }
209384d9c625SLionel Sambuc
209484d9c625SLionel Sambuc /*
209584d9c625SLionel Sambuc * ecp->clen will be 0 for the first discarded command, but
209684d9c625SLionel Sambuc * may not be 0 for subsequent ones, e.g. if the original
209784d9c625SLionel Sambuc * command was ":g/xx/@a|s/b/c/", then when we discard the
209884d9c625SLionel Sambuc * command pushed on the stack by the @a, we have to resume
209984d9c625SLionel Sambuc * the global command which included the substitute command.
210084d9c625SLionel Sambuc */
210184d9c625SLionel Sambuc if (ecp->clen != 0)
210284d9c625SLionel Sambuc return (0);
210384d9c625SLionel Sambuc
210484d9c625SLionel Sambuc /*
210584d9c625SLionel Sambuc * If it's an @, global or v command, we may need to continue
210684d9c625SLionel Sambuc * the command on a different line.
210784d9c625SLionel Sambuc */
210884d9c625SLionel Sambuc if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
210984d9c625SLionel Sambuc /* Discard any exhausted ranges. */
211084d9c625SLionel Sambuc while ((rp = TAILQ_FIRST(&ecp->rq)) != NULL)
211184d9c625SLionel Sambuc if (rp->start > rp->stop) {
211284d9c625SLionel Sambuc TAILQ_REMOVE(&ecp->rq, rp, q);
211384d9c625SLionel Sambuc free(rp);
211484d9c625SLionel Sambuc } else
211584d9c625SLionel Sambuc break;
211684d9c625SLionel Sambuc
211784d9c625SLionel Sambuc /* If there's another range, continue with it. */
211884d9c625SLionel Sambuc if (rp != NULL)
211984d9c625SLionel Sambuc break;
212084d9c625SLionel Sambuc
212184d9c625SLionel Sambuc /* If it's a global/v command, fix up the last line. */
212284d9c625SLionel Sambuc if (FL_ISSET(ecp->agv_flags,
212384d9c625SLionel Sambuc AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO) {
212484d9c625SLionel Sambuc if (db_exist(sp, ecp->range_lno))
212584d9c625SLionel Sambuc sp->lno = ecp->range_lno;
212684d9c625SLionel Sambuc else {
212784d9c625SLionel Sambuc if (db_last(sp, &sp->lno))
212884d9c625SLionel Sambuc return (1);
212984d9c625SLionel Sambuc if (sp->lno == 0)
213084d9c625SLionel Sambuc sp->lno = 1;
213184d9c625SLionel Sambuc }
213284d9c625SLionel Sambuc }
213384d9c625SLionel Sambuc free(ecp->o_cp);
213484d9c625SLionel Sambuc }
213584d9c625SLionel Sambuc
213684d9c625SLionel Sambuc /* Discard the EXCMD. */
213784d9c625SLionel Sambuc LIST_REMOVE(ecp, q);
213884d9c625SLionel Sambuc free(ecp);
213984d9c625SLionel Sambuc }
214084d9c625SLionel Sambuc
214184d9c625SLionel Sambuc /*
214284d9c625SLionel Sambuc * We only get here if it's an active @, global or v command. Set
214384d9c625SLionel Sambuc * the current line number, and get a new copy of the command for
214484d9c625SLionel Sambuc * the parser. Note, the original pointer almost certainly moved,
214584d9c625SLionel Sambuc * so we have play games.
214684d9c625SLionel Sambuc */
214784d9c625SLionel Sambuc ecp->cp = ecp->o_cp;
214884d9c625SLionel Sambuc MEMCPYW(ecp->cp, ecp->cp + ecp->o_clen, ecp->o_clen);
214984d9c625SLionel Sambuc ecp->clen = ecp->o_clen;
215084d9c625SLionel Sambuc ecp->range_lno = sp->lno = rp->start++;
215184d9c625SLionel Sambuc
215284d9c625SLionel Sambuc if (FL_ISSET(ecp->agv_flags, AGV_GLOBAL | AGV_V))
215384d9c625SLionel Sambuc F_SET(sp, SC_EX_GLOBAL);
215484d9c625SLionel Sambuc return (0);
215584d9c625SLionel Sambuc }
215684d9c625SLionel Sambuc
215784d9c625SLionel Sambuc /*
215884d9c625SLionel Sambuc * ex_discard --
215984d9c625SLionel Sambuc * Discard any pending ex commands.
216084d9c625SLionel Sambuc */
216184d9c625SLionel Sambuc static int
ex_discard(SCR * sp)216284d9c625SLionel Sambuc ex_discard(SCR *sp)
216384d9c625SLionel Sambuc {
216484d9c625SLionel Sambuc WIN *wp;
216584d9c625SLionel Sambuc EXCMD *ecp;
216684d9c625SLionel Sambuc RANGE *rp;
216784d9c625SLionel Sambuc
216884d9c625SLionel Sambuc /*
216984d9c625SLionel Sambuc * We know the first command can't be an AGV command, so we don't
217084d9c625SLionel Sambuc * process it specially. We do, however, nail the command itself.
217184d9c625SLionel Sambuc */
217284d9c625SLionel Sambuc for (wp = sp->wp; (ecp = LIST_FIRST(&wp->ecq)) != &wp->excmd;) {
217384d9c625SLionel Sambuc if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
217484d9c625SLionel Sambuc while ((rp = TAILQ_FIRST(&ecp->rq)) != NULL) {
217584d9c625SLionel Sambuc TAILQ_REMOVE(&ecp->rq, rp, q);
217684d9c625SLionel Sambuc free(rp);
217784d9c625SLionel Sambuc }
217884d9c625SLionel Sambuc free(ecp->o_cp);
217984d9c625SLionel Sambuc }
218084d9c625SLionel Sambuc LIST_REMOVE(ecp, q);
218184d9c625SLionel Sambuc free(ecp);
218284d9c625SLionel Sambuc }
218384d9c625SLionel Sambuc LIST_FIRST(&wp->ecq)->clen = 0;
218484d9c625SLionel Sambuc return (0);
218584d9c625SLionel Sambuc }
218684d9c625SLionel Sambuc
218784d9c625SLionel Sambuc /*
218884d9c625SLionel Sambuc * ex_unknown --
218984d9c625SLionel Sambuc * Display an unknown command name.
219084d9c625SLionel Sambuc */
219184d9c625SLionel Sambuc static void
ex_unknown(SCR * sp,CHAR_T * cmd,size_t len)219284d9c625SLionel Sambuc ex_unknown(SCR *sp, CHAR_T *cmd, size_t len)
219384d9c625SLionel Sambuc {
219484d9c625SLionel Sambuc size_t blen;
219584d9c625SLionel Sambuc CHAR_T *bp;
219684d9c625SLionel Sambuc
219784d9c625SLionel Sambuc GET_SPACE_GOTOW(sp, bp, blen, len + 1);
219884d9c625SLionel Sambuc bp[len] = '\0';
219984d9c625SLionel Sambuc MEMCPYW(bp, cmd, len);
220084d9c625SLionel Sambuc msgq_wstr(sp, M_ERR, bp, "098|The %s command is unknown");
220184d9c625SLionel Sambuc FREE_SPACEW(sp, bp, blen);
220284d9c625SLionel Sambuc
220384d9c625SLionel Sambuc alloc_err:
220484d9c625SLionel Sambuc return;
220584d9c625SLionel Sambuc }
220684d9c625SLionel Sambuc
220784d9c625SLionel Sambuc /*
220884d9c625SLionel Sambuc * ex_is_abbrev -
220984d9c625SLionel Sambuc * The vi text input routine needs to know if ex thinks this is an
221084d9c625SLionel Sambuc * [un]abbreviate command, so it can turn off abbreviations. See
221184d9c625SLionel Sambuc * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
221284d9c625SLionel Sambuc *
221384d9c625SLionel Sambuc * PUBLIC: int ex_is_abbrev __P((SCR *, CHAR_T *, size_t));
221484d9c625SLionel Sambuc */
221584d9c625SLionel Sambuc int
ex_is_abbrev(SCR * sp,CHAR_T * name,size_t len)221684d9c625SLionel Sambuc ex_is_abbrev(SCR *sp, CHAR_T *name, size_t len)
221784d9c625SLionel Sambuc {
221884d9c625SLionel Sambuc EXCMDLIST const *cp;
221984d9c625SLionel Sambuc
222084d9c625SLionel Sambuc return ((cp = ex_comm_search(sp, name, len)) != NULL &&
222184d9c625SLionel Sambuc (cp == &cmds[C_ABBR] || cp == &cmds[C_UNABBREVIATE]));
222284d9c625SLionel Sambuc }
222384d9c625SLionel Sambuc
222484d9c625SLionel Sambuc /*
222584d9c625SLionel Sambuc * ex_is_unmap -
222684d9c625SLionel Sambuc * The vi text input routine needs to know if ex thinks this is an
222784d9c625SLionel Sambuc * unmap command, so it can turn off input mapping. See the usual
222884d9c625SLionel Sambuc * ranting in the vi/v_txt_ev.c:txt_unmap() routine.
222984d9c625SLionel Sambuc *
223084d9c625SLionel Sambuc * PUBLIC: int ex_is_unmap __P((SCR *, CHAR_T *, size_t));
223184d9c625SLionel Sambuc */
223284d9c625SLionel Sambuc int
ex_is_unmap(SCR * sp,CHAR_T * name,size_t len)223384d9c625SLionel Sambuc ex_is_unmap(SCR *sp, CHAR_T *name, size_t len)
223484d9c625SLionel Sambuc {
223584d9c625SLionel Sambuc EXCMDLIST const *cp;
223684d9c625SLionel Sambuc
223784d9c625SLionel Sambuc /*
223884d9c625SLionel Sambuc * The command the vi input routines are really interested in
223984d9c625SLionel Sambuc * is "unmap!", not just unmap.
224084d9c625SLionel Sambuc */
224184d9c625SLionel Sambuc if (name[len - 1] != '!')
224284d9c625SLionel Sambuc return (0);
224384d9c625SLionel Sambuc --len;
224484d9c625SLionel Sambuc return ((cp = ex_comm_search(sp, name, len)) != NULL &&
224584d9c625SLionel Sambuc cp == &cmds[C_UNMAP]);
224684d9c625SLionel Sambuc }
224784d9c625SLionel Sambuc
224884d9c625SLionel Sambuc /*
224984d9c625SLionel Sambuc * ex_comm_search --
225084d9c625SLionel Sambuc * Search for a command name.
225184d9c625SLionel Sambuc */
225284d9c625SLionel Sambuc static EXCMDLIST const *
ex_comm_search(SCR * sp,CHAR_T * name,size_t len)225384d9c625SLionel Sambuc ex_comm_search(SCR *sp, CHAR_T *name, size_t len)
225484d9c625SLionel Sambuc {
225584d9c625SLionel Sambuc EXCMDLIST const *cp;
225684d9c625SLionel Sambuc
225784d9c625SLionel Sambuc for (cp = cmds; cp->name != NULL; ++cp) {
225884d9c625SLionel Sambuc if (cp->name[0] > name[0])
225984d9c625SLionel Sambuc return (NULL);
226084d9c625SLionel Sambuc if (cp->name[0] != name[0])
226184d9c625SLionel Sambuc continue;
226284d9c625SLionel Sambuc if (!MEMCMP(name, cp->name, len))
226384d9c625SLionel Sambuc return (cp);
226484d9c625SLionel Sambuc }
226584d9c625SLionel Sambuc return (NULL);
226684d9c625SLionel Sambuc }
226784d9c625SLionel Sambuc
226884d9c625SLionel Sambuc /*
226984d9c625SLionel Sambuc * ex_badaddr --
227084d9c625SLionel Sambuc * Display a bad address message.
227184d9c625SLionel Sambuc *
227284d9c625SLionel Sambuc * PUBLIC: void ex_badaddr
227384d9c625SLionel Sambuc * PUBLIC: __P((SCR *, EXCMDLIST const *, enum badaddr, enum nresult));
227484d9c625SLionel Sambuc */
227584d9c625SLionel Sambuc void
ex_badaddr(SCR * sp,const EXCMDLIST * cp,enum badaddr ba,enum nresult nret)227684d9c625SLionel Sambuc ex_badaddr(SCR *sp, const EXCMDLIST *cp, enum badaddr ba, enum nresult nret)
227784d9c625SLionel Sambuc {
227884d9c625SLionel Sambuc db_recno_t lno;
227984d9c625SLionel Sambuc
228084d9c625SLionel Sambuc switch (nret) {
228184d9c625SLionel Sambuc case NUM_OK:
228284d9c625SLionel Sambuc break;
228384d9c625SLionel Sambuc case NUM_ERR:
228484d9c625SLionel Sambuc msgq(sp, M_SYSERR, NULL);
228584d9c625SLionel Sambuc return;
228684d9c625SLionel Sambuc case NUM_OVER:
228784d9c625SLionel Sambuc msgq(sp, M_ERR, "099|Address value overflow");
228884d9c625SLionel Sambuc return;
228984d9c625SLionel Sambuc case NUM_UNDER:
229084d9c625SLionel Sambuc msgq(sp, M_ERR, "100|Address value underflow");
229184d9c625SLionel Sambuc return;
229284d9c625SLionel Sambuc }
229384d9c625SLionel Sambuc
229484d9c625SLionel Sambuc /*
229584d9c625SLionel Sambuc * When encountering an address error, tell the user if there's no
229684d9c625SLionel Sambuc * underlying file, that's the real problem.
229784d9c625SLionel Sambuc */
229884d9c625SLionel Sambuc if (sp->ep == NULL) {
229984d9c625SLionel Sambuc ex_wemsg(sp, cp ? cp->name : NULL, EXM_NOFILEYET);
230084d9c625SLionel Sambuc return;
230184d9c625SLionel Sambuc }
230284d9c625SLionel Sambuc
230384d9c625SLionel Sambuc switch (ba) {
230484d9c625SLionel Sambuc case A_COMBO:
230584d9c625SLionel Sambuc msgq(sp, M_ERR, "101|Illegal address combination");
230684d9c625SLionel Sambuc break;
230784d9c625SLionel Sambuc case A_EOF:
230884d9c625SLionel Sambuc if (db_last(sp, &lno))
230984d9c625SLionel Sambuc return;
231084d9c625SLionel Sambuc if (lno != 0) {
231184d9c625SLionel Sambuc msgq(sp, M_ERR,
231284d9c625SLionel Sambuc "102|Illegal address: only %lu lines in the file",
231384d9c625SLionel Sambuc (unsigned long)lno);
231484d9c625SLionel Sambuc break;
231584d9c625SLionel Sambuc }
231684d9c625SLionel Sambuc /* FALLTHROUGH */
231784d9c625SLionel Sambuc case A_EMPTY:
231884d9c625SLionel Sambuc msgq(sp, M_ERR, "103|Illegal address: the file is empty");
231984d9c625SLionel Sambuc break;
232084d9c625SLionel Sambuc case A_NOTSET:
232184d9c625SLionel Sambuc abort();
232284d9c625SLionel Sambuc /* NOTREACHED */
232384d9c625SLionel Sambuc case A_ZERO:
232484d9c625SLionel Sambuc msgq_wstr(sp, M_ERR, cp->name,
232584d9c625SLionel Sambuc "104|The %s command doesn't permit an address of 0");
232684d9c625SLionel Sambuc break;
232784d9c625SLionel Sambuc }
232884d9c625SLionel Sambuc return;
232984d9c625SLionel Sambuc }
233084d9c625SLionel Sambuc
233184d9c625SLionel Sambuc #if defined(DEBUG) && defined(COMLOG)
233284d9c625SLionel Sambuc /*
233384d9c625SLionel Sambuc * ex_comlog --
233484d9c625SLionel Sambuc * Log ex commands.
233584d9c625SLionel Sambuc */
233684d9c625SLionel Sambuc static void
ex_comlog(sp,ecp)233784d9c625SLionel Sambuc ex_comlog(sp, ecp)
233884d9c625SLionel Sambuc SCR *sp;
233984d9c625SLionel Sambuc EXCMD *ecp;
234084d9c625SLionel Sambuc {
234184d9c625SLionel Sambuc vtrace(sp, "ecmd: %s", ecp->cmd->name);
234284d9c625SLionel Sambuc if (ecp->addrcnt > 0) {
234384d9c625SLionel Sambuc vtrace(sp, " a1 %d", ecp->addr1.lno);
234484d9c625SLionel Sambuc if (ecp->addrcnt > 1)
234584d9c625SLionel Sambuc vtrace(sp, " a2: %d", ecp->addr2.lno);
234684d9c625SLionel Sambuc }
234784d9c625SLionel Sambuc if (ecp->lineno)
234884d9c625SLionel Sambuc vtrace(sp, " line %d", ecp->lineno);
234984d9c625SLionel Sambuc if (ecp->flags)
235084d9c625SLionel Sambuc vtrace(sp, " flags 0x%x", ecp->flags);
235184d9c625SLionel Sambuc if (F_ISSET(&exc, E_BUFFER))
235284d9c625SLionel Sambuc vtrace(sp, " buffer "WC, ecp->buffer);
235384d9c625SLionel Sambuc if (ecp->argc)
235484d9c625SLionel Sambuc for (cnt = 0; cnt < ecp->argc; ++cnt)
235584d9c625SLionel Sambuc vtrace(sp, " arg %d: {%s}", cnt, ecp->argv[cnt]->bp);
235684d9c625SLionel Sambuc vtrace(sp, "\n");
235784d9c625SLionel Sambuc }
235884d9c625SLionel Sambuc #endif
2359