*** empty log message ***
[nvi.git] / ex / ex.c
blob56f57a28447ce011a0265ceac3b412aa522e379c
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.64 2000/07/01 09:44:40 skimo Exp $ (Berkeley) $Date: 2000/07/01 09:44:40 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "../common/common.h"
32 #include "../vi/vi.h"
34 #if defined(DEBUG) && defined(COMLOG)
35 static void ex_comlog __P((SCR *, EXCMD *));
36 #endif
37 static EXCMDLIST const *
38 ex_comm_search __P((char *, size_t));
39 static int ex_discard __P((SCR *));
40 static int ex_line __P((SCR *, EXCMD *, MARK *, int *, int *));
41 static int ex_load __P((SCR *));
42 static void ex_unknown __P((SCR *, char *, size_t));
45 * ex --
46 * Main ex loop.
48 * PUBLIC: int ex __P((SCR **));
50 int
51 ex(spp)
52 SCR **spp;
54 EX_PRIVATE *exp;
55 GS *gp;
56 WIN *wp;
57 MSGS *mp;
58 SCR *sp;
59 TEXT *tp;
60 u_int32_t flags;
62 sp = *spp;
63 wp = sp->wp;
64 gp = sp->gp;
65 exp = EXP(sp);
67 /* Start the ex screen. */
68 if (ex_init(sp))
69 return (1);
71 /* Flush any saved messages. */
72 while ((mp = gp->msgq.lh_first) != NULL) {
73 gp->scr_msg(sp, mp->mtype, mp->buf, mp->len);
74 LIST_REMOVE(mp, q);
75 free(mp->buf);
76 free(mp);
79 /* If reading from a file, errors should have name and line info. */
80 if (F_ISSET(gp, G_SCRIPTED)) {
81 wp->excmd.if_lno = 1;
82 wp->excmd.if_name = "script";
86 * !!!
87 * Initialize the text flags. The beautify edit option historically
88 * applied to ex command input read from a file. In addition, the
89 * first time a ^H was discarded from the input, there was a message,
90 * "^H discarded", that was displayed. We don't bother.
92 LF_INIT(TXT_BACKSLASH | TXT_CNTRLD | TXT_CR);
93 for (;; ++wp->excmd.if_lno) {
94 /* Display status line and flush. */
95 if (F_ISSET(sp, SC_STATUS)) {
96 if (!F_ISSET(sp, SC_EX_SILENT))
97 msgq_status(sp, sp->lno, 0);
98 F_CLR(sp, SC_STATUS);
100 (void)ex_fflush(sp);
102 /* Set the flags the user can reset. */
103 if (O_ISSET(sp, O_BEAUTIFY))
104 LF_SET(TXT_BEAUTIFY);
105 if (O_ISSET(sp, O_PROMPT))
106 LF_SET(TXT_PROMPT);
108 /* Clear any current interrupts, and get a command. */
109 CLR_INTERRUPT(sp);
110 if (ex_txt(sp, &sp->tiq, ':', flags))
111 return (1);
112 if (INTERRUPTED(sp)) {
113 (void)ex_puts(sp, "\n");
114 (void)ex_fflush(sp);
115 continue;
118 /* Initialize the command structure. */
119 CLEAR_EX_PARSER(&wp->excmd);
122 * If the user entered a single carriage return, send
123 * ex_cmd() a separator -- it discards single newlines.
125 tp = sp->tiq.cqh_first;
126 if (tp->len == 0) {
127 wp->excmd.cp = " "; /* __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(sp)
198 SCR *sp;
200 enum nresult nret;
201 EX_PRIVATE *exp;
202 EXCMD *ecp;
203 GS *gp;
204 WIN *wp;
205 MARK cur;
206 db_recno_t lno;
207 size_t arg1_len, discard, len;
208 u_int32_t flags;
209 long ltmp;
210 int at_found, gv_found;
211 int ch, cnt, delim, isaddr, namelen;
212 int newscreen, notempty, tmp, vi_address;
213 char *arg1, *p, *s, *t;
215 gp = sp->gp;
216 wp = sp->wp;
217 exp = EXP(sp);
220 * We always start running the command on the top of the stack.
221 * This means that *everything* must be resolved when we leave
222 * this function for any reason.
224 loop: ecp = wp->ecq.lh_first;
226 /* If we're reading a command from a file, set up error information. */
227 if (ecp->if_name != NULL) {
228 wp->if_lno = ecp->if_lno;
229 wp->if_name = ecp->if_name;
233 * If a move to the end of the file is scheduled for this command,
234 * do it now.
236 if (F_ISSET(ecp, E_MOVETOEND)) {
237 if (db_last(sp, &sp->lno))
238 goto rfail;
239 sp->cno = 0;
240 F_CLR(ecp, E_MOVETOEND);
243 /* If we found a newline, increment the count now. */
244 if (F_ISSET(ecp, E_NEWLINE)) {
245 ++wp->if_lno;
246 ++ecp->if_lno;
247 F_CLR(ecp, E_NEWLINE);
250 /* (Re)initialize the EXCMD structure, preserving some flags. */
251 CLEAR_EX_CMD(ecp);
253 /* Initialize the argument structures. */
254 if (argv_init(sp, ecp))
255 goto err;
257 /* Initialize +cmd, saved command information. */
258 arg1 = NULL;
259 ecp->save_cmdlen = 0;
261 /* Skip <blank>s, empty lines. */
262 for (notempty = 0; ecp->clen > 0; ++ecp->cp, --ecp->clen)
263 if ((ch = *ecp->cp) == '\n') {
264 ++wp->if_lno;
265 ++ecp->if_lno;
266 } else if (isblank(ch))
267 notempty = 1;
268 else
269 break;
272 * !!!
273 * Permit extra colons at the start of the line. Historically,
274 * ex/vi allowed a single extra one. It's simpler not to count.
275 * The stripping is done here because, historically, any command
276 * could have preceding colons, e.g. ":g/pattern/:p" worked.
278 if (ecp->clen != 0 && ch == ':') {
279 notempty = 1;
280 while (--ecp->clen > 0 && (ch = *++ecp->cp) == ':');
284 * Command lines that start with a double-quote are comments.
286 * !!!
287 * Historically, there was no escape or delimiter for a comment, e.g.
288 * :"foo|set was a single comment and nothing was output. Since nvi
289 * permits users to escape <newline> characters into command lines, we
290 * have to check for that case.
292 if (ecp->clen != 0 && ch == '"') {
293 while (--ecp->clen > 0 && *++ecp->cp != '\n');
294 if (*ecp->cp == '\n') {
295 F_SET(ecp, E_NEWLINE);
296 ++ecp->cp;
297 --ecp->clen;
299 goto loop;
302 /* Skip whitespace. */
303 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
304 ch = *ecp->cp;
305 if (!isblank(ch))
306 break;
310 * The last point at which an empty line can mean do nothing.
312 * !!!
313 * Historically, in ex mode, lines containing only <blank> characters
314 * were the same as a single <carriage-return>, i.e. a default command.
315 * In vi mode, they were ignored. In .exrc files this was a serious
316 * annoyance, as vi kept trying to treat them as print commands. We
317 * ignore backward compatibility in this case, discarding lines that
318 * contain only <blank> characters from .exrc files.
320 * !!!
321 * This is where you end up when you're done a command, i.e. clen has
322 * gone to zero. Continue if there are more commands to run.
324 if (ecp->clen == 0 &&
325 (!notempty || F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_BLIGNORE))) {
326 if (ex_load(sp))
327 goto rfail;
328 ecp = wp->ecq.lh_first;
329 if (ecp->clen == 0)
330 goto rsuccess;
331 goto loop;
335 * Check to see if this is a command for which we may want to move
336 * the cursor back up to the previous line. (The command :1<CR>
337 * wants a <newline> separator, but the command :<CR> wants to erase
338 * the command line.) If the line is empty except for <blank>s,
339 * <carriage-return> or <eof>, we'll probably want to move up. I
340 * don't think there's any way to get <blank> characters *after* the
341 * command character, but this is the ex parser, and I've been wrong
342 * before.
344 if (F_ISSET(ecp, E_NRSEP) &&
345 ecp->clen != 0 && (ecp->clen != 1 || ecp->cp[0] != '\004'))
346 F_CLR(ecp, E_NRSEP);
348 /* Parse command addresses. */
349 if (ex_range(sp, ecp, &tmp))
350 goto rfail;
351 if (tmp)
352 goto err;
355 * Skip <blank>s and any more colons (the command :3,5:print
356 * worked, historically).
358 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
359 ch = *ecp->cp;
360 if (!isblank(ch) && ch != ':')
361 break;
365 * If no command, ex does the last specified of p, l, or #, and vi
366 * moves to the line. Otherwise, determine the length of the command
367 * name by looking for the first non-alphabetic character. (There
368 * are a few non-alphabetic characters in command names, but they're
369 * all single character commands.) This isn't a great test, because
370 * it means that, for the command ":e +cut.c file", we'll report that
371 * the command "cut" wasn't known. However, it makes ":e+35 file" work
372 * correctly.
374 * !!!
375 * Historically, lines with multiple adjacent (or <blank> separated)
376 * command separators were very strange. For example, the command
377 * |||<carriage-return>, when the cursor was on line 1, displayed
378 * lines 2, 3 and 5 of the file. In addition, the command " | "
379 * would only display the line after the next line, instead of the
380 * next two lines. No ideas why. It worked reasonably when executed
381 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
382 * command for each separator.
384 #define SINGLE_CHAR_COMMANDS "\004!#&*<=>@~"
385 newscreen = 0;
386 if (ecp->clen != 0 && ecp->cp[0] != '|' && ecp->cp[0] != '\n') {
387 if (strchr(SINGLE_CHAR_COMMANDS, *ecp->cp)) {
388 p = ecp->cp;
389 ++ecp->cp;
390 --ecp->clen;
391 namelen = 1;
392 } else {
393 for (p = ecp->cp;
394 ecp->clen > 0; --ecp->clen, ++ecp->cp)
395 if (!isalpha(*ecp->cp))
396 break;
397 if ((namelen = ecp->cp - p) == 0) {
398 msgq(sp, M_ERR, "080|Unknown command name");
399 goto err;
404 * !!!
405 * Historic vi permitted flags to immediately follow any
406 * subset of the 'delete' command, but then did not permit
407 * further arguments (flag, buffer, count). Make it work.
408 * Permit further arguments for the few shreds of dignity
409 * it offers.
411 * Adding commands that start with 'd', and match "delete"
412 * up to a l, p, +, - or # character can break this code.
414 * !!!
415 * Capital letters beginning the command names ex, edit,
416 * next, previous, tag and visual (in vi mode) indicate the
417 * command should happen in a new screen.
419 switch (p[0]) {
420 case 'd':
421 for (s = p,
422 t = cmds[C_DELETE].name; *s == *t; ++s, ++t);
423 if (s[0] == 'l' || s[0] == 'p' || s[0] == '+' ||
424 s[0] == '-' || s[0] == '^' || s[0] == '#') {
425 len = (ecp->cp - p) - (s - p);
426 ecp->cp -= len;
427 ecp->clen += len;
428 ecp->rcmd = cmds[C_DELETE];
429 ecp->rcmd.syntax = "1bca1";
430 ecp->cmd = &ecp->rcmd;
431 goto skip_srch;
433 break;
434 case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
435 newscreen = 1;
436 p[0] = tolower(p[0]);
437 break;
441 * Search the table for the command.
443 * !!!
444 * Historic vi permitted the mark to immediately follow the
445 * 'k' in the 'k' command. Make it work.
447 * !!!
448 * Historic vi permitted any flag to follow the s command, e.g.
449 * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
450 * Since the following characters all have to be flags, i.e.
451 * alphabetics, we can let the s command routine return errors
452 * if it was some illegal command string. This code will break
453 * if an "sg" or similar command is ever added. The substitute
454 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
455 * follows the 's', but we limit the choices here to "cgr" so
456 * that we get unknown command messages for wrong combinations.
458 if ((ecp->cmd = ex_comm_search(p, namelen)) == NULL)
459 switch (p[0]) {
460 case 'k':
461 if (namelen == 2) {
462 ecp->cp -= namelen - 1;
463 ecp->clen += namelen - 1;
464 ecp->cmd = &cmds[C_K];
465 break;
467 goto unknown;
468 case 's':
469 for (s = p + 1, cnt = namelen; --cnt; ++s)
470 if (s[0] != 'c' &&
471 s[0] != 'g' && s[0] != 'r')
472 break;
473 if (cnt == 0) {
474 ecp->cp -= namelen - 1;
475 ecp->clen += namelen - 1;
476 ecp->rcmd = cmds[C_SUBSTITUTE];
477 ecp->rcmd.fn = ex_subagain;
478 ecp->cmd = &ecp->rcmd;
479 break;
481 /* FALLTHROUGH */
482 default:
483 unknown: if (newscreen)
484 p[0] = toupper(p[0]);
485 ex_unknown(sp, p, namelen);
486 goto err;
490 * The visual command has a different syntax when called
491 * from ex than when called from a vi colon command. FMH.
492 * Make the change now, before we test for the newscreen
493 * semantic, so that we're testing the right one.
495 skip_srch: if (ecp->cmd == &cmds[C_VISUAL_EX] && F_ISSET(sp, SC_VI))
496 ecp->cmd = &cmds[C_VISUAL_VI];
499 * !!!
500 * Historic vi permitted a capital 'P' at the beginning of
501 * any command that started with 'p'. Probably wanted the
502 * P[rint] command for backward compatibility, and the code
503 * just made Preserve and Put work by accident. Nvi uses
504 * Previous to mean previous-in-a-new-screen, so be careful.
506 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN) &&
507 (ecp->cmd == &cmds[C_PRINT] ||
508 ecp->cmd == &cmds[C_PRESERVE]))
509 newscreen = 0;
511 /* Test for a newscreen associated with this command. */
512 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN))
513 goto unknown;
515 /* Secure means no shell access. */
516 if (F_ISSET(ecp->cmd, E_SECURE) && O_ISSET(sp, O_SECURE)) {
517 ex_emsg(sp, ecp->cmd->name, EXM_SECURE);
518 goto err;
522 * Multiple < and > characters; another "feature". Note,
523 * The string passed to the underlying function may not be
524 * nul terminated in this case.
526 if ((ecp->cmd == &cmds[C_SHIFTL] && *p == '<') ||
527 (ecp->cmd == &cmds[C_SHIFTR] && *p == '>')) {
528 for (ch = *p;
529 ecp->clen > 0; --ecp->clen, ++ecp->cp)
530 if (*ecp->cp != ch)
531 break;
532 if (argv_exp0(sp, ecp, p, ecp->cp - p))
533 goto err;
536 /* Set the format style flags for the next command. */
537 if (ecp->cmd == &cmds[C_HASH])
538 exp->fdef = E_C_HASH;
539 else if (ecp->cmd == &cmds[C_LIST])
540 exp->fdef = E_C_LIST;
541 else if (ecp->cmd == &cmds[C_PRINT])
542 exp->fdef = E_C_PRINT;
543 F_CLR(ecp, E_USELASTCMD);
544 } else {
545 /* Print is the default command. */
546 ecp->cmd = &cmds[C_PRINT];
548 /* Set the saved format flags. */
549 F_SET(ecp, exp->fdef);
552 * !!!
553 * If no address was specified, and it's not a global command,
554 * we up the address by one. (I have no idea why globals are
555 * exempted, but it's (ahem) historic practice.)
557 if (ecp->addrcnt == 0 && !F_ISSET(sp, SC_EX_GLOBAL)) {
558 ecp->addrcnt = 1;
559 ecp->addr1.lno = sp->lno + 1;
560 ecp->addr1.cno = sp->cno;
563 F_SET(ecp, E_USELASTCMD);
567 * !!!
568 * Historically, the number option applied to both ex and vi. One
569 * strangeness was that ex didn't switch display formats until a
570 * command was entered, e.g. <CR>'s after the set didn't change to
571 * the new format, but :1p would.
573 if (O_ISSET(sp, O_NUMBER)) {
574 F_SET(ecp, E_OPTNUM);
575 FL_SET(ecp->iflags, E_C_HASH);
576 } else
577 F_CLR(ecp, E_OPTNUM);
579 /* Check for ex mode legality. */
580 if (F_ISSET(sp, SC_EX) && (F_ISSET(ecp->cmd, E_VIONLY) || newscreen)) {
581 msgq(sp, M_ERR,
582 "082|%s: command not available in ex mode", ecp->cmd->name);
583 goto err;
586 /* Add standard command flags. */
587 F_SET(ecp, ecp->cmd->flags);
588 if (!newscreen)
589 F_CLR(ecp, E_NEWSCREEN);
592 * There are three normal termination cases for an ex command. They
593 * are the end of the string (ecp->clen), or unescaped (by <literal
594 * next> characters) <newline> or '|' characters. As we're now past
595 * possible addresses, we can determine how long the command is, so we
596 * don't have to look for all the possible terminations. Naturally,
597 * there are some exciting special cases:
599 * 1: The bang, global, v and the filter versions of the read and
600 * write commands are delimited by <newline>s (they can contain
601 * shell pipes).
602 * 2: The ex, edit, next and visual in vi mode commands all take ex
603 * commands as their first arguments.
604 * 3: The s command takes an RE as its first argument, and wants it
605 * to be specially delimited.
607 * Historically, '|' characters in the first argument of the ex, edit,
608 * next, vi visual, and s commands didn't delimit the command. And,
609 * in the filter cases for read and write, and the bang, global and v
610 * commands, they did not delimit the command at all.
612 * For example, the following commands were legal:
614 * :edit +25|s/abc/ABC/ file.c
615 * :s/|/PIPE/
616 * :read !spell % | columnate
617 * :global/pattern/p|l
619 * It's not quite as simple as it sounds, however. The command:
621 * :s/a/b/|s/c/d|set
623 * was also legal, i.e. the historic ex parser (using the word loosely,
624 * since "parser" implies some regularity of syntax) delimited the RE's
625 * based on its delimiter and not anything so irretrievably vulgar as a
626 * command syntax.
628 * Anyhow, the following code makes this all work. First, for the
629 * special cases we move past their special argument(s). Then, we
630 * do normal command processing on whatever is left. Barf-O-Rama.
632 discard = 0; /* Characters discarded from the command. */
633 arg1_len = 0;
634 ecp->save_cmd = ecp->cp;
635 if (ecp->cmd == &cmds[C_EDIT] || ecp->cmd == &cmds[C_EX] ||
636 ecp->cmd == &cmds[C_NEXT] || ecp->cmd == &cmds[C_VISUAL_VI] ||
637 ecp->cmd == &cmds[C_VSPLIT]) {
639 * Move to the next non-whitespace character. A '!'
640 * immediately following the command is eaten as a
641 * force flag.
643 if (ecp->clen > 0 && *ecp->cp == '!') {
644 ++ecp->cp;
645 --ecp->clen;
646 FL_SET(ecp->iflags, E_C_FORCE);
648 /* Reset, don't reparse. */
649 ecp->save_cmd = ecp->cp;
651 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
652 if (!isblank(*ecp->cp))
653 break;
655 * QUOTING NOTE:
657 * The historic implementation ignored all escape characters
658 * so there was no way to put a space or newline into the +cmd
659 * field. We do a simplistic job of fixing it by moving to the
660 * first whitespace character that isn't escaped. The escaping
661 * characters are stripped as no longer useful.
663 if (ecp->clen > 0 && *ecp->cp == '+') {
664 ++ecp->cp;
665 --ecp->clen;
666 for (arg1 = p = ecp->cp;
667 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
668 ch = *ecp->cp;
669 if (IS_ESCAPE(sp, ecp, ch) &&
670 ecp->clen > 1) {
671 ++discard;
672 --ecp->clen;
673 ch = *++ecp->cp;
674 } else if (isblank(ch))
675 break;
676 *p++ = ch;
678 arg1_len = ecp->cp - arg1;
680 /* Reset, so the first argument isn't reparsed. */
681 ecp->save_cmd = ecp->cp;
683 } else if (ecp->cmd == &cmds[C_BANG] ||
684 ecp->cmd == &cmds[C_GLOBAL] || ecp->cmd == &cmds[C_V]) {
686 * QUOTING NOTE:
688 * We use backslashes to escape <newline> characters, although
689 * this wasn't historic practice for the bang command. It was
690 * for the global and v commands, and it's common usage when
691 * doing text insert during the command. Escaping characters
692 * are stripped as no longer useful.
694 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
695 ch = *ecp->cp;
696 if (ch == '\\' && ecp->clen > 1 && ecp->cp[1] == '\n') {
697 ++discard;
698 --ecp->clen;
699 ch = *++ecp->cp;
701 ++wp->if_lno;
702 ++ecp->if_lno;
703 } else if (ch == '\n')
704 break;
705 *p++ = ch;
707 } else if (ecp->cmd == &cmds[C_READ] || ecp->cmd == &cmds[C_WRITE]) {
709 * For write commands, if the next character is a <blank>, and
710 * the next non-blank character is a '!', it's a filter command
711 * and we want to eat everything up to the <newline>. For read
712 * commands, if the next non-blank character is a '!', it's a
713 * filter command and we want to eat everything up to the next
714 * <newline>. Otherwise, we're done.
716 for (tmp = 0; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
717 ch = *ecp->cp;
718 if (isblank(ch))
719 tmp = 1;
720 else
721 break;
723 if (ecp->clen > 0 && ch == '!' &&
724 (ecp->cmd == &cmds[C_READ] || tmp))
725 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
726 if (ecp->cp[0] == '\n')
727 break;
728 } else if (ecp->cmd == &cmds[C_SUBSTITUTE]) {
730 * Move to the next non-whitespace character, we'll use it as
731 * the delimiter. If the character isn't an alphanumeric or
732 * a '|', it's the delimiter, so parse it. Otherwise, we're
733 * into something like ":s g", so use the special s command.
735 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
736 if (!isblank(ecp->cp[0]))
737 break;
739 if (isalnum(ecp->cp[0]) || ecp->cp[0] == '|') {
740 ecp->rcmd = cmds[C_SUBSTITUTE];
741 ecp->rcmd.fn = ex_subagain;
742 ecp->cmd = &ecp->rcmd;
743 } else if (ecp->clen > 0) {
745 * QUOTING NOTE:
747 * Backslashes quote delimiter characters for RE's.
748 * The backslashes are NOT removed since they'll be
749 * used by the RE code. Move to the third delimiter
750 * that's not escaped (or the end of the command).
752 delim = *ecp->cp;
753 ++ecp->cp;
754 --ecp->clen;
755 for (cnt = 2; ecp->clen > 0 &&
756 cnt != 0; --ecp->clen, ++ecp->cp)
757 if (ecp->cp[0] == '\\' &&
758 ecp->clen > 1) {
759 ++ecp->cp;
760 --ecp->clen;
761 } else if (ecp->cp[0] == delim)
762 --cnt;
767 * Use normal quoting and termination rules to find the end of this
768 * command.
770 * QUOTING NOTE:
772 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
773 * file. It was almost certainly a bug, but that's what bug-for-bug
774 * compatibility means, Grasshopper. Also, ^V's escape the command
775 * delimiters. Literal next quote characters in front of the newlines,
776 * '|' characters or literal next characters are stripped as they're
777 * no longer useful.
779 vi_address = ecp->clen != 0 && ecp->cp[0] != '\n';
780 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
781 ch = ecp->cp[0];
782 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
783 tmp = ecp->cp[1];
784 if (tmp == '\n' || tmp == '|') {
785 if (tmp == '\n') {
786 ++wp->if_lno;
787 ++ecp->if_lno;
789 ++discard;
790 --ecp->clen;
791 ++ecp->cp;
792 ch = tmp;
794 } else if (ch == '\n' || ch == '|') {
795 if (ch == '\n')
796 F_SET(ecp, E_NEWLINE);
797 --ecp->clen;
798 break;
800 *p++ = ch;
804 * Save off the next command information, go back to the
805 * original start of the command.
807 p = ecp->cp + 1;
808 ecp->cp = ecp->save_cmd;
809 ecp->save_cmd = p;
810 ecp->save_cmdlen = ecp->clen;
811 ecp->clen = ((ecp->save_cmd - ecp->cp) - 1) - discard;
814 * QUOTING NOTE:
816 * The "set tags" command historically used a backslash, not the
817 * user's literal next character, to escape whitespace. Handle
818 * it here instead of complicating the argv_exp3() code. Note,
819 * this isn't a particularly complex trap, and if backslashes were
820 * legal in set commands, this would have to be much more complicated.
822 if (ecp->cmd == &cmds[C_SET])
823 for (p = ecp->cp, len = ecp->clen; len > 0; --len, ++p)
824 if (*p == '\\')
825 *p = CH_LITERAL;
828 * Set the default addresses. It's an error to specify an address for
829 * a command that doesn't take them. If two addresses are specified
830 * for a command that only takes one, lose the first one. Two special
831 * cases here, some commands take 0 or 2 addresses. For most of them
832 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
833 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
835 * Also, if the file is empty, some commands want to use an address of
836 * 0, i.e. the entire file is 0 to 0, and the default first address is
837 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
838 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
839 * case where the 0 address is only valid if it's a default address.
841 * Also, set a flag if we set the default addresses. Some commands
842 * (ex: z) care if the user specified an address or if we just used
843 * the current cursor.
845 switch (F_ISSET(ecp, E_ADDR1 | E_ADDR2 | E_ADDR2_ALL | E_ADDR2_NONE)) {
846 case E_ADDR1: /* One address: */
847 switch (ecp->addrcnt) {
848 case 0: /* Default cursor/empty file. */
849 ecp->addrcnt = 1;
850 F_SET(ecp, E_ADDR_DEF);
851 if (F_ISSET(ecp, E_ADDR_ZERODEF)) {
852 if (db_last(sp, &lno))
853 goto err;
854 if (lno == 0) {
855 ecp->addr1.lno = 0;
856 F_SET(ecp, E_ADDR_ZERO);
857 } else
858 ecp->addr1.lno = sp->lno;
859 } else
860 ecp->addr1.lno = sp->lno;
861 ecp->addr1.cno = sp->cno;
862 break;
863 case 1:
864 break;
865 case 2: /* Lose the first address. */
866 ecp->addrcnt = 1;
867 ecp->addr1 = ecp->addr2;
869 break;
870 case E_ADDR2_NONE: /* Zero/two addresses: */
871 if (ecp->addrcnt == 0) /* Default to nothing. */
872 break;
873 goto two_addr;
874 case E_ADDR2_ALL: /* Zero/two addresses: */
875 if (ecp->addrcnt == 0) { /* Default entire/empty file. */
876 F_SET(ecp, E_ADDR_DEF);
877 ecp->addrcnt = 2;
878 if (sp->ep == NULL)
879 ecp->addr2.lno = 0;
880 else if (db_last(sp, &ecp->addr2.lno))
881 goto err;
882 if (F_ISSET(ecp, E_ADDR_ZERODEF) &&
883 ecp->addr2.lno == 0) {
884 ecp->addr1.lno = 0;
885 F_SET(ecp, E_ADDR_ZERO);
886 } else
887 ecp->addr1.lno = 1;
888 ecp->addr1.cno = ecp->addr2.cno = 0;
889 F_SET(ecp, E_ADDR2_ALL);
890 break;
892 /* FALLTHROUGH */
893 case E_ADDR2: /* Two addresses: */
894 two_addr: switch (ecp->addrcnt) {
895 case 0: /* Default cursor/empty file. */
896 ecp->addrcnt = 2;
897 F_SET(ecp, E_ADDR_DEF);
898 if (sp->lno == 1 &&
899 F_ISSET(ecp, E_ADDR_ZERODEF)) {
900 if (db_last(sp, &lno))
901 goto err;
902 if (lno == 0) {
903 ecp->addr1.lno = ecp->addr2.lno = 0;
904 F_SET(ecp, E_ADDR_ZERO);
905 } else
906 ecp->addr1.lno =
907 ecp->addr2.lno = sp->lno;
908 } else
909 ecp->addr1.lno = ecp->addr2.lno = sp->lno;
910 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
911 break;
912 case 1: /* Default to first address. */
913 ecp->addrcnt = 2;
914 ecp->addr2 = ecp->addr1;
915 break;
916 case 2:
917 break;
919 break;
920 default:
921 if (ecp->addrcnt) /* Error. */
922 goto usage;
926 * !!!
927 * The ^D scroll command historically scrolled the value of the scroll
928 * option or to EOF. It was an error if the cursor was already at EOF.
929 * (Leading addresses were permitted, but were then ignored.)
931 if (ecp->cmd == &cmds[C_SCROLL]) {
932 ecp->addrcnt = 2;
933 ecp->addr1.lno = sp->lno + 1;
934 ecp->addr2.lno = sp->lno + O_VAL(sp, O_SCROLL);
935 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
936 if (db_last(sp, &lno))
937 goto err;
938 if (lno != 0 && lno > sp->lno && ecp->addr2.lno > lno)
939 ecp->addr2.lno = lno;
942 ecp->flagoff = 0;
943 for (p = ecp->cmd->syntax; *p != '\0'; ++p) {
945 * The force flag is sensitive to leading whitespace, i.e.
946 * "next !" is different from "next!". Handle it before
947 * skipping leading <blank>s.
949 if (*p == '!') {
950 if (ecp->clen > 0 && *ecp->cp == '!') {
951 ++ecp->cp;
952 --ecp->clen;
953 FL_SET(ecp->iflags, E_C_FORCE);
955 continue;
958 /* Skip leading <blank>s. */
959 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
960 if (!isblank(*ecp->cp))
961 break;
962 if (ecp->clen == 0)
963 break;
965 switch (*p) {
966 case '1': /* +, -, #, l, p */
968 * !!!
969 * Historically, some flags were ignored depending
970 * on where they occurred in the command line. For
971 * example, in the command, ":3+++p--#", historic vi
972 * acted on the '#' flag, but ignored the '-' flags.
973 * It's unambiguous what the flags mean, so we just
974 * handle them regardless of the stupidity of their
975 * location.
977 for (; ecp->clen; --ecp->clen, ++ecp->cp)
978 switch (*ecp->cp) {
979 case '+':
980 ++ecp->flagoff;
981 break;
982 case '-':
983 case '^':
984 --ecp->flagoff;
985 break;
986 case '#':
987 F_CLR(ecp, E_OPTNUM);
988 FL_SET(ecp->iflags, E_C_HASH);
989 exp->fdef |= E_C_HASH;
990 break;
991 case 'l':
992 FL_SET(ecp->iflags, E_C_LIST);
993 exp->fdef |= E_C_LIST;
994 break;
995 case 'p':
996 FL_SET(ecp->iflags, E_C_PRINT);
997 exp->fdef |= E_C_PRINT;
998 break;
999 default:
1000 goto end_case1;
1002 end_case1: break;
1003 case '2': /* -, ., +, ^ */
1004 case '3': /* -, ., +, ^, = */
1005 for (; ecp->clen; --ecp->clen, ++ecp->cp)
1006 switch (*ecp->cp) {
1007 case '-':
1008 FL_SET(ecp->iflags, E_C_DASH);
1009 break;
1010 case '.':
1011 FL_SET(ecp->iflags, E_C_DOT);
1012 break;
1013 case '+':
1014 FL_SET(ecp->iflags, E_C_PLUS);
1015 break;
1016 case '^':
1017 FL_SET(ecp->iflags, E_C_CARAT);
1018 break;
1019 case '=':
1020 if (*p == '3') {
1021 FL_SET(ecp->iflags, E_C_EQUAL);
1022 break;
1024 /* FALLTHROUGH */
1025 default:
1026 goto end_case23;
1028 end_case23: break;
1029 case 'b': /* buffer */
1031 * !!!
1032 * Historically, "d #" was a delete with a flag, not a
1033 * delete into the '#' buffer. If the current command
1034 * permits a flag, don't use one as a buffer. However,
1035 * the 'l' and 'p' flags were legal buffer names in the
1036 * historic ex, and were used as buffers, not flags.
1038 if ((ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1039 ecp->cp[0] == '^' || ecp->cp[0] == '#') &&
1040 strchr(p, '1') != NULL)
1041 break;
1043 * !!!
1044 * Digits can't be buffer names in ex commands, or the
1045 * command "d2" would be a delete into buffer '2', and
1046 * not a two-line deletion.
1048 if (!isdigit(ecp->cp[0])) {
1049 ecp->buffer = *ecp->cp;
1050 ++ecp->cp;
1051 --ecp->clen;
1052 FL_SET(ecp->iflags, E_C_BUFFER);
1054 break;
1055 case 'c': /* count [01+a] */
1056 ++p;
1057 /* Validate any signed value. */
1058 if (!isdigit(*ecp->cp) && (*p != '+' ||
1059 (*ecp->cp != '+' && *ecp->cp != '-')))
1060 break;
1061 /* If a signed value, set appropriate flags. */
1062 if (*ecp->cp == '-')
1063 FL_SET(ecp->iflags, E_C_COUNT_NEG);
1064 else if (*ecp->cp == '+')
1065 FL_SET(ecp->iflags, E_C_COUNT_POS);
1066 if ((nret =
1067 nget_slong(&ltmp, ecp->cp, &t, 10)) != NUM_OK) {
1068 ex_badaddr(sp, NULL, A_NOTSET, nret);
1069 goto err;
1071 if (ltmp == 0 && *p != '0') {
1072 msgq(sp, M_ERR, "083|Count may not be zero");
1073 goto err;
1075 ecp->clen -= (t - ecp->cp);
1076 ecp->cp = t;
1079 * Counts as address offsets occur in commands taking
1080 * two addresses. Historic vi practice was to use
1081 * the count as an offset from the *second* address.
1083 * Set a count flag; some underlying commands (see
1084 * join) do different things with counts than with
1085 * line addresses.
1087 if (*p == 'a') {
1088 ecp->addr1 = ecp->addr2;
1089 ecp->addr2.lno = ecp->addr1.lno + ltmp - 1;
1090 } else
1091 ecp->count = ltmp;
1092 FL_SET(ecp->iflags, E_C_COUNT);
1093 break;
1094 case 'f': /* file */
1095 if (argv_exp2(sp, ecp, ecp->cp, ecp->clen))
1096 goto err;
1097 goto arg_cnt_chk;
1098 case 'l': /* line */
1100 * Get a line specification.
1102 * If the line was a search expression, we may have
1103 * changed state during the call, and we're now
1104 * searching the file. Push ourselves onto the state
1105 * stack.
1107 if (ex_line(sp, ecp, &cur, &isaddr, &tmp))
1108 goto rfail;
1109 if (tmp)
1110 goto err;
1112 /* Line specifications are always required. */
1113 if (!isaddr) {
1114 msgq_str(sp, M_ERR, ecp->cp,
1115 "084|%s: bad line specification");
1116 goto err;
1119 * The target line should exist for these commands,
1120 * but 0 is legal for them as well.
1122 if (cur.lno != 0 && !db_exist(sp, cur.lno)) {
1123 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1124 goto err;
1126 ecp->lineno = cur.lno;
1127 break;
1128 case 'S': /* string, file exp. */
1129 if (ecp->clen != 0) {
1130 if (argv_exp1(sp, ecp, ecp->cp,
1131 ecp->clen, ecp->cmd == &cmds[C_BANG]))
1132 goto err;
1133 goto addr_verify;
1135 /* FALLTHROUGH */
1136 case 's': /* string */
1137 if (argv_exp0(sp, ecp, ecp->cp, ecp->clen))
1138 goto err;
1139 goto addr_verify;
1140 case 'W': /* word string */
1142 * QUOTING NOTE:
1144 * Literal next characters escape the following
1145 * character. Quoting characters are stripped here
1146 * since they are no longer useful.
1148 * First there was the word.
1150 for (p = t = ecp->cp;
1151 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
1152 ch = *ecp->cp;
1153 if (IS_ESCAPE(sp,
1154 ecp, ch) && ecp->clen > 1) {
1155 --ecp->clen;
1156 *p++ = *++ecp->cp;
1157 } else if (isblank(ch)) {
1158 ++ecp->cp;
1159 --ecp->clen;
1160 break;
1161 } else
1162 *p++ = ch;
1164 if (argv_exp0(sp, ecp, t, p - t))
1165 goto err;
1167 /* Delete intervening whitespace. */
1168 for (; ecp->clen > 0;
1169 --ecp->clen, ++ecp->cp) {
1170 ch = *ecp->cp;
1171 if (!isblank(ch))
1172 break;
1174 if (ecp->clen == 0)
1175 goto usage;
1177 /* Followed by the string. */
1178 for (p = t = ecp->cp; ecp->clen > 0;
1179 --ecp->clen, ++ecp->cp, ++p) {
1180 ch = *ecp->cp;
1181 if (IS_ESCAPE(sp,
1182 ecp, ch) && ecp->clen > 1) {
1183 --ecp->clen;
1184 *p = *++ecp->cp;
1185 } else
1186 *p = ch;
1188 if (argv_exp0(sp, ecp, t, p - t))
1189 goto err;
1190 goto addr_verify;
1191 case 'w': /* word */
1192 if (argv_exp3(sp, ecp, ecp->cp, ecp->clen))
1193 goto err;
1194 arg_cnt_chk: if (*++p != 'N') { /* N */
1196 * If a number is specified, must either be
1197 * 0 or that number, if optional, and that
1198 * number, if required.
1200 tmp = *p - '0';
1201 if ((*++p != 'o' || exp->argsoff != 0) &&
1202 exp->argsoff != tmp)
1203 goto usage;
1205 goto addr_verify;
1206 default:
1207 msgq(sp, M_ERR,
1208 "085|Internal syntax table error (%s: %s)",
1209 ecp->cmd->name, KEY_NAME(sp, *p));
1213 /* Skip trailing whitespace. */
1214 for (; ecp->clen > 0; --ecp->clen) {
1215 ch = *ecp->cp++;
1216 if (!isblank(ch))
1217 break;
1221 * There shouldn't be anything left, and no more required fields,
1222 * i.e neither 'l' or 'r' in the syntax string.
1224 if (ecp->clen != 0 || strpbrk(p, "lr")) {
1225 usage: msgq(sp, M_ERR, "086|Usage: %s", ecp->cmd->usage);
1226 goto err;
1230 * Verify that the addresses are legal. Check the addresses here,
1231 * because this is a place where all ex addresses pass through.
1232 * (They don't all pass through ex_line(), for instance.) We're
1233 * assuming that any non-existent line doesn't exist because it's
1234 * past the end-of-file. That's a pretty good guess.
1236 * If it's a "default vi command", an address of zero is okay.
1238 addr_verify:
1239 switch (ecp->addrcnt) {
1240 case 2:
1242 * Historic ex/vi permitted commands with counts to go past
1243 * EOF. So, for example, if the file only had 5 lines, the
1244 * ex command "1,6>" would fail, but the command ">300"
1245 * would succeed. Since we don't want to have to make all
1246 * of the underlying commands handle random line numbers,
1247 * fix it here.
1249 if (ecp->addr2.lno == 0) {
1250 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1251 (F_ISSET(sp, SC_EX) ||
1252 !F_ISSET(ecp, E_USELASTCMD))) {
1253 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1254 goto err;
1256 } else if (!db_exist(sp, ecp->addr2.lno))
1257 if (FL_ISSET(ecp->iflags, E_C_COUNT)) {
1258 if (db_last(sp, &lno))
1259 goto err;
1260 ecp->addr2.lno = lno;
1261 } else {
1262 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1263 goto err;
1265 /* FALLTHROUGH */
1266 case 1:
1267 if (ecp->addr1.lno == 0) {
1268 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1269 (F_ISSET(sp, SC_EX) ||
1270 !F_ISSET(ecp, E_USELASTCMD))) {
1271 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1272 goto err;
1274 } else if (!db_exist(sp, ecp->addr1.lno)) {
1275 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1276 goto err;
1278 break;
1282 * If doing a default command and there's nothing left on the line,
1283 * vi just moves to the line. For example, ":3" and ":'a,'b" just
1284 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1286 * !!!
1287 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1288 * the line.
1290 * !!!
1291 * This is done before the absolute mark gets set; historically,
1292 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1294 if ((F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_NOPRDEF)) &&
1295 F_ISSET(ecp, E_USELASTCMD) && vi_address == 0) {
1296 switch (ecp->addrcnt) {
1297 case 2:
1298 if (sp->lno !=
1299 (ecp->addr2.lno ? ecp->addr2.lno : 1)) {
1300 sp->lno =
1301 ecp->addr2.lno ? ecp->addr2.lno : 1;
1302 sp->cno = 0;
1303 (void)nonblank(sp, sp->lno, &sp->cno);
1305 break;
1306 case 1:
1307 if (sp->lno !=
1308 (ecp->addr1.lno ? ecp->addr1.lno : 1)) {
1309 sp->lno =
1310 ecp->addr1.lno ? ecp->addr1.lno : 1;
1311 sp->cno = 0;
1312 (void)nonblank(sp, sp->lno, &sp->cno);
1314 break;
1316 ecp->cp = ecp->save_cmd;
1317 ecp->clen = ecp->save_cmdlen;
1318 goto loop;
1322 * Set the absolute mark -- we have to set it for vi here, in case
1323 * it's a compound command, e.g. ":5p|6" should set the absolute
1324 * mark for vi.
1326 if (F_ISSET(ecp, E_ABSMARK)) {
1327 cur.lno = sp->lno;
1328 cur.cno = sp->cno;
1329 F_CLR(ecp, E_ABSMARK);
1330 if (mark_set(sp, ABSMARK1, &cur, 1))
1331 goto err;
1334 #if defined(DEBUG) && defined(COMLOG)
1335 ex_comlog(sp, ecp);
1336 #endif
1337 /* Increment the command count if not called from vi. */
1338 if (F_ISSET(sp, SC_EX))
1339 ++sp->ccnt;
1342 * If file state available, and not doing a global command,
1343 * log the start of an action.
1345 if (sp->ep != NULL && !F_ISSET(sp, SC_EX_GLOBAL))
1346 (void)log_cursor(sp);
1349 * !!!
1350 * There are two special commands for the purposes of this code: the
1351 * default command (<carriage-return>) or the scrolling commands (^D
1352 * and <EOF>) as the first non-<blank> characters in the line.
1354 * If this is the first command in the command line, we received the
1355 * command from the ex command loop and we're talking to a tty, and
1356 * and there's nothing else on the command line, and it's one of the
1357 * special commands, we move back up to the previous line, and erase
1358 * the prompt character with the output. Since ex runs in canonical
1359 * mode, we don't have to do anything else, a <newline> has already
1360 * been echoed by the tty driver. It's OK if vi calls us -- we won't
1361 * be in ex mode so we'll do nothing.
1363 if (F_ISSET(ecp, E_NRSEP)) {
1364 if (sp->ep != NULL &&
1365 F_ISSET(sp, SC_EX) && !F_ISSET(gp, G_SCRIPTED) &&
1366 (F_ISSET(ecp, E_USELASTCMD) || ecp->cmd == &cmds[C_SCROLL]))
1367 gp->scr_ex_adjust(sp, EX_TERM_SCROLL);
1368 F_CLR(ecp, E_NRSEP);
1372 * Call the underlying function for the ex command.
1374 * XXX
1375 * Interrupts behave like errors, for now.
1377 if (ecp->cmd->fn(sp, ecp) || INTERRUPTED(sp)) {
1378 if (F_ISSET(gp, G_SCRIPTED))
1379 F_SET(sp, SC_EXIT_FORCE);
1380 goto err;
1383 #ifdef DEBUG
1384 /* Make sure no function left global temporary space locked. */
1385 if (F_ISSET(gp, G_TMP_INUSE)) {
1386 F_CLR(gp, G_TMP_INUSE);
1387 msgq(sp, M_ERR, "087|%s: temporary buffer not released",
1388 ecp->cmd->name);
1390 #endif
1392 * Ex displayed the number of lines modified immediately after each
1393 * command, so the command "1,10d|1,10d" would display:
1395 * 10 lines deleted
1396 * 10 lines deleted
1397 * <autoprint line>
1399 * Executing ex commands from vi only reported the final modified
1400 * lines message -- that's wrong enough that we don't match it.
1402 if (F_ISSET(sp, SC_EX))
1403 mod_rpt(sp);
1406 * Integrate any offset parsed by the underlying command, and make
1407 * sure the referenced line exists.
1409 * XXX
1410 * May not match historic practice (which I've never been able to
1411 * completely figure out.) For example, the '=' command from vi
1412 * mode often got the offset wrong, and complained it was too large,
1413 * but didn't seem to have a problem with the cursor. If anyone
1414 * complains, ask them how it's supposed to work, they might know.
1416 if (sp->ep != NULL && ecp->flagoff) {
1417 if (ecp->flagoff < 0) {
1418 if (sp->lno <= -ecp->flagoff) {
1419 msgq(sp, M_ERR,
1420 "088|Flag offset to before line 1");
1421 goto err;
1423 } else {
1424 if (!NPFITS(DB_MAX_RECORDS, sp->lno, ecp->flagoff)) {
1425 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1426 goto err;
1428 if (!db_exist(sp, sp->lno + ecp->flagoff)) {
1429 msgq(sp, M_ERR,
1430 "089|Flag offset past end-of-file");
1431 goto err;
1434 sp->lno += ecp->flagoff;
1438 * If the command executed successfully, we may want to display a line
1439 * based on the autoprint option or an explicit print flag. (Make sure
1440 * that there's a line to display.) Also, the autoprint edit option is
1441 * turned off for the duration of global commands.
1443 if (F_ISSET(sp, SC_EX) && sp->ep != NULL && sp->lno != 0) {
1445 * The print commands have already handled the `print' flags.
1446 * If so, clear them.
1448 if (FL_ISSET(ecp->iflags, E_CLRFLAG))
1449 FL_CLR(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT);
1451 /* If hash set only because of the number option, discard it. */
1452 if (F_ISSET(ecp, E_OPTNUM))
1453 FL_CLR(ecp->iflags, E_C_HASH);
1456 * If there was an explicit flag to display the new cursor line,
1457 * or autoprint is set and a change was made, display the line.
1458 * If any print flags were set use them, else default to print.
1460 LF_INIT(FL_ISSET(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT));
1461 if (!LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT | E_NOAUTO) &&
1462 !F_ISSET(sp, SC_EX_GLOBAL) &&
1463 O_ISSET(sp, O_AUTOPRINT) && F_ISSET(ecp, E_AUTOPRINT))
1464 LF_INIT(E_C_PRINT);
1466 if (LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT)) {
1467 cur.lno = sp->lno;
1468 cur.cno = 0;
1469 (void)ex_print(sp, ecp, &cur, &cur, flags);
1474 * If the command had an associated "+cmd", it has to be executed
1475 * before we finish executing any more of this ex command. For
1476 * example, consider a .exrc file that contains the following lines:
1478 * :set all
1479 * :edit +25 file.c|s/abc/ABC/|1
1480 * :3,5 print
1482 * This can happen more than once -- the historic vi simply hung or
1483 * dropped core, of course. Prepend the + command back into the
1484 * current command and continue. We may have to add an additional
1485 * <literal next> character. We know that it will fit because we
1486 * discarded at least one space and the + character.
1488 if (arg1_len != 0) {
1490 * If the last character of the + command was a <literal next>
1491 * character, it would be treated differently because of the
1492 * append. Quote it, if necessary.
1494 if (IS_ESCAPE(sp, ecp, arg1[arg1_len - 1])) {
1495 *--ecp->save_cmd = CH_LITERAL;
1496 ++ecp->save_cmdlen;
1499 ecp->save_cmd -= arg1_len;
1500 ecp->save_cmdlen += arg1_len;
1501 memcpy(ecp->save_cmd, arg1, arg1_len);
1504 * Any commands executed from a +cmd are executed starting at
1505 * the first column of the last line of the file -- NOT the
1506 * first nonblank.) The main file startup code doesn't know
1507 * that a +cmd was set, however, so it may have put us at the
1508 * top of the file. (Note, this is safe because we must have
1509 * switched files to get here.)
1511 F_SET(ecp, E_MOVETOEND);
1514 /* Update the current command. */
1515 ecp->cp = ecp->save_cmd;
1516 ecp->clen = ecp->save_cmdlen;
1519 * !!!
1520 * If we've changed screens or underlying files, any pending global or
1521 * v command, or @ buffer that has associated addresses, has to be
1522 * discarded. This is historic practice for globals, and necessary for
1523 * @ buffers that had associated addresses.
1525 * Otherwise, if we've changed underlying files, it's not a problem,
1526 * we continue with the rest of the ex command(s), operating on the
1527 * new file. However, if we switch screens (either by exiting or by
1528 * an explicit command), we have no way of knowing where to put output
1529 * messages, and, since we don't control screens here, we could screw
1530 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1531 * times). So, return and continue after we've got a new screen.
1533 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH)) {
1534 at_found = gv_found = 0;
1535 for (ecp = wp->ecq.lh_first;
1536 ecp != NULL; ecp = ecp->q.le_next)
1537 switch (ecp->agv_flags) {
1538 case 0:
1539 case AGV_AT_NORANGE:
1540 break;
1541 case AGV_AT:
1542 if (!at_found) {
1543 at_found = 1;
1544 msgq(sp, M_ERR,
1545 "090|@ with range running when the file/screen changed");
1547 break;
1548 case AGV_GLOBAL:
1549 case AGV_V:
1550 if (!gv_found) {
1551 gv_found = 1;
1552 msgq(sp, M_ERR,
1553 "091|Global/v command running when the file/screen changed");
1555 break;
1556 default:
1557 abort();
1559 if (at_found || gv_found)
1560 goto discard;
1561 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_SSWITCH))
1562 goto rsuccess;
1565 goto loop;
1566 /* NOTREACHED */
1568 err: /*
1569 * On command failure, we discard keys and pending commands remaining,
1570 * as well as any keys that were mapped and waiting. The save_cmdlen
1571 * test is not necessarily correct. If we fail early enough we don't
1572 * know if the entire string was a single command or not. Guess, as
1573 * it's useful to know if commands other than the current one are being
1574 * discarded.
1576 if (ecp->save_cmdlen == 0)
1577 for (; ecp->clen; --ecp->clen) {
1578 ch = *ecp->cp++;
1579 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
1580 --ecp->clen;
1581 ++ecp->cp;
1582 } else if (ch == '\n' || ch == '|') {
1583 if (ecp->clen > 1)
1584 ecp->save_cmdlen = 1;
1585 break;
1588 if (ecp->save_cmdlen != 0 || wp->ecq.lh_first != &wp->excmd) {
1589 discard: msgq(sp, M_BERR,
1590 "092|Ex command failed: pending commands discarded");
1591 ex_discard(sp);
1593 if (v_event_flush(sp, CH_MAPPED))
1594 msgq(sp, M_BERR,
1595 "093|Ex command failed: mapped keys discarded");
1597 rfail: tmp = 1;
1598 if (0)
1599 rsuccess: tmp = 0;
1601 /* Turn off any file name error information. */
1602 wp->if_name = NULL;
1604 /* Turn off the global bit. */
1605 F_CLR(sp, SC_EX_GLOBAL);
1607 return (tmp);
1611 * ex_range --
1612 * Get a line range for ex commands, or perform a vi ex address search.
1614 * PUBLIC: int ex_range __P((SCR *, EXCMD *, int *));
1617 ex_range(sp, ecp, errp)
1618 SCR *sp;
1619 EXCMD *ecp;
1620 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(sp, ecp, mp, isaddrp, errp)
1820 SCR *sp;
1821 EXCMD *ecp;
1822 MARK *mp;
1823 int *isaddrp, *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 *, size_t, char **, u_int));
1831 char *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(&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(&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(sp)
2071 SCR *sp;
2073 WIN *wp;
2074 EXCMD *ecp;
2075 RANGE *rp;
2077 F_CLR(sp, SC_EX_GLOBAL);
2080 * Lose any exhausted commands. We know that the first command
2081 * can't be an AGV command, which makes things a bit easier.
2083 for (wp = sp->wp;;) {
2085 * If we're back to the original structure, leave it around,
2086 * but discard any allocated source name, we've returned to
2087 * the beginning of the command stack.
2089 if ((ecp = wp->ecq.lh_first) == &wp->excmd) {
2090 if (F_ISSET(ecp, E_NAMEDISCARD)) {
2091 free(ecp->if_name);
2092 ecp->if_name = NULL;
2094 return (0);
2098 * ecp->clen will be 0 for the first discarded command, but
2099 * may not be 0 for subsequent ones, e.g. if the original
2100 * command was ":g/xx/@a|s/b/c/", then when we discard the
2101 * command pushed on the stack by the @a, we have to resume
2102 * the global command which included the substitute command.
2104 if (ecp->clen != 0)
2105 return (0);
2108 * If it's an @, global or v command, we may need to continue
2109 * the command on a different line.
2111 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2112 /* Discard any exhausted ranges. */
2113 while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq)
2114 if (rp->start > rp->stop) {
2115 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
2116 free(rp);
2117 } else
2118 break;
2120 /* If there's another range, continue with it. */
2121 if (rp != (void *)&ecp->rq)
2122 break;
2124 /* If it's a global/v command, fix up the last line. */
2125 if (FL_ISSET(ecp->agv_flags,
2126 AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO)
2127 if (db_exist(sp, ecp->range_lno))
2128 sp->lno = ecp->range_lno;
2129 else {
2130 if (db_last(sp, &sp->lno))
2131 return (1);
2132 if (sp->lno == 0)
2133 sp->lno = 1;
2135 free(ecp->o_cp);
2138 /* Discard the EXCMD. */
2139 LIST_REMOVE(ecp, q);
2140 free(ecp);
2144 * We only get here if it's an active @, global or v command. Set
2145 * the current line number, and get a new copy of the command for
2146 * the parser. Note, the original pointer almost certainly moved,
2147 * so we have play games.
2149 ecp->cp = ecp->o_cp;
2150 memcpy(ecp->cp, ecp->cp + ecp->o_clen, ecp->o_clen);
2151 ecp->clen = ecp->o_clen;
2152 ecp->range_lno = sp->lno = rp->start++;
2154 if (FL_ISSET(ecp->agv_flags, AGV_GLOBAL | AGV_V))
2155 F_SET(sp, SC_EX_GLOBAL);
2156 return (0);
2160 * ex_discard --
2161 * Discard any pending ex commands.
2163 static int
2164 ex_discard(sp)
2165 SCR *sp;
2167 WIN *wp;
2168 EXCMD *ecp;
2169 RANGE *rp;
2172 * We know the first command can't be an AGV command, so we don't
2173 * process it specially. We do, however, nail the command itself.
2175 for (wp = sp->wp; (ecp = wp->ecq.lh_first) != &wp->excmd;) {
2176 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2177 while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq) {
2178 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
2179 free(rp);
2181 free(ecp->o_cp);
2183 LIST_REMOVE(ecp, q);
2184 free(ecp);
2186 wp->ecq.lh_first->clen = 0;
2187 return (0);
2191 * ex_unknown --
2192 * Display an unknown command name.
2194 static void
2195 ex_unknown(sp, cmd, len)
2196 SCR *sp;
2197 char *cmd;
2198 size_t len;
2200 size_t blen;
2201 char *bp;
2203 GET_SPACE_GOTO(sp, bp, blen, len + 1);
2204 bp[len] = '\0';
2205 memcpy(bp, cmd, len);
2206 msgq_str(sp, M_ERR, bp, "098|The %s command is unknown");
2207 FREE_SPACE(sp, bp, blen);
2209 alloc_err:
2210 return;
2214 * ex_is_abbrev -
2215 * The vi text input routine needs to know if ex thinks this is an
2216 * [un]abbreviate command, so it can turn off abbreviations. See
2217 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2219 * PUBLIC: int ex_is_abbrev __P((char *, size_t));
2222 ex_is_abbrev(name, len)
2223 char *name;
2224 size_t len;
2226 EXCMDLIST const *cp;
2228 return ((cp = ex_comm_search(name, len)) != NULL &&
2229 (cp == &cmds[C_ABBR] || cp == &cmds[C_UNABBREVIATE]));
2233 * ex_is_unmap -
2234 * The vi text input routine needs to know if ex thinks this is an
2235 * unmap command, so it can turn off input mapping. See the usual
2236 * ranting in the vi/v_txt_ev.c:txt_unmap() routine.
2238 * PUBLIC: int ex_is_unmap __P((char *, size_t));
2241 ex_is_unmap(name, len)
2242 char *name;
2243 size_t len;
2245 EXCMDLIST const *cp;
2248 * The command the vi input routines are really interested in
2249 * is "unmap!", not just unmap.
2251 if (name[len - 1] != '!')
2252 return (0);
2253 --len;
2254 return ((cp = ex_comm_search(name, len)) != NULL &&
2255 cp == &cmds[C_UNMAP]);
2259 * ex_comm_search --
2260 * Search for a command name.
2262 static EXCMDLIST const *
2263 ex_comm_search(name, len)
2264 char *name;
2265 size_t len;
2267 EXCMDLIST const *cp;
2269 for (cp = cmds; cp->name != NULL; ++cp) {
2270 if (cp->name[0] > name[0])
2271 return (NULL);
2272 if (cp->name[0] != name[0])
2273 continue;
2274 if (!memcmp(name, cp->name, len))
2275 return (cp);
2277 return (NULL);
2281 * ex_badaddr --
2282 * Display a bad address message.
2284 * PUBLIC: void ex_badaddr
2285 * PUBLIC: __P((SCR *, EXCMDLIST const *, enum badaddr, enum nresult));
2287 void
2288 ex_badaddr(sp, cp, ba, nret)
2289 SCR *sp;
2290 EXCMDLIST const *cp;
2291 enum badaddr ba;
2292 enum nresult nret;
2294 db_recno_t lno;
2296 switch (nret) {
2297 case NUM_OK:
2298 break;
2299 case NUM_ERR:
2300 msgq(sp, M_SYSERR, NULL);
2301 return;
2302 case NUM_OVER:
2303 msgq(sp, M_ERR, "099|Address value overflow");
2304 return;
2305 case NUM_UNDER:
2306 msgq(sp, M_ERR, "100|Address value underflow");
2307 return;
2311 * When encountering an address error, tell the user if there's no
2312 * underlying file, that's the real problem.
2314 if (sp->ep == NULL) {
2315 ex_emsg(sp, cp ? cp->name : NULL, EXM_NOFILEYET);
2316 return;
2319 switch (ba) {
2320 case A_COMBO:
2321 msgq(sp, M_ERR, "101|Illegal address combination");
2322 break;
2323 case A_EOF:
2324 if (db_last(sp, &lno))
2325 return;
2326 if (lno != 0) {
2327 msgq(sp, M_ERR,
2328 "102|Illegal address: only %lu lines in the file",
2329 lno);
2330 break;
2332 /* FALLTHROUGH */
2333 case A_EMPTY:
2334 msgq(sp, M_ERR, "103|Illegal address: the file is empty");
2335 break;
2336 case A_NOTSET:
2337 abort();
2338 /* NOTREACHED */
2339 case A_ZERO:
2340 msgq(sp, M_ERR,
2341 "104|The %s command doesn't permit an address of 0",
2342 cp->name);
2343 break;
2345 return;
2348 #if defined(DEBUG) && defined(COMLOG)
2350 * ex_comlog --
2351 * Log ex commands.
2353 static void
2354 ex_comlog(sp, ecp)
2355 SCR *sp;
2356 EXCMD *ecp;
2358 vtrace(sp, "ecmd: %s", ecp->cmd->name);
2359 if (ecp->addrcnt > 0) {
2360 vtrace(sp, " a1 %d", ecp->addr1.lno);
2361 if (ecp->addrcnt > 1)
2362 vtrace(sp, " a2: %d", ecp->addr2.lno);
2364 if (ecp->lineno)
2365 vtrace(sp, " line %d", ecp->lineno);
2366 if (ecp->flags)
2367 vtrace(sp, " flags 0x%x", ecp->flags);
2368 if (F_ISSET(&exc, E_BUFFER))
2369 vtrace(sp, " buffer %c", ecp->buffer);
2370 if (ecp->argc)
2371 for (cnt = 0; cnt < ecp->argc; ++cnt)
2372 vtrace(sp, " arg %d: {%s}", cnt, ecp->argv[cnt]->bp);
2373 vtrace(sp, "\n");
2375 #endif