nvi forgets to switch tty from ex mode on q when there are other screens
[nvi.git] / ex / ex.c
blob3dba5bfcad63b5918f64dfca0827b0bddbfd7fde
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.75 2004/03/16 14:13:35 skimo Exp $ (Berkeley) $Date: 2004/03/16 14:13:35 $";
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((SCR *, CHAR_T *, 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_T *, size_t));
45 * ex --
46 * Main ex loop.
48 * PUBLIC: int ex __P((SCR **));
50 int
51 ex(SCR **spp)
53 EX_PRIVATE *exp;
54 GS *gp;
55 WIN *wp;
56 MSGS *mp;
57 SCR *sp;
58 TEXT *tp;
59 u_int32_t flags;
61 sp = *spp;
62 wp = sp->wp;
63 gp = sp->gp;
64 exp = EXP(sp);
66 /* Start the ex screen. */
67 if (ex_init(sp))
68 return (1);
70 /* Flush any saved messages. */
71 while ((mp = gp->msgq.lh_first) != NULL) {
72 wp->scr_msg(sp, mp->mtype, mp->buf, mp->len);
73 LIST_REMOVE(mp, q);
74 free(mp->buf);
75 free(mp);
78 /* If reading from a file, errors should have name and line info. */
79 if (F_ISSET(gp, G_SCRIPTED)) {
80 wp->excmd.if_lno = 1;
81 wp->excmd.if_name = "script";
85 * !!!
86 * Initialize the text flags. The beautify edit option historically
87 * applied to ex command input read from a file. In addition, the
88 * first time a ^H was discarded from the input, there was a message,
89 * "^H discarded", that was displayed. We don't bother.
91 LF_INIT(TXT_BACKSLASH | TXT_CNTRLD | TXT_CR);
92 for (;; ++wp->excmd.if_lno) {
93 /* Display status line and flush. */
94 if (F_ISSET(sp, SC_STATUS)) {
95 if (!F_ISSET(sp, SC_EX_SILENT))
96 msgq_status(sp, sp->lno, 0);
97 F_CLR(sp, SC_STATUS);
99 (void)ex_fflush(sp);
101 /* Set the flags the user can reset. */
102 if (O_ISSET(sp, O_BEAUTIFY))
103 LF_SET(TXT_BEAUTIFY);
104 if (O_ISSET(sp, O_PROMPT))
105 LF_SET(TXT_PROMPT);
107 /* Clear any current interrupts, and get a command. */
108 CLR_INTERRUPT(sp);
109 if (ex_txt(sp, &sp->tiq, ':', flags))
110 return (1);
111 if (INTERRUPTED(sp)) {
112 (void)ex_puts(sp, "\n");
113 (void)ex_fflush(sp);
114 continue;
117 /* Initialize the command structure. */
118 CLEAR_EX_PARSER(&wp->excmd);
121 * If the user entered a single carriage return, send
122 * ex_cmd() a separator -- it discards single newlines.
124 tp = sp->tiq.cqh_first;
125 if (tp->len == 0) {
126 static CHAR_T space = ' ';
127 wp->excmd.cp = &space; /* __TK__ why not |? */
128 wp->excmd.clen = 1;
129 } else {
130 wp->excmd.cp = tp->lb;
131 wp->excmd.clen = tp->len;
133 F_INIT(&wp->excmd, E_NRSEP);
135 if (ex_cmd(sp) && F_ISSET(gp, G_SCRIPTED))
136 return (1);
138 if (INTERRUPTED(sp)) {
139 CLR_INTERRUPT(sp);
140 msgq(sp, M_ERR, "170|Interrupted");
144 * If the last command caused a restart, or switched screens
145 * or into vi, return.
147 if (F_ISSET(gp, G_SRESTART) || F_ISSET(sp, SC_SSWITCH | SC_VI)) {
148 *spp = sp;
149 break;
152 /* If the last command switched files, we don't care. */
153 F_CLR(sp, SC_FSWITCH);
156 * If we're exiting this screen, move to the next one. By
157 * definition, this means returning into vi, so return to the
158 * main editor loop. The ordering is careful, don't discard
159 * the contents of sp until the end.
161 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
162 if (file_end(sp, NULL, F_ISSET(sp, SC_EXIT_FORCE)))
163 return (1);
164 *spp = screen_next(sp);
165 if (*spp) {
166 F_CLR(*spp, SC_SCR_VI);
167 F_SET(*spp, SC_SCR_EX);
169 return (screen_end(sp));
172 return (0);
176 * ex_cmd --
177 * The guts of the ex parser: parse and execute a string containing
178 * ex commands.
180 * !!!
181 * This code MODIFIES the string that gets passed in, to delete quoting
182 * characters, etc. The string cannot be readonly/text space, nor should
183 * you expect to use it again after ex_cmd() returns.
185 * !!!
186 * For the fun of it, if you want to see if a vi clone got the ex argument
187 * parsing right, try:
189 * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
190 * vi
191 * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
193 * or: vi
194 * :set|file|append|set|file
196 * For extra credit, try them in a startup .exrc file.
198 * PUBLIC: int ex_cmd __P((SCR *));
201 ex_cmd(SCR *sp)
203 enum nresult nret;
204 EX_PRIVATE *exp;
205 EXCMD *ecp;
206 GS *gp;
207 WIN *wp;
208 MARK cur;
209 db_recno_t lno;
210 size_t arg1_len, discard, len;
211 u_int32_t flags;
212 long ltmp;
213 int at_found, gv_found;
214 int cnt, delim, isaddr, namelen;
215 int newscreen, notempty, tmp, vi_address;
216 CHAR_T *arg1, *s, *p, *t;
217 CHAR_T ch;
218 CHAR_T *n;
219 char *np;
221 gp = sp->gp;
222 wp = sp->wp;
223 exp = EXP(sp);
226 * We always start running the command on the top of the stack.
227 * This means that *everything* must be resolved when we leave
228 * this function for any reason.
230 loop: ecp = wp->ecq.lh_first;
232 /* If we're reading a command from a file, set up error information. */
233 if (ecp->if_name != NULL) {
234 wp->if_lno = ecp->if_lno;
235 wp->if_name = ecp->if_name;
239 * If a move to the end of the file is scheduled for this command,
240 * do it now.
242 if (F_ISSET(ecp, E_MOVETOEND)) {
243 if (db_last(sp, &sp->lno))
244 goto rfail;
245 sp->cno = 0;
246 F_CLR(ecp, E_MOVETOEND);
249 /* If we found a newline, increment the count now. */
250 if (F_ISSET(ecp, E_NEWLINE)) {
251 ++wp->if_lno;
252 ++ecp->if_lno;
253 F_CLR(ecp, E_NEWLINE);
256 /* (Re)initialize the EXCMD structure, preserving some flags. */
257 CLEAR_EX_CMD(ecp);
259 /* Initialize the argument structures. */
260 if (argv_init(sp, ecp))
261 goto err;
263 /* Initialize +cmd, saved command information. */
264 arg1 = NULL;
265 ecp->save_cmdlen = 0;
267 /* Skip <blank>s, empty lines. */
268 for (notempty = 0; ecp->clen > 0; ++ecp->cp, --ecp->clen)
269 if ((ch = *ecp->cp) == '\n') {
270 ++wp->if_lno;
271 ++ecp->if_lno;
272 } else if (ISBLANK(ch))
273 notempty = 1;
274 else
275 break;
278 * !!!
279 * Permit extra colons at the start of the line. Historically,
280 * ex/vi allowed a single extra one. It's simpler not to count.
281 * The stripping is done here because, historically, any command
282 * could have preceding colons, e.g. ":g/pattern/:p" worked.
284 if (ecp->clen != 0 && ch == ':') {
285 notempty = 1;
286 while (--ecp->clen > 0 && (ch = *++ecp->cp) == ':');
290 * Command lines that start with a double-quote are comments.
292 * !!!
293 * Historically, there was no escape or delimiter for a comment, e.g.
294 * :"foo|set was a single comment and nothing was output. Since nvi
295 * permits users to escape <newline> characters into command lines, we
296 * have to check for that case.
298 if (ecp->clen != 0 && ch == '"') {
299 while (--ecp->clen > 0 && *++ecp->cp != '\n');
300 if (*ecp->cp == '\n') {
301 F_SET(ecp, E_NEWLINE);
302 ++ecp->cp;
303 --ecp->clen;
305 goto loop;
308 /* Skip whitespace. */
309 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
310 ch = *ecp->cp;
311 if (!ISBLANK(ch))
312 break;
316 * The last point at which an empty line can mean do nothing.
318 * !!!
319 * Historically, in ex mode, lines containing only <blank> characters
320 * were the same as a single <carriage-return>, i.e. a default command.
321 * In vi mode, they were ignored. In .exrc files this was a serious
322 * annoyance, as vi kept trying to treat them as print commands. We
323 * ignore backward compatibility in this case, discarding lines that
324 * contain only <blank> characters from .exrc files.
326 * !!!
327 * This is where you end up when you're done a command, i.e. clen has
328 * gone to zero. Continue if there are more commands to run.
330 if (ecp->clen == 0 &&
331 (!notempty || F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_BLIGNORE))) {
332 if (ex_load(sp))
333 goto rfail;
334 ecp = wp->ecq.lh_first;
335 if (ecp->clen == 0)
336 goto rsuccess;
337 goto loop;
341 * Check to see if this is a command for which we may want to move
342 * the cursor back up to the previous line. (The command :1<CR>
343 * wants a <newline> separator, but the command :<CR> wants to erase
344 * the command line.) If the line is empty except for <blank>s,
345 * <carriage-return> or <eof>, we'll probably want to move up. I
346 * don't think there's any way to get <blank> characters *after* the
347 * command character, but this is the ex parser, and I've been wrong
348 * before.
350 if (F_ISSET(ecp, E_NRSEP) &&
351 ecp->clen != 0 && (ecp->clen != 1 || ecp->cp[0] != '\004'))
352 F_CLR(ecp, E_NRSEP);
354 /* Parse command addresses. */
355 if (ex_range(sp, ecp, &tmp))
356 goto rfail;
357 if (tmp)
358 goto err;
361 * Skip <blank>s and any more colons (the command :3,5:print
362 * worked, historically).
364 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
365 ch = *ecp->cp;
366 if (!ISBLANK(ch) && ch != ':')
367 break;
371 * If no command, ex does the last specified of p, l, or #, and vi
372 * moves to the line. Otherwise, determine the length of the command
373 * name by looking for the first non-alphabetic character. (There
374 * are a few non-alphabetic characters in command names, but they're
375 * all single character commands.) This isn't a great test, because
376 * it means that, for the command ":e +cut.c file", we'll report that
377 * the command "cut" wasn't known. However, it makes ":e+35 file" work
378 * correctly.
380 * !!!
381 * Historically, lines with multiple adjacent (or <blank> separated)
382 * command separators were very strange. For example, the command
383 * |||<carriage-return>, when the cursor was on line 1, displayed
384 * lines 2, 3 and 5 of the file. In addition, the command " | "
385 * would only display the line after the next line, instead of the
386 * next two lines. No ideas why. It worked reasonably when executed
387 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
388 * command for each separator.
390 #define SINGLE_CHAR_COMMANDS "\004!#&*<=>@~"
391 newscreen = 0;
392 if (ecp->clen != 0 && ecp->cp[0] != '|' && ecp->cp[0] != '\n') {
393 if (strchr(SINGLE_CHAR_COMMANDS, *ecp->cp)) {
394 p = ecp->cp;
395 ++ecp->cp;
396 --ecp->clen;
397 namelen = 1;
398 } else {
399 for (p = ecp->cp;
400 ecp->clen > 0; --ecp->clen, ++ecp->cp)
401 if (!ISALPHA(*ecp->cp))
402 break;
403 if ((namelen = ecp->cp - p) == 0) {
404 msgq(sp, M_ERR, "080|Unknown command name");
405 goto err;
410 * !!!
411 * Historic vi permitted flags to immediately follow any
412 * subset of the 'delete' command, but then did not permit
413 * further arguments (flag, buffer, count). Make it work.
414 * Permit further arguments for the few shreds of dignity
415 * it offers.
417 * Adding commands that start with 'd', and match "delete"
418 * up to a l, p, +, - or # character can break this code.
420 * !!!
421 * Capital letters beginning the command names ex, edit,
422 * next, previous, tag and visual (in vi mode) indicate the
423 * command should happen in a new screen.
425 switch (p[0]) {
426 case 'd':
427 for (s = p,
428 n = cmds[C_DELETE].name; *s == *n; ++s, ++n);
429 if (s[0] == 'l' || s[0] == 'p' || s[0] == '+' ||
430 s[0] == '-' || s[0] == '^' || s[0] == '#') {
431 len = (ecp->cp - p) - (s - p);
432 ecp->cp -= len;
433 ecp->clen += len;
434 ecp->rcmd = cmds[C_DELETE];
435 ecp->rcmd.syntax = "1bca1";
436 ecp->cmd = &ecp->rcmd;
437 goto skip_srch;
439 break;
440 case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
441 newscreen = 1;
442 p[0] = tolower(p[0]);
443 break;
447 * Search the table for the command.
449 * !!!
450 * Historic vi permitted the mark to immediately follow the
451 * 'k' in the 'k' command. Make it work.
453 * !!!
454 * Historic vi permitted any flag to follow the s command, e.g.
455 * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
456 * Since the following characters all have to be flags, i.e.
457 * alphabetics, we can let the s command routine return errors
458 * if it was some illegal command string. This code will break
459 * if an "sg" or similar command is ever added. The substitute
460 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
461 * follows the 's', but we limit the choices here to "cgr" so
462 * that we get unknown command messages for wrong combinations.
464 if ((ecp->cmd = ex_comm_search(sp, p, namelen)) == NULL)
465 switch (p[0]) {
466 case 'k':
467 if (namelen == 2) {
468 ecp->cp -= namelen - 1;
469 ecp->clen += namelen - 1;
470 ecp->cmd = &cmds[C_K];
471 break;
473 goto unknown;
474 case 's':
475 for (s = p + 1, cnt = namelen; --cnt; ++s)
476 if (s[0] != 'c' &&
477 s[0] != 'g' && s[0] != 'r')
478 break;
479 if (cnt == 0) {
480 ecp->cp -= namelen - 1;
481 ecp->clen += namelen - 1;
482 ecp->rcmd = cmds[C_SUBSTITUTE];
483 ecp->rcmd.fn = ex_subagain;
484 ecp->cmd = &ecp->rcmd;
485 break;
487 /* FALLTHROUGH */
488 default:
489 unknown: if (newscreen)
490 p[0] = toupper(p[0]);
491 ex_unknown(sp, p, namelen);
492 goto err;
496 * The visual command has a different syntax when called
497 * from ex than when called from a vi colon command. FMH.
498 * Make the change now, before we test for the newscreen
499 * semantic, so that we're testing the right one.
501 skip_srch: if (ecp->cmd == &cmds[C_VISUAL_EX] && F_ISSET(sp, SC_VI))
502 ecp->cmd = &cmds[C_VISUAL_VI];
505 * !!!
506 * Historic vi permitted a capital 'P' at the beginning of
507 * any command that started with 'p'. Probably wanted the
508 * P[rint] command for backward compatibility, and the code
509 * just made Preserve and Put work by accident. Nvi uses
510 * Previous to mean previous-in-a-new-screen, so be careful.
512 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN) &&
513 (ecp->cmd == &cmds[C_PRINT] ||
514 ecp->cmd == &cmds[C_PRESERVE]))
515 newscreen = 0;
517 /* Test for a newscreen associated with this command. */
518 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN))
519 goto unknown;
521 /* Secure means no shell access. */
522 if (F_ISSET(ecp->cmd, E_SECURE) && O_ISSET(sp, O_SECURE)) {
523 ex_wemsg(sp, ecp->cmd->name, EXM_SECURE);
524 goto err;
528 * Multiple < and > characters; another "feature". Note,
529 * The string passed to the underlying function may not be
530 * nul terminated in this case.
532 if ((ecp->cmd == &cmds[C_SHIFTL] && *p == '<') ||
533 (ecp->cmd == &cmds[C_SHIFTR] && *p == '>')) {
534 for (ch = *p;
535 ecp->clen > 0; --ecp->clen, ++ecp->cp)
536 if (*ecp->cp != ch)
537 break;
538 if (argv_exp0(sp, ecp, p, ecp->cp - p))
539 goto err;
542 /* Set the format style flags for the next command. */
543 if (ecp->cmd == &cmds[C_HASH])
544 exp->fdef = E_C_HASH;
545 else if (ecp->cmd == &cmds[C_LIST])
546 exp->fdef = E_C_LIST;
547 else if (ecp->cmd == &cmds[C_PRINT])
548 exp->fdef = E_C_PRINT;
549 F_CLR(ecp, E_USELASTCMD);
550 } else {
551 /* Print is the default command. */
552 ecp->cmd = &cmds[C_PRINT];
554 /* Set the saved format flags. */
555 F_SET(ecp, exp->fdef);
558 * !!!
559 * If no address was specified, and it's not a global command,
560 * we up the address by one. (I have no idea why globals are
561 * exempted, but it's (ahem) historic practice.)
563 if (ecp->addrcnt == 0 && !F_ISSET(sp, SC_EX_GLOBAL)) {
564 ecp->addrcnt = 1;
565 ecp->addr1.lno = sp->lno + 1;
566 ecp->addr1.cno = sp->cno;
569 F_SET(ecp, E_USELASTCMD);
573 * !!!
574 * Historically, the number option applied to both ex and vi. One
575 * strangeness was that ex didn't switch display formats until a
576 * command was entered, e.g. <CR>'s after the set didn't change to
577 * the new format, but :1p would.
579 if (O_ISSET(sp, O_NUMBER)) {
580 F_SET(ecp, E_OPTNUM);
581 FL_SET(ecp->iflags, E_C_HASH);
582 } else
583 F_CLR(ecp, E_OPTNUM);
585 /* Check for ex mode legality. */
586 if (F_ISSET(sp, SC_EX) && (F_ISSET(ecp->cmd, E_VIONLY) || newscreen)) {
587 msgq(sp, M_ERR,
588 "082|%s: command not available in ex mode", ecp->cmd->name);
589 goto err;
592 /* Add standard command flags. */
593 F_SET(ecp, ecp->cmd->flags);
594 if (!newscreen)
595 F_CLR(ecp, E_NEWSCREEN);
598 * There are three normal termination cases for an ex command. They
599 * are the end of the string (ecp->clen), or unescaped (by <literal
600 * next> characters) <newline> or '|' characters. As we're now past
601 * possible addresses, we can determine how long the command is, so we
602 * don't have to look for all the possible terminations. Naturally,
603 * there are some exciting special cases:
605 * 1: The bang, global, v and the filter versions of the read and
606 * write commands are delimited by <newline>s (they can contain
607 * shell pipes).
608 * 2: The ex, edit, next and visual in vi mode commands all take ex
609 * commands as their first arguments.
610 * 3: The s command takes an RE as its first argument, and wants it
611 * to be specially delimited.
613 * Historically, '|' characters in the first argument of the ex, edit,
614 * next, vi visual, and s commands didn't delimit the command. And,
615 * in the filter cases for read and write, and the bang, global and v
616 * commands, they did not delimit the command at all.
618 * For example, the following commands were legal:
620 * :edit +25|s/abc/ABC/ file.c
621 * :s/|/PIPE/
622 * :read !spell % | columnate
623 * :global/pattern/p|l
625 * It's not quite as simple as it sounds, however. The command:
627 * :s/a/b/|s/c/d|set
629 * was also legal, i.e. the historic ex parser (using the word loosely,
630 * since "parser" implies some regularity of syntax) delimited the RE's
631 * based on its delimiter and not anything so irretrievably vulgar as a
632 * command syntax.
634 * Anyhow, the following code makes this all work. First, for the
635 * special cases we move past their special argument(s). Then, we
636 * do normal command processing on whatever is left. Barf-O-Rama.
638 discard = 0; /* Characters discarded from the command. */
639 arg1_len = 0;
640 ecp->save_cmd = ecp->cp;
641 if (ecp->cmd == &cmds[C_EDIT] || ecp->cmd == &cmds[C_EX] ||
642 ecp->cmd == &cmds[C_NEXT] || ecp->cmd == &cmds[C_VISUAL_VI] ||
643 ecp->cmd == &cmds[C_VSPLIT]) {
645 * Move to the next non-whitespace character. A '!'
646 * immediately following the command is eaten as a
647 * force flag.
649 if (ecp->clen > 0 && *ecp->cp == '!') {
650 ++ecp->cp;
651 --ecp->clen;
652 FL_SET(ecp->iflags, E_C_FORCE);
654 /* Reset, don't reparse. */
655 ecp->save_cmd = ecp->cp;
657 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
658 if (!ISBLANK(*ecp->cp))
659 break;
661 * QUOTING NOTE:
663 * The historic implementation ignored all escape characters
664 * so there was no way to put a space or newline into the +cmd
665 * field. We do a simplistic job of fixing it by moving to the
666 * first whitespace character that isn't escaped. The escaping
667 * characters are stripped as no longer useful.
669 if (ecp->clen > 0 && *ecp->cp == '+') {
670 ++ecp->cp;
671 --ecp->clen;
672 for (arg1 = p = ecp->cp;
673 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
674 ch = *ecp->cp;
675 if (IS_ESCAPE(sp, ecp, ch) &&
676 ecp->clen > 1) {
677 ++discard;
678 --ecp->clen;
679 ch = *++ecp->cp;
680 } else if (ISBLANK(ch))
681 break;
682 *p++ = ch;
684 arg1_len = ecp->cp - arg1;
686 /* Reset, so the first argument isn't reparsed. */
687 ecp->save_cmd = ecp->cp;
689 } else if (ecp->cmd == &cmds[C_BANG] ||
690 ecp->cmd == &cmds[C_GLOBAL] || ecp->cmd == &cmds[C_V]) {
692 * QUOTING NOTE:
694 * We use backslashes to escape <newline> characters, although
695 * this wasn't historic practice for the bang command. It was
696 * for the global and v commands, and it's common usage when
697 * doing text insert during the command. Escaping characters
698 * are stripped as no longer useful.
700 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
701 ch = *ecp->cp;
702 if (ch == '\\' && ecp->clen > 1 && ecp->cp[1] == '\n') {
703 ++discard;
704 --ecp->clen;
705 ch = *++ecp->cp;
707 ++wp->if_lno;
708 ++ecp->if_lno;
709 } else if (ch == '\n')
710 break;
711 *p++ = ch;
713 } else if (ecp->cmd == &cmds[C_READ] || ecp->cmd == &cmds[C_WRITE]) {
715 * For write commands, if the next character is a <blank>, and
716 * the next non-blank character is a '!', it's a filter command
717 * and we want to eat everything up to the <newline>. For read
718 * commands, if the next non-blank character is a '!', it's a
719 * filter command and we want to eat everything up to the next
720 * <newline>. Otherwise, we're done.
722 for (tmp = 0; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
723 ch = *ecp->cp;
724 if (ISBLANK(ch))
725 tmp = 1;
726 else
727 break;
729 if (ecp->clen > 0 && ch == '!' &&
730 (ecp->cmd == &cmds[C_READ] || tmp))
731 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
732 if (ecp->cp[0] == '\n')
733 break;
734 } else if (ecp->cmd == &cmds[C_SUBSTITUTE]) {
736 * Move to the next non-whitespace character, we'll use it as
737 * the delimiter. If the character isn't an alphanumeric or
738 * a '|', it's the delimiter, so parse it. Otherwise, we're
739 * into something like ":s g", so use the special s command.
741 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
742 if (!ISBLANK(ecp->cp[0]))
743 break;
745 if (ISALNUM(ecp->cp[0]) || ecp->cp[0] == '|') {
746 ecp->rcmd = cmds[C_SUBSTITUTE];
747 ecp->rcmd.fn = ex_subagain;
748 ecp->cmd = &ecp->rcmd;
749 } else if (ecp->clen > 0) {
751 * QUOTING NOTE:
753 * Backslashes quote delimiter characters for RE's.
754 * The backslashes are NOT removed since they'll be
755 * used by the RE code. Move to the third delimiter
756 * that's not escaped (or the end of the command).
758 delim = *ecp->cp;
759 ++ecp->cp;
760 --ecp->clen;
761 for (cnt = 2; ecp->clen > 0 &&
762 cnt != 0; --ecp->clen, ++ecp->cp)
763 if (ecp->cp[0] == '\\' &&
764 ecp->clen > 1) {
765 ++ecp->cp;
766 --ecp->clen;
767 } else if (ecp->cp[0] == delim)
768 --cnt;
773 * Use normal quoting and termination rules to find the end of this
774 * command.
776 * QUOTING NOTE:
778 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
779 * file. It was almost certainly a bug, but that's what bug-for-bug
780 * compatibility means, Grasshopper. Also, ^V's escape the command
781 * delimiters. Literal next quote characters in front of the newlines,
782 * '|' characters or literal next characters are stripped as they're
783 * no longer useful.
785 vi_address = ecp->clen != 0 && ecp->cp[0] != '\n';
786 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
787 ch = ecp->cp[0];
788 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
789 CHAR_T tmp = ecp->cp[1];
790 if (tmp == '\n' || tmp == '|') {
791 if (tmp == '\n') {
792 ++wp->if_lno;
793 ++ecp->if_lno;
795 ++discard;
796 --ecp->clen;
797 ++ecp->cp;
798 ch = tmp;
800 } else if (ch == '\n' || ch == '|') {
801 if (ch == '\n')
802 F_SET(ecp, E_NEWLINE);
803 --ecp->clen;
804 break;
806 *p++ = ch;
810 * Save off the next command information, go back to the
811 * original start of the command.
813 p = ecp->cp + 1;
814 ecp->cp = ecp->save_cmd;
815 ecp->save_cmd = p;
816 ecp->save_cmdlen = ecp->clen;
817 ecp->clen = ((ecp->save_cmd - ecp->cp) - 1) - discard;
820 * QUOTING NOTE:
822 * The "set tags" command historically used a backslash, not the
823 * user's literal next character, to escape whitespace. Handle
824 * it here instead of complicating the argv_exp3() code. Note,
825 * this isn't a particularly complex trap, and if backslashes were
826 * legal in set commands, this would have to be much more complicated.
828 if (ecp->cmd == &cmds[C_SET])
829 for (p = ecp->cp, len = ecp->clen; len > 0; --len, ++p)
830 if (*p == '\\')
831 *p = CH_LITERAL;
834 * Set the default addresses. It's an error to specify an address for
835 * a command that doesn't take them. If two addresses are specified
836 * for a command that only takes one, lose the first one. Two special
837 * cases here, some commands take 0 or 2 addresses. For most of them
838 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
839 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
841 * Also, if the file is empty, some commands want to use an address of
842 * 0, i.e. the entire file is 0 to 0, and the default first address is
843 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
844 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
845 * case where the 0 address is only valid if it's a default address.
847 * Also, set a flag if we set the default addresses. Some commands
848 * (ex: z) care if the user specified an address or if we just used
849 * the current cursor.
851 switch (F_ISSET(ecp, E_ADDR1 | E_ADDR2 | E_ADDR2_ALL | E_ADDR2_NONE)) {
852 case E_ADDR1: /* One address: */
853 switch (ecp->addrcnt) {
854 case 0: /* Default cursor/empty file. */
855 ecp->addrcnt = 1;
856 F_SET(ecp, E_ADDR_DEF);
857 if (F_ISSET(ecp, E_ADDR_ZERODEF)) {
858 if (db_last(sp, &lno))
859 goto err;
860 if (lno == 0) {
861 ecp->addr1.lno = 0;
862 F_SET(ecp, E_ADDR_ZERO);
863 } else
864 ecp->addr1.lno = sp->lno;
865 } else
866 ecp->addr1.lno = sp->lno;
867 ecp->addr1.cno = sp->cno;
868 break;
869 case 1:
870 break;
871 case 2: /* Lose the first address. */
872 ecp->addrcnt = 1;
873 ecp->addr1 = ecp->addr2;
875 break;
876 case E_ADDR2_NONE: /* Zero/two addresses: */
877 if (ecp->addrcnt == 0) /* Default to nothing. */
878 break;
879 goto two_addr;
880 case E_ADDR2_ALL: /* Zero/two addresses: */
881 if (ecp->addrcnt == 0) { /* Default entire/empty file. */
882 F_SET(ecp, E_ADDR_DEF);
883 ecp->addrcnt = 2;
884 if (sp->ep == NULL)
885 ecp->addr2.lno = 0;
886 else if (db_last(sp, &ecp->addr2.lno))
887 goto err;
888 if (F_ISSET(ecp, E_ADDR_ZERODEF) &&
889 ecp->addr2.lno == 0) {
890 ecp->addr1.lno = 0;
891 F_SET(ecp, E_ADDR_ZERO);
892 } else
893 ecp->addr1.lno = 1;
894 ecp->addr1.cno = ecp->addr2.cno = 0;
895 F_SET(ecp, E_ADDR2_ALL);
896 break;
898 /* FALLTHROUGH */
899 case E_ADDR2: /* Two addresses: */
900 two_addr: switch (ecp->addrcnt) {
901 case 0: /* Default cursor/empty file. */
902 ecp->addrcnt = 2;
903 F_SET(ecp, E_ADDR_DEF);
904 if (sp->lno == 1 &&
905 F_ISSET(ecp, E_ADDR_ZERODEF)) {
906 if (db_last(sp, &lno))
907 goto err;
908 if (lno == 0) {
909 ecp->addr1.lno = ecp->addr2.lno = 0;
910 F_SET(ecp, E_ADDR_ZERO);
911 } else
912 ecp->addr1.lno =
913 ecp->addr2.lno = sp->lno;
914 } else
915 ecp->addr1.lno = ecp->addr2.lno = sp->lno;
916 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
917 break;
918 case 1: /* Default to first address. */
919 //ecp->addrcnt = 2; /* XXX Was this needed ??? */
920 ecp->addr2 = ecp->addr1;
921 break;
922 case 2:
923 break;
925 break;
926 default:
927 if (ecp->addrcnt) /* Error. */
928 goto usage;
932 * !!!
933 * The ^D scroll command historically scrolled the value of the scroll
934 * option or to EOF. It was an error if the cursor was already at EOF.
935 * (Leading addresses were permitted, but were then ignored.)
937 if (ecp->cmd == &cmds[C_SCROLL]) {
938 ecp->addrcnt = 2;
939 ecp->addr1.lno = sp->lno + 1;
940 ecp->addr2.lno = sp->lno + O_VAL(sp, O_SCROLL);
941 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
942 if (db_last(sp, &lno))
943 goto err;
944 if (lno != 0 && lno > sp->lno && ecp->addr2.lno > lno)
945 ecp->addr2.lno = lno;
948 ecp->flagoff = 0;
949 for (np = ecp->cmd->syntax; *np != '\0'; ++np) {
951 * The force flag is sensitive to leading whitespace, i.e.
952 * "next !" is different from "next!". Handle it before
953 * skipping leading <blank>s.
955 if (*np == '!') {
956 if (ecp->clen > 0 && *ecp->cp == '!') {
957 ++ecp->cp;
958 --ecp->clen;
959 FL_SET(ecp->iflags, E_C_FORCE);
961 continue;
964 /* Skip leading <blank>s. */
965 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
966 if (!ISBLANK(*ecp->cp))
967 break;
968 if (ecp->clen == 0)
969 break;
971 switch (*np) {
972 case '1': /* +, -, #, l, p */
974 * !!!
975 * Historically, some flags were ignored depending
976 * on where they occurred in the command line. For
977 * example, in the command, ":3+++p--#", historic vi
978 * acted on the '#' flag, but ignored the '-' flags.
979 * It's unambiguous what the flags mean, so we just
980 * handle them regardless of the stupidity of their
981 * location.
983 for (; ecp->clen; --ecp->clen, ++ecp->cp)
984 switch (*ecp->cp) {
985 case '+':
986 ++ecp->flagoff;
987 break;
988 case '-':
989 case '^':
990 --ecp->flagoff;
991 break;
992 case '#':
993 F_CLR(ecp, E_OPTNUM);
994 FL_SET(ecp->iflags, E_C_HASH);
995 exp->fdef |= E_C_HASH;
996 break;
997 case 'l':
998 FL_SET(ecp->iflags, E_C_LIST);
999 exp->fdef |= E_C_LIST;
1000 break;
1001 case 'p':
1002 FL_SET(ecp->iflags, E_C_PRINT);
1003 exp->fdef |= E_C_PRINT;
1004 break;
1005 default:
1006 goto end_case1;
1008 end_case1: break;
1009 case '2': /* -, ., +, ^ */
1010 case '3': /* -, ., +, ^, = */
1011 for (; ecp->clen; --ecp->clen, ++ecp->cp)
1012 switch (*ecp->cp) {
1013 case '-':
1014 FL_SET(ecp->iflags, E_C_DASH);
1015 break;
1016 case '.':
1017 FL_SET(ecp->iflags, E_C_DOT);
1018 break;
1019 case '+':
1020 FL_SET(ecp->iflags, E_C_PLUS);
1021 break;
1022 case '^':
1023 FL_SET(ecp->iflags, E_C_CARAT);
1024 break;
1025 case '=':
1026 if (*np == '3') {
1027 FL_SET(ecp->iflags, E_C_EQUAL);
1028 break;
1030 /* FALLTHROUGH */
1031 default:
1032 goto end_case23;
1034 end_case23: break;
1035 case 'b': /* buffer */
1037 * !!!
1038 * Historically, "d #" was a delete with a flag, not a
1039 * delete into the '#' buffer. If the current command
1040 * permits a flag, don't use one as a buffer. However,
1041 * the 'l' and 'p' flags were legal buffer names in the
1042 * historic ex, and were used as buffers, not flags.
1044 if ((ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1045 ecp->cp[0] == '^' || ecp->cp[0] == '#') &&
1046 strchr(np, '1') != NULL)
1047 break;
1049 * !!!
1050 * Digits can't be buffer names in ex commands, or the
1051 * command "d2" would be a delete into buffer '2', and
1052 * not a two-line deletion.
1054 if (!ISDIGIT(ecp->cp[0])) {
1055 ecp->buffer = *ecp->cp;
1056 ++ecp->cp;
1057 --ecp->clen;
1058 FL_SET(ecp->iflags, E_C_BUFFER);
1060 break;
1061 case 'c': /* count [01+a] */
1062 ++np;
1063 /* Validate any signed value. */
1064 if (!ISDIGIT(*ecp->cp) && (*np != '+' ||
1065 (*ecp->cp != '+' && *ecp->cp != '-')))
1066 break;
1067 /* If a signed value, set appropriate flags. */
1068 if (*ecp->cp == '-')
1069 FL_SET(ecp->iflags, E_C_COUNT_NEG);
1070 else if (*ecp->cp == '+')
1071 FL_SET(ecp->iflags, E_C_COUNT_POS);
1072 if ((nret =
1073 nget_slong(sp, &ltmp, ecp->cp, &t, 10)) != NUM_OK) {
1074 ex_badaddr(sp, NULL, A_NOTSET, nret);
1075 goto err;
1077 if (ltmp == 0 && *np != '0') {
1078 msgq(sp, M_ERR, "083|Count may not be zero");
1079 goto err;
1081 ecp->clen -= (t - ecp->cp);
1082 ecp->cp = t;
1085 * Counts as address offsets occur in commands taking
1086 * two addresses. Historic vi practice was to use
1087 * the count as an offset from the *second* address.
1089 * Set a count flag; some underlying commands (see
1090 * join) do different things with counts than with
1091 * line addresses.
1093 if (*np == 'a') {
1094 ecp->addr1 = ecp->addr2;
1095 ecp->addr2.lno = ecp->addr1.lno + ltmp - 1;
1096 } else
1097 ecp->count = ltmp;
1098 FL_SET(ecp->iflags, E_C_COUNT);
1099 break;
1100 case 'f': /* file */
1101 if (argv_exp2(sp, ecp, ecp->cp, ecp->clen))
1102 goto err;
1103 goto arg_cnt_chk;
1104 case 'l': /* line */
1106 * Get a line specification.
1108 * If the line was a search expression, we may have
1109 * changed state during the call, and we're now
1110 * searching the file. Push ourselves onto the state
1111 * stack.
1113 if (ex_line(sp, ecp, &cur, &isaddr, &tmp))
1114 goto rfail;
1115 if (tmp)
1116 goto err;
1118 /* Line specifications are always required. */
1119 if (!isaddr) {
1120 msgq_wstr(sp, M_ERR, ecp->cp,
1121 "084|%s: bad line specification");
1122 goto err;
1125 * The target line should exist for these commands,
1126 * but 0 is legal for them as well.
1128 if (cur.lno != 0 && !db_exist(sp, cur.lno)) {
1129 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1130 goto err;
1132 ecp->lineno = cur.lno;
1133 break;
1134 case 'S': /* string, file exp. */
1135 if (ecp->clen != 0) {
1136 if (argv_exp1(sp, ecp, ecp->cp,
1137 ecp->clen, ecp->cmd == &cmds[C_BANG]))
1138 goto err;
1139 goto addr_verify;
1141 /* FALLTHROUGH */
1142 case 's': /* string */
1143 if (argv_exp0(sp, ecp, ecp->cp, ecp->clen))
1144 goto err;
1145 goto addr_verify;
1146 case 'W': /* word string */
1148 * QUOTING NOTE:
1150 * Literal next characters escape the following
1151 * character. Quoting characters are stripped here
1152 * since they are no longer useful.
1154 * First there was the word.
1156 for (p = t = ecp->cp;
1157 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
1158 ch = *ecp->cp;
1159 if (IS_ESCAPE(sp,
1160 ecp, ch) && ecp->clen > 1) {
1161 --ecp->clen;
1162 *p++ = *++ecp->cp;
1163 } else if (ISBLANK(ch)) {
1164 ++ecp->cp;
1165 --ecp->clen;
1166 break;
1167 } else
1168 *p++ = ch;
1170 if (argv_exp0(sp, ecp, t, p - t))
1171 goto err;
1173 /* Delete intervening whitespace. */
1174 for (; ecp->clen > 0;
1175 --ecp->clen, ++ecp->cp) {
1176 ch = *ecp->cp;
1177 if (!ISBLANK(ch))
1178 break;
1180 if (ecp->clen == 0)
1181 goto usage;
1183 /* Followed by the string. */
1184 for (p = t = ecp->cp; ecp->clen > 0;
1185 --ecp->clen, ++ecp->cp, ++p) {
1186 ch = *ecp->cp;
1187 if (IS_ESCAPE(sp,
1188 ecp, ch) && ecp->clen > 1) {
1189 --ecp->clen;
1190 *p = *++ecp->cp;
1191 } else
1192 *p = ch;
1194 if (argv_exp0(sp, ecp, t, p - t))
1195 goto err;
1196 goto addr_verify;
1197 case 'w': /* word */
1198 if (argv_exp3(sp, ecp, ecp->cp, ecp->clen))
1199 goto err;
1200 arg_cnt_chk: if (*++np != 'N') { /* N */
1202 * If a number is specified, must either be
1203 * 0 or that number, if optional, and that
1204 * number, if required.
1206 tmp = *np - '0';
1207 if ((*++np != 'o' || exp->argsoff != 0) &&
1208 exp->argsoff != tmp)
1209 goto usage;
1211 goto addr_verify;
1212 default:
1213 msgq(sp, M_ERR,
1214 "085|Internal syntax table error (%s: %s)",
1215 ecp->cmd->name, KEY_NAME(sp, *np));
1219 /* Skip trailing whitespace. */
1220 for (; ecp->clen > 0; --ecp->clen) {
1221 ch = *ecp->cp++;
1222 if (!ISBLANK(ch))
1223 break;
1227 * There shouldn't be anything left, and no more required fields,
1228 * i.e neither 'l' or 'r' in the syntax string.
1230 if (ecp->clen != 0 || strpbrk(np, "lr")) {
1231 usage: msgq(sp, M_ERR, "086|Usage: %s", ecp->cmd->usage);
1232 goto err;
1236 * Verify that the addresses are legal. Check the addresses here,
1237 * because this is a place where all ex addresses pass through.
1238 * (They don't all pass through ex_line(), for instance.) We're
1239 * assuming that any non-existent line doesn't exist because it's
1240 * past the end-of-file. That's a pretty good guess.
1242 * If it's a "default vi command", an address of zero is okay.
1244 addr_verify:
1245 switch (ecp->addrcnt) {
1246 case 2:
1248 * Historic ex/vi permitted commands with counts to go past
1249 * EOF. So, for example, if the file only had 5 lines, the
1250 * ex command "1,6>" would fail, but the command ">300"
1251 * would succeed. Since we don't want to have to make all
1252 * of the underlying commands handle random line numbers,
1253 * fix it here.
1255 if (ecp->addr2.lno == 0) {
1256 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1257 (F_ISSET(sp, SC_EX) ||
1258 !F_ISSET(ecp, E_USELASTCMD))) {
1259 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1260 goto err;
1262 } else if (!db_exist(sp, ecp->addr2.lno)) {
1263 if (FL_ISSET(ecp->iflags, E_C_COUNT)) {
1264 if (db_last(sp, &lno))
1265 goto err;
1266 ecp->addr2.lno = lno;
1267 } else {
1268 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1269 goto err;
1272 /* FALLTHROUGH */
1273 case 1:
1274 if (ecp->addr1.lno == 0) {
1275 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1276 (F_ISSET(sp, SC_EX) ||
1277 !F_ISSET(ecp, E_USELASTCMD))) {
1278 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1279 goto err;
1281 } else if (!db_exist(sp, ecp->addr1.lno)) {
1282 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1283 goto err;
1285 break;
1289 * If doing a default command and there's nothing left on the line,
1290 * vi just moves to the line. For example, ":3" and ":'a,'b" just
1291 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1293 * !!!
1294 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1295 * the line.
1297 * !!!
1298 * This is done before the absolute mark gets set; historically,
1299 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1301 if ((F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_NOPRDEF)) &&
1302 F_ISSET(ecp, E_USELASTCMD) && vi_address == 0) {
1303 switch (ecp->addrcnt) {
1304 case 2:
1305 if (sp->lno !=
1306 (ecp->addr2.lno ? ecp->addr2.lno : 1)) {
1307 sp->lno =
1308 ecp->addr2.lno ? ecp->addr2.lno : 1;
1309 sp->cno = 0;
1310 (void)nonblank(sp, sp->lno, &sp->cno);
1312 break;
1313 case 1:
1314 if (sp->lno !=
1315 (ecp->addr1.lno ? ecp->addr1.lno : 1)) {
1316 sp->lno =
1317 ecp->addr1.lno ? ecp->addr1.lno : 1;
1318 sp->cno = 0;
1319 (void)nonblank(sp, sp->lno, &sp->cno);
1321 break;
1323 ecp->cp = ecp->save_cmd;
1324 ecp->clen = ecp->save_cmdlen;
1325 goto loop;
1329 * Set the absolute mark -- we have to set it for vi here, in case
1330 * it's a compound command, e.g. ":5p|6" should set the absolute
1331 * mark for vi.
1333 if (F_ISSET(ecp, E_ABSMARK)) {
1334 cur.lno = sp->lno;
1335 cur.cno = sp->cno;
1336 F_CLR(ecp, E_ABSMARK);
1337 if (mark_set(sp, ABSMARK1, &cur, 1))
1338 goto err;
1341 #if defined(DEBUG) && defined(COMLOG)
1342 ex_comlog(sp, ecp);
1343 #endif
1344 /* Increment the command count if not called from vi. */
1345 if (F_ISSET(sp, SC_EX))
1346 ++sp->ccnt;
1349 * If file state available, and not doing a global command,
1350 * log the start of an action.
1352 if (sp->ep != NULL && !F_ISSET(sp, SC_EX_GLOBAL))
1353 (void)log_cursor(sp);
1356 * !!!
1357 * There are two special commands for the purposes of this code: the
1358 * default command (<carriage-return>) or the scrolling commands (^D
1359 * and <EOF>) as the first non-<blank> characters in the line.
1361 * If this is the first command in the command line, we received the
1362 * command from the ex command loop and we're talking to a tty, and
1363 * and there's nothing else on the command line, and it's one of the
1364 * special commands, we move back up to the previous line, and erase
1365 * the prompt character with the output. Since ex runs in canonical
1366 * mode, we don't have to do anything else, a <newline> has already
1367 * been echoed by the tty driver. It's OK if vi calls us -- we won't
1368 * be in ex mode so we'll do nothing.
1370 if (F_ISSET(ecp, E_NRSEP)) {
1371 if (sp->ep != NULL &&
1372 F_ISSET(sp, SC_EX) && !F_ISSET(gp, G_SCRIPTED) &&
1373 (F_ISSET(ecp, E_USELASTCMD) || ecp->cmd == &cmds[C_SCROLL]))
1374 gp->scr_ex_adjust(sp, EX_TERM_SCROLL);
1375 F_CLR(ecp, E_NRSEP);
1379 * Call the underlying function for the ex command.
1381 * XXX
1382 * Interrupts behave like errors, for now.
1384 if (ecp->cmd->fn(sp, ecp) || INTERRUPTED(sp)) {
1385 if (F_ISSET(gp, G_SCRIPTED))
1386 F_SET(sp, SC_EXIT_FORCE);
1387 goto err;
1390 #ifdef DEBUG
1391 /* Make sure no function left global temporary space locked. */
1392 if (F_ISSET(wp, W_TMP_INUSE)) {
1393 F_CLR(wp, W_TMP_INUSE);
1394 msgq(sp, M_ERR, "087|%s: temporary buffer not released",
1395 ecp->cmd->name);
1397 #endif
1399 * Ex displayed the number of lines modified immediately after each
1400 * command, so the command "1,10d|1,10d" would display:
1402 * 10 lines deleted
1403 * 10 lines deleted
1404 * <autoprint line>
1406 * Executing ex commands from vi only reported the final modified
1407 * lines message -- that's wrong enough that we don't match it.
1409 if (F_ISSET(sp, SC_EX))
1410 mod_rpt(sp);
1413 * Integrate any offset parsed by the underlying command, and make
1414 * sure the referenced line exists.
1416 * XXX
1417 * May not match historic practice (which I've never been able to
1418 * completely figure out.) For example, the '=' command from vi
1419 * mode often got the offset wrong, and complained it was too large,
1420 * but didn't seem to have a problem with the cursor. If anyone
1421 * complains, ask them how it's supposed to work, they might know.
1423 if (sp->ep != NULL && ecp->flagoff) {
1424 if (ecp->flagoff < 0) {
1425 if (sp->lno <= -ecp->flagoff) {
1426 msgq(sp, M_ERR,
1427 "088|Flag offset to before line 1");
1428 goto err;
1430 } else {
1431 if (!NPFITS(DB_MAX_RECORDS, sp->lno, ecp->flagoff)) {
1432 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1433 goto err;
1435 if (!db_exist(sp, sp->lno + ecp->flagoff)) {
1436 msgq(sp, M_ERR,
1437 "089|Flag offset past end-of-file");
1438 goto err;
1441 sp->lno += ecp->flagoff;
1445 * If the command executed successfully, we may want to display a line
1446 * based on the autoprint option or an explicit print flag. (Make sure
1447 * that there's a line to display.) Also, the autoprint edit option is
1448 * turned off for the duration of global commands.
1450 if (F_ISSET(sp, SC_EX) && sp->ep != NULL && sp->lno != 0) {
1452 * The print commands have already handled the `print' flags.
1453 * If so, clear them.
1455 if (FL_ISSET(ecp->iflags, E_CLRFLAG))
1456 FL_CLR(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT);
1458 /* If hash set only because of the number option, discard it. */
1459 if (F_ISSET(ecp, E_OPTNUM))
1460 FL_CLR(ecp->iflags, E_C_HASH);
1463 * If there was an explicit flag to display the new cursor line,
1464 * or autoprint is set and a change was made, display the line.
1465 * If any print flags were set use them, else default to print.
1467 LF_INIT(FL_ISSET(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT));
1468 if (!LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT | E_NOAUTO) &&
1469 !F_ISSET(sp, SC_EX_GLOBAL) &&
1470 O_ISSET(sp, O_AUTOPRINT) && F_ISSET(ecp, E_AUTOPRINT))
1471 LF_INIT(E_C_PRINT);
1473 if (LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT)) {
1474 cur.lno = sp->lno;
1475 cur.cno = 0;
1476 (void)ex_print(sp, ecp, &cur, &cur, flags);
1481 * If the command had an associated "+cmd", it has to be executed
1482 * before we finish executing any more of this ex command. For
1483 * example, consider a .exrc file that contains the following lines:
1485 * :set all
1486 * :edit +25 file.c|s/abc/ABC/|1
1487 * :3,5 print
1489 * This can happen more than once -- the historic vi simply hung or
1490 * dropped core, of course. Prepend the + command back into the
1491 * current command and continue. We may have to add an additional
1492 * <literal next> character. We know that it will fit because we
1493 * discarded at least one space and the + character.
1495 if (arg1_len != 0) {
1497 * If the last character of the + command was a <literal next>
1498 * character, it would be treated differently because of the
1499 * append. Quote it, if necessary.
1501 if (IS_ESCAPE(sp, ecp, arg1[arg1_len - 1])) {
1502 *--ecp->save_cmd = CH_LITERAL;
1503 ++ecp->save_cmdlen;
1506 ecp->save_cmd -= arg1_len;
1507 ecp->save_cmdlen += arg1_len;
1508 MEMCPYW(ecp->save_cmd, arg1, arg1_len);
1511 * Any commands executed from a +cmd are executed starting at
1512 * the first column of the last line of the file -- NOT the
1513 * first nonblank.) The main file startup code doesn't know
1514 * that a +cmd was set, however, so it may have put us at the
1515 * top of the file. (Note, this is safe because we must have
1516 * switched files to get here.)
1518 F_SET(ecp, E_MOVETOEND);
1521 /* Update the current command. */
1522 ecp->cp = ecp->save_cmd;
1523 ecp->clen = ecp->save_cmdlen;
1526 * !!!
1527 * If we've changed screens or underlying files, any pending global or
1528 * v command, or @ buffer that has associated addresses, has to be
1529 * discarded. This is historic practice for globals, and necessary for
1530 * @ buffers that had associated addresses.
1532 * Otherwise, if we've changed underlying files, it's not a problem,
1533 * we continue with the rest of the ex command(s), operating on the
1534 * new file. However, if we switch screens (either by exiting or by
1535 * an explicit command), we have no way of knowing where to put output
1536 * messages, and, since we don't control screens here, we could screw
1537 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1538 * times). So, return and continue after we've got a new screen.
1540 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH)) {
1541 at_found = gv_found = 0;
1542 for (ecp = wp->ecq.lh_first;
1543 ecp != NULL; ecp = ecp->q.le_next)
1544 switch (ecp->agv_flags) {
1545 case 0:
1546 case AGV_AT_NORANGE:
1547 break;
1548 case AGV_AT:
1549 if (!at_found) {
1550 at_found = 1;
1551 msgq(sp, M_ERR,
1552 "090|@ with range running when the file/screen changed");
1554 break;
1555 case AGV_GLOBAL:
1556 case AGV_V:
1557 if (!gv_found) {
1558 gv_found = 1;
1559 msgq(sp, M_ERR,
1560 "091|Global/v command running when the file/screen changed");
1562 break;
1563 default:
1564 abort();
1566 if (at_found || gv_found)
1567 goto discard;
1568 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_SSWITCH))
1569 goto rsuccess;
1572 goto loop;
1573 /* NOTREACHED */
1575 err: /*
1576 * On command failure, we discard keys and pending commands remaining,
1577 * as well as any keys that were mapped and waiting. The save_cmdlen
1578 * test is not necessarily correct. If we fail early enough we don't
1579 * know if the entire string was a single command or not. Guess, as
1580 * it's useful to know if commands other than the current one are being
1581 * discarded.
1583 if (ecp->save_cmdlen == 0)
1584 for (; ecp->clen; --ecp->clen) {
1585 ch = *ecp->cp++;
1586 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
1587 --ecp->clen;
1588 ++ecp->cp;
1589 } else if (ch == '\n' || ch == '|') {
1590 if (ecp->clen > 1)
1591 ecp->save_cmdlen = 1;
1592 break;
1595 if (ecp->save_cmdlen != 0 || wp->ecq.lh_first != &wp->excmd) {
1596 discard: msgq(sp, M_BERR,
1597 "092|Ex command failed: pending commands discarded");
1598 ex_discard(sp);
1600 if (v_event_flush(sp, CH_MAPPED))
1601 msgq(sp, M_BERR,
1602 "093|Ex command failed: mapped keys discarded");
1604 rfail: tmp = 1;
1605 if (0)
1606 rsuccess: tmp = 0;
1608 /* Turn off any file name error information. */
1609 wp->if_name = NULL;
1611 /* Turn off the global bit. */
1612 F_CLR(sp, SC_EX_GLOBAL);
1614 return (tmp);
1618 * ex_range --
1619 * Get a line range for ex commands, or perform a vi ex address search.
1621 * PUBLIC: int ex_range __P((SCR *, EXCMD *, int *));
1624 ex_range(SCR *sp, EXCMD *ecp, int *errp)
1626 enum { ADDR_FOUND, ADDR_NEED, ADDR_NONE } addr;
1627 GS *gp;
1628 EX_PRIVATE *exp;
1629 MARK m;
1630 int isaddr;
1632 *errp = 0;
1635 * Parse comma or semi-colon delimited line specs.
1637 * Semi-colon delimiters update the current address to be the last
1638 * address. For example, the command
1640 * :3;/pattern/ecp->cp
1642 * will search for pattern from line 3. In addition, if ecp->cp
1643 * is not a valid command, the current line will be left at 3, not
1644 * at the original address.
1646 * Extra addresses are discarded, starting with the first.
1648 * !!!
1649 * If any addresses are missing, they default to the current line.
1650 * This was historically true for both leading and trailing comma
1651 * delimited addresses as well as for trailing semicolon delimited
1652 * addresses. For consistency, we make it true for leading semicolon
1653 * addresses as well.
1655 gp = sp->gp;
1656 exp = EXP(sp);
1657 for (addr = ADDR_NONE, ecp->addrcnt = 0; ecp->clen > 0;)
1658 switch (*ecp->cp) {
1659 case '%': /* Entire file. */
1660 /* Vi ex address searches didn't permit % signs. */
1661 if (F_ISSET(ecp, E_VISEARCH))
1662 goto ret;
1664 /* It's an error if the file is empty. */
1665 if (sp->ep == NULL) {
1666 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1667 *errp = 1;
1668 return (0);
1671 * !!!
1672 * A percent character addresses all of the lines in
1673 * the file. Historically, it couldn't be followed by
1674 * any other address. We do it as a text substitution
1675 * for simplicity. POSIX 1003.2 is expected to follow
1676 * this practice.
1678 * If it's an empty file, the first line is 0, not 1.
1680 if (addr == ADDR_FOUND) {
1681 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1682 *errp = 1;
1683 return (0);
1685 if (db_last(sp, &ecp->addr2.lno))
1686 return (1);
1687 ecp->addr1.lno = ecp->addr2.lno == 0 ? 0 : 1;
1688 ecp->addr1.cno = ecp->addr2.cno = 0;
1689 ecp->addrcnt = 2;
1690 addr = ADDR_FOUND;
1691 ++ecp->cp;
1692 --ecp->clen;
1693 break;
1694 case ',': /* Comma delimiter. */
1695 /* Vi ex address searches didn't permit commas. */
1696 if (F_ISSET(ecp, E_VISEARCH))
1697 goto ret;
1698 /* FALLTHROUGH */
1699 case ';': /* Semi-colon delimiter. */
1700 if (sp->ep == NULL) {
1701 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1702 *errp = 1;
1703 return (0);
1705 if (addr != ADDR_FOUND)
1706 switch (ecp->addrcnt) {
1707 case 0:
1708 ecp->addr1.lno = sp->lno;
1709 ecp->addr1.cno = sp->cno;
1710 ecp->addrcnt = 1;
1711 break;
1712 case 2:
1713 ecp->addr1 = ecp->addr2;
1714 /* FALLTHROUGH */
1715 case 1:
1716 ecp->addr2.lno = sp->lno;
1717 ecp->addr2.cno = sp->cno;
1718 ecp->addrcnt = 2;
1719 break;
1721 if (*ecp->cp == ';')
1722 switch (ecp->addrcnt) {
1723 case 0:
1724 abort();
1725 /* NOTREACHED */
1726 case 1:
1727 sp->lno = ecp->addr1.lno;
1728 sp->cno = ecp->addr1.cno;
1729 break;
1730 case 2:
1731 sp->lno = ecp->addr2.lno;
1732 sp->cno = ecp->addr2.cno;
1733 break;
1735 addr = ADDR_NEED;
1736 /* FALLTHROUGH */
1737 case ' ': /* Whitespace. */
1738 case '\t': /* Whitespace. */
1739 ++ecp->cp;
1740 --ecp->clen;
1741 break;
1742 default:
1743 /* Get a line specification. */
1744 if (ex_line(sp, ecp, &m, &isaddr, errp))
1745 return (1);
1746 if (*errp)
1747 return (0);
1748 if (!isaddr)
1749 goto ret;
1750 if (addr == ADDR_FOUND) {
1751 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1752 *errp = 1;
1753 return (0);
1755 switch (ecp->addrcnt) {
1756 case 0:
1757 ecp->addr1 = m;
1758 ecp->addrcnt = 1;
1759 break;
1760 case 1:
1761 ecp->addr2 = m;
1762 ecp->addrcnt = 2;
1763 break;
1764 case 2:
1765 ecp->addr1 = ecp->addr2;
1766 ecp->addr2 = m;
1767 break;
1769 addr = ADDR_FOUND;
1770 break;
1774 * !!!
1775 * Vi ex address searches are indifferent to order or trailing
1776 * semi-colons.
1778 ret: if (F_ISSET(ecp, E_VISEARCH))
1779 return (0);
1781 if (addr == ADDR_NEED)
1782 switch (ecp->addrcnt) {
1783 case 0:
1784 ecp->addr1.lno = sp->lno;
1785 ecp->addr1.cno = sp->cno;
1786 ecp->addrcnt = 1;
1787 break;
1788 case 2:
1789 ecp->addr1 = ecp->addr2;
1790 /* FALLTHROUGH */
1791 case 1:
1792 ecp->addr2.lno = sp->lno;
1793 ecp->addr2.cno = sp->cno;
1794 ecp->addrcnt = 2;
1795 break;
1798 if (ecp->addrcnt == 2 && ecp->addr2.lno < ecp->addr1.lno) {
1799 msgq(sp, M_ERR,
1800 "094|The second address is smaller than the first");
1801 *errp = 1;
1803 return (0);
1807 * ex_line --
1808 * Get a single line address specifier.
1810 * The way the "previous context" mark worked was that any "non-relative"
1811 * motion set it. While ex/vi wasn't totally consistent about this, ANY
1812 * numeric address, search pattern, '$', or mark reference in an address
1813 * was considered non-relative, and set the value. Which should explain
1814 * why we're hacking marks down here. The problem was that the mark was
1815 * only set if the command was called, i.e. we have to set a flag and test
1816 * it later.
1818 * XXX
1819 * This is probably still not exactly historic practice, although I think
1820 * it's fairly close.
1822 static int
1823 ex_line(SCR *sp, EXCMD *ecp, MARK *mp, int *isaddrp, int *errp)
1825 enum nresult nret;
1826 EX_PRIVATE *exp;
1827 GS *gp;
1828 long total, val;
1829 int isneg;
1830 int (*sf) __P((SCR *, MARK *, MARK *, CHAR_T *, size_t, CHAR_T **, u_int));
1831 CHAR_T *endp;
1833 gp = sp->gp;
1834 exp = EXP(sp);
1836 *isaddrp = *errp = 0;
1837 F_CLR(ecp, E_DELTA);
1839 /* No addresses permitted until a file has been read in. */
1840 if (sp->ep == NULL && strchr("$0123456789'\\/?.+-^", *ecp->cp)) {
1841 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1842 *errp = 1;
1843 return (0);
1846 switch (*ecp->cp) {
1847 case '$': /* Last line in the file. */
1848 *isaddrp = 1;
1849 F_SET(ecp, E_ABSMARK);
1851 mp->cno = 0;
1852 if (db_last(sp, &mp->lno))
1853 return (1);
1854 ++ecp->cp;
1855 --ecp->clen;
1856 break; /* Absolute line number. */
1857 case '0': case '1': case '2': case '3': case '4':
1858 case '5': case '6': case '7': case '8': case '9':
1859 *isaddrp = 1;
1860 F_SET(ecp, E_ABSMARK);
1862 if ((nret = nget_slong(sp, &val, ecp->cp, &endp, 10)) != NUM_OK) {
1863 ex_badaddr(sp, NULL, A_NOTSET, nret);
1864 *errp = 1;
1865 return (0);
1867 if (!NPFITS(DB_MAX_RECORDS, 0, val)) {
1868 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1869 *errp = 1;
1870 return (0);
1872 mp->lno = val;
1873 mp->cno = 0;
1874 ecp->clen -= (endp - ecp->cp);
1875 ecp->cp = endp;
1876 break;
1877 case '\'': /* Use a mark. */
1878 *isaddrp = 1;
1879 F_SET(ecp, E_ABSMARK);
1881 if (ecp->clen == 1) {
1882 msgq(sp, M_ERR, "095|No mark name supplied");
1883 *errp = 1;
1884 return (0);
1886 if (mark_get(sp, ecp->cp[1], mp, M_ERR)) {
1887 *errp = 1;
1888 return (0);
1890 ecp->cp += 2;
1891 ecp->clen -= 2;
1892 break;
1893 case '\\': /* Search: forward/backward. */
1895 * !!!
1896 * I can't find any difference between // and \/ or between
1897 * ?? and \?. Mark Horton doesn't remember there being any
1898 * difference. C'est la vie.
1900 if (ecp->clen < 2 ||
1901 ecp->cp[1] != '/' && ecp->cp[1] != '?') {
1902 msgq(sp, M_ERR, "096|\\ not followed by / or ?");
1903 *errp = 1;
1904 return (0);
1906 ++ecp->cp;
1907 --ecp->clen;
1908 sf = ecp->cp[0] == '/' ? f_search : b_search;
1909 goto search;
1910 case '/': /* Search forward. */
1911 sf = f_search;
1912 goto search;
1913 case '?': /* Search backward. */
1914 sf = b_search;
1916 search: mp->lno = sp->lno;
1917 mp->cno = sp->cno;
1918 if (sf(sp, mp, mp, ecp->cp, ecp->clen, &endp,
1919 SEARCH_MSG | SEARCH_PARSE | SEARCH_SET |
1920 (F_ISSET(ecp, E_SEARCH_WMSG) ? SEARCH_WMSG : 0))) {
1921 *errp = 1;
1922 return (0);
1925 /* Fix up the command pointers. */
1926 ecp->clen -= (endp - ecp->cp);
1927 ecp->cp = endp;
1929 *isaddrp = 1;
1930 F_SET(ecp, E_ABSMARK);
1931 break;
1932 case '.': /* Current position. */
1933 *isaddrp = 1;
1934 mp->cno = sp->cno;
1936 /* If an empty file, then '.' is 0, not 1. */
1937 if (sp->lno == 1) {
1938 if (db_last(sp, &mp->lno))
1939 return (1);
1940 if (mp->lno != 0)
1941 mp->lno = 1;
1942 } else
1943 mp->lno = sp->lno;
1946 * !!!
1947 * Historically, .<number> was the same as .+<number>, i.e.
1948 * the '+' could be omitted. (This feature is found in ed
1949 * as well.)
1951 if (ecp->clen > 1 && ISDIGIT(ecp->cp[1]))
1952 *ecp->cp = '+';
1953 else {
1954 ++ecp->cp;
1955 --ecp->clen;
1957 break;
1960 /* Skip trailing <blank>s. */
1961 for (; ecp->clen > 0 &&
1962 ISBLANK(ecp->cp[0]); ++ecp->cp, --ecp->clen);
1965 * Evaluate any offset. If no address yet found, the offset
1966 * is relative to ".".
1968 total = 0;
1969 if (ecp->clen != 0 && (ISDIGIT(ecp->cp[0]) ||
1970 ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1971 ecp->cp[0] == '^')) {
1972 if (!*isaddrp) {
1973 *isaddrp = 1;
1974 mp->lno = sp->lno;
1975 mp->cno = sp->cno;
1978 * Evaluate an offset, defined as:
1980 * [+-^<blank>]*[<blank>]*[0-9]*
1982 * The rough translation is any number of signs, optionally
1983 * followed by numbers, or a number by itself, all <blank>
1984 * separated.
1986 * !!!
1987 * All address offsets were additive, e.g. "2 2 3p" was the
1988 * same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2".
1989 * Note, however, "2 /ZZZ/" was an error. It was also legal
1990 * to insert signs without numbers, so "3 - 2" was legal, and
1991 * equal to 4.
1993 * !!!
1994 * Offsets were historically permitted for any line address,
1995 * e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after
1996 * line 8.
1998 * !!!
1999 * Offsets were historically permitted for search commands,
2000 * and handled as addresses: "/pattern/2 2 2" was legal, and
2001 * referenced the 6th line after pattern.
2003 F_SET(ecp, E_DELTA);
2004 for (;;) {
2005 for (; ecp->clen > 0 && ISBLANK(ecp->cp[0]);
2006 ++ecp->cp, --ecp->clen);
2007 if (ecp->clen == 0 || !ISDIGIT(ecp->cp[0]) &&
2008 ecp->cp[0] != '+' && ecp->cp[0] != '-' &&
2009 ecp->cp[0] != '^')
2010 break;
2011 if (!ISDIGIT(ecp->cp[0]) &&
2012 !ISDIGIT(ecp->cp[1])) {
2013 total += ecp->cp[0] == '+' ? 1 : -1;
2014 --ecp->clen;
2015 ++ecp->cp;
2016 } else {
2017 if (ecp->cp[0] == '-' ||
2018 ecp->cp[0] == '^') {
2019 ++ecp->cp;
2020 --ecp->clen;
2021 isneg = 1;
2022 } else
2023 isneg = 0;
2025 /* Get a signed long, add it to the total. */
2026 if ((nret = nget_slong(sp, &val,
2027 ecp->cp, &endp, 10)) != NUM_OK ||
2028 (nret = NADD_SLONG(sp,
2029 total, val)) != NUM_OK) {
2030 ex_badaddr(sp, NULL, A_NOTSET, nret);
2031 *errp = 1;
2032 return (0);
2034 total += isneg ? -val : val;
2035 ecp->clen -= (endp - ecp->cp);
2036 ecp->cp = endp;
2042 * Any value less than 0 is an error. Make sure that the new value
2043 * will fit into a db_recno_t.
2045 if (*isaddrp && total != 0) {
2046 if (total < 0) {
2047 if (-total > mp->lno) {
2048 msgq(sp, M_ERR,
2049 "097|Reference to a line number less than 0");
2050 *errp = 1;
2051 return (0);
2053 } else
2054 if (!NPFITS(DB_MAX_RECORDS, mp->lno, total)) {
2055 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
2056 *errp = 1;
2057 return (0);
2059 mp->lno += total;
2061 return (0);
2066 * ex_load --
2067 * Load up the next command, which may be an @ buffer or global command.
2069 static int
2070 ex_load(SCR *sp)
2072 WIN *wp;
2073 EXCMD *ecp;
2074 RANGE *rp;
2076 F_CLR(sp, SC_EX_GLOBAL);
2079 * Lose any exhausted commands. We know that the first command
2080 * can't be an AGV command, which makes things a bit easier.
2082 for (wp = sp->wp;;) {
2084 * If we're back to the original structure, leave it around,
2085 * but discard any allocated source name, we've returned to
2086 * the beginning of the command stack.
2088 if ((ecp = wp->ecq.lh_first) == &wp->excmd) {
2089 if (F_ISSET(ecp, E_NAMEDISCARD)) {
2090 free(ecp->if_name);
2091 ecp->if_name = NULL;
2093 return (0);
2097 * ecp->clen will be 0 for the first discarded command, but
2098 * may not be 0 for subsequent ones, e.g. if the original
2099 * command was ":g/xx/@a|s/b/c/", then when we discard the
2100 * command pushed on the stack by the @a, we have to resume
2101 * the global command which included the substitute command.
2103 if (ecp->clen != 0)
2104 return (0);
2107 * If it's an @, global or v command, we may need to continue
2108 * the command on a different line.
2110 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2111 /* Discard any exhausted ranges. */
2112 while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq)
2113 if (rp->start > rp->stop) {
2114 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
2115 free(rp);
2116 } else
2117 break;
2119 /* If there's another range, continue with it. */
2120 if (rp != (void *)&ecp->rq)
2121 break;
2123 /* If it's a global/v command, fix up the last line. */
2124 if (FL_ISSET(ecp->agv_flags,
2125 AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO)
2126 if (db_exist(sp, ecp->range_lno))
2127 sp->lno = ecp->range_lno;
2128 else {
2129 if (db_last(sp, &sp->lno))
2130 return (1);
2131 if (sp->lno == 0)
2132 sp->lno = 1;
2134 free(ecp->o_cp);
2137 /* Discard the EXCMD. */
2138 LIST_REMOVE(ecp, q);
2139 free(ecp);
2143 * We only get here if it's an active @, global or v command. Set
2144 * the current line number, and get a new copy of the command for
2145 * the parser. Note, the original pointer almost certainly moved,
2146 * so we have play games.
2148 ecp->cp = ecp->o_cp;
2149 MEMCPYW(ecp->cp, ecp->cp + ecp->o_clen, ecp->o_clen);
2150 ecp->clen = ecp->o_clen;
2151 ecp->range_lno = sp->lno = rp->start++;
2153 if (FL_ISSET(ecp->agv_flags, AGV_GLOBAL | AGV_V))
2154 F_SET(sp, SC_EX_GLOBAL);
2155 return (0);
2159 * ex_discard --
2160 * Discard any pending ex commands.
2162 static int
2163 ex_discard(SCR *sp)
2165 WIN *wp;
2166 EXCMD *ecp;
2167 RANGE *rp;
2170 * We know the first command can't be an AGV command, so we don't
2171 * process it specially. We do, however, nail the command itself.
2173 for (wp = sp->wp; (ecp = wp->ecq.lh_first) != &wp->excmd;) {
2174 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2175 while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq) {
2176 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
2177 free(rp);
2179 free(ecp->o_cp);
2181 LIST_REMOVE(ecp, q);
2182 free(ecp);
2184 wp->ecq.lh_first->clen = 0;
2185 return (0);
2189 * ex_unknown --
2190 * Display an unknown command name.
2192 static void
2193 ex_unknown(SCR *sp, CHAR_T *cmd, size_t len)
2195 size_t blen;
2196 CHAR_T *bp;
2198 GET_SPACE_GOTOW(sp, bp, blen, len + 1);
2199 bp[len] = '\0';
2200 MEMCPYW(bp, cmd, len);
2201 msgq_wstr(sp, M_ERR, bp, "098|The %s command is unknown");
2202 FREE_SPACEW(sp, bp, blen);
2204 alloc_err:
2205 return;
2209 * ex_is_abbrev -
2210 * The vi text input routine needs to know if ex thinks this is an
2211 * [un]abbreviate command, so it can turn off abbreviations. See
2212 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2214 * PUBLIC: int ex_is_abbrev __P((SCR *, CHAR_T *, size_t));
2217 ex_is_abbrev(SCR *sp, CHAR_T *name, size_t len)
2219 EXCMDLIST const *cp;
2221 return ((cp = ex_comm_search(sp, name, len)) != NULL &&
2222 (cp == &cmds[C_ABBR] || cp == &cmds[C_UNABBREVIATE]));
2226 * ex_is_unmap -
2227 * The vi text input routine needs to know if ex thinks this is an
2228 * unmap command, so it can turn off input mapping. See the usual
2229 * ranting in the vi/v_txt_ev.c:txt_unmap() routine.
2231 * PUBLIC: int ex_is_unmap __P((SCR *, CHAR_T *, size_t));
2234 ex_is_unmap(SCR *sp, CHAR_T *name, size_t len)
2236 EXCMDLIST const *cp;
2239 * The command the vi input routines are really interested in
2240 * is "unmap!", not just unmap.
2242 if (name[len - 1] != '!')
2243 return (0);
2244 --len;
2245 return ((cp = ex_comm_search(sp, name, len)) != NULL &&
2246 cp == &cmds[C_UNMAP]);
2250 * ex_comm_search --
2251 * Search for a command name.
2253 static EXCMDLIST const *
2254 ex_comm_search(SCR *sp, CHAR_T *name, size_t len)
2256 EXCMDLIST const *cp;
2258 for (cp = cmds; cp->name != NULL; ++cp) {
2259 if (cp->name[0] > name[0])
2260 return (NULL);
2261 if (cp->name[0] != name[0])
2262 continue;
2263 if (!MEMCMP(name, cp->name, len))
2264 return (cp);
2266 return (NULL);
2270 * ex_badaddr --
2271 * Display a bad address message.
2273 * PUBLIC: void ex_badaddr
2274 * PUBLIC: __P((SCR *, EXCMDLIST const *, enum badaddr, enum nresult));
2276 void
2277 ex_badaddr(SCR *sp, const EXCMDLIST *cp, enum badaddr ba, enum nresult nret)
2279 db_recno_t lno;
2281 switch (nret) {
2282 case NUM_OK:
2283 break;
2284 case NUM_ERR:
2285 msgq(sp, M_SYSERR, NULL);
2286 return;
2287 case NUM_OVER:
2288 msgq(sp, M_ERR, "099|Address value overflow");
2289 return;
2290 case NUM_UNDER:
2291 msgq(sp, M_ERR, "100|Address value underflow");
2292 return;
2296 * When encountering an address error, tell the user if there's no
2297 * underlying file, that's the real problem.
2299 if (sp->ep == NULL) {
2300 ex_wemsg(sp, cp ? cp->name : NULL, EXM_NOFILEYET);
2301 return;
2304 switch (ba) {
2305 case A_COMBO:
2306 msgq(sp, M_ERR, "101|Illegal address combination");
2307 break;
2308 case A_EOF:
2309 if (db_last(sp, &lno))
2310 return;
2311 if (lno != 0) {
2312 msgq(sp, M_ERR,
2313 "102|Illegal address: only %lu lines in the file",
2314 lno);
2315 break;
2317 /* FALLTHROUGH */
2318 case A_EMPTY:
2319 msgq(sp, M_ERR, "103|Illegal address: the file is empty");
2320 break;
2321 case A_NOTSET:
2322 abort();
2323 /* NOTREACHED */
2324 case A_ZERO:
2325 msgq(sp, M_ERR,
2326 "104|The %s command doesn't permit an address of 0",
2327 cp->name);
2328 break;
2330 return;
2333 #if defined(DEBUG) && defined(COMLOG)
2335 * ex_comlog --
2336 * Log ex commands.
2338 static void
2339 ex_comlog(sp, ecp)
2340 SCR *sp;
2341 EXCMD *ecp;
2343 vtrace(sp, "ecmd: %s", ecp->cmd->name);
2344 if (ecp->addrcnt > 0) {
2345 vtrace(sp, " a1 %d", ecp->addr1.lno);
2346 if (ecp->addrcnt > 1)
2347 vtrace(sp, " a2: %d", ecp->addr2.lno);
2349 if (ecp->lineno)
2350 vtrace(sp, " line %d", ecp->lineno);
2351 if (ecp->flags)
2352 vtrace(sp, " flags 0x%x", ecp->flags);
2353 if (F_ISSET(&exc, E_BUFFER))
2354 vtrace(sp, " buffer %c", ecp->buffer);
2355 if (ecp->argc)
2356 for (cnt = 0; cnt < ecp->argc; ++cnt)
2357 vtrace(sp, " arg %d: {%s}", cnt, ecp->argv[cnt]->bp);
2358 vtrace(sp, "\n");
2360 #endif