2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char sccsid
[] = "$Id: ex.c,v 8.57 1993/11/22 18:33:24 bostic Exp $ (Berkeley) $Date: 1993/11/22 18:33:24 $";
12 #include <sys/types.h>
25 static void ep_comm
__P((SCR
*, char **, char **, int *, char **, int *));
26 static char *ep_line
__P((SCR
*, EXF
*, char *, MARK
*));
27 static char *ep_range
__P((SCR
*, EXF
*, char *, EXCMDARG
*));
28 static void ep_re
__P((SCR
*, char **, char **, int *));
29 static void ep_rw
__P((SCR
*, char **, char **, int *));
35 * Read an ex command and execute it.
45 char defcom
[sizeof(DEFCOM
)];
50 if (sp
->s_refresh(sp
, ep
))
54 /* Get the next command. */
55 switch (sp
->s_get(sp
, ep
, &sp
->tiq
, ':', TXT_CR
| TXT_PROMPT
)) {
59 F_SET(sp
, S_EXIT_FORCE
);
65 saved_mode
= F_ISSET(sp
, S_SCREENS
| S_MAJOR_CHANGE
);
66 tp
= sp
->tiq
.cqh_first
;
68 if (F_ISSET(sp
->gp
, G_ISFROMTTY
)) {
69 (void)fputc('\r', stdout
);
72 memmove(defcom
, DEFCOM
, sizeof(DEFCOM
));
73 (void)ex_cstring(sp
, ep
, defcom
, sizeof(DEFCOM
) - 1);
75 if (F_ISSET(sp
->gp
, G_ISFROMTTY
))
76 (void)fputc('\n', stdout
);
77 (void)ex_cstring(sp
, ep
, tp
->lb
, tp
->len
);
81 if (saved_mode
!= F_ISSET(sp
, S_SCREENS
| S_MAJOR_CHANGE
))
84 if (sp
->s_refresh(sp
, ep
)) {
89 ret
: return (ex_end(sp
) || eval
);
94 * Execute ex commands from a file.
97 ex_cfile(sp
, ep
, filename
)
106 if ((fd
= open(filename
, O_RDONLY
, 0)) < 0)
113 * We'd like to test if the file is too big to malloc. Since we don't
114 * know what size or type off_t's or size_t's are, what the largest
115 * unsigned integral type is, or what random insanity the local C
116 * compiler will perpetrate, doing the comparison in a portable way
117 * is flatly impossible. Hope that malloc fails if the file is too
120 if ((bp
= malloc((size_t)sb
.st_size
)) == NULL
)
123 len
= read(fd
, bp
, (int)sb
.st_size
);
124 if (len
== -1 || len
!= sb
.st_size
) {
125 if (len
!= sb
.st_size
)
129 bp
[sb
.st_size
] = '\0';
131 rval
= ex_cstring(sp
, ep
, bp
, len
);
134 * THE UNDERLYING EXF MAY HAVE CHANGED (but we don't care).
142 e1
: msgq(sp
, M_SYSERR
, filename
);
148 * Execute EX commands from a string.
151 ex_cstring(sp
, ep
, cmd
, len
)
162 * Ex goes through here for each vi :colon command and for each ex
163 * command, however, globally executed commands don't go through
164 * here, instead, they call ex_cmd directly. So, reset all of the
165 * interruptible flags now.
167 F_CLR(sp
, S_INTERRUPTED
| S_INTERRUPTIBLE
);
169 /* This is probably not necessary, but it's worth being safe. */
176 * Commands may be separated by newline or '|' characters, and may
177 * be escaped by literal next characters. The quote characters are
178 * stripped here since they are no longer useful.
180 * There are seven exceptions to this. The filter versions of read
181 * and write are delimited by newlines (the filter command can contain
182 * shell pipes) ex and edit take ex commands as arguments, and global,
183 * vglobal and substitute take RE's as arguments and want their first
184 * argument be specially delimited, not necessarily by '|' characters.
185 * See ep_comm(), ep_re() and ep_rw() below for details.
189 saved_mode
= F_ISSET(sp
, S_SCREENS
| S_MAJOR_CHANGE
);
190 for (p
= t
= cmd
;;) {
192 /* Skip leading whitespace, literals, newlines. */
193 for (; len
> 0; ++t
, --len
) {
195 if (isblank(ch
) || ch
== '|')
197 if (sp
->special
[ch
] == K_CR
||
198 sp
->special
[ch
] == K_NL
||
199 sp
->special
[ch
] == K_VLNEXT
)
205 * Skip leading address. The command starts with
206 * the first alphabetic character.
208 for (; len
> 0; ++p
, ++t
, --len
) {
211 sp
->special
[ch
] == K_CR
||
212 sp
->special
[ch
] == K_NL
)
216 /* Special case read/write, ex/edit, RE commands. */
218 strchr("egrvws", t
[0] == ':' ? t
[1] : t
[0]))
219 switch (t
[0] == ':' ? t
[1] : t
[0]) {
222 &p
, &t
, &len
, &arg1
, &arg1_len
);
226 ep_rw(sp
, &p
, &t
, &len
);
231 ep_re(sp
, &p
, &t
, &len
);
239 --len
; /* Characters remaining. */
242 * Historically, vi permitted ^V's to escape <newline>'s in
243 * the .exrc file. It was almost certainly a bug, but that's
244 * what bug-for-bug compatibility means, Grasshopper.
246 if (sp
->special
[ch
] == K_VLNEXT
&& len
> 0 &&
247 (t
[0] == '|' || sp
->special
[t
[0]] == K_NL
)) {
252 if (len
== 0 || ch
== '|' || sp
->special
[ch
] == K_NL
) {
253 if (len
== 0 && ch
!= '|' && sp
->special
[ch
] != K_NL
)
256 *p
= '\0'; /* XXX: 8BIT */
257 if (ex_cmd(sp
, ep
, cmd
, arg1_len
)) {
258 if (len
|| TERM_MORE(sp
->gp
->key
)) {
259 TERM_FLUSH(sp
->gp
->key
);
261 "Ex command failed, remaining command input discarded.");
268 F_ISSET(sp
, S_SCREENS
| S_MAJOR_CHANGE
))
276 * Only here if the mode of the underlying file changed, the user
277 * switched files or is exiting. There are two things that we may
278 * have to save. First, any "+cmd" field that ep_comm() set up will
279 * have to be saved for later. Also, there may be part of the
280 * current ex command which we haven't executed:
282 * :edit +25 file.c|s/abc/ABC/|1
286 * The historic vi just hung, of course; we handle by pushing the
287 * keys onto the SCR's tty buffer. If we're switching modes to
288 * vi, since the commands are intended as ex commands, add the extra
289 * characters to make it work.
291 * For the fun of it, if you want to see if a vi clone got the ex
292 * argument parsing right, try:
294 * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
296 * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
298 if (arg1
== NULL
&& len
== 0)
300 if (IN_VI_MODE(sp
) && term_push(sp
, sp
->gp
->tty
, "\n", 1))
303 if (term_push(sp
, sp
->gp
->tty
, t
, len
))
306 if (IN_VI_MODE(sp
) && len
!= 0 &&
307 term_push(sp
, sp
->gp
->tty
, "|", 1))
309 if (term_push(sp
, sp
->gp
->tty
, arg1
, arg1_len
))
312 if (IN_VI_MODE(sp
) && term_push(sp
, sp
->gp
->tty
, ":", 1)) {
313 err
: msgq(sp
, M_SYSERR
, "remaining command input discarded");
314 TERM_FLUSH(sp
->gp
->key
);
321 * Parse and execute an ex command.
324 ex_cmd(sp
, ep
, exc
, arg1_len
)
334 recno_t lcount
, lno
, num
;
337 int ch
, cmdlen
, esc
, flags
, uselastcmd
;
340 #if defined(DEBUG) && 0
341 TRACE(sp
, "ex: {%s}\n", exc
);
345 * Permit extra colons at the start of the line. Historically,
346 * ex/vi allowed a single extra one. It's simpler not to count.
347 * The stripping is done here because, historically, any command
348 * could have preceding colons, e.g. ":g/pattern/:p" worked.
350 for (; *exc
== ':'; ++exc
);
352 /* Ignore command lines that start with a double-quote. */
356 /* Skip whitespace. */
357 for (; isblank(*exc
); ++exc
);
359 /* Initialize the argument structure. */
360 memset(&cmd
, 0, sizeof(EXCMDARG
));
362 exp
->ex_argv
[0] = "";
363 exp
->ex_argv
[1] = NULL
;
365 cmd
.argv
= exp
->ex_argv
;
368 * Parse line specifiers if the command uses addresses.
369 * New command line position is returned, or NULL on error.
371 if ((exc
= ep_range(sp
, ep
, exc
, &cmd
)) == NULL
)
374 /* Skip whitespace. */
375 for (; isblank(*exc
); ++exc
);
378 * If no command, ex does the last specified of p, l, or #, and vi
379 * moves to the line. Otherwise, find out how long the command name
380 * is. There are a few commands that aren't alphabetic, but they
381 * are all single character commands.
383 #define SINGLE_CHAR_COMMANDS "!#&<=>@~"
385 if (strchr(SINGLE_CHAR_COMMANDS
, *exc
)) {
390 for (p
= exc
; isalpha(*exc
); ++exc
);
393 msgq(sp
, M_ERR
, "Unknown command name.");
397 for (cp
= cmds
; cp
->name
&& memcmp(p
, cp
->name
, cmdlen
); ++cp
);
401 * Historic vi permitted the mark to immediately follow the 'k'
402 * in the 'k' command. Make it work.
404 * Use of msgq below is safe, command names are all alphabetics.
406 if (cp
->name
== NULL
)
407 if (p
[0] == 'k' && p
[1] && !p
[2]) {
412 "The %.*s command is unknown.", cmdlen
, p
);
417 /* Some commands are turned off. */
418 if (F_ISSET(cp
, E_NOPERM
)) {
420 "The %.*s command is not currently supported.",
425 /* Some commands aren't okay in globals. */
426 if (F_ISSET(sp
, S_GLOBAL
) && F_ISSET(cp
, E_NOGLOBAL
)) {
428 "The %.*s command can't be used as part of a global command.", cmdlen
, p
);
433 * Multiple < and > characters; another "special" feature.
434 * NOTE: The string may not be nul terminated in this case.
436 if ((cp
== &cmds
[C_SHIFTL
] && *p
== '<') ||
437 (cp
== &cmds
[C_SHIFTR
] && *p
== '>')) {
439 exp
->ex_argv
[1] = NULL
;
441 cmd
.argv
= exp
->ex_argv
;
442 for (ch
= *p
, exc
= p
; *++exc
== ch
;);
446 * The visual command has a different syntax when called
447 * from ex than when called from a vi colon command. FMH.
449 if (cp
== &cmds
[C_VISUAL_EX
] && IN_VI_MODE(sp
))
450 cp
= &cmds
[C_VISUAL_VI
];
458 * File state must be checked throughout this code, because it is
459 * called when reading the .exrc file and similar things. There's
460 * this little chicken and egg problem -- if we read the file first,
461 * we won't know how to display it. If we read/set the exrc stuff
462 * first, we can't allow any command that requires file state.
463 * Historic vi generally took the easy way out and dropped core.
465 if (LF_ISSET(E_NORC
) && ep
== NULL
) {
467 "The %s command requires a file to already have been read in.",
473 * Set the default addresses. It's an error to specify an address for
474 * a command that doesn't take them. If two addresses are specified
475 * for a command that only takes one, lose the first one. Two special
476 * cases here, some commands take 0 or 2 addresses. For most of them
477 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
478 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
480 * Also, if the file is empty, some commands want to use an address of
481 * 0, i.e. the entire file is 0 to 0, and the default first address is
482 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
483 * Note, we also add the E_ZERO flag to the command flags, for the case
484 * where the 0 address is only valid if it's a default address.
486 * Also, set a flag if we set the default addresses. Some commands
487 * (ex: z) care if the user specified an address of if we just used
488 * the current cursor.
491 switch (flags
& (E_ADDR1
|E_ADDR2
|E_ADDR2_ALL
|E_ADDR2_NONE
)) {
492 case E_ADDR1
: /* One address: */
493 switch (cmd
.addrcnt
) {
494 case 0: /* Default cursor/empty file. */
496 F_SET(&cmd
, E_ADDRDEF
);
497 if (LF_ISSET(E_ZERODEF
)) {
498 if (file_lline(sp
, ep
, &lno
))
504 cmd
.addr1
.lno
= sp
->lno
;
506 cmd
.addr1
.lno
= sp
->lno
;
507 cmd
.addr1
.cno
= sp
->cno
;
511 case 2: /* Lose the first address. */
513 cmd
.addr1
= cmd
.addr2
;
516 case E_ADDR2_NONE
: /* Zero/two addresses: */
517 if (cmd
.addrcnt
== 0) /* Default to nothing. */
520 case E_ADDR2_ALL
: /* Zero/two addresses: */
521 if (cmd
.addrcnt
== 0) { /* Default entire/empty file. */
523 F_SET(&cmd
, E_ADDRDEF
);
524 if (file_lline(sp
, ep
, &cmd
.addr2
.lno
))
526 if (LF_ISSET(E_ZERODEF
) && cmd
.addr2
.lno
== 0) {
531 cmd
.addr1
.cno
= cmd
.addr2
.cno
= 0;
532 F_SET(&cmd
, E_ADDR2_ALL
);
536 case E_ADDR2
: /* Two addresses: */
537 two
: switch (cmd
.addrcnt
) {
538 case 0: /* Default cursor/empty file. */
540 F_SET(&cmd
, E_ADDRDEF
);
541 if (LF_ISSET(E_ZERODEF
) && sp
->lno
== 1) {
542 if (file_lline(sp
, ep
, &lno
))
545 cmd
.addr1
.lno
= cmd
.addr2
.lno
= 0;
548 cmd
.addr1
.lno
= cmd
.addr2
.lno
= sp
->lno
;
550 cmd
.addr1
.lno
= cmd
.addr2
.lno
= sp
->lno
;
551 cmd
.addr1
.cno
= cmd
.addr2
.cno
= sp
->cno
;
553 case 1: /* Default to first address. */
555 cmd
.addr2
= cmd
.addr1
;
562 if (cmd
.addrcnt
) /* Error. */
567 * YASC. The "set tags" command historically used a backslash, not
568 * the user's literal next character, to escape whitespace. Handle
569 * it here instead of complicating the argv_exp3() code. Note, this
570 * isn't a particularly complex trap, and if backslashes were legal
571 * in set commands, this would have to be much more complicated.
573 if (cp
== &cmds
[C_SET
]) {
574 esc
= sp
->gp
->original_termios
.c_cc
[VLNEXT
];
575 for (p
= exc
; (ch
= *p
) != '\0'; ++p
)
580 for (lcount
= 0, p
= cp
->syntax
; *p
!= '\0'; ++p
) {
582 * The write command is sensitive to leading whitespace,
583 * i.e. "write !" is different from "write!". If not write,
584 * skip leading whitespace.
586 if (cp
!= &cmds
[C_WRITE
])
587 for (; isblank(*exc
); ++exc
);
590 * When reach the end of the command, quit, unless it's
591 * a command that does its own parsing, in which case we
592 * want to build a reasonable argv for it.
594 if (*p
!= 's' && *p
!= 'S' && *exc
== '\0')
601 F_SET(&cmd
, E_FORCE
);
611 case '1': /* +, -, #, l, p */
614 * Historically, some flags were ignored depending
615 * on where they occurred in the command line. For
616 * example, in the command, ":3+++p--#", historic vi
617 * acted on the '#' flag, but ignored the '-' flags.
618 * It's unambiguous what the flags mean, so we just
619 * handle them regardless of the stupidity of their
631 F_SET(&cmd
, E_F_HASH
);
634 F_SET(&cmd
, E_F_LIST
);
637 F_SET(&cmd
, E_F_PRINT
);
643 case '2': /* -, ., +, ^ */
644 case '3': /* -, ., +, ^, = */
648 F_SET(&cmd
, E_F_DASH
);
651 F_SET(&cmd
, E_F_DOT
);
654 F_SET(&cmd
, E_F_PLUS
);
657 F_SET(&cmd
, E_F_CARAT
);
661 F_SET(&cmd
, E_F_EQUAL
);
669 case 'b': /* buffer */
671 F_SET(&cmd
, E_BUFFER
);
673 case 'C': /* count */
675 case 'c': /* count (address) */
676 if (!isdigit(*exc
) &&
677 (*p
!= 'n' || (*exc
!= '+' && *exc
!= '-')))
679 lcount
= strtol(exc
, &endp
, 10);
682 "Count may not be zero.");
687 * Count as address offsets occur in commands taking
688 * two addresses. Historic vi practice was to use
689 * the count as an offset from the *second* address.
691 * Set the count flag; some underlying commands (see
692 * join) do different things with counts than with
695 if (*p
== 'C' || *p
== 'n')
698 cmd
.addr1
= cmd
.addr2
;
699 cmd
.addr2
.lno
= cmd
.addr1
.lno
+ lcount
- 1;
701 F_SET(&cmd
, E_COUNT
);
704 if (argv_exp2(sp
, ep
, &cmd
, exc
, cp
== &cmds
[C_BANG
]))
708 endp
= ep_line(sp
, ep
, exc
, &cur
);
709 if (endp
== NULL
|| exc
== endp
) {
711 "%s: bad line specification", exc
);
714 cmd
.lineno
= cur
.lno
;
718 case 'S': /* string, file exp. */
719 if (argv_exp1(sp
, ep
, &cmd
, exc
, cp
== &cmds
[C_BANG
]))
722 case 's': /* string */
723 exp
->ex_argv
[0] = exc
;
724 exp
->ex_argv
[1] = NULL
;
726 cmd
.argv
= exp
->ex_argv
;
728 case 'W': /* word string */
732 * Literal next characters escape the following
733 * character. The quote characters are stripped
734 * here since they are no longer useful.
738 for (p
= t
= exc
; (ch
= *p
) != '\0'; *t
++ = ch
, ++p
)
739 if (sp
->special
[ch
] == K_VLNEXT
&& p
[1] != '\0')
741 else if (isblank(ch
))
745 exp
->ex_argv
[0] = exc
;
747 /* Delete leading whitespace. */
748 for (*t
++ = '\0'; (ch
= *++p
) != '\0' && isblank(ch
););
752 for (t
= p
; (ch
= *p
++) != '\0'; *t
++ = ch
)
753 if (sp
->special
[ch
] == K_VLNEXT
&& p
[0] != '\0')
756 exp
->ex_argv
[2] = NULL
;
758 cmd
.argv
= exp
->ex_argv
;
761 if (argv_exp3(sp
, ep
, &cmd
, exc
))
763 countchk
: if (*++p
!= 'N') { /* N */
765 * If a number is specified, must either be
766 * 0 or that number, if optional, and that
767 * number, if required.
770 if ((*++p
!= 'o' || cmd
.argc
!= 0) &&
777 "Internal syntax table error (%s).", cp
->name
);
782 * Shouldn't be anything left, and no more required fields.
783 * That means neither 'l' or 'r' in the syntax.
785 for (; *exc
&& isblank(*exc
); ++exc
);
786 if (*exc
|| strpbrk(p
, "lr")) {
787 usage
: msgq(sp
, M_ERR
, "Usage: %s.", cp
->usage
);
791 /* Verify that the addresses are legal. */
792 addr2
: switch (cmd
.addrcnt
) {
794 if (file_lline(sp
, ep
, &lcount
))
797 * Historic ex/vi permitted commands with counts to go past
798 * EOF. So, for example, if the file only had 5 lines, the
799 * ex command "1,6>" would fail, but the command ">300"
800 * would succeed. Since we don't want to have to make all
801 * of the underlying commands handle random line numbers,
804 if (cmd
.addr2
.lno
> lcount
)
805 if (F_ISSET(&cmd
, E_COUNT
))
806 cmd
.addr2
.lno
= lcount
;
809 msgq(sp
, M_ERR
, "The file is empty.");
812 "Only %lu line%s in the file",
813 lcount
, lcount
> 1 ? "s" : "");
820 * If it's a "default vi command", zero is okay. Historic
821 * vi allowed this, note, it's also the hack that allows
822 * "vi + nonexistent_file" to work.
824 if (num
== 0 && (!IN_VI_MODE(sp
) || uselastcmd
!= 1) &&
827 "The %s command doesn't permit an address of 0.",
831 if (file_lline(sp
, ep
, &lcount
))
835 msgq(sp
, M_ERR
, "The file is empty.");
837 msgq(sp
, M_ERR
, "Only %lu line%s in the file",
838 lcount
, lcount
> 1 ? "s" : "");
844 /* If doing a default command, vi just moves to the line. */
845 if (IN_VI_MODE(sp
) && uselastcmd
) {
846 switch (cmd
.addrcnt
) {
848 sp
->lno
= cmd
.addr2
.lno
? cmd
.addr2
.lno
: 1;
849 sp
->cno
= cmd
.addr2
.cno
;
852 sp
->lno
= cmd
.addr1
.lno
? cmd
.addr1
.lno
: 1;
853 sp
->cno
= cmd
.addr1
.cno
;
859 /* Reset "last" command. */
860 if (LF_ISSET(E_SETLAST
))
864 #if defined(DEBUG) && 0
868 TRACE(sp
, "ex_cmd: %s", cmd
.cmd
->name
);
869 if (cmd
.addrcnt
> 0) {
870 TRACE(sp
, "\taddr1 %d", cmd
.addr1
.lno
);
872 TRACE(sp
, " addr2: %d", cmd
.addr2
.lno
);
876 TRACE(sp
, "\tlineno %d", cmd
.lineno
);
878 TRACE(sp
, "\tflags %0x", cmd
.flags
);
879 if (F_ISSET(&cmd
, E_BUFFER
))
880 TRACE(sp
, "\tbuffer %c", cmd
.buffer
);
883 for (__cnt
= 0; __cnt
< cmd
.argc
; ++__cnt
)
884 TRACE(sp
, "\targ %d: {%s}", __cnt
, cmd
.argv
[__cnt
]);
889 /* Clear autoprint. */
890 F_CLR(sp
, S_AUTOPRINT
);
893 * If file state and not doing a global command, log the start of
896 if (ep
!= NULL
&& !F_ISSET(sp
, S_GLOBAL
))
897 (void)log_cursor(sp
, ep
);
899 /* Save the current mode. */
900 saved_mode
= F_ISSET(sp
, S_SCREENS
| S_MAJOR_CHANGE
);
902 /* Increment the command count if not called from vi. */
906 /* Do the command. */
907 if ((cp
->fn
)(sp
, ep
, &cmd
))
911 /* Make sure no function left the temporary space locked. */
912 if (F_ISSET(sp
->gp
, G_TMP_INUSE
)) {
913 msgq(sp
, M_ERR
, "Error: ex: temporary buffer not released.");
918 /* If the world changed, we're done. */
919 if (saved_mode
!= F_ISSET(sp
, S_SCREENS
| S_MAJOR_CHANGE
))
922 /* If just starting up, or not in ex mode, we're done. */
923 if (ep
== NULL
|| !IN_EX_MODE(sp
))
927 * The print commands have already handled the `print' flags.
928 * If so, clear them. Don't return, autoprint may still have
929 * stuff to print out.
931 if (LF_ISSET(E_F_PRCLEAR
))
932 F_CLR(&cmd
, E_F_HASH
| E_F_LIST
| E_F_PRINT
);
935 * If the command was successful, and there was an explicit flag to
936 * display the new cursor line, or we're in ex mode, autoprint is set,
937 * and a change was made, display the line.
941 if (sp
->lno
< -flagoff
) {
942 msgq(sp
, M_ERR
, "Flag offset before line 1.");
946 if (file_lline(sp
, ep
, &lno
))
948 if (sp
->lno
+ flagoff
> lno
) {
950 "Flag offset past end-of-file.");
957 if (F_ISSET(sp
, S_AUTOPRINT
) && O_ISSET(sp
, O_AUTOPRINT
))
960 LF_INIT(F_ISSET(&cmd
, E_F_HASH
| E_F_LIST
| E_F_PRINT
));
962 memset(&cmd
, 0, sizeof(EXCMDARG
));
964 cmd
.addr1
.lno
= cmd
.addr2
.lno
= sp
->lno
;
965 cmd
.addr1
.cno
= cmd
.addr2
.cno
= sp
->cno
;
969 cmd
.cmd
= &cmds
[C_HASH
];
970 ex_number(sp
, ep
, &cmd
);
973 cmd
.cmd
= &cmds
[C_LIST
];
974 ex_list(sp
, ep
, &cmd
);
977 cmd
.cmd
= &cmds
[C_PRINT
];
986 * ep_comm, ep_re, ep_rw --
988 * Historically, '|' characters in the first argument of the ex, edit,
989 * global, vglobal and substitute commands did not separate commands.
990 * And, in the filter cases for read and write, they did not delimit
991 * the command at all.
993 * For example, the following commands were legal:
995 * :edit +25|s/abc/ABC/ file.c
996 * :substitute s/|/PIPE/
997 * :read !spell % | columnate
999 * It's not quite as simple as it looks, however. The command:
1001 * :substitute s/a/b/|s/c/d|set
1003 * was also legal, i.e. the historic ex parser (and I use the word loosely,
1004 * since "parser" implies some regularity of syntax) delimited the RE's
1005 * based on its delimiter and not anything so vulgar as a command syntax.
1007 * The ep_comm(), ep_re(), and ep_rw routines make this work. They're passed
1008 * the state from ex_cstring(), and, if it's a special case, they parse the
1009 * first (or entire) argument and return the new state. For the +cmd field,
1010 * since we don't want to parse the line more than once, a pointer to, and the
1011 * length of, the first argument is returned to ex_cstring(), which passes it
1012 * to ex_cmd(). Barf-O-Rama.
1015 ep_comm(sp
, pp
, tp
, lenp
, arg1p
, arg1_lenp
)
1017 char **pp
, **tp
, **arg1p
;
1018 int *lenp
, *arg1_lenp
;
1023 /* Copy the state to local variables. */
1029 * Move to the next non-lower-case, alphabetic character. We can
1030 * do this fairly brutally because we know that the command names
1031 * are all lower-case alphabetics, and there has to be a '+' to
1032 * start the arguments. If there isn't one, we're done.
1039 for (; len
&& islower(*p
= *t
); ++p
, ++t
, --len
);
1044 * Make sure it's the ex or edit command. Note, 'e' has
1045 * to map to the edit command or the strncmp's aren't right.
1048 if (strncmp(cp
, "ex", cnt
) && strncmp(cp
, "edit", cnt
))
1052 * Move to the '+'. We don't check syntax here, if it's not
1053 * there, we're done.
1060 if (len
== 0 || *p
!= '+')
1066 * The historic implementation of this "feature" ignored any escape
1067 * characters so there was no way to put a space or newline into the
1068 * +cmd field. We do a simplistic job of handling it by moving to
1069 * the first whitespace character that isn't escaped by a literal next
1070 * character. The literal next quote characters are stripped here
1071 * since they are no longer useful.
1073 * Move to the first non-escaped space.
1075 for (cp
= p
; len
;) {
1078 if (sp
->special
[ch
] == K_VLNEXT
&& len
> 0) {
1082 } else if (isblank(ch
))
1086 /* Return information about the first argument. */
1088 *arg1_lenp
= (p
- cp
) - 1;
1090 /* Restore the state. */
1097 ep_re(sp
, pp
, tp
, lenp
)
1103 int ch
, cnt
, delim
, len
;
1105 /* Copy the state to local variables. */
1111 * Move to the next non-lower-case, alphabetic character. We can
1112 * do this fairly brutally because we know that the command names
1113 * are all lower-case alphabetics, and there has to be a delimiter
1114 * to start the arguments. If there isn't one, we're done.
1121 for (; len
; ++p
, ++t
, --len
) {
1130 * Make sure it's the substitute, global or vglobal command.
1131 * Note, 's', 'g and 'v' have to map to these commands or the
1132 * strncmp's aren't right.
1135 if (strncmp(cp
, "substitute", cnt
) &&
1136 strncmp(cp
, "global", cnt
) && strncmp(cp
, "vglobal", cnt
))
1140 * Move to the delimiter. (The first character; if it's an illegal
1141 * one, the RE code will catch it.)
1156 * Backslashes quote delimiter characters for regular expressions.
1157 * The backslashes are left here since they'll be needed by the RE
1160 * Move to the third (non-escaped) delimiter.
1162 for (cnt
= 2; len
&& cnt
;) {
1165 if (ch
== '\\' && len
> 0) {
1169 } else if (ch
== delim
)
1173 /* Move past the delimiter if it's possible. */
1180 /* Restore the state. */
1187 ep_rw(sp
, pp
, tp
, lenp
)
1195 /* Copy the state to local variables. */
1201 * Move to the next non-lower-case, alphabetic character. We can
1202 * do this fairly brutally because we know that the command names
1203 * are all lower-case alphabetics, and there has to be a delimiter
1204 * to start the arguments. If there isn't one, we're done.
1211 for (; len
; ++p
, ++t
, --len
) {
1220 * Make sure it's the read or write command. Note, 'r' and 'w'
1221 * have to map to these commands or the strncmp's aren't right.
1224 if (strncmp(cp
, "read", cnt
) && strncmp(cp
, "write", cnt
))
1228 * Move to the next character. If it's a '!', it's a filter
1229 * command we want to eat it all, otherwise, we're done.
1243 /* Restore the state. */
1251 * Get a line range for ex commands.
1254 ep_range(sp
, ep
, cmd
, cp
)
1260 MARK cur
, savecursor
;
1264 /* Percent character is all lines in the file. */
1267 if (file_lline(sp
, ep
, &cp
->addr2
.lno
))
1269 /* If an empty file, then the first line is 0, not 1. */
1270 if (cp
->addr2
.lno
== 0)
1272 cp
->addr1
.cno
= cp
->addr2
.cno
= 0;
1277 /* Parse comma or semi-colon delimited line specs. */
1278 for (savecursor_set
= 0, cp
->addrcnt
= 0;;)
1280 case ';': /* Semi-colon delimiter. */
1282 * Comma delimiters delimit; semi-colon delimiters
1283 * change the current address for the 2nd address
1284 * to be the first address. Trailing or multiple
1285 * delimiters are discarded.
1287 if (cp
->addrcnt
== 0)
1289 if (!savecursor_set
) {
1290 savecursor
.lno
= sp
->lno
;
1291 savecursor
.cno
= sp
->cno
;
1292 sp
->lno
= cp
->addr1
.lno
;
1293 sp
->cno
= cp
->addr1
.cno
;
1299 case ',': /* Comma delimiter. */
1303 if ((endp
= ep_line(sp
, ep
, cmd
, &cur
)) == NULL
)
1309 * Extra addresses are discarded, starting with
1312 switch (cp
->addrcnt
) {
1322 cp
->addr1
= cp
->addr2
;
1335 * This is probably not right for treatment of savecursor -- figure
1336 * out what the historical ex did for ";,;,;5p" or similar stupidity.
1338 done
: if (savecursor_set
) {
1339 sp
->lno
= savecursor
.lno
;
1340 sp
->cno
= savecursor
.cno
;
1342 if (cp
->addrcnt
== 2 &&
1343 (cp
->addr2
.lno
< cp
->addr1
.lno
||
1344 cp
->addr2
.lno
== cp
->addr1
.lno
&& cp
->addr2
.cno
< cp
->addr1
.cno
)) {
1346 "The second address is smaller than the first.");
1353 * Get a single line address specifier.
1356 ep_line(sp
, ep
, cmd
, cur
)
1369 case '$': /* Last line. */
1370 if (file_lline(sp
, ep
, &cur
->lno
))
1374 break; /* Absolute line number. */
1375 case '0': case '1': case '2': case '3': case '4':
1376 case '5': case '6': case '7': case '8': case '9':
1378 * The way the vi "previous context" mark worked was
1379 * that "non-relative" motions set it. While vi was
1380 * not completely consistent about this, ANY numeric
1381 * address was considered non-relative, and set the
1384 if (IN_VI_MODE(sp
)) {
1387 if (mark_set(sp
, ep
, ABSMARK1
, &m
, 1))
1390 cur
->lno
= strtol(cmd
, &endp
, 10);
1394 case '\'': /* Set mark. */
1395 if (cmd
[1] == '\0') {
1397 "No mark name; use 'a' to 'z'.");
1400 if ((mp
= mark_get(sp
, ep
, cmd
[1])) == NULL
)
1405 case '/': /* Search forward. */
1408 flags
= SEARCH_MSG
| SEARCH_PARSE
| SEARCH_SET
;
1409 if (f_search(sp
, ep
, &m
, &m
, cmd
, &endp
, &flags
))
1415 case '?': /* Search backward. */
1418 flags
= SEARCH_MSG
| SEARCH_PARSE
| SEARCH_SET
;
1419 if (b_search(sp
, ep
, &m
, &m
, cmd
, &endp
, &flags
))
1425 case '.': /* Current position. */
1428 case '+': /* Increment. */
1429 case '-': /* Decrement. */
1430 /* If an empty file, then '.' is 0, not 1. */
1432 if (file_lline(sp
, ep
, &cur
->lno
))
1445 * Evaluate any offset. Offsets are +/- any number, or,
1446 * any number of +/- signs, or any combination thereof.
1448 for (total
= 0; *cmd
== '-' || *cmd
== '+'; total
+= num
) {
1449 num
= *cmd
== '-' ? -1 : 1;
1450 if (isdigit(*++cmd
)) {
1451 num
*= strtol(cmd
, &endp
, 10);
1455 if (total
< 0 && -total
> cur
->lno
) {
1457 "Reference to a line number less than 0.");