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.48 1993/11/04 16:16:49 bostic Exp $ (Berkeley) $Date: 1993/11/04 16:16:49 $";
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.
44 char defcom
[sizeof(DEFCOM
)];
49 if (sp
->s_refresh(sp
, ep
))
52 /* Edited as it can be. */
53 F_SET(sp
->frp
, FR_EDITED
);
56 /* Get the next command. */
57 switch (sp
->s_get(sp
, ep
,
58 &sp
->txthdr
, ':', TXT_CR
| TXT_PROMPT
)) {
62 F_SET(sp
, S_EXIT_FORCE
);
70 if (F_ISSET(sp
->gp
, G_ISFROMTTY
)) {
71 (void)fputc('\r', stdout
);
74 memmove(defcom
, DEFCOM
, sizeof(DEFCOM
));
75 (void)ex_cstring(sp
, ep
, defcom
, sizeof(DEFCOM
) - 1);
77 if (F_ISSET(sp
->gp
, G_ISFROMTTY
))
78 (void)fputc('\n', stdout
);
79 (void)ex_cstring(sp
, ep
, tp
->lb
, tp
->len
);
83 if (!F_ISSET(sp
, S_MODE_EX
) || F_ISSET(sp
, S_MAJOR_CHANGE
))
86 if (sp
->s_refresh(sp
, ep
)) {
91 ret
: return (ex_end(sp
) || eval
);
96 * Execute ex commands from a file.
99 ex_cfile(sp
, ep
, filename
)
108 if ((fd
= open(filename
, O_RDONLY
, 0)) < 0)
115 * We'd like to test if the file is too big to malloc. Since we don't
116 * know what size or type off_t's or size_t's are, what the largest
117 * unsigned integral type is, or what random insanity the local C
118 * compiler will perpetrate, doing the comparison in a portable way
119 * is flatly impossible. Hope that malloc fails if the file is too
122 if ((bp
= malloc((size_t)sb
.st_size
)) == NULL
)
125 len
= read(fd
, bp
, (int)sb
.st_size
);
126 if (len
== -1 || len
!= sb
.st_size
) {
127 if (len
!= sb
.st_size
)
131 bp
[sb
.st_size
] = '\0';
133 rval
= ex_cstring(sp
, ep
, bp
, len
);
136 * THE UNDERLYING EXF MAY HAVE CHANGED (but we don't care).
144 e1
: msgq(sp
, M_ERR
, "%s: %s.", filename
, strerror(errno
));
150 * Execute EX commands from a string.
153 ex_cstring(sp
, ep
, cmd
, len
)
164 * Ex goes through here for each vi :colon command and for each ex
165 * command, however, globally executed commands don't go through
166 * here, instead, they call ex_cmd directly. So, reset all of the
167 * interruptible flags now.
169 F_CLR(sp
, S_INTERRUPTED
| S_INTERRUPTIBLE
);
171 /* This is probably not necessary, but it's worth being safe. */
178 * Commands may be separated by newline or '|' characters, and may
179 * be escaped by literal next characters. The quote characters are
180 * stripped here since they are no longer useful.
182 * There are seven exceptions to this. The filter versions of read
183 * and write are delimited by newlines (the filter command can contain
184 * shell pipes) ex and edit take ex commands as arguments, and global,
185 * vglobal and substitute take RE's as arguments and want their first
186 * argument be specially delimited, not necessarily by '|' characters.
187 * See ep_comm(), ep_re() and ep_rw() below for details.
191 saved_mode
= F_ISSET(sp
, S_MODE_EX
| S_MODE_VI
| S_MAJOR_CHANGE
);
192 for (p
= t
= cmd
;;) {
194 /* Skip leading whitespace. */
195 for (; len
> 0 && (isblank(t
[0]) || t
[0] == '|' ||
196 sp
->special
[t
[0]] == K_VLNEXT
); ++t
, --len
);
199 * Skip leading address. The command starts with
200 * the first alphabetic character.
202 for (; len
> 0; ++p
, ++t
, --len
) {
208 /* Special case read/write, ex/edit, RE commands. */
210 strchr("egrvws", t
[0] == ':' ? t
[1] : t
[0]))
211 switch (t
[0] == ':' ? t
[1] : t
[0]) {
214 &p
, &t
, &len
, &arg1
, &arg1_len
);
218 ep_rw(sp
, &p
, &t
, &len
);
223 ep_re(sp
, &p
, &t
, &len
);
231 --len
; /* Characters remaining. */
234 * Historically, vi permitted ^V's to escape <newline>'s in
235 * the .exrc file. It was almost certainly a bug, but that's
236 * what bug-for-bug compatibility means, Grasshopper.
238 if (sp
->special
[ch
] == K_VLNEXT
&& len
> 0 &&
239 (t
[0] == '|' || sp
->special
[t
[0]] == K_NL
)) {
244 if (len
== 0 || ch
== '|' || sp
->special
[ch
] == K_NL
) {
245 if (len
== 0 && ch
!= '|' && sp
->special
[ch
] != K_NL
)
248 *p
= '\0'; /* XXX: 8BIT */
249 if (ex_cmd(sp
, ep
, cmd
, arg1_len
)) {
250 if (len
|| TERM_KEY_MORE(sp
)) {
253 "Ex command failed, remaining command input discarded.");
260 F_ISSET(sp
, S_MODE_EX
| S_MODE_VI
| S_MAJOR_CHANGE
))
268 * Only here if the mode of the underlying file changed, the user
269 * switched files or is exiting. There are two things that we may
270 * have to save. First, any "+cmd" field that ep_comm() set up will
271 * have to be saved for later. Also, there may be part of the
272 * current ex command which we haven't executed:
274 * :edit +25 file.c|s/abc/ABC/|1
278 * The historic vi just hung, of course; we handle by pushing the
279 * keys onto the SCR's tty buffer. If we're switching modes to
280 * vi, since the commands are intended as ex commands, add the extra
281 * characters to make it work.
283 * For the fun of it, if you want to see if a vi clone got the ex
284 * argument parsing right, try:
286 * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
288 * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
290 if (arg1
== NULL
&& len
== 0)
292 if (F_ISSET(sp
, S_MODE_VI
) && term_push(sp
, sp
->gp
->tty
, "\n", 1))
295 if (term_push(sp
, sp
->gp
->tty
, t
, len
))
298 if (F_ISSET(sp
, S_MODE_VI
) && len
!= 0 &&
299 term_push(sp
, sp
->gp
->tty
, "|", 1))
301 if (term_push(sp
, sp
->gp
->tty
, arg1
, arg1_len
))
304 if (F_ISSET(sp
, S_MODE_VI
) && term_push(sp
, sp
->gp
->tty
, ":", 1)) {
305 err
: TERM_KEY_FLUSH(sp
);
306 msgq(sp
, M_ERR
, "Error: %s: remaining command input discarded",
314 * Parse and execute an ex command.
317 ex_cmd(sp
, ep
, exc
, arg1_len
)
326 recno_t lcount
, lno
, num
;
329 int ch
, cmdlen
, esc
, flags
, uselastcmd
;
332 #if defined(DEBUG) && 0
333 TRACE(sp
, "ex: {%s}\n", exc
);
336 * Permit a single extra colon at the start of the line, for
337 * historical reasons.
342 /* Ignore command lines that start with a double-quote. */
346 /* Skip whitespace. */
347 for (; isblank(*exc
); ++exc
);
349 /* Initialize the argument structure. */
350 memset(&cmd
, 0, sizeof(EXCMDARG
));
352 sp
->ex_argv
[1] = NULL
;
354 cmd
.argv
= sp
->ex_argv
;
357 * Parse line specifiers if the command uses addresses.
358 * New command line position is returned, or NULL on error.
360 if ((exc
= ep_range(sp
, ep
, exc
, &cmd
)) == NULL
)
363 /* Skip whitespace. */
364 for (; isblank(*exc
); ++exc
);
367 * If no command, ex does the last specified of p, l, or #, and vi
368 * moves to the line. Otherwise, find out how long the command name
369 * is. There are a few commands that aren't alphabetic, but they
370 * are all single character commands.
372 #define SINGLE_CHAR_COMMANDS "!#&<=>@~"
374 if (strchr(SINGLE_CHAR_COMMANDS
, *exc
)) {
379 for (p
= exc
; isalpha(*exc
); ++exc
);
382 msgq(sp
, M_ERR
, "Unknown command name.");
386 for (cp
= cmds
; cp
->name
&& memcmp(p
, cp
->name
, cmdlen
); ++cp
);
390 * Historic vi permitted the mark to immediately follow the 'k'
391 * in the 'k' command. Make it work.
393 * Use of msgq below is safe, command names are all alphabetics.
395 if (cp
->name
== NULL
)
396 if (p
[0] == 'k' && p
[1] && !p
[2]) {
401 "The %.*s command is unknown.", cmdlen
, p
);
406 /* Some commands are turned off. */
407 if (F_ISSET(cp
, E_NOPERM
)) {
409 "The %.*s command is not currently supported.",
414 /* Some commands aren't okay in globals. */
415 if (F_ISSET(sp
, S_GLOBAL
) && F_ISSET(cp
, E_NOGLOBAL
)) {
417 "The %.*s command can't be used as part of a global command.", cmdlen
, p
);
422 * Multiple < and > characters; another "special" feature.
423 * NOTE: The string may not be nul terminated in this case.
425 if ((cp
== &cmds
[C_SHIFTL
] && *p
== '<') ||
426 (cp
== &cmds
[C_SHIFTR
] && *p
== '>')) {
428 sp
->ex_argv
[1] = NULL
;
430 cmd
.argv
= sp
->ex_argv
;
431 for (ch
= *p
, exc
= p
; *++exc
== ch
;);
435 * The visual command has a different syntax when called
436 * from ex than when called from a vi colon command. FMH.
438 if (cp
== &cmds
[C_VISUAL_EX
] && F_ISSET(sp
, S_MODE_VI
))
439 cp
= &cmds
[C_VISUAL_VI
];
447 * File state must be checked throughout this code, because it is
448 * called when reading the .exrc file and similar things. There's
449 * this little chicken and egg problem -- if we read the file first,
450 * we won't know how to display it. If we read/set the exrc stuff
451 * first, we can't allow any command that requires file state.
452 * Historic vi generally took the easy way out and dropped core.
454 if (LF_ISSET(E_NORC
) && ep
== NULL
) {
456 "The %s command requires a file to already have been read in.",
462 * Set the default addresses. It's an error to specify an address for
463 * a command that doesn't take them. If two addresses are specified
464 * for a command that only takes one, lose the first one. Two special
465 * cases here, some commands take 0 or 2 addresses. For most of them
466 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
467 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
469 * Also, if the file is empty, some commands want to use an address of
470 * 0, i.e. the entire file is 0 to 0, and the default first address is
471 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
472 * Note, we also add the E_ZERO flag to the command flags, for the case
473 * where the 0 address is only valid if it's a default address.
475 * Also, set a flag if we set the default addresses. Some commands
476 * (ex: z) care if the user specified an address of if we just used
477 * the current cursor.
480 switch (flags
& (E_ADDR1
|E_ADDR2
|E_ADDR2_ALL
|E_ADDR2_NONE
)) {
481 case E_ADDR1
: /* One address: */
482 switch (cmd
.addrcnt
) {
483 case 0: /* Default cursor/empty file. */
485 F_SET(&cmd
, E_ADDRDEF
);
486 if (LF_ISSET(E_ZERODEF
)) {
487 if (file_lline(sp
, ep
, &lno
))
493 cmd
.addr1
.lno
= sp
->lno
;
495 cmd
.addr1
.lno
= sp
->lno
;
496 cmd
.addr1
.cno
= sp
->cno
;
500 case 2: /* Lose the first address. */
502 cmd
.addr1
= cmd
.addr2
;
505 case E_ADDR2_NONE
: /* Zero/two addresses: */
506 if (cmd
.addrcnt
== 0) /* Default to nothing. */
509 case E_ADDR2_ALL
: /* Zero/two addresses: */
510 if (cmd
.addrcnt
== 0) { /* Default entire/empty file. */
512 F_SET(&cmd
, E_ADDRDEF
);
513 if (file_lline(sp
, ep
, &cmd
.addr2
.lno
))
515 if (LF_ISSET(E_ZERODEF
) && cmd
.addr2
.lno
== 0) {
520 cmd
.addr1
.cno
= cmd
.addr2
.cno
= 0;
521 F_SET(&cmd
, E_ADDR2_ALL
);
525 case E_ADDR2
: /* Two addresses: */
526 two
: switch (cmd
.addrcnt
) {
527 case 0: /* Default cursor/empty file. */
529 F_SET(&cmd
, E_ADDRDEF
);
530 if (LF_ISSET(E_ZERODEF
) && sp
->lno
== 1) {
531 if (file_lline(sp
, ep
, &lno
))
534 cmd
.addr1
.lno
= cmd
.addr2
.lno
= 0;
537 cmd
.addr1
.lno
= cmd
.addr2
.lno
= sp
->lno
;
539 cmd
.addr1
.lno
= cmd
.addr2
.lno
= sp
->lno
;
540 cmd
.addr1
.cno
= cmd
.addr2
.cno
= sp
->cno
;
542 case 1: /* Default to first address. */
544 cmd
.addr2
= cmd
.addr1
;
551 if (cmd
.addrcnt
) /* Error. */
556 * YASC. The "set tags" command historically used a backslash, not
557 * the user's literal next character, to escape whitespace. Handle
558 * it here instead of complicating the argv_exp3() code. Note, this
559 * isn't a particularly complex trap, and if backslashes were legal
560 * in set commands, this would have to be much more complicated.
562 if (cp
== &cmds
[C_SET
]) {
563 esc
= sp
->gp
->original_termios
.c_cc
[VLNEXT
];
564 for (p
= exc
; (ch
= *p
) != '\0'; ++p
)
569 for (lcount
= 0, p
= cp
->syntax
; *p
!= '\0'; ++p
) {
571 * The write command is sensitive to leading whitespace,
572 * i.e. "write !" is different from "write!". If not write,
573 * skip leading whitespace.
575 if (cp
!= &cmds
[C_WRITE
])
576 for (; isblank(*exc
); ++exc
);
579 * When reach the end of the command, quit, unless it's
580 * a command that does its own parsing, in which case we
581 * want to build a reasonable argv for it.
583 if (*p
!= 's' && *p
!= 'S' && *exc
== '\0')
590 F_SET(&cmd
, E_FORCE
);
600 case '1': /* +, -, #, l, p */
603 * Historically, some flags were ignored depending
604 * on where they occurred in the command line. For
605 * example, in the command, ":3+++p--#", historic vi
606 * acted on the '#' flag, but ignored the '-' flags.
607 * It's unambiguous what the flags mean, so we just
608 * handle them regardless of the stupidity of their
620 F_SET(&cmd
, E_F_HASH
);
623 F_SET(&cmd
, E_F_LIST
);
626 F_SET(&cmd
, E_F_PRINT
);
632 case '2': /* -, ., +, ^ */
633 case '3': /* -, ., +, ^, = */
637 F_SET(&cmd
, E_F_DASH
);
640 F_SET(&cmd
, E_F_DOT
);
643 F_SET(&cmd
, E_F_PLUS
);
646 F_SET(&cmd
, E_F_CARAT
);
650 F_SET(&cmd
, E_F_EQUAL
);
658 case 'b': /* buffer */
660 F_SET(&cmd
, E_BUFFER
);
662 case 'C': /* count */
663 case 'c': /* count (address) */
666 lcount
= strtol(exc
, &endp
, 10);
669 "Count may not be zero.");
674 * Count as address offsets occur in commands taking
675 * two addresses. Historic vi practice was to use
676 * the count as an offset from the *second* address.
678 * Set the count flag; some underlying commands (see
679 * join) do different things with counts than with
685 cmd
.addr1
= cmd
.addr2
;
686 cmd
.addr2
.lno
= cmd
.addr1
.lno
+ lcount
- 1;
688 F_SET(&cmd
, E_COUNT
);
691 if (argv_exp2(sp
, ep
, &cmd
, exc
, cp
== &cmds
[C_BANG
]))
695 endp
= ep_line(sp
, ep
, exc
, &cur
);
696 if (endp
== NULL
|| exc
== endp
) {
698 "%s: bad line specification", exc
);
701 cmd
.lineno
= cur
.lno
;
705 case 'S': /* string, file exp. */
706 if (argv_exp1(sp
, ep
, &cmd
, exc
, cp
== &cmds
[C_BANG
]))
709 case 's': /* string */
710 sp
->ex_argv
[0] = exc
;
711 sp
->ex_argv
[1] = NULL
;
713 cmd
.argv
= sp
->ex_argv
;
715 case 'W': /* word string */
719 * Literal next characters escape the following
720 * character. The quote characters are stripped
721 * here since they are no longer useful.
725 for (p
= t
= exc
; (ch
= *p
) != '\0'; *t
++ = ch
, ++p
)
726 if (sp
->special
[ch
] == K_VLNEXT
&& p
[1] != '\0')
728 else if (isblank(ch
))
732 sp
->ex_argv
[0] = exc
;
734 /* Delete leading whitespace. */
735 for (*t
++ = '\0'; (ch
= *++p
) != '\0' && isblank(ch
););
739 for (t
= p
; (ch
= *p
++) != '\0'; *t
++ = ch
)
740 if (sp
->special
[ch
] == K_VLNEXT
&& p
[0] != '\0')
743 sp
->ex_argv
[2] = NULL
;
745 cmd
.argv
= sp
->ex_argv
;
748 if (argv_exp3(sp
, ep
, &cmd
, exc
))
750 countchk
: if (*++p
!= 'N') { /* N */
752 * If a number is specified, must either be
753 * 0 or that number, if optional, and that
754 * number, if required.
757 if ((*++p
!= 'o' || cmd
.argc
!= 0) &&
764 "Internal syntax table error (%s).", cp
->name
);
769 * Shouldn't be anything left, and no more required fields.
770 * That means neither 'l' or 'r' in the syntax.
772 for (; *exc
&& isblank(*exc
); ++exc
);
773 if (*exc
|| strpbrk(p
, "lr")) {
774 usage
: msgq(sp
, M_ERR
, "Usage: %s.", cp
->usage
);
778 /* Verify that the addresses are legal. */
779 addr2
: switch (cmd
.addrcnt
) {
781 if (file_lline(sp
, ep
, &lcount
))
784 * Historic ex/vi permitted commands with counts to go past
785 * EOF. So, for example, if the file only had 5 lines, the
786 * ex command "1,6>" would fail, but the command ">300"
787 * would succeed. Since we don't want to have to make all
788 * of the underlying commands handle random line numbers,
791 if (cmd
.addr2
.lno
> lcount
)
792 if (F_ISSET(&cmd
, E_COUNT
))
793 cmd
.addr2
.lno
= lcount
;
796 msgq(sp
, M_ERR
, "The file is empty.");
799 "Only %lu line%s in the file",
800 lcount
, lcount
> 1 ? "s" : "");
807 * If it's a "default vi command", zero is okay. Historic
808 * vi allowed this, note, it's also the hack that allows
809 * "vi + nonexistent_file" to work.
811 if (num
== 0 && (!F_ISSET(sp
, S_MODE_VI
) || uselastcmd
!= 1) &&
814 "The %s command doesn't permit an address of 0.",
818 if (file_lline(sp
, ep
, &lcount
))
822 msgq(sp
, M_ERR
, "The file is empty.");
824 msgq(sp
, M_ERR
, "Only %lu line%s in the file",
825 lcount
, lcount
> 1 ? "s" : "");
831 /* If doing a default command, vi just moves to the line. */
832 if (F_ISSET(sp
, S_MODE_VI
) && uselastcmd
) {
833 switch (cmd
.addrcnt
) {
835 sp
->lno
= cmd
.addr2
.lno
? cmd
.addr2
.lno
: 1;
836 sp
->cno
= cmd
.addr2
.cno
;
839 sp
->lno
= cmd
.addr1
.lno
? cmd
.addr1
.lno
: 1;
840 sp
->cno
= cmd
.addr1
.cno
;
846 /* Reset "last" command. */
847 if (LF_ISSET(E_SETLAST
))
851 #if defined(DEBUG) && 0
855 TRACE(sp
, "ex_cmd: %s", cmd
.cmd
->name
);
856 if (cmd
.addrcnt
> 0) {
857 TRACE(sp
, "\taddr1 %d", cmd
.addr1
.lno
);
859 TRACE(sp
, " addr2: %d", cmd
.addr2
.lno
);
863 TRACE(sp
, "\tlineno %d", cmd
.lineno
);
865 TRACE(sp
, "\tflags %0x", cmd
.flags
);
866 if (F_ISSET(&cmd
, E_BUFFER
))
867 TRACE(sp
, "\tbuffer %c", cmd
.buffer
);
870 for (__cnt
= 0; __cnt
< cmd
.argc
; ++__cnt
)
871 TRACE(sp
, "\targ %d: {%s}", __cnt
, cmd
.argv
[__cnt
]);
876 /* Clear autoprint. */
877 F_CLR(sp
, S_AUTOPRINT
);
880 * If file state and not doing a global command, log the start of
883 if (ep
!= NULL
&& !F_ISSET(sp
, S_GLOBAL
))
884 (void)log_cursor(sp
, ep
);
886 /* Save the current mode. */
887 saved_mode
= F_ISSET(sp
, S_MODE_EX
| S_MODE_VI
| S_MAJOR_CHANGE
);
889 /* Increment the command count if not called from vi. */
890 if (!F_ISSET(sp
, S_MODE_VI
))
893 /* Do the command. */
894 if ((cp
->fn
)(sp
, ep
, &cmd
))
898 /* Make sure no function left the temporary space locked. */
899 if (F_ISSET(sp
->gp
, G_TMP_INUSE
)) {
900 msgq(sp
, M_ERR
, "Error: ex: temporary buffer not released.");
905 /* If the world changed, we're done. */
906 if (saved_mode
!= F_ISSET(sp
, S_MODE_EX
| S_MODE_VI
| S_MAJOR_CHANGE
))
909 /* If just starting up, or not in ex mode, we're done. */
910 if (ep
== NULL
|| !F_ISSET(sp
, S_MODE_EX
))
914 * The print commands have already handled the `print' flags.
915 * If so, clear them. Don't return, autoprint may still have
916 * stuff to print out.
918 if (LF_ISSET(E_F_PRCLEAR
))
919 F_CLR(&cmd
, E_F_HASH
| E_F_LIST
| E_F_PRINT
);
922 * If the command was successful, and there was an explicit flag to
923 * display the new cursor line, or we're in ex mode, autoprint is set,
924 * and a change was made, display the line.
928 if (sp
->lno
< -flagoff
) {
929 msgq(sp
, M_ERR
, "Flag offset before line 1.");
933 if (file_lline(sp
, ep
, &lno
))
935 if (sp
->lno
+ flagoff
> lno
) {
937 "Flag offset past end-of-file.");
944 if (F_ISSET(sp
, S_AUTOPRINT
) && O_ISSET(sp
, O_AUTOPRINT
))
947 LF_INIT(F_ISSET(&cmd
, E_F_HASH
| E_F_LIST
| E_F_PRINT
));
949 memset(&cmd
, 0, sizeof(EXCMDARG
));
951 cmd
.addr1
.lno
= cmd
.addr2
.lno
= sp
->lno
;
952 cmd
.addr1
.cno
= cmd
.addr2
.cno
= sp
->cno
;
956 cmd
.cmd
= &cmds
[C_HASH
];
957 ex_number(sp
, ep
, &cmd
);
960 cmd
.cmd
= &cmds
[C_LIST
];
961 ex_list(sp
, ep
, &cmd
);
964 cmd
.cmd
= &cmds
[C_PRINT
];
973 * ep_comm, ep_re, ep_rw --
975 * Historically, '|' characters in the first argument of the ex, edit,
976 * global, vglobal and substitute commands did not separate commands.
977 * And, in the filter cases for read and write, they did not delimit
978 * the command at all.
980 * For example, the following commands were legal:
982 * :edit +25|s/abc/ABC/ file.c
983 * :substitute s/|/PIPE/
984 * :read !spell % | columnate
986 * It's not quite as simple as it looks, however. The command:
988 * :substitute s/a/b/|s/c/d|set
990 * was also legal, i.e. the historic ex parser (and I use the word loosely,
991 * since "parser" implies some regularity of syntax) delimited the RE's
992 * based on its delimiter and not anything so vulgar as a command syntax.
994 * The ep_comm(), ep_re(), and ep_rw routines make this work. They're passed
995 * the state from ex_cstring(), and, if it's a special case, they parse the
996 * first (or entire) argument and return the new state. For the +cmd field,
997 * since we don't want to parse the line more than once, a pointer to, and the
998 * length of, the first argument is returned to ex_cstring(), which passes it
999 * to ex_cmd(). Barf-O-Rama.
1002 ep_comm(sp
, pp
, tp
, lenp
, arg1p
, arg1_lenp
)
1004 char **pp
, **tp
, **arg1p
;
1005 int *lenp
, *arg1_lenp
;
1010 /* Copy the state to local variables. */
1016 * Move to the next non-lower-case, alphabetic character. We can
1017 * do this fairly brutally because we know that the command names
1018 * are all lower-case alphabetics, and there has to be a '+' to
1019 * start the arguments. If there isn't one, we're done.
1026 for (; len
&& islower(*p
= *t
); ++p
, ++t
, --len
);
1031 * Make sure it's the ex or edit command. Note, 'e' has
1032 * to map to the edit command or the strncmp's aren't right.
1035 if (strncmp(cp
, "ex", cnt
) && strncmp(cp
, "edit", cnt
))
1039 * Move to the '+'. We don't check syntax here, if it's not
1040 * there, we're done.
1047 if (len
== 0 || *p
!= '+')
1053 * The historic implementation of this "feature" ignored any escape
1054 * characters so there was no way to put a space or newline into the
1055 * +cmd field. We do a simplistic job of handling it by moving to
1056 * the first whitespace character that isn't escaped by a literal next
1057 * character. The literal next quote characters are stripped here
1058 * since they are no longer useful.
1060 * Move to the first non-escaped space.
1062 for (cp
= p
; len
;) {
1065 if (sp
->special
[ch
] == K_VLNEXT
&& len
> 0) {
1069 } else if (isblank(ch
))
1073 /* Return information about the first argument. */
1075 *arg1_lenp
= (p
- cp
) - 1;
1077 /* Restore the state. */
1084 ep_re(sp
, pp
, tp
, lenp
)
1090 int ch
, cnt
, delim
, len
;
1092 /* Copy the state to local variables. */
1098 * Move to the next non-lower-case, alphabetic character. We can
1099 * do this fairly brutally because we know that the command names
1100 * are all lower-case alphabetics, and there has to be a delimiter
1101 * to start the arguments. If there isn't one, we're done.
1108 for (; len
; ++p
, ++t
, --len
) {
1117 * Make sure it's the substitute, global or vglobal command.
1118 * Note, 's', 'g and 'v' have to map to these commands or the
1119 * strncmp's aren't right.
1122 if (strncmp(cp
, "substitute", cnt
) &&
1123 strncmp(cp
, "global", cnt
) && strncmp(cp
, "vglobal", cnt
))
1127 * Move to the delimiter. (The first character; if it's an illegal
1128 * one, the RE code will catch it.)
1143 * Backslashes quote delimiter characters for regular expressions.
1144 * The backslashes are left here since they'll be needed by the RE
1147 * Move to the third (non-escaped) delimiter.
1149 for (cnt
= 2; len
&& cnt
;) {
1152 if (ch
== '\\' && len
> 0) {
1156 } else if (ch
== delim
)
1160 /* Move past the delimiter if it's possible. */
1167 /* Restore the state. */
1174 ep_rw(sp
, pp
, tp
, lenp
)
1182 /* Copy the state to local variables. */
1188 * Move to the next non-lower-case, alphabetic character. We can
1189 * do this fairly brutally because we know that the command names
1190 * are all lower-case alphabetics, and there has to be a delimiter
1191 * to start the arguments. If there isn't one, we're done.
1198 for (; len
; ++p
, ++t
, --len
) {
1207 * Make sure it's the read or write command. Note, 'r' and 'w'
1208 * have to map to these commands or the strncmp's aren't right.
1211 if (strncmp(cp
, "read", cnt
) && strncmp(cp
, "write", cnt
))
1215 * Move to the next character. If it's a '!', it's a filter
1216 * command we want to eat it all, otherwise, we're done.
1230 /* Restore the state. */
1238 * Get a line range for ex commands.
1241 ep_range(sp
, ep
, cmd
, cp
)
1247 MARK cur
, savecursor
;
1251 /* Percent character is all lines in the file. */
1254 if (file_lline(sp
, ep
, &cp
->addr2
.lno
))
1256 /* If an empty file, then the first line is 0, not 1. */
1257 if (cp
->addr2
.lno
== 0)
1259 cp
->addr1
.cno
= cp
->addr2
.cno
= 0;
1264 /* Parse comma or semi-colon delimited line specs. */
1265 for (savecursor_set
= 0, cp
->addrcnt
= 0;;)
1267 case ';': /* Semi-colon delimiter. */
1269 * Comma delimiters delimit; semi-colon delimiters
1270 * change the current address for the 2nd address
1271 * to be the first address. Trailing or multiple
1272 * delimiters are discarded.
1274 if (cp
->addrcnt
== 0)
1276 if (!savecursor_set
) {
1277 savecursor
.lno
= sp
->lno
;
1278 savecursor
.cno
= sp
->cno
;
1279 sp
->lno
= cp
->addr1
.lno
;
1280 sp
->cno
= cp
->addr1
.cno
;
1286 case ',': /* Comma delimiter. */
1290 if ((endp
= ep_line(sp
, ep
, cmd
, &cur
)) == NULL
)
1296 * Extra addresses are discarded, starting with
1299 switch (cp
->addrcnt
) {
1309 cp
->addr1
= cp
->addr2
;
1322 * This is probably not right for treatment of savecursor -- figure
1323 * out what the historical ex did for ";,;,;5p" or similar stupidity.
1325 done
: if (savecursor_set
) {
1326 sp
->lno
= savecursor
.lno
;
1327 sp
->cno
= savecursor
.cno
;
1329 if (cp
->addrcnt
== 2 &&
1330 (cp
->addr2
.lno
< cp
->addr1
.lno
||
1331 cp
->addr2
.lno
== cp
->addr1
.lno
&& cp
->addr2
.cno
< cp
->addr1
.cno
)) {
1333 "The second address is smaller than the first.");
1340 * Get a single line address specifier.
1343 ep_line(sp
, ep
, cmd
, cur
)
1356 case '$': /* Last line. */
1357 if (file_lline(sp
, ep
, &cur
->lno
))
1361 break; /* Absolute line number. */
1362 case '0': case '1': case '2': case '3': case '4':
1363 case '5': case '6': case '7': case '8': case '9':
1365 * The way the vi "previous context" mark worked was
1366 * that "non-relative" motions set it. While vi was
1367 * not completely consistent about this, ANY numeric
1368 * address was considered non-relative, and set the
1371 if (F_ISSET(sp
, S_MODE_VI
)) {
1374 if (mark_set(sp
, ep
, ABSMARK1
, &m
, 1))
1377 cur
->lno
= strtol(cmd
, &endp
, 10);
1381 case '\'': /* Set mark. */
1382 if (cmd
[1] == '\0') {
1384 "No mark name; use 'a' to 'z'.");
1387 if ((mp
= mark_get(sp
, ep
, cmd
[1])) == NULL
)
1392 case '/': /* Search forward. */
1395 flags
= SEARCH_MSG
| SEARCH_PARSE
| SEARCH_SET
;
1396 if (f_search(sp
, ep
, &m
, &m
, cmd
, &endp
, &flags
))
1402 case '?': /* Search backward. */
1405 flags
= SEARCH_MSG
| SEARCH_PARSE
| SEARCH_SET
;
1406 if (b_search(sp
, ep
, &m
, &m
, cmd
, &endp
, &flags
))
1412 case '.': /* Current position. */
1415 case '+': /* Increment. */
1416 case '-': /* Decrement. */
1417 /* If an empty file, then '.' is 0, not 1. */
1419 if (file_lline(sp
, ep
, &cur
->lno
))
1432 * Evaluate any offset. Offsets are +/- any number, or,
1433 * any number of +/- signs, or any combination thereof.
1435 for (total
= 0; *cmd
== '-' || *cmd
== '+'; total
+= num
) {
1436 num
= *cmd
== '-' ? -1 : 1;
1437 if (isdigit(*++cmd
)) {
1438 num
*= strtol(cmd
, &endp
, 10);
1442 if (total
< 0 && -total
> cur
->lno
) {
1444 "Reference to a line number less than 0.");