1 /* $NetBSD: ex.c,v 1.5 2009/11/14 23:31:37 christos Exp $ */
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 static const char sccsid
[] = "Id: ex.c,v 10.75 2004/03/16 14:13:35 skimo Exp (Berkeley) Date: 2004/03/16 14:13:35";
18 #include <sys/types.h>
19 #include <sys/queue.h>
23 #include <bitstring.h>
33 #include "../common/common.h"
36 #if defined(DEBUG) && defined(COMLOG)
37 static void ex_comlog
__P((SCR
*, EXCMD
*));
39 static EXCMDLIST
const *
40 ex_comm_search
__P((SCR
*, CHAR_T
*, size_t));
41 static int ex_discard
__P((SCR
*));
42 static int ex_line
__P((SCR
*, EXCMD
*, MARK
*, int *, int *));
43 static int ex_load
__P((SCR
*));
44 static void ex_unknown
__P((SCR
*, CHAR_T
*, size_t));
50 * PUBLIC: int ex __P((SCR **));
68 /* Start the ex screen. */
72 /* Flush any saved messages. */
73 while ((mp
= gp
->msgq
.lh_first
) != NULL
) {
74 wp
->scr_msg(sp
, mp
->mtype
, mp
->buf
, mp
->len
);
80 /* If reading from a file, errors should have name and line info. */
81 if (F_ISSET(gp
, G_SCRIPTED
)) {
83 wp
->excmd
.if_name
= strdup("script");
88 * Initialize the text flags. The beautify edit option historically
89 * applied to ex command input read from a file. In addition, the
90 * first time a ^H was discarded from the input, there was a message,
91 * "^H discarded", that was displayed. We don't bother.
93 LF_INIT(TXT_BACKSLASH
| TXT_CNTRLD
| TXT_CR
);
94 for (;; ++wp
->excmd
.if_lno
) {
95 /* Display status line and flush. */
96 if (F_ISSET(sp
, SC_STATUS
)) {
97 if (!F_ISSET(sp
, SC_EX_SILENT
))
98 msgq_status(sp
, sp
->lno
, 0);
103 /* Set the flags the user can reset. */
104 if (O_ISSET(sp
, O_BEAUTIFY
))
105 LF_SET(TXT_BEAUTIFY
);
106 if (O_ISSET(sp
, O_PROMPT
))
109 /* Clear any current interrupts, and get a command. */
111 if (ex_txt(sp
, &sp
->tiq
, ':', flags
))
113 if (INTERRUPTED(sp
)) {
114 (void)ex_puts(sp
, "\n");
119 /* Initialize the command structure. */
120 CLEAR_EX_PARSER(&wp
->excmd
);
123 * If the user entered a single carriage return, send
124 * ex_cmd() a separator -- it discards single newlines.
126 tp
= sp
->tiq
.cqh_first
;
128 static CHAR_T space
= ' ';
129 wp
->excmd
.cp
= &space
; /* __TK__ why not |? */
132 wp
->excmd
.cp
= tp
->lb
;
133 wp
->excmd
.clen
= tp
->len
;
135 F_INIT(&wp
->excmd
, E_NRSEP
);
137 if (ex_cmd(sp
) && F_ISSET(gp
, G_SCRIPTED
))
140 if (INTERRUPTED(sp
)) {
142 msgq(sp
, M_ERR
, "170|Interrupted");
146 * If the last command caused a restart, or switched screens
147 * or into vi, return.
149 if (F_ISSET(gp
, G_SRESTART
) || F_ISSET(sp
, SC_SSWITCH
| SC_VI
)) {
154 /* If the last command switched files, we don't care. */
155 F_CLR(sp
, SC_FSWITCH
);
158 * If we're exiting this screen, move to the next one. By
159 * definition, this means returning into vi, so return to the
160 * main editor loop. The ordering is careful, don't discard
161 * the contents of sp until the end.
163 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
)) {
164 if (file_end(sp
, NULL
, F_ISSET(sp
, SC_EXIT_FORCE
)))
166 *spp
= screen_next(sp
);
167 return (screen_end(sp
));
175 * The guts of the ex parser: parse and execute a string containing
179 * This code MODIFIES the string that gets passed in, to delete quoting
180 * characters, etc. The string cannot be readonly/text space, nor should
181 * you expect to use it again after ex_cmd() returns.
184 * For the fun of it, if you want to see if a vi clone got the ex argument
185 * parsing right, try:
187 * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
189 * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
192 * :set|file|append|set|file
194 * For extra credit, try them in a startup .exrc file.
196 * PUBLIC: int ex_cmd __P((SCR *));
208 size_t arg1_len
, discard
, len
;
211 int at_found
, gv_found
;
212 int cnt
, delim
, isaddr
, namelen
;
213 int newscreen
, notempty
, tmp
, vi_address
;
214 CHAR_T
*arg1
, *s
, *p
, *t
;
222 ch
= '\0'; /* XXX: gcc -O1 -Wuninitialized */
225 * We always start running the command on the top of the stack.
226 * This means that *everything* must be resolved when we leave
227 * this function for any reason.
229 loop
: ecp
= wp
->ecq
.lh_first
;
231 /* If we're reading a command from a file, set up error information. */
232 if (ecp
->if_name
!= NULL
) {
233 wp
->if_lno
= ecp
->if_lno
;
234 wp
->if_name
= ecp
->if_name
;
238 * If a move to the end of the file is scheduled for this command,
241 if (F_ISSET(ecp
, E_MOVETOEND
)) {
242 if (db_last(sp
, &sp
->lno
))
245 F_CLR(ecp
, E_MOVETOEND
);
248 /* If we found a newline, increment the count now. */
249 if (F_ISSET(ecp
, E_NEWLINE
)) {
252 F_CLR(ecp
, E_NEWLINE
);
255 /* (Re)initialize the EXCMD structure, preserving some flags. */
258 /* Initialize the argument structures. */
259 if (argv_init(sp
, ecp
))
262 /* Initialize +cmd, saved command information. */
264 ecp
->save_cmdlen
= 0;
266 /* Skip <blank>s, empty lines. */
267 for (notempty
= 0; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
)
268 if ((ch
= *ecp
->cp
) == '\n') {
271 } else if (ISBLANK(ch
))
278 * Permit extra colons at the start of the line. Historically,
279 * ex/vi allowed a single extra one. It's simpler not to count.
280 * The stripping is done here because, historically, any command
281 * could have preceding colons, e.g. ":g/pattern/:p" worked.
283 if (ecp
->clen
!= 0 && ch
== ':') {
285 while (--ecp
->clen
> 0 && (ch
= *++ecp
->cp
) == ':');
289 * Command lines that start with a double-quote are comments.
292 * Historically, there was no escape or delimiter for a comment, e.g.
293 * :"foo|set was a single comment and nothing was output. Since nvi
294 * permits users to escape <newline> characters into command lines, we
295 * have to check for that case.
297 if (ecp
->clen
!= 0 && ch
== '"') {
298 while (--ecp
->clen
> 0 && *++ecp
->cp
!= '\n');
299 if (*ecp
->cp
== '\n') {
300 F_SET(ecp
, E_NEWLINE
);
307 /* Skip whitespace. */
308 for (; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
) {
315 * The last point at which an empty line can mean do nothing.
318 * Historically, in ex mode, lines containing only <blank> characters
319 * were the same as a single <carriage-return>, i.e. a default command.
320 * In vi mode, they were ignored. In .exrc files this was a serious
321 * annoyance, as vi kept trying to treat them as print commands. We
322 * ignore backward compatibility in this case, discarding lines that
323 * contain only <blank> characters from .exrc files.
326 * This is where you end up when you're done a command, i.e. clen has
327 * gone to zero. Continue if there are more commands to run.
329 if (ecp
->clen
== 0 &&
330 (!notempty
|| F_ISSET(sp
, SC_VI
) || F_ISSET(ecp
, E_BLIGNORE
))) {
333 ecp
= wp
->ecq
.lh_first
;
340 * Check to see if this is a command for which we may want to move
341 * the cursor back up to the previous line. (The command :1<CR>
342 * wants a <newline> separator, but the command :<CR> wants to erase
343 * the command line.) If the line is empty except for <blank>s,
344 * <carriage-return> or <eof>, we'll probably want to move up. I
345 * don't think there's any way to get <blank> characters *after* the
346 * command character, but this is the ex parser, and I've been wrong
349 if (F_ISSET(ecp
, E_NRSEP
) &&
350 ecp
->clen
!= 0 && (ecp
->clen
!= 1 || ecp
->cp
[0] != '\004'))
353 /* Parse command addresses. */
354 if (ex_range(sp
, ecp
, &tmp
))
360 * Skip <blank>s and any more colons (the command :3,5:print
361 * worked, historically).
363 for (; ecp
->clen
> 0; ++ecp
->cp
, --ecp
->clen
) {
365 if (!ISBLANK(ch
) && ch
!= ':')
370 * If no command, ex does the last specified of p, l, or #, and vi
371 * moves to the line. Otherwise, determine the length of the command
372 * name by looking for the first non-alphabetic character. (There
373 * are a few non-alphabetic characters in command names, but they're
374 * all single character commands.) This isn't a great test, because
375 * it means that, for the command ":e +cut.c file", we'll report that
376 * the command "cut" wasn't known. However, it makes ":e+35 file" work
380 * Historically, lines with multiple adjacent (or <blank> separated)
381 * command separators were very strange. For example, the command
382 * |||<carriage-return>, when the cursor was on line 1, displayed
383 * lines 2, 3 and 5 of the file. In addition, the command " | "
384 * would only display the line after the next line, instead of the
385 * next two lines. No ideas why. It worked reasonably when executed
386 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
387 * command for each separator.
389 #define SINGLE_CHAR_COMMANDS "\004!#&*<=>@~"
391 if (ecp
->clen
!= 0 && ecp
->cp
[0] != '|' && ecp
->cp
[0] != '\n') {
392 if (strchr(SINGLE_CHAR_COMMANDS
, *ecp
->cp
)) {
399 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
400 if (!ISALPHA(*ecp
->cp
))
402 if ((namelen
= ecp
->cp
- p
) == 0) {
403 msgq(sp
, M_ERR
, "080|Unknown command name");
410 * Historic vi permitted flags to immediately follow any
411 * subset of the 'delete' command, but then did not permit
412 * further arguments (flag, buffer, count). Make it work.
413 * Permit further arguments for the few shreds of dignity
416 * Adding commands that start with 'd', and match "delete"
417 * up to a l, p, +, - or # character can break this code.
420 * Capital letters beginning the command names ex, edit,
421 * next, previous, tag and visual (in vi mode) indicate the
422 * command should happen in a new screen.
427 n
= cmds
[C_DELETE
].name
; *s
== *n
; ++s
, ++n
);
428 if (s
[0] == 'l' || s
[0] == 'p' || s
[0] == '+' ||
429 s
[0] == '-' || s
[0] == '^' || s
[0] == '#') {
430 len
= (ecp
->cp
- p
) - (s
- p
);
433 ecp
->rcmd
= cmds
[C_DELETE
];
434 ecp
->rcmd
.syntax
= "1bca1";
435 ecp
->cmd
= &ecp
->rcmd
;
439 case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
441 p
[0] = TOLOWER(p
[0]);
446 * Search the table for the command.
449 * Historic vi permitted the mark to immediately follow the
450 * 'k' in the 'k' command. Make it work.
453 * Historic vi permitted any flag to follow the s command, e.g.
454 * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
455 * Since the following characters all have to be flags, i.e.
456 * alphabetics, we can let the s command routine return errors
457 * if it was some illegal command string. This code will break
458 * if an "sg" or similar command is ever added. The substitute
459 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
460 * follows the 's', but we limit the choices here to "cgr" so
461 * that we get unknown command messages for wrong combinations.
463 if ((ecp
->cmd
= ex_comm_search(sp
, p
, namelen
)) == NULL
)
467 ecp
->cp
-= namelen
- 1;
468 ecp
->clen
+= namelen
- 1;
469 ecp
->cmd
= &cmds
[C_K
];
474 for (s
= p
+ 1, cnt
= namelen
; --cnt
; ++s
)
476 s
[0] != 'g' && s
[0] != 'r')
479 ecp
->cp
-= namelen
- 1;
480 ecp
->clen
+= namelen
- 1;
481 ecp
->rcmd
= cmds
[C_SUBSTITUTE
];
482 ecp
->rcmd
.fn
= ex_subagain
;
483 ecp
->cmd
= &ecp
->rcmd
;
488 unknown
: if (newscreen
)
489 p
[0] = TOUPPER(p
[0]);
490 ex_unknown(sp
, p
, namelen
);
495 * The visual command has a different syntax when called
496 * from ex than when called from a vi colon command. FMH.
497 * Make the change now, before we test for the newscreen
498 * semantic, so that we're testing the right one.
500 skip_srch
: if (ecp
->cmd
== &cmds
[C_VISUAL_EX
] && F_ISSET(sp
, SC_VI
))
501 ecp
->cmd
= &cmds
[C_VISUAL_VI
];
505 * Historic vi permitted a capital 'P' at the beginning of
506 * any command that started with 'p'. Probably wanted the
507 * P[rint] command for backward compatibility, and the code
508 * just made Preserve and Put work by accident. Nvi uses
509 * Previous to mean previous-in-a-new-screen, so be careful.
511 if (newscreen
&& !F_ISSET(ecp
->cmd
, E_NEWSCREEN
) &&
512 (ecp
->cmd
== &cmds
[C_PRINT
] ||
513 ecp
->cmd
== &cmds
[C_PRESERVE
]))
516 /* Test for a newscreen associated with this command. */
517 if (newscreen
&& !F_ISSET(ecp
->cmd
, E_NEWSCREEN
))
520 /* Secure means no shell access. */
521 if (F_ISSET(ecp
->cmd
, E_SECURE
) && O_ISSET(sp
, O_SECURE
)) {
522 ex_wemsg(sp
, ecp
->cmd
->name
, EXM_SECURE
);
527 * Multiple < and > characters; another "feature". Note,
528 * The string passed to the underlying function may not be
529 * nul terminated in this case.
531 if ((ecp
->cmd
== &cmds
[C_SHIFTL
] && *p
== '<') ||
532 (ecp
->cmd
== &cmds
[C_SHIFTR
] && *p
== '>')) {
534 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
537 if (argv_exp0(sp
, ecp
, p
, ecp
->cp
- p
))
541 /* Set the format style flags for the next command. */
542 if (ecp
->cmd
== &cmds
[C_HASH
])
543 exp
->fdef
= E_C_HASH
;
544 else if (ecp
->cmd
== &cmds
[C_LIST
])
545 exp
->fdef
= E_C_LIST
;
546 else if (ecp
->cmd
== &cmds
[C_PRINT
])
547 exp
->fdef
= E_C_PRINT
;
548 F_CLR(ecp
, E_USELASTCMD
);
550 /* Print is the default command. */
551 ecp
->cmd
= &cmds
[C_PRINT
];
553 /* Set the saved format flags. */
554 F_SET(ecp
, exp
->fdef
);
558 * If no address was specified, and it's not a global command,
559 * we up the address by one. (I have no idea why globals are
560 * exempted, but it's (ahem) historic practice.)
562 if (ecp
->addrcnt
== 0 && !F_ISSET(sp
, SC_EX_GLOBAL
)) {
564 ecp
->addr1
.lno
= sp
->lno
+ 1;
565 ecp
->addr1
.cno
= sp
->cno
;
568 F_SET(ecp
, E_USELASTCMD
);
573 * Historically, the number option applied to both ex and vi. One
574 * strangeness was that ex didn't switch display formats until a
575 * command was entered, e.g. <CR>'s after the set didn't change to
576 * the new format, but :1p would.
578 if (O_ISSET(sp
, O_NUMBER
)) {
579 F_SET(ecp
, E_OPTNUM
);
580 FL_SET(ecp
->iflags
, E_C_HASH
);
582 F_CLR(ecp
, E_OPTNUM
);
584 /* Check for ex mode legality. */
585 if (F_ISSET(sp
, SC_EX
) && (F_ISSET(ecp
->cmd
, E_VIONLY
) || newscreen
)) {
586 msgq_wstr(sp
, M_ERR
, ecp
->cmd
->name
,
587 "082|%s: command not available in ex mode");
591 /* Add standard command flags. */
592 F_SET(ecp
, ecp
->cmd
->flags
);
594 F_CLR(ecp
, E_NEWSCREEN
);
597 * There are three normal termination cases for an ex command. They
598 * are the end of the string (ecp->clen), or unescaped (by <literal
599 * next> characters) <newline> or '|' characters. As we're now past
600 * possible addresses, we can determine how long the command is, so we
601 * don't have to look for all the possible terminations. Naturally,
602 * there are some exciting special cases:
604 * 1: The bang, global, v and the filter versions of the read and
605 * write commands are delimited by <newline>s (they can contain
607 * 2: The ex, edit, next and visual in vi mode commands all take ex
608 * commands as their first arguments.
609 * 3: The s command takes an RE as its first argument, and wants it
610 * to be specially delimited.
612 * Historically, '|' characters in the first argument of the ex, edit,
613 * next, vi visual, and s commands didn't delimit the command. And,
614 * in the filter cases for read and write, and the bang, global and v
615 * commands, they did not delimit the command at all.
617 * For example, the following commands were legal:
619 * :edit +25|s/abc/ABC/ file.c
621 * :read !spell % | columnate
622 * :global/pattern/p|l
624 * It's not quite as simple as it sounds, however. The command:
628 * was also legal, i.e. the historic ex parser (using the word loosely,
629 * since "parser" implies some regularity of syntax) delimited the RE's
630 * based on its delimiter and not anything so irretrievably vulgar as a
633 * Anyhow, the following code makes this all work. First, for the
634 * special cases we move past their special argument(s). Then, we
635 * do normal command processing on whatever is left. Barf-O-Rama.
637 discard
= 0; /* Characters discarded from the command. */
639 ecp
->save_cmd
= ecp
->cp
;
640 if (ecp
->cmd
== &cmds
[C_EDIT
] || ecp
->cmd
== &cmds
[C_EX
] ||
641 ecp
->cmd
== &cmds
[C_NEXT
] || ecp
->cmd
== &cmds
[C_VISUAL_VI
] ||
642 ecp
->cmd
== &cmds
[C_VSPLIT
]) {
644 * Move to the next non-whitespace character. A '!'
645 * immediately following the command is eaten as a
648 if (ecp
->clen
> 0 && *ecp
->cp
== '!') {
651 FL_SET(ecp
->iflags
, E_C_FORCE
);
653 /* Reset, don't reparse. */
654 ecp
->save_cmd
= ecp
->cp
;
656 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
657 if (!ISBLANK(*ecp
->cp
))
662 * The historic implementation ignored all escape characters
663 * so there was no way to put a space or newline into the +cmd
664 * field. We do a simplistic job of fixing it by moving to the
665 * first whitespace character that isn't escaped. The escaping
666 * characters are stripped as no longer useful.
668 if (ecp
->clen
> 0 && *ecp
->cp
== '+') {
671 for (arg1
= p
= ecp
->cp
;
672 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
674 if (IS_ESCAPE(sp
, ecp
, ch
) &&
679 } else if (ISBLANK(ch
))
683 arg1_len
= ecp
->cp
- arg1
;
685 /* Reset, so the first argument isn't reparsed. */
686 ecp
->save_cmd
= ecp
->cp
;
688 } else if (ecp
->cmd
== &cmds
[C_BANG
] ||
689 ecp
->cmd
== &cmds
[C_GLOBAL
] || ecp
->cmd
== &cmds
[C_V
]) {
693 * We use backslashes to escape <newline> characters, although
694 * this wasn't historic practice for the bang command. It was
695 * for the global and v commands, and it's common usage when
696 * doing text insert during the command. Escaping characters
697 * are stripped as no longer useful.
699 for (p
= ecp
->cp
; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
701 if (ch
== '\\' && ecp
->clen
> 1 && ecp
->cp
[1] == '\n') {
708 } else if (ch
== '\n')
712 } else if (ecp
->cmd
== &cmds
[C_READ
] || ecp
->cmd
== &cmds
[C_WRITE
]) {
714 * For write commands, if the next character is a <blank>, and
715 * the next non-blank character is a '!', it's a filter command
716 * and we want to eat everything up to the <newline>. For read
717 * commands, if the next non-blank character is a '!', it's a
718 * filter command and we want to eat everything up to the next
719 * <newline>. Otherwise, we're done.
721 for (tmp
= 0; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
728 if (ecp
->clen
> 0 && ch
== '!' &&
729 (ecp
->cmd
== &cmds
[C_READ
] || tmp
))
730 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
731 if (ecp
->cp
[0] == '\n')
733 } else if (ecp
->cmd
== &cmds
[C_SUBSTITUTE
]) {
735 * Move to the next non-whitespace character, we'll use it as
736 * the delimiter. If the character isn't an alphanumeric or
737 * a '|', it's the delimiter, so parse it. Otherwise, we're
738 * into something like ":s g", so use the special s command.
740 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
741 if (!ISBLANK(ecp
->cp
[0]))
744 if (ISALNUM(ecp
->cp
[0]) || ecp
->cp
[0] == '|') {
745 ecp
->rcmd
= cmds
[C_SUBSTITUTE
];
746 ecp
->rcmd
.fn
= ex_subagain
;
747 ecp
->cmd
= &ecp
->rcmd
;
748 } else if (ecp
->clen
> 0) {
752 * Backslashes quote delimiter characters for RE's.
753 * The backslashes are NOT removed since they'll be
754 * used by the RE code. Move to the third delimiter
755 * that's not escaped (or the end of the command).
760 for (cnt
= 2; ecp
->clen
> 0 &&
761 cnt
!= 0; --ecp
->clen
, ++ecp
->cp
)
762 if (ecp
->cp
[0] == '\\' &&
766 } else if (ecp
->cp
[0] == delim
)
772 * Use normal quoting and termination rules to find the end of this
777 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
778 * file. It was almost certainly a bug, but that's what bug-for-bug
779 * compatibility means, Grasshopper. Also, ^V's escape the command
780 * delimiters. Literal next quote characters in front of the newlines,
781 * '|' characters or literal next characters are stripped as they're
784 vi_address
= ecp
->clen
!= 0 && ecp
->cp
[0] != '\n';
785 for (p
= ecp
->cp
; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
787 if (IS_ESCAPE(sp
, ecp
, ch
) && ecp
->clen
> 1) {
788 CHAR_T tmp1
= ecp
->cp
[1];
789 if (tmp1
== '\n' || tmp1
== '|') {
799 } else if (ch
== '\n' || ch
== '|') {
801 F_SET(ecp
, E_NEWLINE
);
809 * Save off the next command information, go back to the
810 * original start of the command.
813 ecp
->cp
= ecp
->save_cmd
;
815 ecp
->save_cmdlen
= ecp
->clen
;
816 ecp
->clen
= ((ecp
->save_cmd
- ecp
->cp
) - 1) - discard
;
821 * The "set tags" command historically used a backslash, not the
822 * user's literal next character, to escape whitespace. Handle
823 * it here instead of complicating the argv_exp3() code. Note,
824 * this isn't a particularly complex trap, and if backslashes were
825 * legal in set commands, this would have to be much more complicated.
827 if (ecp
->cmd
== &cmds
[C_SET
])
828 for (p
= ecp
->cp
, len
= ecp
->clen
; len
> 0; --len
, ++p
)
833 * Set the default addresses. It's an error to specify an address for
834 * a command that doesn't take them. If two addresses are specified
835 * for a command that only takes one, lose the first one. Two special
836 * cases here, some commands take 0 or 2 addresses. For most of them
837 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
838 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
840 * Also, if the file is empty, some commands want to use an address of
841 * 0, i.e. the entire file is 0 to 0, and the default first address is
842 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
843 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
844 * case where the 0 address is only valid if it's a default address.
846 * Also, set a flag if we set the default addresses. Some commands
847 * (ex: z) care if the user specified an address or if we just used
848 * the current cursor.
850 switch (F_ISSET(ecp
, E_ADDR1
| E_ADDR2
| E_ADDR2_ALL
| E_ADDR2_NONE
)) {
851 case E_ADDR1
: /* One address: */
852 switch (ecp
->addrcnt
) {
853 case 0: /* Default cursor/empty file. */
855 F_SET(ecp
, E_ADDR_DEF
);
856 if (F_ISSET(ecp
, E_ADDR_ZERODEF
)) {
857 if (db_last(sp
, &lno
))
861 F_SET(ecp
, E_ADDR_ZERO
);
863 ecp
->addr1
.lno
= sp
->lno
;
865 ecp
->addr1
.lno
= sp
->lno
;
866 ecp
->addr1
.cno
= sp
->cno
;
870 case 2: /* Lose the first address. */
872 ecp
->addr1
= ecp
->addr2
;
875 case E_ADDR2_NONE
: /* Zero/two addresses: */
876 if (ecp
->addrcnt
== 0) /* Default to nothing. */
879 case E_ADDR2_ALL
: /* Zero/two addresses: */
880 if (ecp
->addrcnt
== 0) { /* Default entire/empty file. */
881 F_SET(ecp
, E_ADDR_DEF
);
885 else if (db_last(sp
, &ecp
->addr2
.lno
))
887 if (F_ISSET(ecp
, E_ADDR_ZERODEF
) &&
888 ecp
->addr2
.lno
== 0) {
890 F_SET(ecp
, E_ADDR_ZERO
);
893 ecp
->addr1
.cno
= ecp
->addr2
.cno
= 0;
894 F_SET(ecp
, E_ADDR2_ALL
);
898 case E_ADDR2
: /* Two addresses: */
899 two_addr
: switch (ecp
->addrcnt
) {
900 case 0: /* Default cursor/empty file. */
902 F_SET(ecp
, E_ADDR_DEF
);
904 F_ISSET(ecp
, E_ADDR_ZERODEF
)) {
905 if (db_last(sp
, &lno
))
908 ecp
->addr1
.lno
= ecp
->addr2
.lno
= 0;
909 F_SET(ecp
, E_ADDR_ZERO
);
912 ecp
->addr2
.lno
= sp
->lno
;
914 ecp
->addr1
.lno
= ecp
->addr2
.lno
= sp
->lno
;
915 ecp
->addr1
.cno
= ecp
->addr2
.cno
= sp
->cno
;
917 case 1: /* Default to first address. */
918 //ecp->addrcnt = 2; /* XXX Was this needed ??? */
919 ecp
->addr2
= ecp
->addr1
;
926 if (ecp
->addrcnt
) /* Error. */
932 * The ^D scroll command historically scrolled the value of the scroll
933 * option or to EOF. It was an error if the cursor was already at EOF.
934 * (Leading addresses were permitted, but were then ignored.)
936 if (ecp
->cmd
== &cmds
[C_SCROLL
]) {
938 ecp
->addr1
.lno
= sp
->lno
+ 1;
939 ecp
->addr2
.lno
= sp
->lno
+ O_VAL(sp
, O_SCROLL
);
940 ecp
->addr1
.cno
= ecp
->addr2
.cno
= sp
->cno
;
941 if (db_last(sp
, &lno
))
943 if (lno
!= 0 && lno
> sp
->lno
&& ecp
->addr2
.lno
> lno
)
944 ecp
->addr2
.lno
= lno
;
948 for (np
= ecp
->cmd
->syntax
; *np
!= '\0'; ++np
) {
950 * The force flag is sensitive to leading whitespace, i.e.
951 * "next !" is different from "next!". Handle it before
952 * skipping leading <blank>s.
955 if (ecp
->clen
> 0 && *ecp
->cp
== '!') {
958 FL_SET(ecp
->iflags
, E_C_FORCE
);
963 /* Skip leading <blank>s. */
964 for (; ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
)
965 if (!ISBLANK(*ecp
->cp
))
971 case '1': /* +, -, #, l, p */
974 * Historically, some flags were ignored depending
975 * on where they occurred in the command line. For
976 * example, in the command, ":3+++p--#", historic vi
977 * acted on the '#' flag, but ignored the '-' flags.
978 * It's unambiguous what the flags mean, so we just
979 * handle them regardless of the stupidity of their
982 for (; ecp
->clen
; --ecp
->clen
, ++ecp
->cp
)
992 F_CLR(ecp
, E_OPTNUM
);
993 FL_SET(ecp
->iflags
, E_C_HASH
);
994 exp
->fdef
|= E_C_HASH
;
997 FL_SET(ecp
->iflags
, E_C_LIST
);
998 exp
->fdef
|= E_C_LIST
;
1001 FL_SET(ecp
->iflags
, E_C_PRINT
);
1002 exp
->fdef
|= E_C_PRINT
;
1008 case '2': /* -, ., +, ^ */
1009 case '3': /* -, ., +, ^, = */
1010 for (; ecp
->clen
; --ecp
->clen
, ++ecp
->cp
)
1013 FL_SET(ecp
->iflags
, E_C_DASH
);
1016 FL_SET(ecp
->iflags
, E_C_DOT
);
1019 FL_SET(ecp
->iflags
, E_C_PLUS
);
1022 FL_SET(ecp
->iflags
, E_C_CARAT
);
1026 FL_SET(ecp
->iflags
, E_C_EQUAL
);
1034 case 'b': /* buffer */
1037 * Historically, "d #" was a delete with a flag, not a
1038 * delete into the '#' buffer. If the current command
1039 * permits a flag, don't use one as a buffer. However,
1040 * the 'l' and 'p' flags were legal buffer names in the
1041 * historic ex, and were used as buffers, not flags.
1043 if ((ecp
->cp
[0] == '+' || ecp
->cp
[0] == '-' ||
1044 ecp
->cp
[0] == '^' || ecp
->cp
[0] == '#') &&
1045 strchr(np
, '1') != NULL
)
1049 * Digits can't be buffer names in ex commands, or the
1050 * command "d2" would be a delete into buffer '2', and
1051 * not a two-line deletion.
1053 if (!ISDIGIT(ecp
->cp
[0])) {
1054 ecp
->buffer
= *ecp
->cp
;
1057 FL_SET(ecp
->iflags
, E_C_BUFFER
);
1060 case 'c': /* count [01+a] */
1062 /* Validate any signed value. */
1063 if (!ISDIGIT(*ecp
->cp
) && (*np
!= '+' ||
1064 (*ecp
->cp
!= '+' && *ecp
->cp
!= '-')))
1066 /* If a signed value, set appropriate flags. */
1067 if (*ecp
->cp
== '-')
1068 FL_SET(ecp
->iflags
, E_C_COUNT_NEG
);
1069 else if (*ecp
->cp
== '+')
1070 FL_SET(ecp
->iflags
, E_C_COUNT_POS
);
1072 nget_slong(sp
, <mp
, ecp
->cp
, &t
, 10)) != NUM_OK
) {
1073 ex_badaddr(sp
, NULL
, A_NOTSET
, nret
);
1076 if (ltmp
== 0 && *np
!= '0') {
1077 msgq(sp
, M_ERR
, "083|Count may not be zero");
1080 ecp
->clen
-= (t
- ecp
->cp
);
1084 * Counts as address offsets occur in commands taking
1085 * two addresses. Historic vi practice was to use
1086 * the count as an offset from the *second* address.
1088 * Set a count flag; some underlying commands (see
1089 * join) do different things with counts than with
1093 ecp
->addr1
= ecp
->addr2
;
1094 ecp
->addr2
.lno
= ecp
->addr1
.lno
+ ltmp
- 1;
1097 FL_SET(ecp
->iflags
, E_C_COUNT
);
1099 case 'f': /* file */
1100 if (argv_exp2(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1103 case 'l': /* line */
1105 * Get a line specification.
1107 * If the line was a search expression, we may have
1108 * changed state during the call, and we're now
1109 * searching the file. Push ourselves onto the state
1112 if (ex_line(sp
, ecp
, &cur
, &isaddr
, &tmp
))
1117 /* Line specifications are always required. */
1119 msgq_wstr(sp
, M_ERR
, ecp
->cp
,
1120 "084|%s: bad line specification");
1124 * The target line should exist for these commands,
1125 * but 0 is legal for them as well.
1127 if (cur
.lno
!= 0 && !db_exist(sp
, cur
.lno
)) {
1128 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1131 ecp
->lineno
= cur
.lno
;
1133 case 'S': /* string, file exp. */
1134 if (ecp
->clen
!= 0) {
1135 if (argv_exp1(sp
, ecp
, ecp
->cp
,
1136 ecp
->clen
, ecp
->cmd
== &cmds
[C_BANG
]))
1141 case 's': /* string */
1142 if (argv_exp0(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1145 case 'W': /* word string */
1149 * Literal next characters escape the following
1150 * character. Quoting characters are stripped here
1151 * since they are no longer useful.
1153 * First there was the word.
1155 for (p
= t
= ecp
->cp
;
1156 ecp
->clen
> 0; --ecp
->clen
, ++ecp
->cp
) {
1159 ecp
, ch
) && ecp
->clen
> 1) {
1162 } else if (ISBLANK(ch
)) {
1169 if (argv_exp0(sp
, ecp
, t
, p
- t
))
1172 /* Delete intervening whitespace. */
1173 for (; ecp
->clen
> 0;
1174 --ecp
->clen
, ++ecp
->cp
) {
1182 /* Followed by the string. */
1183 for (p
= t
= ecp
->cp
; ecp
->clen
> 0;
1184 --ecp
->clen
, ++ecp
->cp
, ++p
) {
1187 ecp
, ch
) && ecp
->clen
> 1) {
1193 if (argv_exp0(sp
, ecp
, t
, p
- t
))
1196 case 'w': /* word */
1197 if (argv_exp3(sp
, ecp
, ecp
->cp
, ecp
->clen
))
1199 arg_cnt_chk
: if (*++np
!= 'N') { /* N */
1201 * If a number is specified, must either be
1202 * 0 or that number, if optional, and that
1203 * number, if required.
1206 if ((*++np
!= 'o' || exp
->argsoff
!= 0) &&
1207 exp
->argsoff
!= tmp
)
1214 INT2CHAR(sp
, ecp
->cmd
->name
, STRLEN(ecp
->cmd
->name
) + 1,
1217 "085|Internal syntax table error (%s: %s)",
1218 nstr
, KEY_NAME(sp
, *np
));
1223 /* Skip trailing whitespace. */
1224 for (; ecp
->clen
> 0; --ecp
->clen
) {
1231 * There shouldn't be anything left, and no more required fields,
1232 * i.e neither 'l' or 'r' in the syntax string.
1234 if (ecp
->clen
!= 0 || strpbrk(np
, "lr")) {
1235 usage
: msgq(sp
, M_ERR
, "086|Usage: %s", ecp
->cmd
->usage
);
1240 * Verify that the addresses are legal. Check the addresses here,
1241 * because this is a place where all ex addresses pass through.
1242 * (They don't all pass through ex_line(), for instance.) We're
1243 * assuming that any non-existent line doesn't exist because it's
1244 * past the end-of-file. That's a pretty good guess.
1246 * If it's a "default vi command", an address of zero is okay.
1249 switch (ecp
->addrcnt
) {
1252 * Historic ex/vi permitted commands with counts to go past
1253 * EOF. So, for example, if the file only had 5 lines, the
1254 * ex command "1,6>" would fail, but the command ">300"
1255 * would succeed. Since we don't want to have to make all
1256 * of the underlying commands handle random line numbers,
1259 if (ecp
->addr2
.lno
== 0) {
1260 if (!F_ISSET(ecp
, E_ADDR_ZERO
) &&
1261 (F_ISSET(sp
, SC_EX
) ||
1262 !F_ISSET(ecp
, E_USELASTCMD
))) {
1263 ex_badaddr(sp
, ecp
->cmd
, A_ZERO
, NUM_OK
);
1266 } else if (!db_exist(sp
, ecp
->addr2
.lno
)) {
1267 if (FL_ISSET(ecp
->iflags
, E_C_COUNT
)) {
1268 if (db_last(sp
, &lno
))
1270 ecp
->addr2
.lno
= lno
;
1272 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1278 if (ecp
->addr1
.lno
== 0) {
1279 if (!F_ISSET(ecp
, E_ADDR_ZERO
) &&
1280 (F_ISSET(sp
, SC_EX
) ||
1281 !F_ISSET(ecp
, E_USELASTCMD
))) {
1282 ex_badaddr(sp
, ecp
->cmd
, A_ZERO
, NUM_OK
);
1285 } else if (!db_exist(sp
, ecp
->addr1
.lno
)) {
1286 ex_badaddr(sp
, NULL
, A_EOF
, NUM_OK
);
1293 * If doing a default command and there's nothing left on the line,
1294 * vi just moves to the line. For example, ":3" and ":'a,'b" just
1295 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1298 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1302 * This is done before the absolute mark gets set; historically,
1303 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1305 if ((F_ISSET(sp
, SC_VI
) || F_ISSET(ecp
, E_NOPRDEF
)) &&
1306 F_ISSET(ecp
, E_USELASTCMD
) && vi_address
== 0) {
1307 switch (ecp
->addrcnt
) {
1310 (ecp
->addr2
.lno
? ecp
->addr2
.lno
: 1)) {
1312 ecp
->addr2
.lno
? ecp
->addr2
.lno
: 1;
1314 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
1319 (ecp
->addr1
.lno
? ecp
->addr1
.lno
: 1)) {
1321 ecp
->addr1
.lno
? ecp
->addr1
.lno
: 1;
1323 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
1327 ecp
->cp
= ecp
->save_cmd
;
1328 ecp
->clen
= ecp
->save_cmdlen
;
1333 * Set the absolute mark -- we have to set it for vi here, in case
1334 * it's a compound command, e.g. ":5p|6" should set the absolute
1337 if (F_ISSET(ecp
, E_ABSMARK
)) {
1340 F_CLR(ecp
, E_ABSMARK
);
1341 if (mark_set(sp
, ABSMARK1
, &cur
, 1))
1345 #if defined(DEBUG) && defined(COMLOG)
1348 /* Increment the command count if not called from vi. */
1349 if (F_ISSET(sp
, SC_EX
))
1353 * If file state available, and not doing a global command,
1354 * log the start of an action.
1356 if (sp
->ep
!= NULL
&& !F_ISSET(sp
, SC_EX_GLOBAL
))
1357 (void)log_cursor(sp
);
1361 * There are two special commands for the purposes of this code: the
1362 * default command (<carriage-return>) or the scrolling commands (^D
1363 * and <EOF>) as the first non-<blank> characters in the line.
1365 * If this is the first command in the command line, we received the
1366 * command from the ex command loop and we're talking to a tty, and
1367 * and there's nothing else on the command line, and it's one of the
1368 * special commands, we move back up to the previous line, and erase
1369 * the prompt character with the output. Since ex runs in canonical
1370 * mode, we don't have to do anything else, a <newline> has already
1371 * been echoed by the tty driver. It's OK if vi calls us -- we won't
1372 * be in ex mode so we'll do nothing.
1374 if (F_ISSET(ecp
, E_NRSEP
)) {
1375 if (sp
->ep
!= NULL
&&
1376 F_ISSET(sp
, SC_EX
) && !F_ISSET(gp
, G_SCRIPTED
) &&
1377 (F_ISSET(ecp
, E_USELASTCMD
) || ecp
->cmd
== &cmds
[C_SCROLL
]))
1378 gp
->scr_ex_adjust(sp
, EX_TERM_SCROLL
);
1379 F_CLR(ecp
, E_NRSEP
);
1383 * Call the underlying function for the ex command.
1386 * Interrupts behave like errors, for now.
1388 if (ecp
->cmd
->fn(sp
, ecp
) || INTERRUPTED(sp
)) {
1389 if (F_ISSET(gp
, G_SCRIPTED
))
1390 F_SET(sp
, SC_EXIT_FORCE
);
1395 /* Make sure no function left global temporary space locked. */
1396 if (F_ISSET(wp
, W_TMP_INUSE
)) {
1397 F_CLR(wp
, W_TMP_INUSE
);
1398 msgq(sp
, M_ERR
, "087|%s: temporary buffer not released",
1403 * Ex displayed the number of lines modified immediately after each
1404 * command, so the command "1,10d|1,10d" would display:
1410 * Executing ex commands from vi only reported the final modified
1411 * lines message -- that's wrong enough that we don't match it.
1413 if (F_ISSET(sp
, SC_EX
))
1417 * Integrate any offset parsed by the underlying command, and make
1418 * sure the referenced line exists.
1421 * May not match historic practice (which I've never been able to
1422 * completely figure out.) For example, the '=' command from vi
1423 * mode often got the offset wrong, and complained it was too large,
1424 * but didn't seem to have a problem with the cursor. If anyone
1425 * complains, ask them how it's supposed to work, they might know.
1427 if (sp
->ep
!= NULL
&& ecp
->flagoff
) {
1428 if (ecp
->flagoff
< 0) {
1429 if (sp
->lno
<= (db_recno_t
)(-ecp
->flagoff
)) {
1431 "088|Flag offset to before line 1");
1435 if (!NPFITS(DB_MAX_RECORDS
, sp
->lno
, (db_recno_t
)ecp
->flagoff
)) {
1436 ex_badaddr(sp
, NULL
, A_NOTSET
, NUM_OVER
);
1439 if (!db_exist(sp
, sp
->lno
+ ecp
->flagoff
)) {
1441 "089|Flag offset past end-of-file");
1445 sp
->lno
+= ecp
->flagoff
;
1449 * If the command executed successfully, we may want to display a line
1450 * based on the autoprint option or an explicit print flag. (Make sure
1451 * that there's a line to display.) Also, the autoprint edit option is
1452 * turned off for the duration of global commands.
1454 if (F_ISSET(sp
, SC_EX
) && sp
->ep
!= NULL
&& sp
->lno
!= 0) {
1456 * The print commands have already handled the `print' flags.
1457 * If so, clear them.
1459 if (FL_ISSET(ecp
->iflags
, E_CLRFLAG
))
1460 FL_CLR(ecp
->iflags
, E_C_HASH
| E_C_LIST
| E_C_PRINT
);
1462 /* If hash set only because of the number option, discard it. */
1463 if (F_ISSET(ecp
, E_OPTNUM
))
1464 FL_CLR(ecp
->iflags
, E_C_HASH
);
1467 * If there was an explicit flag to display the new cursor line,
1468 * or autoprint is set and a change was made, display the line.
1469 * If any print flags were set use them, else default to print.
1471 LF_INIT(FL_ISSET(ecp
->iflags
, E_C_HASH
| E_C_LIST
| E_C_PRINT
));
1472 if (!LF_ISSET(E_C_HASH
| E_C_LIST
| E_C_PRINT
| E_NOAUTO
) &&
1473 !F_ISSET(sp
, SC_EX_GLOBAL
) &&
1474 O_ISSET(sp
, O_AUTOPRINT
) && F_ISSET(ecp
, E_AUTOPRINT
))
1477 if (LF_ISSET(E_C_HASH
| E_C_LIST
| E_C_PRINT
)) {
1480 (void)ex_print(sp
, ecp
, &cur
, &cur
, flags
);
1485 * If the command had an associated "+cmd", it has to be executed
1486 * before we finish executing any more of this ex command. For
1487 * example, consider a .exrc file that contains the following lines:
1490 * :edit +25 file.c|s/abc/ABC/|1
1493 * This can happen more than once -- the historic vi simply hung or
1494 * dropped core, of course. Prepend the + command back into the
1495 * current command and continue. We may have to add an additional
1496 * <literal next> character. We know that it will fit because we
1497 * discarded at least one space and the + character.
1499 if (arg1_len
!= 0) {
1501 * If the last character of the + command was a <literal next>
1502 * character, it would be treated differently because of the
1503 * append. Quote it, if necessary.
1505 if (IS_ESCAPE(sp
, ecp
, arg1
[arg1_len
- 1])) {
1506 *--ecp
->save_cmd
= CH_LITERAL
;
1510 ecp
->save_cmd
-= arg1_len
;
1511 ecp
->save_cmdlen
+= arg1_len
;
1512 MEMCPYW(ecp
->save_cmd
, arg1
, arg1_len
);
1515 * Any commands executed from a +cmd are executed starting at
1516 * the first column of the last line of the file -- NOT the
1517 * first nonblank.) The main file startup code doesn't know
1518 * that a +cmd was set, however, so it may have put us at the
1519 * top of the file. (Note, this is safe because we must have
1520 * switched files to get here.)
1522 F_SET(ecp
, E_MOVETOEND
);
1525 /* Update the current command. */
1526 ecp
->cp
= ecp
->save_cmd
;
1527 ecp
->clen
= ecp
->save_cmdlen
;
1531 * If we've changed screens or underlying files, any pending global or
1532 * v command, or @ buffer that has associated addresses, has to be
1533 * discarded. This is historic practice for globals, and necessary for
1534 * @ buffers that had associated addresses.
1536 * Otherwise, if we've changed underlying files, it's not a problem,
1537 * we continue with the rest of the ex command(s), operating on the
1538 * new file. However, if we switch screens (either by exiting or by
1539 * an explicit command), we have no way of knowing where to put output
1540 * messages, and, since we don't control screens here, we could screw
1541 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1542 * times). So, return and continue after we've got a new screen.
1544 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
| SC_FSWITCH
| SC_SSWITCH
)) {
1545 at_found
= gv_found
= 0;
1546 for (ecp
= wp
->ecq
.lh_first
;
1547 ecp
!= NULL
; ecp
= ecp
->q
.le_next
)
1548 switch (ecp
->agv_flags
) {
1550 case AGV_AT_NORANGE
:
1556 "090|@ with range running when the file/screen changed");
1564 "091|Global/v command running when the file/screen changed");
1570 if (at_found
|| gv_found
)
1572 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
| SC_SSWITCH
))
1580 * On command failure, we discard keys and pending commands remaining,
1581 * as well as any keys that were mapped and waiting. The save_cmdlen
1582 * test is not necessarily correct. If we fail early enough we don't
1583 * know if the entire string was a single command or not. Guess, as
1584 * it's useful to know if commands other than the current one are being
1587 if (ecp
->save_cmdlen
== 0)
1588 for (; ecp
->clen
; --ecp
->clen
) {
1590 if (IS_ESCAPE(sp
, ecp
, ch
) && ecp
->clen
> 1) {
1593 } else if (ch
== '\n' || ch
== '|') {
1595 ecp
->save_cmdlen
= 1;
1599 if (ecp
->save_cmdlen
!= 0 || wp
->ecq
.lh_first
!= &wp
->excmd
) {
1600 discard
: msgq(sp
, M_BERR
,
1601 "092|Ex command failed: pending commands discarded");
1604 if (v_event_flush(sp
, CH_MAPPED
))
1606 "093|Ex command failed: mapped keys discarded");
1612 /* Turn off any file name error information. */
1615 /* Turn off the global bit. */
1616 F_CLR(sp
, SC_EX_GLOBAL
);
1623 * Get a line range for ex commands, or perform a vi ex address search.
1625 * PUBLIC: int ex_range __P((SCR *, EXCMD *, int *));
1628 ex_range(SCR
*sp
, EXCMD
*ecp
, int *errp
)
1630 enum { ADDR_FOUND
, ADDR_NEED
, ADDR_NONE
} addr
;
1639 * Parse comma or semi-colon delimited line specs.
1641 * Semi-colon delimiters update the current address to be the last
1642 * address. For example, the command
1644 * :3;/pattern/ecp->cp
1646 * will search for pattern from line 3. In addition, if ecp->cp
1647 * is not a valid command, the current line will be left at 3, not
1648 * at the original address.
1650 * Extra addresses are discarded, starting with the first.
1653 * If any addresses are missing, they default to the current line.
1654 * This was historically true for both leading and trailing comma
1655 * delimited addresses as well as for trailing semicolon delimited
1656 * addresses. For consistency, we make it true for leading semicolon
1657 * addresses as well.
1661 for (addr
= ADDR_NONE
, ecp
->addrcnt
= 0; ecp
->clen
> 0;)
1663 case '%': /* Entire file. */
1664 /* Vi ex address searches didn't permit % signs. */
1665 if (F_ISSET(ecp
, E_VISEARCH
))
1668 /* It's an error if the file is empty. */
1669 if (sp
->ep
== NULL
) {
1670 ex_badaddr(sp
, NULL
, A_EMPTY
, NUM_OK
);
1676 * A percent character addresses all of the lines in
1677 * the file. Historically, it couldn't be followed by
1678 * any other address. We do it as a text substitution
1679 * for simplicity. POSIX 1003.2 is expected to follow
1682 * If it's an empty file, the first line is 0, not 1.
1684 if (addr
== ADDR_FOUND
) {
1685 ex_badaddr(sp
, NULL
, A_COMBO
, NUM_OK
);
1689 if (db_last(sp
, &ecp
->addr2
.lno
))
1691 ecp
->addr1
.lno
= ecp
->addr2
.lno
== 0 ? 0 : 1;
1692 ecp
->addr1
.cno
= ecp
->addr2
.cno
= 0;
1698 case ',': /* Comma delimiter. */
1699 /* Vi ex address searches didn't permit commas. */
1700 if (F_ISSET(ecp
, E_VISEARCH
))
1703 case ';': /* Semi-colon delimiter. */
1704 if (sp
->ep
== NULL
) {
1705 ex_badaddr(sp
, NULL
, A_EMPTY
, NUM_OK
);
1709 if (addr
!= ADDR_FOUND
)
1710 switch (ecp
->addrcnt
) {
1712 ecp
->addr1
.lno
= sp
->lno
;
1713 ecp
->addr1
.cno
= sp
->cno
;
1717 ecp
->addr1
= ecp
->addr2
;
1720 ecp
->addr2
.lno
= sp
->lno
;
1721 ecp
->addr2
.cno
= sp
->cno
;
1725 if (*ecp
->cp
== ';')
1726 switch (ecp
->addrcnt
) {
1731 sp
->lno
= ecp
->addr1
.lno
;
1732 sp
->cno
= ecp
->addr1
.cno
;
1735 sp
->lno
= ecp
->addr2
.lno
;
1736 sp
->cno
= ecp
->addr2
.cno
;
1741 case ' ': /* Whitespace. */
1742 case '\t': /* Whitespace. */
1747 /* Get a line specification. */
1748 if (ex_line(sp
, ecp
, &m
, &isaddr
, errp
))
1754 if (addr
== ADDR_FOUND
) {
1755 ex_badaddr(sp
, NULL
, A_COMBO
, NUM_OK
);
1759 switch (ecp
->addrcnt
) {
1769 ecp
->addr1
= ecp
->addr2
;
1779 * Vi ex address searches are indifferent to order or trailing
1782 ret
: if (F_ISSET(ecp
, E_VISEARCH
))
1785 if (addr
== ADDR_NEED
)
1786 switch (ecp
->addrcnt
) {
1788 ecp
->addr1
.lno
= sp
->lno
;
1789 ecp
->addr1
.cno
= sp
->cno
;
1793 ecp
->addr1
= ecp
->addr2
;
1796 ecp
->addr2
.lno
= sp
->lno
;
1797 ecp
->addr2
.cno
= sp
->cno
;
1802 if (ecp
->addrcnt
== 2 && ecp
->addr2
.lno
< ecp
->addr1
.lno
) {
1804 "094|The second address is smaller than the first");
1812 * Get a single line address specifier.
1814 * The way the "previous context" mark worked was that any "non-relative"
1815 * motion set it. While ex/vi wasn't totally consistent about this, ANY
1816 * numeric address, search pattern, '$', or mark reference in an address
1817 * was considered non-relative, and set the value. Which should explain
1818 * why we're hacking marks down here. The problem was that the mark was
1819 * only set if the command was called, i.e. we have to set a flag and test
1823 * This is probably still not exactly historic practice, although I think
1824 * it's fairly close.
1827 ex_line(SCR
*sp
, EXCMD
*ecp
, MARK
*mp
, int *isaddrp
, int *errp
)
1835 int (*sf
) __P((SCR
*, MARK
*, MARK
*, CHAR_T
*, size_t, CHAR_T
**, u_int
));
1841 *isaddrp
= *errp
= 0;
1842 F_CLR(ecp
, E_DELTA
);
1844 /* No addresses permitted until a file has been read in. */
1845 if (sp
->ep
== NULL
&& strchr("$0123456789'\\/?.+-^", *ecp
->cp
)) {
1846 ex_badaddr(sp
, NULL
, A_EMPTY
, NUM_OK
);
1852 case '$': /* Last line in the file. */
1854 F_SET(ecp
, E_ABSMARK
);
1857 if (db_last(sp
, &mp
->lno
))
1861 break; /* Absolute line number. */
1862 case '0': case '1': case '2': case '3': case '4':
1863 case '5': case '6': case '7': case '8': case '9':
1865 F_SET(ecp
, E_ABSMARK
);
1867 if ((nret
= nget_uslong(sp
, &uval
, ecp
->cp
, &endp
, 10)) != NUM_OK
) {
1868 ex_badaddr(sp
, NULL
, A_NOTSET
, nret
);
1872 if (!NPFITS(DB_MAX_RECORDS
, 0, uval
)) {
1873 ex_badaddr(sp
, NULL
, A_NOTSET
, NUM_OVER
);
1879 ecp
->clen
-= (endp
- ecp
->cp
);
1882 case '\'': /* Use a mark. */
1884 F_SET(ecp
, E_ABSMARK
);
1886 if (ecp
->clen
== 1) {
1887 msgq(sp
, M_ERR
, "095|No mark name supplied");
1891 if (mark_get(sp
, ecp
->cp
[1], mp
, M_ERR
)) {
1898 case '\\': /* Search: forward/backward. */
1901 * I can't find any difference between // and \/ or between
1902 * ?? and \?. Mark Horton doesn't remember there being any
1903 * difference. C'est la vie.
1905 if (ecp
->clen
< 2 ||
1906 (ecp
->cp
[1] != '/' && ecp
->cp
[1] != '?')) {
1907 msgq(sp
, M_ERR
, "096|\\ not followed by / or ?");
1913 sf
= ecp
->cp
[0] == '/' ? f_search
: b_search
;
1915 case '/': /* Search forward. */
1918 case '?': /* Search backward. */
1921 search
: mp
->lno
= sp
->lno
;
1923 if (sf(sp
, mp
, mp
, ecp
->cp
, ecp
->clen
, &endp
,
1924 SEARCH_MSG
| SEARCH_PARSE
| SEARCH_SET
|
1925 (F_ISSET(ecp
, E_SEARCH_WMSG
) ? SEARCH_WMSG
: 0))) {
1930 /* Fix up the command pointers. */
1931 ecp
->clen
-= (endp
- ecp
->cp
);
1935 F_SET(ecp
, E_ABSMARK
);
1937 case '.': /* Current position. */
1941 /* If an empty file, then '.' is 0, not 1. */
1943 if (db_last(sp
, &mp
->lno
))
1952 * Historically, .<number> was the same as .+<number>, i.e.
1953 * the '+' could be omitted. (This feature is found in ed
1956 if (ecp
->clen
> 1 && ISDIGIT(ecp
->cp
[1]))
1965 /* Skip trailing <blank>s. */
1966 for (; ecp
->clen
> 0 &&
1967 ISBLANK(ecp
->cp
[0]); ++ecp
->cp
, --ecp
->clen
);
1970 * Evaluate any offset. If no address yet found, the offset
1971 * is relative to ".".
1974 if (ecp
->clen
!= 0 && (ISDIGIT(ecp
->cp
[0]) ||
1975 ecp
->cp
[0] == '+' || ecp
->cp
[0] == '-' ||
1976 ecp
->cp
[0] == '^')) {
1983 * Evaluate an offset, defined as:
1985 * [+-^<blank>]*[<blank>]*[0-9]*
1987 * The rough translation is any number of signs, optionally
1988 * followed by numbers, or a number by itself, all <blank>
1992 * All address offsets were additive, e.g. "2 2 3p" was the
1993 * same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2".
1994 * Note, however, "2 /ZZZ/" was an error. It was also legal
1995 * to insert signs without numbers, so "3 - 2" was legal, and
1999 * Offsets were historically permitted for any line address,
2000 * e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after
2004 * Offsets were historically permitted for search commands,
2005 * and handled as addresses: "/pattern/2 2 2" was legal, and
2006 * referenced the 6th line after pattern.
2008 F_SET(ecp
, E_DELTA
);
2010 for (; ecp
->clen
> 0 && ISBLANK(ecp
->cp
[0]);
2011 ++ecp
->cp
, --ecp
->clen
);
2012 if (ecp
->clen
== 0 || (!ISDIGIT(ecp
->cp
[0]) &&
2013 ecp
->cp
[0] != '+' && ecp
->cp
[0] != '-' &&
2016 if (!ISDIGIT(ecp
->cp
[0]) &&
2017 !ISDIGIT(ecp
->cp
[1])) {
2018 total
+= ecp
->cp
[0] == '+' ? 1 : -1;
2022 if (ecp
->cp
[0] == '-' ||
2023 ecp
->cp
[0] == '^') {
2030 /* Get a signed long, add it to the total. */
2031 if ((nret
= nget_slong(sp
, &val
,
2032 ecp
->cp
, &endp
, 10)) != NUM_OK
||
2033 (nret
= NADD_SLONG(sp
,
2034 total
, val
)) != NUM_OK
) {
2035 ex_badaddr(sp
, NULL
, A_NOTSET
, nret
);
2039 total
+= isneg
? -val
: val
;
2040 ecp
->clen
-= (endp
- ecp
->cp
);
2047 * Any value less than 0 is an error. Make sure that the new value
2048 * will fit into a db_recno_t.
2050 if (*isaddrp
&& total
!= 0) {
2052 if ((db_recno_t
)-total
> mp
->lno
) {
2054 "097|Reference to a line number less than 0");
2059 if (!NPFITS(DB_MAX_RECORDS
, mp
->lno
, (unsigned long)total
)) {
2060 ex_badaddr(sp
, NULL
, A_NOTSET
, NUM_OVER
);
2072 * Load up the next command, which may be an @ buffer or global command.
2081 F_CLR(sp
, SC_EX_GLOBAL
);
2084 * Lose any exhausted commands. We know that the first command
2085 * can't be an AGV command, which makes things a bit easier.
2087 for (wp
= sp
->wp
;;) {
2089 * If we're back to the original structure, leave it around,
2090 * but discard any allocated source name, we've returned to
2091 * the beginning of the command stack.
2093 if ((ecp
= wp
->ecq
.lh_first
) == &wp
->excmd
) {
2094 if (F_ISSET(ecp
, E_NAMEDISCARD
)) {
2096 ecp
->if_name
= NULL
;
2102 * ecp->clen will be 0 for the first discarded command, but
2103 * may not be 0 for subsequent ones, e.g. if the original
2104 * command was ":g/xx/@a|s/b/c/", then when we discard the
2105 * command pushed on the stack by the @a, we have to resume
2106 * the global command which included the substitute command.
2112 * If it's an @, global or v command, we may need to continue
2113 * the command on a different line.
2115 if (FL_ISSET(ecp
->agv_flags
, AGV_ALL
)) {
2116 /* Discard any exhausted ranges. */
2117 while ((rp
= ecp
->rq
.cqh_first
) != (void *)&ecp
->rq
)
2118 if (rp
->start
> rp
->stop
) {
2119 CIRCLEQ_REMOVE(&ecp
->rq
, rp
, q
);
2124 /* If there's another range, continue with it. */
2125 if (rp
!= (void *)&ecp
->rq
)
2128 /* If it's a global/v command, fix up the last line. */
2129 if (FL_ISSET(ecp
->agv_flags
,
2130 AGV_GLOBAL
| AGV_V
) && ecp
->range_lno
!= OOBLNO
) {
2131 if (db_exist(sp
, ecp
->range_lno
))
2132 sp
->lno
= ecp
->range_lno
;
2134 if (db_last(sp
, &sp
->lno
))
2143 /* Discard the EXCMD. */
2144 LIST_REMOVE(ecp
, q
);
2149 * We only get here if it's an active @, global or v command. Set
2150 * the current line number, and get a new copy of the command for
2151 * the parser. Note, the original pointer almost certainly moved,
2152 * so we have play games.
2154 ecp
->cp
= ecp
->o_cp
;
2155 MEMCPYW(ecp
->cp
, ecp
->cp
+ ecp
->o_clen
, ecp
->o_clen
);
2156 ecp
->clen
= ecp
->o_clen
;
2157 ecp
->range_lno
= sp
->lno
= rp
->start
++;
2159 if (FL_ISSET(ecp
->agv_flags
, AGV_GLOBAL
| AGV_V
))
2160 F_SET(sp
, SC_EX_GLOBAL
);
2166 * Discard any pending ex commands.
2176 * We know the first command can't be an AGV command, so we don't
2177 * process it specially. We do, however, nail the command itself.
2179 for (wp
= sp
->wp
; (ecp
= wp
->ecq
.lh_first
) != &wp
->excmd
;) {
2180 if (FL_ISSET(ecp
->agv_flags
, AGV_ALL
)) {
2181 while ((rp
= ecp
->rq
.cqh_first
) != (void *)&ecp
->rq
) {
2182 CIRCLEQ_REMOVE(&ecp
->rq
, rp
, q
);
2187 LIST_REMOVE(ecp
, q
);
2190 wp
->ecq
.lh_first
->clen
= 0;
2196 * Display an unknown command name.
2199 ex_unknown(SCR
*sp
, CHAR_T
*cmd
, size_t len
)
2204 GET_SPACE_GOTOW(sp
, bp
, blen
, len
+ 1);
2206 MEMCPYW(bp
, cmd
, len
);
2207 msgq_wstr(sp
, M_ERR
, bp
, "098|The %s command is unknown");
2208 FREE_SPACEW(sp
, bp
, blen
);
2216 * The vi text input routine needs to know if ex thinks this is an
2217 * [un]abbreviate command, so it can turn off abbreviations. See
2218 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2220 * PUBLIC: int ex_is_abbrev __P((SCR *, CHAR_T *, size_t));
2223 ex_is_abbrev(SCR
*sp
, CHAR_T
*name
, size_t len
)
2225 EXCMDLIST
const *cp
;
2227 return ((cp
= ex_comm_search(sp
, name
, len
)) != NULL
&&
2228 (cp
== &cmds
[C_ABBR
] || cp
== &cmds
[C_UNABBREVIATE
]));
2233 * The vi text input routine needs to know if ex thinks this is an
2234 * unmap command, so it can turn off input mapping. See the usual
2235 * ranting in the vi/v_txt_ev.c:txt_unmap() routine.
2237 * PUBLIC: int ex_is_unmap __P((SCR *, CHAR_T *, size_t));
2240 ex_is_unmap(SCR
*sp
, CHAR_T
*name
, size_t len
)
2242 EXCMDLIST
const *cp
;
2245 * The command the vi input routines are really interested in
2246 * is "unmap!", not just unmap.
2248 if (name
[len
- 1] != '!')
2251 return ((cp
= ex_comm_search(sp
, name
, len
)) != NULL
&&
2252 cp
== &cmds
[C_UNMAP
]);
2257 * Search for a command name.
2259 static EXCMDLIST
const *
2260 ex_comm_search(SCR
*sp
, CHAR_T
*name
, size_t len
)
2262 EXCMDLIST
const *cp
;
2264 for (cp
= cmds
; cp
->name
!= NULL
; ++cp
) {
2265 if (cp
->name
[0] > name
[0])
2267 if (cp
->name
[0] != name
[0])
2269 if (!MEMCMP(name
, cp
->name
, len
))
2277 * Display a bad address message.
2279 * PUBLIC: void ex_badaddr
2280 * PUBLIC: __P((SCR *, EXCMDLIST const *, enum badaddr, enum nresult));
2283 ex_badaddr(SCR
*sp
, const EXCMDLIST
*cp
, enum badaddr ba
, enum nresult nret
)
2291 msgq(sp
, M_SYSERR
, NULL
);
2294 msgq(sp
, M_ERR
, "099|Address value overflow");
2297 msgq(sp
, M_ERR
, "100|Address value underflow");
2302 * When encountering an address error, tell the user if there's no
2303 * underlying file, that's the real problem.
2305 if (sp
->ep
== NULL
) {
2306 ex_wemsg(sp
, cp
? cp
->name
: NULL
, EXM_NOFILEYET
);
2312 msgq(sp
, M_ERR
, "101|Illegal address combination");
2315 if (db_last(sp
, &lno
))
2319 "102|Illegal address: only %lu lines in the file",
2320 (unsigned long)lno
);
2325 msgq(sp
, M_ERR
, "103|Illegal address: the file is empty");
2331 msgq_wstr(sp
, M_ERR
, cp
->name
,
2332 "104|The %s command doesn't permit an address of 0");
2338 #if defined(DEBUG) && defined(COMLOG)
2348 vtrace(sp
, "ecmd: %s", ecp
->cmd
->name
);
2349 if (ecp
->addrcnt
> 0) {
2350 vtrace(sp
, " a1 %d", ecp
->addr1
.lno
);
2351 if (ecp
->addrcnt
> 1)
2352 vtrace(sp
, " a2: %d", ecp
->addr2
.lno
);
2355 vtrace(sp
, " line %d", ecp
->lineno
);
2357 vtrace(sp
, " flags 0x%x", ecp
->flags
);
2358 if (F_ISSET(&exc
, E_BUFFER
))
2359 vtrace(sp
, " buffer %c", ecp
->buffer
);
2361 for (cnt
= 0; cnt
< ecp
->argc
; ++cnt
)
2362 vtrace(sp
, " arg %d: {%s}", cnt
, ecp
->argv
[cnt
]->bp
);