o nvi failed to correctly handle EINTR returned from tcsetattr(), exiting
[nvi.git] / ex / ex.c
blob85f2969b7ac50b839ecc63defc98376ea2bf9970
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.74 2001/12/16 18:18:54 skimo Exp $ (Berkeley) $Date: 2001/12/16 18:18:54 $";
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 return (screen_end(sp));
168 return (0);
172 * ex_cmd --
173 * The guts of the ex parser: parse and execute a string containing
174 * ex commands.
176 * !!!
177 * This code MODIFIES the string that gets passed in, to delete quoting
178 * characters, etc. The string cannot be readonly/text space, nor should
179 * you expect to use it again after ex_cmd() returns.
181 * !!!
182 * For the fun of it, if you want to see if a vi clone got the ex argument
183 * parsing right, try:
185 * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
186 * vi
187 * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
189 * or: vi
190 * :set|file|append|set|file
192 * For extra credit, try them in a startup .exrc file.
194 * PUBLIC: int ex_cmd __P((SCR *));
197 ex_cmd(SCR *sp)
199 enum nresult nret;
200 EX_PRIVATE *exp;
201 EXCMD *ecp;
202 GS *gp;
203 WIN *wp;
204 MARK cur;
205 db_recno_t lno;
206 size_t arg1_len, discard, len;
207 u_int32_t flags;
208 long ltmp;
209 int at_found, gv_found;
210 int cnt, delim, isaddr, namelen;
211 int newscreen, notempty, tmp, vi_address;
212 CHAR_T *arg1, *s, *p, *t;
213 CHAR_T ch;
214 CHAR_T *n;
215 char *np;
217 gp = sp->gp;
218 wp = sp->wp;
219 exp = EXP(sp);
222 * We always start running the command on the top of the stack.
223 * This means that *everything* must be resolved when we leave
224 * this function for any reason.
226 loop: ecp = wp->ecq.lh_first;
228 /* If we're reading a command from a file, set up error information. */
229 if (ecp->if_name != NULL) {
230 wp->if_lno = ecp->if_lno;
231 wp->if_name = ecp->if_name;
235 * If a move to the end of the file is scheduled for this command,
236 * do it now.
238 if (F_ISSET(ecp, E_MOVETOEND)) {
239 if (db_last(sp, &sp->lno))
240 goto rfail;
241 sp->cno = 0;
242 F_CLR(ecp, E_MOVETOEND);
245 /* If we found a newline, increment the count now. */
246 if (F_ISSET(ecp, E_NEWLINE)) {
247 ++wp->if_lno;
248 ++ecp->if_lno;
249 F_CLR(ecp, E_NEWLINE);
252 /* (Re)initialize the EXCMD structure, preserving some flags. */
253 CLEAR_EX_CMD(ecp);
255 /* Initialize the argument structures. */
256 if (argv_init(sp, ecp))
257 goto err;
259 /* Initialize +cmd, saved command information. */
260 arg1 = NULL;
261 ecp->save_cmdlen = 0;
263 /* Skip <blank>s, empty lines. */
264 for (notempty = 0; ecp->clen > 0; ++ecp->cp, --ecp->clen)
265 if ((ch = *ecp->cp) == '\n') {
266 ++wp->if_lno;
267 ++ecp->if_lno;
268 } else if (ISBLANK(ch))
269 notempty = 1;
270 else
271 break;
274 * !!!
275 * Permit extra colons at the start of the line. Historically,
276 * ex/vi allowed a single extra one. It's simpler not to count.
277 * The stripping is done here because, historically, any command
278 * could have preceding colons, e.g. ":g/pattern/:p" worked.
280 if (ecp->clen != 0 && ch == ':') {
281 notempty = 1;
282 while (--ecp->clen > 0 && (ch = *++ecp->cp) == ':');
286 * Command lines that start with a double-quote are comments.
288 * !!!
289 * Historically, there was no escape or delimiter for a comment, e.g.
290 * :"foo|set was a single comment and nothing was output. Since nvi
291 * permits users to escape <newline> characters into command lines, we
292 * have to check for that case.
294 if (ecp->clen != 0 && ch == '"') {
295 while (--ecp->clen > 0 && *++ecp->cp != '\n');
296 if (*ecp->cp == '\n') {
297 F_SET(ecp, E_NEWLINE);
298 ++ecp->cp;
299 --ecp->clen;
301 goto loop;
304 /* Skip whitespace. */
305 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
306 ch = *ecp->cp;
307 if (!ISBLANK(ch))
308 break;
312 * The last point at which an empty line can mean do nothing.
314 * !!!
315 * Historically, in ex mode, lines containing only <blank> characters
316 * were the same as a single <carriage-return>, i.e. a default command.
317 * In vi mode, they were ignored. In .exrc files this was a serious
318 * annoyance, as vi kept trying to treat them as print commands. We
319 * ignore backward compatibility in this case, discarding lines that
320 * contain only <blank> characters from .exrc files.
322 * !!!
323 * This is where you end up when you're done a command, i.e. clen has
324 * gone to zero. Continue if there are more commands to run.
326 if (ecp->clen == 0 &&
327 (!notempty || F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_BLIGNORE))) {
328 if (ex_load(sp))
329 goto rfail;
330 ecp = wp->ecq.lh_first;
331 if (ecp->clen == 0)
332 goto rsuccess;
333 goto loop;
337 * Check to see if this is a command for which we may want to move
338 * the cursor back up to the previous line. (The command :1<CR>
339 * wants a <newline> separator, but the command :<CR> wants to erase
340 * the command line.) If the line is empty except for <blank>s,
341 * <carriage-return> or <eof>, we'll probably want to move up. I
342 * don't think there's any way to get <blank> characters *after* the
343 * command character, but this is the ex parser, and I've been wrong
344 * before.
346 if (F_ISSET(ecp, E_NRSEP) &&
347 ecp->clen != 0 && (ecp->clen != 1 || ecp->cp[0] != '\004'))
348 F_CLR(ecp, E_NRSEP);
350 /* Parse command addresses. */
351 if (ex_range(sp, ecp, &tmp))
352 goto rfail;
353 if (tmp)
354 goto err;
357 * Skip <blank>s and any more colons (the command :3,5:print
358 * worked, historically).
360 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
361 ch = *ecp->cp;
362 if (!ISBLANK(ch) && ch != ':')
363 break;
367 * If no command, ex does the last specified of p, l, or #, and vi
368 * moves to the line. Otherwise, determine the length of the command
369 * name by looking for the first non-alphabetic character. (There
370 * are a few non-alphabetic characters in command names, but they're
371 * all single character commands.) This isn't a great test, because
372 * it means that, for the command ":e +cut.c file", we'll report that
373 * the command "cut" wasn't known. However, it makes ":e+35 file" work
374 * correctly.
376 * !!!
377 * Historically, lines with multiple adjacent (or <blank> separated)
378 * command separators were very strange. For example, the command
379 * |||<carriage-return>, when the cursor was on line 1, displayed
380 * lines 2, 3 and 5 of the file. In addition, the command " | "
381 * would only display the line after the next line, instead of the
382 * next two lines. No ideas why. It worked reasonably when executed
383 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
384 * command for each separator.
386 #define SINGLE_CHAR_COMMANDS "\004!#&*<=>@~"
387 newscreen = 0;
388 if (ecp->clen != 0 && ecp->cp[0] != '|' && ecp->cp[0] != '\n') {
389 if (strchr(SINGLE_CHAR_COMMANDS, *ecp->cp)) {
390 p = ecp->cp;
391 ++ecp->cp;
392 --ecp->clen;
393 namelen = 1;
394 } else {
395 for (p = ecp->cp;
396 ecp->clen > 0; --ecp->clen, ++ecp->cp)
397 if (!ISALPHA(*ecp->cp))
398 break;
399 if ((namelen = ecp->cp - p) == 0) {
400 msgq(sp, M_ERR, "080|Unknown command name");
401 goto err;
406 * !!!
407 * Historic vi permitted flags to immediately follow any
408 * subset of the 'delete' command, but then did not permit
409 * further arguments (flag, buffer, count). Make it work.
410 * Permit further arguments for the few shreds of dignity
411 * it offers.
413 * Adding commands that start with 'd', and match "delete"
414 * up to a l, p, +, - or # character can break this code.
416 * !!!
417 * Capital letters beginning the command names ex, edit,
418 * next, previous, tag and visual (in vi mode) indicate the
419 * command should happen in a new screen.
421 switch (p[0]) {
422 case 'd':
423 for (s = p,
424 n = cmds[C_DELETE].name; *s == *n; ++s, ++n);
425 if (s[0] == 'l' || s[0] == 'p' || s[0] == '+' ||
426 s[0] == '-' || s[0] == '^' || s[0] == '#') {
427 len = (ecp->cp - p) - (s - p);
428 ecp->cp -= len;
429 ecp->clen += len;
430 ecp->rcmd = cmds[C_DELETE];
431 ecp->rcmd.syntax = "1bca1";
432 ecp->cmd = &ecp->rcmd;
433 goto skip_srch;
435 break;
436 case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
437 newscreen = 1;
438 p[0] = tolower(p[0]);
439 break;
443 * Search the table for the command.
445 * !!!
446 * Historic vi permitted the mark to immediately follow the
447 * 'k' in the 'k' command. Make it work.
449 * !!!
450 * Historic vi permitted any flag to follow the s command, e.g.
451 * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
452 * Since the following characters all have to be flags, i.e.
453 * alphabetics, we can let the s command routine return errors
454 * if it was some illegal command string. This code will break
455 * if an "sg" or similar command is ever added. The substitute
456 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
457 * follows the 's', but we limit the choices here to "cgr" so
458 * that we get unknown command messages for wrong combinations.
460 if ((ecp->cmd = ex_comm_search(sp, p, namelen)) == NULL)
461 switch (p[0]) {
462 case 'k':
463 if (namelen == 2) {
464 ecp->cp -= namelen - 1;
465 ecp->clen += namelen - 1;
466 ecp->cmd = &cmds[C_K];
467 break;
469 goto unknown;
470 case 's':
471 for (s = p + 1, cnt = namelen; --cnt; ++s)
472 if (s[0] != 'c' &&
473 s[0] != 'g' && s[0] != 'r')
474 break;
475 if (cnt == 0) {
476 ecp->cp -= namelen - 1;
477 ecp->clen += namelen - 1;
478 ecp->rcmd = cmds[C_SUBSTITUTE];
479 ecp->rcmd.fn = ex_subagain;
480 ecp->cmd = &ecp->rcmd;
481 break;
483 /* FALLTHROUGH */
484 default:
485 unknown: if (newscreen)
486 p[0] = toupper(p[0]);
487 ex_unknown(sp, p, namelen);
488 goto err;
492 * The visual command has a different syntax when called
493 * from ex than when called from a vi colon command. FMH.
494 * Make the change now, before we test for the newscreen
495 * semantic, so that we're testing the right one.
497 skip_srch: if (ecp->cmd == &cmds[C_VISUAL_EX] && F_ISSET(sp, SC_VI))
498 ecp->cmd = &cmds[C_VISUAL_VI];
501 * !!!
502 * Historic vi permitted a capital 'P' at the beginning of
503 * any command that started with 'p'. Probably wanted the
504 * P[rint] command for backward compatibility, and the code
505 * just made Preserve and Put work by accident. Nvi uses
506 * Previous to mean previous-in-a-new-screen, so be careful.
508 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN) &&
509 (ecp->cmd == &cmds[C_PRINT] ||
510 ecp->cmd == &cmds[C_PRESERVE]))
511 newscreen = 0;
513 /* Test for a newscreen associated with this command. */
514 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN))
515 goto unknown;
517 /* Secure means no shell access. */
518 if (F_ISSET(ecp->cmd, E_SECURE) && O_ISSET(sp, O_SECURE)) {
519 ex_wemsg(sp, ecp->cmd->name, EXM_SECURE);
520 goto err;
524 * Multiple < and > characters; another "feature". Note,
525 * The string passed to the underlying function may not be
526 * nul terminated in this case.
528 if ((ecp->cmd == &cmds[C_SHIFTL] && *p == '<') ||
529 (ecp->cmd == &cmds[C_SHIFTR] && *p == '>')) {
530 for (ch = *p;
531 ecp->clen > 0; --ecp->clen, ++ecp->cp)
532 if (*ecp->cp != ch)
533 break;
534 if (argv_exp0(sp, ecp, p, ecp->cp - p))
535 goto err;
538 /* Set the format style flags for the next command. */
539 if (ecp->cmd == &cmds[C_HASH])
540 exp->fdef = E_C_HASH;
541 else if (ecp->cmd == &cmds[C_LIST])
542 exp->fdef = E_C_LIST;
543 else if (ecp->cmd == &cmds[C_PRINT])
544 exp->fdef = E_C_PRINT;
545 F_CLR(ecp, E_USELASTCMD);
546 } else {
547 /* Print is the default command. */
548 ecp->cmd = &cmds[C_PRINT];
550 /* Set the saved format flags. */
551 F_SET(ecp, exp->fdef);
554 * !!!
555 * If no address was specified, and it's not a global command,
556 * we up the address by one. (I have no idea why globals are
557 * exempted, but it's (ahem) historic practice.)
559 if (ecp->addrcnt == 0 && !F_ISSET(sp, SC_EX_GLOBAL)) {
560 ecp->addrcnt = 1;
561 ecp->addr1.lno = sp->lno + 1;
562 ecp->addr1.cno = sp->cno;
565 F_SET(ecp, E_USELASTCMD);
569 * !!!
570 * Historically, the number option applied to both ex and vi. One
571 * strangeness was that ex didn't switch display formats until a
572 * command was entered, e.g. <CR>'s after the set didn't change to
573 * the new format, but :1p would.
575 if (O_ISSET(sp, O_NUMBER)) {
576 F_SET(ecp, E_OPTNUM);
577 FL_SET(ecp->iflags, E_C_HASH);
578 } else
579 F_CLR(ecp, E_OPTNUM);
581 /* Check for ex mode legality. */
582 if (F_ISSET(sp, SC_EX) && (F_ISSET(ecp->cmd, E_VIONLY) || newscreen)) {
583 msgq(sp, M_ERR,
584 "082|%s: command not available in ex mode", ecp->cmd->name);
585 goto err;
588 /* Add standard command flags. */
589 F_SET(ecp, ecp->cmd->flags);
590 if (!newscreen)
591 F_CLR(ecp, E_NEWSCREEN);
594 * There are three normal termination cases for an ex command. They
595 * are the end of the string (ecp->clen), or unescaped (by <literal
596 * next> characters) <newline> or '|' characters. As we're now past
597 * possible addresses, we can determine how long the command is, so we
598 * don't have to look for all the possible terminations. Naturally,
599 * there are some exciting special cases:
601 * 1: The bang, global, v and the filter versions of the read and
602 * write commands are delimited by <newline>s (they can contain
603 * shell pipes).
604 * 2: The ex, edit, next and visual in vi mode commands all take ex
605 * commands as their first arguments.
606 * 3: The s command takes an RE as its first argument, and wants it
607 * to be specially delimited.
609 * Historically, '|' characters in the first argument of the ex, edit,
610 * next, vi visual, and s commands didn't delimit the command. And,
611 * in the filter cases for read and write, and the bang, global and v
612 * commands, they did not delimit the command at all.
614 * For example, the following commands were legal:
616 * :edit +25|s/abc/ABC/ file.c
617 * :s/|/PIPE/
618 * :read !spell % | columnate
619 * :global/pattern/p|l
621 * It's not quite as simple as it sounds, however. The command:
623 * :s/a/b/|s/c/d|set
625 * was also legal, i.e. the historic ex parser (using the word loosely,
626 * since "parser" implies some regularity of syntax) delimited the RE's
627 * based on its delimiter and not anything so irretrievably vulgar as a
628 * command syntax.
630 * Anyhow, the following code makes this all work. First, for the
631 * special cases we move past their special argument(s). Then, we
632 * do normal command processing on whatever is left. Barf-O-Rama.
634 discard = 0; /* Characters discarded from the command. */
635 arg1_len = 0;
636 ecp->save_cmd = ecp->cp;
637 if (ecp->cmd == &cmds[C_EDIT] || ecp->cmd == &cmds[C_EX] ||
638 ecp->cmd == &cmds[C_NEXT] || ecp->cmd == &cmds[C_VISUAL_VI] ||
639 ecp->cmd == &cmds[C_VSPLIT]) {
641 * Move to the next non-whitespace character. A '!'
642 * immediately following the command is eaten as a
643 * force flag.
645 if (ecp->clen > 0 && *ecp->cp == '!') {
646 ++ecp->cp;
647 --ecp->clen;
648 FL_SET(ecp->iflags, E_C_FORCE);
650 /* Reset, don't reparse. */
651 ecp->save_cmd = ecp->cp;
653 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
654 if (!ISBLANK(*ecp->cp))
655 break;
657 * QUOTING NOTE:
659 * The historic implementation ignored all escape characters
660 * so there was no way to put a space or newline into the +cmd
661 * field. We do a simplistic job of fixing it by moving to the
662 * first whitespace character that isn't escaped. The escaping
663 * characters are stripped as no longer useful.
665 if (ecp->clen > 0 && *ecp->cp == '+') {
666 ++ecp->cp;
667 --ecp->clen;
668 for (arg1 = p = ecp->cp;
669 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
670 ch = *ecp->cp;
671 if (IS_ESCAPE(sp, ecp, ch) &&
672 ecp->clen > 1) {
673 ++discard;
674 --ecp->clen;
675 ch = *++ecp->cp;
676 } else if (ISBLANK(ch))
677 break;
678 *p++ = ch;
680 arg1_len = ecp->cp - arg1;
682 /* Reset, so the first argument isn't reparsed. */
683 ecp->save_cmd = ecp->cp;
685 } else if (ecp->cmd == &cmds[C_BANG] ||
686 ecp->cmd == &cmds[C_GLOBAL] || ecp->cmd == &cmds[C_V]) {
688 * QUOTING NOTE:
690 * We use backslashes to escape <newline> characters, although
691 * this wasn't historic practice for the bang command. It was
692 * for the global and v commands, and it's common usage when
693 * doing text insert during the command. Escaping characters
694 * are stripped as no longer useful.
696 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
697 ch = *ecp->cp;
698 if (ch == '\\' && ecp->clen > 1 && ecp->cp[1] == '\n') {
699 ++discard;
700 --ecp->clen;
701 ch = *++ecp->cp;
703 ++wp->if_lno;
704 ++ecp->if_lno;
705 } else if (ch == '\n')
706 break;
707 *p++ = ch;
709 } else if (ecp->cmd == &cmds[C_READ] || ecp->cmd == &cmds[C_WRITE]) {
711 * For write commands, if the next character is a <blank>, and
712 * the next non-blank character is a '!', it's a filter command
713 * and we want to eat everything up to the <newline>. For read
714 * commands, if the next non-blank character is a '!', it's a
715 * filter command and we want to eat everything up to the next
716 * <newline>. Otherwise, we're done.
718 for (tmp = 0; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
719 ch = *ecp->cp;
720 if (ISBLANK(ch))
721 tmp = 1;
722 else
723 break;
725 if (ecp->clen > 0 && ch == '!' &&
726 (ecp->cmd == &cmds[C_READ] || tmp))
727 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
728 if (ecp->cp[0] == '\n')
729 break;
730 } else if (ecp->cmd == &cmds[C_SUBSTITUTE]) {
732 * Move to the next non-whitespace character, we'll use it as
733 * the delimiter. If the character isn't an alphanumeric or
734 * a '|', it's the delimiter, so parse it. Otherwise, we're
735 * into something like ":s g", so use the special s command.
737 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
738 if (!ISBLANK(ecp->cp[0]))
739 break;
741 if (ISALNUM(ecp->cp[0]) || ecp->cp[0] == '|') {
742 ecp->rcmd = cmds[C_SUBSTITUTE];
743 ecp->rcmd.fn = ex_subagain;
744 ecp->cmd = &ecp->rcmd;
745 } else if (ecp->clen > 0) {
747 * QUOTING NOTE:
749 * Backslashes quote delimiter characters for RE's.
750 * The backslashes are NOT removed since they'll be
751 * used by the RE code. Move to the third delimiter
752 * that's not escaped (or the end of the command).
754 delim = *ecp->cp;
755 ++ecp->cp;
756 --ecp->clen;
757 for (cnt = 2; ecp->clen > 0 &&
758 cnt != 0; --ecp->clen, ++ecp->cp)
759 if (ecp->cp[0] == '\\' &&
760 ecp->clen > 1) {
761 ++ecp->cp;
762 --ecp->clen;
763 } else if (ecp->cp[0] == delim)
764 --cnt;
769 * Use normal quoting and termination rules to find the end of this
770 * command.
772 * QUOTING NOTE:
774 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
775 * file. It was almost certainly a bug, but that's what bug-for-bug
776 * compatibility means, Grasshopper. Also, ^V's escape the command
777 * delimiters. Literal next quote characters in front of the newlines,
778 * '|' characters or literal next characters are stripped as they're
779 * no longer useful.
781 vi_address = ecp->clen != 0 && ecp->cp[0] != '\n';
782 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
783 ch = ecp->cp[0];
784 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
785 CHAR_T tmp = ecp->cp[1];
786 if (tmp == '\n' || tmp == '|') {
787 if (tmp == '\n') {
788 ++wp->if_lno;
789 ++ecp->if_lno;
791 ++discard;
792 --ecp->clen;
793 ++ecp->cp;
794 ch = tmp;
796 } else if (ch == '\n' || ch == '|') {
797 if (ch == '\n')
798 F_SET(ecp, E_NEWLINE);
799 --ecp->clen;
800 break;
802 *p++ = ch;
806 * Save off the next command information, go back to the
807 * original start of the command.
809 p = ecp->cp + 1;
810 ecp->cp = ecp->save_cmd;
811 ecp->save_cmd = p;
812 ecp->save_cmdlen = ecp->clen;
813 ecp->clen = ((ecp->save_cmd - ecp->cp) - 1) - discard;
816 * QUOTING NOTE:
818 * The "set tags" command historically used a backslash, not the
819 * user's literal next character, to escape whitespace. Handle
820 * it here instead of complicating the argv_exp3() code. Note,
821 * this isn't a particularly complex trap, and if backslashes were
822 * legal in set commands, this would have to be much more complicated.
824 if (ecp->cmd == &cmds[C_SET])
825 for (p = ecp->cp, len = ecp->clen; len > 0; --len, ++p)
826 if (*p == '\\')
827 *p = CH_LITERAL;
830 * Set the default addresses. It's an error to specify an address for
831 * a command that doesn't take them. If two addresses are specified
832 * for a command that only takes one, lose the first one. Two special
833 * cases here, some commands take 0 or 2 addresses. For most of them
834 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
835 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
837 * Also, if the file is empty, some commands want to use an address of
838 * 0, i.e. the entire file is 0 to 0, and the default first address is
839 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
840 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
841 * case where the 0 address is only valid if it's a default address.
843 * Also, set a flag if we set the default addresses. Some commands
844 * (ex: z) care if the user specified an address or if we just used
845 * the current cursor.
847 switch (F_ISSET(ecp, E_ADDR1 | E_ADDR2 | E_ADDR2_ALL | E_ADDR2_NONE)) {
848 case E_ADDR1: /* One address: */
849 switch (ecp->addrcnt) {
850 case 0: /* Default cursor/empty file. */
851 ecp->addrcnt = 1;
852 F_SET(ecp, E_ADDR_DEF);
853 if (F_ISSET(ecp, E_ADDR_ZERODEF)) {
854 if (db_last(sp, &lno))
855 goto err;
856 if (lno == 0) {
857 ecp->addr1.lno = 0;
858 F_SET(ecp, E_ADDR_ZERO);
859 } else
860 ecp->addr1.lno = sp->lno;
861 } else
862 ecp->addr1.lno = sp->lno;
863 ecp->addr1.cno = sp->cno;
864 break;
865 case 1:
866 break;
867 case 2: /* Lose the first address. */
868 ecp->addrcnt = 1;
869 ecp->addr1 = ecp->addr2;
871 break;
872 case E_ADDR2_NONE: /* Zero/two addresses: */
873 if (ecp->addrcnt == 0) /* Default to nothing. */
874 break;
875 goto two_addr;
876 case E_ADDR2_ALL: /* Zero/two addresses: */
877 if (ecp->addrcnt == 0) { /* Default entire/empty file. */
878 F_SET(ecp, E_ADDR_DEF);
879 ecp->addrcnt = 2;
880 if (sp->ep == NULL)
881 ecp->addr2.lno = 0;
882 else if (db_last(sp, &ecp->addr2.lno))
883 goto err;
884 if (F_ISSET(ecp, E_ADDR_ZERODEF) &&
885 ecp->addr2.lno == 0) {
886 ecp->addr1.lno = 0;
887 F_SET(ecp, E_ADDR_ZERO);
888 } else
889 ecp->addr1.lno = 1;
890 ecp->addr1.cno = ecp->addr2.cno = 0;
891 F_SET(ecp, E_ADDR2_ALL);
892 break;
894 /* FALLTHROUGH */
895 case E_ADDR2: /* Two addresses: */
896 two_addr: switch (ecp->addrcnt) {
897 case 0: /* Default cursor/empty file. */
898 ecp->addrcnt = 2;
899 F_SET(ecp, E_ADDR_DEF);
900 if (sp->lno == 1 &&
901 F_ISSET(ecp, E_ADDR_ZERODEF)) {
902 if (db_last(sp, &lno))
903 goto err;
904 if (lno == 0) {
905 ecp->addr1.lno = ecp->addr2.lno = 0;
906 F_SET(ecp, E_ADDR_ZERO);
907 } else
908 ecp->addr1.lno =
909 ecp->addr2.lno = sp->lno;
910 } else
911 ecp->addr1.lno = ecp->addr2.lno = sp->lno;
912 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
913 break;
914 case 1: /* Default to first address. */
915 ecp->addrcnt = 2;
916 ecp->addr2 = ecp->addr1;
917 break;
918 case 2:
919 break;
921 break;
922 default:
923 if (ecp->addrcnt) /* Error. */
924 goto usage;
928 * !!!
929 * The ^D scroll command historically scrolled the value of the scroll
930 * option or to EOF. It was an error if the cursor was already at EOF.
931 * (Leading addresses were permitted, but were then ignored.)
933 if (ecp->cmd == &cmds[C_SCROLL]) {
934 ecp->addrcnt = 2;
935 ecp->addr1.lno = sp->lno + 1;
936 ecp->addr2.lno = sp->lno + O_VAL(sp, O_SCROLL);
937 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
938 if (db_last(sp, &lno))
939 goto err;
940 if (lno != 0 && lno > sp->lno && ecp->addr2.lno > lno)
941 ecp->addr2.lno = lno;
944 ecp->flagoff = 0;
945 for (np = ecp->cmd->syntax; *np != '\0'; ++np) {
947 * The force flag is sensitive to leading whitespace, i.e.
948 * "next !" is different from "next!". Handle it before
949 * skipping leading <blank>s.
951 if (*np == '!') {
952 if (ecp->clen > 0 && *ecp->cp == '!') {
953 ++ecp->cp;
954 --ecp->clen;
955 FL_SET(ecp->iflags, E_C_FORCE);
957 continue;
960 /* Skip leading <blank>s. */
961 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
962 if (!ISBLANK(*ecp->cp))
963 break;
964 if (ecp->clen == 0)
965 break;
967 switch (*np) {
968 case '1': /* +, -, #, l, p */
970 * !!!
971 * Historically, some flags were ignored depending
972 * on where they occurred in the command line. For
973 * example, in the command, ":3+++p--#", historic vi
974 * acted on the '#' flag, but ignored the '-' flags.
975 * It's unambiguous what the flags mean, so we just
976 * handle them regardless of the stupidity of their
977 * location.
979 for (; ecp->clen; --ecp->clen, ++ecp->cp)
980 switch (*ecp->cp) {
981 case '+':
982 ++ecp->flagoff;
983 break;
984 case '-':
985 case '^':
986 --ecp->flagoff;
987 break;
988 case '#':
989 F_CLR(ecp, E_OPTNUM);
990 FL_SET(ecp->iflags, E_C_HASH);
991 exp->fdef |= E_C_HASH;
992 break;
993 case 'l':
994 FL_SET(ecp->iflags, E_C_LIST);
995 exp->fdef |= E_C_LIST;
996 break;
997 case 'p':
998 FL_SET(ecp->iflags, E_C_PRINT);
999 exp->fdef |= E_C_PRINT;
1000 break;
1001 default:
1002 goto end_case1;
1004 end_case1: break;
1005 case '2': /* -, ., +, ^ */
1006 case '3': /* -, ., +, ^, = */
1007 for (; ecp->clen; --ecp->clen, ++ecp->cp)
1008 switch (*ecp->cp) {
1009 case '-':
1010 FL_SET(ecp->iflags, E_C_DASH);
1011 break;
1012 case '.':
1013 FL_SET(ecp->iflags, E_C_DOT);
1014 break;
1015 case '+':
1016 FL_SET(ecp->iflags, E_C_PLUS);
1017 break;
1018 case '^':
1019 FL_SET(ecp->iflags, E_C_CARAT);
1020 break;
1021 case '=':
1022 if (*np == '3') {
1023 FL_SET(ecp->iflags, E_C_EQUAL);
1024 break;
1026 /* FALLTHROUGH */
1027 default:
1028 goto end_case23;
1030 end_case23: break;
1031 case 'b': /* buffer */
1033 * !!!
1034 * Historically, "d #" was a delete with a flag, not a
1035 * delete into the '#' buffer. If the current command
1036 * permits a flag, don't use one as a buffer. However,
1037 * the 'l' and 'p' flags were legal buffer names in the
1038 * historic ex, and were used as buffers, not flags.
1040 if ((ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1041 ecp->cp[0] == '^' || ecp->cp[0] == '#') &&
1042 strchr(np, '1') != NULL)
1043 break;
1045 * !!!
1046 * Digits can't be buffer names in ex commands, or the
1047 * command "d2" would be a delete into buffer '2', and
1048 * not a two-line deletion.
1050 if (!ISDIGIT(ecp->cp[0])) {
1051 ecp->buffer = *ecp->cp;
1052 ++ecp->cp;
1053 --ecp->clen;
1054 FL_SET(ecp->iflags, E_C_BUFFER);
1056 break;
1057 case 'c': /* count [01+a] */
1058 ++np;
1059 /* Validate any signed value. */
1060 if (!ISDIGIT(*ecp->cp) && (*np != '+' ||
1061 (*ecp->cp != '+' && *ecp->cp != '-')))
1062 break;
1063 /* If a signed value, set appropriate flags. */
1064 if (*ecp->cp == '-')
1065 FL_SET(ecp->iflags, E_C_COUNT_NEG);
1066 else if (*ecp->cp == '+')
1067 FL_SET(ecp->iflags, E_C_COUNT_POS);
1068 if ((nret =
1069 nget_slong(sp, &ltmp, ecp->cp, &t, 10)) != NUM_OK) {
1070 ex_badaddr(sp, NULL, A_NOTSET, nret);
1071 goto err;
1073 if (ltmp == 0 && *np != '0') {
1074 msgq(sp, M_ERR, "083|Count may not be zero");
1075 goto err;
1077 ecp->clen -= (t - ecp->cp);
1078 ecp->cp = t;
1081 * Counts as address offsets occur in commands taking
1082 * two addresses. Historic vi practice was to use
1083 * the count as an offset from the *second* address.
1085 * Set a count flag; some underlying commands (see
1086 * join) do different things with counts than with
1087 * line addresses.
1089 if (*np == 'a') {
1090 ecp->addr1 = ecp->addr2;
1091 ecp->addr2.lno = ecp->addr1.lno + ltmp - 1;
1092 } else
1093 ecp->count = ltmp;
1094 FL_SET(ecp->iflags, E_C_COUNT);
1095 break;
1096 case 'f': /* file */
1097 if (argv_exp2(sp, ecp, ecp->cp, ecp->clen))
1098 goto err;
1099 goto arg_cnt_chk;
1100 case 'l': /* line */
1102 * Get a line specification.
1104 * If the line was a search expression, we may have
1105 * changed state during the call, and we're now
1106 * searching the file. Push ourselves onto the state
1107 * stack.
1109 if (ex_line(sp, ecp, &cur, &isaddr, &tmp))
1110 goto rfail;
1111 if (tmp)
1112 goto err;
1114 /* Line specifications are always required. */
1115 if (!isaddr) {
1116 msgq_wstr(sp, M_ERR, ecp->cp,
1117 "084|%s: bad line specification");
1118 goto err;
1121 * The target line should exist for these commands,
1122 * but 0 is legal for them as well.
1124 if (cur.lno != 0 && !db_exist(sp, cur.lno)) {
1125 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1126 goto err;
1128 ecp->lineno = cur.lno;
1129 break;
1130 case 'S': /* string, file exp. */
1131 if (ecp->clen != 0) {
1132 if (argv_exp1(sp, ecp, ecp->cp,
1133 ecp->clen, ecp->cmd == &cmds[C_BANG]))
1134 goto err;
1135 goto addr_verify;
1137 /* FALLTHROUGH */
1138 case 's': /* string */
1139 if (argv_exp0(sp, ecp, ecp->cp, ecp->clen))
1140 goto err;
1141 goto addr_verify;
1142 case 'W': /* word string */
1144 * QUOTING NOTE:
1146 * Literal next characters escape the following
1147 * character. Quoting characters are stripped here
1148 * since they are no longer useful.
1150 * First there was the word.
1152 for (p = t = ecp->cp;
1153 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
1154 ch = *ecp->cp;
1155 if (IS_ESCAPE(sp,
1156 ecp, ch) && ecp->clen > 1) {
1157 --ecp->clen;
1158 *p++ = *++ecp->cp;
1159 } else if (ISBLANK(ch)) {
1160 ++ecp->cp;
1161 --ecp->clen;
1162 break;
1163 } else
1164 *p++ = ch;
1166 if (argv_exp0(sp, ecp, t, p - t))
1167 goto err;
1169 /* Delete intervening whitespace. */
1170 for (; ecp->clen > 0;
1171 --ecp->clen, ++ecp->cp) {
1172 ch = *ecp->cp;
1173 if (!ISBLANK(ch))
1174 break;
1176 if (ecp->clen == 0)
1177 goto usage;
1179 /* Followed by the string. */
1180 for (p = t = ecp->cp; ecp->clen > 0;
1181 --ecp->clen, ++ecp->cp, ++p) {
1182 ch = *ecp->cp;
1183 if (IS_ESCAPE(sp,
1184 ecp, ch) && ecp->clen > 1) {
1185 --ecp->clen;
1186 *p = *++ecp->cp;
1187 } else
1188 *p = ch;
1190 if (argv_exp0(sp, ecp, t, p - t))
1191 goto err;
1192 goto addr_verify;
1193 case 'w': /* word */
1194 if (argv_exp3(sp, ecp, ecp->cp, ecp->clen))
1195 goto err;
1196 arg_cnt_chk: if (*++np != 'N') { /* N */
1198 * If a number is specified, must either be
1199 * 0 or that number, if optional, and that
1200 * number, if required.
1202 tmp = *np - '0';
1203 if ((*++np != 'o' || exp->argsoff != 0) &&
1204 exp->argsoff != tmp)
1205 goto usage;
1207 goto addr_verify;
1208 default:
1209 msgq(sp, M_ERR,
1210 "085|Internal syntax table error (%s: %s)",
1211 ecp->cmd->name, KEY_NAME(sp, *np));
1215 /* Skip trailing whitespace. */
1216 for (; ecp->clen > 0; --ecp->clen) {
1217 ch = *ecp->cp++;
1218 if (!ISBLANK(ch))
1219 break;
1223 * There shouldn't be anything left, and no more required fields,
1224 * i.e neither 'l' or 'r' in the syntax string.
1226 if (ecp->clen != 0 || strpbrk(np, "lr")) {
1227 usage: msgq(sp, M_ERR, "086|Usage: %s", ecp->cmd->usage);
1228 goto err;
1232 * Verify that the addresses are legal. Check the addresses here,
1233 * because this is a place where all ex addresses pass through.
1234 * (They don't all pass through ex_line(), for instance.) We're
1235 * assuming that any non-existent line doesn't exist because it's
1236 * past the end-of-file. That's a pretty good guess.
1238 * If it's a "default vi command", an address of zero is okay.
1240 addr_verify:
1241 switch (ecp->addrcnt) {
1242 case 2:
1244 * Historic ex/vi permitted commands with counts to go past
1245 * EOF. So, for example, if the file only had 5 lines, the
1246 * ex command "1,6>" would fail, but the command ">300"
1247 * would succeed. Since we don't want to have to make all
1248 * of the underlying commands handle random line numbers,
1249 * fix it here.
1251 if (ecp->addr2.lno == 0) {
1252 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1253 (F_ISSET(sp, SC_EX) ||
1254 !F_ISSET(ecp, E_USELASTCMD))) {
1255 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1256 goto err;
1258 } else if (!db_exist(sp, ecp->addr2.lno)) {
1259 if (FL_ISSET(ecp->iflags, E_C_COUNT)) {
1260 if (db_last(sp, &lno))
1261 goto err;
1262 ecp->addr2.lno = lno;
1263 } else {
1264 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1265 goto err;
1268 /* FALLTHROUGH */
1269 case 1:
1270 if (ecp->addr1.lno == 0) {
1271 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1272 (F_ISSET(sp, SC_EX) ||
1273 !F_ISSET(ecp, E_USELASTCMD))) {
1274 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1275 goto err;
1277 } else if (!db_exist(sp, ecp->addr1.lno)) {
1278 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1279 goto err;
1281 break;
1285 * If doing a default command and there's nothing left on the line,
1286 * vi just moves to the line. For example, ":3" and ":'a,'b" just
1287 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1289 * !!!
1290 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1291 * the line.
1293 * !!!
1294 * This is done before the absolute mark gets set; historically,
1295 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1297 if ((F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_NOPRDEF)) &&
1298 F_ISSET(ecp, E_USELASTCMD) && vi_address == 0) {
1299 switch (ecp->addrcnt) {
1300 case 2:
1301 if (sp->lno !=
1302 (ecp->addr2.lno ? ecp->addr2.lno : 1)) {
1303 sp->lno =
1304 ecp->addr2.lno ? ecp->addr2.lno : 1;
1305 sp->cno = 0;
1306 (void)nonblank(sp, sp->lno, &sp->cno);
1308 break;
1309 case 1:
1310 if (sp->lno !=
1311 (ecp->addr1.lno ? ecp->addr1.lno : 1)) {
1312 sp->lno =
1313 ecp->addr1.lno ? ecp->addr1.lno : 1;
1314 sp->cno = 0;
1315 (void)nonblank(sp, sp->lno, &sp->cno);
1317 break;
1319 ecp->cp = ecp->save_cmd;
1320 ecp->clen = ecp->save_cmdlen;
1321 goto loop;
1325 * Set the absolute mark -- we have to set it for vi here, in case
1326 * it's a compound command, e.g. ":5p|6" should set the absolute
1327 * mark for vi.
1329 if (F_ISSET(ecp, E_ABSMARK)) {
1330 cur.lno = sp->lno;
1331 cur.cno = sp->cno;
1332 F_CLR(ecp, E_ABSMARK);
1333 if (mark_set(sp, ABSMARK1, &cur, 1))
1334 goto err;
1337 #if defined(DEBUG) && defined(COMLOG)
1338 ex_comlog(sp, ecp);
1339 #endif
1340 /* Increment the command count if not called from vi. */
1341 if (F_ISSET(sp, SC_EX))
1342 ++sp->ccnt;
1345 * If file state available, and not doing a global command,
1346 * log the start of an action.
1348 if (sp->ep != NULL && !F_ISSET(sp, SC_EX_GLOBAL))
1349 (void)log_cursor(sp);
1352 * !!!
1353 * There are two special commands for the purposes of this code: the
1354 * default command (<carriage-return>) or the scrolling commands (^D
1355 * and <EOF>) as the first non-<blank> characters in the line.
1357 * If this is the first command in the command line, we received the
1358 * command from the ex command loop and we're talking to a tty, and
1359 * and there's nothing else on the command line, and it's one of the
1360 * special commands, we move back up to the previous line, and erase
1361 * the prompt character with the output. Since ex runs in canonical
1362 * mode, we don't have to do anything else, a <newline> has already
1363 * been echoed by the tty driver. It's OK if vi calls us -- we won't
1364 * be in ex mode so we'll do nothing.
1366 if (F_ISSET(ecp, E_NRSEP)) {
1367 if (sp->ep != NULL &&
1368 F_ISSET(sp, SC_EX) && !F_ISSET(gp, G_SCRIPTED) &&
1369 (F_ISSET(ecp, E_USELASTCMD) || ecp->cmd == &cmds[C_SCROLL]))
1370 gp->scr_ex_adjust(sp, EX_TERM_SCROLL);
1371 F_CLR(ecp, E_NRSEP);
1375 * Call the underlying function for the ex command.
1377 * XXX
1378 * Interrupts behave like errors, for now.
1380 if (ecp->cmd->fn(sp, ecp) || INTERRUPTED(sp)) {
1381 if (F_ISSET(gp, G_SCRIPTED))
1382 F_SET(sp, SC_EXIT_FORCE);
1383 goto err;
1386 #ifdef DEBUG
1387 /* Make sure no function left global temporary space locked. */
1388 if (F_ISSET(wp, W_TMP_INUSE)) {
1389 F_CLR(wp, W_TMP_INUSE);
1390 msgq(sp, M_ERR, "087|%s: temporary buffer not released",
1391 ecp->cmd->name);
1393 #endif
1395 * Ex displayed the number of lines modified immediately after each
1396 * command, so the command "1,10d|1,10d" would display:
1398 * 10 lines deleted
1399 * 10 lines deleted
1400 * <autoprint line>
1402 * Executing ex commands from vi only reported the final modified
1403 * lines message -- that's wrong enough that we don't match it.
1405 if (F_ISSET(sp, SC_EX))
1406 mod_rpt(sp);
1409 * Integrate any offset parsed by the underlying command, and make
1410 * sure the referenced line exists.
1412 * XXX
1413 * May not match historic practice (which I've never been able to
1414 * completely figure out.) For example, the '=' command from vi
1415 * mode often got the offset wrong, and complained it was too large,
1416 * but didn't seem to have a problem with the cursor. If anyone
1417 * complains, ask them how it's supposed to work, they might know.
1419 if (sp->ep != NULL && ecp->flagoff) {
1420 if (ecp->flagoff < 0) {
1421 if (sp->lno <= -ecp->flagoff) {
1422 msgq(sp, M_ERR,
1423 "088|Flag offset to before line 1");
1424 goto err;
1426 } else {
1427 if (!NPFITS(DB_MAX_RECORDS, sp->lno, ecp->flagoff)) {
1428 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1429 goto err;
1431 if (!db_exist(sp, sp->lno + ecp->flagoff)) {
1432 msgq(sp, M_ERR,
1433 "089|Flag offset past end-of-file");
1434 goto err;
1437 sp->lno += ecp->flagoff;
1441 * If the command executed successfully, we may want to display a line
1442 * based on the autoprint option or an explicit print flag. (Make sure
1443 * that there's a line to display.) Also, the autoprint edit option is
1444 * turned off for the duration of global commands.
1446 if (F_ISSET(sp, SC_EX) && sp->ep != NULL && sp->lno != 0) {
1448 * The print commands have already handled the `print' flags.
1449 * If so, clear them.
1451 if (FL_ISSET(ecp->iflags, E_CLRFLAG))
1452 FL_CLR(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT);
1454 /* If hash set only because of the number option, discard it. */
1455 if (F_ISSET(ecp, E_OPTNUM))
1456 FL_CLR(ecp->iflags, E_C_HASH);
1459 * If there was an explicit flag to display the new cursor line,
1460 * or autoprint is set and a change was made, display the line.
1461 * If any print flags were set use them, else default to print.
1463 LF_INIT(FL_ISSET(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT));
1464 if (!LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT | E_NOAUTO) &&
1465 !F_ISSET(sp, SC_EX_GLOBAL) &&
1466 O_ISSET(sp, O_AUTOPRINT) && F_ISSET(ecp, E_AUTOPRINT))
1467 LF_INIT(E_C_PRINT);
1469 if (LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT)) {
1470 cur.lno = sp->lno;
1471 cur.cno = 0;
1472 (void)ex_print(sp, ecp, &cur, &cur, flags);
1477 * If the command had an associated "+cmd", it has to be executed
1478 * before we finish executing any more of this ex command. For
1479 * example, consider a .exrc file that contains the following lines:
1481 * :set all
1482 * :edit +25 file.c|s/abc/ABC/|1
1483 * :3,5 print
1485 * This can happen more than once -- the historic vi simply hung or
1486 * dropped core, of course. Prepend the + command back into the
1487 * current command and continue. We may have to add an additional
1488 * <literal next> character. We know that it will fit because we
1489 * discarded at least one space and the + character.
1491 if (arg1_len != 0) {
1493 * If the last character of the + command was a <literal next>
1494 * character, it would be treated differently because of the
1495 * append. Quote it, if necessary.
1497 if (IS_ESCAPE(sp, ecp, arg1[arg1_len - 1])) {
1498 *--ecp->save_cmd = CH_LITERAL;
1499 ++ecp->save_cmdlen;
1502 ecp->save_cmd -= arg1_len;
1503 ecp->save_cmdlen += arg1_len;
1504 MEMCPYW(ecp->save_cmd, arg1, arg1_len);
1507 * Any commands executed from a +cmd are executed starting at
1508 * the first column of the last line of the file -- NOT the
1509 * first nonblank.) The main file startup code doesn't know
1510 * that a +cmd was set, however, so it may have put us at the
1511 * top of the file. (Note, this is safe because we must have
1512 * switched files to get here.)
1514 F_SET(ecp, E_MOVETOEND);
1517 /* Update the current command. */
1518 ecp->cp = ecp->save_cmd;
1519 ecp->clen = ecp->save_cmdlen;
1522 * !!!
1523 * If we've changed screens or underlying files, any pending global or
1524 * v command, or @ buffer that has associated addresses, has to be
1525 * discarded. This is historic practice for globals, and necessary for
1526 * @ buffers that had associated addresses.
1528 * Otherwise, if we've changed underlying files, it's not a problem,
1529 * we continue with the rest of the ex command(s), operating on the
1530 * new file. However, if we switch screens (either by exiting or by
1531 * an explicit command), we have no way of knowing where to put output
1532 * messages, and, since we don't control screens here, we could screw
1533 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1534 * times). So, return and continue after we've got a new screen.
1536 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH)) {
1537 at_found = gv_found = 0;
1538 for (ecp = wp->ecq.lh_first;
1539 ecp != NULL; ecp = ecp->q.le_next)
1540 switch (ecp->agv_flags) {
1541 case 0:
1542 case AGV_AT_NORANGE:
1543 break;
1544 case AGV_AT:
1545 if (!at_found) {
1546 at_found = 1;
1547 msgq(sp, M_ERR,
1548 "090|@ with range running when the file/screen changed");
1550 break;
1551 case AGV_GLOBAL:
1552 case AGV_V:
1553 if (!gv_found) {
1554 gv_found = 1;
1555 msgq(sp, M_ERR,
1556 "091|Global/v command running when the file/screen changed");
1558 break;
1559 default:
1560 abort();
1562 if (at_found || gv_found)
1563 goto discard;
1564 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_SSWITCH))
1565 goto rsuccess;
1568 goto loop;
1569 /* NOTREACHED */
1571 err: /*
1572 * On command failure, we discard keys and pending commands remaining,
1573 * as well as any keys that were mapped and waiting. The save_cmdlen
1574 * test is not necessarily correct. If we fail early enough we don't
1575 * know if the entire string was a single command or not. Guess, as
1576 * it's useful to know if commands other than the current one are being
1577 * discarded.
1579 if (ecp->save_cmdlen == 0)
1580 for (; ecp->clen; --ecp->clen) {
1581 ch = *ecp->cp++;
1582 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
1583 --ecp->clen;
1584 ++ecp->cp;
1585 } else if (ch == '\n' || ch == '|') {
1586 if (ecp->clen > 1)
1587 ecp->save_cmdlen = 1;
1588 break;
1591 if (ecp->save_cmdlen != 0 || wp->ecq.lh_first != &wp->excmd) {
1592 discard: msgq(sp, M_BERR,
1593 "092|Ex command failed: pending commands discarded");
1594 ex_discard(sp);
1596 if (v_event_flush(sp, CH_MAPPED))
1597 msgq(sp, M_BERR,
1598 "093|Ex command failed: mapped keys discarded");
1600 rfail: tmp = 1;
1601 if (0)
1602 rsuccess: tmp = 0;
1604 /* Turn off any file name error information. */
1605 wp->if_name = NULL;
1607 /* Turn off the global bit. */
1608 F_CLR(sp, SC_EX_GLOBAL);
1610 return (tmp);
1614 * ex_range --
1615 * Get a line range for ex commands, or perform a vi ex address search.
1617 * PUBLIC: int ex_range __P((SCR *, EXCMD *, int *));
1620 ex_range(SCR *sp, EXCMD *ecp, int *errp)
1622 enum { ADDR_FOUND, ADDR_NEED, ADDR_NONE } addr;
1623 GS *gp;
1624 EX_PRIVATE *exp;
1625 MARK m;
1626 int isaddr;
1628 *errp = 0;
1631 * Parse comma or semi-colon delimited line specs.
1633 * Semi-colon delimiters update the current address to be the last
1634 * address. For example, the command
1636 * :3;/pattern/ecp->cp
1638 * will search for pattern from line 3. In addition, if ecp->cp
1639 * is not a valid command, the current line will be left at 3, not
1640 * at the original address.
1642 * Extra addresses are discarded, starting with the first.
1644 * !!!
1645 * If any addresses are missing, they default to the current line.
1646 * This was historically true for both leading and trailing comma
1647 * delimited addresses as well as for trailing semicolon delimited
1648 * addresses. For consistency, we make it true for leading semicolon
1649 * addresses as well.
1651 gp = sp->gp;
1652 exp = EXP(sp);
1653 for (addr = ADDR_NONE, ecp->addrcnt = 0; ecp->clen > 0;)
1654 switch (*ecp->cp) {
1655 case '%': /* Entire file. */
1656 /* Vi ex address searches didn't permit % signs. */
1657 if (F_ISSET(ecp, E_VISEARCH))
1658 goto ret;
1660 /* It's an error if the file is empty. */
1661 if (sp->ep == NULL) {
1662 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1663 *errp = 1;
1664 return (0);
1667 * !!!
1668 * A percent character addresses all of the lines in
1669 * the file. Historically, it couldn't be followed by
1670 * any other address. We do it as a text substitution
1671 * for simplicity. POSIX 1003.2 is expected to follow
1672 * this practice.
1674 * If it's an empty file, the first line is 0, not 1.
1676 if (addr == ADDR_FOUND) {
1677 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1678 *errp = 1;
1679 return (0);
1681 if (db_last(sp, &ecp->addr2.lno))
1682 return (1);
1683 ecp->addr1.lno = ecp->addr2.lno == 0 ? 0 : 1;
1684 ecp->addr1.cno = ecp->addr2.cno = 0;
1685 ecp->addrcnt = 2;
1686 addr = ADDR_FOUND;
1687 ++ecp->cp;
1688 --ecp->clen;
1689 break;
1690 case ',': /* Comma delimiter. */
1691 /* Vi ex address searches didn't permit commas. */
1692 if (F_ISSET(ecp, E_VISEARCH))
1693 goto ret;
1694 /* FALLTHROUGH */
1695 case ';': /* Semi-colon delimiter. */
1696 if (sp->ep == NULL) {
1697 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1698 *errp = 1;
1699 return (0);
1701 if (addr != ADDR_FOUND)
1702 switch (ecp->addrcnt) {
1703 case 0:
1704 ecp->addr1.lno = sp->lno;
1705 ecp->addr1.cno = sp->cno;
1706 ecp->addrcnt = 1;
1707 break;
1708 case 2:
1709 ecp->addr1 = ecp->addr2;
1710 /* FALLTHROUGH */
1711 case 1:
1712 ecp->addr2.lno = sp->lno;
1713 ecp->addr2.cno = sp->cno;
1714 ecp->addrcnt = 2;
1715 break;
1717 if (*ecp->cp == ';')
1718 switch (ecp->addrcnt) {
1719 case 0:
1720 abort();
1721 /* NOTREACHED */
1722 case 1:
1723 sp->lno = ecp->addr1.lno;
1724 sp->cno = ecp->addr1.cno;
1725 break;
1726 case 2:
1727 sp->lno = ecp->addr2.lno;
1728 sp->cno = ecp->addr2.cno;
1729 break;
1731 addr = ADDR_NEED;
1732 /* FALLTHROUGH */
1733 case ' ': /* Whitespace. */
1734 case '\t': /* Whitespace. */
1735 ++ecp->cp;
1736 --ecp->clen;
1737 break;
1738 default:
1739 /* Get a line specification. */
1740 if (ex_line(sp, ecp, &m, &isaddr, errp))
1741 return (1);
1742 if (*errp)
1743 return (0);
1744 if (!isaddr)
1745 goto ret;
1746 if (addr == ADDR_FOUND) {
1747 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1748 *errp = 1;
1749 return (0);
1751 switch (ecp->addrcnt) {
1752 case 0:
1753 ecp->addr1 = m;
1754 ecp->addrcnt = 1;
1755 break;
1756 case 1:
1757 ecp->addr2 = m;
1758 ecp->addrcnt = 2;
1759 break;
1760 case 2:
1761 ecp->addr1 = ecp->addr2;
1762 ecp->addr2 = m;
1763 break;
1765 addr = ADDR_FOUND;
1766 break;
1770 * !!!
1771 * Vi ex address searches are indifferent to order or trailing
1772 * semi-colons.
1774 ret: if (F_ISSET(ecp, E_VISEARCH))
1775 return (0);
1777 if (addr == ADDR_NEED)
1778 switch (ecp->addrcnt) {
1779 case 0:
1780 ecp->addr1.lno = sp->lno;
1781 ecp->addr1.cno = sp->cno;
1782 ecp->addrcnt = 1;
1783 break;
1784 case 2:
1785 ecp->addr1 = ecp->addr2;
1786 /* FALLTHROUGH */
1787 case 1:
1788 ecp->addr2.lno = sp->lno;
1789 ecp->addr2.cno = sp->cno;
1790 ecp->addrcnt = 2;
1791 break;
1794 if (ecp->addrcnt == 2 && ecp->addr2.lno < ecp->addr1.lno) {
1795 msgq(sp, M_ERR,
1796 "094|The second address is smaller than the first");
1797 *errp = 1;
1799 return (0);
1803 * ex_line --
1804 * Get a single line address specifier.
1806 * The way the "previous context" mark worked was that any "non-relative"
1807 * motion set it. While ex/vi wasn't totally consistent about this, ANY
1808 * numeric address, search pattern, '$', or mark reference in an address
1809 * was considered non-relative, and set the value. Which should explain
1810 * why we're hacking marks down here. The problem was that the mark was
1811 * only set if the command was called, i.e. we have to set a flag and test
1812 * it later.
1814 * XXX
1815 * This is probably still not exactly historic practice, although I think
1816 * it's fairly close.
1818 static int
1819 ex_line(SCR *sp, EXCMD *ecp, MARK *mp, int *isaddrp, int *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_T *, size_t, CHAR_T **, u_int));
1827 CHAR_T *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(sp, &val, ecp->cp, &endp, 10)) != NUM_OK) {
1859 ex_badaddr(sp, NULL, A_NOTSET, nret);
1860 *errp = 1;
1861 return (0);
1863 if (!NPFITS(DB_MAX_RECORDS, 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(sp, &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 db_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(DB_MAX_RECORDS, 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(SCR *sp)
2068 WIN *wp;
2069 EXCMD *ecp;
2070 RANGE *rp;
2072 F_CLR(sp, SC_EX_GLOBAL);
2075 * Lose any exhausted commands. We know that the first command
2076 * can't be an AGV command, which makes things a bit easier.
2078 for (wp = sp->wp;;) {
2080 * If we're back to the original structure, leave it around,
2081 * but discard any allocated source name, we've returned to
2082 * the beginning of the command stack.
2084 if ((ecp = wp->ecq.lh_first) == &wp->excmd) {
2085 if (F_ISSET(ecp, E_NAMEDISCARD)) {
2086 free(ecp->if_name);
2087 ecp->if_name = NULL;
2089 return (0);
2093 * ecp->clen will be 0 for the first discarded command, but
2094 * may not be 0 for subsequent ones, e.g. if the original
2095 * command was ":g/xx/@a|s/b/c/", then when we discard the
2096 * command pushed on the stack by the @a, we have to resume
2097 * the global command which included the substitute command.
2099 if (ecp->clen != 0)
2100 return (0);
2103 * If it's an @, global or v command, we may need to continue
2104 * the command on a different line.
2106 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2107 /* Discard any exhausted ranges. */
2108 while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq)
2109 if (rp->start > rp->stop) {
2110 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
2111 free(rp);
2112 } else
2113 break;
2115 /* If there's another range, continue with it. */
2116 if (rp != (void *)&ecp->rq)
2117 break;
2119 /* If it's a global/v command, fix up the last line. */
2120 if (FL_ISSET(ecp->agv_flags,
2121 AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO)
2122 if (db_exist(sp, ecp->range_lno))
2123 sp->lno = ecp->range_lno;
2124 else {
2125 if (db_last(sp, &sp->lno))
2126 return (1);
2127 if (sp->lno == 0)
2128 sp->lno = 1;
2130 free(ecp->o_cp);
2133 /* Discard the EXCMD. */
2134 LIST_REMOVE(ecp, q);
2135 free(ecp);
2139 * We only get here if it's an active @, global or v command. Set
2140 * the current line number, and get a new copy of the command for
2141 * the parser. Note, the original pointer almost certainly moved,
2142 * so we have play games.
2144 ecp->cp = ecp->o_cp;
2145 MEMCPYW(ecp->cp, ecp->cp + ecp->o_clen, ecp->o_clen);
2146 ecp->clen = ecp->o_clen;
2147 ecp->range_lno = sp->lno = rp->start++;
2149 if (FL_ISSET(ecp->agv_flags, AGV_GLOBAL | AGV_V))
2150 F_SET(sp, SC_EX_GLOBAL);
2151 return (0);
2155 * ex_discard --
2156 * Discard any pending ex commands.
2158 static int
2159 ex_discard(SCR *sp)
2161 WIN *wp;
2162 EXCMD *ecp;
2163 RANGE *rp;
2166 * We know the first command can't be an AGV command, so we don't
2167 * process it specially. We do, however, nail the command itself.
2169 for (wp = sp->wp; (ecp = wp->ecq.lh_first) != &wp->excmd;) {
2170 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2171 while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq) {
2172 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
2173 free(rp);
2175 free(ecp->o_cp);
2177 LIST_REMOVE(ecp, q);
2178 free(ecp);
2180 wp->ecq.lh_first->clen = 0;
2181 return (0);
2185 * ex_unknown --
2186 * Display an unknown command name.
2188 static void
2189 ex_unknown(SCR *sp, CHAR_T *cmd, size_t len)
2191 size_t blen;
2192 CHAR_T *bp;
2194 GET_SPACE_GOTOW(sp, bp, blen, len + 1);
2195 bp[len] = '\0';
2196 MEMCPYW(bp, cmd, len);
2197 msgq_wstr(sp, M_ERR, bp, "098|The %s command is unknown");
2198 FREE_SPACEW(sp, bp, blen);
2200 alloc_err:
2201 return;
2205 * ex_is_abbrev -
2206 * The vi text input routine needs to know if ex thinks this is an
2207 * [un]abbreviate command, so it can turn off abbreviations. See
2208 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2210 * PUBLIC: int ex_is_abbrev __P((SCR *, CHAR_T *, size_t));
2213 ex_is_abbrev(SCR *sp, CHAR_T *name, size_t len)
2215 EXCMDLIST const *cp;
2217 return ((cp = ex_comm_search(sp, name, len)) != NULL &&
2218 (cp == &cmds[C_ABBR] || cp == &cmds[C_UNABBREVIATE]));
2222 * ex_is_unmap -
2223 * The vi text input routine needs to know if ex thinks this is an
2224 * unmap command, so it can turn off input mapping. See the usual
2225 * ranting in the vi/v_txt_ev.c:txt_unmap() routine.
2227 * PUBLIC: int ex_is_unmap __P((SCR *, CHAR_T *, size_t));
2230 ex_is_unmap(SCR *sp, CHAR_T *name, size_t len)
2232 EXCMDLIST const *cp;
2235 * The command the vi input routines are really interested in
2236 * is "unmap!", not just unmap.
2238 if (name[len - 1] != '!')
2239 return (0);
2240 --len;
2241 return ((cp = ex_comm_search(sp, name, len)) != NULL &&
2242 cp == &cmds[C_UNMAP]);
2246 * ex_comm_search --
2247 * Search for a command name.
2249 static EXCMDLIST const *
2250 ex_comm_search(SCR *sp, CHAR_T *name, size_t len)
2252 EXCMDLIST const *cp;
2254 for (cp = cmds; cp->name != NULL; ++cp) {
2255 if (cp->name[0] > name[0])
2256 return (NULL);
2257 if (cp->name[0] != name[0])
2258 continue;
2259 if (!MEMCMP(name, cp->name, len))
2260 return (cp);
2262 return (NULL);
2266 * ex_badaddr --
2267 * Display a bad address message.
2269 * PUBLIC: void ex_badaddr
2270 * PUBLIC: __P((SCR *, EXCMDLIST const *, enum badaddr, enum nresult));
2272 void
2273 ex_badaddr(SCR *sp, const EXCMDLIST *cp, enum badaddr ba, enum nresult nret)
2275 db_recno_t lno;
2277 switch (nret) {
2278 case NUM_OK:
2279 break;
2280 case NUM_ERR:
2281 msgq(sp, M_SYSERR, NULL);
2282 return;
2283 case NUM_OVER:
2284 msgq(sp, M_ERR, "099|Address value overflow");
2285 return;
2286 case NUM_UNDER:
2287 msgq(sp, M_ERR, "100|Address value underflow");
2288 return;
2292 * When encountering an address error, tell the user if there's no
2293 * underlying file, that's the real problem.
2295 if (sp->ep == NULL) {
2296 ex_wemsg(sp, cp ? cp->name : NULL, EXM_NOFILEYET);
2297 return;
2300 switch (ba) {
2301 case A_COMBO:
2302 msgq(sp, M_ERR, "101|Illegal address combination");
2303 break;
2304 case A_EOF:
2305 if (db_last(sp, &lno))
2306 return;
2307 if (lno != 0) {
2308 msgq(sp, M_ERR,
2309 "102|Illegal address: only %lu lines in the file",
2310 lno);
2311 break;
2313 /* FALLTHROUGH */
2314 case A_EMPTY:
2315 msgq(sp, M_ERR, "103|Illegal address: the file is empty");
2316 break;
2317 case A_NOTSET:
2318 abort();
2319 /* NOTREACHED */
2320 case A_ZERO:
2321 msgq(sp, M_ERR,
2322 "104|The %s command doesn't permit an address of 0",
2323 cp->name);
2324 break;
2326 return;
2329 #if defined(DEBUG) && defined(COMLOG)
2331 * ex_comlog --
2332 * Log ex commands.
2334 static void
2335 ex_comlog(sp, ecp)
2336 SCR *sp;
2337 EXCMD *ecp;
2339 vtrace(sp, "ecmd: %s", ecp->cmd->name);
2340 if (ecp->addrcnt > 0) {
2341 vtrace(sp, " a1 %d", ecp->addr1.lno);
2342 if (ecp->addrcnt > 1)
2343 vtrace(sp, " a2: %d", ecp->addr2.lno);
2345 if (ecp->lineno)
2346 vtrace(sp, " line %d", ecp->lineno);
2347 if (ecp->flags)
2348 vtrace(sp, " flags 0x%x", ecp->flags);
2349 if (F_ISSET(&exc, E_BUFFER))
2350 vtrace(sp, " buffer %c", ecp->buffer);
2351 if (ecp->argc)
2352 for (cnt = 0; cnt < ecp->argc; ++cnt)
2353 vtrace(sp, " arg %d: {%s}", cnt, ecp->argv[cnt]->bp);
2354 vtrace(sp, "\n");
2356 #endif