2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
26 #include "../common/common.h"
29 #if defined(DEBUG) && defined(COMLOG)
30 static void ex_comlog(SCR *, EXCMD *);
32 static EXCMDLIST const *
33 ex_comm_search(CHAR_T *, size_t);
34 static int ex_discard(SCR *);
35 static int ex_line(SCR *, EXCMD *, MARK *, int *, int *);
36 static int ex_load(SCR *);
37 static void ex_unknown(SCR *, CHAR_T *, size_t);
43 * PUBLIC: int ex(SCR **);
59 /* Start the ex screen. */
63 /* Flush any saved messages. */
64 while ((mp = SLIST_FIRST(gp->msgq)) != NULL) {
65 gp->scr_msg(sp, mp->mtype, mp->buf, mp->len);
66 SLIST_REMOVE_HEAD(gp->msgq, q);
71 /* If reading from a file, errors should have name and line info. */
72 if (F_ISSET(gp, G_SCRIPTED)) {
74 gp->excmd.if_name = "script";
79 * Initialize the text flags. The beautify edit option historically
80 * applied to ex command input read from a file. In addition, the
81 * first time a ^H was discarded from the input, there was a message,
82 * "^H discarded", that was displayed. We don't bother.
84 LF_INIT(TXT_BACKSLASH | TXT_CNTRLD | TXT_CR);
85 for (;; ++gp->excmd.if_lno) {
86 /* Display status line and flush. */
87 if (F_ISSET(sp, SC_STATUS)) {
88 if (!F_ISSET(sp, SC_EX_SILENT))
89 msgq_status(sp, sp->lno, 0);
94 /* Set the flags the user can reset. */
95 if (O_ISSET(sp, O_BEAUTIFY))
97 if (O_ISSET(sp, O_PROMPT))
100 /* Clear any current interrupts, and get a command. */
102 if (ex_txt(sp, sp->tiq, ':', flags))
104 if (INTERRUPTED(sp)) {
105 (void)ex_puts(sp, "\n");
110 /* Initialize the command structure. */
111 CLEAR_EX_PARSER(&gp->excmd);
114 * If the user entered a single carriage return, send
115 * ex_cmd() a separator -- it discards single newlines.
117 tp = TAILQ_FIRST(sp->tiq);
119 gp->excmd.cp = L(" "); /* __TK__ why not |? */
122 gp->excmd.cp = tp->lb;
123 gp->excmd.clen = tp->len;
125 F_INIT(&gp->excmd, E_NRSEP);
127 if (ex_cmd(sp) && F_ISSET(gp, G_SCRIPTED))
130 if (INTERRUPTED(sp)) {
132 msgq(sp, M_ERR, "170|Interrupted");
136 * If the last command caused a restart, or switched screens
137 * or into vi, return.
139 if (F_ISSET(gp, G_SRESTART) || F_ISSET(sp, SC_SSWITCH | SC_VI)) {
144 /* If the last command switched files, we don't care. */
145 F_CLR(sp, SC_FSWITCH);
148 * If we're exiting this screen, move to the next one. By
149 * definition, this means returning into vi, so return to the
150 * main editor loop. The ordering is careful, don't discard
151 * the contents of sp until the end.
153 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
154 if (file_end(sp, NULL, F_ISSET(sp, SC_EXIT_FORCE)))
156 *spp = screen_next(sp);
157 return (screen_end(sp));
165 * The guts of the ex parser: parse and execute a string containing
169 * This code MODIFIES the string that gets passed in, to delete quoting
170 * characters, etc. The string cannot be readonly/text space, nor should
171 * you expect to use it again after ex_cmd() returns.
174 * For the fun of it, if you want to see if a vi clone got the ex argument
175 * parsing right, try:
177 * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
179 * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
182 * :set|file|append|set|file
184 * For extra credit, try them in a startup .exrc file.
186 * PUBLIC: int ex_cmd(SCR *);
197 size_t arg1_len, discard, len;
200 int at_found, gv_found;
201 int cnt, delim, isaddr, namelen;
202 int newscreen, notempty, tmp, vi_address;
203 CHAR_T *arg1, *s, *p, *t;
212 * We always start running the command on the top of the stack.
213 * This means that *everything* must be resolved when we leave
214 * this function for any reason.
216 loop: ecp = SLIST_FIRST(gp->ecq);
218 /* If we're reading a command from a file, set up error information. */
219 if (ecp->if_name != NULL) {
220 gp->if_lno = ecp->if_lno;
221 gp->if_name = ecp->if_name;
225 * If a move to the end of the file is scheduled for this command,
228 if (F_ISSET(ecp, E_MOVETOEND)) {
229 if (db_last(sp, &sp->lno))
232 F_CLR(ecp, E_MOVETOEND);
235 /* If we found a newline, increment the count now. */
236 if (F_ISSET(ecp, E_NEWLINE)) {
239 F_CLR(ecp, E_NEWLINE);
242 /* (Re)initialize the EXCMD structure, preserving some flags. */
245 /* Initialize the argument structures. */
246 if (argv_init(sp, ecp))
249 /* Initialize +cmd, saved command information. */
251 ecp->save_cmdlen = 0;
253 /* Skip <blank>s, empty lines. */
254 for (notempty = 0; ecp->clen > 0; ++ecp->cp, --ecp->clen)
255 if ((ch = *ecp->cp) == '\n') {
258 } else if (cmdskip(ch))
265 * Permit extra colons at the start of the line. Historically,
266 * ex/vi allowed a single extra one. It's simpler not to count.
267 * The stripping is done here because, historically, any command
268 * could have preceding colons, e.g. ":g/pattern/:p" worked.
270 if (ecp->clen != 0 && ch == ':') {
272 while (--ecp->clen > 0 && (ch = *++ecp->cp) == ':');
276 * Command lines that start with a double-quote are comments.
279 * Historically, there was no escape or delimiter for a comment, e.g.
280 * :"foo|set was a single comment and nothing was output. Since nvi
281 * permits users to escape <newline> characters into command lines, we
282 * have to check for that case.
284 if (ecp->clen != 0 && ch == '"') {
285 while (--ecp->clen > 0 && *++ecp->cp != '\n');
286 if (*ecp->cp == '\n') {
287 F_SET(ecp, E_NEWLINE);
294 /* Skip whitespace. */
295 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
302 * The last point at which an empty line can mean do nothing.
305 * Historically, in ex mode, lines containing only <blank> characters
306 * were the same as a single <carriage-return>, i.e. a default command.
307 * In vi mode, they were ignored. In .exrc files this was a serious
308 * annoyance, as vi kept trying to treat them as print commands. We
309 * ignore backward compatibility in this case, discarding lines that
310 * contain only <blank> characters from .exrc files.
313 * This is where you end up when you're done a command, i.e. clen has
314 * gone to zero. Continue if there are more commands to run.
316 if (ecp->clen == 0 &&
317 (!notempty || F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_BLIGNORE))) {
320 ecp = SLIST_FIRST(gp->ecq);
327 * Check to see if this is a command for which we may want to move
328 * the cursor back up to the previous line. (The command :1<CR>
329 * wants a <newline> separator, but the command :<CR> wants to erase
330 * the command line.) If the line is empty except for <blank>s,
331 * <carriage-return> or <eof>, we'll probably want to move up. I
332 * don't think there's any way to get <blank> characters *after* the
333 * command character, but this is the ex parser, and I've been wrong
336 if (F_ISSET(ecp, E_NRSEP) &&
337 ecp->clen != 0 && (ecp->clen != 1 || ecp->cp[0] != '\004'))
340 /* Parse command addresses. */
341 if (ex_range(sp, ecp, &tmp))
347 * Skip <blank>s and any more colons (the command :3,5:print
348 * worked, historically).
350 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
352 if (!cmdskip(ch) && ch != ':')
357 * If no command, ex does the last specified of p, l, or #, and vi
358 * moves to the line. Otherwise, determine the length of the command
359 * name by looking for the first non-alphabetic character. (There
360 * are a few non-alphabetic characters in command names, but they're
361 * all single character commands.) This isn't a great test, because
362 * it means that, for the command ":e +cut.c file", we'll report that
363 * the command "cut" wasn't known. However, it makes ":e+35 file" work
367 * Historically, lines with multiple adjacent (or <blank> separated)
368 * command separators were very strange. For example, the command
369 * |||<carriage-return>, when the cursor was on line 1, displayed
370 * lines 2, 3 and 5 of the file. In addition, the command " | "
371 * would only display the line after the next line, instead of the
372 * next two lines. No ideas why. It worked reasonably when executed
373 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
374 * command for each separator.
376 #define SINGLE_CHAR_COMMANDS L("\004!#&*<=>@~")
378 if (ecp->clen != 0 && ecp->cp[0] != '|' && ecp->cp[0] != '\n') {
379 if (STRCHR(SINGLE_CHAR_COMMANDS, *ecp->cp)) {
386 ecp->clen > 0; --ecp->clen, ++ecp->cp)
387 if (!isazAZ(*ecp->cp))
389 if ((namelen = ecp->cp - p) == 0) {
390 msgq(sp, M_ERR, "080|Unknown command name");
397 * Historic vi permitted flags to immediately follow any
398 * subset of the 'delete' command, but then did not permit
399 * further arguments (flag, buffer, count). Make it work.
400 * Permit further arguments for the few shreds of dignity
403 * Adding commands that start with 'd', and match "delete"
404 * up to a l, p, +, - or # character can break this code.
407 * Capital letters beginning the command names ex, edit,
408 * next, previous, tag and visual (in vi mode) indicate the
409 * command should happen in a new screen.
414 n = cmds[C_DELETE].name; *s == *n; ++s, ++n);
415 if (s[0] == 'l' || s[0] == 'p' || s[0] == '+' ||
416 s[0] == '-' || s[0] == '^' || s[0] == '#') {
417 len = (ecp->cp - p) - (s - p);
420 ecp->rcmd = cmds[C_DELETE];
421 ecp->rcmd.syntax = "1bca1";
422 ecp->cmd = &ecp->rcmd;
426 case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
428 p[0] = tolower(p[0]);
433 * Search the table for the command.
436 * Historic vi permitted the mark to immediately follow the
437 * 'k' in the 'k' command. Make it work.
440 * Historic vi permitted any flag to follow the s command, e.g.
441 * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
442 * Since the following characters all have to be flags, i.e.
443 * alphabetics, we can let the s command routine return errors
444 * if it was some illegal command string. This code will break
445 * if an "sg" or similar command is ever added. The substitute
446 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
447 * follows the 's', but we limit the choices here to "cgr" so
448 * that we get unknown command messages for wrong combinations.
450 if ((ecp->cmd = ex_comm_search(p, namelen)) == NULL)
454 ecp->cp -= namelen - 1;
455 ecp->clen += namelen - 1;
456 ecp->cmd = &cmds[C_K];
461 for (s = p + 1, cnt = namelen; --cnt; ++s)
463 s[0] != 'g' && s[0] != 'r')
466 ecp->cp -= namelen - 1;
467 ecp->clen += namelen - 1;
468 ecp->rcmd = cmds[C_SUBSTITUTE];
469 ecp->rcmd.fn = ex_subagain;
470 ecp->cmd = &ecp->rcmd;
475 unknown: if (newscreen)
476 p[0] = toupper(p[0]);
477 ex_unknown(sp, p, namelen);
482 * The visual command has a different syntax when called
483 * from ex than when called from a vi colon command. FMH.
484 * Make the change now, before we test for the newscreen
485 * semantic, so that we're testing the right one.
487 skip_srch: if (ecp->cmd == &cmds[C_VISUAL_EX] && F_ISSET(sp, SC_VI))
488 ecp->cmd = &cmds[C_VISUAL_VI];
492 * Historic vi permitted a capital 'P' at the beginning of
493 * any command that started with 'p'. Probably wanted the
494 * P[rint] command for backward compatibility, and the code
495 * just made Preserve and Put work by accident. Nvi uses
496 * Previous to mean previous-in-a-new-screen, so be careful.
498 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN) &&
499 (ecp->cmd == &cmds[C_PRINT] ||
500 ecp->cmd == &cmds[C_PRESERVE]))
503 /* Test for a newscreen associated with this command. */
504 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN))
507 /* Secure means no shell access. */
508 if (F_ISSET(ecp->cmd, E_SECURE) && O_ISSET(sp, O_SECURE)) {
509 ex_wemsg(sp, ecp->cmd->name, EXM_SECURE);
514 * Multiple < and > characters; another "feature". Note,
515 * The string passed to the underlying function may not be
516 * nul terminated in this case.
518 if ((ecp->cmd == &cmds[C_SHIFTL] && *p == '<') ||
519 (ecp->cmd == &cmds[C_SHIFTR] && *p == '>')) {
521 ecp->clen > 0; --ecp->clen, ++ecp->cp)
524 if (argv_exp0(sp, ecp, p, ecp->cp - p))
528 /* Set the format style flags for the next command. */
529 if (ecp->cmd == &cmds[C_HASH])
530 exp->fdef = E_C_HASH;
531 else if (ecp->cmd == &cmds[C_LIST])
532 exp->fdef = E_C_LIST;
533 else if (ecp->cmd == &cmds[C_PRINT])
534 exp->fdef = E_C_PRINT;
535 F_CLR(ecp, E_USELASTCMD);
537 /* Print is the default command. */
538 ecp->cmd = &cmds[C_PRINT];
540 /* Set the saved format flags. */
541 F_SET(ecp, exp->fdef);
545 * If no address was specified, and it's not a global command,
546 * we up the address by one. (I have no idea why globals are
547 * exempted, but it's (ahem) historic practice.)
549 if (ecp->addrcnt == 0 && !F_ISSET(sp, SC_EX_GLOBAL)) {
551 ecp->addr1.lno = sp->lno + 1;
552 ecp->addr1.cno = sp->cno;
555 F_SET(ecp, E_USELASTCMD);
560 * Historically, the number option applied to both ex and vi. One
561 * strangeness was that ex didn't switch display formats until a
562 * command was entered, e.g. <CR>'s after the set didn't change to
563 * the new format, but :1p would.
565 if (O_ISSET(sp, O_NUMBER)) {
566 F_SET(ecp, E_OPTNUM);
567 FL_SET(ecp->iflags, E_C_HASH);
569 F_CLR(ecp, E_OPTNUM);
571 /* Check for ex mode legality. */
572 if (F_ISSET(sp, SC_EX) && (F_ISSET(ecp->cmd, E_VIONLY) || newscreen)) {
573 msgq_wstr(sp, M_ERR, ecp->cmd->name,
574 "082|%s: command not available in ex mode");
578 /* Add standard command flags. */
579 F_SET(ecp, ecp->cmd->flags);
581 F_CLR(ecp, E_NEWSCREEN);
584 * There are three normal termination cases for an ex command. They
585 * are the end of the string (ecp->clen), or unescaped (by <literal
586 * next> characters) <newline> or '|' characters. As we're now past
587 * possible addresses, we can determine how long the command is, so we
588 * don't have to look for all the possible terminations. Naturally,
589 * there are some exciting special cases:
591 * 1: The bang, global, v and the filter versions of the read and
592 * write commands are delimited by <newline>s (they can contain
594 * 2: The ex, edit, next and visual in vi mode commands all take ex
595 * commands as their first arguments.
596 * 3: The s command takes an RE as its first argument, and wants it
597 * to be specially delimited.
599 * Historically, '|' characters in the first argument of the ex, edit,
600 * next, vi visual, and s commands didn't delimit the command. And,
601 * in the filter cases for read and write, and the bang, global and v
602 * commands, they did not delimit the command at all.
604 * For example, the following commands were legal:
606 * :edit +25|s/abc/ABC/ file.c
608 * :read !spell % | columnate
609 * :global/pattern/p|l
611 * It's not quite as simple as it sounds, however. The command:
615 * was also legal, i.e. the historic ex parser (using the word loosely,
616 * since "parser" implies some regularity of syntax) delimited the RE's
617 * based on its delimiter and not anything so irretrievably vulgar as a
620 * Anyhow, the following code makes this all work. First, for the
621 * special cases we move past their special argument(s). Then, we
622 * do normal command processing on whatever is left. Barf-O-Rama.
624 discard = 0; /* Characters discarded from the command. */
626 ecp->save_cmd = ecp->cp;
627 if (ecp->cmd == &cmds[C_EDIT] || ecp->cmd == &cmds[C_EX] ||
628 ecp->cmd == &cmds[C_NEXT] || ecp->cmd == &cmds[C_VISUAL_VI] ||
629 ecp->cmd == &cmds[C_VSPLIT]) {
631 * Move to the next non-whitespace character. A '!'
632 * immediately following the command is eaten as a
635 if (ecp->clen > 0 && *ecp->cp == '!') {
638 FL_SET(ecp->iflags, E_C_FORCE);
640 /* Reset, don't reparse. */
641 ecp->save_cmd = ecp->cp;
643 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
644 if (!cmdskip(*ecp->cp))
649 * The historic implementation ignored all escape characters
650 * so there was no way to put a space or newline into the +cmd
651 * field. We do a simplistic job of fixing it by moving to the
652 * first whitespace character that isn't escaped. The escaping
653 * characters are stripped as no longer useful.
655 if (ecp->clen > 0 && *ecp->cp == '+') {
658 for (arg1 = p = ecp->cp;
659 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
661 if (IS_ESCAPE(sp, ecp, ch) &&
666 } else if (cmdskip(ch))
670 arg1_len = ecp->cp - arg1;
672 /* Reset, so the first argument isn't reparsed. */
673 ecp->save_cmd = ecp->cp;
675 } else if (ecp->cmd == &cmds[C_BANG] ||
676 ecp->cmd == &cmds[C_GLOBAL] || ecp->cmd == &cmds[C_V]) {
680 * We use backslashes to escape <newline> characters, although
681 * this wasn't historic practice for the bang command. It was
682 * for the global and v commands, and it's common usage when
683 * doing text insert during the command. Escaping characters
684 * are stripped as no longer useful.
686 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
688 if (ch == '\\' && ecp->clen > 1 && ecp->cp[1] == '\n') {
695 } else if (ch == '\n')
699 } else if (ecp->cmd == &cmds[C_READ] || ecp->cmd == &cmds[C_WRITE]) {
701 * For write commands, if the next character is a <blank>, and
702 * the next non-blank character is a '!', it's a filter command
703 * and we want to eat everything up to the <newline>. For read
704 * commands, if the next non-blank character is a '!', it's a
705 * filter command and we want to eat everything up to the next
706 * <newline>. Otherwise, we're done.
708 for (tmp = 0; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
715 if (ecp->clen > 0 && ch == '!' &&
716 (ecp->cmd == &cmds[C_READ] || tmp))
717 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
718 if (ecp->cp[0] == '\n')
720 } else if (ecp->cmd == &cmds[C_SUBSTITUTE]) {
722 * Move to the next non-whitespace character, we'll use it as
723 * the delimiter. If the character isn't an alphanumeric or
724 * a '|', it's the delimiter, so parse it. Otherwise, we're
725 * into something like ":s g", so use the special s command.
727 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
728 if (!cmdskip(ecp->cp[0]))
731 if (is09azAZ(ecp->cp[0]) || ecp->cp[0] == '|') {
732 ecp->rcmd = cmds[C_SUBSTITUTE];
733 ecp->rcmd.fn = ex_subagain;
734 ecp->cmd = &ecp->rcmd;
735 } else if (ecp->clen > 0) {
739 * Backslashes quote delimiter characters for RE's.
740 * The backslashes are NOT removed since they'll be
741 * used by the RE code. Move to the third delimiter
742 * that's not escaped (or the end of the command).
747 for (cnt = 2; ecp->clen > 0 &&
748 cnt != 0; --ecp->clen, ++ecp->cp)
749 if (ecp->cp[0] == '\\' &&
753 } else if (ecp->cp[0] == delim)
759 * Use normal quoting and termination rules to find the end of this
764 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
765 * file. It was almost certainly a bug, but that's what bug-for-bug
766 * compatibility means, Grasshopper. Also, ^V's escape the command
767 * delimiters. Literal next quote characters in front of the newlines,
768 * '|' characters or literal next characters are stripped as they're
771 vi_address = ecp->clen != 0 && ecp->cp[0] != '\n';
772 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
774 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
775 CHAR_T tmp = ecp->cp[1];
776 if (tmp == '\n' || tmp == '|') {
786 } else if (ch == '\n' || ch == '|') {
788 F_SET(ecp, E_NEWLINE);
796 * Save off the next command information, go back to the
797 * original start of the command.
800 ecp->cp = ecp->save_cmd;
802 ecp->save_cmdlen = ecp->clen;
803 ecp->clen = ((ecp->save_cmd - ecp->cp) - 1) - discard;
808 * The "set tags" command historically used a backslash, not the
809 * user's literal next character, to escape whitespace. Handle
810 * it here instead of complicating the argv_exp3() code. Note,
811 * this isn't a particularly complex trap, and if backslashes were
812 * legal in set commands, this would have to be much more complicated.
814 if (ecp->cmd == &cmds[C_SET])
815 for (p = ecp->cp, len = ecp->clen; len > 0; --len, ++p)
816 if (IS_ESCAPE(sp, ecp, *p) && len > 1) {
819 } else if (*p == '\\')
823 * Set the default addresses. It's an error to specify an address for
824 * a command that doesn't take them. If two addresses are specified
825 * for a command that only takes one, lose the first one. Two special
826 * cases here, some commands take 0 or 2 addresses. For most of them
827 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
828 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
830 * Also, if the file is empty, some commands want to use an address of
831 * 0, i.e. the entire file is 0 to 0, and the default first address is
832 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
833 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
834 * case where the 0 address is only valid if it's a default address.
836 * Also, set a flag if we set the default addresses. Some commands
837 * (ex: z) care if the user specified an address or if we just used
838 * the current cursor.
840 switch (F_ISSET(ecp, E_ADDR1 | E_ADDR2 | E_ADDR2_ALL | E_ADDR2_NONE)) {
841 case E_ADDR1: /* One address: */
842 switch (ecp->addrcnt) {
843 case 0: /* Default cursor/empty file. */
845 F_SET(ecp, E_ADDR_DEF);
846 if (F_ISSET(ecp, E_ADDR_ZERODEF)) {
847 if (db_last(sp, &lno))
851 F_SET(ecp, E_ADDR_ZERO);
853 ecp->addr1.lno = sp->lno;
855 ecp->addr1.lno = sp->lno;
856 ecp->addr1.cno = sp->cno;
860 case 2: /* Lose the first address. */
862 ecp->addr1 = ecp->addr2;
865 case E_ADDR2_NONE: /* Zero/two addresses: */
866 if (ecp->addrcnt == 0) /* Default to nothing. */
869 case E_ADDR2_ALL: /* Zero/two addresses: */
870 if (ecp->addrcnt == 0) { /* Default entire/empty file. */
871 F_SET(ecp, E_ADDR_DEF);
875 else if (db_last(sp, &ecp->addr2.lno))
877 if (F_ISSET(ecp, E_ADDR_ZERODEF) &&
878 ecp->addr2.lno == 0) {
880 F_SET(ecp, E_ADDR_ZERO);
883 ecp->addr1.cno = ecp->addr2.cno = 0;
884 F_SET(ecp, E_ADDR2_ALL);
888 case E_ADDR2: /* Two addresses: */
889 two_addr: switch (ecp->addrcnt) {
890 case 0: /* Default cursor/empty file. */
892 F_SET(ecp, E_ADDR_DEF);
894 F_ISSET(ecp, E_ADDR_ZERODEF)) {
895 if (db_last(sp, &lno))
898 ecp->addr1.lno = ecp->addr2.lno = 0;
899 F_SET(ecp, E_ADDR_ZERO);
902 ecp->addr2.lno = sp->lno;
904 ecp->addr1.lno = ecp->addr2.lno = sp->lno;
905 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
907 case 1: /* Default to first address. */
909 ecp->addr2 = ecp->addr1;
916 if (ecp->addrcnt) /* Error. */
922 * The ^D scroll command historically scrolled the value of the scroll
923 * option or to EOF. It was an error if the cursor was already at EOF.
924 * (Leading addresses were permitted, but were then ignored.)
926 if (ecp->cmd == &cmds[C_SCROLL]) {
928 ecp->addr1.lno = sp->lno + 1;
929 ecp->addr2.lno = sp->lno + O_VAL(sp, O_SCROLL);
930 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
931 if (db_last(sp, &lno))
933 if (lno != 0 && lno > sp->lno && ecp->addr2.lno > lno)
934 ecp->addr2.lno = lno;
938 for (np = ecp->cmd->syntax; *np != '\0'; ++np) {
940 * The force flag is sensitive to leading whitespace, i.e.
941 * "next !" is different from "next!". Handle it before
942 * skipping leading <blank>s.
945 if (ecp->clen > 0 && *ecp->cp == '!') {
948 FL_SET(ecp->iflags, E_C_FORCE);
953 /* Skip leading <blank>s. */
954 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
955 if (!cmdskip(*ecp->cp))
961 case '1': /* +, -, #, l, p */
964 * Historically, some flags were ignored depending
965 * on where they occurred in the command line. For
966 * example, in the command, ":3+++p--#", historic vi
967 * acted on the '#' flag, but ignored the '-' flags.
968 * It's unambiguous what the flags mean, so we just
969 * handle them regardless of the stupidity of their
972 for (; ecp->clen; --ecp->clen, ++ecp->cp)
982 F_CLR(ecp, E_OPTNUM);
983 FL_SET(ecp->iflags, E_C_HASH);
984 exp->fdef |= E_C_HASH;
987 FL_SET(ecp->iflags, E_C_LIST);
988 exp->fdef |= E_C_LIST;
991 FL_SET(ecp->iflags, E_C_PRINT);
992 exp->fdef |= E_C_PRINT;
998 case '2': /* -, ., +, ^ */
999 case '3': /* -, ., +, ^, = */
1000 for (; ecp->clen; --ecp->clen, ++ecp->cp)
1003 FL_SET(ecp->iflags, E_C_DASH);
1006 FL_SET(ecp->iflags, E_C_DOT);
1009 FL_SET(ecp->iflags, E_C_PLUS);
1012 FL_SET(ecp->iflags, E_C_CARAT);
1016 FL_SET(ecp->iflags, E_C_EQUAL);
1024 case 'b': /* buffer */
1027 * Historically, "d #" was a delete with a flag, not a
1028 * delete into the '#' buffer. If the current command
1029 * permits a flag, don't use one as a buffer. However,
1030 * the 'l' and 'p' flags were legal buffer names in the
1031 * historic ex, and were used as buffers, not flags.
1033 if ((ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1034 ecp->cp[0] == '^' || ecp->cp[0] == '#') &&
1035 strchr(np, '1') != NULL)
1039 * Digits can't be buffer names in ex commands, or the
1040 * command "d2" would be a delete into buffer '2', and
1041 * not a two-line deletion.
1043 if (!ISDIGIT(ecp->cp[0])) {
1044 ecp->buffer = *ecp->cp;
1047 FL_SET(ecp->iflags, E_C_BUFFER);
1050 case 'c': /* count [01+a] */
1052 /* Validate any signed value. */
1053 if (!ISDIGIT(*ecp->cp) && (*np != '+' ||
1054 (*ecp->cp != '+' && *ecp->cp != '-')))
1056 /* If a signed value, set appropriate flags. */
1057 if (*ecp->cp == '-')
1058 FL_SET(ecp->iflags, E_C_COUNT_NEG);
1059 else if (*ecp->cp == '+')
1060 FL_SET(ecp->iflags, E_C_COUNT_POS);
1062 nget_slong(<mp, ecp->cp, &t, 10)) != NUM_OK) {
1063 ex_badaddr(sp, NULL, A_NOTSET, nret);
1066 if (ltmp == 0 && *np != '0') {
1067 msgq(sp, M_ERR, "083|Count may not be zero");
1070 ecp->clen -= (t - ecp->cp);
1074 * Counts as address offsets occur in commands taking
1075 * two addresses. Historic vi practice was to use
1076 * the count as an offset from the *second* address.
1078 * Set a count flag; some underlying commands (see
1079 * join) do different things with counts than with
1083 ecp->addr1 = ecp->addr2;
1084 ecp->addr2.lno = ecp->addr1.lno + ltmp - 1;
1087 FL_SET(ecp->iflags, E_C_COUNT);
1089 case 'f': /* file */
1090 if (argv_exp2(sp, ecp, ecp->cp, ecp->clen))
1093 case 'l': /* line */
1095 * Get a line specification.
1097 * If the line was a search expression, we may have
1098 * changed state during the call, and we're now
1099 * searching the file. Push ourselves onto the state
1102 if (ex_line(sp, ecp, &cur, &isaddr, &tmp))
1107 /* Line specifications are always required. */
1109 msgq_wstr(sp, M_ERR, ecp->cp,
1110 "084|%s: bad line specification");
1114 * The target line should exist for these commands,
1115 * but 0 is legal for them as well.
1117 if (cur.lno != 0 && !db_exist(sp, cur.lno)) {
1118 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1121 ecp->lineno = cur.lno;
1123 case 'S': /* string, file exp. */
1124 if (ecp->clen != 0) {
1125 if (argv_exp1(sp, ecp, ecp->cp,
1126 ecp->clen, ecp->cmd == &cmds[C_BANG]))
1131 case 's': /* string */
1132 if (argv_exp0(sp, ecp, ecp->cp, ecp->clen))
1135 case 'W': /* word string */
1139 * Literal next characters escape the following
1140 * character. Quoting characters are stripped here
1141 * since they are no longer useful.
1143 * First there was the word.
1145 for (p = t = ecp->cp;
1146 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
1149 ecp, ch) && ecp->clen > 1) {
1152 } else if (cmdskip(ch)) {
1159 if (argv_exp0(sp, ecp, t, p - t))
1162 /* Delete intervening whitespace. */
1163 for (; ecp->clen > 0;
1164 --ecp->clen, ++ecp->cp) {
1172 /* Followed by the string. */
1173 for (p = t = ecp->cp; ecp->clen > 0;
1174 --ecp->clen, ++ecp->cp, ++p) {
1177 ecp, ch) && ecp->clen > 1) {
1183 if (argv_exp0(sp, ecp, t, p - t))
1186 case 'w': /* word */
1187 if (argv_exp3(sp, ecp, ecp->cp, ecp->clen))
1189 arg_cnt_chk: if (*++np != 'N') { /* N */
1191 * If a number is specified, must either be
1192 * 0 or that number, if optional, and that
1193 * number, if required.
1196 if ((*++np != 'o' || exp->argsoff != 0) &&
1197 exp->argsoff != tmp)
1205 INT2CHAR(sp, ecp->cmd->name, STRLEN(ecp->cmd->name) + 1,
1208 "085|Internal syntax table error (%s: %s)",
1209 nstr, KEY_NAME(sp, *np));
1214 /* Skip trailing whitespace. */
1215 for (; ecp->clen > 0; --ecp->clen) {
1222 * There shouldn't be anything left, and no more required fields,
1223 * i.e neither 'l' or 'r' in the syntax string.
1225 if (ecp->clen != 0 || strpbrk(np, "lr")) {
1226 usage: msgq(sp, M_ERR, "086|Usage: %s", ecp->cmd->usage);
1231 * Verify that the addresses are legal. Check the addresses here,
1232 * because this is a place where all ex addresses pass through.
1233 * (They don't all pass through ex_line(), for instance.) We're
1234 * assuming that any non-existent line doesn't exist because it's
1235 * past the end-of-file. That's a pretty good guess.
1237 * If it's a "default vi command", an address of zero is okay.
1240 switch (ecp->addrcnt) {
1243 * Historic ex/vi permitted commands with counts to go past
1244 * EOF. So, for example, if the file only had 5 lines, the
1245 * ex command "1,6>" would fail, but the command ">300"
1246 * would succeed. Since we don't want to have to make all
1247 * of the underlying commands handle random line numbers,
1250 if (ecp->addr2.lno == 0) {
1251 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1252 (F_ISSET(sp, SC_EX) ||
1253 !F_ISSET(ecp, E_USELASTCMD))) {
1254 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1257 } else if (!db_exist(sp, ecp->addr2.lno))
1258 if (FL_ISSET(ecp->iflags, E_C_COUNT)) {
1259 if (db_last(sp, &lno))
1261 ecp->addr2.lno = lno;
1263 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1268 if (ecp->addr1.lno == 0) {
1269 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1270 (F_ISSET(sp, SC_EX) ||
1271 !F_ISSET(ecp, E_USELASTCMD))) {
1272 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1275 } else if (!db_exist(sp, ecp->addr1.lno)) {
1276 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1283 * If doing a default command and there's nothing left on the line,
1284 * vi just moves to the line. For example, ":3" and ":'a,'b" just
1285 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1288 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1292 * This is done before the absolute mark gets set; historically,
1293 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1295 if ((F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_NOPRDEF)) &&
1296 F_ISSET(ecp, E_USELASTCMD) && vi_address == 0) {
1297 switch (ecp->addrcnt) {
1300 (ecp->addr2.lno ? ecp->addr2.lno : 1)) {
1302 ecp->addr2.lno ? ecp->addr2.lno : 1;
1304 (void)nonblank(sp, sp->lno, &sp->cno);
1309 (ecp->addr1.lno ? ecp->addr1.lno : 1)) {
1311 ecp->addr1.lno ? ecp->addr1.lno : 1;
1313 (void)nonblank(sp, sp->lno, &sp->cno);
1317 ecp->cp = ecp->save_cmd;
1318 ecp->clen = ecp->save_cmdlen;
1323 * Set the absolute mark -- we have to set it for vi here, in case
1324 * it's a compound command, e.g. ":5p|6" should set the absolute
1327 if (F_ISSET(ecp, E_ABSMARK)) {
1330 F_CLR(ecp, E_ABSMARK);
1331 if (mark_set(sp, ABSMARK1, &cur, 1))
1335 #if defined(DEBUG) && defined(COMLOG)
1338 /* Increment the command count if not called from vi. */
1339 if (F_ISSET(sp, SC_EX))
1343 * If file state available, and not doing a global command,
1344 * log the start of an action.
1346 if (sp->ep != NULL && !F_ISSET(sp, SC_EX_GLOBAL))
1347 (void)log_cursor(sp);
1351 * There are two special commands for the purposes of this code: the
1352 * default command (<carriage-return>) or the scrolling commands (^D
1353 * and <EOF>) as the first non-<blank> characters in the line.
1355 * If this is the first command in the command line, we received the
1356 * command from the ex command loop and we're talking to a tty, and
1357 * and there's nothing else on the command line, and it's one of the
1358 * special commands, we move back up to the previous line, and erase
1359 * the prompt character with the output. Since ex runs in canonical
1360 * mode, we don't have to do anything else, a <newline> has already
1361 * been echoed by the tty driver. It's OK if vi calls us -- we won't
1362 * be in ex mode so we'll do nothing.
1364 if (F_ISSET(ecp, E_NRSEP)) {
1365 if (sp->ep != NULL &&
1366 F_ISSET(sp, SC_EX) && !F_ISSET(gp, G_SCRIPTED) &&
1367 (F_ISSET(ecp, E_USELASTCMD) || ecp->cmd == &cmds[C_SCROLL]))
1368 gp->scr_ex_adjust(sp, EX_TERM_SCROLL);
1369 F_CLR(ecp, E_NRSEP);
1373 * Call the underlying function for the ex command.
1376 * Interrupts behave like errors, for now.
1378 if (ecp->cmd->fn(sp, ecp) || INTERRUPTED(sp)) {
1379 if (F_ISSET(gp, G_SCRIPTED))
1380 F_SET(sp, SC_EXIT_FORCE);
1385 /* Make sure no function left global temporary space locked. */
1386 if (F_ISSET(gp, G_TMP_INUSE)) {
1387 F_CLR(gp, G_TMP_INUSE);
1388 msgq_wstr(sp, M_ERR, ecp->cmd->name,
1389 "087|%s: temporary buffer not released");
1393 * Ex displayed the number of lines modified immediately after each
1394 * command, so the command "1,10d|1,10d" would display:
1400 * Executing ex commands from vi only reported the final modified
1401 * lines message -- that's wrong enough that we don't match it.
1403 if (F_ISSET(sp, SC_EX))
1407 * Integrate any offset parsed by the underlying command, and make
1408 * sure the referenced line exists.
1411 * May not match historic practice (which I've never been able to
1412 * completely figure out.) For example, the '=' command from vi
1413 * mode often got the offset wrong, and complained it was too large,
1414 * but didn't seem to have a problem with the cursor. If anyone
1415 * complains, ask them how it's supposed to work, they might know.
1417 if (sp->ep != NULL && ecp->flagoff) {
1418 if (ecp->flagoff < 0) {
1419 if (sp->lno <= -ecp->flagoff) {
1421 "088|Flag offset to before line 1");
1425 if (!NPFITS(MAX_REC_NUMBER, sp->lno, ecp->flagoff)) {
1426 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1429 if (!db_exist(sp, sp->lno + ecp->flagoff)) {
1431 "089|Flag offset past end-of-file");
1435 sp->lno += ecp->flagoff;
1439 * If the command executed successfully, we may want to display a line
1440 * based on the autoprint option or an explicit print flag. (Make sure
1441 * that there's a line to display.) Also, the autoprint edit option is
1442 * turned off for the duration of global commands.
1444 if (F_ISSET(sp, SC_EX) && sp->ep != NULL && sp->lno != 0) {
1446 * The print commands have already handled the `print' flags.
1447 * If so, clear them.
1449 if (FL_ISSET(ecp->iflags, E_CLRFLAG))
1450 FL_CLR(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT);
1452 /* If hash set only because of the number option, discard it. */
1453 if (F_ISSET(ecp, E_OPTNUM))
1454 FL_CLR(ecp->iflags, E_C_HASH);
1457 * If there was an explicit flag to display the new cursor line,
1458 * or autoprint is set and a change was made, display the line.
1459 * If any print flags were set use them, else default to print.
1461 LF_INIT(FL_ISSET(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT));
1462 if (!LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT | E_NOAUTO) &&
1463 !F_ISSET(sp, SC_EX_GLOBAL) &&
1464 O_ISSET(sp, O_AUTOPRINT) && F_ISSET(ecp, E_AUTOPRINT))
1467 if (LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT)) {
1470 (void)ex_print(sp, ecp, &cur, &cur, flags);
1475 * If the command had an associated "+cmd", it has to be executed
1476 * before we finish executing any more of this ex command. For
1477 * example, consider a .exrc file that contains the following lines:
1480 * :edit +25 file.c|s/abc/ABC/|1
1483 * This can happen more than once -- the historic vi simply hung or
1484 * dropped core, of course. Prepend the + command back into the
1485 * current command and continue. We may have to add an additional
1486 * <literal next> character. We know that it will fit because we
1487 * discarded at least one space and the + character.
1489 if (arg1_len != 0) {
1491 * If the last character of the + command was a <literal next>
1492 * character, it would be treated differently because of the
1493 * append. Quote it, if necessary.
1495 if (IS_ESCAPE(sp, ecp, arg1[arg1_len - 1])) {
1496 *--ecp->save_cmd = CH_LITERAL;
1500 ecp->save_cmd -= arg1_len;
1501 ecp->save_cmdlen += arg1_len;
1502 MEMMOVE(ecp->save_cmd, arg1, arg1_len);
1505 * Any commands executed from a +cmd are executed starting at
1506 * the first column of the last line of the file -- NOT the
1507 * first nonblank.) The main file startup code doesn't know
1508 * that a +cmd was set, however, so it may have put us at the
1509 * top of the file. (Note, this is safe because we must have
1510 * switched files to get here.)
1512 F_SET(ecp, E_MOVETOEND);
1515 /* Update the current command. */
1516 ecp->cp = ecp->save_cmd;
1517 ecp->clen = ecp->save_cmdlen;
1521 * If we've changed screens or underlying files, any pending global or
1522 * v command, or @ buffer that has associated addresses, has to be
1523 * discarded. This is historic practice for globals, and necessary for
1524 * @ buffers that had associated addresses.
1526 * Otherwise, if we've changed underlying files, it's not a problem,
1527 * we continue with the rest of the ex command(s), operating on the
1528 * new file. However, if we switch screens (either by exiting or by
1529 * an explicit command), we have no way of knowing where to put output
1530 * messages, and, since we don't control screens here, we could screw
1531 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1532 * times). So, return and continue after we've got a new screen.
1534 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH)) {
1535 at_found = gv_found = 0;
1536 SLIST_FOREACH(ecp, sp->gp->ecq, q)
1537 switch (ecp->agv_flags) {
1539 case AGV_AT_NORANGE:
1545 "090|@ with range running when the file/screen changed");
1553 "091|Global/v command running when the file/screen changed");
1559 if (at_found || gv_found)
1561 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_SSWITCH))
1569 * On command failure, we discard keys and pending commands remaining,
1570 * as well as any keys that were mapped and waiting. The save_cmdlen
1571 * test is not necessarily correct. If we fail early enough we don't
1572 * know if the entire string was a single command or not. Guess, as
1573 * it's useful to know if commands other than the current one are being
1576 if (ecp->save_cmdlen == 0)
1577 for (; ecp->clen; --ecp->clen) {
1579 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
1582 } else if (ch == '\n' || ch == '|') {
1584 ecp->save_cmdlen = 1;
1588 if (ecp->save_cmdlen != 0 || SLIST_FIRST(gp->ecq) != &gp->excmd) {
1589 discard: msgq(sp, M_BERR,
1590 "092|Ex command failed: pending commands discarded");
1593 if (v_event_flush(sp, CH_MAPPED))
1595 "093|Ex command failed: mapped keys discarded");
1601 /* Turn off any file name error information. */
1604 /* Turn off the global bit. */
1605 F_CLR(sp, SC_EX_GLOBAL);
1612 * Get a line range for ex commands, or perform a vi ex address search.
1614 * PUBLIC: int ex_range(SCR *, EXCMD *, int *);
1617 ex_range(SCR *sp, EXCMD *ecp, int *errp)
1619 enum { ADDR_FOUND, ADDR_NEED, ADDR_NONE } addr;
1628 * Parse comma or semi-colon delimited line specs.
1630 * Semi-colon delimiters update the current address to be the last
1631 * address. For example, the command
1633 * :3;/pattern/ecp->cp
1635 * will search for pattern from line 3. In addition, if ecp->cp
1636 * is not a valid command, the current line will be left at 3, not
1637 * at the original address.
1639 * Extra addresses are discarded, starting with the first.
1642 * If any addresses are missing, they default to the current line.
1643 * This was historically true for both leading and trailing comma
1644 * delimited addresses as well as for trailing semicolon delimited
1645 * addresses. For consistency, we make it true for leading semicolon
1646 * addresses as well.
1650 for (addr = ADDR_NONE, ecp->addrcnt = 0; ecp->clen > 0;)
1652 case '%': /* Entire file. */
1653 /* Vi ex address searches didn't permit % signs. */
1654 if (F_ISSET(ecp, E_VISEARCH))
1657 /* It's an error if the file is empty. */
1658 if (sp->ep == NULL) {
1659 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1665 * A percent character addresses all of the lines in
1666 * the file. Historically, it couldn't be followed by
1667 * any other address. We do it as a text substitution
1668 * for simplicity. POSIX 1003.2 is expected to follow
1671 * If it's an empty file, the first line is 0, not 1.
1673 if (addr == ADDR_FOUND) {
1674 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1678 if (db_last(sp, &ecp->addr2.lno))
1680 ecp->addr1.lno = ecp->addr2.lno == 0 ? 0 : 1;
1681 ecp->addr1.cno = ecp->addr2.cno = 0;
1687 case ',': /* Comma delimiter. */
1688 /* Vi ex address searches didn't permit commas. */
1689 if (F_ISSET(ecp, E_VISEARCH))
1692 case ';': /* Semi-colon delimiter. */
1693 if (sp->ep == NULL) {
1694 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1698 if (addr != ADDR_FOUND)
1699 switch (ecp->addrcnt) {
1701 ecp->addr1.lno = sp->lno;
1702 ecp->addr1.cno = sp->cno;
1706 ecp->addr1 = ecp->addr2;
1709 ecp->addr2.lno = sp->lno;
1710 ecp->addr2.cno = sp->cno;
1714 if (*ecp->cp == ';')
1715 switch (ecp->addrcnt) {
1720 sp->lno = ecp->addr1.lno;
1721 sp->cno = ecp->addr1.cno;
1724 sp->lno = ecp->addr2.lno;
1725 sp->cno = ecp->addr2.cno;
1730 case ' ': /* Whitespace. */
1731 case '\t': /* Whitespace. */
1736 /* Get a line specification. */
1737 if (ex_line(sp, ecp, &m, &isaddr, errp))
1743 if (addr == ADDR_FOUND) {
1744 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1748 switch (ecp->addrcnt) {
1758 ecp->addr1 = ecp->addr2;
1768 * Vi ex address searches are indifferent to order or trailing
1771 ret: if (F_ISSET(ecp, E_VISEARCH))
1774 if (addr == ADDR_NEED)
1775 switch (ecp->addrcnt) {
1777 ecp->addr1.lno = sp->lno;
1778 ecp->addr1.cno = sp->cno;
1782 ecp->addr1 = ecp->addr2;
1785 ecp->addr2.lno = sp->lno;
1786 ecp->addr2.cno = sp->cno;
1791 if (ecp->addrcnt == 2 && ecp->addr2.lno < ecp->addr1.lno) {
1793 "094|The second address is smaller than the first");
1801 * Get a single line address specifier.
1803 * The way the "previous context" mark worked was that any "non-relative"
1804 * motion set it. While ex/vi wasn't totally consistent about this, ANY
1805 * numeric address, search pattern, '$', or mark reference in an address
1806 * was considered non-relative, and set the value. Which should explain
1807 * why we're hacking marks down here. The problem was that the mark was
1808 * only set if the command was called, i.e. we have to set a flag and test
1812 * This is probably still not exactly historic practice, although I think
1813 * it's fairly close.
1816 ex_line(SCR *sp, EXCMD *ecp, MARK *mp, int *isaddrp, int *errp)
1823 int (*sf)(SCR *, MARK *, MARK *, CHAR_T *, size_t, CHAR_T **, u_int);
1829 *isaddrp = *errp = 0;
1830 F_CLR(ecp, E_DELTA);
1832 /* No addresses permitted until a file has been read in. */
1833 if (sp->ep == NULL && STRCHR(L("$0123456789'\\/?.+-^"), *ecp->cp)) {
1834 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1840 case '$': /* Last line in the file. */
1842 F_SET(ecp, E_ABSMARK);
1845 if (db_last(sp, &mp->lno))
1849 break; /* Absolute line number. */
1850 case '0': case '1': case '2': case '3': case '4':
1851 case '5': case '6': case '7': case '8': case '9':
1853 F_SET(ecp, E_ABSMARK);
1855 if ((nret = nget_slong(&val, ecp->cp, &endp, 10)) != NUM_OK) {
1856 ex_badaddr(sp, NULL, A_NOTSET, nret);
1860 if (!NPFITS(MAX_REC_NUMBER, 0, val)) {
1861 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1867 ecp->clen -= (endp - ecp->cp);
1870 case '\'': /* Use a mark. */
1872 F_SET(ecp, E_ABSMARK);
1874 if (ecp->clen == 1) {
1875 msgq(sp, M_ERR, "095|No mark name supplied");
1879 if (mark_get(sp, ecp->cp[1], mp, M_ERR)) {
1886 case '\\': /* Search: forward/backward. */
1889 * I can't find any difference between // and \/ or between
1890 * ?? and \?. Mark Horton doesn't remember there being any
1891 * difference. C'est la vie.
1893 if (ecp->clen < 2 ||
1894 (ecp->cp[1] != '/' && ecp->cp[1] != '?')) {
1895 msgq(sp, M_ERR, "096|\\ not followed by / or ?");
1901 sf = ecp->cp[0] == '/' ? f_search : b_search;
1903 case '/': /* Search forward. */
1906 case '?': /* Search backward. */
1909 search: mp->lno = sp->lno;
1911 if (sf(sp, mp, mp, ecp->cp, ecp->clen, &endp,
1912 SEARCH_MSG | SEARCH_PARSE | SEARCH_SET |
1913 (F_ISSET(ecp, E_SEARCH_WMSG) ? SEARCH_WMSG : 0))) {
1918 /* Fix up the command pointers. */
1919 ecp->clen -= (endp - ecp->cp);
1923 F_SET(ecp, E_ABSMARK);
1925 case '.': /* Current position. */
1929 /* If an empty file, then '.' is 0, not 1. */
1931 if (db_last(sp, &mp->lno))
1940 * Historically, .<number> was the same as .+<number>, i.e.
1941 * the '+' could be omitted. (This feature is found in ed
1944 if (ecp->clen > 1 && ISDIGIT(ecp->cp[1]))
1953 /* Skip trailing <blank>s. */
1954 for (; ecp->clen > 0 &&
1955 cmdskip(ecp->cp[0]); ++ecp->cp, --ecp->clen);
1958 * Evaluate any offset. If no address yet found, the offset
1959 * is relative to ".".
1962 if (ecp->clen != 0 && (ISDIGIT(ecp->cp[0]) ||
1963 ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1964 ecp->cp[0] == '^')) {
1971 * Evaluate an offset, defined as:
1973 * [+-^<blank>]*[<blank>]*[0-9]*
1975 * The rough translation is any number of signs, optionally
1976 * followed by numbers, or a number by itself, all <blank>
1980 * All address offsets were additive, e.g. "2 2 3p" was the
1981 * same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2".
1982 * Note, however, "2 /ZZZ/" was an error. It was also legal
1983 * to insert signs without numbers, so "3 - 2" was legal, and
1987 * Offsets were historically permitted for any line address,
1988 * e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after
1992 * Offsets were historically permitted for search commands,
1993 * and handled as addresses: "/pattern/2 2 2" was legal, and
1994 * referenced the 6th line after pattern.
1996 F_SET(ecp, E_DELTA);
1998 for (; ecp->clen > 0 && cmdskip(ecp->cp[0]);
1999 ++ecp->cp, --ecp->clen);
2000 if (ecp->clen == 0 || (!ISDIGIT(ecp->cp[0]) &&
2001 ecp->cp[0] != '+' && ecp->cp[0] != '-' &&
2004 if (!ISDIGIT(ecp->cp[0]) &&
2005 !ISDIGIT(ecp->cp[1])) {
2006 total += ecp->cp[0] == '+' ? 1 : -1;
2010 if (ecp->cp[0] == '-' ||
2011 ecp->cp[0] == '^') {
2018 /* Get a signed long, add it to the total. */
2019 if ((nret = nget_slong(&val,
2020 ecp->cp, &endp, 10)) != NUM_OK ||
2021 (nret = NADD_SLONG(sp,
2022 total, val)) != NUM_OK) {
2023 ex_badaddr(sp, NULL, A_NOTSET, nret);
2027 total += isneg ? -val : val;
2028 ecp->clen -= (endp - ecp->cp);
2035 * Any value less than 0 is an error. Make sure that the new value
2036 * will fit into a recno_t.
2038 if (*isaddrp && total != 0) {
2040 if (-total > mp->lno) {
2042 "097|Reference to a line number less than 0");
2047 if (!NPFITS(MAX_REC_NUMBER, mp->lno, total)) {
2048 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
2060 * Load up the next command, which may be an @ buffer or global command.
2069 F_CLR(sp, SC_EX_GLOBAL);
2072 * Lose any exhausted commands. We know that the first command
2073 * can't be an AGV command, which makes things a bit easier.
2075 for (gp = sp->gp;;) {
2076 ecp = SLIST_FIRST(gp->ecq);
2078 /* Discard the allocated source name as requested. */
2079 if (F_ISSET(ecp, E_NAMEDISCARD))
2083 * If we're back to the original structure, leave it around,
2084 * since we've returned to the beginning of the command stack.
2086 if (ecp == &gp->excmd) {
2087 ecp->if_name = NULL;
2092 * ecp->clen will be 0 for the first discarded command, but
2093 * may not be 0 for subsequent ones, e.g. if the original
2094 * command was ":g/xx/@a|s/b/c/", then when we discard the
2095 * command pushed on the stack by the @a, we have to resume
2096 * the global command which included the substitute command.
2102 * If it's an @, global or v command, we may need to continue
2103 * the command on a different line.
2105 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2106 /* Discard any exhausted ranges. */
2107 while ((rp = TAILQ_FIRST(ecp->rq)) != NULL)
2108 if (rp->start > rp->stop) {
2109 TAILQ_REMOVE(ecp->rq, rp, q);
2114 /* If there's another range, continue with it. */
2118 /* If it's a global/v command, fix up the last line. */
2119 if (FL_ISSET(ecp->agv_flags,
2120 AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO)
2121 if (db_exist(sp, ecp->range_lno))
2122 sp->lno = ecp->range_lno;
2124 if (db_last(sp, &sp->lno))
2132 /* Discard the EXCMD. */
2133 SLIST_REMOVE_HEAD(gp->ecq, q);
2138 * We only get here if it's an active @, global or v command. Set
2139 * the current line number, and get a new copy of the command for
2140 * the parser. Note, the original pointer almost certainly moved,
2141 * so we have play games.
2143 ecp->cp = ecp->o_cp;
2144 MEMCPY(ecp->cp, ecp->cp + ecp->o_clen, ecp->o_clen);
2145 ecp->clen = ecp->o_clen;
2146 ecp->range_lno = sp->lno = rp->start++;
2148 if (FL_ISSET(ecp->agv_flags, AGV_GLOBAL | AGV_V))
2149 F_SET(sp, SC_EX_GLOBAL);
2155 * Discard any pending ex commands.
2165 * We know the first command can't be an AGV command, so we don't
2166 * process it specially. We do, however, nail the command itself.
2168 for (gp = sp->gp;;) {
2169 ecp = SLIST_FIRST(gp->ecq);
2170 if (F_ISSET(ecp, E_NAMEDISCARD))
2172 /* Reset the last command without dropping it. */
2173 if (ecp == &gp->excmd)
2175 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2176 while ((rp = TAILQ_FIRST(ecp->rq)) != NULL) {
2177 TAILQ_REMOVE(ecp->rq, rp, q);
2182 SLIST_REMOVE_HEAD(gp->ecq, q);
2186 ecp->if_name = NULL;
2193 * Display an unknown command name.
2196 ex_unknown(SCR *sp, CHAR_T *cmd, size_t len)
2201 GET_SPACE_GOTOW(sp, bp, blen, len + 1);
2203 MEMCPY(bp, cmd, len);
2204 msgq_wstr(sp, M_ERR, bp, "098|The %s command is unknown");
2205 FREE_SPACEW(sp, bp, blen);
2213 * The vi text input routine needs to know if ex thinks this is an
2214 * [un]abbreviate command, so it can turn off abbreviations. See
2215 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2217 * PUBLIC: int ex_is_abbrev(CHAR_T *, size_t);
2220 ex_is_abbrev(CHAR_T *name, size_t len)
2222 EXCMDLIST const *cp;
2224 return ((cp = ex_comm_search(name, len)) != NULL &&
2225 (cp == &cmds[C_ABBR] || cp == &cmds[C_UNABBREVIATE]));
2230 * The vi text input routine needs to know if ex thinks this is an
2231 * unmap command, so it can turn off input mapping. See the usual
2232 * ranting in the vi/v_txt_ev.c:txt_unmap() routine.
2234 * PUBLIC: int ex_is_unmap(CHAR_T *, size_t);
2237 ex_is_unmap(CHAR_T *name, size_t len)
2239 EXCMDLIST const *cp;
2242 * The command the vi input routines are really interested in
2243 * is "unmap!", not just unmap.
2245 if (name[len - 1] != '!')
2248 return ((cp = ex_comm_search(name, len)) != NULL &&
2249 cp == &cmds[C_UNMAP]);
2254 * Search for a command name.
2256 static EXCMDLIST const *
2257 ex_comm_search(CHAR_T *name, size_t len)
2259 EXCMDLIST const *cp;
2261 for (cp = cmds; cp->name != NULL; ++cp) {
2262 if (cp->name[0] > name[0])
2264 if (cp->name[0] != name[0])
2266 if (!MEMCMP(name, cp->name, len))
2274 * Display a bad address message.
2276 * PUBLIC: void ex_badaddr
2277 * PUBLIC: (SCR *, EXCMDLIST const *, enum badaddr, enum nresult);
2280 ex_badaddr(SCR *sp, const EXCMDLIST *cp, enum badaddr ba, enum nresult nret)
2288 msgq(sp, M_SYSERR, NULL);
2291 msgq(sp, M_ERR, "099|Address value overflow");
2294 msgq(sp, M_ERR, "100|Address value underflow");
2299 * When encountering an address error, tell the user if there's no
2300 * underlying file, that's the real problem.
2302 if (sp->ep == NULL) {
2303 ex_wemsg(sp, cp ? cp->name : NULL, EXM_NOFILEYET);
2309 msgq(sp, M_ERR, "101|Illegal address combination");
2312 if (db_last(sp, &lno))
2316 "102|Illegal address: only %lu lines in the file",
2322 msgq(sp, M_ERR, "103|Illegal address: the file is empty");
2328 msgq_wstr(sp, M_ERR, cp->name,
2329 "104|The %s command doesn't permit an address of 0");
2335 #if defined(DEBUG) && defined(COMLOG)
2345 TRACE(sp, "ecmd: "WS, ecp->cmd->name);
2346 if (ecp->addrcnt > 0) {
2347 TRACE(sp, " a1 %d", ecp->addr1.lno);
2348 if (ecp->addrcnt > 1)
2349 TRACE(sp, " a2: %d", ecp->addr2.lno);
2352 TRACE(sp, " line %d", ecp->lineno);
2354 TRACE(sp, " flags 0x%x", ecp->flags);
2355 if (FL_ISSET(ecp->iflags, E_C_BUFFER))
2356 TRACE(sp, " buffer "WC, ecp->buffer);
2359 for (cnt = 0; cnt < ecp->argc; ++cnt)
2360 TRACE(sp, " arg %d: {"WS"}", cnt, ecp->argv[cnt]->bp);