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.
13 static const char sccsid
[] = "$Id: ex.c,v 10.74 2001/12/16 18:18:54 skimo Exp $ (Berkeley) $Date: 2001/12/16 18:18:54 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
21 #include <bitstring.h>
31 #include "../common/common.h"
34 #if defined(DEBUG) && defined(COMLOG)
35 static void ex_comlog
__P((SCR
*, EXCMD
*));
37 static EXCMDLIST
const *
38 ex_comm_search
__P((SCR
*, CHAR_T
*, size_t));
39 static int ex_discard
__P((SCR
*));
40 static int ex_line
__P((SCR
*, EXCMD
*, MARK
*, int *, int *));
41 static int ex_load
__P((SCR
*));
42 static void ex_unknown
__P((SCR
*, CHAR_T
*, size_t));
48 * PUBLIC: int ex __P((SCR **));
66 /* Start the ex screen. */
70 /* Flush any saved messages. */
71 while ((mp
= gp
->msgq
.lh_first
) != NULL
) {
72 wp
->scr_msg(sp
, mp
->mtype
, mp
->buf
, mp
->len
);
78 /* If reading from a file, errors should have name and line info. */
79 if (F_ISSET(gp
, G_SCRIPTED
)) {
81 wp
->excmd
.if_name
= "script";
86 * Initialize the text flags. The beautify edit option historically
87 * applied to ex command input read from a file. In addition, the
88 * first time a ^H was discarded from the input, there was a message,
89 * "^H discarded", that was displayed. We don't bother.
91 LF_INIT(TXT_BACKSLASH
| TXT_CNTRLD
| TXT_CR
);
92 for (;; ++wp
->excmd
.if_lno
) {
93 /* Display status line and flush. */
94 if (F_ISSET(sp
, SC_STATUS
)) {
95 if (!F_ISSET(sp
, SC_EX_SILENT
))
96 msgq_status(sp
, sp
->lno
, 0);
101 /* Set the flags the user can reset. */
102 if (O_ISSET(sp
, O_BEAUTIFY
))
103 LF_SET(TXT_BEAUTIFY
);
104 if (O_ISSET(sp
, O_PROMPT
))
107 /* Clear any current interrupts, and get a command. */
109 if (ex_txt(sp
, &sp
->tiq
, ':', flags
))
111 if (INTERRUPTED(sp
)) {
112 (void)ex_puts(sp
, "\n");
117 /* Initialize the command structure. */
118 CLEAR_EX_PARSER(&wp
->excmd
);
121 * If the user entered a single carriage return, send
122 * ex_cmd() a separator -- it discards single newlines.
124 tp
= sp
->tiq
.cqh_first
;
126 static CHAR_T space
= ' ';
127 wp
->excmd
.cp
= &space
; /* __TK__ why not |? */
130 wp
->excmd
.cp
= tp
->lb
;
131 wp
->excmd
.clen
= tp
->len
;
133 F_INIT(&wp
->excmd
, E_NRSEP
);
135 if (ex_cmd(sp
) && F_ISSET(gp
, G_SCRIPTED
))
138 if (INTERRUPTED(sp
)) {
140 msgq(sp
, M_ERR
, "170|Interrupted");
144 * If the last command caused a restart, or switched screens
145 * or into vi, return.
147 if (F_ISSET(gp
, G_SRESTART
) || F_ISSET(sp
, SC_SSWITCH
| SC_VI
)) {
152 /* If the last command switched files, we don't care. */
153 F_CLR(sp
, SC_FSWITCH
);
156 * If we're exiting this screen, move to the next one. By
157 * definition, this means returning into vi, so return to the
158 * main editor loop. The ordering is careful, don't discard
159 * the contents of sp until the end.
161 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
162 if (file_end(sp
, NULL
, F_ISSET(sp
, SC_EXIT_FORCE
)))
164 *spp
= screen_next(sp
);
165 return (screen_end(sp
));
173 * The guts of the ex parser: parse and execute a string containing
177 * This code MODIFIES the string that gets passed in, to delete quoting
178 * characters, etc. The string cannot be readonly/text space, nor should
179 * you expect to use it again after ex_cmd() returns.
182 * For the fun of it, if you want to see if a vi clone got the ex argument
183 * parsing right, try:
185 * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
187 * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
190 * :set|file|append|set|file
192 * For extra credit, try them in a startup .exrc file.
194 * PUBLIC: int ex_cmd __P((SCR *));
206 size_t arg1_len
, discard
, len
;
209 int at_found
, gv_found
;
210 int cnt
, delim
, isaddr
, namelen
;
211 int newscreen
, notempty
, tmp
, vi_address
;
212 CHAR_T
*arg1
, *s
, *p
, *t
;
222 * We always start running the command on the top of the stack.
223 * This means that *everything* must be resolved when we leave
224 * this function for any reason.
226 loop
: ecp
= wp
->ecq
.lh_first
;
228 /* If we're reading a command from a file, set up error information. */
229 if (ecp
->if_name
!= NULL
) {
230 wp
->if_lno
= ecp
->if_lno
;
231 wp
->if_name
= ecp
->if_name
;
235 * If a move to the end of the file is scheduled for this command,
238 if (F_ISSET(ecp
, E_MOVETOEND
)) {
239 if (db_last(sp
, &sp
->lno
))
242 F_CLR(ecp
, E_MOVETOEND
);
245 /* If we found a newline, increment the count now. */
246 if (F_ISSET(ecp
, E_NEWLINE
)) {
249 F_CLR(ecp
, E_NEWLINE
);
252 /* (Re)initialize the EXCMD structure, preserving some flags. */
255 /* Initialize the argument structures. */
256 if (argv_init(sp
, ecp
))
259 /* Initialize +cmd, saved command information. */
261 ecp
->save_cmdlen
= 0;
263 /* Skip <blank>s, empty lines. */
264 for (notempty
= 0; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
)
265 if ((ch
= *ecp
->cp
) == '\n') {
268 } else if (ISBLANK(ch
))
275 * Permit extra colons at the start of the line. Historically,
276 * ex/vi allowed a single extra one. It's simpler not to count.
277 * The stripping is done here because, historically, any command
278 * could have preceding colons, e.g. ":g/pattern/:p" worked.
280 if (ecp
->clen
!= 0 && ch
== ':') {
282 while (--ecp
->clen
> 0 && (ch
= *++ecp
->cp
) == ':');
286 * Command lines that start with a double-quote are comments.
289 * Historically, there was no escape or delimiter for a comment, e.g.
290 * :"foo|set was a single comment and nothing was output. Since nvi
291 * permits users to escape <newline> characters into command lines, we
292 * have to check for that case.
294 if (ecp
->clen
!= 0 && ch
== '"') {
295 while (--ecp
->clen
> 0 && *++ecp
->cp
!= '\n');
296 if (*ecp
->cp
== '\n') {
297 F_SET(ecp
, E_NEWLINE
);
304 /* Skip whitespace. */
305 for (; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
) {
312 * The last point at which an empty line can mean do nothing.
315 * Historically, in ex mode, lines containing only <blank> characters
316 * were the same as a single <carriage-return>, i.e. a default command.
317 * In vi mode, they were ignored. In .exrc files this was a serious
318 * annoyance, as vi kept trying to treat them as print commands. We
319 * ignore backward compatibility in this case, discarding lines that
320 * contain only <blank> characters from .exrc files.
323 * This is where you end up when you're done a command, i.e. clen has
324 * gone to zero. Continue if there are more commands to run.
326 if (ecp
->clen
== 0 &&
327 (!notempty
|| F_ISSET(sp
, SC_VI
) || F_ISSET(ecp
, E_BLIGNORE
))) {
330 ecp
= wp
->ecq
.lh_first
;
337 * Check to see if this is a command for which we may want to move
338 * the cursor back up to the previous line. (The command :1<CR>
339 * wants a <newline> separator, but the command :<CR> wants to erase
340 * the command line.) If the line is empty except for <blank>s,
341 * <carriage-return> or <eof>, we'll probably want to move up. I
342 * don't think there's any way to get <blank> characters *after* the
343 * command character, but this is the ex parser, and I've been wrong
346 if (F_ISSET(ecp
, E_NRSEP
) &&
347 ecp
->clen
!= 0 && (ecp
->clen
!= 1 || ecp
->cp
[0] != '\004'))
350 /* Parse command addresses. */
351 if (ex_range(sp
, ecp
, &tmp
))
357 * Skip <blank>s and any more colons (the command :3,5:print
358 * worked, historically).
360 for (; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
) {
362 if (!ISBLANK(ch
) && ch
!= ':')
367 * If no command, ex does the last specified of p, l, or #, and vi
368 * moves to the line. Otherwise, determine the length of the command
369 * name by looking for the first non-alphabetic character. (There
370 * are a few non-alphabetic characters in command names, but they're
371 * all single character commands.) This isn't a great test, because
372 * it means that, for the command ":e +cut.c file", we'll report that
373 * the command "cut" wasn't known. However, it makes ":e+35 file" work
377 * Historically, lines with multiple adjacent (or <blank> separated)
378 * command separators were very strange. For example, the command
379 * |||<carriage-return>, when the cursor was on line 1, displayed
380 * lines 2, 3 and 5 of the file. In addition, the command " | "
381 * would only display the line after the next line, instead of the
382 * next two lines. No ideas why. It worked reasonably when executed
383 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
384 * command for each separator.
386 #define SINGLE_CHAR_COMMANDS "\004!#&*<=>@~"
388 if (ecp
->clen
!= 0 && ecp
->cp
[0] != '|' && ecp
->cp
[0] != '\n') {
389 if (strchr(SINGLE_CHAR_COMMANDS
, *ecp
->cp
)) {
396 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
397 if (!ISALPHA(*ecp
->cp
))
399 if ((namelen
= ecp
->cp
- p
) == 0) {
400 msgq(sp
, M_ERR
, "080|Unknown command name");
407 * Historic vi permitted flags to immediately follow any
408 * subset of the 'delete' command, but then did not permit
409 * further arguments (flag, buffer, count). Make it work.
410 * Permit further arguments for the few shreds of dignity
413 * Adding commands that start with 'd', and match "delete"
414 * up to a l, p, +, - or # character can break this code.
417 * Capital letters beginning the command names ex, edit,
418 * next, previous, tag and visual (in vi mode) indicate the
419 * command should happen in a new screen.
424 n
= cmds
[C_DELETE
].name
; *s
== *n
; ++s
, ++n
);
425 if (s
[0] == 'l' || s
[0] == 'p' || s
[0] == '+' ||
426 s
[0] == '-' || s
[0] == '^' || s
[0] == '#') {
427 len
= (ecp
->cp
- p
) - (s
- p
);
430 ecp
->rcmd
= cmds
[C_DELETE
];
431 ecp
->rcmd
.syntax
= "1bca1";
432 ecp
->cmd
= &ecp
->rcmd
;
436 case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
438 p
[0] = tolower(p
[0]);
443 * Search the table for the command.
446 * Historic vi permitted the mark to immediately follow the
447 * 'k' in the 'k' command. Make it work.
450 * Historic vi permitted any flag to follow the s command, e.g.
451 * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
452 * Since the following characters all have to be flags, i.e.
453 * alphabetics, we can let the s command routine return errors
454 * if it was some illegal command string. This code will break
455 * if an "sg" or similar command is ever added. The substitute
456 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
457 * follows the 's', but we limit the choices here to "cgr" so
458 * that we get unknown command messages for wrong combinations.
460 if ((ecp
->cmd
= ex_comm_search(sp
, p
, namelen
)) == NULL
)
464 ecp
->cp
-= namelen
- 1;
465 ecp
->clen
+= namelen
- 1;
466 ecp
->cmd
= &cmds
[C_K
];
471 for (s
= p
+ 1, cnt
= namelen
; --cnt
; ++s
)
473 s
[0] != 'g' && s
[0] != 'r')
476 ecp
->cp
-= namelen
- 1;
477 ecp
->clen
+= namelen
- 1;
478 ecp
->rcmd
= cmds
[C_SUBSTITUTE
];
479 ecp
->rcmd
.fn
= ex_subagain
;
480 ecp
->cmd
= &ecp
->rcmd
;
485 unknown
: if (newscreen
)
486 p
[0] = toupper(p
[0]);
487 ex_unknown(sp
, p
, namelen
);
492 * The visual command has a different syntax when called
493 * from ex than when called from a vi colon command. FMH.
494 * Make the change now, before we test for the newscreen
495 * semantic, so that we're testing the right one.
497 skip_srch
: if (ecp
->cmd
== &cmds
[C_VISUAL_EX
] && F_ISSET(sp
, SC_VI
))
498 ecp
->cmd
= &cmds
[C_VISUAL_VI
];
502 * Historic vi permitted a capital 'P' at the beginning of
503 * any command that started with 'p'. Probably wanted the
504 * P[rint] command for backward compatibility, and the code
505 * just made Preserve and Put work by accident. Nvi uses
506 * Previous to mean previous-in-a-new-screen, so be careful.
508 if (newscreen
&& !F_ISSET(ecp
->cmd
, E_NEWSCREEN
) &&
509 (ecp
->cmd
== &cmds
[C_PRINT
] ||
510 ecp
->cmd
== &cmds
[C_PRESERVE
]))
513 /* Test for a newscreen associated with this command. */
514 if (newscreen
&& !F_ISSET(ecp
->cmd
, E_NEWSCREEN
))
517 /* Secure means no shell access. */
518 if (F_ISSET(ecp
->cmd
, E_SECURE
) && O_ISSET(sp
, O_SECURE
)) {
519 ex_wemsg(sp
, ecp
->cmd
->name
, EXM_SECURE
);
524 * Multiple < and > characters; another "feature". Note,
525 * The string passed to the underlying function may not be
526 * nul terminated in this case.
528 if ((ecp
->cmd
== &cmds
[C_SHIFTL
] && *p
== '<') ||
529 (ecp
->cmd
== &cmds
[C_SHIFTR
] && *p
== '>')) {
531 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
534 if (argv_exp0(sp
, ecp
, p
, ecp
->cp
- p
))
538 /* Set the format style flags for the next command. */
539 if (ecp
->cmd
== &cmds
[C_HASH
])
540 exp
->fdef
= E_C_HASH
;
541 else if (ecp
->cmd
== &cmds
[C_LIST
])
542 exp
->fdef
= E_C_LIST
;
543 else if (ecp
->cmd
== &cmds
[C_PRINT
])
544 exp
->fdef
= E_C_PRINT
;
545 F_CLR(ecp
, E_USELASTCMD
);
547 /* Print is the default command. */
548 ecp
->cmd
= &cmds
[C_PRINT
];
550 /* Set the saved format flags. */
551 F_SET(ecp
, exp
->fdef
);
555 * If no address was specified, and it's not a global command,
556 * we up the address by one. (I have no idea why globals are
557 * exempted, but it's (ahem) historic practice.)
559 if (ecp
->addrcnt
== 0 && !F_ISSET(sp
, SC_EX_GLOBAL
)) {
561 ecp
->addr1
.lno
= sp
->lno
+ 1;
562 ecp
->addr1
.cno
= sp
->cno
;
565 F_SET(ecp
, E_USELASTCMD
);
570 * Historically, the number option applied to both ex and vi. One
571 * strangeness was that ex didn't switch display formats until a
572 * command was entered, e.g. <CR>'s after the set didn't change to
573 * the new format, but :1p would.
575 if (O_ISSET(sp
, O_NUMBER
)) {
576 F_SET(ecp
, E_OPTNUM
);
577 FL_SET(ecp
->iflags
, E_C_HASH
);
579 F_CLR(ecp
, E_OPTNUM
);
581 /* Check for ex mode legality. */
582 if (F_ISSET(sp
, SC_EX
) && (F_ISSET(ecp
->cmd
, E_VIONLY
) || newscreen
)) {
584 "082|%s: command not available in ex mode", ecp
->cmd
->name
);
588 /* Add standard command flags. */
589 F_SET(ecp
, ecp
->cmd
->flags
);
591 F_CLR(ecp
, E_NEWSCREEN
);
594 * There are three normal termination cases for an ex command. They
595 * are the end of the string (ecp->clen), or unescaped (by <literal
596 * next> characters) <newline> or '|' characters. As we're now past
597 * possible addresses, we can determine how long the command is, so we
598 * don't have to look for all the possible terminations. Naturally,
599 * there are some exciting special cases:
601 * 1: The bang, global, v and the filter versions of the read and
602 * write commands are delimited by <newline>s (they can contain
604 * 2: The ex, edit, next and visual in vi mode commands all take ex
605 * commands as their first arguments.
606 * 3: The s command takes an RE as its first argument, and wants it
607 * to be specially delimited.
609 * Historically, '|' characters in the first argument of the ex, edit,
610 * next, vi visual, and s commands didn't delimit the command. And,
611 * in the filter cases for read and write, and the bang, global and v
612 * commands, they did not delimit the command at all.
614 * For example, the following commands were legal:
616 * :edit +25|s/abc/ABC/ file.c
618 * :read !spell % | columnate
619 * :global/pattern/p|l
621 * It's not quite as simple as it sounds, however. The command:
625 * was also legal, i.e. the historic ex parser (using the word loosely,
626 * since "parser" implies some regularity of syntax) delimited the RE's
627 * based on its delimiter and not anything so irretrievably vulgar as a
630 * Anyhow, the following code makes this all work. First, for the
631 * special cases we move past their special argument(s). Then, we
632 * do normal command processing on whatever is left. Barf-O-Rama.
634 discard
= 0; /* Characters discarded from the command. */
636 ecp
->save_cmd
= ecp
->cp
;
637 if (ecp
->cmd
== &cmds
[C_EDIT
] || ecp
->cmd
== &cmds
[C_EX
] ||
638 ecp
->cmd
== &cmds
[C_NEXT
] || ecp
->cmd
== &cmds
[C_VISUAL_VI
] ||
639 ecp
->cmd
== &cmds
[C_VSPLIT
]) {
641 * Move to the next non-whitespace character. A '!'
642 * immediately following the command is eaten as a
645 if (ecp
->clen
> 0 && *ecp
->cp
== '!') {
648 FL_SET(ecp
->iflags
, E_C_FORCE
);
650 /* Reset, don't reparse. */
651 ecp
->save_cmd
= ecp
->cp
;
653 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
654 if (!ISBLANK(*ecp
->cp
))
659 * The historic implementation ignored all escape characters
660 * so there was no way to put a space or newline into the +cmd
661 * field. We do a simplistic job of fixing it by moving to the
662 * first whitespace character that isn't escaped. The escaping
663 * characters are stripped as no longer useful.
665 if (ecp
->clen
> 0 && *ecp
->cp
== '+') {
668 for (arg1
= p
= ecp
->cp
;
669 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
671 if (IS_ESCAPE(sp
, ecp
, ch
) &&
676 } else if (ISBLANK(ch
))
680 arg1_len
= ecp
->cp
- arg1
;
682 /* Reset, so the first argument isn't reparsed. */
683 ecp
->save_cmd
= ecp
->cp
;
685 } else if (ecp
->cmd
== &cmds
[C_BANG
] ||
686 ecp
->cmd
== &cmds
[C_GLOBAL
] || ecp
->cmd
== &cmds
[C_V
]) {
690 * We use backslashes to escape <newline> characters, although
691 * this wasn't historic practice for the bang command. It was
692 * for the global and v commands, and it's common usage when
693 * doing text insert during the command. Escaping characters
694 * are stripped as no longer useful.
696 for (p
= ecp
->cp
; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
698 if (ch
== '\\' && ecp
->clen
> 1 && ecp
->cp
[1] == '\n') {
705 } else if (ch
== '\n')
709 } else if (ecp
->cmd
== &cmds
[C_READ
] || ecp
->cmd
== &cmds
[C_WRITE
]) {
711 * For write commands, if the next character is a <blank>, and
712 * the next non-blank character is a '!', it's a filter command
713 * and we want to eat everything up to the <newline>. For read
714 * commands, if the next non-blank character is a '!', it's a
715 * filter command and we want to eat everything up to the next
716 * <newline>. Otherwise, we're done.
718 for (tmp
= 0; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
725 if (ecp
->clen
> 0 && ch
== '!' &&
726 (ecp
->cmd
== &cmds
[C_READ
] || tmp
))
727 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
728 if (ecp
->cp
[0] == '\n')
730 } else if (ecp
->cmd
== &cmds
[C_SUBSTITUTE
]) {
732 * Move to the next non-whitespace character, we'll use it as
733 * the delimiter. If the character isn't an alphanumeric or
734 * a '|', it's the delimiter, so parse it. Otherwise, we're
735 * into something like ":s g", so use the special s command.
737 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
738 if (!ISBLANK(ecp
->cp
[0]))
741 if (ISALNUM(ecp
->cp
[0]) || ecp
->cp
[0] == '|') {
742 ecp
->rcmd
= cmds
[C_SUBSTITUTE
];
743 ecp
->rcmd
.fn
= ex_subagain
;
744 ecp
->cmd
= &ecp
->rcmd
;
745 } else if (ecp
->clen
> 0) {
749 * Backslashes quote delimiter characters for RE's.
750 * The backslashes are NOT removed since they'll be
751 * used by the RE code. Move to the third delimiter
752 * that's not escaped (or the end of the command).
757 for (cnt
= 2; ecp
->clen
> 0 &&
758 cnt
!= 0; --ecp
->clen
, ++ecp
->cp
)
759 if (ecp
->cp
[0] == '\\' &&
763 } else if (ecp
->cp
[0] == delim
)
769 * Use normal quoting and termination rules to find the end of this
774 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
775 * file. It was almost certainly a bug, but that's what bug-for-bug
776 * compatibility means, Grasshopper. Also, ^V's escape the command
777 * delimiters. Literal next quote characters in front of the newlines,
778 * '|' characters or literal next characters are stripped as they're
781 vi_address
= ecp
->clen
!= 0 && ecp
->cp
[0] != '\n';
782 for (p
= ecp
->cp
; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
784 if (IS_ESCAPE(sp
, ecp
, ch
) && ecp
->clen
> 1) {
785 CHAR_T tmp
= ecp
->cp
[1];
786 if (tmp
== '\n' || tmp
== '|') {
796 } else if (ch
== '\n' || ch
== '|') {
798 F_SET(ecp
, E_NEWLINE
);
806 * Save off the next command information, go back to the
807 * original start of the command.
810 ecp
->cp
= ecp
->save_cmd
;
812 ecp
->save_cmdlen
= ecp
->clen
;
813 ecp
->clen
= ((ecp
->save_cmd
- ecp
->cp
) - 1) - discard
;
818 * The "set tags" command historically used a backslash, not the
819 * user's literal next character, to escape whitespace. Handle
820 * it here instead of complicating the argv_exp3() code. Note,
821 * this isn't a particularly complex trap, and if backslashes were
822 * legal in set commands, this would have to be much more complicated.
824 if (ecp
->cmd
== &cmds
[C_SET
])
825 for (p
= ecp
->cp
, len
= ecp
->clen
; len
> 0; --len
, ++p
)
830 * Set the default addresses. It's an error to specify an address for
831 * a command that doesn't take them. If two addresses are specified
832 * for a command that only takes one, lose the first one. Two special
833 * cases here, some commands take 0 or 2 addresses. For most of them
834 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
835 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
837 * Also, if the file is empty, some commands want to use an address of
838 * 0, i.e. the entire file is 0 to 0, and the default first address is
839 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
840 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
841 * case where the 0 address is only valid if it's a default address.
843 * Also, set a flag if we set the default addresses. Some commands
844 * (ex: z) care if the user specified an address or if we just used
845 * the current cursor.
847 switch (F_ISSET(ecp
, E_ADDR1
| E_ADDR2
| E_ADDR2_ALL
| E_ADDR2_NONE
)) {
848 case E_ADDR1
: /* One address: */
849 switch (ecp
->addrcnt
) {
850 case 0: /* Default cursor/empty file. */
852 F_SET(ecp
, E_ADDR_DEF
);
853 if (F_ISSET(ecp
, E_ADDR_ZERODEF
)) {
854 if (db_last(sp
, &lno
))
858 F_SET(ecp
, E_ADDR_ZERO
);
860 ecp
->addr1
.lno
= sp
->lno
;
862 ecp
->addr1
.lno
= sp
->lno
;
863 ecp
->addr1
.cno
= sp
->cno
;
867 case 2: /* Lose the first address. */
869 ecp
->addr1
= ecp
->addr2
;
872 case E_ADDR2_NONE
: /* Zero/two addresses: */
873 if (ecp
->addrcnt
== 0) /* Default to nothing. */
876 case E_ADDR2_ALL
: /* Zero/two addresses: */
877 if (ecp
->addrcnt
== 0) { /* Default entire/empty file. */
878 F_SET(ecp
, E_ADDR_DEF
);
882 else if (db_last(sp
, &ecp
->addr2
.lno
))
884 if (F_ISSET(ecp
, E_ADDR_ZERODEF
) &&
885 ecp
->addr2
.lno
== 0) {
887 F_SET(ecp
, E_ADDR_ZERO
);
890 ecp
->addr1
.cno
= ecp
->addr2
.cno
= 0;
891 F_SET(ecp
, E_ADDR2_ALL
);
895 case E_ADDR2
: /* Two addresses: */
896 two_addr
: switch (ecp
->addrcnt
) {
897 case 0: /* Default cursor/empty file. */
899 F_SET(ecp
, E_ADDR_DEF
);
901 F_ISSET(ecp
, E_ADDR_ZERODEF
)) {
902 if (db_last(sp
, &lno
))
905 ecp
->addr1
.lno
= ecp
->addr2
.lno
= 0;
906 F_SET(ecp
, E_ADDR_ZERO
);
909 ecp
->addr2
.lno
= sp
->lno
;
911 ecp
->addr1
.lno
= ecp
->addr2
.lno
= sp
->lno
;
912 ecp
->addr1
.cno
= ecp
->addr2
.cno
= sp
->cno
;
914 case 1: /* Default to first address. */
916 ecp
->addr2
= ecp
->addr1
;
923 if (ecp
->addrcnt
) /* Error. */
929 * The ^D scroll command historically scrolled the value of the scroll
930 * option or to EOF. It was an error if the cursor was already at EOF.
931 * (Leading addresses were permitted, but were then ignored.)
933 if (ecp
->cmd
== &cmds
[C_SCROLL
]) {
935 ecp
->addr1
.lno
= sp
->lno
+ 1;
936 ecp
->addr2
.lno
= sp
->lno
+ O_VAL(sp
, O_SCROLL
);
937 ecp
->addr1
.cno
= ecp
->addr2
.cno
= sp
->cno
;
938 if (db_last(sp
, &lno
))
940 if (lno
!= 0 && lno
> sp
->lno
&& ecp
->addr2
.lno
> lno
)
941 ecp
->addr2
.lno
= lno
;
945 for (np
= ecp
->cmd
->syntax
; *np
!= '\0'; ++np
) {
947 * The force flag is sensitive to leading whitespace, i.e.
948 * "next !" is different from "next!". Handle it before
949 * skipping leading <blank>s.
952 if (ecp
->clen
> 0 && *ecp
->cp
== '!') {
955 FL_SET(ecp
->iflags
, E_C_FORCE
);
960 /* Skip leading <blank>s. */
961 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
962 if (!ISBLANK(*ecp
->cp
))
968 case '1': /* +, -, #, l, p */
971 * Historically, some flags were ignored depending
972 * on where they occurred in the command line. For
973 * example, in the command, ":3+++p--#", historic vi
974 * acted on the '#' flag, but ignored the '-' flags.
975 * It's unambiguous what the flags mean, so we just
976 * handle them regardless of the stupidity of their
979 for (; ecp
->clen
; --ecp
->clen
, ++ecp
->cp
)
989 F_CLR(ecp
, E_OPTNUM
);
990 FL_SET(ecp
->iflags
, E_C_HASH
);
991 exp
->fdef
|= E_C_HASH
;
994 FL_SET(ecp
->iflags
, E_C_LIST
);
995 exp
->fdef
|= E_C_LIST
;
998 FL_SET(ecp
->iflags
, E_C_PRINT
);
999 exp
->fdef
|= E_C_PRINT
;
1005 case '2': /* -, ., +, ^ */
1006 case '3': /* -, ., +, ^, = */
1007 for (; ecp
->clen
; --ecp
->clen
, ++ecp
->cp
)
1010 FL_SET(ecp
->iflags
, E_C_DASH
);
1013 FL_SET(ecp
->iflags
, E_C_DOT
);
1016 FL_SET(ecp
->iflags
, E_C_PLUS
);
1019 FL_SET(ecp
->iflags
, E_C_CARAT
);
1023 FL_SET(ecp
->iflags
, E_C_EQUAL
);
1031 case 'b': /* buffer */
1034 * Historically, "d #" was a delete with a flag, not a
1035 * delete into the '#' buffer. If the current command
1036 * permits a flag, don't use one as a buffer. However,
1037 * the 'l' and 'p' flags were legal buffer names in the
1038 * historic ex, and were used as buffers, not flags.
1040 if ((ecp
->cp
[0] == '+' || ecp
->cp
[0] == '-' ||
1041 ecp
->cp
[0] == '^' || ecp
->cp
[0] == '#') &&
1042 strchr(np
, '1') != NULL
)
1046 * Digits can't be buffer names in ex commands, or the
1047 * command "d2" would be a delete into buffer '2', and
1048 * not a two-line deletion.
1050 if (!ISDIGIT(ecp
->cp
[0])) {
1051 ecp
->buffer
= *ecp
->cp
;
1054 FL_SET(ecp
->iflags
, E_C_BUFFER
);
1057 case 'c': /* count [01+a] */
1059 /* Validate any signed value. */
1060 if (!ISDIGIT(*ecp
->cp
) && (*np
!= '+' ||
1061 (*ecp
->cp
!= '+' && *ecp
->cp
!= '-')))
1063 /* If a signed value, set appropriate flags. */
1064 if (*ecp
->cp
== '-')
1065 FL_SET(ecp
->iflags
, E_C_COUNT_NEG
);
1066 else if (*ecp
->cp
== '+')
1067 FL_SET(ecp
->iflags
, E_C_COUNT_POS
);
1069 nget_slong(sp
, <mp
, ecp
->cp
, &t
, 10)) != NUM_OK
) {
1070 ex_badaddr(sp
, NULL
, A_NOTSET
, nret
);
1073 if (ltmp
== 0 && *np
!= '0') {
1074 msgq(sp
, M_ERR
, "083|Count may not be zero");
1077 ecp
->clen
-= (t
- ecp
->cp
);
1081 * Counts as address offsets occur in commands taking
1082 * two addresses. Historic vi practice was to use
1083 * the count as an offset from the *second* address.
1085 * Set a count flag; some underlying commands (see
1086 * join) do different things with counts than with
1090 ecp
->addr1
= ecp
->addr2
;
1091 ecp
->addr2
.lno
= ecp
->addr1
.lno
+ ltmp
- 1;
1094 FL_SET(ecp
->iflags
, E_C_COUNT
);
1096 case 'f': /* file */
1097 if (argv_exp2(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1100 case 'l': /* line */
1102 * Get a line specification.
1104 * If the line was a search expression, we may have
1105 * changed state during the call, and we're now
1106 * searching the file. Push ourselves onto the state
1109 if (ex_line(sp
, ecp
, &cur
, &isaddr
, &tmp
))
1114 /* Line specifications are always required. */
1116 msgq_wstr(sp
, M_ERR
, ecp
->cp
,
1117 "084|%s: bad line specification");
1121 * The target line should exist for these commands,
1122 * but 0 is legal for them as well.
1124 if (cur
.lno
!= 0 && !db_exist(sp
, cur
.lno
)) {
1125 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1128 ecp
->lineno
= cur
.lno
;
1130 case 'S': /* string, file exp. */
1131 if (ecp
->clen
!= 0) {
1132 if (argv_exp1(sp
, ecp
, ecp
->cp
,
1133 ecp
->clen
, ecp
->cmd
== &cmds
[C_BANG
]))
1138 case 's': /* string */
1139 if (argv_exp0(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1142 case 'W': /* word string */
1146 * Literal next characters escape the following
1147 * character. Quoting characters are stripped here
1148 * since they are no longer useful.
1150 * First there was the word.
1152 for (p
= t
= ecp
->cp
;
1153 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
1156 ecp
, ch
) && ecp
->clen
> 1) {
1159 } else if (ISBLANK(ch
)) {
1166 if (argv_exp0(sp
, ecp
, t
, p
- t
))
1169 /* Delete intervening whitespace. */
1170 for (; ecp
->clen
> 0;
1171 --ecp
->clen
, ++ecp
->cp
) {
1179 /* Followed by the string. */
1180 for (p
= t
= ecp
->cp
; ecp
->clen
> 0;
1181 --ecp
->clen
, ++ecp
->cp
, ++p
) {
1184 ecp
, ch
) && ecp
->clen
> 1) {
1190 if (argv_exp0(sp
, ecp
, t
, p
- t
))
1193 case 'w': /* word */
1194 if (argv_exp3(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1196 arg_cnt_chk
: if (*++np
!= 'N') { /* N */
1198 * If a number is specified, must either be
1199 * 0 or that number, if optional, and that
1200 * number, if required.
1203 if ((*++np
!= 'o' || exp
->argsoff
!= 0) &&
1204 exp
->argsoff
!= tmp
)
1210 "085|Internal syntax table error (%s: %s)",
1211 ecp
->cmd
->name
, KEY_NAME(sp
, *np
));
1215 /* Skip trailing whitespace. */
1216 for (; ecp
->clen
> 0; --ecp
->clen
) {
1223 * There shouldn't be anything left, and no more required fields,
1224 * i.e neither 'l' or 'r' in the syntax string.
1226 if (ecp
->clen
!= 0 || strpbrk(np
, "lr")) {
1227 usage
: msgq(sp
, M_ERR
, "086|Usage: %s", ecp
->cmd
->usage
);
1232 * Verify that the addresses are legal. Check the addresses here,
1233 * because this is a place where all ex addresses pass through.
1234 * (They don't all pass through ex_line(), for instance.) We're
1235 * assuming that any non-existent line doesn't exist because it's
1236 * past the end-of-file. That's a pretty good guess.
1238 * If it's a "default vi command", an address of zero is okay.
1241 switch (ecp
->addrcnt
) {
1244 * Historic ex/vi permitted commands with counts to go past
1245 * EOF. So, for example, if the file only had 5 lines, the
1246 * ex command "1,6>" would fail, but the command ">300"
1247 * would succeed. Since we don't want to have to make all
1248 * of the underlying commands handle random line numbers,
1251 if (ecp
->addr2
.lno
== 0) {
1252 if (!F_ISSET(ecp
, E_ADDR_ZERO
) &&
1253 (F_ISSET(sp
, SC_EX
) ||
1254 !F_ISSET(ecp
, E_USELASTCMD
))) {
1255 ex_badaddr(sp
, ecp
->cmd
, A_ZERO
, NUM_OK
);
1258 } else if (!db_exist(sp
, ecp
->addr2
.lno
)) {
1259 if (FL_ISSET(ecp
->iflags
, E_C_COUNT
)) {
1260 if (db_last(sp
, &lno
))
1262 ecp
->addr2
.lno
= lno
;
1264 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1270 if (ecp
->addr1
.lno
== 0) {
1271 if (!F_ISSET(ecp
, E_ADDR_ZERO
) &&
1272 (F_ISSET(sp
, SC_EX
) ||
1273 !F_ISSET(ecp
, E_USELASTCMD
))) {
1274 ex_badaddr(sp
, ecp
->cmd
, A_ZERO
, NUM_OK
);
1277 } else if (!db_exist(sp
, ecp
->addr1
.lno
)) {
1278 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1285 * If doing a default command and there's nothing left on the line,
1286 * vi just moves to the line. For example, ":3" and ":'a,'b" just
1287 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1290 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1294 * This is done before the absolute mark gets set; historically,
1295 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1297 if ((F_ISSET(sp
, SC_VI
) || F_ISSET(ecp
, E_NOPRDEF
)) &&
1298 F_ISSET(ecp
, E_USELASTCMD
) && vi_address
== 0) {
1299 switch (ecp
->addrcnt
) {
1302 (ecp
->addr2
.lno
? ecp
->addr2
.lno
: 1)) {
1304 ecp
->addr2
.lno
? ecp
->addr2
.lno
: 1;
1306 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
1311 (ecp
->addr1
.lno
? ecp
->addr1
.lno
: 1)) {
1313 ecp
->addr1
.lno
? ecp
->addr1
.lno
: 1;
1315 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
1319 ecp
->cp
= ecp
->save_cmd
;
1320 ecp
->clen
= ecp
->save_cmdlen
;
1325 * Set the absolute mark -- we have to set it for vi here, in case
1326 * it's a compound command, e.g. ":5p|6" should set the absolute
1329 if (F_ISSET(ecp
, E_ABSMARK
)) {
1332 F_CLR(ecp
, E_ABSMARK
);
1333 if (mark_set(sp
, ABSMARK1
, &cur
, 1))
1337 #if defined(DEBUG) && defined(COMLOG)
1340 /* Increment the command count if not called from vi. */
1341 if (F_ISSET(sp
, SC_EX
))
1345 * If file state available, and not doing a global command,
1346 * log the start of an action.
1348 if (sp
->ep
!= NULL
&& !F_ISSET(sp
, SC_EX_GLOBAL
))
1349 (void)log_cursor(sp
);
1353 * There are two special commands for the purposes of this code: the
1354 * default command (<carriage-return>) or the scrolling commands (^D
1355 * and <EOF>) as the first non-<blank> characters in the line.
1357 * If this is the first command in the command line, we received the
1358 * command from the ex command loop and we're talking to a tty, and
1359 * and there's nothing else on the command line, and it's one of the
1360 * special commands, we move back up to the previous line, and erase
1361 * the prompt character with the output. Since ex runs in canonical
1362 * mode, we don't have to do anything else, a <newline> has already
1363 * been echoed by the tty driver. It's OK if vi calls us -- we won't
1364 * be in ex mode so we'll do nothing.
1366 if (F_ISSET(ecp
, E_NRSEP
)) {
1367 if (sp
->ep
!= NULL
&&
1368 F_ISSET(sp
, SC_EX
) && !F_ISSET(gp
, G_SCRIPTED
) &&
1369 (F_ISSET(ecp
, E_USELASTCMD
) || ecp
->cmd
== &cmds
[C_SCROLL
]))
1370 gp
->scr_ex_adjust(sp
, EX_TERM_SCROLL
);
1371 F_CLR(ecp
, E_NRSEP
);
1375 * Call the underlying function for the ex command.
1378 * Interrupts behave like errors, for now.
1380 if (ecp
->cmd
->fn(sp
, ecp
) || INTERRUPTED(sp
)) {
1381 if (F_ISSET(gp
, G_SCRIPTED
))
1382 F_SET(sp
, SC_EXIT_FORCE
);
1387 /* Make sure no function left global temporary space locked. */
1388 if (F_ISSET(wp
, W_TMP_INUSE
)) {
1389 F_CLR(wp
, W_TMP_INUSE
);
1390 msgq(sp
, M_ERR
, "087|%s: temporary buffer not released",
1395 * Ex displayed the number of lines modified immediately after each
1396 * command, so the command "1,10d|1,10d" would display:
1402 * Executing ex commands from vi only reported the final modified
1403 * lines message -- that's wrong enough that we don't match it.
1405 if (F_ISSET(sp
, SC_EX
))
1409 * Integrate any offset parsed by the underlying command, and make
1410 * sure the referenced line exists.
1413 * May not match historic practice (which I've never been able to
1414 * completely figure out.) For example, the '=' command from vi
1415 * mode often got the offset wrong, and complained it was too large,
1416 * but didn't seem to have a problem with the cursor. If anyone
1417 * complains, ask them how it's supposed to work, they might know.
1419 if (sp
->ep
!= NULL
&& ecp
->flagoff
) {
1420 if (ecp
->flagoff
< 0) {
1421 if (sp
->lno
<= -ecp
->flagoff
) {
1423 "088|Flag offset to before line 1");
1427 if (!NPFITS(DB_MAX_RECORDS
, sp
->lno
, ecp
->flagoff
)) {
1428 ex_badaddr(sp
, NULL
, A_NOTSET
, NUM_OVER
);
1431 if (!db_exist(sp
, sp
->lno
+ ecp
->flagoff
)) {
1433 "089|Flag offset past end-of-file");
1437 sp
->lno
+= ecp
->flagoff
;
1441 * If the command executed successfully, we may want to display a line
1442 * based on the autoprint option or an explicit print flag. (Make sure
1443 * that there's a line to display.) Also, the autoprint edit option is
1444 * turned off for the duration of global commands.
1446 if (F_ISSET(sp
, SC_EX
) && sp
->ep
!= NULL
&& sp
->lno
!= 0) {
1448 * The print commands have already handled the `print' flags.
1449 * If so, clear them.
1451 if (FL_ISSET(ecp
->iflags
, E_CLRFLAG
))
1452 FL_CLR(ecp
->iflags
, E_C_HASH
| E_C_LIST
| E_C_PRINT
);
1454 /* If hash set only because of the number option, discard it. */
1455 if (F_ISSET(ecp
, E_OPTNUM
))
1456 FL_CLR(ecp
->iflags
, E_C_HASH
);
1459 * If there was an explicit flag to display the new cursor line,
1460 * or autoprint is set and a change was made, display the line.
1461 * If any print flags were set use them, else default to print.
1463 LF_INIT(FL_ISSET(ecp
->iflags
, E_C_HASH
| E_C_LIST
| E_C_PRINT
));
1464 if (!LF_ISSET(E_C_HASH
| E_C_LIST
| E_C_PRINT
| E_NOAUTO
) &&
1465 !F_ISSET(sp
, SC_EX_GLOBAL
) &&
1466 O_ISSET(sp
, O_AUTOPRINT
) && F_ISSET(ecp
, E_AUTOPRINT
))
1469 if (LF_ISSET(E_C_HASH
| E_C_LIST
| E_C_PRINT
)) {
1472 (void)ex_print(sp
, ecp
, &cur
, &cur
, flags
);
1477 * If the command had an associated "+cmd", it has to be executed
1478 * before we finish executing any more of this ex command. For
1479 * example, consider a .exrc file that contains the following lines:
1482 * :edit +25 file.c|s/abc/ABC/|1
1485 * This can happen more than once -- the historic vi simply hung or
1486 * dropped core, of course. Prepend the + command back into the
1487 * current command and continue. We may have to add an additional
1488 * <literal next> character. We know that it will fit because we
1489 * discarded at least one space and the + character.
1491 if (arg1_len
!= 0) {
1493 * If the last character of the + command was a <literal next>
1494 * character, it would be treated differently because of the
1495 * append. Quote it, if necessary.
1497 if (IS_ESCAPE(sp
, ecp
, arg1
[arg1_len
- 1])) {
1498 *--ecp
->save_cmd
= CH_LITERAL
;
1502 ecp
->save_cmd
-= arg1_len
;
1503 ecp
->save_cmdlen
+= arg1_len
;
1504 MEMCPYW(ecp
->save_cmd
, arg1
, arg1_len
);
1507 * Any commands executed from a +cmd are executed starting at
1508 * the first column of the last line of the file -- NOT the
1509 * first nonblank.) The main file startup code doesn't know
1510 * that a +cmd was set, however, so it may have put us at the
1511 * top of the file. (Note, this is safe because we must have
1512 * switched files to get here.)
1514 F_SET(ecp
, E_MOVETOEND
);
1517 /* Update the current command. */
1518 ecp
->cp
= ecp
->save_cmd
;
1519 ecp
->clen
= ecp
->save_cmdlen
;
1523 * If we've changed screens or underlying files, any pending global or
1524 * v command, or @ buffer that has associated addresses, has to be
1525 * discarded. This is historic practice for globals, and necessary for
1526 * @ buffers that had associated addresses.
1528 * Otherwise, if we've changed underlying files, it's not a problem,
1529 * we continue with the rest of the ex command(s), operating on the
1530 * new file. However, if we switch screens (either by exiting or by
1531 * an explicit command), we have no way of knowing where to put output
1532 * messages, and, since we don't control screens here, we could screw
1533 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1534 * times). So, return and continue after we've got a new screen.
1536 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
| SC_FSWITCH
| SC_SSWITCH
)) {
1537 at_found
= gv_found
= 0;
1538 for (ecp
= wp
->ecq
.lh_first
;
1539 ecp
!= NULL
; ecp
= ecp
->q
.le_next
)
1540 switch (ecp
->agv_flags
) {
1542 case AGV_AT_NORANGE
:
1548 "090|@ with range running when the file/screen changed");
1556 "091|Global/v command running when the file/screen changed");
1562 if (at_found
|| gv_found
)
1564 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
| SC_SSWITCH
))
1572 * On command failure, we discard keys and pending commands remaining,
1573 * as well as any keys that were mapped and waiting. The save_cmdlen
1574 * test is not necessarily correct. If we fail early enough we don't
1575 * know if the entire string was a single command or not. Guess, as
1576 * it's useful to know if commands other than the current one are being
1579 if (ecp
->save_cmdlen
== 0)
1580 for (; ecp
->clen
; --ecp
->clen
) {
1582 if (IS_ESCAPE(sp
, ecp
, ch
) && ecp
->clen
> 1) {
1585 } else if (ch
== '\n' || ch
== '|') {
1587 ecp
->save_cmdlen
= 1;
1591 if (ecp
->save_cmdlen
!= 0 || wp
->ecq
.lh_first
!= &wp
->excmd
) {
1592 discard
: msgq(sp
, M_BERR
,
1593 "092|Ex command failed: pending commands discarded");
1596 if (v_event_flush(sp
, CH_MAPPED
))
1598 "093|Ex command failed: mapped keys discarded");
1604 /* Turn off any file name error information. */
1607 /* Turn off the global bit. */
1608 F_CLR(sp
, SC_EX_GLOBAL
);
1615 * Get a line range for ex commands, or perform a vi ex address search.
1617 * PUBLIC: int ex_range __P((SCR *, EXCMD *, int *));
1620 ex_range(SCR
*sp
, EXCMD
*ecp
, int *errp
)
1622 enum { ADDR_FOUND
, ADDR_NEED
, ADDR_NONE
} addr
;
1631 * Parse comma or semi-colon delimited line specs.
1633 * Semi-colon delimiters update the current address to be the last
1634 * address. For example, the command
1636 * :3;/pattern/ecp->cp
1638 * will search for pattern from line 3. In addition, if ecp->cp
1639 * is not a valid command, the current line will be left at 3, not
1640 * at the original address.
1642 * Extra addresses are discarded, starting with the first.
1645 * If any addresses are missing, they default to the current line.
1646 * This was historically true for both leading and trailing comma
1647 * delimited addresses as well as for trailing semicolon delimited
1648 * addresses. For consistency, we make it true for leading semicolon
1649 * addresses as well.
1653 for (addr
= ADDR_NONE
, ecp
->addrcnt
= 0; ecp
->clen
> 0;)
1655 case '%': /* Entire file. */
1656 /* Vi ex address searches didn't permit % signs. */
1657 if (F_ISSET(ecp
, E_VISEARCH
))
1660 /* It's an error if the file is empty. */
1661 if (sp
->ep
== NULL
) {
1662 ex_badaddr(sp
, NULL
, A_EMPTY
, NUM_OK
);
1668 * A percent character addresses all of the lines in
1669 * the file. Historically, it couldn't be followed by
1670 * any other address. We do it as a text substitution
1671 * for simplicity. POSIX 1003.2 is expected to follow
1674 * If it's an empty file, the first line is 0, not 1.
1676 if (addr
== ADDR_FOUND
) {
1677 ex_badaddr(sp
, NULL
, A_COMBO
, NUM_OK
);
1681 if (db_last(sp
, &ecp
->addr2
.lno
))
1683 ecp
->addr1
.lno
= ecp
->addr2
.lno
== 0 ? 0 : 1;
1684 ecp
->addr1
.cno
= ecp
->addr2
.cno
= 0;
1690 case ',': /* Comma delimiter. */
1691 /* Vi ex address searches didn't permit commas. */
1692 if (F_ISSET(ecp
, E_VISEARCH
))
1695 case ';': /* Semi-colon delimiter. */
1696 if (sp
->ep
== NULL
) {
1697 ex_badaddr(sp
, NULL
, A_EMPTY
, NUM_OK
);
1701 if (addr
!= ADDR_FOUND
)
1702 switch (ecp
->addrcnt
) {
1704 ecp
->addr1
.lno
= sp
->lno
;
1705 ecp
->addr1
.cno
= sp
->cno
;
1709 ecp
->addr1
= ecp
->addr2
;
1712 ecp
->addr2
.lno
= sp
->lno
;
1713 ecp
->addr2
.cno
= sp
->cno
;
1717 if (*ecp
->cp
== ';')
1718 switch (ecp
->addrcnt
) {
1723 sp
->lno
= ecp
->addr1
.lno
;
1724 sp
->cno
= ecp
->addr1
.cno
;
1727 sp
->lno
= ecp
->addr2
.lno
;
1728 sp
->cno
= ecp
->addr2
.cno
;
1733 case ' ': /* Whitespace. */
1734 case '\t': /* Whitespace. */
1739 /* Get a line specification. */
1740 if (ex_line(sp
, ecp
, &m
, &isaddr
, errp
))
1746 if (addr
== ADDR_FOUND
) {
1747 ex_badaddr(sp
, NULL
, A_COMBO
, NUM_OK
);
1751 switch (ecp
->addrcnt
) {
1761 ecp
->addr1
= ecp
->addr2
;
1771 * Vi ex address searches are indifferent to order or trailing
1774 ret
: if (F_ISSET(ecp
, E_VISEARCH
))
1777 if (addr
== ADDR_NEED
)
1778 switch (ecp
->addrcnt
) {
1780 ecp
->addr1
.lno
= sp
->lno
;
1781 ecp
->addr1
.cno
= sp
->cno
;
1785 ecp
->addr1
= ecp
->addr2
;
1788 ecp
->addr2
.lno
= sp
->lno
;
1789 ecp
->addr2
.cno
= sp
->cno
;
1794 if (ecp
->addrcnt
== 2 && ecp
->addr2
.lno
< ecp
->addr1
.lno
) {
1796 "094|The second address is smaller than the first");
1804 * Get a single line address specifier.
1806 * The way the "previous context" mark worked was that any "non-relative"
1807 * motion set it. While ex/vi wasn't totally consistent about this, ANY
1808 * numeric address, search pattern, '$', or mark reference in an address
1809 * was considered non-relative, and set the value. Which should explain
1810 * why we're hacking marks down here. The problem was that the mark was
1811 * only set if the command was called, i.e. we have to set a flag and test
1815 * This is probably still not exactly historic practice, although I think
1816 * it's fairly close.
1819 ex_line(SCR
*sp
, EXCMD
*ecp
, MARK
*mp
, int *isaddrp
, int *errp
)
1826 int (*sf
) __P((SCR
*, MARK
*, MARK
*, CHAR_T
*, size_t, CHAR_T
**, u_int
));
1832 *isaddrp
= *errp
= 0;
1833 F_CLR(ecp
, E_DELTA
);
1835 /* No addresses permitted until a file has been read in. */
1836 if (sp
->ep
== NULL
&& strchr("$0123456789'\\/?.+-^", *ecp
->cp
)) {
1837 ex_badaddr(sp
, NULL
, A_EMPTY
, NUM_OK
);
1843 case '$': /* Last line in the file. */
1845 F_SET(ecp
, E_ABSMARK
);
1848 if (db_last(sp
, &mp
->lno
))
1852 break; /* Absolute line number. */
1853 case '0': case '1': case '2': case '3': case '4':
1854 case '5': case '6': case '7': case '8': case '9':
1856 F_SET(ecp
, E_ABSMARK
);
1858 if ((nret
= nget_slong(sp
, &val
, ecp
->cp
, &endp
, 10)) != NUM_OK
) {
1859 ex_badaddr(sp
, NULL
, A_NOTSET
, nret
);
1863 if (!NPFITS(DB_MAX_RECORDS
, 0, val
)) {
1864 ex_badaddr(sp
, NULL
, A_NOTSET
, NUM_OVER
);
1870 ecp
->clen
-= (endp
- ecp
->cp
);
1873 case '\'': /* Use a mark. */
1875 F_SET(ecp
, E_ABSMARK
);
1877 if (ecp
->clen
== 1) {
1878 msgq(sp
, M_ERR
, "095|No mark name supplied");
1882 if (mark_get(sp
, ecp
->cp
[1], mp
, M_ERR
)) {
1889 case '\\': /* Search: forward/backward. */
1892 * I can't find any difference between // and \/ or between
1893 * ?? and \?. Mark Horton doesn't remember there being any
1894 * difference. C'est la vie.
1896 if (ecp
->clen
< 2 ||
1897 ecp
->cp
[1] != '/' && ecp
->cp
[1] != '?') {
1898 msgq(sp
, M_ERR
, "096|\\ not followed by / or ?");
1904 sf
= ecp
->cp
[0] == '/' ? f_search
: b_search
;
1906 case '/': /* Search forward. */
1909 case '?': /* Search backward. */
1912 search
: mp
->lno
= sp
->lno
;
1914 if (sf(sp
, mp
, mp
, ecp
->cp
, ecp
->clen
, &endp
,
1915 SEARCH_MSG
| SEARCH_PARSE
| SEARCH_SET
|
1916 (F_ISSET(ecp
, E_SEARCH_WMSG
) ? SEARCH_WMSG
: 0))) {
1921 /* Fix up the command pointers. */
1922 ecp
->clen
-= (endp
- ecp
->cp
);
1926 F_SET(ecp
, E_ABSMARK
);
1928 case '.': /* Current position. */
1932 /* If an empty file, then '.' is 0, not 1. */
1934 if (db_last(sp
, &mp
->lno
))
1943 * Historically, .<number> was the same as .+<number>, i.e.
1944 * the '+' could be omitted. (This feature is found in ed
1947 if (ecp
->clen
> 1 && ISDIGIT(ecp
->cp
[1]))
1956 /* Skip trailing <blank>s. */
1957 for (; ecp
->clen
> 0 &&
1958 ISBLANK(ecp
->cp
[0]); ++ecp
->cp
, --ecp
->clen
);
1961 * Evaluate any offset. If no address yet found, the offset
1962 * is relative to ".".
1965 if (ecp
->clen
!= 0 && (ISDIGIT(ecp
->cp
[0]) ||
1966 ecp
->cp
[0] == '+' || ecp
->cp
[0] == '-' ||
1967 ecp
->cp
[0] == '^')) {
1974 * Evaluate an offset, defined as:
1976 * [+-^<blank>]*[<blank>]*[0-9]*
1978 * The rough translation is any number of signs, optionally
1979 * followed by numbers, or a number by itself, all <blank>
1983 * All address offsets were additive, e.g. "2 2 3p" was the
1984 * same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2".
1985 * Note, however, "2 /ZZZ/" was an error. It was also legal
1986 * to insert signs without numbers, so "3 - 2" was legal, and
1990 * Offsets were historically permitted for any line address,
1991 * e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after
1995 * Offsets were historically permitted for search commands,
1996 * and handled as addresses: "/pattern/2 2 2" was legal, and
1997 * referenced the 6th line after pattern.
1999 F_SET(ecp
, E_DELTA
);
2001 for (; ecp
->clen
> 0 && ISBLANK(ecp
->cp
[0]);
2002 ++ecp
->cp
, --ecp
->clen
);
2003 if (ecp
->clen
== 0 || !ISDIGIT(ecp
->cp
[0]) &&
2004 ecp
->cp
[0] != '+' && ecp
->cp
[0] != '-' &&
2007 if (!ISDIGIT(ecp
->cp
[0]) &&
2008 !ISDIGIT(ecp
->cp
[1])) {
2009 total
+= ecp
->cp
[0] == '+' ? 1 : -1;
2013 if (ecp
->cp
[0] == '-' ||
2014 ecp
->cp
[0] == '^') {
2021 /* Get a signed long, add it to the total. */
2022 if ((nret
= nget_slong(sp
, &val
,
2023 ecp
->cp
, &endp
, 10)) != NUM_OK
||
2024 (nret
= NADD_SLONG(sp
,
2025 total
, val
)) != NUM_OK
) {
2026 ex_badaddr(sp
, NULL
, A_NOTSET
, nret
);
2030 total
+= isneg
? -val
: val
;
2031 ecp
->clen
-= (endp
- ecp
->cp
);
2038 * Any value less than 0 is an error. Make sure that the new value
2039 * will fit into a db_recno_t.
2041 if (*isaddrp
&& total
!= 0) {
2043 if (-total
> mp
->lno
) {
2045 "097|Reference to a line number less than 0");
2050 if (!NPFITS(DB_MAX_RECORDS
, mp
->lno
, total
)) {
2051 ex_badaddr(sp
, NULL
, A_NOTSET
, NUM_OVER
);
2063 * Load up the next command, which may be an @ buffer or global command.
2072 F_CLR(sp
, SC_EX_GLOBAL
);
2075 * Lose any exhausted commands. We know that the first command
2076 * can't be an AGV command, which makes things a bit easier.
2078 for (wp
= sp
->wp
;;) {
2080 * If we're back to the original structure, leave it around,
2081 * but discard any allocated source name, we've returned to
2082 * the beginning of the command stack.
2084 if ((ecp
= wp
->ecq
.lh_first
) == &wp
->excmd
) {
2085 if (F_ISSET(ecp
, E_NAMEDISCARD
)) {
2087 ecp
->if_name
= NULL
;
2093 * ecp->clen will be 0 for the first discarded command, but
2094 * may not be 0 for subsequent ones, e.g. if the original
2095 * command was ":g/xx/@a|s/b/c/", then when we discard the
2096 * command pushed on the stack by the @a, we have to resume
2097 * the global command which included the substitute command.
2103 * If it's an @, global or v command, we may need to continue
2104 * the command on a different line.
2106 if (FL_ISSET(ecp
->agv_flags
, AGV_ALL
)) {
2107 /* Discard any exhausted ranges. */
2108 while ((rp
= ecp
->rq
.cqh_first
) != (void *)&ecp
->rq
)
2109 if (rp
->start
> rp
->stop
) {
2110 CIRCLEQ_REMOVE(&ecp
->rq
, rp
, q
);
2115 /* If there's another range, continue with it. */
2116 if (rp
!= (void *)&ecp
->rq
)
2119 /* If it's a global/v command, fix up the last line. */
2120 if (FL_ISSET(ecp
->agv_flags
,
2121 AGV_GLOBAL
| AGV_V
) && ecp
->range_lno
!= OOBLNO
)
2122 if (db_exist(sp
, ecp
->range_lno
))
2123 sp
->lno
= ecp
->range_lno
;
2125 if (db_last(sp
, &sp
->lno
))
2133 /* Discard the EXCMD. */
2134 LIST_REMOVE(ecp
, q
);
2139 * We only get here if it's an active @, global or v command. Set
2140 * the current line number, and get a new copy of the command for
2141 * the parser. Note, the original pointer almost certainly moved,
2142 * so we have play games.
2144 ecp
->cp
= ecp
->o_cp
;
2145 MEMCPYW(ecp
->cp
, ecp
->cp
+ ecp
->o_clen
, ecp
->o_clen
);
2146 ecp
->clen
= ecp
->o_clen
;
2147 ecp
->range_lno
= sp
->lno
= rp
->start
++;
2149 if (FL_ISSET(ecp
->agv_flags
, AGV_GLOBAL
| AGV_V
))
2150 F_SET(sp
, SC_EX_GLOBAL
);
2156 * Discard any pending ex commands.
2166 * We know the first command can't be an AGV command, so we don't
2167 * process it specially. We do, however, nail the command itself.
2169 for (wp
= sp
->wp
; (ecp
= wp
->ecq
.lh_first
) != &wp
->excmd
;) {
2170 if (FL_ISSET(ecp
->agv_flags
, AGV_ALL
)) {
2171 while ((rp
= ecp
->rq
.cqh_first
) != (void *)&ecp
->rq
) {
2172 CIRCLEQ_REMOVE(&ecp
->rq
, rp
, q
);
2177 LIST_REMOVE(ecp
, q
);
2180 wp
->ecq
.lh_first
->clen
= 0;
2186 * Display an unknown command name.
2189 ex_unknown(SCR
*sp
, CHAR_T
*cmd
, size_t len
)
2194 GET_SPACE_GOTOW(sp
, bp
, blen
, len
+ 1);
2196 MEMCPYW(bp
, cmd
, len
);
2197 msgq_wstr(sp
, M_ERR
, bp
, "098|The %s command is unknown");
2198 FREE_SPACEW(sp
, bp
, blen
);
2206 * The vi text input routine needs to know if ex thinks this is an
2207 * [un]abbreviate command, so it can turn off abbreviations. See
2208 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2210 * PUBLIC: int ex_is_abbrev __P((SCR *, CHAR_T *, size_t));
2213 ex_is_abbrev(SCR
*sp
, CHAR_T
*name
, size_t len
)
2215 EXCMDLIST
const *cp
;
2217 return ((cp
= ex_comm_search(sp
, name
, len
)) != NULL
&&
2218 (cp
== &cmds
[C_ABBR
] || cp
== &cmds
[C_UNABBREVIATE
]));
2223 * The vi text input routine needs to know if ex thinks this is an
2224 * unmap command, so it can turn off input mapping. See the usual
2225 * ranting in the vi/v_txt_ev.c:txt_unmap() routine.
2227 * PUBLIC: int ex_is_unmap __P((SCR *, CHAR_T *, size_t));
2230 ex_is_unmap(SCR
*sp
, CHAR_T
*name
, size_t len
)
2232 EXCMDLIST
const *cp
;
2235 * The command the vi input routines are really interested in
2236 * is "unmap!", not just unmap.
2238 if (name
[len
- 1] != '!')
2241 return ((cp
= ex_comm_search(sp
, name
, len
)) != NULL
&&
2242 cp
== &cmds
[C_UNMAP
]);
2247 * Search for a command name.
2249 static EXCMDLIST
const *
2250 ex_comm_search(SCR
*sp
, CHAR_T
*name
, size_t len
)
2252 EXCMDLIST
const *cp
;
2254 for (cp
= cmds
; cp
->name
!= NULL
; ++cp
) {
2255 if (cp
->name
[0] > name
[0])
2257 if (cp
->name
[0] != name
[0])
2259 if (!MEMCMP(name
, cp
->name
, len
))
2267 * Display a bad address message.
2269 * PUBLIC: void ex_badaddr
2270 * PUBLIC: __P((SCR *, EXCMDLIST const *, enum badaddr, enum nresult));
2273 ex_badaddr(SCR
*sp
, const EXCMDLIST
*cp
, enum badaddr ba
, enum nresult nret
)
2281 msgq(sp
, M_SYSERR
, NULL
);
2284 msgq(sp
, M_ERR
, "099|Address value overflow");
2287 msgq(sp
, M_ERR
, "100|Address value underflow");
2292 * When encountering an address error, tell the user if there's no
2293 * underlying file, that's the real problem.
2295 if (sp
->ep
== NULL
) {
2296 ex_wemsg(sp
, cp
? cp
->name
: NULL
, EXM_NOFILEYET
);
2302 msgq(sp
, M_ERR
, "101|Illegal address combination");
2305 if (db_last(sp
, &lno
))
2309 "102|Illegal address: only %lu lines in the file",
2315 msgq(sp
, M_ERR
, "103|Illegal address: the file is empty");
2322 "104|The %s command doesn't permit an address of 0",
2329 #if defined(DEBUG) && defined(COMLOG)
2339 vtrace(sp
, "ecmd: %s", ecp
->cmd
->name
);
2340 if (ecp
->addrcnt
> 0) {
2341 vtrace(sp
, " a1 %d", ecp
->addr1
.lno
);
2342 if (ecp
->addrcnt
> 1)
2343 vtrace(sp
, " a2: %d", ecp
->addr2
.lno
);
2346 vtrace(sp
, " line %d", ecp
->lineno
);
2348 vtrace(sp
, " flags 0x%x", ecp
->flags
);
2349 if (F_ISSET(&exc
, E_BUFFER
))
2350 vtrace(sp
, " buffer %c", ecp
->buffer
);
2352 for (cnt
= 0; cnt
< ecp
->argc
; ++cnt
)
2353 vtrace(sp
, " arg %d: {%s}", cnt
, ecp
->argv
[cnt
]->bp
);