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.80 2012/10/03 16:24:40 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
30 #include "../common/common.h"
33 #if defined(DEBUG) && defined(COMLOG)
34 static void ex_comlog(SCR
*, EXCMD
*);
36 static EXCMDLIST
const *
37 ex_comm_search(CHAR_T
*, size_t);
38 static int ex_discard(SCR
*);
39 static int ex_line(SCR
*, EXCMD
*, MARK
*, int *, int *);
40 static int ex_load(SCR
*);
41 static void ex_unknown(SCR
*, CHAR_T
*, size_t);
47 * PUBLIC: int ex(SCR **);
63 /* Start the ex screen. */
67 /* Flush any saved messages. */
68 while ((mp
= SLIST_FIRST(gp
->msgq
)) != NULL
) {
69 gp
->scr_msg(sp
, mp
->mtype
, mp
->buf
, mp
->len
);
70 SLIST_REMOVE_HEAD(gp
->msgq
, q
);
75 /* If reading from a file, errors should have name and line info. */
76 if (F_ISSET(gp
, G_SCRIPTED
)) {
78 gp
->excmd
.if_name
= "script";
83 * Initialize the text flags. The beautify edit option historically
84 * applied to ex command input read from a file. In addition, the
85 * first time a ^H was discarded from the input, there was a message,
86 * "^H discarded", that was displayed. We don't bother.
88 LF_INIT(TXT_BACKSLASH
| TXT_CNTRLD
| TXT_CR
);
89 for (;; ++gp
->excmd
.if_lno
) {
90 /* Display status line and flush. */
91 if (F_ISSET(sp
, SC_STATUS
)) {
92 if (!F_ISSET(sp
, SC_EX_SILENT
))
93 msgq_status(sp
, sp
->lno
, 0);
98 /* Set the flags the user can reset. */
99 if (O_ISSET(sp
, O_BEAUTIFY
))
100 LF_SET(TXT_BEAUTIFY
);
101 if (O_ISSET(sp
, O_PROMPT
))
104 /* Clear any current interrupts, and get a command. */
106 if (ex_txt(sp
, sp
->tiq
, ':', flags
))
108 if (INTERRUPTED(sp
)) {
109 (void)ex_puts(sp
, "\n");
114 /* Initialize the command structure. */
115 CLEAR_EX_PARSER(&gp
->excmd
);
118 * If the user entered a single carriage return, send
119 * ex_cmd() a separator -- it discards single newlines.
121 tp
= TAILQ_FIRST(sp
->tiq
);
123 gp
->excmd
.cp
= L(" "); /* __TK__ why not |? */
126 gp
->excmd
.cp
= tp
->lb
;
127 gp
->excmd
.clen
= tp
->len
;
129 F_INIT(&gp
->excmd
, E_NRSEP
);
131 if (ex_cmd(sp
) && F_ISSET(gp
, G_SCRIPTED
))
134 if (INTERRUPTED(sp
)) {
136 msgq(sp
, M_ERR
, "170|Interrupted");
140 * If the last command caused a restart, or switched screens
141 * or into vi, return.
143 if (F_ISSET(gp
, G_SRESTART
) || F_ISSET(sp
, SC_SSWITCH
| SC_VI
)) {
148 /* If the last command switched files, we don't care. */
149 F_CLR(sp
, SC_FSWITCH
);
152 * If we're exiting this screen, move to the next one. By
153 * definition, this means returning into vi, so return to the
154 * main editor loop. The ordering is careful, don't discard
155 * the contents of sp until the end.
157 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
158 if (file_end(sp
, NULL
, F_ISSET(sp
, SC_EXIT_FORCE
)))
160 *spp
= screen_next(sp
);
161 return (screen_end(sp
));
169 * The guts of the ex parser: parse and execute a string containing
173 * This code MODIFIES the string that gets passed in, to delete quoting
174 * characters, etc. The string cannot be readonly/text space, nor should
175 * you expect to use it again after ex_cmd() returns.
178 * For the fun of it, if you want to see if a vi clone got the ex argument
179 * parsing right, try:
181 * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
183 * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
186 * :set|file|append|set|file
188 * For extra credit, try them in a startup .exrc file.
190 * PUBLIC: int ex_cmd(SCR *);
201 size_t arg1_len
, discard
, len
;
204 int at_found
, gv_found
;
205 int cnt
, delim
, isaddr
, namelen
;
206 int newscreen
, notempty
, tmp
, vi_address
;
207 CHAR_T
*arg1
, *s
, *p
, *t
;
216 * We always start running the command on the top of the stack.
217 * This means that *everything* must be resolved when we leave
218 * this function for any reason.
220 loop
: ecp
= SLIST_FIRST(gp
->ecq
);
222 /* If we're reading a command from a file, set up error information. */
223 if (ecp
->if_name
!= NULL
) {
224 gp
->if_lno
= ecp
->if_lno
;
225 gp
->if_name
= ecp
->if_name
;
229 * If a move to the end of the file is scheduled for this command,
232 if (F_ISSET(ecp
, E_MOVETOEND
)) {
233 if (db_last(sp
, &sp
->lno
))
236 F_CLR(ecp
, E_MOVETOEND
);
239 /* If we found a newline, increment the count now. */
240 if (F_ISSET(ecp
, E_NEWLINE
)) {
243 F_CLR(ecp
, E_NEWLINE
);
246 /* (Re)initialize the EXCMD structure, preserving some flags. */
249 /* Initialize the argument structures. */
250 if (argv_init(sp
, ecp
))
253 /* Initialize +cmd, saved command information. */
255 ecp
->save_cmdlen
= 0;
257 /* Skip <blank>s, empty lines. */
258 for (notempty
= 0; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
)
259 if ((ch
= *ecp
->cp
) == '\n') {
262 } else if (cmdskip(ch
))
269 * Permit extra colons at the start of the line. Historically,
270 * ex/vi allowed a single extra one. It's simpler not to count.
271 * The stripping is done here because, historically, any command
272 * could have preceding colons, e.g. ":g/pattern/:p" worked.
274 if (ecp
->clen
!= 0 && ch
== ':') {
276 while (--ecp
->clen
> 0 && (ch
= *++ecp
->cp
) == ':');
280 * Command lines that start with a double-quote are comments.
283 * Historically, there was no escape or delimiter for a comment, e.g.
284 * :"foo|set was a single comment and nothing was output. Since nvi
285 * permits users to escape <newline> characters into command lines, we
286 * have to check for that case.
288 if (ecp
->clen
!= 0 && ch
== '"') {
289 while (--ecp
->clen
> 0 && *++ecp
->cp
!= '\n');
290 if (*ecp
->cp
== '\n') {
291 F_SET(ecp
, E_NEWLINE
);
298 /* Skip whitespace. */
299 for (; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
) {
306 * The last point at which an empty line can mean do nothing.
309 * Historically, in ex mode, lines containing only <blank> characters
310 * were the same as a single <carriage-return>, i.e. a default command.
311 * In vi mode, they were ignored. In .exrc files this was a serious
312 * annoyance, as vi kept trying to treat them as print commands. We
313 * ignore backward compatibility in this case, discarding lines that
314 * contain only <blank> characters from .exrc files.
317 * This is where you end up when you're done a command, i.e. clen has
318 * gone to zero. Continue if there are more commands to run.
320 if (ecp
->clen
== 0 &&
321 (!notempty
|| F_ISSET(sp
, SC_VI
) || F_ISSET(ecp
, E_BLIGNORE
))) {
324 ecp
= SLIST_FIRST(gp
->ecq
);
331 * Check to see if this is a command for which we may want to move
332 * the cursor back up to the previous line. (The command :1<CR>
333 * wants a <newline> separator, but the command :<CR> wants to erase
334 * the command line.) If the line is empty except for <blank>s,
335 * <carriage-return> or <eof>, we'll probably want to move up. I
336 * don't think there's any way to get <blank> characters *after* the
337 * command character, but this is the ex parser, and I've been wrong
340 if (F_ISSET(ecp
, E_NRSEP
) &&
341 ecp
->clen
!= 0 && (ecp
->clen
!= 1 || ecp
->cp
[0] != '\004'))
344 /* Parse command addresses. */
345 if (ex_range(sp
, ecp
, &tmp
))
351 * Skip <blank>s and any more colons (the command :3,5:print
352 * worked, historically).
354 for (; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
) {
356 if (!cmdskip(ch
) && ch
!= ':')
361 * If no command, ex does the last specified of p, l, or #, and vi
362 * moves to the line. Otherwise, determine the length of the command
363 * name by looking for the first non-alphabetic character. (There
364 * are a few non-alphabetic characters in command names, but they're
365 * all single character commands.) This isn't a great test, because
366 * it means that, for the command ":e +cut.c file", we'll report that
367 * the command "cut" wasn't known. However, it makes ":e+35 file" work
371 * Historically, lines with multiple adjacent (or <blank> separated)
372 * command separators were very strange. For example, the command
373 * |||<carriage-return>, when the cursor was on line 1, displayed
374 * lines 2, 3 and 5 of the file. In addition, the command " | "
375 * would only display the line after the next line, instead of the
376 * next two lines. No ideas why. It worked reasonably when executed
377 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
378 * command for each separator.
380 #define SINGLE_CHAR_COMMANDS L("\004!#&*<=>@~")
382 if (ecp
->clen
!= 0 && ecp
->cp
[0] != '|' && ecp
->cp
[0] != '\n') {
383 if (STRCHR(SINGLE_CHAR_COMMANDS
, *ecp
->cp
)) {
390 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
391 if (!isascii(*ecp
->cp
) || !isalpha(*ecp
->cp
))
393 if ((namelen
= ecp
->cp
- p
) == 0) {
394 msgq(sp
, M_ERR
, "080|Unknown command name");
401 * Historic vi permitted flags to immediately follow any
402 * subset of the 'delete' command, but then did not permit
403 * further arguments (flag, buffer, count). Make it work.
404 * Permit further arguments for the few shreds of dignity
407 * Adding commands that start with 'd', and match "delete"
408 * up to a l, p, +, - or # character can break this code.
411 * Capital letters beginning the command names ex, edit,
412 * next, previous, tag and visual (in vi mode) indicate the
413 * command should happen in a new screen.
418 n
= cmds
[C_DELETE
].name
; *s
== *n
; ++s
, ++n
);
419 if (s
[0] == 'l' || s
[0] == 'p' || s
[0] == '+' ||
420 s
[0] == '-' || s
[0] == '^' || s
[0] == '#') {
421 len
= (ecp
->cp
- p
) - (s
- p
);
424 ecp
->rcmd
= cmds
[C_DELETE
];
425 ecp
->rcmd
.syntax
= "1bca1";
426 ecp
->cmd
= &ecp
->rcmd
;
430 case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
432 p
[0] = tolower(p
[0]);
437 * Search the table for the command.
440 * Historic vi permitted the mark to immediately follow the
441 * 'k' in the 'k' command. Make it work.
444 * Historic vi permitted any flag to follow the s command, e.g.
445 * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
446 * Since the following characters all have to be flags, i.e.
447 * alphabetics, we can let the s command routine return errors
448 * if it was some illegal command string. This code will break
449 * if an "sg" or similar command is ever added. The substitute
450 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
451 * follows the 's', but we limit the choices here to "cgr" so
452 * that we get unknown command messages for wrong combinations.
454 if ((ecp
->cmd
= ex_comm_search(p
, namelen
)) == NULL
)
458 ecp
->cp
-= namelen
- 1;
459 ecp
->clen
+= namelen
- 1;
460 ecp
->cmd
= &cmds
[C_K
];
465 for (s
= p
+ 1, cnt
= namelen
; --cnt
; ++s
)
467 s
[0] != 'g' && s
[0] != 'r')
470 ecp
->cp
-= namelen
- 1;
471 ecp
->clen
+= namelen
- 1;
472 ecp
->rcmd
= cmds
[C_SUBSTITUTE
];
473 ecp
->rcmd
.fn
= ex_subagain
;
474 ecp
->cmd
= &ecp
->rcmd
;
479 unknown
: if (newscreen
)
480 p
[0] = toupper(p
[0]);
481 ex_unknown(sp
, p
, namelen
);
486 * The visual command has a different syntax when called
487 * from ex than when called from a vi colon command. FMH.
488 * Make the change now, before we test for the newscreen
489 * semantic, so that we're testing the right one.
491 skip_srch
: if (ecp
->cmd
== &cmds
[C_VISUAL_EX
] && F_ISSET(sp
, SC_VI
))
492 ecp
->cmd
= &cmds
[C_VISUAL_VI
];
496 * Historic vi permitted a capital 'P' at the beginning of
497 * any command that started with 'p'. Probably wanted the
498 * P[rint] command for backward compatibility, and the code
499 * just made Preserve and Put work by accident. Nvi uses
500 * Previous to mean previous-in-a-new-screen, so be careful.
502 if (newscreen
&& !F_ISSET(ecp
->cmd
, E_NEWSCREEN
) &&
503 (ecp
->cmd
== &cmds
[C_PRINT
] ||
504 ecp
->cmd
== &cmds
[C_PRESERVE
]))
507 /* Test for a newscreen associated with this command. */
508 if (newscreen
&& !F_ISSET(ecp
->cmd
, E_NEWSCREEN
))
511 /* Secure means no shell access. */
512 if (F_ISSET(ecp
->cmd
, E_SECURE
) && O_ISSET(sp
, O_SECURE
)) {
513 ex_wemsg(sp
, ecp
->cmd
->name
, EXM_SECURE
);
518 * Multiple < and > characters; another "feature". Note,
519 * The string passed to the underlying function may not be
520 * nul terminated in this case.
522 if ((ecp
->cmd
== &cmds
[C_SHIFTL
] && *p
== '<') ||
523 (ecp
->cmd
== &cmds
[C_SHIFTR
] && *p
== '>')) {
525 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
528 if (argv_exp0(sp
, ecp
, p
, ecp
->cp
- p
))
532 /* Set the format style flags for the next command. */
533 if (ecp
->cmd
== &cmds
[C_HASH
])
534 exp
->fdef
= E_C_HASH
;
535 else if (ecp
->cmd
== &cmds
[C_LIST
])
536 exp
->fdef
= E_C_LIST
;
537 else if (ecp
->cmd
== &cmds
[C_PRINT
])
538 exp
->fdef
= E_C_PRINT
;
539 F_CLR(ecp
, E_USELASTCMD
);
541 /* Print is the default command. */
542 ecp
->cmd
= &cmds
[C_PRINT
];
544 /* Set the saved format flags. */
545 F_SET(ecp
, exp
->fdef
);
549 * If no address was specified, and it's not a global command,
550 * we up the address by one. (I have no idea why globals are
551 * exempted, but it's (ahem) historic practice.)
553 if (ecp
->addrcnt
== 0 && !F_ISSET(sp
, SC_EX_GLOBAL
)) {
555 ecp
->addr1
.lno
= sp
->lno
+ 1;
556 ecp
->addr1
.cno
= sp
->cno
;
559 F_SET(ecp
, E_USELASTCMD
);
564 * Historically, the number option applied to both ex and vi. One
565 * strangeness was that ex didn't switch display formats until a
566 * command was entered, e.g. <CR>'s after the set didn't change to
567 * the new format, but :1p would.
569 if (O_ISSET(sp
, O_NUMBER
)) {
570 F_SET(ecp
, E_OPTNUM
);
571 FL_SET(ecp
->iflags
, E_C_HASH
);
573 F_CLR(ecp
, E_OPTNUM
);
575 /* Check for ex mode legality. */
576 if (F_ISSET(sp
, SC_EX
) && (F_ISSET(ecp
->cmd
, E_VIONLY
) || newscreen
)) {
577 msgq_wstr(sp
, M_ERR
, ecp
->cmd
->name
,
578 "082|%s: command not available in ex mode");
582 /* Add standard command flags. */
583 F_SET(ecp
, ecp
->cmd
->flags
);
585 F_CLR(ecp
, E_NEWSCREEN
);
588 * There are three normal termination cases for an ex command. They
589 * are the end of the string (ecp->clen), or unescaped (by <literal
590 * next> characters) <newline> or '|' characters. As we're now past
591 * possible addresses, we can determine how long the command is, so we
592 * don't have to look for all the possible terminations. Naturally,
593 * there are some exciting special cases:
595 * 1: The bang, global, v and the filter versions of the read and
596 * write commands are delimited by <newline>s (they can contain
598 * 2: The ex, edit, next and visual in vi mode commands all take ex
599 * commands as their first arguments.
600 * 3: The s command takes an RE as its first argument, and wants it
601 * to be specially delimited.
603 * Historically, '|' characters in the first argument of the ex, edit,
604 * next, vi visual, and s commands didn't delimit the command. And,
605 * in the filter cases for read and write, and the bang, global and v
606 * commands, they did not delimit the command at all.
608 * For example, the following commands were legal:
610 * :edit +25|s/abc/ABC/ file.c
612 * :read !spell % | columnate
613 * :global/pattern/p|l
615 * It's not quite as simple as it sounds, however. The command:
619 * was also legal, i.e. the historic ex parser (using the word loosely,
620 * since "parser" implies some regularity of syntax) delimited the RE's
621 * based on its delimiter and not anything so irretrievably vulgar as a
624 * Anyhow, the following code makes this all work. First, for the
625 * special cases we move past their special argument(s). Then, we
626 * do normal command processing on whatever is left. Barf-O-Rama.
628 discard
= 0; /* Characters discarded from the command. */
630 ecp
->save_cmd
= ecp
->cp
;
631 if (ecp
->cmd
== &cmds
[C_EDIT
] || ecp
->cmd
== &cmds
[C_EX
] ||
632 ecp
->cmd
== &cmds
[C_NEXT
] || ecp
->cmd
== &cmds
[C_VISUAL_VI
] ||
633 ecp
->cmd
== &cmds
[C_VSPLIT
]) {
635 * Move to the next non-whitespace character. A '!'
636 * immediately following the command is eaten as a
639 if (ecp
->clen
> 0 && *ecp
->cp
== '!') {
642 FL_SET(ecp
->iflags
, E_C_FORCE
);
644 /* Reset, don't reparse. */
645 ecp
->save_cmd
= ecp
->cp
;
647 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
648 if (!cmdskip(*ecp
->cp
))
653 * The historic implementation ignored all escape characters
654 * so there was no way to put a space or newline into the +cmd
655 * field. We do a simplistic job of fixing it by moving to the
656 * first whitespace character that isn't escaped. The escaping
657 * characters are stripped as no longer useful.
659 if (ecp
->clen
> 0 && *ecp
->cp
== '+') {
662 for (arg1
= p
= ecp
->cp
;
663 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
665 if (IS_ESCAPE(sp
, ecp
, ch
) &&
670 } else if (cmdskip(ch
))
674 arg1_len
= ecp
->cp
- arg1
;
676 /* Reset, so the first argument isn't reparsed. */
677 ecp
->save_cmd
= ecp
->cp
;
679 } else if (ecp
->cmd
== &cmds
[C_BANG
] ||
680 ecp
->cmd
== &cmds
[C_GLOBAL
] || ecp
->cmd
== &cmds
[C_V
]) {
684 * We use backslashes to escape <newline> characters, although
685 * this wasn't historic practice for the bang command. It was
686 * for the global and v commands, and it's common usage when
687 * doing text insert during the command. Escaping characters
688 * are stripped as no longer useful.
690 for (p
= ecp
->cp
; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
692 if (ch
== '\\' && ecp
->clen
> 1 && ecp
->cp
[1] == '\n') {
699 } else if (ch
== '\n')
703 } else if (ecp
->cmd
== &cmds
[C_READ
] || ecp
->cmd
== &cmds
[C_WRITE
]) {
705 * For write commands, if the next character is a <blank>, and
706 * the next non-blank character is a '!', it's a filter command
707 * and we want to eat everything up to the <newline>. For read
708 * commands, if the next non-blank character is a '!', it's a
709 * filter command and we want to eat everything up to the next
710 * <newline>. Otherwise, we're done.
712 for (tmp
= 0; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
719 if (ecp
->clen
> 0 && ch
== '!' &&
720 (ecp
->cmd
== &cmds
[C_READ
] || tmp
))
721 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
722 if (ecp
->cp
[0] == '\n')
724 } else if (ecp
->cmd
== &cmds
[C_SUBSTITUTE
]) {
726 * Move to the next non-whitespace character, we'll use it as
727 * the delimiter. If the character isn't an alphanumeric or
728 * a '|', it's the delimiter, so parse it. Otherwise, we're
729 * into something like ":s g", so use the special s command.
731 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
732 if (!cmdskip(ecp
->cp
[0]))
735 if (!isascii(ecp
->cp
[0]) ||
736 isalnum(ecp
->cp
[0]) || ecp
->cp
[0] == '|') {
737 ecp
->rcmd
= cmds
[C_SUBSTITUTE
];
738 ecp
->rcmd
.fn
= ex_subagain
;
739 ecp
->cmd
= &ecp
->rcmd
;
740 } else if (ecp
->clen
> 0) {
744 * Backslashes quote delimiter characters for RE's.
745 * The backslashes are NOT removed since they'll be
746 * used by the RE code. Move to the third delimiter
747 * that's not escaped (or the end of the command).
752 for (cnt
= 2; ecp
->clen
> 0 &&
753 cnt
!= 0; --ecp
->clen
, ++ecp
->cp
)
754 if (ecp
->cp
[0] == '\\' &&
758 } else if (ecp
->cp
[0] == delim
)
764 * Use normal quoting and termination rules to find the end of this
769 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
770 * file. It was almost certainly a bug, but that's what bug-for-bug
771 * compatibility means, Grasshopper. Also, ^V's escape the command
772 * delimiters. Literal next quote characters in front of the newlines,
773 * '|' characters or literal next characters are stripped as they're
776 vi_address
= ecp
->clen
!= 0 && ecp
->cp
[0] != '\n';
777 for (p
= ecp
->cp
; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
779 if (IS_ESCAPE(sp
, ecp
, ch
) && ecp
->clen
> 1) {
780 CHAR_T tmp
= ecp
->cp
[1];
781 if (tmp
== '\n' || tmp
== '|') {
791 } else if (ch
== '\n' || ch
== '|') {
793 F_SET(ecp
, E_NEWLINE
);
801 * Save off the next command information, go back to the
802 * original start of the command.
805 ecp
->cp
= ecp
->save_cmd
;
807 ecp
->save_cmdlen
= ecp
->clen
;
808 ecp
->clen
= ((ecp
->save_cmd
- ecp
->cp
) - 1) - discard
;
813 * The "set tags" command historically used a backslash, not the
814 * user's literal next character, to escape whitespace. Handle
815 * it here instead of complicating the argv_exp3() code. Note,
816 * this isn't a particularly complex trap, and if backslashes were
817 * legal in set commands, this would have to be much more complicated.
819 if (ecp
->cmd
== &cmds
[C_SET
])
820 for (p
= ecp
->cp
, len
= ecp
->clen
; len
> 0; --len
, ++p
)
821 if (IS_ESCAPE(sp
, ecp
, *p
) && len
> 1) {
824 } else if (*p
== '\\')
828 * Set the default addresses. It's an error to specify an address for
829 * a command that doesn't take them. If two addresses are specified
830 * for a command that only takes one, lose the first one. Two special
831 * cases here, some commands take 0 or 2 addresses. For most of them
832 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
833 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
835 * Also, if the file is empty, some commands want to use an address of
836 * 0, i.e. the entire file is 0 to 0, and the default first address is
837 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
838 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
839 * case where the 0 address is only valid if it's a default address.
841 * Also, set a flag if we set the default addresses. Some commands
842 * (ex: z) care if the user specified an address or if we just used
843 * the current cursor.
845 switch (F_ISSET(ecp
, E_ADDR1
| E_ADDR2
| E_ADDR2_ALL
| E_ADDR2_NONE
)) {
846 case E_ADDR1
: /* One address: */
847 switch (ecp
->addrcnt
) {
848 case 0: /* Default cursor/empty file. */
850 F_SET(ecp
, E_ADDR_DEF
);
851 if (F_ISSET(ecp
, E_ADDR_ZERODEF
)) {
852 if (db_last(sp
, &lno
))
856 F_SET(ecp
, E_ADDR_ZERO
);
858 ecp
->addr1
.lno
= sp
->lno
;
860 ecp
->addr1
.lno
= sp
->lno
;
861 ecp
->addr1
.cno
= sp
->cno
;
865 case 2: /* Lose the first address. */
867 ecp
->addr1
= ecp
->addr2
;
870 case E_ADDR2_NONE
: /* Zero/two addresses: */
871 if (ecp
->addrcnt
== 0) /* Default to nothing. */
874 case E_ADDR2_ALL
: /* Zero/two addresses: */
875 if (ecp
->addrcnt
== 0) { /* Default entire/empty file. */
876 F_SET(ecp
, E_ADDR_DEF
);
880 else if (db_last(sp
, &ecp
->addr2
.lno
))
882 if (F_ISSET(ecp
, E_ADDR_ZERODEF
) &&
883 ecp
->addr2
.lno
== 0) {
885 F_SET(ecp
, E_ADDR_ZERO
);
888 ecp
->addr1
.cno
= ecp
->addr2
.cno
= 0;
889 F_SET(ecp
, E_ADDR2_ALL
);
893 case E_ADDR2
: /* Two addresses: */
894 two_addr
: switch (ecp
->addrcnt
) {
895 case 0: /* Default cursor/empty file. */
897 F_SET(ecp
, E_ADDR_DEF
);
899 F_ISSET(ecp
, E_ADDR_ZERODEF
)) {
900 if (db_last(sp
, &lno
))
903 ecp
->addr1
.lno
= ecp
->addr2
.lno
= 0;
904 F_SET(ecp
, E_ADDR_ZERO
);
907 ecp
->addr2
.lno
= sp
->lno
;
909 ecp
->addr1
.lno
= ecp
->addr2
.lno
= sp
->lno
;
910 ecp
->addr1
.cno
= ecp
->addr2
.cno
= sp
->cno
;
912 case 1: /* Default to first address. */
914 ecp
->addr2
= ecp
->addr1
;
921 if (ecp
->addrcnt
) /* Error. */
927 * The ^D scroll command historically scrolled the value of the scroll
928 * option or to EOF. It was an error if the cursor was already at EOF.
929 * (Leading addresses were permitted, but were then ignored.)
931 if (ecp
->cmd
== &cmds
[C_SCROLL
]) {
933 ecp
->addr1
.lno
= sp
->lno
+ 1;
934 ecp
->addr2
.lno
= sp
->lno
+ O_VAL(sp
, O_SCROLL
);
935 ecp
->addr1
.cno
= ecp
->addr2
.cno
= sp
->cno
;
936 if (db_last(sp
, &lno
))
938 if (lno
!= 0 && lno
> sp
->lno
&& ecp
->addr2
.lno
> lno
)
939 ecp
->addr2
.lno
= lno
;
943 for (np
= ecp
->cmd
->syntax
; *np
!= '\0'; ++np
) {
945 * The force flag is sensitive to leading whitespace, i.e.
946 * "next !" is different from "next!". Handle it before
947 * skipping leading <blank>s.
950 if (ecp
->clen
> 0 && *ecp
->cp
== '!') {
953 FL_SET(ecp
->iflags
, E_C_FORCE
);
958 /* Skip leading <blank>s. */
959 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
960 if (!cmdskip(*ecp
->cp
))
966 case '1': /* +, -, #, l, p */
969 * Historically, some flags were ignored depending
970 * on where they occurred in the command line. For
971 * example, in the command, ":3+++p--#", historic vi
972 * acted on the '#' flag, but ignored the '-' flags.
973 * It's unambiguous what the flags mean, so we just
974 * handle them regardless of the stupidity of their
977 for (; ecp
->clen
; --ecp
->clen
, ++ecp
->cp
)
987 F_CLR(ecp
, E_OPTNUM
);
988 FL_SET(ecp
->iflags
, E_C_HASH
);
989 exp
->fdef
|= E_C_HASH
;
992 FL_SET(ecp
->iflags
, E_C_LIST
);
993 exp
->fdef
|= E_C_LIST
;
996 FL_SET(ecp
->iflags
, E_C_PRINT
);
997 exp
->fdef
|= E_C_PRINT
;
1003 case '2': /* -, ., +, ^ */
1004 case '3': /* -, ., +, ^, = */
1005 for (; ecp
->clen
; --ecp
->clen
, ++ecp
->cp
)
1008 FL_SET(ecp
->iflags
, E_C_DASH
);
1011 FL_SET(ecp
->iflags
, E_C_DOT
);
1014 FL_SET(ecp
->iflags
, E_C_PLUS
);
1017 FL_SET(ecp
->iflags
, E_C_CARAT
);
1021 FL_SET(ecp
->iflags
, E_C_EQUAL
);
1029 case 'b': /* buffer */
1032 * Historically, "d #" was a delete with a flag, not a
1033 * delete into the '#' buffer. If the current command
1034 * permits a flag, don't use one as a buffer. However,
1035 * the 'l' and 'p' flags were legal buffer names in the
1036 * historic ex, and were used as buffers, not flags.
1038 if ((ecp
->cp
[0] == '+' || ecp
->cp
[0] == '-' ||
1039 ecp
->cp
[0] == '^' || ecp
->cp
[0] == '#') &&
1040 strchr(np
, '1') != NULL
)
1044 * Digits can't be buffer names in ex commands, or the
1045 * command "d2" would be a delete into buffer '2', and
1046 * not a two-line deletion.
1048 if (!ISDIGIT(ecp
->cp
[0])) {
1049 ecp
->buffer
= *ecp
->cp
;
1052 FL_SET(ecp
->iflags
, E_C_BUFFER
);
1055 case 'c': /* count [01+a] */
1057 /* Validate any signed value. */
1058 if (!ISDIGIT(*ecp
->cp
) && (*np
!= '+' ||
1059 (*ecp
->cp
!= '+' && *ecp
->cp
!= '-')))
1061 /* If a signed value, set appropriate flags. */
1062 if (*ecp
->cp
== '-')
1063 FL_SET(ecp
->iflags
, E_C_COUNT_NEG
);
1064 else if (*ecp
->cp
== '+')
1065 FL_SET(ecp
->iflags
, E_C_COUNT_POS
);
1067 nget_slong(<mp
, ecp
->cp
, &t
, 10)) != NUM_OK
) {
1068 ex_badaddr(sp
, NULL
, A_NOTSET
, nret
);
1071 if (ltmp
== 0 && *np
!= '0') {
1072 msgq(sp
, M_ERR
, "083|Count may not be zero");
1075 ecp
->clen
-= (t
- ecp
->cp
);
1079 * Counts as address offsets occur in commands taking
1080 * two addresses. Historic vi practice was to use
1081 * the count as an offset from the *second* address.
1083 * Set a count flag; some underlying commands (see
1084 * join) do different things with counts than with
1088 ecp
->addr1
= ecp
->addr2
;
1089 ecp
->addr2
.lno
= ecp
->addr1
.lno
+ ltmp
- 1;
1092 FL_SET(ecp
->iflags
, E_C_COUNT
);
1094 case 'f': /* file */
1095 if (argv_exp2(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1098 case 'l': /* line */
1100 * Get a line specification.
1102 * If the line was a search expression, we may have
1103 * changed state during the call, and we're now
1104 * searching the file. Push ourselves onto the state
1107 if (ex_line(sp
, ecp
, &cur
, &isaddr
, &tmp
))
1112 /* Line specifications are always required. */
1114 msgq_wstr(sp
, M_ERR
, ecp
->cp
,
1115 "084|%s: bad line specification");
1119 * The target line should exist for these commands,
1120 * but 0 is legal for them as well.
1122 if (cur
.lno
!= 0 && !db_exist(sp
, cur
.lno
)) {
1123 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1126 ecp
->lineno
= cur
.lno
;
1128 case 'S': /* string, file exp. */
1129 if (ecp
->clen
!= 0) {
1130 if (argv_exp1(sp
, ecp
, ecp
->cp
,
1131 ecp
->clen
, ecp
->cmd
== &cmds
[C_BANG
]))
1136 case 's': /* string */
1137 if (argv_exp0(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1140 case 'W': /* word string */
1144 * Literal next characters escape the following
1145 * character. Quoting characters are stripped here
1146 * since they are no longer useful.
1148 * First there was the word.
1150 for (p
= t
= ecp
->cp
;
1151 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
1154 ecp
, ch
) && ecp
->clen
> 1) {
1157 } else if (cmdskip(ch
)) {
1164 if (argv_exp0(sp
, ecp
, t
, p
- t
))
1167 /* Delete intervening whitespace. */
1168 for (; ecp
->clen
> 0;
1169 --ecp
->clen
, ++ecp
->cp
) {
1177 /* Followed by the string. */
1178 for (p
= t
= ecp
->cp
; ecp
->clen
> 0;
1179 --ecp
->clen
, ++ecp
->cp
, ++p
) {
1182 ecp
, ch
) && ecp
->clen
> 1) {
1188 if (argv_exp0(sp
, ecp
, t
, p
- t
))
1191 case 'w': /* word */
1192 if (argv_exp3(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1194 arg_cnt_chk
: if (*++np
!= 'N') { /* N */
1196 * If a number is specified, must either be
1197 * 0 or that number, if optional, and that
1198 * number, if required.
1201 if ((*++np
!= 'o' || exp
->argsoff
!= 0) &&
1202 exp
->argsoff
!= tmp
)
1210 INT2CHAR(sp
, ecp
->cmd
->name
, STRLEN(ecp
->cmd
->name
) + 1,
1213 "085|Internal syntax table error (%s: %s)",
1214 nstr
, KEY_NAME(sp
, *np
));
1219 /* Skip trailing whitespace. */
1220 for (; ecp
->clen
> 0; --ecp
->clen
) {
1227 * There shouldn't be anything left, and no more required fields,
1228 * i.e neither 'l' or 'r' in the syntax string.
1230 if (ecp
->clen
!= 0 || strpbrk(np
, "lr")) {
1231 usage
: msgq(sp
, M_ERR
, "086|Usage: %s", ecp
->cmd
->usage
);
1236 * Verify that the addresses are legal. Check the addresses here,
1237 * because this is a place where all ex addresses pass through.
1238 * (They don't all pass through ex_line(), for instance.) We're
1239 * assuming that any non-existent line doesn't exist because it's
1240 * past the end-of-file. That's a pretty good guess.
1242 * If it's a "default vi command", an address of zero is okay.
1245 switch (ecp
->addrcnt
) {
1248 * Historic ex/vi permitted commands with counts to go past
1249 * EOF. So, for example, if the file only had 5 lines, the
1250 * ex command "1,6>" would fail, but the command ">300"
1251 * would succeed. Since we don't want to have to make all
1252 * of the underlying commands handle random line numbers,
1255 if (ecp
->addr2
.lno
== 0) {
1256 if (!F_ISSET(ecp
, E_ADDR_ZERO
) &&
1257 (F_ISSET(sp
, SC_EX
) ||
1258 !F_ISSET(ecp
, E_USELASTCMD
))) {
1259 ex_badaddr(sp
, ecp
->cmd
, A_ZERO
, NUM_OK
);
1262 } else if (!db_exist(sp
, ecp
->addr2
.lno
))
1263 if (FL_ISSET(ecp
->iflags
, E_C_COUNT
)) {
1264 if (db_last(sp
, &lno
))
1266 ecp
->addr2
.lno
= lno
;
1268 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1273 if (ecp
->addr1
.lno
== 0) {
1274 if (!F_ISSET(ecp
, E_ADDR_ZERO
) &&
1275 (F_ISSET(sp
, SC_EX
) ||
1276 !F_ISSET(ecp
, E_USELASTCMD
))) {
1277 ex_badaddr(sp
, ecp
->cmd
, A_ZERO
, NUM_OK
);
1280 } else if (!db_exist(sp
, ecp
->addr1
.lno
)) {
1281 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1288 * If doing a default command and there's nothing left on the line,
1289 * vi just moves to the line. For example, ":3" and ":'a,'b" just
1290 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1293 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1297 * This is done before the absolute mark gets set; historically,
1298 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1300 if ((F_ISSET(sp
, SC_VI
) || F_ISSET(ecp
, E_NOPRDEF
)) &&
1301 F_ISSET(ecp
, E_USELASTCMD
) && vi_address
== 0) {
1302 switch (ecp
->addrcnt
) {
1305 (ecp
->addr2
.lno
? ecp
->addr2
.lno
: 1)) {
1307 ecp
->addr2
.lno
? ecp
->addr2
.lno
: 1;
1309 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
1314 (ecp
->addr1
.lno
? ecp
->addr1
.lno
: 1)) {
1316 ecp
->addr1
.lno
? ecp
->addr1
.lno
: 1;
1318 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
1322 ecp
->cp
= ecp
->save_cmd
;
1323 ecp
->clen
= ecp
->save_cmdlen
;
1328 * Set the absolute mark -- we have to set it for vi here, in case
1329 * it's a compound command, e.g. ":5p|6" should set the absolute
1332 if (F_ISSET(ecp
, E_ABSMARK
)) {
1335 F_CLR(ecp
, E_ABSMARK
);
1336 if (mark_set(sp
, ABSMARK1
, &cur
, 1))
1340 #if defined(DEBUG) && defined(COMLOG)
1343 /* Increment the command count if not called from vi. */
1344 if (F_ISSET(sp
, SC_EX
))
1348 * If file state available, and not doing a global command,
1349 * log the start of an action.
1351 if (sp
->ep
!= NULL
&& !F_ISSET(sp
, SC_EX_GLOBAL
))
1352 (void)log_cursor(sp
);
1356 * There are two special commands for the purposes of this code: the
1357 * default command (<carriage-return>) or the scrolling commands (^D
1358 * and <EOF>) as the first non-<blank> characters in the line.
1360 * If this is the first command in the command line, we received the
1361 * command from the ex command loop and we're talking to a tty, and
1362 * and there's nothing else on the command line, and it's one of the
1363 * special commands, we move back up to the previous line, and erase
1364 * the prompt character with the output. Since ex runs in canonical
1365 * mode, we don't have to do anything else, a <newline> has already
1366 * been echoed by the tty driver. It's OK if vi calls us -- we won't
1367 * be in ex mode so we'll do nothing.
1369 if (F_ISSET(ecp
, E_NRSEP
)) {
1370 if (sp
->ep
!= NULL
&&
1371 F_ISSET(sp
, SC_EX
) && !F_ISSET(gp
, G_SCRIPTED
) &&
1372 (F_ISSET(ecp
, E_USELASTCMD
) || ecp
->cmd
== &cmds
[C_SCROLL
]))
1373 gp
->scr_ex_adjust(sp
, EX_TERM_SCROLL
);
1374 F_CLR(ecp
, E_NRSEP
);
1378 * Call the underlying function for the ex command.
1381 * Interrupts behave like errors, for now.
1383 if (ecp
->cmd
->fn(sp
, ecp
) || INTERRUPTED(sp
)) {
1384 if (F_ISSET(gp
, G_SCRIPTED
))
1385 F_SET(sp
, SC_EXIT_FORCE
);
1390 /* Make sure no function left global temporary space locked. */
1391 if (F_ISSET(gp
, G_TMP_INUSE
)) {
1392 F_CLR(gp
, G_TMP_INUSE
);
1393 msgq_wstr(sp
, M_ERR
, ecp
->cmd
->name
,
1394 "087|%s: temporary buffer not released");
1398 * Ex displayed the number of lines modified immediately after each
1399 * command, so the command "1,10d|1,10d" would display:
1405 * Executing ex commands from vi only reported the final modified
1406 * lines message -- that's wrong enough that we don't match it.
1408 if (F_ISSET(sp
, SC_EX
))
1412 * Integrate any offset parsed by the underlying command, and make
1413 * sure the referenced line exists.
1416 * May not match historic practice (which I've never been able to
1417 * completely figure out.) For example, the '=' command from vi
1418 * mode often got the offset wrong, and complained it was too large,
1419 * but didn't seem to have a problem with the cursor. If anyone
1420 * complains, ask them how it's supposed to work, they might know.
1422 if (sp
->ep
!= NULL
&& ecp
->flagoff
) {
1423 if (ecp
->flagoff
< 0) {
1424 if (sp
->lno
<= -ecp
->flagoff
) {
1426 "088|Flag offset to before line 1");
1430 if (!NPFITS(MAX_REC_NUMBER
, sp
->lno
, ecp
->flagoff
)) {
1431 ex_badaddr(sp
, NULL
, A_NOTSET
, NUM_OVER
);
1434 if (!db_exist(sp
, sp
->lno
+ ecp
->flagoff
)) {
1436 "089|Flag offset past end-of-file");
1440 sp
->lno
+= ecp
->flagoff
;
1444 * If the command executed successfully, we may want to display a line
1445 * based on the autoprint option or an explicit print flag. (Make sure
1446 * that there's a line to display.) Also, the autoprint edit option is
1447 * turned off for the duration of global commands.
1449 if (F_ISSET(sp
, SC_EX
) && sp
->ep
!= NULL
&& sp
->lno
!= 0) {
1451 * The print commands have already handled the `print' flags.
1452 * If so, clear them.
1454 if (FL_ISSET(ecp
->iflags
, E_CLRFLAG
))
1455 FL_CLR(ecp
->iflags
, E_C_HASH
| E_C_LIST
| E_C_PRINT
);
1457 /* If hash set only because of the number option, discard it. */
1458 if (F_ISSET(ecp
, E_OPTNUM
))
1459 FL_CLR(ecp
->iflags
, E_C_HASH
);
1462 * If there was an explicit flag to display the new cursor line,
1463 * or autoprint is set and a change was made, display the line.
1464 * If any print flags were set use them, else default to print.
1466 LF_INIT(FL_ISSET(ecp
->iflags
, E_C_HASH
| E_C_LIST
| E_C_PRINT
));
1467 if (!LF_ISSET(E_C_HASH
| E_C_LIST
| E_C_PRINT
| E_NOAUTO
) &&
1468 !F_ISSET(sp
, SC_EX_GLOBAL
) &&
1469 O_ISSET(sp
, O_AUTOPRINT
) && F_ISSET(ecp
, E_AUTOPRINT
))
1472 if (LF_ISSET(E_C_HASH
| E_C_LIST
| E_C_PRINT
)) {
1475 (void)ex_print(sp
, ecp
, &cur
, &cur
, flags
);
1480 * If the command had an associated "+cmd", it has to be executed
1481 * before we finish executing any more of this ex command. For
1482 * example, consider a .exrc file that contains the following lines:
1485 * :edit +25 file.c|s/abc/ABC/|1
1488 * This can happen more than once -- the historic vi simply hung or
1489 * dropped core, of course. Prepend the + command back into the
1490 * current command and continue. We may have to add an additional
1491 * <literal next> character. We know that it will fit because we
1492 * discarded at least one space and the + character.
1494 if (arg1_len
!= 0) {
1496 * If the last character of the + command was a <literal next>
1497 * character, it would be treated differently because of the
1498 * append. Quote it, if necessary.
1500 if (IS_ESCAPE(sp
, ecp
, arg1
[arg1_len
- 1])) {
1501 *--ecp
->save_cmd
= CH_LITERAL
;
1505 ecp
->save_cmd
-= arg1_len
;
1506 ecp
->save_cmdlen
+= arg1_len
;
1507 MEMCPY(ecp
->save_cmd
, arg1
, arg1_len
);
1510 * Any commands executed from a +cmd are executed starting at
1511 * the first column of the last line of the file -- NOT the
1512 * first nonblank.) The main file startup code doesn't know
1513 * that a +cmd was set, however, so it may have put us at the
1514 * top of the file. (Note, this is safe because we must have
1515 * switched files to get here.)
1517 F_SET(ecp
, E_MOVETOEND
);
1520 /* Update the current command. */
1521 ecp
->cp
= ecp
->save_cmd
;
1522 ecp
->clen
= ecp
->save_cmdlen
;
1526 * If we've changed screens or underlying files, any pending global or
1527 * v command, or @ buffer that has associated addresses, has to be
1528 * discarded. This is historic practice for globals, and necessary for
1529 * @ buffers that had associated addresses.
1531 * Otherwise, if we've changed underlying files, it's not a problem,
1532 * we continue with the rest of the ex command(s), operating on the
1533 * new file. However, if we switch screens (either by exiting or by
1534 * an explicit command), we have no way of knowing where to put output
1535 * messages, and, since we don't control screens here, we could screw
1536 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1537 * times). So, return and continue after we've got a new screen.
1539 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
| SC_FSWITCH
| SC_SSWITCH
)) {
1540 at_found
= gv_found
= 0;
1541 SLIST_FOREACH(ecp
, sp
->gp
->ecq
, q
)
1542 switch (ecp
->agv_flags
) {
1544 case AGV_AT_NORANGE
:
1550 "090|@ with range running when the file/screen changed");
1558 "091|Global/v command running when the file/screen changed");
1564 if (at_found
|| gv_found
)
1566 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
| SC_SSWITCH
))
1574 * On command failure, we discard keys and pending commands remaining,
1575 * as well as any keys that were mapped and waiting. The save_cmdlen
1576 * test is not necessarily correct. If we fail early enough we don't
1577 * know if the entire string was a single command or not. Guess, as
1578 * it's useful to know if commands other than the current one are being
1581 if (ecp
->save_cmdlen
== 0)
1582 for (; ecp
->clen
; --ecp
->clen
) {
1584 if (IS_ESCAPE(sp
, ecp
, ch
) && ecp
->clen
> 1) {
1587 } else if (ch
== '\n' || ch
== '|') {
1589 ecp
->save_cmdlen
= 1;
1593 if (ecp
->save_cmdlen
!= 0 || SLIST_FIRST(gp
->ecq
) != &gp
->excmd
) {
1594 discard
: msgq(sp
, M_BERR
,
1595 "092|Ex command failed: pending commands discarded");
1598 if (v_event_flush(sp
, CH_MAPPED
))
1600 "093|Ex command failed: mapped keys discarded");
1606 /* Turn off any file name error information. */
1609 /* Turn off the global bit. */
1610 F_CLR(sp
, SC_EX_GLOBAL
);
1617 * Get a line range for ex commands, or perform a vi ex address search.
1619 * PUBLIC: int ex_range(SCR *, EXCMD *, int *);
1622 ex_range(SCR
*sp
, EXCMD
*ecp
, int *errp
)
1624 enum { ADDR_FOUND
, ADDR_NEED
, ADDR_NONE
} addr
;
1633 * Parse comma or semi-colon delimited line specs.
1635 * Semi-colon delimiters update the current address to be the last
1636 * address. For example, the command
1638 * :3;/pattern/ecp->cp
1640 * will search for pattern from line 3. In addition, if ecp->cp
1641 * is not a valid command, the current line will be left at 3, not
1642 * at the original address.
1644 * Extra addresses are discarded, starting with the first.
1647 * If any addresses are missing, they default to the current line.
1648 * This was historically true for both leading and trailing comma
1649 * delimited addresses as well as for trailing semicolon delimited
1650 * addresses. For consistency, we make it true for leading semicolon
1651 * addresses as well.
1655 for (addr
= ADDR_NONE
, ecp
->addrcnt
= 0; ecp
->clen
> 0;)
1657 case '%': /* Entire file. */
1658 /* Vi ex address searches didn't permit % signs. */
1659 if (F_ISSET(ecp
, E_VISEARCH
))
1662 /* It's an error if the file is empty. */
1663 if (sp
->ep
== NULL
) {
1664 ex_badaddr(sp
, NULL
, A_EMPTY
, NUM_OK
);
1670 * A percent character addresses all of the lines in
1671 * the file. Historically, it couldn't be followed by
1672 * any other address. We do it as a text substitution
1673 * for simplicity. POSIX 1003.2 is expected to follow
1676 * If it's an empty file, the first line is 0, not 1.
1678 if (addr
== ADDR_FOUND
) {
1679 ex_badaddr(sp
, NULL
, A_COMBO
, NUM_OK
);
1683 if (db_last(sp
, &ecp
->addr2
.lno
))
1685 ecp
->addr1
.lno
= ecp
->addr2
.lno
== 0 ? 0 : 1;
1686 ecp
->addr1
.cno
= ecp
->addr2
.cno
= 0;
1692 case ',': /* Comma delimiter. */
1693 /* Vi ex address searches didn't permit commas. */
1694 if (F_ISSET(ecp
, E_VISEARCH
))
1697 case ';': /* Semi-colon delimiter. */
1698 if (sp
->ep
== NULL
) {
1699 ex_badaddr(sp
, NULL
, A_EMPTY
, NUM_OK
);
1703 if (addr
!= ADDR_FOUND
)
1704 switch (ecp
->addrcnt
) {
1706 ecp
->addr1
.lno
= sp
->lno
;
1707 ecp
->addr1
.cno
= sp
->cno
;
1711 ecp
->addr1
= ecp
->addr2
;
1714 ecp
->addr2
.lno
= sp
->lno
;
1715 ecp
->addr2
.cno
= sp
->cno
;
1719 if (*ecp
->cp
== ';')
1720 switch (ecp
->addrcnt
) {
1725 sp
->lno
= ecp
->addr1
.lno
;
1726 sp
->cno
= ecp
->addr1
.cno
;
1729 sp
->lno
= ecp
->addr2
.lno
;
1730 sp
->cno
= ecp
->addr2
.cno
;
1735 case ' ': /* Whitespace. */
1736 case '\t': /* Whitespace. */
1741 /* Get a line specification. */
1742 if (ex_line(sp
, ecp
, &m
, &isaddr
, errp
))
1748 if (addr
== ADDR_FOUND
) {
1749 ex_badaddr(sp
, NULL
, A_COMBO
, NUM_OK
);
1753 switch (ecp
->addrcnt
) {
1763 ecp
->addr1
= ecp
->addr2
;
1773 * Vi ex address searches are indifferent to order or trailing
1776 ret
: if (F_ISSET(ecp
, E_VISEARCH
))
1779 if (addr
== ADDR_NEED
)
1780 switch (ecp
->addrcnt
) {
1782 ecp
->addr1
.lno
= sp
->lno
;
1783 ecp
->addr1
.cno
= sp
->cno
;
1787 ecp
->addr1
= ecp
->addr2
;
1790 ecp
->addr2
.lno
= sp
->lno
;
1791 ecp
->addr2
.cno
= sp
->cno
;
1796 if (ecp
->addrcnt
== 2 && ecp
->addr2
.lno
< ecp
->addr1
.lno
) {
1798 "094|The second address is smaller than the first");
1806 * Get a single line address specifier.
1808 * The way the "previous context" mark worked was that any "non-relative"
1809 * motion set it. While ex/vi wasn't totally consistent about this, ANY
1810 * numeric address, search pattern, '$', or mark reference in an address
1811 * was considered non-relative, and set the value. Which should explain
1812 * why we're hacking marks down here. The problem was that the mark was
1813 * only set if the command was called, i.e. we have to set a flag and test
1817 * This is probably still not exactly historic practice, although I think
1818 * it's fairly close.
1821 ex_line(SCR
*sp
, EXCMD
*ecp
, MARK
*mp
, int *isaddrp
, int *errp
)
1828 int (*sf
)(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_slong(&val
, ecp
->cp
, &endp
, 10)) != NUM_OK
) {
1861 ex_badaddr(sp
, NULL
, A_NOTSET
, nret
);
1865 if (!NPFITS(MAX_REC_NUMBER
, 0, val
)) {
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(ecp
->cp
[1]))
1958 /* Skip trailing <blank>s. */
1959 for (; ecp
->clen
> 0 &&
1960 cmdskip(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(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 && cmdskip(ecp
->cp
[0]);
2004 ++ecp
->cp
, --ecp
->clen
);
2005 if (ecp
->clen
== 0 || (!ISDIGIT(ecp
->cp
[0]) &&
2006 ecp
->cp
[0] != '+' && ecp
->cp
[0] != '-' &&
2009 if (!ISDIGIT(ecp
->cp
[0]) &&
2010 !ISDIGIT(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(&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 recno_t.
2043 if (*isaddrp
&& total
!= 0) {
2045 if (-total
> mp
->lno
) {
2047 "097|Reference to a line number less than 0");
2052 if (!NPFITS(MAX_REC_NUMBER
, mp
->lno
, 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 (gp
= sp
->gp
;;) {
2081 ecp
= SLIST_FIRST(gp
->ecq
);
2083 /* Discard the allocated source name as requested. */
2084 if (F_ISSET(ecp
, E_NAMEDISCARD
))
2088 * If we're back to the original structure, leave it around,
2089 * since we've returned to the beginning of the command stack.
2091 if (ecp
== &gp
->excmd
) {
2092 ecp
->if_name
= NULL
;
2097 * ecp->clen will be 0 for the first discarded command, but
2098 * may not be 0 for subsequent ones, e.g. if the original
2099 * command was ":g/xx/@a|s/b/c/", then when we discard the
2100 * command pushed on the stack by the @a, we have to resume
2101 * the global command which included the substitute command.
2107 * If it's an @, global or v command, we may need to continue
2108 * the command on a different line.
2110 if (FL_ISSET(ecp
->agv_flags
, AGV_ALL
)) {
2111 /* Discard any exhausted ranges. */
2112 while ((rp
= TAILQ_FIRST(ecp
->rq
)) != NULL
)
2113 if (rp
->start
> rp
->stop
) {
2114 TAILQ_REMOVE(ecp
->rq
, rp
, q
);
2119 /* If there's another range, continue with it. */
2123 /* If it's a global/v command, fix up the last line. */
2124 if (FL_ISSET(ecp
->agv_flags
,
2125 AGV_GLOBAL
| AGV_V
) && ecp
->range_lno
!= OOBLNO
)
2126 if (db_exist(sp
, ecp
->range_lno
))
2127 sp
->lno
= ecp
->range_lno
;
2129 if (db_last(sp
, &sp
->lno
))
2137 /* Discard the EXCMD. */
2138 SLIST_REMOVE_HEAD(gp
->ecq
, q
);
2143 * We only get here if it's an active @, global or v command. Set
2144 * the current line number, and get a new copy of the command for
2145 * the parser. Note, the original pointer almost certainly moved,
2146 * so we have play games.
2148 ecp
->cp
= ecp
->o_cp
;
2149 MEMCPY(ecp
->cp
, ecp
->cp
+ ecp
->o_clen
, ecp
->o_clen
);
2150 ecp
->clen
= ecp
->o_clen
;
2151 ecp
->range_lno
= sp
->lno
= rp
->start
++;
2153 if (FL_ISSET(ecp
->agv_flags
, AGV_GLOBAL
| AGV_V
))
2154 F_SET(sp
, SC_EX_GLOBAL
);
2160 * Discard any pending ex commands.
2170 * We know the first command can't be an AGV command, so we don't
2171 * process it specially. We do, however, nail the command itself.
2173 for (gp
= sp
->gp
;;) {
2174 ecp
= SLIST_FIRST(gp
->ecq
);
2175 if (F_ISSET(ecp
, E_NAMEDISCARD
))
2177 /* Reset the last command without dropping it. */
2178 if (ecp
== &gp
->excmd
)
2180 if (FL_ISSET(ecp
->agv_flags
, AGV_ALL
)) {
2181 while ((rp
= TAILQ_FIRST(ecp
->rq
)) != NULL
) {
2182 TAILQ_REMOVE(ecp
->rq
, rp
, q
);
2187 SLIST_REMOVE_HEAD(gp
->ecq
, q
);
2191 ecp
->if_name
= NULL
;
2198 * Display an unknown command name.
2201 ex_unknown(SCR
*sp
, CHAR_T
*cmd
, size_t len
)
2206 GET_SPACE_GOTOW(sp
, bp
, blen
, len
+ 1);
2208 MEMCPY(bp
, cmd
, len
);
2209 msgq_wstr(sp
, M_ERR
, bp
, "098|The %s command is unknown");
2210 FREE_SPACEW(sp
, bp
, blen
);
2218 * The vi text input routine needs to know if ex thinks this is an
2219 * [un]abbreviate command, so it can turn off abbreviations. See
2220 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2222 * PUBLIC: int ex_is_abbrev(CHAR_T *, size_t);
2225 ex_is_abbrev(CHAR_T
*name
, size_t len
)
2227 EXCMDLIST
const *cp
;
2229 return ((cp
= ex_comm_search(name
, len
)) != NULL
&&
2230 (cp
== &cmds
[C_ABBR
] || cp
== &cmds
[C_UNABBREVIATE
]));
2235 * The vi text input routine needs to know if ex thinks this is an
2236 * unmap command, so it can turn off input mapping. See the usual
2237 * ranting in the vi/v_txt_ev.c:txt_unmap() routine.
2239 * PUBLIC: int ex_is_unmap(CHAR_T *, size_t);
2242 ex_is_unmap(CHAR_T
*name
, size_t len
)
2244 EXCMDLIST
const *cp
;
2247 * The command the vi input routines are really interested in
2248 * is "unmap!", not just unmap.
2250 if (name
[len
- 1] != '!')
2253 return ((cp
= ex_comm_search(name
, len
)) != NULL
&&
2254 cp
== &cmds
[C_UNMAP
]);
2259 * Search for a command name.
2261 static EXCMDLIST
const *
2262 ex_comm_search(CHAR_T
*name
, size_t len
)
2264 EXCMDLIST
const *cp
;
2266 for (cp
= cmds
; cp
->name
!= NULL
; ++cp
) {
2267 if (cp
->name
[0] > name
[0])
2269 if (cp
->name
[0] != name
[0])
2271 if (!MEMCMP(name
, cp
->name
, len
))
2279 * Display a bad address message.
2281 * PUBLIC: void ex_badaddr
2282 * PUBLIC: (SCR *, EXCMDLIST const *, enum badaddr, enum nresult);
2285 ex_badaddr(SCR
*sp
, const EXCMDLIST
*cp
, enum badaddr ba
, enum nresult nret
)
2293 msgq(sp
, M_SYSERR
, NULL
);
2296 msgq(sp
, M_ERR
, "099|Address value overflow");
2299 msgq(sp
, M_ERR
, "100|Address value underflow");
2304 * When encountering an address error, tell the user if there's no
2305 * underlying file, that's the real problem.
2307 if (sp
->ep
== NULL
) {
2308 ex_wemsg(sp
, cp
? cp
->name
: NULL
, EXM_NOFILEYET
);
2314 msgq(sp
, M_ERR
, "101|Illegal address combination");
2317 if (db_last(sp
, &lno
))
2321 "102|Illegal address: only %lu lines in the file",
2327 msgq(sp
, M_ERR
, "103|Illegal address: the file is empty");
2333 msgq_wstr(sp
, M_ERR
, cp
->name
,
2334 "104|The %s command doesn't permit an address of 0");
2340 #if defined(DEBUG) && defined(COMLOG)
2350 TRACE(sp
, "ecmd: "WS
, ecp
->cmd
->name
);
2351 if (ecp
->addrcnt
> 0) {
2352 TRACE(sp
, " a1 %d", ecp
->addr1
.lno
);
2353 if (ecp
->addrcnt
> 1)
2354 TRACE(sp
, " a2: %d", ecp
->addr2
.lno
);
2357 TRACE(sp
, " line %d", ecp
->lineno
);
2359 TRACE(sp
, " flags 0x%x", ecp
->flags
);
2360 if (FL_ISSET(ecp
->iflags
, E_C_BUFFER
))
2361 TRACE(sp
, " buffer "WC
, ecp
->buffer
);
2364 for (cnt
= 0; cnt
< ecp
->argc
; ++cnt
)
2365 TRACE(sp
, " arg %d: {"WS
"}", cnt
, ecp
->argv
[cnt
]->bp
);