I've added a 'description' field.
[nvi.git] / ex / ex.c
blob5c9da5ff46831108b2c1fea02a324e8b35ed5332
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex.c,v 10.60 1996/12/17 14:50:32 bostic Exp $ (Berkeley) $Date: 1996/12/17 14:50:32 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "../common/common.h"
32 #include "../vi/vi.h"
34 #if defined(DEBUG) && defined(COMLOG)
35 static void ex_comlog __P((SCR *, EXCMD *));
36 #endif
37 static EXCMDLIST const *
38 ex_comm_search __P((char *, size_t));
39 static int ex_discard __P((SCR *));
40 static int ex_line __P((SCR *, EXCMD *, MARK *, int *, int *));
41 static int ex_load __P((SCR *));
42 static void ex_unknown __P((SCR *, char *, size_t));
45 * ex --
46 * Main ex loop.
48 * PUBLIC: int ex __P((SCR **));
50 int
51 ex(spp)
52 SCR **spp;
54 EX_PRIVATE *exp;
55 GS *gp;
56 MSGS *mp;
57 SCR *sp;
58 TEXT *tp;
59 u_int32_t flags;
61 sp = *spp;
62 gp = sp->gp;
63 exp = EXP(sp);
65 /* Start the ex screen. */
66 if (ex_init(sp))
67 return (1);
69 /* Flush any saved messages. */
70 while ((mp = gp->msgq.lh_first) != NULL) {
71 gp->scr_msg(sp, mp->mtype, mp->buf, mp->len);
72 LIST_REMOVE(mp, q);
73 free(mp->buf);
74 free(mp);
77 /* If reading from a file, errors should have name and line info. */
78 if (F_ISSET(gp, G_SCRIPTED)) {
79 gp->excmd.if_lno = 1;
80 gp->excmd.if_name = "script";
84 * !!!
85 * Initialize the text flags. The beautify edit option historically
86 * applied to ex command input read from a file. In addition, the
87 * first time a ^H was discarded from the input, there was a message,
88 * "^H discarded", that was displayed. We don't bother.
90 LF_INIT(TXT_BACKSLASH | TXT_CNTRLD | TXT_CR);
91 for (;; ++gp->excmd.if_lno) {
92 /* Display status line and flush. */
93 if (F_ISSET(sp, SC_STATUS)) {
94 if (!F_ISSET(sp, SC_EX_SILENT))
95 msgq_status(sp, sp->lno, 0);
96 F_CLR(sp, SC_STATUS);
98 (void)ex_fflush(sp);
100 /* Set the flags the user can reset. */
101 if (O_ISSET(sp, O_BEAUTIFY))
102 LF_SET(TXT_BEAUTIFY);
103 if (O_ISSET(sp, O_PROMPT))
104 LF_SET(TXT_PROMPT);
106 /* Clear any current interrupts, and get a command. */
107 CLR_INTERRUPT(sp);
108 if (ex_txt(sp, &sp->tiq, ':', flags))
109 return (1);
110 if (INTERRUPTED(sp)) {
111 (void)ex_puts(sp, "\n");
112 (void)ex_fflush(sp);
113 continue;
116 /* Initialize the command structure. */
117 CLEAR_EX_PARSER(&gp->excmd);
120 * If the user entered a single carriage return, send
121 * ex_cmd() a separator -- it discards single newlines.
123 tp = sp->tiq.cqh_first;
124 if (tp->len == 0) {
125 gp->excmd.cp = " "; /* __TK__ why not |? */
126 gp->excmd.clen = 1;
127 } else {
128 gp->excmd.cp = tp->lb;
129 gp->excmd.clen = tp->len;
131 F_INIT(&gp->excmd, E_NRSEP);
133 if (ex_cmd(sp) && F_ISSET(gp, G_SCRIPTED))
134 return (1);
136 if (INTERRUPTED(sp)) {
137 CLR_INTERRUPT(sp);
138 msgq(sp, M_ERR, "170|Interrupted");
142 * If the last command caused a restart, or switched screens
143 * or into vi, return.
145 if (F_ISSET(gp, G_SRESTART) || F_ISSET(sp, SC_SSWITCH | SC_VI)) {
146 *spp = sp;
147 break;
150 /* If the last command switched files, we don't care. */
151 F_CLR(sp, SC_FSWITCH);
154 * If we're exiting this screen, move to the next one. By
155 * definition, this means returning into vi, so return to the
156 * main editor loop. The ordering is careful, don't discard
157 * the contents of sp until the end.
159 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
160 if (file_end(sp, NULL, F_ISSET(sp, SC_EXIT_FORCE)))
161 return (1);
162 *spp = screen_next(sp);
163 return (screen_end(sp));
166 return (0);
170 * ex_cmd --
171 * The guts of the ex parser: parse and execute a string containing
172 * ex commands.
174 * !!!
175 * This code MODIFIES the string that gets passed in, to delete quoting
176 * characters, etc. The string cannot be readonly/text space, nor should
177 * you expect to use it again after ex_cmd() returns.
179 * !!!
180 * For the fun of it, if you want to see if a vi clone got the ex argument
181 * parsing right, try:
183 * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
184 * vi
185 * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
187 * or: vi
188 * :set|file|append|set|file
190 * For extra credit, try them in a startup .exrc file.
192 * PUBLIC: int ex_cmd __P((SCR *));
195 ex_cmd(sp)
196 SCR *sp;
198 enum nresult nret;
199 EX_PRIVATE *exp;
200 EXCMD *ecp;
201 GS *gp;
202 MARK cur;
203 recno_t lno;
204 size_t arg1_len, discard, len;
205 u_int32_t flags;
206 long ltmp;
207 int at_found, gv_found;
208 int ch, cnt, delim, isaddr, namelen;
209 int newscreen, notempty, tmp, vi_address;
210 char *arg1, *p, *s, *t;
212 gp = sp->gp;
213 exp = EXP(sp);
216 * We always start running the command on the top of the stack.
217 * This means that *everything* must be resolved when we leave
218 * this function for any reason.
220 loop: ecp = gp->ecq.lh_first;
222 /* If we're reading a command from a file, set up error information. */
223 if (ecp->if_name != NULL) {
224 gp->if_lno = ecp->if_lno;
225 gp->if_name = ecp->if_name;
229 * If a move to the end of the file is scheduled for this command,
230 * do it now.
232 if (F_ISSET(ecp, E_MOVETOEND)) {
233 if (db_last(sp, &sp->lno))
234 goto rfail;
235 sp->cno = 0;
236 F_CLR(ecp, E_MOVETOEND);
239 /* If we found a newline, increment the count now. */
240 if (F_ISSET(ecp, E_NEWLINE)) {
241 ++gp->if_lno;
242 ++ecp->if_lno;
243 F_CLR(ecp, E_NEWLINE);
246 /* (Re)initialize the EXCMD structure, preserving some flags. */
247 CLEAR_EX_CMD(ecp);
249 /* Initialize the argument structures. */
250 if (argv_init(sp, ecp))
251 goto err;
253 /* Initialize +cmd, saved command information. */
254 arg1 = NULL;
255 ecp->save_cmdlen = 0;
257 /* Skip <blank>s, empty lines. */
258 for (notempty = 0; ecp->clen > 0; ++ecp->cp, --ecp->clen)
259 if ((ch = *ecp->cp) == '\n') {
260 ++gp->if_lno;
261 ++ecp->if_lno;
262 } else if (isblank(ch))
263 notempty = 1;
264 else
265 break;
268 * !!!
269 * Permit extra colons at the start of the line. Historically,
270 * ex/vi allowed a single extra one. It's simpler not to count.
271 * The stripping is done here because, historically, any command
272 * could have preceding colons, e.g. ":g/pattern/:p" worked.
274 if (ecp->clen != 0 && ch == ':') {
275 notempty = 1;
276 while (--ecp->clen > 0 && (ch = *++ecp->cp) == ':');
280 * Command lines that start with a double-quote are comments.
282 * !!!
283 * Historically, there was no escape or delimiter for a comment, e.g.
284 * :"foo|set was a single comment and nothing was output. Since nvi
285 * permits users to escape <newline> characters into command lines, we
286 * have to check for that case.
288 if (ecp->clen != 0 && ch == '"') {
289 while (--ecp->clen > 0 && *++ecp->cp != '\n');
290 if (*ecp->cp == '\n') {
291 F_SET(ecp, E_NEWLINE);
292 ++ecp->cp;
293 --ecp->clen;
295 goto loop;
298 /* Skip whitespace. */
299 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
300 ch = *ecp->cp;
301 if (!isblank(ch))
302 break;
306 * The last point at which an empty line can mean do nothing.
308 * !!!
309 * Historically, in ex mode, lines containing only <blank> characters
310 * were the same as a single <carriage-return>, i.e. a default command.
311 * In vi mode, they were ignored. In .exrc files this was a serious
312 * annoyance, as vi kept trying to treat them as print commands. We
313 * ignore backward compatibility in this case, discarding lines that
314 * contain only <blank> characters from .exrc files.
316 * !!!
317 * This is where you end up when you're done a command, i.e. clen has
318 * gone to zero. Continue if there are more commands to run.
320 if (ecp->clen == 0 &&
321 (!notempty || F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_BLIGNORE))) {
322 if (ex_load(sp))
323 goto rfail;
324 ecp = gp->ecq.lh_first;
325 if (ecp->clen == 0)
326 goto rsuccess;
327 goto loop;
331 * Check to see if this is a command for which we may want to move
332 * the cursor back up to the previous line. (The command :1<CR>
333 * wants a <newline> separator, but the command :<CR> wants to erase
334 * the command line.) If the line is empty except for <blank>s,
335 * <carriage-return> or <eof>, we'll probably want to move up. I
336 * don't think there's any way to get <blank> characters *after* the
337 * command character, but this is the ex parser, and I've been wrong
338 * before.
340 if (F_ISSET(ecp, E_NRSEP) &&
341 ecp->clen != 0 && (ecp->clen != 1 || ecp->cp[0] != '\004'))
342 F_CLR(ecp, E_NRSEP);
344 /* Parse command addresses. */
345 if (ex_range(sp, ecp, &tmp))
346 goto rfail;
347 if (tmp)
348 goto err;
351 * Skip <blank>s and any more colons (the command :3,5:print
352 * worked, historically).
354 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
355 ch = *ecp->cp;
356 if (!isblank(ch) && ch != ':')
357 break;
361 * If no command, ex does the last specified of p, l, or #, and vi
362 * moves to the line. Otherwise, determine the length of the command
363 * name by looking for the first non-alphabetic character. (There
364 * are a few non-alphabetic characters in command names, but they're
365 * all single character commands.) This isn't a great test, because
366 * it means that, for the command ":e +cut.c file", we'll report that
367 * the command "cut" wasn't known. However, it makes ":e+35 file" work
368 * correctly.
370 * !!!
371 * Historically, lines with multiple adjacent (or <blank> separated)
372 * command separators were very strange. For example, the command
373 * |||<carriage-return>, when the cursor was on line 1, displayed
374 * lines 2, 3 and 5 of the file. In addition, the command " | "
375 * would only display the line after the next line, instead of the
376 * next two lines. No ideas why. It worked reasonably when executed
377 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
378 * command for each separator.
380 #define SINGLE_CHAR_COMMANDS "\004!#&*<=>@~"
381 newscreen = 0;
382 if (ecp->clen != 0 && ecp->cp[0] != '|' && ecp->cp[0] != '\n') {
383 if (strchr(SINGLE_CHAR_COMMANDS, *ecp->cp)) {
384 p = ecp->cp;
385 ++ecp->cp;
386 --ecp->clen;
387 namelen = 1;
388 } else {
389 for (p = ecp->cp;
390 ecp->clen > 0; --ecp->clen, ++ecp->cp)
391 if (!isalpha(*ecp->cp))
392 break;
393 if ((namelen = ecp->cp - p) == 0) {
394 msgq(sp, M_ERR, "080|Unknown command name");
395 goto err;
400 * !!!
401 * Historic vi permitted flags to immediately follow any
402 * subset of the 'delete' command, but then did not permit
403 * further arguments (flag, buffer, count). Make it work.
404 * Permit further arguments for the few shreds of dignity
405 * it offers.
407 * Adding commands that start with 'd', and match "delete"
408 * up to a l, p, +, - or # character can break this code.
410 * !!!
411 * Capital letters beginning the command names ex, edit,
412 * next, previous, tag and visual (in vi mode) indicate the
413 * command should happen in a new screen.
415 switch (p[0]) {
416 case 'd':
417 for (s = p,
418 t = cmds[C_DELETE].name; *s == *t; ++s, ++t);
419 if (s[0] == 'l' || s[0] == 'p' || s[0] == '+' ||
420 s[0] == '-' || s[0] == '^' || s[0] == '#') {
421 len = (ecp->cp - p) - (s - p);
422 ecp->cp -= len;
423 ecp->clen += len;
424 ecp->rcmd = cmds[C_DELETE];
425 ecp->rcmd.syntax = "1bca1";
426 ecp->cmd = &ecp->rcmd;
427 goto skip_srch;
429 break;
430 case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
431 newscreen = 1;
432 p[0] = tolower(p[0]);
433 break;
437 * Search the table for the command.
439 * !!!
440 * Historic vi permitted the mark to immediately follow the
441 * 'k' in the 'k' command. Make it work.
443 * !!!
444 * Historic vi permitted any flag to follow the s command, e.g.
445 * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
446 * Since the following characters all have to be flags, i.e.
447 * alphabetics, we can let the s command routine return errors
448 * if it was some illegal command string. This code will break
449 * if an "sg" or similar command is ever added. The substitute
450 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
451 * follows the 's', but we limit the choices here to "cgr" so
452 * that we get unknown command messages for wrong combinations.
454 if ((ecp->cmd = ex_comm_search(p, namelen)) == NULL)
455 switch (p[0]) {
456 case 'k':
457 if (namelen == 2) {
458 ecp->cp -= namelen - 1;
459 ecp->clen += namelen - 1;
460 ecp->cmd = &cmds[C_K];
461 break;
463 goto unknown;
464 case 's':
465 for (s = p + 1, cnt = namelen; --cnt; ++s)
466 if (s[0] != 'c' &&
467 s[0] != 'g' && s[0] != 'r')
468 break;
469 if (cnt == 0) {
470 ecp->cp -= namelen - 1;
471 ecp->clen += namelen - 1;
472 ecp->rcmd = cmds[C_SUBSTITUTE];
473 ecp->rcmd.fn = ex_subagain;
474 ecp->cmd = &ecp->rcmd;
475 break;
477 /* FALLTHROUGH */
478 default:
479 unknown: if (newscreen)
480 p[0] = toupper(p[0]);
481 ex_unknown(sp, p, namelen);
482 goto err;
486 * The visual command has a different syntax when called
487 * from ex than when called from a vi colon command. FMH.
488 * Make the change now, before we test for the newscreen
489 * semantic, so that we're testing the right one.
491 skip_srch: if (ecp->cmd == &cmds[C_VISUAL_EX] && F_ISSET(sp, SC_VI))
492 ecp->cmd = &cmds[C_VISUAL_VI];
495 * !!!
496 * Historic vi permitted a capital 'P' at the beginning of
497 * any command that started with 'p'. Probably wanted the
498 * P[rint] command for backward compatibility, and the code
499 * just made Preserve and Put work by accident. Nvi uses
500 * Previous to mean previous-in-a-new-screen, so be careful.
502 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN) &&
503 (ecp->cmd == &cmds[C_PRINT] ||
504 ecp->cmd == &cmds[C_PRESERVE]))
505 newscreen = 0;
507 /* Test for a newscreen associated with this command. */
508 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN))
509 goto unknown;
511 /* Secure means no shell access. */
512 if (F_ISSET(ecp->cmd, E_SECURE) && O_ISSET(sp, O_SECURE)) {
513 ex_emsg(sp, ecp->cmd->name, EXM_SECURE);
514 goto err;
518 * Multiple < and > characters; another "feature". Note,
519 * The string passed to the underlying function may not be
520 * nul terminated in this case.
522 if ((ecp->cmd == &cmds[C_SHIFTL] && *p == '<') ||
523 (ecp->cmd == &cmds[C_SHIFTR] && *p == '>')) {
524 for (ch = *p;
525 ecp->clen > 0; --ecp->clen, ++ecp->cp)
526 if (*ecp->cp != ch)
527 break;
528 if (argv_exp0(sp, ecp, p, ecp->cp - p))
529 goto err;
532 /* Set the format style flags for the next command. */
533 if (ecp->cmd == &cmds[C_HASH])
534 exp->fdef = E_C_HASH;
535 else if (ecp->cmd == &cmds[C_LIST])
536 exp->fdef = E_C_LIST;
537 else if (ecp->cmd == &cmds[C_PRINT])
538 exp->fdef = E_C_PRINT;
539 F_CLR(ecp, E_USELASTCMD);
540 } else {
541 /* Print is the default command. */
542 ecp->cmd = &cmds[C_PRINT];
544 /* Set the saved format flags. */
545 F_SET(ecp, exp->fdef);
548 * !!!
549 * If no address was specified, and it's not a global command,
550 * we up the address by one. (I have no idea why globals are
551 * exempted, but it's (ahem) historic practice.)
553 if (ecp->addrcnt == 0 && !F_ISSET(sp, SC_EX_GLOBAL)) {
554 ecp->addrcnt = 1;
555 ecp->addr1.lno = sp->lno + 1;
556 ecp->addr1.cno = sp->cno;
559 F_SET(ecp, E_USELASTCMD);
563 * !!!
564 * Historically, the number option applied to both ex and vi. One
565 * strangeness was that ex didn't switch display formats until a
566 * command was entered, e.g. <CR>'s after the set didn't change to
567 * the new format, but :1p would.
569 if (O_ISSET(sp, O_NUMBER)) {
570 F_SET(ecp, E_OPTNUM);
571 FL_SET(ecp->iflags, E_C_HASH);
572 } else
573 F_CLR(ecp, E_OPTNUM);
575 /* Check for ex mode legality. */
576 if (F_ISSET(sp, SC_EX) && (F_ISSET(ecp->cmd, E_VIONLY) || newscreen)) {
577 msgq(sp, M_ERR,
578 "082|%s: command not available in ex mode", ecp->cmd->name);
579 goto err;
582 /* Add standard command flags. */
583 F_SET(ecp, ecp->cmd->flags);
584 if (!newscreen)
585 F_CLR(ecp, E_NEWSCREEN);
588 * There are three normal termination cases for an ex command. They
589 * are the end of the string (ecp->clen), or unescaped (by <literal
590 * next> characters) <newline> or '|' characters. As we're now past
591 * possible addresses, we can determine how long the command is, so we
592 * don't have to look for all the possible terminations. Naturally,
593 * there are some exciting special cases:
595 * 1: The bang, global, v and the filter versions of the read and
596 * write commands are delimited by <newline>s (they can contain
597 * shell pipes).
598 * 2: The ex, edit, next and visual in vi mode commands all take ex
599 * commands as their first arguments.
600 * 3: The s command takes an RE as its first argument, and wants it
601 * to be specially delimited.
603 * Historically, '|' characters in the first argument of the ex, edit,
604 * next, vi visual, and s commands didn't delimit the command. And,
605 * in the filter cases for read and write, and the bang, global and v
606 * commands, they did not delimit the command at all.
608 * For example, the following commands were legal:
610 * :edit +25|s/abc/ABC/ file.c
611 * :s/|/PIPE/
612 * :read !spell % | columnate
613 * :global/pattern/p|l
615 * It's not quite as simple as it sounds, however. The command:
617 * :s/a/b/|s/c/d|set
619 * was also legal, i.e. the historic ex parser (using the word loosely,
620 * since "parser" implies some regularity of syntax) delimited the RE's
621 * based on its delimiter and not anything so irretrievably vulgar as a
622 * command syntax.
624 * Anyhow, the following code makes this all work. First, for the
625 * special cases we move past their special argument(s). Then, we
626 * do normal command processing on whatever is left. Barf-O-Rama.
628 discard = 0; /* Characters discarded from the command. */
629 arg1_len = 0;
630 ecp->save_cmd = ecp->cp;
631 if (ecp->cmd == &cmds[C_EDIT] || ecp->cmd == &cmds[C_EX] ||
632 ecp->cmd == &cmds[C_NEXT] || ecp->cmd == &cmds[C_VISUAL_VI] ||
633 ecp->cmd == &cmds[C_VSPLIT]) {
635 * Move to the next non-whitespace character. A '!'
636 * immediately following the command is eaten as a
637 * force flag.
639 if (ecp->clen > 0 && *ecp->cp == '!') {
640 ++ecp->cp;
641 --ecp->clen;
642 FL_SET(ecp->iflags, E_C_FORCE);
644 /* Reset, don't reparse. */
645 ecp->save_cmd = ecp->cp;
647 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
648 if (!isblank(*ecp->cp))
649 break;
651 * QUOTING NOTE:
653 * The historic implementation ignored all escape characters
654 * so there was no way to put a space or newline into the +cmd
655 * field. We do a simplistic job of fixing it by moving to the
656 * first whitespace character that isn't escaped. The escaping
657 * characters are stripped as no longer useful.
659 if (ecp->clen > 0 && *ecp->cp == '+') {
660 ++ecp->cp;
661 --ecp->clen;
662 for (arg1 = p = ecp->cp;
663 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
664 ch = *ecp->cp;
665 if (IS_ESCAPE(sp, ecp, ch) &&
666 ecp->clen > 1) {
667 ++discard;
668 --ecp->clen;
669 ch = *++ecp->cp;
670 } else if (isblank(ch))
671 break;
672 *p++ = ch;
674 arg1_len = ecp->cp - arg1;
676 /* Reset, so the first argument isn't reparsed. */
677 ecp->save_cmd = ecp->cp;
679 } else if (ecp->cmd == &cmds[C_BANG] ||
680 ecp->cmd == &cmds[C_GLOBAL] || ecp->cmd == &cmds[C_V]) {
682 * QUOTING NOTE:
684 * We use backslashes to escape <newline> characters, although
685 * this wasn't historic practice for the bang command. It was
686 * for the global and v commands, and it's common usage when
687 * doing text insert during the command. Escaping characters
688 * are stripped as no longer useful.
690 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
691 ch = *ecp->cp;
692 if (ch == '\\' && ecp->clen > 1 && ecp->cp[1] == '\n') {
693 ++discard;
694 --ecp->clen;
695 ch = *++ecp->cp;
697 ++gp->if_lno;
698 ++ecp->if_lno;
699 } else if (ch == '\n')
700 break;
701 *p++ = ch;
703 } else if (ecp->cmd == &cmds[C_READ] || ecp->cmd == &cmds[C_WRITE]) {
705 * For write commands, if the next character is a <blank>, and
706 * the next non-blank character is a '!', it's a filter command
707 * and we want to eat everything up to the <newline>. For read
708 * commands, if the next non-blank character is a '!', it's a
709 * filter command and we want to eat everything up to the next
710 * <newline>. Otherwise, we're done.
712 for (tmp = 0; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
713 ch = *ecp->cp;
714 if (isblank(ch))
715 tmp = 1;
716 else
717 break;
719 if (ecp->clen > 0 && ch == '!' &&
720 (ecp->cmd == &cmds[C_READ] || tmp))
721 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
722 if (ecp->cp[0] == '\n')
723 break;
724 } else if (ecp->cmd == &cmds[C_SUBSTITUTE]) {
726 * Move to the next non-whitespace character, we'll use it as
727 * the delimiter. If the character isn't an alphanumeric or
728 * a '|', it's the delimiter, so parse it. Otherwise, we're
729 * into something like ":s g", so use the special s command.
731 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
732 if (!isblank(ecp->cp[0]))
733 break;
735 if (isalnum(ecp->cp[0]) || ecp->cp[0] == '|') {
736 ecp->rcmd = cmds[C_SUBSTITUTE];
737 ecp->rcmd.fn = ex_subagain;
738 ecp->cmd = &ecp->rcmd;
739 } else if (ecp->clen > 0) {
741 * QUOTING NOTE:
743 * Backslashes quote delimiter characters for RE's.
744 * The backslashes are NOT removed since they'll be
745 * used by the RE code. Move to the third delimiter
746 * that's not escaped (or the end of the command).
748 delim = *ecp->cp;
749 ++ecp->cp;
750 --ecp->clen;
751 for (cnt = 2; ecp->clen > 0 &&
752 cnt != 0; --ecp->clen, ++ecp->cp)
753 if (ecp->cp[0] == '\\' &&
754 ecp->clen > 1) {
755 ++ecp->cp;
756 --ecp->clen;
757 } else if (ecp->cp[0] == delim)
758 --cnt;
763 * Use normal quoting and termination rules to find the end of this
764 * command.
766 * QUOTING NOTE:
768 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
769 * file. It was almost certainly a bug, but that's what bug-for-bug
770 * compatibility means, Grasshopper. Also, ^V's escape the command
771 * delimiters. Literal next quote characters in front of the newlines,
772 * '|' characters or literal next characters are stripped as they're
773 * no longer useful.
775 vi_address = ecp->clen != 0 && ecp->cp[0] != '\n';
776 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
777 ch = ecp->cp[0];
778 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
779 tmp = ecp->cp[1];
780 if (tmp == '\n' || tmp == '|') {
781 if (tmp == '\n') {
782 ++gp->if_lno;
783 ++ecp->if_lno;
785 ++discard;
786 --ecp->clen;
787 ++ecp->cp;
788 ch = tmp;
790 } else if (ch == '\n' || ch == '|') {
791 if (ch == '\n')
792 F_SET(ecp, E_NEWLINE);
793 --ecp->clen;
794 break;
796 *p++ = ch;
800 * Save off the next command information, go back to the
801 * original start of the command.
803 p = ecp->cp + 1;
804 ecp->cp = ecp->save_cmd;
805 ecp->save_cmd = p;
806 ecp->save_cmdlen = ecp->clen;
807 ecp->clen = ((ecp->save_cmd - ecp->cp) - 1) - discard;
810 * QUOTING NOTE:
812 * The "set tags" command historically used a backslash, not the
813 * user's literal next character, to escape whitespace. Handle
814 * it here instead of complicating the argv_exp3() code. Note,
815 * this isn't a particularly complex trap, and if backslashes were
816 * legal in set commands, this would have to be much more complicated.
818 if (ecp->cmd == &cmds[C_SET])
819 for (p = ecp->cp, len = ecp->clen; len > 0; --len, ++p)
820 if (*p == '\\')
821 *p = CH_LITERAL;
824 * Set the default addresses. It's an error to specify an address for
825 * a command that doesn't take them. If two addresses are specified
826 * for a command that only takes one, lose the first one. Two special
827 * cases here, some commands take 0 or 2 addresses. For most of them
828 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
829 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
831 * Also, if the file is empty, some commands want to use an address of
832 * 0, i.e. the entire file is 0 to 0, and the default first address is
833 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
834 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
835 * case where the 0 address is only valid if it's a default address.
837 * Also, set a flag if we set the default addresses. Some commands
838 * (ex: z) care if the user specified an address or if we just used
839 * the current cursor.
841 switch (F_ISSET(ecp, E_ADDR1 | E_ADDR2 | E_ADDR2_ALL | E_ADDR2_NONE)) {
842 case E_ADDR1: /* One address: */
843 switch (ecp->addrcnt) {
844 case 0: /* Default cursor/empty file. */
845 ecp->addrcnt = 1;
846 F_SET(ecp, E_ADDR_DEF);
847 if (F_ISSET(ecp, E_ADDR_ZERODEF)) {
848 if (db_last(sp, &lno))
849 goto err;
850 if (lno == 0) {
851 ecp->addr1.lno = 0;
852 F_SET(ecp, E_ADDR_ZERO);
853 } else
854 ecp->addr1.lno = sp->lno;
855 } else
856 ecp->addr1.lno = sp->lno;
857 ecp->addr1.cno = sp->cno;
858 break;
859 case 1:
860 break;
861 case 2: /* Lose the first address. */
862 ecp->addrcnt = 1;
863 ecp->addr1 = ecp->addr2;
865 break;
866 case E_ADDR2_NONE: /* Zero/two addresses: */
867 if (ecp->addrcnt == 0) /* Default to nothing. */
868 break;
869 goto two_addr;
870 case E_ADDR2_ALL: /* Zero/two addresses: */
871 if (ecp->addrcnt == 0) { /* Default entire/empty file. */
872 F_SET(ecp, E_ADDR_DEF);
873 ecp->addrcnt = 2;
874 if (sp->ep == NULL)
875 ecp->addr2.lno = 0;
876 else if (db_last(sp, &ecp->addr2.lno))
877 goto err;
878 if (F_ISSET(ecp, E_ADDR_ZERODEF) &&
879 ecp->addr2.lno == 0) {
880 ecp->addr1.lno = 0;
881 F_SET(ecp, E_ADDR_ZERO);
882 } else
883 ecp->addr1.lno = 1;
884 ecp->addr1.cno = ecp->addr2.cno = 0;
885 F_SET(ecp, E_ADDR2_ALL);
886 break;
888 /* FALLTHROUGH */
889 case E_ADDR2: /* Two addresses: */
890 two_addr: switch (ecp->addrcnt) {
891 case 0: /* Default cursor/empty file. */
892 ecp->addrcnt = 2;
893 F_SET(ecp, E_ADDR_DEF);
894 if (sp->lno == 1 &&
895 F_ISSET(ecp, E_ADDR_ZERODEF)) {
896 if (db_last(sp, &lno))
897 goto err;
898 if (lno == 0) {
899 ecp->addr1.lno = ecp->addr2.lno = 0;
900 F_SET(ecp, E_ADDR_ZERO);
901 } else
902 ecp->addr1.lno =
903 ecp->addr2.lno = sp->lno;
904 } else
905 ecp->addr1.lno = ecp->addr2.lno = sp->lno;
906 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
907 break;
908 case 1: /* Default to first address. */
909 ecp->addrcnt = 2;
910 ecp->addr2 = ecp->addr1;
911 break;
912 case 2:
913 break;
915 break;
916 default:
917 if (ecp->addrcnt) /* Error. */
918 goto usage;
922 * !!!
923 * The ^D scroll command historically scrolled the value of the scroll
924 * option or to EOF. It was an error if the cursor was already at EOF.
925 * (Leading addresses were permitted, but were then ignored.)
927 if (ecp->cmd == &cmds[C_SCROLL]) {
928 ecp->addrcnt = 2;
929 ecp->addr1.lno = sp->lno + 1;
930 ecp->addr2.lno = sp->lno + O_VAL(sp, O_SCROLL);
931 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
932 if (db_last(sp, &lno))
933 goto err;
934 if (lno != 0 && lno > sp->lno && ecp->addr2.lno > lno)
935 ecp->addr2.lno = lno;
938 ecp->flagoff = 0;
939 for (p = ecp->cmd->syntax; *p != '\0'; ++p) {
941 * The force flag is sensitive to leading whitespace, i.e.
942 * "next !" is different from "next!". Handle it before
943 * skipping leading <blank>s.
945 if (*p == '!') {
946 if (ecp->clen > 0 && *ecp->cp == '!') {
947 ++ecp->cp;
948 --ecp->clen;
949 FL_SET(ecp->iflags, E_C_FORCE);
951 continue;
954 /* Skip leading <blank>s. */
955 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
956 if (!isblank(*ecp->cp))
957 break;
958 if (ecp->clen == 0)
959 break;
961 switch (*p) {
962 case '1': /* +, -, #, l, p */
964 * !!!
965 * Historically, some flags were ignored depending
966 * on where they occurred in the command line. For
967 * example, in the command, ":3+++p--#", historic vi
968 * acted on the '#' flag, but ignored the '-' flags.
969 * It's unambiguous what the flags mean, so we just
970 * handle them regardless of the stupidity of their
971 * location.
973 for (; ecp->clen; --ecp->clen, ++ecp->cp)
974 switch (*ecp->cp) {
975 case '+':
976 ++ecp->flagoff;
977 break;
978 case '-':
979 case '^':
980 --ecp->flagoff;
981 break;
982 case '#':
983 F_CLR(ecp, E_OPTNUM);
984 FL_SET(ecp->iflags, E_C_HASH);
985 exp->fdef |= E_C_HASH;
986 break;
987 case 'l':
988 FL_SET(ecp->iflags, E_C_LIST);
989 exp->fdef |= E_C_LIST;
990 break;
991 case 'p':
992 FL_SET(ecp->iflags, E_C_PRINT);
993 exp->fdef |= E_C_PRINT;
994 break;
995 default:
996 goto end_case1;
998 end_case1: break;
999 case '2': /* -, ., +, ^ */
1000 case '3': /* -, ., +, ^, = */
1001 for (; ecp->clen; --ecp->clen, ++ecp->cp)
1002 switch (*ecp->cp) {
1003 case '-':
1004 FL_SET(ecp->iflags, E_C_DASH);
1005 break;
1006 case '.':
1007 FL_SET(ecp->iflags, E_C_DOT);
1008 break;
1009 case '+':
1010 FL_SET(ecp->iflags, E_C_PLUS);
1011 break;
1012 case '^':
1013 FL_SET(ecp->iflags, E_C_CARAT);
1014 break;
1015 case '=':
1016 if (*p == '3') {
1017 FL_SET(ecp->iflags, E_C_EQUAL);
1018 break;
1020 /* FALLTHROUGH */
1021 default:
1022 goto end_case23;
1024 end_case23: break;
1025 case 'b': /* buffer */
1027 * !!!
1028 * Historically, "d #" was a delete with a flag, not a
1029 * delete into the '#' buffer. If the current command
1030 * permits a flag, don't use one as a buffer. However,
1031 * the 'l' and 'p' flags were legal buffer names in the
1032 * historic ex, and were used as buffers, not flags.
1034 if ((ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1035 ecp->cp[0] == '^' || ecp->cp[0] == '#') &&
1036 strchr(p, '1') != NULL)
1037 break;
1039 * !!!
1040 * Digits can't be buffer names in ex commands, or the
1041 * command "d2" would be a delete into buffer '2', and
1042 * not a two-line deletion.
1044 if (!isdigit(ecp->cp[0])) {
1045 ecp->buffer = *ecp->cp;
1046 ++ecp->cp;
1047 --ecp->clen;
1048 FL_SET(ecp->iflags, E_C_BUFFER);
1050 break;
1051 case 'c': /* count [01+a] */
1052 ++p;
1053 /* Validate any signed value. */
1054 if (!isdigit(*ecp->cp) && (*p != '+' ||
1055 (*ecp->cp != '+' && *ecp->cp != '-')))
1056 break;
1057 /* If a signed value, set appropriate flags. */
1058 if (*ecp->cp == '-')
1059 FL_SET(ecp->iflags, E_C_COUNT_NEG);
1060 else if (*ecp->cp == '+')
1061 FL_SET(ecp->iflags, E_C_COUNT_POS);
1062 if ((nret =
1063 nget_slong(&ltmp, ecp->cp, &t, 10)) != NUM_OK) {
1064 ex_badaddr(sp, NULL, A_NOTSET, nret);
1065 goto err;
1067 if (ltmp == 0 && *p != '0') {
1068 msgq(sp, M_ERR, "083|Count may not be zero");
1069 goto err;
1071 ecp->clen -= (t - ecp->cp);
1072 ecp->cp = t;
1075 * Counts as address offsets occur in commands taking
1076 * two addresses. Historic vi practice was to use
1077 * the count as an offset from the *second* address.
1079 * Set a count flag; some underlying commands (see
1080 * join) do different things with counts than with
1081 * line addresses.
1083 if (*p == 'a') {
1084 ecp->addr1 = ecp->addr2;
1085 ecp->addr2.lno = ecp->addr1.lno + ltmp - 1;
1086 } else
1087 ecp->count = ltmp;
1088 FL_SET(ecp->iflags, E_C_COUNT);
1089 break;
1090 case 'f': /* file */
1091 if (argv_exp2(sp, ecp, ecp->cp, ecp->clen))
1092 goto err;
1093 goto arg_cnt_chk;
1094 case 'l': /* line */
1096 * Get a line specification.
1098 * If the line was a search expression, we may have
1099 * changed state during the call, and we're now
1100 * searching the file. Push ourselves onto the state
1101 * stack.
1103 if (ex_line(sp, ecp, &cur, &isaddr, &tmp))
1104 goto rfail;
1105 if (tmp)
1106 goto err;
1108 /* Line specifications are always required. */
1109 if (!isaddr) {
1110 msgq_str(sp, M_ERR, ecp->cp,
1111 "084|%s: bad line specification");
1112 goto err;
1115 * The target line should exist for these commands,
1116 * but 0 is legal for them as well.
1118 if (cur.lno != 0 && !db_exist(sp, cur.lno)) {
1119 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1120 goto err;
1122 ecp->lineno = cur.lno;
1123 break;
1124 case 'S': /* string, file exp. */
1125 if (ecp->clen != 0) {
1126 if (argv_exp1(sp, ecp, ecp->cp,
1127 ecp->clen, ecp->cmd == &cmds[C_BANG]))
1128 goto err;
1129 goto addr_verify;
1131 /* FALLTHROUGH */
1132 case 's': /* string */
1133 if (argv_exp0(sp, ecp, ecp->cp, ecp->clen))
1134 goto err;
1135 goto addr_verify;
1136 case 'W': /* word string */
1138 * QUOTING NOTE:
1140 * Literal next characters escape the following
1141 * character. Quoting characters are stripped here
1142 * since they are no longer useful.
1144 * First there was the word.
1146 for (p = t = ecp->cp;
1147 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
1148 ch = *ecp->cp;
1149 if (IS_ESCAPE(sp,
1150 ecp, ch) && ecp->clen > 1) {
1151 --ecp->clen;
1152 *p++ = *++ecp->cp;
1153 } else if (isblank(ch)) {
1154 ++ecp->cp;
1155 --ecp->clen;
1156 break;
1157 } else
1158 *p++ = ch;
1160 if (argv_exp0(sp, ecp, t, p - t))
1161 goto err;
1163 /* Delete intervening whitespace. */
1164 for (; ecp->clen > 0;
1165 --ecp->clen, ++ecp->cp) {
1166 ch = *ecp->cp;
1167 if (!isblank(ch))
1168 break;
1170 if (ecp->clen == 0)
1171 goto usage;
1173 /* Followed by the string. */
1174 for (p = t = ecp->cp; ecp->clen > 0;
1175 --ecp->clen, ++ecp->cp, ++p) {
1176 ch = *ecp->cp;
1177 if (IS_ESCAPE(sp,
1178 ecp, ch) && ecp->clen > 1) {
1179 --ecp->clen;
1180 *p = *++ecp->cp;
1181 } else
1182 *p = ch;
1184 if (argv_exp0(sp, ecp, t, p - t))
1185 goto err;
1186 goto addr_verify;
1187 case 'w': /* word */
1188 if (argv_exp3(sp, ecp, ecp->cp, ecp->clen))
1189 goto err;
1190 arg_cnt_chk: if (*++p != 'N') { /* N */
1192 * If a number is specified, must either be
1193 * 0 or that number, if optional, and that
1194 * number, if required.
1196 tmp = *p - '0';
1197 if ((*++p != 'o' || exp->argsoff != 0) &&
1198 exp->argsoff != tmp)
1199 goto usage;
1201 goto addr_verify;
1202 default:
1203 msgq(sp, M_ERR,
1204 "085|Internal syntax table error (%s: %s)",
1205 ecp->cmd->name, KEY_NAME(sp, *p));
1209 /* Skip trailing whitespace. */
1210 for (; ecp->clen > 0; --ecp->clen) {
1211 ch = *ecp->cp++;
1212 if (!isblank(ch))
1213 break;
1217 * There shouldn't be anything left, and no more required fields,
1218 * i.e neither 'l' or 'r' in the syntax string.
1220 if (ecp->clen != 0 || strpbrk(p, "lr")) {
1221 usage: msgq(sp, M_ERR, "086|Usage: %s", ecp->cmd->usage);
1222 goto err;
1226 * Verify that the addresses are legal. Check the addresses here,
1227 * because this is a place where all ex addresses pass through.
1228 * (They don't all pass through ex_line(), for instance.) We're
1229 * assuming that any non-existent line doesn't exist because it's
1230 * past the end-of-file. That's a pretty good guess.
1232 * If it's a "default vi command", an address of zero is okay.
1234 addr_verify:
1235 switch (ecp->addrcnt) {
1236 case 2:
1238 * Historic ex/vi permitted commands with counts to go past
1239 * EOF. So, for example, if the file only had 5 lines, the
1240 * ex command "1,6>" would fail, but the command ">300"
1241 * would succeed. Since we don't want to have to make all
1242 * of the underlying commands handle random line numbers,
1243 * fix it here.
1245 if (ecp->addr2.lno == 0) {
1246 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1247 (F_ISSET(sp, SC_EX) ||
1248 !F_ISSET(ecp, E_USELASTCMD))) {
1249 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1250 goto err;
1252 } else if (!db_exist(sp, ecp->addr2.lno))
1253 if (FL_ISSET(ecp->iflags, E_C_COUNT)) {
1254 if (db_last(sp, &lno))
1255 goto err;
1256 ecp->addr2.lno = lno;
1257 } else {
1258 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1259 goto err;
1261 /* FALLTHROUGH */
1262 case 1:
1263 if (ecp->addr1.lno == 0) {
1264 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1265 (F_ISSET(sp, SC_EX) ||
1266 !F_ISSET(ecp, E_USELASTCMD))) {
1267 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1268 goto err;
1270 } else if (!db_exist(sp, ecp->addr1.lno)) {
1271 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1272 goto err;
1274 break;
1278 * If doing a default command and there's nothing left on the line,
1279 * vi just moves to the line. For example, ":3" and ":'a,'b" just
1280 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1282 * !!!
1283 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1284 * the line.
1286 * !!!
1287 * This is done before the absolute mark gets set; historically,
1288 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1290 if ((F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_NOPRDEF)) &&
1291 F_ISSET(ecp, E_USELASTCMD) && vi_address == 0) {
1292 switch (ecp->addrcnt) {
1293 case 2:
1294 if (sp->lno !=
1295 (ecp->addr2.lno ? ecp->addr2.lno : 1)) {
1296 sp->lno =
1297 ecp->addr2.lno ? ecp->addr2.lno : 1;
1298 sp->cno = 0;
1299 (void)nonblank(sp, sp->lno, &sp->cno);
1301 break;
1302 case 1:
1303 if (sp->lno !=
1304 (ecp->addr1.lno ? ecp->addr1.lno : 1)) {
1305 sp->lno =
1306 ecp->addr1.lno ? ecp->addr1.lno : 1;
1307 sp->cno = 0;
1308 (void)nonblank(sp, sp->lno, &sp->cno);
1310 break;
1312 ecp->cp = ecp->save_cmd;
1313 ecp->clen = ecp->save_cmdlen;
1314 goto loop;
1318 * Set the absolute mark -- we have to set it for vi here, in case
1319 * it's a compound command, e.g. ":5p|6" should set the absolute
1320 * mark for vi.
1322 if (F_ISSET(ecp, E_ABSMARK)) {
1323 cur.lno = sp->lno;
1324 cur.cno = sp->cno;
1325 F_CLR(ecp, E_ABSMARK);
1326 if (mark_set(sp, ABSMARK1, &cur, 1))
1327 goto err;
1330 #if defined(DEBUG) && defined(COMLOG)
1331 ex_comlog(sp, ecp);
1332 #endif
1333 /* Increment the command count if not called from vi. */
1334 if (F_ISSET(sp, SC_EX))
1335 ++sp->ccnt;
1338 * If file state available, and not doing a global command,
1339 * log the start of an action.
1341 if (sp->ep != NULL && !F_ISSET(sp, SC_EX_GLOBAL))
1342 (void)log_cursor(sp);
1345 * !!!
1346 * There are two special commands for the purposes of this code: the
1347 * default command (<carriage-return>) or the scrolling commands (^D
1348 * and <EOF>) as the first non-<blank> characters in the line.
1350 * If this is the first command in the command line, we received the
1351 * command from the ex command loop and we're talking to a tty, and
1352 * and there's nothing else on the command line, and it's one of the
1353 * special commands, we move back up to the previous line, and erase
1354 * the prompt character with the output. Since ex runs in canonical
1355 * mode, we don't have to do anything else, a <newline> has already
1356 * been echoed by the tty driver. It's OK if vi calls us -- we won't
1357 * be in ex mode so we'll do nothing.
1359 if (F_ISSET(ecp, E_NRSEP)) {
1360 if (sp->ep != NULL &&
1361 F_ISSET(sp, SC_EX) && !F_ISSET(gp, G_SCRIPTED) &&
1362 (F_ISSET(ecp, E_USELASTCMD) || ecp->cmd == &cmds[C_SCROLL]))
1363 gp->scr_ex_adjust(sp, EX_TERM_SCROLL);
1364 F_CLR(ecp, E_NRSEP);
1368 * Call the underlying function for the ex command.
1370 * XXX
1371 * Interrupts behave like errors, for now.
1373 if (ecp->cmd->fn(sp, ecp) || INTERRUPTED(sp)) {
1374 if (F_ISSET(gp, G_SCRIPTED))
1375 F_SET(sp, SC_EXIT_FORCE);
1376 goto err;
1379 #ifdef DEBUG
1380 /* Make sure no function left global temporary space locked. */
1381 if (F_ISSET(gp, G_TMP_INUSE)) {
1382 F_CLR(gp, G_TMP_INUSE);
1383 msgq(sp, M_ERR, "087|%s: temporary buffer not released",
1384 ecp->cmd->name);
1386 #endif
1388 * Ex displayed the number of lines modified immediately after each
1389 * command, so the command "1,10d|1,10d" would display:
1391 * 10 lines deleted
1392 * 10 lines deleted
1393 * <autoprint line>
1395 * Executing ex commands from vi only reported the final modified
1396 * lines message -- that's wrong enough that we don't match it.
1398 if (F_ISSET(sp, SC_EX))
1399 mod_rpt(sp);
1402 * Integrate any offset parsed by the underlying command, and make
1403 * sure the referenced line exists.
1405 * XXX
1406 * May not match historic practice (which I've never been able to
1407 * completely figure out.) For example, the '=' command from vi
1408 * mode often got the offset wrong, and complained it was too large,
1409 * but didn't seem to have a problem with the cursor. If anyone
1410 * complains, ask them how it's supposed to work, they might know.
1412 if (sp->ep != NULL && ecp->flagoff) {
1413 if (ecp->flagoff < 0) {
1414 if (sp->lno <= -ecp->flagoff) {
1415 msgq(sp, M_ERR,
1416 "088|Flag offset to before line 1");
1417 goto err;
1419 } else {
1420 if (!NPFITS(MAX_REC_NUMBER, sp->lno, ecp->flagoff)) {
1421 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1422 goto err;
1424 if (!db_exist(sp, sp->lno + ecp->flagoff)) {
1425 msgq(sp, M_ERR,
1426 "089|Flag offset past end-of-file");
1427 goto err;
1430 sp->lno += ecp->flagoff;
1434 * If the command executed successfully, we may want to display a line
1435 * based on the autoprint option or an explicit print flag. (Make sure
1436 * that there's a line to display.) Also, the autoprint edit option is
1437 * turned off for the duration of global commands.
1439 if (F_ISSET(sp, SC_EX) && sp->ep != NULL && sp->lno != 0) {
1441 * The print commands have already handled the `print' flags.
1442 * If so, clear them.
1444 if (FL_ISSET(ecp->iflags, E_CLRFLAG))
1445 FL_CLR(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT);
1447 /* If hash set only because of the number option, discard it. */
1448 if (F_ISSET(ecp, E_OPTNUM))
1449 FL_CLR(ecp->iflags, E_C_HASH);
1452 * If there was an explicit flag to display the new cursor line,
1453 * or autoprint is set and a change was made, display the line.
1454 * If any print flags were set use them, else default to print.
1456 LF_INIT(FL_ISSET(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT));
1457 if (!LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT | E_NOAUTO) &&
1458 !F_ISSET(sp, SC_EX_GLOBAL) &&
1459 O_ISSET(sp, O_AUTOPRINT) && F_ISSET(ecp, E_AUTOPRINT))
1460 LF_INIT(E_C_PRINT);
1462 if (LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT)) {
1463 cur.lno = sp->lno;
1464 cur.cno = 0;
1465 (void)ex_print(sp, ecp, &cur, &cur, flags);
1470 * If the command had an associated "+cmd", it has to be executed
1471 * before we finish executing any more of this ex command. For
1472 * example, consider a .exrc file that contains the following lines:
1474 * :set all
1475 * :edit +25 file.c|s/abc/ABC/|1
1476 * :3,5 print
1478 * This can happen more than once -- the historic vi simply hung or
1479 * dropped core, of course. Prepend the + command back into the
1480 * current command and continue. We may have to add an additional
1481 * <literal next> character. We know that it will fit because we
1482 * discarded at least one space and the + character.
1484 if (arg1_len != 0) {
1486 * If the last character of the + command was a <literal next>
1487 * character, it would be treated differently because of the
1488 * append. Quote it, if necessary.
1490 if (IS_ESCAPE(sp, ecp, arg1[arg1_len - 1])) {
1491 *--ecp->save_cmd = CH_LITERAL;
1492 ++ecp->save_cmdlen;
1495 ecp->save_cmd -= arg1_len;
1496 ecp->save_cmdlen += arg1_len;
1497 memcpy(ecp->save_cmd, arg1, arg1_len);
1500 * Any commands executed from a +cmd are executed starting at
1501 * the first column of the last line of the file -- NOT the
1502 * first nonblank.) The main file startup code doesn't know
1503 * that a +cmd was set, however, so it may have put us at the
1504 * top of the file. (Note, this is safe because we must have
1505 * switched files to get here.)
1507 F_SET(ecp, E_MOVETOEND);
1510 /* Update the current command. */
1511 ecp->cp = ecp->save_cmd;
1512 ecp->clen = ecp->save_cmdlen;
1515 * !!!
1516 * If we've changed screens or underlying files, any pending global or
1517 * v command, or @ buffer that has associated addresses, has to be
1518 * discarded. This is historic practice for globals, and necessary for
1519 * @ buffers that had associated addresses.
1521 * Otherwise, if we've changed underlying files, it's not a problem,
1522 * we continue with the rest of the ex command(s), operating on the
1523 * new file. However, if we switch screens (either by exiting or by
1524 * an explicit command), we have no way of knowing where to put output
1525 * messages, and, since we don't control screens here, we could screw
1526 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1527 * times). So, return and continue after we've got a new screen.
1529 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH)) {
1530 at_found = gv_found = 0;
1531 for (ecp = sp->gp->ecq.lh_first;
1532 ecp != NULL; ecp = ecp->q.le_next)
1533 switch (ecp->agv_flags) {
1534 case 0:
1535 case AGV_AT_NORANGE:
1536 break;
1537 case AGV_AT:
1538 if (!at_found) {
1539 at_found = 1;
1540 msgq(sp, M_ERR,
1541 "090|@ with range running when the file/screen changed");
1543 break;
1544 case AGV_GLOBAL:
1545 case AGV_V:
1546 if (!gv_found) {
1547 gv_found = 1;
1548 msgq(sp, M_ERR,
1549 "091|Global/v command running when the file/screen changed");
1551 break;
1552 default:
1553 abort();
1555 if (at_found || gv_found)
1556 goto discard;
1557 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_SSWITCH))
1558 goto rsuccess;
1561 goto loop;
1562 /* NOTREACHED */
1564 err: /*
1565 * On command failure, we discard keys and pending commands remaining,
1566 * as well as any keys that were mapped and waiting. The save_cmdlen
1567 * test is not necessarily correct. If we fail early enough we don't
1568 * know if the entire string was a single command or not. Guess, as
1569 * it's useful to know if commands other than the current one are being
1570 * discarded.
1572 if (ecp->save_cmdlen == 0)
1573 for (; ecp->clen; --ecp->clen) {
1574 ch = *ecp->cp++;
1575 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
1576 --ecp->clen;
1577 ++ecp->cp;
1578 } else if (ch == '\n' || ch == '|') {
1579 if (ecp->clen > 1)
1580 ecp->save_cmdlen = 1;
1581 break;
1584 if (ecp->save_cmdlen != 0 || gp->ecq.lh_first != &gp->excmd) {
1585 discard: msgq(sp, M_BERR,
1586 "092|Ex command failed: pending commands discarded");
1587 ex_discard(sp);
1589 if (v_event_flush(sp, CH_MAPPED))
1590 msgq(sp, M_BERR,
1591 "093|Ex command failed: mapped keys discarded");
1593 rfail: tmp = 1;
1594 if (0)
1595 rsuccess: tmp = 0;
1597 /* Turn off any file name error information. */
1598 gp->if_name = NULL;
1600 /* Turn off the global bit. */
1601 F_CLR(sp, SC_EX_GLOBAL);
1603 return (tmp);
1607 * ex_range --
1608 * Get a line range for ex commands, or perform a vi ex address search.
1610 * PUBLIC: int ex_range __P((SCR *, EXCMD *, int *));
1613 ex_range(sp, ecp, errp)
1614 SCR *sp;
1615 EXCMD *ecp;
1616 int *errp;
1618 enum { ADDR_FOUND, ADDR_NEED, ADDR_NONE } addr;
1619 GS *gp;
1620 EX_PRIVATE *exp;
1621 MARK m;
1622 int isaddr;
1624 *errp = 0;
1627 * Parse comma or semi-colon delimited line specs.
1629 * Semi-colon delimiters update the current address to be the last
1630 * address. For example, the command
1632 * :3;/pattern/ecp->cp
1634 * will search for pattern from line 3. In addition, if ecp->cp
1635 * is not a valid command, the current line will be left at 3, not
1636 * at the original address.
1638 * Extra addresses are discarded, starting with the first.
1640 * !!!
1641 * If any addresses are missing, they default to the current line.
1642 * This was historically true for both leading and trailing comma
1643 * delimited addresses as well as for trailing semicolon delimited
1644 * addresses. For consistency, we make it true for leading semicolon
1645 * addresses as well.
1647 gp = sp->gp;
1648 exp = EXP(sp);
1649 for (addr = ADDR_NONE, ecp->addrcnt = 0; ecp->clen > 0;)
1650 switch (*ecp->cp) {
1651 case '%': /* Entire file. */
1652 /* Vi ex address searches didn't permit % signs. */
1653 if (F_ISSET(ecp, E_VISEARCH))
1654 goto ret;
1656 /* It's an error if the file is empty. */
1657 if (sp->ep == NULL) {
1658 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1659 *errp = 1;
1660 return (0);
1663 * !!!
1664 * A percent character addresses all of the lines in
1665 * the file. Historically, it couldn't be followed by
1666 * any other address. We do it as a text substitution
1667 * for simplicity. POSIX 1003.2 is expected to follow
1668 * this practice.
1670 * If it's an empty file, the first line is 0, not 1.
1672 if (addr == ADDR_FOUND) {
1673 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1674 *errp = 1;
1675 return (0);
1677 if (db_last(sp, &ecp->addr2.lno))
1678 return (1);
1679 ecp->addr1.lno = ecp->addr2.lno == 0 ? 0 : 1;
1680 ecp->addr1.cno = ecp->addr2.cno = 0;
1681 ecp->addrcnt = 2;
1682 addr = ADDR_FOUND;
1683 ++ecp->cp;
1684 --ecp->clen;
1685 break;
1686 case ',': /* Comma delimiter. */
1687 /* Vi ex address searches didn't permit commas. */
1688 if (F_ISSET(ecp, E_VISEARCH))
1689 goto ret;
1690 /* FALLTHROUGH */
1691 case ';': /* Semi-colon delimiter. */
1692 if (sp->ep == NULL) {
1693 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1694 *errp = 1;
1695 return (0);
1697 if (addr != ADDR_FOUND)
1698 switch (ecp->addrcnt) {
1699 case 0:
1700 ecp->addr1.lno = sp->lno;
1701 ecp->addr1.cno = sp->cno;
1702 ecp->addrcnt = 1;
1703 break;
1704 case 2:
1705 ecp->addr1 = ecp->addr2;
1706 /* FALLTHROUGH */
1707 case 1:
1708 ecp->addr2.lno = sp->lno;
1709 ecp->addr2.cno = sp->cno;
1710 ecp->addrcnt = 2;
1711 break;
1713 if (*ecp->cp == ';')
1714 switch (ecp->addrcnt) {
1715 case 0:
1716 abort();
1717 /* NOTREACHED */
1718 case 1:
1719 sp->lno = ecp->addr1.lno;
1720 sp->cno = ecp->addr1.cno;
1721 break;
1722 case 2:
1723 sp->lno = ecp->addr2.lno;
1724 sp->cno = ecp->addr2.cno;
1725 break;
1727 addr = ADDR_NEED;
1728 /* FALLTHROUGH */
1729 case ' ': /* Whitespace. */
1730 case '\t': /* Whitespace. */
1731 ++ecp->cp;
1732 --ecp->clen;
1733 break;
1734 default:
1735 /* Get a line specification. */
1736 if (ex_line(sp, ecp, &m, &isaddr, errp))
1737 return (1);
1738 if (*errp)
1739 return (0);
1740 if (!isaddr)
1741 goto ret;
1742 if (addr == ADDR_FOUND) {
1743 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1744 *errp = 1;
1745 return (0);
1747 switch (ecp->addrcnt) {
1748 case 0:
1749 ecp->addr1 = m;
1750 ecp->addrcnt = 1;
1751 break;
1752 case 1:
1753 ecp->addr2 = m;
1754 ecp->addrcnt = 2;
1755 break;
1756 case 2:
1757 ecp->addr1 = ecp->addr2;
1758 ecp->addr2 = m;
1759 break;
1761 addr = ADDR_FOUND;
1762 break;
1766 * !!!
1767 * Vi ex address searches are indifferent to order or trailing
1768 * semi-colons.
1770 ret: if (F_ISSET(ecp, E_VISEARCH))
1771 return (0);
1773 if (addr == ADDR_NEED)
1774 switch (ecp->addrcnt) {
1775 case 0:
1776 ecp->addr1.lno = sp->lno;
1777 ecp->addr1.cno = sp->cno;
1778 ecp->addrcnt = 1;
1779 break;
1780 case 2:
1781 ecp->addr1 = ecp->addr2;
1782 /* FALLTHROUGH */
1783 case 1:
1784 ecp->addr2.lno = sp->lno;
1785 ecp->addr2.cno = sp->cno;
1786 ecp->addrcnt = 2;
1787 break;
1790 if (ecp->addrcnt == 2 && ecp->addr2.lno < ecp->addr1.lno) {
1791 msgq(sp, M_ERR,
1792 "094|The second address is smaller than the first");
1793 *errp = 1;
1795 return (0);
1799 * ex_line --
1800 * Get a single line address specifier.
1802 * The way the "previous context" mark worked was that any "non-relative"
1803 * motion set it. While ex/vi wasn't totally consistent about this, ANY
1804 * numeric address, search pattern, '$', or mark reference in an address
1805 * was considered non-relative, and set the value. Which should explain
1806 * why we're hacking marks down here. The problem was that the mark was
1807 * only set if the command was called, i.e. we have to set a flag and test
1808 * it later.
1810 * XXX
1811 * This is probably still not exactly historic practice, although I think
1812 * it's fairly close.
1814 static int
1815 ex_line(sp, ecp, mp, isaddrp, errp)
1816 SCR *sp;
1817 EXCMD *ecp;
1818 MARK *mp;
1819 int *isaddrp, *errp;
1821 enum nresult nret;
1822 EX_PRIVATE *exp;
1823 GS *gp;
1824 long total, val;
1825 int isneg;
1826 int (*sf) __P((SCR *, MARK *, MARK *, char *, size_t, char **, u_int));
1827 char *endp;
1829 gp = sp->gp;
1830 exp = EXP(sp);
1832 *isaddrp = *errp = 0;
1833 F_CLR(ecp, E_DELTA);
1835 /* No addresses permitted until a file has been read in. */
1836 if (sp->ep == NULL && strchr("$0123456789'\\/?.+-^", *ecp->cp)) {
1837 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1838 *errp = 1;
1839 return (0);
1842 switch (*ecp->cp) {
1843 case '$': /* Last line in the file. */
1844 *isaddrp = 1;
1845 F_SET(ecp, E_ABSMARK);
1847 mp->cno = 0;
1848 if (db_last(sp, &mp->lno))
1849 return (1);
1850 ++ecp->cp;
1851 --ecp->clen;
1852 break; /* Absolute line number. */
1853 case '0': case '1': case '2': case '3': case '4':
1854 case '5': case '6': case '7': case '8': case '9':
1855 *isaddrp = 1;
1856 F_SET(ecp, E_ABSMARK);
1858 if ((nret = nget_slong(&val, ecp->cp, &endp, 10)) != NUM_OK) {
1859 ex_badaddr(sp, NULL, A_NOTSET, nret);
1860 *errp = 1;
1861 return (0);
1863 if (!NPFITS(MAX_REC_NUMBER, 0, val)) {
1864 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1865 *errp = 1;
1866 return (0);
1868 mp->lno = val;
1869 mp->cno = 0;
1870 ecp->clen -= (endp - ecp->cp);
1871 ecp->cp = endp;
1872 break;
1873 case '\'': /* Use a mark. */
1874 *isaddrp = 1;
1875 F_SET(ecp, E_ABSMARK);
1877 if (ecp->clen == 1) {
1878 msgq(sp, M_ERR, "095|No mark name supplied");
1879 *errp = 1;
1880 return (0);
1882 if (mark_get(sp, ecp->cp[1], mp, M_ERR)) {
1883 *errp = 1;
1884 return (0);
1886 ecp->cp += 2;
1887 ecp->clen -= 2;
1888 break;
1889 case '\\': /* Search: forward/backward. */
1891 * !!!
1892 * I can't find any difference between // and \/ or between
1893 * ?? and \?. Mark Horton doesn't remember there being any
1894 * difference. C'est la vie.
1896 if (ecp->clen < 2 ||
1897 ecp->cp[1] != '/' && ecp->cp[1] != '?') {
1898 msgq(sp, M_ERR, "096|\\ not followed by / or ?");
1899 *errp = 1;
1900 return (0);
1902 ++ecp->cp;
1903 --ecp->clen;
1904 sf = ecp->cp[0] == '/' ? f_search : b_search;
1905 goto search;
1906 case '/': /* Search forward. */
1907 sf = f_search;
1908 goto search;
1909 case '?': /* Search backward. */
1910 sf = b_search;
1912 search: mp->lno = sp->lno;
1913 mp->cno = sp->cno;
1914 if (sf(sp, mp, mp, ecp->cp, ecp->clen, &endp,
1915 SEARCH_MSG | SEARCH_PARSE | SEARCH_SET |
1916 (F_ISSET(ecp, E_SEARCH_WMSG) ? SEARCH_WMSG : 0))) {
1917 *errp = 1;
1918 return (0);
1921 /* Fix up the command pointers. */
1922 ecp->clen -= (endp - ecp->cp);
1923 ecp->cp = endp;
1925 *isaddrp = 1;
1926 F_SET(ecp, E_ABSMARK);
1927 break;
1928 case '.': /* Current position. */
1929 *isaddrp = 1;
1930 mp->cno = sp->cno;
1932 /* If an empty file, then '.' is 0, not 1. */
1933 if (sp->lno == 1) {
1934 if (db_last(sp, &mp->lno))
1935 return (1);
1936 if (mp->lno != 0)
1937 mp->lno = 1;
1938 } else
1939 mp->lno = sp->lno;
1942 * !!!
1943 * Historically, .<number> was the same as .+<number>, i.e.
1944 * the '+' could be omitted. (This feature is found in ed
1945 * as well.)
1947 if (ecp->clen > 1 && isdigit(ecp->cp[1]))
1948 *ecp->cp = '+';
1949 else {
1950 ++ecp->cp;
1951 --ecp->clen;
1953 break;
1956 /* Skip trailing <blank>s. */
1957 for (; ecp->clen > 0 &&
1958 isblank(ecp->cp[0]); ++ecp->cp, --ecp->clen);
1961 * Evaluate any offset. If no address yet found, the offset
1962 * is relative to ".".
1964 total = 0;
1965 if (ecp->clen != 0 && (isdigit(ecp->cp[0]) ||
1966 ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1967 ecp->cp[0] == '^')) {
1968 if (!*isaddrp) {
1969 *isaddrp = 1;
1970 mp->lno = sp->lno;
1971 mp->cno = sp->cno;
1974 * Evaluate an offset, defined as:
1976 * [+-^<blank>]*[<blank>]*[0-9]*
1978 * The rough translation is any number of signs, optionally
1979 * followed by numbers, or a number by itself, all <blank>
1980 * separated.
1982 * !!!
1983 * All address offsets were additive, e.g. "2 2 3p" was the
1984 * same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2".
1985 * Note, however, "2 /ZZZ/" was an error. It was also legal
1986 * to insert signs without numbers, so "3 - 2" was legal, and
1987 * equal to 4.
1989 * !!!
1990 * Offsets were historically permitted for any line address,
1991 * e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after
1992 * line 8.
1994 * !!!
1995 * Offsets were historically permitted for search commands,
1996 * and handled as addresses: "/pattern/2 2 2" was legal, and
1997 * referenced the 6th line after pattern.
1999 F_SET(ecp, E_DELTA);
2000 for (;;) {
2001 for (; ecp->clen > 0 && isblank(ecp->cp[0]);
2002 ++ecp->cp, --ecp->clen);
2003 if (ecp->clen == 0 || !isdigit(ecp->cp[0]) &&
2004 ecp->cp[0] != '+' && ecp->cp[0] != '-' &&
2005 ecp->cp[0] != '^')
2006 break;
2007 if (!isdigit(ecp->cp[0]) &&
2008 !isdigit(ecp->cp[1])) {
2009 total += ecp->cp[0] == '+' ? 1 : -1;
2010 --ecp->clen;
2011 ++ecp->cp;
2012 } else {
2013 if (ecp->cp[0] == '-' ||
2014 ecp->cp[0] == '^') {
2015 ++ecp->cp;
2016 --ecp->clen;
2017 isneg = 1;
2018 } else
2019 isneg = 0;
2021 /* Get a signed long, add it to the total. */
2022 if ((nret = nget_slong(&val,
2023 ecp->cp, &endp, 10)) != NUM_OK ||
2024 (nret = NADD_SLONG(sp,
2025 total, val)) != NUM_OK) {
2026 ex_badaddr(sp, NULL, A_NOTSET, nret);
2027 *errp = 1;
2028 return (0);
2030 total += isneg ? -val : val;
2031 ecp->clen -= (endp - ecp->cp);
2032 ecp->cp = endp;
2038 * Any value less than 0 is an error. Make sure that the new value
2039 * will fit into a recno_t.
2041 if (*isaddrp && total != 0) {
2042 if (total < 0) {
2043 if (-total > mp->lno) {
2044 msgq(sp, M_ERR,
2045 "097|Reference to a line number less than 0");
2046 *errp = 1;
2047 return (0);
2049 } else
2050 if (!NPFITS(MAX_REC_NUMBER, mp->lno, total)) {
2051 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
2052 *errp = 1;
2053 return (0);
2055 mp->lno += total;
2057 return (0);
2062 * ex_load --
2063 * Load up the next command, which may be an @ buffer or global command.
2065 static int
2066 ex_load(sp)
2067 SCR *sp;
2069 GS *gp;
2070 EXCMD *ecp;
2071 RANGE *rp;
2073 F_CLR(sp, SC_EX_GLOBAL);
2076 * Lose any exhausted commands. We know that the first command
2077 * can't be an AGV command, which makes things a bit easier.
2079 for (gp = sp->gp;;) {
2081 * If we're back to the original structure, leave it around,
2082 * but discard any allocated source name, we've returned to
2083 * the beginning of the command stack.
2085 if ((ecp = gp->ecq.lh_first) == &gp->excmd) {
2086 if (F_ISSET(ecp, E_NAMEDISCARD)) {
2087 free(ecp->if_name);
2088 ecp->if_name = NULL;
2090 return (0);
2094 * ecp->clen will be 0 for the first discarded command, but
2095 * may not be 0 for subsequent ones, e.g. if the original
2096 * command was ":g/xx/@a|s/b/c/", then when we discard the
2097 * command pushed on the stack by the @a, we have to resume
2098 * the global command which included the substitute command.
2100 if (ecp->clen != 0)
2101 return (0);
2104 * If it's an @, global or v command, we may need to continue
2105 * the command on a different line.
2107 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2108 /* Discard any exhausted ranges. */
2109 while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq)
2110 if (rp->start > rp->stop) {
2111 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
2112 free(rp);
2113 } else
2114 break;
2116 /* If there's another range, continue with it. */
2117 if (rp != (void *)&ecp->rq)
2118 break;
2120 /* If it's a global/v command, fix up the last line. */
2121 if (FL_ISSET(ecp->agv_flags,
2122 AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO)
2123 if (db_exist(sp, ecp->range_lno))
2124 sp->lno = ecp->range_lno;
2125 else {
2126 if (db_last(sp, &sp->lno))
2127 return (1);
2128 if (sp->lno == 0)
2129 sp->lno = 1;
2131 free(ecp->o_cp);
2134 /* Discard the EXCMD. */
2135 LIST_REMOVE(ecp, q);
2136 free(ecp);
2140 * We only get here if it's an active @, global or v command. Set
2141 * the current line number, and get a new copy of the command for
2142 * the parser. Note, the original pointer almost certainly moved,
2143 * so we have play games.
2145 ecp->cp = ecp->o_cp;
2146 memcpy(ecp->cp, ecp->cp + ecp->o_clen, ecp->o_clen);
2147 ecp->clen = ecp->o_clen;
2148 ecp->range_lno = sp->lno = rp->start++;
2150 if (FL_ISSET(ecp->agv_flags, AGV_GLOBAL | AGV_V))
2151 F_SET(sp, SC_EX_GLOBAL);
2152 return (0);
2156 * ex_discard --
2157 * Discard any pending ex commands.
2159 static int
2160 ex_discard(sp)
2161 SCR *sp;
2163 GS *gp;
2164 EXCMD *ecp;
2165 RANGE *rp;
2168 * We know the first command can't be an AGV command, so we don't
2169 * process it specially. We do, however, nail the command itself.
2171 for (gp = sp->gp; (ecp = gp->ecq.lh_first) != &gp->excmd;) {
2172 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2173 while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq) {
2174 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
2175 free(rp);
2177 free(ecp->o_cp);
2179 LIST_REMOVE(ecp, q);
2180 free(ecp);
2182 gp->ecq.lh_first->clen = 0;
2183 return (0);
2187 * ex_unknown --
2188 * Display an unknown command name.
2190 static void
2191 ex_unknown(sp, cmd, len)
2192 SCR *sp;
2193 char *cmd;
2194 size_t len;
2196 size_t blen;
2197 char *bp;
2199 GET_SPACE_GOTO(sp, bp, blen, len + 1);
2200 bp[len] = '\0';
2201 memcpy(bp, cmd, len);
2202 msgq_str(sp, M_ERR, bp, "098|The %s command is unknown");
2203 FREE_SPACE(sp, bp, blen);
2205 alloc_err:
2206 return;
2210 * ex_is_abbrev -
2211 * The vi text input routine needs to know if ex thinks this is an
2212 * [un]abbreviate command, so it can turn off abbreviations. See
2213 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2215 * PUBLIC: int ex_is_abbrev __P((char *, size_t));
2218 ex_is_abbrev(name, len)
2219 char *name;
2220 size_t len;
2222 EXCMDLIST const *cp;
2224 return ((cp = ex_comm_search(name, len)) != NULL &&
2225 (cp == &cmds[C_ABBR] || cp == &cmds[C_UNABBREVIATE]));
2229 * ex_is_unmap -
2230 * The vi text input routine needs to know if ex thinks this is an
2231 * unmap command, so it can turn off input mapping. See the usual
2232 * ranting in the vi/v_txt_ev.c:txt_unmap() routine.
2234 * PUBLIC: int ex_is_unmap __P((char *, size_t));
2237 ex_is_unmap(name, len)
2238 char *name;
2239 size_t len;
2241 EXCMDLIST const *cp;
2244 * The command the vi input routines are really interested in
2245 * is "unmap!", not just unmap.
2247 if (name[len - 1] != '!')
2248 return (0);
2249 --len;
2250 return ((cp = ex_comm_search(name, len)) != NULL &&
2251 cp == &cmds[C_UNMAP]);
2255 * ex_comm_search --
2256 * Search for a command name.
2258 static EXCMDLIST const *
2259 ex_comm_search(name, len)
2260 char *name;
2261 size_t len;
2263 EXCMDLIST const *cp;
2265 for (cp = cmds; cp->name != NULL; ++cp) {
2266 if (cp->name[0] > name[0])
2267 return (NULL);
2268 if (cp->name[0] != name[0])
2269 continue;
2270 if (!memcmp(name, cp->name, len))
2271 return (cp);
2273 return (NULL);
2277 * ex_badaddr --
2278 * Display a bad address message.
2280 * PUBLIC: void ex_badaddr
2281 * PUBLIC: __P((SCR *, EXCMDLIST const *, enum badaddr, enum nresult));
2283 void
2284 ex_badaddr(sp, cp, ba, nret)
2285 SCR *sp;
2286 EXCMDLIST const *cp;
2287 enum badaddr ba;
2288 enum nresult nret;
2290 recno_t lno;
2292 switch (nret) {
2293 case NUM_OK:
2294 break;
2295 case NUM_ERR:
2296 msgq(sp, M_SYSERR, NULL);
2297 return;
2298 case NUM_OVER:
2299 msgq(sp, M_ERR, "099|Address value overflow");
2300 return;
2301 case NUM_UNDER:
2302 msgq(sp, M_ERR, "100|Address value underflow");
2303 return;
2307 * When encountering an address error, tell the user if there's no
2308 * underlying file, that's the real problem.
2310 if (sp->ep == NULL) {
2311 ex_emsg(sp, cp->name, EXM_NOFILEYET);
2312 return;
2315 switch (ba) {
2316 case A_COMBO:
2317 msgq(sp, M_ERR, "101|Illegal address combination");
2318 break;
2319 case A_EOF:
2320 if (db_last(sp, &lno))
2321 return;
2322 if (lno != 0) {
2323 msgq(sp, M_ERR,
2324 "102|Illegal address: only %lu lines in the file",
2325 lno);
2326 break;
2328 /* FALLTHROUGH */
2329 case A_EMPTY:
2330 msgq(sp, M_ERR, "103|Illegal address: the file is empty");
2331 break;
2332 case A_NOTSET:
2333 abort();
2334 /* NOTREACHED */
2335 case A_ZERO:
2336 msgq(sp, M_ERR,
2337 "104|The %s command doesn't permit an address of 0",
2338 cp->name);
2339 break;
2341 return;
2344 #if defined(DEBUG) && defined(COMLOG)
2346 * ex_comlog --
2347 * Log ex commands.
2349 static void
2350 ex_comlog(sp, ecp)
2351 SCR *sp;
2352 EXCMD *ecp;
2354 vtrace(sp, "ecmd: %s", ecp->cmd->name);
2355 if (ecp->addrcnt > 0) {
2356 vtrace(sp, " a1 %d", ecp->addr1.lno);
2357 if (ecp->addrcnt > 1)
2358 vtrace(sp, " a2: %d", ecp->addr2.lno);
2360 if (ecp->lineno)
2361 vtrace(sp, " line %d", ecp->lineno);
2362 if (ecp->flags)
2363 vtrace(sp, " flags 0x%x", ecp->flags);
2364 if (F_ISSET(&exc, E_BUFFER))
2365 vtrace(sp, " buffer %c", ecp->buffer);
2366 if (ecp->argc)
2367 for (cnt = 0; cnt < ecp->argc; ++cnt)
2368 vtrace(sp, " arg %d: {%s}", cnt, ecp->argv[cnt]->bp);
2369 vtrace(sp, "\n");
2371 #endif