1 /* $NetBSD: ex.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 static const char sccsid
[] = "Id: ex.c,v 10.75 2004/03/16 14:13:35 skimo Exp (Berkeley) Date: 2004/03/16 14:13:35 ";
19 __RCSID("$NetBSD: ex.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
27 #include <bitstring.h>
37 #include "../common/common.h"
38 #include "../common/multibyte.h"
41 #if defined(DEBUG) && defined(COMLOG)
42 static void ex_comlog
__P((SCR
*, EXCMD
*));
44 static EXCMDLIST
const *
45 ex_comm_search
__P((SCR
*, CHAR_T
*, size_t));
46 static int ex_discard
__P((SCR
*));
47 static int ex_line
__P((SCR
*, EXCMD
*, MARK
*, int *, int *));
48 static int ex_load
__P((SCR
*));
49 static void ex_unknown
__P((SCR
*, CHAR_T
*, size_t));
55 * PUBLIC: int ex __P((SCR **));
71 /* Start the ex screen. */
75 /* Flush any saved messages. */
76 while ((mp
= LIST_FIRST(&gp
->msgq
)) != NULL
) {
77 wp
->scr_msg(sp
, mp
->mtype
, mp
->buf
, mp
->len
);
83 /* If reading from a file, errors should have name and line info. */
84 if (F_ISSET(gp
, G_SCRIPTED
)) {
86 wp
->excmd
.if_name
= strdup("script");
91 * Initialize the text flags. The beautify edit option historically
92 * applied to ex command input read from a file. In addition, the
93 * first time a ^H was discarded from the input, there was a message,
94 * "^H discarded", that was displayed. We don't bother.
96 LF_INIT(TXT_BACKSLASH
| TXT_CNTRLD
| TXT_CR
);
97 for (;; ++wp
->excmd
.if_lno
) {
98 /* Display status line and flush. */
99 if (F_ISSET(sp
, SC_STATUS
)) {
100 if (!F_ISSET(sp
, SC_EX_SILENT
))
101 msgq_status(sp
, sp
->lno
, 0);
102 F_CLR(sp
, SC_STATUS
);
106 /* Set the flags the user can reset. */
107 if (O_ISSET(sp
, O_BEAUTIFY
))
108 LF_SET(TXT_BEAUTIFY
);
109 if (O_ISSET(sp
, O_PROMPT
))
112 /* Clear any current interrupts, and get a command. */
114 if (ex_txt(sp
, &sp
->tiq
, ':', flags
))
116 if (INTERRUPTED(sp
)) {
117 (void)ex_puts(sp
, "\n");
122 /* Initialize the command structure. */
123 CLEAR_EX_PARSER(&wp
->excmd
);
126 * If the user entered a single carriage return, send
127 * ex_cmd() a separator -- it discards single newlines.
129 tp
= TAILQ_FIRST(&sp
->tiq
);
131 static CHAR_T space
= ' ';
132 wp
->excmd
.cp
= &space
; /* __TK__ why not |? */
135 wp
->excmd
.cp
= tp
->lb
;
136 wp
->excmd
.clen
= tp
->len
;
138 F_INIT(&wp
->excmd
, E_NRSEP
);
140 if (ex_cmd(sp
) && F_ISSET(gp
, G_SCRIPTED
))
143 if (INTERRUPTED(sp
)) {
145 msgq(sp
, M_ERR
, "170|Interrupted");
149 * If the last command caused a restart, or switched screens
150 * or into vi, return.
152 if (F_ISSET(gp
, G_SRESTART
) || F_ISSET(sp
, SC_SSWITCH
| SC_VI
)) {
157 /* If the last command switched files, we don't care. */
158 F_CLR(sp
, SC_FSWITCH
);
161 * If we're exiting this screen, move to the next one. By
162 * definition, this means returning into vi, so return to the
163 * main editor loop. The ordering is careful, don't discard
164 * the contents of sp until the end.
166 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
167 if (file_end(sp
, NULL
, F_ISSET(sp
, SC_EXIT_FORCE
)))
169 *spp
= screen_next(sp
);
170 return (screen_end(sp
));
178 * The guts of the ex parser: parse and execute a string containing
182 * This code MODIFIES the string that gets passed in, to delete quoting
183 * characters, etc. The string cannot be readonly/text space, nor should
184 * you expect to use it again after ex_cmd() returns.
187 * For the fun of it, if you want to see if a vi clone got the ex argument
188 * parsing right, try:
190 * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
192 * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
195 * :set|file|append|set|file
197 * For extra credit, try them in a startup .exrc file.
199 * PUBLIC: int ex_cmd __P((SCR *));
211 size_t arg1_len
, discard
, len
;
214 int at_found
, gv_found
;
215 int cnt
, delim
, isaddr
, namelen
;
216 int newscreen
, notempty
, tmp
, vi_address
;
217 CHAR_T
*arg1
, *s
, *p
, *t
;
225 ch
= '\0'; /* XXX: gcc -O1 -Wuninitialized */
228 * We always start running the command on the top of the stack.
229 * This means that *everything* must be resolved when we leave
230 * this function for any reason.
232 loop
: ecp
= LIST_FIRST(&wp
->ecq
);
234 /* If we're reading a command from a file, set up error information. */
235 if (ecp
->if_name
!= NULL
) {
236 wp
->if_lno
= ecp
->if_lno
;
237 wp
->if_name
= ecp
->if_name
;
241 * If a move to the end of the file is scheduled for this command,
244 if (F_ISSET(ecp
, E_MOVETOEND
)) {
245 if (db_last(sp
, &sp
->lno
))
248 F_CLR(ecp
, E_MOVETOEND
);
251 /* If we found a newline, increment the count now. */
252 if (F_ISSET(ecp
, E_NEWLINE
)) {
255 F_CLR(ecp
, E_NEWLINE
);
258 /* (Re)initialize the EXCMD structure, preserving some flags. */
261 /* Initialize the argument structures. */
262 if (argv_init(sp
, ecp
))
265 /* Initialize +cmd, saved command information. */
267 ecp
->save_cmdlen
= 0;
269 /* Skip <blank>s, empty lines. */
270 for (notempty
= 0; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
)
271 if ((ch
= (UCHAR_T
)*ecp
->cp
) == '\n') {
274 } else if (ISBLANK(ch
))
281 * Permit extra colons at the start of the line. Historically,
282 * ex/vi allowed a single extra one. It's simpler not to count.
283 * The stripping is done here because, historically, any command
284 * could have preceding colons, e.g. ":g/pattern/:p" worked.
286 if (ecp
->clen
!= 0 && ch
== ':') {
288 while (--ecp
->clen
> 0 && (ch
= (UCHAR_T
)*++ecp
->cp
) == ':');
292 * Command lines that start with a double-quote are comments.
295 * Historically, there was no escape or delimiter for a comment, e.g.
296 * :"foo|set was a single comment and nothing was output. Since nvi
297 * permits users to escape <newline> characters into command lines, we
298 * have to check for that case.
300 if (ecp
->clen
!= 0 && ch
== '"') {
301 while (--ecp
->clen
> 0 && *++ecp
->cp
!= '\n');
302 if (*ecp
->cp
== '\n') {
303 F_SET(ecp
, E_NEWLINE
);
310 /* Skip whitespace. */
311 for (; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
) {
312 ch
= (UCHAR_T
)*ecp
->cp
;
318 * The last point at which an empty line can mean do nothing.
321 * Historically, in ex mode, lines containing only <blank> characters
322 * were the same as a single <carriage-return>, i.e. a default command.
323 * In vi mode, they were ignored. In .exrc files this was a serious
324 * annoyance, as vi kept trying to treat them as print commands. We
325 * ignore backward compatibility in this case, discarding lines that
326 * contain only <blank> characters from .exrc files.
329 * This is where you end up when you're done a command, i.e. clen has
330 * gone to zero. Continue if there are more commands to run.
332 if (ecp
->clen
== 0 &&
333 (!notempty
|| F_ISSET(sp
, SC_VI
) || F_ISSET(ecp
, E_BLIGNORE
))) {
336 ecp
= LIST_FIRST(&wp
->ecq
);
343 * Check to see if this is a command for which we may want to move
344 * the cursor back up to the previous line. (The command :1<CR>
345 * wants a <newline> separator, but the command :<CR> wants to erase
346 * the command line.) If the line is empty except for <blank>s,
347 * <carriage-return> or <eof>, we'll probably want to move up. I
348 * don't think there's any way to get <blank> characters *after* the
349 * command character, but this is the ex parser, and I've been wrong
352 if (F_ISSET(ecp
, E_NRSEP
) &&
353 ecp
->clen
!= 0 && (ecp
->clen
!= 1 || ecp
->cp
[0] != '\004'))
356 /* Parse command addresses. */
357 if (ex_range(sp
, ecp
, &tmp
))
363 * Skip <blank>s and any more colons (the command :3,5:print
364 * worked, historically).
366 for (; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
) {
367 ch
= (UCHAR_T
)*ecp
->cp
;
368 if (!ISBLANK(ch
) && ch
!= ':')
373 * If no command, ex does the last specified of p, l, or #, and vi
374 * moves to the line. Otherwise, determine the length of the command
375 * name by looking for the first non-alphabetic character. (There
376 * are a few non-alphabetic characters in command names, but they're
377 * all single character commands.) This isn't a great test, because
378 * it means that, for the command ":e +cut.c file", we'll report that
379 * the command "cut" wasn't known. However, it makes ":e+35 file" work
383 * Historically, lines with multiple adjacent (or <blank> separated)
384 * command separators were very strange. For example, the command
385 * |||<carriage-return>, when the cursor was on line 1, displayed
386 * lines 2, 3 and 5 of the file. In addition, the command " | "
387 * would only display the line after the next line, instead of the
388 * next two lines. No ideas why. It worked reasonably when executed
389 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
390 * command for each separator.
392 #define SINGLE_CHAR_COMMANDS "\004!#&*<=>@~"
394 if (ecp
->clen
!= 0 && ecp
->cp
[0] != '|' && ecp
->cp
[0] != '\n') {
395 if (strchr(SINGLE_CHAR_COMMANDS
, *ecp
->cp
)) {
402 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
403 if (!ISALPHA((UCHAR_T
)*ecp
->cp
))
405 if ((namelen
= ecp
->cp
- p
) == 0) {
406 msgq(sp
, M_ERR
, "080|Unknown command name");
413 * Historic vi permitted flags to immediately follow any
414 * subset of the 'delete' command, but then did not permit
415 * further arguments (flag, buffer, count). Make it work.
416 * Permit further arguments for the few shreds of dignity
419 * Adding commands that start with 'd', and match "delete"
420 * up to a l, p, +, - or # character can break this code.
423 * Capital letters beginning the command names ex, edit,
424 * next, previous, tag and visual (in vi mode) indicate the
425 * command should happen in a new screen.
430 n
= cmds
[C_DELETE
].name
; *s
== *n
; ++s
, ++n
);
431 if (s
[0] == 'l' || s
[0] == 'p' || s
[0] == '+' ||
432 s
[0] == '-' || s
[0] == '^' || s
[0] == '#') {
433 len
= (ecp
->cp
- p
) - (s
- p
);
436 ecp
->rcmd
= cmds
[C_DELETE
];
437 ecp
->rcmd
.syntax
= "1bca1";
438 ecp
->cmd
= &ecp
->rcmd
;
442 case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
444 p
[0] = TOLOWER(p
[0]);
449 * Search the table for the command.
452 * Historic vi permitted the mark to immediately follow the
453 * 'k' in the 'k' command. Make it work.
456 * Historic vi permitted any flag to follow the s command, e.g.
457 * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
458 * Since the following characters all have to be flags, i.e.
459 * alphabetics, we can let the s command routine return errors
460 * if it was some illegal command string. This code will break
461 * if an "sg" or similar command is ever added. The substitute
462 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
463 * follows the 's', but we limit the choices here to "cgr" so
464 * that we get unknown command messages for wrong combinations.
466 if ((ecp
->cmd
= ex_comm_search(sp
, p
, namelen
)) == NULL
)
470 ecp
->cp
-= namelen
- 1;
471 ecp
->clen
+= namelen
- 1;
472 ecp
->cmd
= &cmds
[C_K
];
477 for (s
= p
+ 1, cnt
= namelen
; --cnt
; ++s
)
479 s
[0] != 'g' && s
[0] != 'r')
482 ecp
->cp
-= namelen
- 1;
483 ecp
->clen
+= namelen
- 1;
484 ecp
->rcmd
= cmds
[C_SUBSTITUTE
];
485 ecp
->rcmd
.fn
= ex_subagain
;
486 ecp
->cmd
= &ecp
->rcmd
;
491 unknown
: if (newscreen
)
492 p
[0] = TOUPPER((UCHAR_T
)p
[0]);
493 ex_unknown(sp
, p
, namelen
);
498 * The visual command has a different syntax when called
499 * from ex than when called from a vi colon command. FMH.
500 * Make the change now, before we test for the newscreen
501 * semantic, so that we're testing the right one.
503 skip_srch
: if (ecp
->cmd
== &cmds
[C_VISUAL_EX
] && F_ISSET(sp
, SC_VI
))
504 ecp
->cmd
= &cmds
[C_VISUAL_VI
];
508 * Historic vi permitted a capital 'P' at the beginning of
509 * any command that started with 'p'. Probably wanted the
510 * P[rint] command for backward compatibility, and the code
511 * just made Preserve and Put work by accident. Nvi uses
512 * Previous to mean previous-in-a-new-screen, so be careful.
514 if (newscreen
&& !F_ISSET(ecp
->cmd
, E_NEWSCREEN
) &&
515 (ecp
->cmd
== &cmds
[C_PRINT
] ||
516 ecp
->cmd
== &cmds
[C_PRESERVE
]))
519 /* Test for a newscreen associated with this command. */
520 if (newscreen
&& !F_ISSET(ecp
->cmd
, E_NEWSCREEN
))
523 /* Secure means no shell access. */
524 if (F_ISSET(ecp
->cmd
, E_SECURE
) && O_ISSET(sp
, O_SECURE
)) {
525 ex_wemsg(sp
, ecp
->cmd
->name
, EXM_SECURE
);
530 * Multiple < and > characters; another "feature". Note,
531 * The string passed to the underlying function may not be
532 * nul terminated in this case.
534 if ((ecp
->cmd
== &cmds
[C_SHIFTL
] && *p
== '<') ||
535 (ecp
->cmd
== &cmds
[C_SHIFTR
] && *p
== '>')) {
536 for (ch
= (UCHAR_T
)*p
;
537 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
540 if (argv_exp0(sp
, ecp
, p
, ecp
->cp
- p
))
544 /* Set the format style flags for the next command. */
545 if (ecp
->cmd
== &cmds
[C_HASH
])
546 exp
->fdef
= E_C_HASH
;
547 else if (ecp
->cmd
== &cmds
[C_LIST
])
548 exp
->fdef
= E_C_LIST
;
549 else if (ecp
->cmd
== &cmds
[C_PRINT
])
550 exp
->fdef
= E_C_PRINT
;
551 F_CLR(ecp
, E_USELASTCMD
);
553 /* Print is the default command. */
554 ecp
->cmd
= &cmds
[C_PRINT
];
556 /* Set the saved format flags. */
557 F_SET(ecp
, exp
->fdef
);
561 * If no address was specified, and it's not a global command,
562 * we up the address by one. (I have no idea why globals are
563 * exempted, but it's (ahem) historic practice.)
565 if (ecp
->addrcnt
== 0 && !F_ISSET(sp
, SC_EX_GLOBAL
)) {
567 ecp
->addr1
.lno
= sp
->lno
+ 1;
568 ecp
->addr1
.cno
= sp
->cno
;
571 F_SET(ecp
, E_USELASTCMD
);
576 * Historically, the number option applied to both ex and vi. One
577 * strangeness was that ex didn't switch display formats until a
578 * command was entered, e.g. <CR>'s after the set didn't change to
579 * the new format, but :1p would.
581 if (O_ISSET(sp
, O_NUMBER
)) {
582 F_SET(ecp
, E_OPTNUM
);
583 FL_SET(ecp
->iflags
, E_C_HASH
);
585 F_CLR(ecp
, E_OPTNUM
);
587 /* Check for ex mode legality. */
588 if (F_ISSET(sp
, SC_EX
) && (F_ISSET(ecp
->cmd
, E_VIONLY
) || newscreen
)) {
589 msgq_wstr(sp
, M_ERR
, ecp
->cmd
->name
,
590 "082|%s: command not available in ex mode");
594 /* Add standard command flags. */
595 F_SET(ecp
, ecp
->cmd
->flags
);
597 F_CLR(ecp
, E_NEWSCREEN
);
600 * There are three normal termination cases for an ex command. They
601 * are the end of the string (ecp->clen), or unescaped (by <literal
602 * next> characters) <newline> or '|' characters. As we're now past
603 * possible addresses, we can determine how long the command is, so we
604 * don't have to look for all the possible terminations. Naturally,
605 * there are some exciting special cases:
607 * 1: The bang, global, v and the filter versions of the read and
608 * write commands are delimited by <newline>s (they can contain
610 * 2: The ex, edit, next and visual in vi mode commands all take ex
611 * commands as their first arguments.
612 * 3: The s command takes an RE as its first argument, and wants it
613 * to be specially delimited.
615 * Historically, '|' characters in the first argument of the ex, edit,
616 * next, vi visual, and s commands didn't delimit the command. And,
617 * in the filter cases for read and write, and the bang, global and v
618 * commands, they did not delimit the command at all.
620 * For example, the following commands were legal:
622 * :edit +25|s/abc/ABC/ file.c
624 * :read !spell % | columnate
625 * :global/pattern/p|l
627 * It's not quite as simple as it sounds, however. The command:
631 * was also legal, i.e. the historic ex parser (using the word loosely,
632 * since "parser" implies some regularity of syntax) delimited the RE's
633 * based on its delimiter and not anything so irretrievably vulgar as a
636 * Anyhow, the following code makes this all work. First, for the
637 * special cases we move past their special argument(s). Then, we
638 * do normal command processing on whatever is left. Barf-O-Rama.
640 discard
= 0; /* Characters discarded from the command. */
642 ecp
->save_cmd
= ecp
->cp
;
643 if (ecp
->cmd
== &cmds
[C_EDIT
] || ecp
->cmd
== &cmds
[C_EX
] ||
644 ecp
->cmd
== &cmds
[C_NEXT
] || ecp
->cmd
== &cmds
[C_VISUAL_VI
] ||
645 ecp
->cmd
== &cmds
[C_VSPLIT
]) {
647 * Move to the next non-whitespace character. A '!'
648 * immediately following the command is eaten as a
651 if (ecp
->clen
> 0 && *ecp
->cp
== '!') {
654 FL_SET(ecp
->iflags
, E_C_FORCE
);
656 /* Reset, don't reparse. */
657 ecp
->save_cmd
= ecp
->cp
;
659 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
660 if (!ISBLANK(*ecp
->cp
))
665 * The historic implementation ignored all escape characters
666 * so there was no way to put a space or newline into the +cmd
667 * field. We do a simplistic job of fixing it by moving to the
668 * first whitespace character that isn't escaped. The escaping
669 * characters are stripped as no longer useful.
671 if (ecp
->clen
> 0 && *ecp
->cp
== '+') {
674 for (arg1
= p
= ecp
->cp
;
675 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
676 ch
= (UCHAR_T
)*ecp
->cp
;
677 if (IS_ESCAPE(sp
, ecp
, ch
) &&
681 ch
= (UCHAR_T
)*++ecp
->cp
;
682 } else if (ISBLANK(ch
))
686 arg1_len
= ecp
->cp
- arg1
;
688 /* Reset, so the first argument isn't reparsed. */
689 ecp
->save_cmd
= ecp
->cp
;
691 } else if (ecp
->cmd
== &cmds
[C_BANG
] ||
692 ecp
->cmd
== &cmds
[C_GLOBAL
] || ecp
->cmd
== &cmds
[C_V
]) {
696 * We use backslashes to escape <newline> characters, although
697 * this wasn't historic practice for the bang command. It was
698 * for the global and v commands, and it's common usage when
699 * doing text insert during the command. Escaping characters
700 * are stripped as no longer useful.
702 for (p
= ecp
->cp
; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
703 ch
= (UCHAR_T
)*ecp
->cp
;
704 if (ch
== '\\' && ecp
->clen
> 1 && ecp
->cp
[1] == '\n') {
707 ch
= (UCHAR_T
)*++ecp
->cp
;
711 } else if (ch
== '\n')
715 } else if (ecp
->cmd
== &cmds
[C_READ
] || ecp
->cmd
== &cmds
[C_WRITE
]) {
717 * For write commands, if the next character is a <blank>, and
718 * the next non-blank character is a '!', it's a filter command
719 * and we want to eat everything up to the <newline>. For read
720 * commands, if the next non-blank character is a '!', it's a
721 * filter command and we want to eat everything up to the next
722 * <newline>. Otherwise, we're done.
724 for (tmp
= 0; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
725 ch
= (UCHAR_T
)*ecp
->cp
;
731 if (ecp
->clen
> 0 && ch
== '!' &&
732 (ecp
->cmd
== &cmds
[C_READ
] || tmp
))
733 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
734 if (ecp
->cp
[0] == '\n')
736 } else if (ecp
->cmd
== &cmds
[C_SUBSTITUTE
]) {
738 * Move to the next non-whitespace character, we'll use it as
739 * the delimiter. If the character isn't an alphanumeric or
740 * a '|', it's the delimiter, so parse it. Otherwise, we're
741 * into something like ":s g", so use the special s command.
743 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
744 if (!ISBLANK(ecp
->cp
[0]))
747 if (ISALNUM((UCHAR_T
)ecp
->cp
[0]) || ecp
->cp
[0] == '|') {
748 ecp
->rcmd
= cmds
[C_SUBSTITUTE
];
749 ecp
->rcmd
.fn
= ex_subagain
;
750 ecp
->cmd
= &ecp
->rcmd
;
751 } else if (ecp
->clen
> 0) {
755 * Backslashes quote delimiter characters for RE's.
756 * The backslashes are NOT removed since they'll be
757 * used by the RE code. Move to the third delimiter
758 * that's not escaped (or the end of the command).
763 for (cnt
= 2; ecp
->clen
> 0 &&
764 cnt
!= 0; --ecp
->clen
, ++ecp
->cp
)
765 if (ecp
->cp
[0] == '\\' &&
769 } else if (ecp
->cp
[0] == delim
)
775 * Use normal quoting and termination rules to find the end of this
780 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
781 * file. It was almost certainly a bug, but that's what bug-for-bug
782 * compatibility means, Grasshopper. Also, ^V's escape the command
783 * delimiters. Literal next quote characters in front of the newlines,
784 * '|' characters or literal next characters are stripped as they're
787 vi_address
= ecp
->clen
!= 0 && ecp
->cp
[0] != '\n';
788 for (p
= ecp
->cp
; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
789 ch
= (UCHAR_T
)ecp
->cp
[0];
790 if (IS_ESCAPE(sp
, ecp
, ch
) && ecp
->clen
> 1) {
791 ARG_CHAR_T tmp1
= (UCHAR_T
)ecp
->cp
[1];
792 if (tmp1
== '\n' || tmp1
== '|') {
802 } else if (ch
== '\n' || ch
== '|') {
804 F_SET(ecp
, E_NEWLINE
);
812 * Save off the next command information, go back to the
813 * original start of the command.
816 ecp
->cp
= ecp
->save_cmd
;
818 ecp
->save_cmdlen
= ecp
->clen
;
819 ecp
->clen
= ((ecp
->save_cmd
- ecp
->cp
) - 1) - discard
;
824 * The "set tags" command historically used a backslash, not the
825 * user's literal next character, to escape whitespace. Handle
826 * it here instead of complicating the argv_exp3() code. Note,
827 * this isn't a particularly complex trap, and if backslashes were
828 * legal in set commands, this would have to be much more complicated.
830 if (ecp
->cmd
== &cmds
[C_SET
])
831 for (p
= ecp
->cp
, len
= ecp
->clen
; len
> 0; --len
, ++p
)
836 * Set the default addresses. It's an error to specify an address for
837 * a command that doesn't take them. If two addresses are specified
838 * for a command that only takes one, lose the first one. Two special
839 * cases here, some commands take 0 or 2 addresses. For most of them
840 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
841 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
843 * Also, if the file is empty, some commands want to use an address of
844 * 0, i.e. the entire file is 0 to 0, and the default first address is
845 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
846 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
847 * case where the 0 address is only valid if it's a default address.
849 * Also, set a flag if we set the default addresses. Some commands
850 * (ex: z) care if the user specified an address or if we just used
851 * the current cursor.
853 switch (F_ISSET(ecp
, E_ADDR1
| E_ADDR2
| E_ADDR2_ALL
| E_ADDR2_NONE
)) {
854 case E_ADDR1
: /* One address: */
855 switch (ecp
->addrcnt
) {
856 case 0: /* Default cursor/empty file. */
858 F_SET(ecp
, E_ADDR_DEF
);
859 if (F_ISSET(ecp
, E_ADDR_ZERODEF
)) {
860 if (db_last(sp
, &lno
))
864 F_SET(ecp
, E_ADDR_ZERO
);
866 ecp
->addr1
.lno
= sp
->lno
;
868 ecp
->addr1
.lno
= sp
->lno
;
869 ecp
->addr1
.cno
= sp
->cno
;
873 case 2: /* Lose the first address. */
875 ecp
->addr1
= ecp
->addr2
;
878 case E_ADDR2_NONE
: /* Zero/two addresses: */
879 if (ecp
->addrcnt
== 0) /* Default to nothing. */
882 case E_ADDR2_ALL
: /* Zero/two addresses: */
883 if (ecp
->addrcnt
== 0) { /* Default entire/empty file. */
884 F_SET(ecp
, E_ADDR_DEF
);
888 else if (db_last(sp
, &ecp
->addr2
.lno
))
890 if (F_ISSET(ecp
, E_ADDR_ZERODEF
) &&
891 ecp
->addr2
.lno
== 0) {
893 F_SET(ecp
, E_ADDR_ZERO
);
896 ecp
->addr1
.cno
= ecp
->addr2
.cno
= 0;
897 F_SET(ecp
, E_ADDR2_ALL
);
901 case E_ADDR2
: /* Two addresses: */
902 two_addr
: switch (ecp
->addrcnt
) {
903 case 0: /* Default cursor/empty file. */
905 F_SET(ecp
, E_ADDR_DEF
);
907 F_ISSET(ecp
, E_ADDR_ZERODEF
)) {
908 if (db_last(sp
, &lno
))
911 ecp
->addr1
.lno
= ecp
->addr2
.lno
= 0;
912 F_SET(ecp
, E_ADDR_ZERO
);
915 ecp
->addr2
.lno
= sp
->lno
;
917 ecp
->addr1
.lno
= ecp
->addr2
.lno
= sp
->lno
;
918 ecp
->addr1
.cno
= ecp
->addr2
.cno
= sp
->cno
;
920 case 1: /* Default to first address. */
921 //ecp->addrcnt = 2; /* XXX Was this needed ??? */
922 ecp
->addr2
= ecp
->addr1
;
929 if (ecp
->addrcnt
) /* Error. */
935 * The ^D scroll command historically scrolled the value of the scroll
936 * option or to EOF. It was an error if the cursor was already at EOF.
937 * (Leading addresses were permitted, but were then ignored.)
939 if (ecp
->cmd
== &cmds
[C_SCROLL
]) {
941 ecp
->addr1
.lno
= sp
->lno
+ 1;
942 ecp
->addr2
.lno
= sp
->lno
+ O_VAL(sp
, O_SCROLL
);
943 ecp
->addr1
.cno
= ecp
->addr2
.cno
= sp
->cno
;
944 if (db_last(sp
, &lno
))
946 if (lno
!= 0 && lno
> sp
->lno
&& ecp
->addr2
.lno
> lno
)
947 ecp
->addr2
.lno
= lno
;
951 for (np
= ecp
->cmd
->syntax
; *np
!= '\0'; ++np
) {
953 * The force flag is sensitive to leading whitespace, i.e.
954 * "next !" is different from "next!". Handle it before
955 * skipping leading <blank>s.
958 if (ecp
->clen
> 0 && *ecp
->cp
== '!') {
961 FL_SET(ecp
->iflags
, E_C_FORCE
);
966 /* Skip leading <blank>s. */
967 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
968 if (!ISBLANK(*ecp
->cp
))
974 case '1': /* +, -, #, l, p */
977 * Historically, some flags were ignored depending
978 * on where they occurred in the command line. For
979 * example, in the command, ":3+++p--#", historic vi
980 * acted on the '#' flag, but ignored the '-' flags.
981 * It's unambiguous what the flags mean, so we just
982 * handle them regardless of the stupidity of their
985 for (; ecp
->clen
; --ecp
->clen
, ++ecp
->cp
)
995 F_CLR(ecp
, E_OPTNUM
);
996 FL_SET(ecp
->iflags
, E_C_HASH
);
997 exp
->fdef
|= E_C_HASH
;
1000 FL_SET(ecp
->iflags
, E_C_LIST
);
1001 exp
->fdef
|= E_C_LIST
;
1004 FL_SET(ecp
->iflags
, E_C_PRINT
);
1005 exp
->fdef
|= E_C_PRINT
;
1011 case '2': /* -, ., +, ^ */
1012 case '3': /* -, ., +, ^, = */
1013 for (; ecp
->clen
; --ecp
->clen
, ++ecp
->cp
)
1016 FL_SET(ecp
->iflags
, E_C_DASH
);
1019 FL_SET(ecp
->iflags
, E_C_DOT
);
1022 FL_SET(ecp
->iflags
, E_C_PLUS
);
1025 FL_SET(ecp
->iflags
, E_C_CARAT
);
1029 FL_SET(ecp
->iflags
, E_C_EQUAL
);
1037 case 'b': /* buffer */
1040 * Historically, "d #" was a delete with a flag, not a
1041 * delete into the '#' buffer. If the current command
1042 * permits a flag, don't use one as a buffer. However,
1043 * the 'l' and 'p' flags were legal buffer names in the
1044 * historic ex, and were used as buffers, not flags.
1046 if ((ecp
->cp
[0] == '+' || ecp
->cp
[0] == '-' ||
1047 ecp
->cp
[0] == '^' || ecp
->cp
[0] == '#') &&
1048 strchr(np
, '1') != NULL
)
1052 * Digits can't be buffer names in ex commands, or the
1053 * command "d2" would be a delete into buffer '2', and
1054 * not a two-line deletion.
1056 if (!ISDIGIT((UCHAR_T
)ecp
->cp
[0])) {
1057 ecp
->buffer
= (UCHAR_T
)*ecp
->cp
;
1060 FL_SET(ecp
->iflags
, E_C_BUFFER
);
1063 case 'c': /* count [01+a] */
1065 /* Validate any signed value. */
1066 if (!ISDIGIT((UCHAR_T
)*ecp
->cp
) && (*np
!= '+' ||
1067 (*ecp
->cp
!= '+' && *ecp
->cp
!= '-')))
1069 /* If a signed value, set appropriate flags. */
1070 if (*ecp
->cp
== '-')
1071 FL_SET(ecp
->iflags
, E_C_COUNT_NEG
);
1072 else if (*ecp
->cp
== '+')
1073 FL_SET(ecp
->iflags
, E_C_COUNT_POS
);
1075 nget_slong(sp
, <mp
, ecp
->cp
, &t
, 10)) != NUM_OK
) {
1076 ex_badaddr(sp
, NULL
, A_NOTSET
, nret
);
1079 if (ltmp
== 0 && *np
!= '0') {
1080 msgq(sp
, M_ERR
, "083|Count may not be zero");
1083 ecp
->clen
-= (t
- ecp
->cp
);
1087 * Counts as address offsets occur in commands taking
1088 * two addresses. Historic vi practice was to use
1089 * the count as an offset from the *second* address.
1091 * Set a count flag; some underlying commands (see
1092 * join) do different things with counts than with
1096 ecp
->addr1
= ecp
->addr2
;
1097 ecp
->addr2
.lno
= ecp
->addr1
.lno
+ ltmp
- 1;
1100 FL_SET(ecp
->iflags
, E_C_COUNT
);
1102 case 'f': /* file */
1103 if (argv_exp2(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1106 case 'l': /* line */
1108 * Get a line specification.
1110 * If the line was a search expression, we may have
1111 * changed state during the call, and we're now
1112 * searching the file. Push ourselves onto the state
1115 if (ex_line(sp
, ecp
, &cur
, &isaddr
, &tmp
))
1120 /* Line specifications are always required. */
1122 msgq_wstr(sp
, M_ERR
, ecp
->cp
,
1123 "084|%s: bad line specification");
1127 * The target line should exist for these commands,
1128 * but 0 is legal for them as well.
1130 if (cur
.lno
!= 0 && !db_exist(sp
, cur
.lno
)) {
1131 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1134 ecp
->lineno
= cur
.lno
;
1136 case 'S': /* string, file exp. */
1137 if (ecp
->clen
!= 0) {
1138 if (argv_exp1(sp
, ecp
, ecp
->cp
,
1139 ecp
->clen
, ecp
->cmd
== &cmds
[C_BANG
]))
1144 case 's': /* string */
1145 if (argv_exp0(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1148 case 'W': /* word string */
1152 * Literal next characters escape the following
1153 * character. Quoting characters are stripped here
1154 * since they are no longer useful.
1156 * First there was the word.
1158 for (p
= t
= ecp
->cp
;
1159 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
1160 ch
= (UCHAR_T
)*ecp
->cp
;
1162 ecp
, ch
) && ecp
->clen
> 1) {
1165 } else if (ISBLANK(ch
)) {
1172 if (argv_exp0(sp
, ecp
, t
, p
- t
))
1175 /* Delete intervening whitespace. */
1176 for (; ecp
->clen
> 0;
1177 --ecp
->clen
, ++ecp
->cp
) {
1178 ch
= (UCHAR_T
)*ecp
->cp
;
1185 /* Followed by the string. */
1186 for (p
= t
= ecp
->cp
; ecp
->clen
> 0;
1187 --ecp
->clen
, ++ecp
->cp
, ++p
) {
1188 ch
= (UCHAR_T
)*ecp
->cp
;
1190 ecp
, ch
) && ecp
->clen
> 1) {
1196 if (argv_exp0(sp
, ecp
, t
, p
- t
))
1199 case 'w': /* word */
1200 if (argv_exp3(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1202 arg_cnt_chk
: if (*++np
!= 'N') { /* N */
1204 * If a number is specified, must either be
1205 * 0 or that number, if optional, and that
1206 * number, if required.
1209 if ((*++np
!= 'o' || exp
->argsoff
!= 0) &&
1210 exp
->argsoff
!= tmp
)
1217 INT2CHAR(sp
, ecp
->cmd
->name
, STRLEN(ecp
->cmd
->name
) + 1,
1220 "085|Internal syntax table error (%s: %s)",
1221 nstr
, KEY_NAME(sp
, *np
));
1226 /* Skip trailing whitespace. */
1227 for (; ecp
->clen
> 0; --ecp
->clen
) {
1228 ch
= (UCHAR_T
)*ecp
->cp
++;
1234 * There shouldn't be anything left, and no more required fields,
1235 * i.e neither 'l' or 'r' in the syntax string.
1237 if (ecp
->clen
!= 0 || strpbrk(np
, "lr")) {
1238 usage
: msgq(sp
, M_ERR
, "086|Usage: %s", ecp
->cmd
->usage
);
1243 * Verify that the addresses are legal. Check the addresses here,
1244 * because this is a place where all ex addresses pass through.
1245 * (They don't all pass through ex_line(), for instance.) We're
1246 * assuming that any non-existent line doesn't exist because it's
1247 * past the end-of-file. That's a pretty good guess.
1249 * If it's a "default vi command", an address of zero is okay.
1252 switch (ecp
->addrcnt
) {
1255 * Historic ex/vi permitted commands with counts to go past
1256 * EOF. So, for example, if the file only had 5 lines, the
1257 * ex command "1,6>" would fail, but the command ">300"
1258 * would succeed. Since we don't want to have to make all
1259 * of the underlying commands handle random line numbers,
1262 if (ecp
->addr2
.lno
== 0) {
1263 if (!F_ISSET(ecp
, E_ADDR_ZERO
) &&
1264 (F_ISSET(sp
, SC_EX
) ||
1265 !F_ISSET(ecp
, E_USELASTCMD
))) {
1266 ex_badaddr(sp
, ecp
->cmd
, A_ZERO
, NUM_OK
);
1269 } else if (!db_exist(sp
, ecp
->addr2
.lno
)) {
1270 if (FL_ISSET(ecp
->iflags
, E_C_COUNT
)) {
1271 if (db_last(sp
, &lno
))
1273 ecp
->addr2
.lno
= lno
;
1275 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1281 if (ecp
->addr1
.lno
== 0) {
1282 if (!F_ISSET(ecp
, E_ADDR_ZERO
) &&
1283 (F_ISSET(sp
, SC_EX
) ||
1284 !F_ISSET(ecp
, E_USELASTCMD
))) {
1285 ex_badaddr(sp
, ecp
->cmd
, A_ZERO
, NUM_OK
);
1288 } else if (!db_exist(sp
, ecp
->addr1
.lno
)) {
1289 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1296 * If doing a default command and there's nothing left on the line,
1297 * vi just moves to the line. For example, ":3" and ":'a,'b" just
1298 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1301 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1305 * This is done before the absolute mark gets set; historically,
1306 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1308 if ((F_ISSET(sp
, SC_VI
) || F_ISSET(ecp
, E_NOPRDEF
)) &&
1309 F_ISSET(ecp
, E_USELASTCMD
) && vi_address
== 0) {
1310 switch (ecp
->addrcnt
) {
1313 (ecp
->addr2
.lno
? ecp
->addr2
.lno
: 1)) {
1315 ecp
->addr2
.lno
? ecp
->addr2
.lno
: 1;
1317 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
1322 (ecp
->addr1
.lno
? ecp
->addr1
.lno
: 1)) {
1324 ecp
->addr1
.lno
? ecp
->addr1
.lno
: 1;
1326 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
1330 ecp
->cp
= ecp
->save_cmd
;
1331 ecp
->clen
= ecp
->save_cmdlen
;
1336 * Set the absolute mark -- we have to set it for vi here, in case
1337 * it's a compound command, e.g. ":5p|6" should set the absolute
1340 if (F_ISSET(ecp
, E_ABSMARK
)) {
1343 F_CLR(ecp
, E_ABSMARK
);
1344 if (mark_set(sp
, ABSMARK1
, &cur
, 1))
1348 #if defined(DEBUG) && defined(COMLOG)
1351 /* Increment the command count if not called from vi. */
1352 if (F_ISSET(sp
, SC_EX
))
1356 * If file state available, and not doing a global command,
1357 * log the start of an action.
1359 if (sp
->ep
!= NULL
&& !F_ISSET(sp
, SC_EX_GLOBAL
))
1360 (void)log_cursor(sp
);
1364 * There are two special commands for the purposes of this code: the
1365 * default command (<carriage-return>) or the scrolling commands (^D
1366 * and <EOF>) as the first non-<blank> characters in the line.
1368 * If this is the first command in the command line, we received the
1369 * command from the ex command loop and we're talking to a tty, and
1370 * and there's nothing else on the command line, and it's one of the
1371 * special commands, we move back up to the previous line, and erase
1372 * the prompt character with the output. Since ex runs in canonical
1373 * mode, we don't have to do anything else, a <newline> has already
1374 * been echoed by the tty driver. It's OK if vi calls us -- we won't
1375 * be in ex mode so we'll do nothing.
1377 if (F_ISSET(ecp
, E_NRSEP
)) {
1378 if (sp
->ep
!= NULL
&&
1379 F_ISSET(sp
, SC_EX
) && !F_ISSET(gp
, G_SCRIPTED
) &&
1380 (F_ISSET(ecp
, E_USELASTCMD
) || ecp
->cmd
== &cmds
[C_SCROLL
]))
1381 gp
->scr_ex_adjust(sp
, EX_TERM_SCROLL
);
1382 F_CLR(ecp
, E_NRSEP
);
1386 * Call the underlying function for the ex command.
1389 * Interrupts behave like errors, for now.
1391 if (ecp
->cmd
->fn(sp
, ecp
) || INTERRUPTED(sp
)) {
1392 if (F_ISSET(gp
, G_SCRIPTED
))
1393 F_SET(sp
, SC_EXIT_FORCE
);
1398 /* Make sure no function left global temporary space locked. */
1399 if (F_ISSET(wp
, W_TMP_INUSE
)) {
1400 F_CLR(wp
, W_TMP_INUSE
);
1401 msgq(sp
, M_ERR
, "087|%s: temporary buffer not released",
1406 * Ex displayed the number of lines modified immediately after each
1407 * command, so the command "1,10d|1,10d" would display:
1413 * Executing ex commands from vi only reported the final modified
1414 * lines message -- that's wrong enough that we don't match it.
1416 if (F_ISSET(sp
, SC_EX
))
1420 * Integrate any offset parsed by the underlying command, and make
1421 * sure the referenced line exists.
1424 * May not match historic practice (which I've never been able to
1425 * completely figure out.) For example, the '=' command from vi
1426 * mode often got the offset wrong, and complained it was too large,
1427 * but didn't seem to have a problem with the cursor. If anyone
1428 * complains, ask them how it's supposed to work, they might know.
1430 if (sp
->ep
!= NULL
&& ecp
->flagoff
) {
1431 if (ecp
->flagoff
< 0) {
1432 if (sp
->lno
<= (db_recno_t
)(-ecp
->flagoff
)) {
1434 "088|Flag offset to before line 1");
1438 if (!NPFITS(DB_MAX_RECORDS
, sp
->lno
, (db_recno_t
)ecp
->flagoff
)) {
1439 ex_badaddr(sp
, NULL
, A_NOTSET
, NUM_OVER
);
1442 if (!db_exist(sp
, sp
->lno
+ ecp
->flagoff
)) {
1444 "089|Flag offset past end-of-file");
1448 sp
->lno
+= ecp
->flagoff
;
1452 * If the command executed successfully, we may want to display a line
1453 * based on the autoprint option or an explicit print flag. (Make sure
1454 * that there's a line to display.) Also, the autoprint edit option is
1455 * turned off for the duration of global commands.
1457 if (F_ISSET(sp
, SC_EX
) && sp
->ep
!= NULL
&& sp
->lno
!= 0) {
1459 * The print commands have already handled the `print' flags.
1460 * If so, clear them.
1462 if (FL_ISSET(ecp
->iflags
, E_CLRFLAG
))
1463 FL_CLR(ecp
->iflags
, E_C_HASH
| E_C_LIST
| E_C_PRINT
);
1465 /* If hash set only because of the number option, discard it. */
1466 if (F_ISSET(ecp
, E_OPTNUM
))
1467 FL_CLR(ecp
->iflags
, E_C_HASH
);
1470 * If there was an explicit flag to display the new cursor line,
1471 * or autoprint is set and a change was made, display the line.
1472 * If any print flags were set use them, else default to print.
1474 LF_INIT(FL_ISSET(ecp
->iflags
, E_C_HASH
| E_C_LIST
| E_C_PRINT
));
1475 if (!LF_ISSET(E_C_HASH
| E_C_LIST
| E_C_PRINT
| E_NOAUTO
) &&
1476 !F_ISSET(sp
, SC_EX_GLOBAL
) &&
1477 O_ISSET(sp
, O_AUTOPRINT
) && F_ISSET(ecp
, E_AUTOPRINT
))
1480 if (LF_ISSET(E_C_HASH
| E_C_LIST
| E_C_PRINT
)) {
1483 (void)ex_print(sp
, ecp
, &cur
, &cur
, flags
);
1488 * If the command had an associated "+cmd", it has to be executed
1489 * before we finish executing any more of this ex command. For
1490 * example, consider a .exrc file that contains the following lines:
1493 * :edit +25 file.c|s/abc/ABC/|1
1496 * This can happen more than once -- the historic vi simply hung or
1497 * dropped core, of course. Prepend the + command back into the
1498 * current command and continue. We may have to add an additional
1499 * <literal next> character. We know that it will fit because we
1500 * discarded at least one space and the + character.
1502 if (arg1_len
!= 0) {
1504 * If the last character of the + command was a <literal next>
1505 * character, it would be treated differently because of the
1506 * append. Quote it, if necessary.
1508 if (IS_ESCAPE(sp
, ecp
, arg1
[arg1_len
- 1])) {
1509 *--ecp
->save_cmd
= CH_LITERAL
;
1513 ecp
->save_cmd
-= arg1_len
;
1514 ecp
->save_cmdlen
+= arg1_len
;
1515 MEMCPYW(ecp
->save_cmd
, arg1
, arg1_len
);
1518 * Any commands executed from a +cmd are executed starting at
1519 * the first column of the last line of the file -- NOT the
1520 * first nonblank.) The main file startup code doesn't know
1521 * that a +cmd was set, however, so it may have put us at the
1522 * top of the file. (Note, this is safe because we must have
1523 * switched files to get here.)
1525 F_SET(ecp
, E_MOVETOEND
);
1528 /* Update the current command. */
1529 ecp
->cp
= ecp
->save_cmd
;
1530 ecp
->clen
= ecp
->save_cmdlen
;
1534 * If we've changed screens or underlying files, any pending global or
1535 * v command, or @ buffer that has associated addresses, has to be
1536 * discarded. This is historic practice for globals, and necessary for
1537 * @ buffers that had associated addresses.
1539 * Otherwise, if we've changed underlying files, it's not a problem,
1540 * we continue with the rest of the ex command(s), operating on the
1541 * new file. However, if we switch screens (either by exiting or by
1542 * an explicit command), we have no way of knowing where to put output
1543 * messages, and, since we don't control screens here, we could screw
1544 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1545 * times). So, return and continue after we've got a new screen.
1547 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
| SC_FSWITCH
| SC_SSWITCH
)) {
1548 at_found
= gv_found
= 0;
1549 LIST_FOREACH(ecp
, &wp
->ecq
, q
)
1550 switch (ecp
->agv_flags
) {
1552 case AGV_AT_NORANGE
:
1558 "090|@ with range running when the file/screen changed");
1566 "091|Global/v command running when the file/screen changed");
1572 if (at_found
|| gv_found
)
1574 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
| SC_SSWITCH
))
1582 * On command failure, we discard keys and pending commands remaining,
1583 * as well as any keys that were mapped and waiting. The save_cmdlen
1584 * test is not necessarily correct. If we fail early enough we don't
1585 * know if the entire string was a single command or not. Guess, as
1586 * it's useful to know if commands other than the current one are being
1589 if (ecp
->save_cmdlen
== 0)
1590 for (; ecp
->clen
; --ecp
->clen
) {
1591 ch
= (UCHAR_T
)*ecp
->cp
++;
1592 if (IS_ESCAPE(sp
, ecp
, ch
) && ecp
->clen
> 1) {
1595 } else if (ch
== '\n' || ch
== '|') {
1597 ecp
->save_cmdlen
= 1;
1601 if (ecp
->save_cmdlen
!= 0 || LIST_FIRST(&wp
->ecq
) != &wp
->excmd
) {
1602 discard
: msgq(sp
, M_BERR
,
1603 "092|Ex command failed: pending commands discarded");
1606 if (v_event_flush(sp
, CH_MAPPED
))
1608 "093|Ex command failed: mapped keys discarded");
1614 /* Turn off any file name error information. */
1617 /* Turn off the global bit. */
1618 F_CLR(sp
, SC_EX_GLOBAL
);
1625 * Get a line range for ex commands, or perform a vi ex address search.
1627 * PUBLIC: int ex_range __P((SCR *, EXCMD *, int *));
1630 ex_range(SCR
*sp
, EXCMD
*ecp
, int *errp
)
1632 enum { ADDR_FOUND
, ADDR_NEED
, ADDR_NONE
} addr
;
1639 * Parse comma or semi-colon delimited line specs.
1641 * Semi-colon delimiters update the current address to be the last
1642 * address. For example, the command
1644 * :3;/pattern/ecp->cp
1646 * will search for pattern from line 3. In addition, if ecp->cp
1647 * is not a valid command, the current line will be left at 3, not
1648 * at the original address.
1650 * Extra addresses are discarded, starting with the first.
1653 * If any addresses are missing, they default to the current line.
1654 * This was historically true for both leading and trailing comma
1655 * delimited addresses as well as for trailing semicolon delimited
1656 * addresses. For consistency, we make it true for leading semicolon
1657 * addresses as well.
1659 for (addr
= ADDR_NONE
, ecp
->addrcnt
= 0; ecp
->clen
> 0;)
1661 case '%': /* Entire file. */
1662 /* Vi ex address searches didn't permit % signs. */
1663 if (F_ISSET(ecp
, E_VISEARCH
))
1666 /* It's an error if the file is empty. */
1667 if (sp
->ep
== NULL
) {
1668 ex_badaddr(sp
, NULL
, A_EMPTY
, NUM_OK
);
1674 * A percent character addresses all of the lines in
1675 * the file. Historically, it couldn't be followed by
1676 * any other address. We do it as a text substitution
1677 * for simplicity. POSIX 1003.2 is expected to follow
1680 * If it's an empty file, the first line is 0, not 1.
1682 if (addr
== ADDR_FOUND
) {
1683 ex_badaddr(sp
, NULL
, A_COMBO
, NUM_OK
);
1687 if (db_last(sp
, &ecp
->addr2
.lno
))
1689 ecp
->addr1
.lno
= ecp
->addr2
.lno
== 0 ? 0 : 1;
1690 ecp
->addr1
.cno
= ecp
->addr2
.cno
= 0;
1696 case ',': /* Comma delimiter. */
1697 /* Vi ex address searches didn't permit commas. */
1698 if (F_ISSET(ecp
, E_VISEARCH
))
1701 case ';': /* Semi-colon delimiter. */
1702 if (sp
->ep
== NULL
) {
1703 ex_badaddr(sp
, NULL
, A_EMPTY
, NUM_OK
);
1707 if (addr
!= ADDR_FOUND
)
1708 switch (ecp
->addrcnt
) {
1710 ecp
->addr1
.lno
= sp
->lno
;
1711 ecp
->addr1
.cno
= sp
->cno
;
1715 ecp
->addr1
= ecp
->addr2
;
1718 ecp
->addr2
.lno
= sp
->lno
;
1719 ecp
->addr2
.cno
= sp
->cno
;
1723 if (*ecp
->cp
== ';')
1724 switch (ecp
->addrcnt
) {
1729 sp
->lno
= ecp
->addr1
.lno
;
1730 sp
->cno
= ecp
->addr1
.cno
;
1733 sp
->lno
= ecp
->addr2
.lno
;
1734 sp
->cno
= ecp
->addr2
.cno
;
1739 case ' ': /* Whitespace. */
1740 case '\t': /* Whitespace. */
1745 /* Get a line specification. */
1746 if (ex_line(sp
, ecp
, &m
, &isaddr
, errp
))
1752 if (addr
== ADDR_FOUND
) {
1753 ex_badaddr(sp
, NULL
, A_COMBO
, NUM_OK
);
1757 switch (ecp
->addrcnt
) {
1767 ecp
->addr1
= ecp
->addr2
;
1777 * Vi ex address searches are indifferent to order or trailing
1780 ret
: if (F_ISSET(ecp
, E_VISEARCH
))
1783 if (addr
== ADDR_NEED
)
1784 switch (ecp
->addrcnt
) {
1786 ecp
->addr1
.lno
= sp
->lno
;
1787 ecp
->addr1
.cno
= sp
->cno
;
1791 ecp
->addr1
= ecp
->addr2
;
1794 ecp
->addr2
.lno
= sp
->lno
;
1795 ecp
->addr2
.cno
= sp
->cno
;
1800 if (ecp
->addrcnt
== 2 && ecp
->addr2
.lno
< ecp
->addr1
.lno
) {
1802 "094|The second address is smaller than the first");
1810 * Get a single line address specifier.
1812 * The way the "previous context" mark worked was that any "non-relative"
1813 * motion set it. While ex/vi wasn't totally consistent about this, ANY
1814 * numeric address, search pattern, '$', or mark reference in an address
1815 * was considered non-relative, and set the value. Which should explain
1816 * why we're hacking marks down here. The problem was that the mark was
1817 * only set if the command was called, i.e. we have to set a flag and test
1821 * This is probably still not exactly historic practice, although I think
1822 * it's fairly close.
1825 ex_line(SCR
*sp
, EXCMD
*ecp
, MARK
*mp
, int *isaddrp
, int *errp
)
1831 int (*sf
) __P((SCR
*, MARK
*, MARK
*, CHAR_T
*, size_t, CHAR_T
**, u_int
));
1834 *isaddrp
= *errp
= 0;
1835 F_CLR(ecp
, E_DELTA
);
1837 /* No addresses permitted until a file has been read in. */
1838 if (sp
->ep
== NULL
&& STRCHR(L("$0123456789'\\/?.+-^"), *ecp
->cp
)) {
1839 ex_badaddr(sp
, NULL
, A_EMPTY
, NUM_OK
);
1845 case '$': /* Last line in the file. */
1847 F_SET(ecp
, E_ABSMARK
);
1850 if (db_last(sp
, &mp
->lno
))
1854 break; /* Absolute line number. */
1855 case '0': case '1': case '2': case '3': case '4':
1856 case '5': case '6': case '7': case '8': case '9':
1858 F_SET(ecp
, E_ABSMARK
);
1860 if ((nret
= nget_uslong(sp
, &uval
, ecp
->cp
, &endp
, 10)) != NUM_OK
) {
1861 ex_badaddr(sp
, NULL
, A_NOTSET
, nret
);
1865 if (!NPFITS(DB_MAX_RECORDS
, 0, uval
)) {
1866 ex_badaddr(sp
, NULL
, A_NOTSET
, NUM_OVER
);
1872 ecp
->clen
-= (endp
- ecp
->cp
);
1875 case '\'': /* Use a mark. */
1877 F_SET(ecp
, E_ABSMARK
);
1879 if (ecp
->clen
== 1) {
1880 msgq(sp
, M_ERR
, "095|No mark name supplied");
1884 if (mark_get(sp
, ecp
->cp
[1], mp
, M_ERR
)) {
1891 case '\\': /* Search: forward/backward. */
1894 * I can't find any difference between // and \/ or between
1895 * ?? and \?. Mark Horton doesn't remember there being any
1896 * difference. C'est la vie.
1898 if (ecp
->clen
< 2 ||
1899 (ecp
->cp
[1] != '/' && ecp
->cp
[1] != '?')) {
1900 msgq(sp
, M_ERR
, "096|\\ not followed by / or ?");
1906 sf
= ecp
->cp
[0] == '/' ? f_search
: b_search
;
1908 case '/': /* Search forward. */
1911 case '?': /* Search backward. */
1914 search
: mp
->lno
= sp
->lno
;
1916 if (sf(sp
, mp
, mp
, ecp
->cp
, ecp
->clen
, &endp
,
1917 SEARCH_MSG
| SEARCH_PARSE
| SEARCH_SET
|
1918 (F_ISSET(ecp
, E_SEARCH_WMSG
) ? SEARCH_WMSG
: 0))) {
1923 /* Fix up the command pointers. */
1924 ecp
->clen
-= (endp
- ecp
->cp
);
1928 F_SET(ecp
, E_ABSMARK
);
1930 case '.': /* Current position. */
1934 /* If an empty file, then '.' is 0, not 1. */
1936 if (db_last(sp
, &mp
->lno
))
1945 * Historically, .<number> was the same as .+<number>, i.e.
1946 * the '+' could be omitted. (This feature is found in ed
1949 if (ecp
->clen
> 1 && ISDIGIT((UCHAR_T
)ecp
->cp
[1]))
1958 /* Skip trailing <blank>s. */
1959 for (; ecp
->clen
> 0 &&
1960 ISBLANK((UCHAR_T
)ecp
->cp
[0]); ++ecp
->cp
, --ecp
->clen
);
1963 * Evaluate any offset. If no address yet found, the offset
1964 * is relative to ".".
1967 if (ecp
->clen
!= 0 && (ISDIGIT((UCHAR_T
)ecp
->cp
[0]) ||
1968 ecp
->cp
[0] == '+' || ecp
->cp
[0] == '-' ||
1969 ecp
->cp
[0] == '^')) {
1976 * Evaluate an offset, defined as:
1978 * [+-^<blank>]*[<blank>]*[0-9]*
1980 * The rough translation is any number of signs, optionally
1981 * followed by numbers, or a number by itself, all <blank>
1985 * All address offsets were additive, e.g. "2 2 3p" was the
1986 * same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2".
1987 * Note, however, "2 /ZZZ/" was an error. It was also legal
1988 * to insert signs without numbers, so "3 - 2" was legal, and
1992 * Offsets were historically permitted for any line address,
1993 * e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after
1997 * Offsets were historically permitted for search commands,
1998 * and handled as addresses: "/pattern/2 2 2" was legal, and
1999 * referenced the 6th line after pattern.
2001 F_SET(ecp
, E_DELTA
);
2003 for (; ecp
->clen
> 0 && ISBLANK((UCHAR_T
)ecp
->cp
[0]);
2004 ++ecp
->cp
, --ecp
->clen
);
2005 if (ecp
->clen
== 0 || (!ISDIGIT((UCHAR_T
)ecp
->cp
[0]) &&
2006 ecp
->cp
[0] != '+' && ecp
->cp
[0] != '-' &&
2009 if (!ISDIGIT((UCHAR_T
)ecp
->cp
[0]) &&
2010 !ISDIGIT((UCHAR_T
)ecp
->cp
[1])) {
2011 total
+= ecp
->cp
[0] == '+' ? 1 : -1;
2015 if (ecp
->cp
[0] == '-' ||
2016 ecp
->cp
[0] == '^') {
2023 /* Get a signed long, add it to the total. */
2024 if ((nret
= nget_slong(sp
, &val
,
2025 ecp
->cp
, &endp
, 10)) != NUM_OK
||
2026 (nret
= NADD_SLONG(sp
,
2027 total
, val
)) != NUM_OK
) {
2028 ex_badaddr(sp
, NULL
, A_NOTSET
, nret
);
2032 total
+= isneg
? -val
: val
;
2033 ecp
->clen
-= (endp
- ecp
->cp
);
2040 * Any value less than 0 is an error. Make sure that the new value
2041 * will fit into a db_recno_t.
2043 if (*isaddrp
&& total
!= 0) {
2045 if ((db_recno_t
)-total
> mp
->lno
) {
2047 "097|Reference to a line number less than 0");
2052 if (!NPFITS(DB_MAX_RECORDS
, mp
->lno
, (unsigned long)total
)) {
2053 ex_badaddr(sp
, NULL
, A_NOTSET
, NUM_OVER
);
2065 * Load up the next command, which may be an @ buffer or global command.
2074 F_CLR(sp
, SC_EX_GLOBAL
);
2077 * Lose any exhausted commands. We know that the first command
2078 * can't be an AGV command, which makes things a bit easier.
2080 for (wp
= sp
->wp
;;) {
2082 * If we're back to the original structure, leave it around,
2083 * but discard any allocated source name, we've returned to
2084 * the beginning of the command stack.
2086 if ((ecp
= LIST_FIRST(&wp
->ecq
)) == &wp
->excmd
) {
2087 if (F_ISSET(ecp
, E_NAMEDISCARD
)) {
2089 ecp
->if_name
= NULL
;
2095 * ecp->clen will be 0 for the first discarded command, but
2096 * may not be 0 for subsequent ones, e.g. if the original
2097 * command was ":g/xx/@a|s/b/c/", then when we discard the
2098 * command pushed on the stack by the @a, we have to resume
2099 * the global command which included the substitute command.
2105 * If it's an @, global or v command, we may need to continue
2106 * the command on a different line.
2108 if (FL_ISSET(ecp
->agv_flags
, AGV_ALL
)) {
2109 /* Discard any exhausted ranges. */
2110 while ((rp
= TAILQ_FIRST(&ecp
->rq
)) != NULL
)
2111 if (rp
->start
> rp
->stop
) {
2112 TAILQ_REMOVE(&ecp
->rq
, rp
, q
);
2117 /* If there's another range, continue with it. */
2121 /* If it's a global/v command, fix up the last line. */
2122 if (FL_ISSET(ecp
->agv_flags
,
2123 AGV_GLOBAL
| AGV_V
) && ecp
->range_lno
!= OOBLNO
) {
2124 if (db_exist(sp
, ecp
->range_lno
))
2125 sp
->lno
= ecp
->range_lno
;
2127 if (db_last(sp
, &sp
->lno
))
2136 /* Discard the EXCMD. */
2137 LIST_REMOVE(ecp
, q
);
2142 * We only get here if it's an active @, global or v command. Set
2143 * the current line number, and get a new copy of the command for
2144 * the parser. Note, the original pointer almost certainly moved,
2145 * so we have play games.
2147 ecp
->cp
= ecp
->o_cp
;
2148 MEMCPYW(ecp
->cp
, ecp
->cp
+ ecp
->o_clen
, ecp
->o_clen
);
2149 ecp
->clen
= ecp
->o_clen
;
2150 ecp
->range_lno
= sp
->lno
= rp
->start
++;
2152 if (FL_ISSET(ecp
->agv_flags
, AGV_GLOBAL
| AGV_V
))
2153 F_SET(sp
, SC_EX_GLOBAL
);
2159 * Discard any pending ex commands.
2169 * We know the first command can't be an AGV command, so we don't
2170 * process it specially. We do, however, nail the command itself.
2172 for (wp
= sp
->wp
; (ecp
= LIST_FIRST(&wp
->ecq
)) != &wp
->excmd
;) {
2173 if (FL_ISSET(ecp
->agv_flags
, AGV_ALL
)) {
2174 while ((rp
= TAILQ_FIRST(&ecp
->rq
)) != NULL
) {
2175 TAILQ_REMOVE(&ecp
->rq
, rp
, q
);
2180 LIST_REMOVE(ecp
, q
);
2183 LIST_FIRST(&wp
->ecq
)->clen
= 0;
2189 * Display an unknown command name.
2192 ex_unknown(SCR
*sp
, CHAR_T
*cmd
, size_t len
)
2197 GET_SPACE_GOTOW(sp
, bp
, blen
, len
+ 1);
2199 MEMCPYW(bp
, cmd
, len
);
2200 msgq_wstr(sp
, M_ERR
, bp
, "098|The %s command is unknown");
2201 FREE_SPACEW(sp
, bp
, blen
);
2209 * The vi text input routine needs to know if ex thinks this is an
2210 * [un]abbreviate command, so it can turn off abbreviations. See
2211 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2213 * PUBLIC: int ex_is_abbrev __P((SCR *, CHAR_T *, size_t));
2216 ex_is_abbrev(SCR
*sp
, CHAR_T
*name
, size_t len
)
2218 EXCMDLIST
const *cp
;
2220 return ((cp
= ex_comm_search(sp
, name
, len
)) != NULL
&&
2221 (cp
== &cmds
[C_ABBR
] || cp
== &cmds
[C_UNABBREVIATE
]));
2226 * The vi text input routine needs to know if ex thinks this is an
2227 * unmap command, so it can turn off input mapping. See the usual
2228 * ranting in the vi/v_txt_ev.c:txt_unmap() routine.
2230 * PUBLIC: int ex_is_unmap __P((SCR *, CHAR_T *, size_t));
2233 ex_is_unmap(SCR
*sp
, CHAR_T
*name
, size_t len
)
2235 EXCMDLIST
const *cp
;
2238 * The command the vi input routines are really interested in
2239 * is "unmap!", not just unmap.
2241 if (name
[len
- 1] != '!')
2244 return ((cp
= ex_comm_search(sp
, name
, len
)) != NULL
&&
2245 cp
== &cmds
[C_UNMAP
]);
2250 * Search for a command name.
2252 static EXCMDLIST
const *
2253 ex_comm_search(SCR
*sp
, CHAR_T
*name
, size_t len
)
2255 EXCMDLIST
const *cp
;
2257 for (cp
= cmds
; cp
->name
!= NULL
; ++cp
) {
2258 if (cp
->name
[0] > name
[0])
2260 if (cp
->name
[0] != name
[0])
2262 if (!MEMCMP(name
, cp
->name
, len
))
2270 * Display a bad address message.
2272 * PUBLIC: void ex_badaddr
2273 * PUBLIC: __P((SCR *, EXCMDLIST const *, enum badaddr, enum nresult));
2276 ex_badaddr(SCR
*sp
, const EXCMDLIST
*cp
, enum badaddr ba
, enum nresult nret
)
2284 msgq(sp
, M_SYSERR
, NULL
);
2287 msgq(sp
, M_ERR
, "099|Address value overflow");
2290 msgq(sp
, M_ERR
, "100|Address value underflow");
2295 * When encountering an address error, tell the user if there's no
2296 * underlying file, that's the real problem.
2298 if (sp
->ep
== NULL
) {
2299 ex_wemsg(sp
, cp
? cp
->name
: NULL
, EXM_NOFILEYET
);
2305 msgq(sp
, M_ERR
, "101|Illegal address combination");
2308 if (db_last(sp
, &lno
))
2312 "102|Illegal address: only %lu lines in the file",
2313 (unsigned long)lno
);
2318 msgq(sp
, M_ERR
, "103|Illegal address: the file is empty");
2324 msgq_wstr(sp
, M_ERR
, cp
->name
,
2325 "104|The %s command doesn't permit an address of 0");
2331 #if defined(DEBUG) && defined(COMLOG)
2341 vtrace(sp
, "ecmd: %s", ecp
->cmd
->name
);
2342 if (ecp
->addrcnt
> 0) {
2343 vtrace(sp
, " a1 %d", ecp
->addr1
.lno
);
2344 if (ecp
->addrcnt
> 1)
2345 vtrace(sp
, " a2: %d", ecp
->addr2
.lno
);
2348 vtrace(sp
, " line %d", ecp
->lineno
);
2350 vtrace(sp
, " flags 0x%x", ecp
->flags
);
2351 if (F_ISSET(&exc
, E_BUFFER
))
2352 vtrace(sp
, " buffer "WC
, ecp
->buffer
);
2354 for (cnt
= 0; cnt
< ecp
->argc
; ++cnt
)
2355 vtrace(sp
, " arg %d: {%s}", cnt
, ecp
->argv
[cnt
]->bp
);