tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / ex / ex.c
blob58540882f179813cc315acadb0f37ac4c6337b8a
1 /* $NetBSD: ex.c,v 1.3 2013/11/25 22:43:46 christos Exp $ */
2 /*-
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #ifndef lint
14 static const char sccsid[] = "Id: ex.c,v 10.75 2004/03/16 14:13:35 skimo Exp (Berkeley) Date: 2004/03/16 14:13:35 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
22 #include <bitstring.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "../common/common.h"
33 #include "../common/multibyte.h"
34 #include "../vi/vi.h"
36 #if defined(DEBUG) && defined(COMLOG)
37 static void ex_comlog __P((SCR *, EXCMD *));
38 #endif
39 static EXCMDLIST const *
40 ex_comm_search __P((SCR *, CHAR_T *, size_t));
41 static int ex_discard __P((SCR *));
42 static int ex_line __P((SCR *, EXCMD *, MARK *, int *, int *));
43 static int ex_load __P((SCR *));
44 static void ex_unknown __P((SCR *, CHAR_T *, size_t));
47 * ex --
48 * Main ex loop.
50 * PUBLIC: int ex __P((SCR **));
52 int
53 ex(SCR **spp)
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;
66 /* Start the ex screen. */
67 if (ex_init(sp))
68 return (1);
70 /* Flush any saved messages. */
71 while ((mp = LIST_FIRST(&gp->msgq)) != 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 = strdup("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 = TAILQ_FIRST(&sp->tiq);
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 ARG_CHAR_T ch;
214 const CHAR_T *n;
215 const char *np;
217 gp = sp->gp;
218 wp = sp->wp;
219 exp = EXP(sp);
220 ch = '\0'; /* XXX: gcc -O1 -Wuninitialized */
223 * We always start running the command on the top of the stack.
224 * This means that *everything* must be resolved when we leave
225 * this function for any reason.
227 loop: ecp = LIST_FIRST(&wp->ecq);
229 /* If we're reading a command from a file, set up error information. */
230 if (ecp->if_name != NULL) {
231 wp->if_lno = ecp->if_lno;
232 wp->if_name = ecp->if_name;
236 * If a move to the end of the file is scheduled for this command,
237 * do it now.
239 if (F_ISSET(ecp, E_MOVETOEND)) {
240 if (db_last(sp, &sp->lno))
241 goto rfail;
242 sp->cno = 0;
243 F_CLR(ecp, E_MOVETOEND);
246 /* If we found a newline, increment the count now. */
247 if (F_ISSET(ecp, E_NEWLINE)) {
248 ++wp->if_lno;
249 ++ecp->if_lno;
250 F_CLR(ecp, E_NEWLINE);
253 /* (Re)initialize the EXCMD structure, preserving some flags. */
254 CLEAR_EX_CMD(ecp);
256 /* Initialize the argument structures. */
257 if (argv_init(sp, ecp))
258 goto err;
260 /* Initialize +cmd, saved command information. */
261 arg1 = NULL;
262 ecp->save_cmdlen = 0;
264 /* Skip <blank>s, empty lines. */
265 for (notempty = 0; ecp->clen > 0; ++ecp->cp, --ecp->clen)
266 if ((ch = (UCHAR_T)*ecp->cp) == '\n') {
267 ++wp->if_lno;
268 ++ecp->if_lno;
269 } else if (ISBLANK(ch))
270 notempty = 1;
271 else
272 break;
275 * !!!
276 * Permit extra colons at the start of the line. Historically,
277 * ex/vi allowed a single extra one. It's simpler not to count.
278 * The stripping is done here because, historically, any command
279 * could have preceding colons, e.g. ":g/pattern/:p" worked.
281 if (ecp->clen != 0 && ch == ':') {
282 notempty = 1;
283 while (--ecp->clen > 0 && (ch = (UCHAR_T)*++ecp->cp) == ':');
287 * Command lines that start with a double-quote are comments.
289 * !!!
290 * Historically, there was no escape or delimiter for a comment, e.g.
291 * :"foo|set was a single comment and nothing was output. Since nvi
292 * permits users to escape <newline> characters into command lines, we
293 * have to check for that case.
295 if (ecp->clen != 0 && ch == '"') {
296 while (--ecp->clen > 0 && *++ecp->cp != '\n');
297 if (*ecp->cp == '\n') {
298 F_SET(ecp, E_NEWLINE);
299 ++ecp->cp;
300 --ecp->clen;
302 goto loop;
305 /* Skip whitespace. */
306 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
307 ch = (UCHAR_T)*ecp->cp;
308 if (!ISBLANK(ch))
309 break;
313 * The last point at which an empty line can mean do nothing.
315 * !!!
316 * Historically, in ex mode, lines containing only <blank> characters
317 * were the same as a single <carriage-return>, i.e. a default command.
318 * In vi mode, they were ignored. In .exrc files this was a serious
319 * annoyance, as vi kept trying to treat them as print commands. We
320 * ignore backward compatibility in this case, discarding lines that
321 * contain only <blank> characters from .exrc files.
323 * !!!
324 * This is where you end up when you're done a command, i.e. clen has
325 * gone to zero. Continue if there are more commands to run.
327 if (ecp->clen == 0 &&
328 (!notempty || F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_BLIGNORE))) {
329 if (ex_load(sp))
330 goto rfail;
331 ecp = LIST_FIRST(&wp->ecq);
332 if (ecp->clen == 0)
333 goto rsuccess;
334 goto loop;
338 * Check to see if this is a command for which we may want to move
339 * the cursor back up to the previous line. (The command :1<CR>
340 * wants a <newline> separator, but the command :<CR> wants to erase
341 * the command line.) If the line is empty except for <blank>s,
342 * <carriage-return> or <eof>, we'll probably want to move up. I
343 * don't think there's any way to get <blank> characters *after* the
344 * command character, but this is the ex parser, and I've been wrong
345 * before.
347 if (F_ISSET(ecp, E_NRSEP) &&
348 ecp->clen != 0 && (ecp->clen != 1 || ecp->cp[0] != '\004'))
349 F_CLR(ecp, E_NRSEP);
351 /* Parse command addresses. */
352 if (ex_range(sp, ecp, &tmp))
353 goto rfail;
354 if (tmp)
355 goto err;
358 * Skip <blank>s and any more colons (the command :3,5:print
359 * worked, historically).
361 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
362 ch = (UCHAR_T)*ecp->cp;
363 if (!ISBLANK(ch) && ch != ':')
364 break;
368 * If no command, ex does the last specified of p, l, or #, and vi
369 * moves to the line. Otherwise, determine the length of the command
370 * name by looking for the first non-alphabetic character. (There
371 * are a few non-alphabetic characters in command names, but they're
372 * all single character commands.) This isn't a great test, because
373 * it means that, for the command ":e +cut.c file", we'll report that
374 * the command "cut" wasn't known. However, it makes ":e+35 file" work
375 * correctly.
377 * !!!
378 * Historically, lines with multiple adjacent (or <blank> separated)
379 * command separators were very strange. For example, the command
380 * |||<carriage-return>, when the cursor was on line 1, displayed
381 * lines 2, 3 and 5 of the file. In addition, the command " | "
382 * would only display the line after the next line, instead of the
383 * next two lines. No ideas why. It worked reasonably when executed
384 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
385 * command for each separator.
387 #define SINGLE_CHAR_COMMANDS "\004!#&*<=>@~"
388 newscreen = 0;
389 if (ecp->clen != 0 && ecp->cp[0] != '|' && ecp->cp[0] != '\n') {
390 if (strchr(SINGLE_CHAR_COMMANDS, *ecp->cp)) {
391 p = ecp->cp;
392 ++ecp->cp;
393 --ecp->clen;
394 namelen = 1;
395 } else {
396 for (p = ecp->cp;
397 ecp->clen > 0; --ecp->clen, ++ecp->cp)
398 if (!ISALPHA((UCHAR_T)*ecp->cp))
399 break;
400 if ((namelen = ecp->cp - p) == 0) {
401 msgq(sp, M_ERR, "080|Unknown command name");
402 goto err;
407 * !!!
408 * Historic vi permitted flags to immediately follow any
409 * subset of the 'delete' command, but then did not permit
410 * further arguments (flag, buffer, count). Make it work.
411 * Permit further arguments for the few shreds of dignity
412 * it offers.
414 * Adding commands that start with 'd', and match "delete"
415 * up to a l, p, +, - or # character can break this code.
417 * !!!
418 * Capital letters beginning the command names ex, edit,
419 * next, previous, tag and visual (in vi mode) indicate the
420 * command should happen in a new screen.
422 switch (p[0]) {
423 case 'd':
424 for (s = p,
425 n = cmds[C_DELETE].name; *s == *n; ++s, ++n);
426 if (s[0] == 'l' || s[0] == 'p' || s[0] == '+' ||
427 s[0] == '-' || s[0] == '^' || s[0] == '#') {
428 len = (ecp->cp - p) - (s - p);
429 ecp->cp -= len;
430 ecp->clen += len;
431 ecp->rcmd = cmds[C_DELETE];
432 ecp->rcmd.syntax = "1bca1";
433 ecp->cmd = &ecp->rcmd;
434 goto skip_srch;
436 break;
437 case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
438 newscreen = 1;
439 p[0] = TOLOWER(p[0]);
440 break;
444 * Search the table for the command.
446 * !!!
447 * Historic vi permitted the mark to immediately follow the
448 * 'k' in the 'k' command. Make it work.
450 * !!!
451 * Historic vi permitted any flag to follow the s command, e.g.
452 * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
453 * Since the following characters all have to be flags, i.e.
454 * alphabetics, we can let the s command routine return errors
455 * if it was some illegal command string. This code will break
456 * if an "sg" or similar command is ever added. The substitute
457 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
458 * follows the 's', but we limit the choices here to "cgr" so
459 * that we get unknown command messages for wrong combinations.
461 if ((ecp->cmd = ex_comm_search(sp, p, namelen)) == NULL)
462 switch (p[0]) {
463 case 'k':
464 if (namelen == 2) {
465 ecp->cp -= namelen - 1;
466 ecp->clen += namelen - 1;
467 ecp->cmd = &cmds[C_K];
468 break;
470 goto unknown;
471 case 's':
472 for (s = p + 1, cnt = namelen; --cnt; ++s)
473 if (s[0] != 'c' &&
474 s[0] != 'g' && s[0] != 'r')
475 break;
476 if (cnt == 0) {
477 ecp->cp -= namelen - 1;
478 ecp->clen += namelen - 1;
479 ecp->rcmd = cmds[C_SUBSTITUTE];
480 ecp->rcmd.fn = ex_subagain;
481 ecp->cmd = &ecp->rcmd;
482 break;
484 /* FALLTHROUGH */
485 default:
486 unknown: if (newscreen)
487 p[0] = TOUPPER((UCHAR_T)p[0]);
488 ex_unknown(sp, p, namelen);
489 goto err;
493 * The visual command has a different syntax when called
494 * from ex than when called from a vi colon command. FMH.
495 * Make the change now, before we test for the newscreen
496 * semantic, so that we're testing the right one.
498 skip_srch: if (ecp->cmd == &cmds[C_VISUAL_EX] && F_ISSET(sp, SC_VI))
499 ecp->cmd = &cmds[C_VISUAL_VI];
502 * !!!
503 * Historic vi permitted a capital 'P' at the beginning of
504 * any command that started with 'p'. Probably wanted the
505 * P[rint] command for backward compatibility, and the code
506 * just made Preserve and Put work by accident. Nvi uses
507 * Previous to mean previous-in-a-new-screen, so be careful.
509 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN) &&
510 (ecp->cmd == &cmds[C_PRINT] ||
511 ecp->cmd == &cmds[C_PRESERVE]))
512 newscreen = 0;
514 /* Test for a newscreen associated with this command. */
515 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN))
516 goto unknown;
518 /* Secure means no shell access. */
519 if (F_ISSET(ecp->cmd, E_SECURE) && O_ISSET(sp, O_SECURE)) {
520 ex_wemsg(sp, ecp->cmd->name, EXM_SECURE);
521 goto err;
525 * Multiple < and > characters; another "feature". Note,
526 * The string passed to the underlying function may not be
527 * nul terminated in this case.
529 if ((ecp->cmd == &cmds[C_SHIFTL] && *p == '<') ||
530 (ecp->cmd == &cmds[C_SHIFTR] && *p == '>')) {
531 for (ch = (UCHAR_T)*p;
532 ecp->clen > 0; --ecp->clen, ++ecp->cp)
533 if (*ecp->cp != ch)
534 break;
535 if (argv_exp0(sp, ecp, p, ecp->cp - p))
536 goto err;
539 /* Set the format style flags for the next command. */
540 if (ecp->cmd == &cmds[C_HASH])
541 exp->fdef = E_C_HASH;
542 else if (ecp->cmd == &cmds[C_LIST])
543 exp->fdef = E_C_LIST;
544 else if (ecp->cmd == &cmds[C_PRINT])
545 exp->fdef = E_C_PRINT;
546 F_CLR(ecp, E_USELASTCMD);
547 } else {
548 /* Print is the default command. */
549 ecp->cmd = &cmds[C_PRINT];
551 /* Set the saved format flags. */
552 F_SET(ecp, exp->fdef);
555 * !!!
556 * If no address was specified, and it's not a global command,
557 * we up the address by one. (I have no idea why globals are
558 * exempted, but it's (ahem) historic practice.)
560 if (ecp->addrcnt == 0 && !F_ISSET(sp, SC_EX_GLOBAL)) {
561 ecp->addrcnt = 1;
562 ecp->addr1.lno = sp->lno + 1;
563 ecp->addr1.cno = sp->cno;
566 F_SET(ecp, E_USELASTCMD);
570 * !!!
571 * Historically, the number option applied to both ex and vi. One
572 * strangeness was that ex didn't switch display formats until a
573 * command was entered, e.g. <CR>'s after the set didn't change to
574 * the new format, but :1p would.
576 if (O_ISSET(sp, O_NUMBER)) {
577 F_SET(ecp, E_OPTNUM);
578 FL_SET(ecp->iflags, E_C_HASH);
579 } else
580 F_CLR(ecp, E_OPTNUM);
582 /* Check for ex mode legality. */
583 if (F_ISSET(sp, SC_EX) && (F_ISSET(ecp->cmd, E_VIONLY) || newscreen)) {
584 msgq_wstr(sp, M_ERR, ecp->cmd->name,
585 "082|%s: command not available in ex mode");
586 goto err;
589 /* Add standard command flags. */
590 F_SET(ecp, ecp->cmd->flags);
591 if (!newscreen)
592 F_CLR(ecp, E_NEWSCREEN);
595 * There are three normal termination cases for an ex command. They
596 * are the end of the string (ecp->clen), or unescaped (by <literal
597 * next> characters) <newline> or '|' characters. As we're now past
598 * possible addresses, we can determine how long the command is, so we
599 * don't have to look for all the possible terminations. Naturally,
600 * there are some exciting special cases:
602 * 1: The bang, global, v and the filter versions of the read and
603 * write commands are delimited by <newline>s (they can contain
604 * shell pipes).
605 * 2: The ex, edit, next and visual in vi mode commands all take ex
606 * commands as their first arguments.
607 * 3: The s command takes an RE as its first argument, and wants it
608 * to be specially delimited.
610 * Historically, '|' characters in the first argument of the ex, edit,
611 * next, vi visual, and s commands didn't delimit the command. And,
612 * in the filter cases for read and write, and the bang, global and v
613 * commands, they did not delimit the command at all.
615 * For example, the following commands were legal:
617 * :edit +25|s/abc/ABC/ file.c
618 * :s/|/PIPE/
619 * :read !spell % | columnate
620 * :global/pattern/p|l
622 * It's not quite as simple as it sounds, however. The command:
624 * :s/a/b/|s/c/d|set
626 * was also legal, i.e. the historic ex parser (using the word loosely,
627 * since "parser" implies some regularity of syntax) delimited the RE's
628 * based on its delimiter and not anything so irretrievably vulgar as a
629 * command syntax.
631 * Anyhow, the following code makes this all work. First, for the
632 * special cases we move past their special argument(s). Then, we
633 * do normal command processing on whatever is left. Barf-O-Rama.
635 discard = 0; /* Characters discarded from the command. */
636 arg1_len = 0;
637 ecp->save_cmd = ecp->cp;
638 if (ecp->cmd == &cmds[C_EDIT] || ecp->cmd == &cmds[C_EX] ||
639 ecp->cmd == &cmds[C_NEXT] || ecp->cmd == &cmds[C_VISUAL_VI] ||
640 ecp->cmd == &cmds[C_VSPLIT]) {
642 * Move to the next non-whitespace character. A '!'
643 * immediately following the command is eaten as a
644 * force flag.
646 if (ecp->clen > 0 && *ecp->cp == '!') {
647 ++ecp->cp;
648 --ecp->clen;
649 FL_SET(ecp->iflags, E_C_FORCE);
651 /* Reset, don't reparse. */
652 ecp->save_cmd = ecp->cp;
654 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
655 if (!ISBLANK(*ecp->cp))
656 break;
658 * QUOTING NOTE:
660 * The historic implementation ignored all escape characters
661 * so there was no way to put a space or newline into the +cmd
662 * field. We do a simplistic job of fixing it by moving to the
663 * first whitespace character that isn't escaped. The escaping
664 * characters are stripped as no longer useful.
666 if (ecp->clen > 0 && *ecp->cp == '+') {
667 ++ecp->cp;
668 --ecp->clen;
669 for (arg1 = p = ecp->cp;
670 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
671 ch = (UCHAR_T)*ecp->cp;
672 if (IS_ESCAPE(sp, ecp, ch) &&
673 ecp->clen > 1) {
674 ++discard;
675 --ecp->clen;
676 ch = (UCHAR_T)*++ecp->cp;
677 } else if (ISBLANK(ch))
678 break;
679 *p++ = ch;
681 arg1_len = ecp->cp - arg1;
683 /* Reset, so the first argument isn't reparsed. */
684 ecp->save_cmd = ecp->cp;
686 } else if (ecp->cmd == &cmds[C_BANG] ||
687 ecp->cmd == &cmds[C_GLOBAL] || ecp->cmd == &cmds[C_V]) {
689 * QUOTING NOTE:
691 * We use backslashes to escape <newline> characters, although
692 * this wasn't historic practice for the bang command. It was
693 * for the global and v commands, and it's common usage when
694 * doing text insert during the command. Escaping characters
695 * are stripped as no longer useful.
697 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
698 ch = (UCHAR_T)*ecp->cp;
699 if (ch == '\\' && ecp->clen > 1 && ecp->cp[1] == '\n') {
700 ++discard;
701 --ecp->clen;
702 ch = (UCHAR_T)*++ecp->cp;
704 ++wp->if_lno;
705 ++ecp->if_lno;
706 } else if (ch == '\n')
707 break;
708 *p++ = ch;
710 } else if (ecp->cmd == &cmds[C_READ] || ecp->cmd == &cmds[C_WRITE]) {
712 * For write commands, if the next character is a <blank>, and
713 * the next non-blank character is a '!', it's a filter command
714 * and we want to eat everything up to the <newline>. For read
715 * commands, if the next non-blank character is a '!', it's a
716 * filter command and we want to eat everything up to the next
717 * <newline>. Otherwise, we're done.
719 for (tmp = 0; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
720 ch = (UCHAR_T)*ecp->cp;
721 if (ISBLANK(ch))
722 tmp = 1;
723 else
724 break;
726 if (ecp->clen > 0 && ch == '!' &&
727 (ecp->cmd == &cmds[C_READ] || tmp))
728 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
729 if (ecp->cp[0] == '\n')
730 break;
731 } else if (ecp->cmd == &cmds[C_SUBSTITUTE]) {
733 * Move to the next non-whitespace character, we'll use it as
734 * the delimiter. If the character isn't an alphanumeric or
735 * a '|', it's the delimiter, so parse it. Otherwise, we're
736 * into something like ":s g", so use the special s command.
738 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
739 if (!ISBLANK(ecp->cp[0]))
740 break;
742 if (ISALNUM((UCHAR_T)ecp->cp[0]) || ecp->cp[0] == '|') {
743 ecp->rcmd = cmds[C_SUBSTITUTE];
744 ecp->rcmd.fn = ex_subagain;
745 ecp->cmd = &ecp->rcmd;
746 } else if (ecp->clen > 0) {
748 * QUOTING NOTE:
750 * Backslashes quote delimiter characters for RE's.
751 * The backslashes are NOT removed since they'll be
752 * used by the RE code. Move to the third delimiter
753 * that's not escaped (or the end of the command).
755 delim = *ecp->cp;
756 ++ecp->cp;
757 --ecp->clen;
758 for (cnt = 2; ecp->clen > 0 &&
759 cnt != 0; --ecp->clen, ++ecp->cp)
760 if (ecp->cp[0] == '\\' &&
761 ecp->clen > 1) {
762 ++ecp->cp;
763 --ecp->clen;
764 } else if (ecp->cp[0] == delim)
765 --cnt;
770 * Use normal quoting and termination rules to find the end of this
771 * command.
773 * QUOTING NOTE:
775 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
776 * file. It was almost certainly a bug, but that's what bug-for-bug
777 * compatibility means, Grasshopper. Also, ^V's escape the command
778 * delimiters. Literal next quote characters in front of the newlines,
779 * '|' characters or literal next characters are stripped as they're
780 * no longer useful.
782 vi_address = ecp->clen != 0 && ecp->cp[0] != '\n';
783 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
784 ch = (UCHAR_T)ecp->cp[0];
785 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
786 ARG_CHAR_T tmp1 = (UCHAR_T)ecp->cp[1];
787 if (tmp1 == '\n' || tmp1 == '|') {
788 if (tmp1 == '\n') {
789 ++wp->if_lno;
790 ++ecp->if_lno;
792 ++discard;
793 --ecp->clen;
794 ++ecp->cp;
795 ch = tmp1;
797 } else if (ch == '\n' || ch == '|') {
798 if (ch == '\n')
799 F_SET(ecp, E_NEWLINE);
800 --ecp->clen;
801 break;
803 *p++ = ch;
807 * Save off the next command information, go back to the
808 * original start of the command.
810 p = ecp->cp + 1;
811 ecp->cp = ecp->save_cmd;
812 ecp->save_cmd = p;
813 ecp->save_cmdlen = ecp->clen;
814 ecp->clen = ((ecp->save_cmd - ecp->cp) - 1) - discard;
817 * QUOTING NOTE:
819 * The "set tags" command historically used a backslash, not the
820 * user's literal next character, to escape whitespace. Handle
821 * it here instead of complicating the argv_exp3() code. Note,
822 * this isn't a particularly complex trap, and if backslashes were
823 * legal in set commands, this would have to be much more complicated.
825 if (ecp->cmd == &cmds[C_SET])
826 for (p = ecp->cp, len = ecp->clen; len > 0; --len, ++p)
827 if (*p == '\\')
828 *p = CH_LITERAL;
831 * Set the default addresses. It's an error to specify an address for
832 * a command that doesn't take them. If two addresses are specified
833 * for a command that only takes one, lose the first one. Two special
834 * cases here, some commands take 0 or 2 addresses. For most of them
835 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
836 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
838 * Also, if the file is empty, some commands want to use an address of
839 * 0, i.e. the entire file is 0 to 0, and the default first address is
840 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
841 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
842 * case where the 0 address is only valid if it's a default address.
844 * Also, set a flag if we set the default addresses. Some commands
845 * (ex: z) care if the user specified an address or if we just used
846 * the current cursor.
848 switch (F_ISSET(ecp, E_ADDR1 | E_ADDR2 | E_ADDR2_ALL | E_ADDR2_NONE)) {
849 case E_ADDR1: /* One address: */
850 switch (ecp->addrcnt) {
851 case 0: /* Default cursor/empty file. */
852 ecp->addrcnt = 1;
853 F_SET(ecp, E_ADDR_DEF);
854 if (F_ISSET(ecp, E_ADDR_ZERODEF)) {
855 if (db_last(sp, &lno))
856 goto err;
857 if (lno == 0) {
858 ecp->addr1.lno = 0;
859 F_SET(ecp, E_ADDR_ZERO);
860 } else
861 ecp->addr1.lno = sp->lno;
862 } else
863 ecp->addr1.lno = sp->lno;
864 ecp->addr1.cno = sp->cno;
865 break;
866 case 1:
867 break;
868 case 2: /* Lose the first address. */
869 ecp->addrcnt = 1;
870 ecp->addr1 = ecp->addr2;
872 break;
873 case E_ADDR2_NONE: /* Zero/two addresses: */
874 if (ecp->addrcnt == 0) /* Default to nothing. */
875 break;
876 goto two_addr;
877 case E_ADDR2_ALL: /* Zero/two addresses: */
878 if (ecp->addrcnt == 0) { /* Default entire/empty file. */
879 F_SET(ecp, E_ADDR_DEF);
880 ecp->addrcnt = 2;
881 if (sp->ep == NULL)
882 ecp->addr2.lno = 0;
883 else if (db_last(sp, &ecp->addr2.lno))
884 goto err;
885 if (F_ISSET(ecp, E_ADDR_ZERODEF) &&
886 ecp->addr2.lno == 0) {
887 ecp->addr1.lno = 0;
888 F_SET(ecp, E_ADDR_ZERO);
889 } else
890 ecp->addr1.lno = 1;
891 ecp->addr1.cno = ecp->addr2.cno = 0;
892 F_SET(ecp, E_ADDR2_ALL);
893 break;
895 /* FALLTHROUGH */
896 case E_ADDR2: /* Two addresses: */
897 two_addr: switch (ecp->addrcnt) {
898 case 0: /* Default cursor/empty file. */
899 ecp->addrcnt = 2;
900 F_SET(ecp, E_ADDR_DEF);
901 if (sp->lno == 1 &&
902 F_ISSET(ecp, E_ADDR_ZERODEF)) {
903 if (db_last(sp, &lno))
904 goto err;
905 if (lno == 0) {
906 ecp->addr1.lno = ecp->addr2.lno = 0;
907 F_SET(ecp, E_ADDR_ZERO);
908 } else
909 ecp->addr1.lno =
910 ecp->addr2.lno = sp->lno;
911 } else
912 ecp->addr1.lno = ecp->addr2.lno = sp->lno;
913 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
914 break;
915 case 1: /* Default to first address. */
916 //ecp->addrcnt = 2; /* XXX Was this needed ??? */
917 ecp->addr2 = ecp->addr1;
918 break;
919 case 2:
920 break;
922 break;
923 default:
924 if (ecp->addrcnt) /* Error. */
925 goto usage;
929 * !!!
930 * The ^D scroll command historically scrolled the value of the scroll
931 * option or to EOF. It was an error if the cursor was already at EOF.
932 * (Leading addresses were permitted, but were then ignored.)
934 if (ecp->cmd == &cmds[C_SCROLL]) {
935 ecp->addrcnt = 2;
936 ecp->addr1.lno = sp->lno + 1;
937 ecp->addr2.lno = sp->lno + O_VAL(sp, O_SCROLL);
938 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
939 if (db_last(sp, &lno))
940 goto err;
941 if (lno != 0 && lno > sp->lno && ecp->addr2.lno > lno)
942 ecp->addr2.lno = lno;
945 ecp->flagoff = 0;
946 for (np = ecp->cmd->syntax; *np != '\0'; ++np) {
948 * The force flag is sensitive to leading whitespace, i.e.
949 * "next !" is different from "next!". Handle it before
950 * skipping leading <blank>s.
952 if (*np == '!') {
953 if (ecp->clen > 0 && *ecp->cp == '!') {
954 ++ecp->cp;
955 --ecp->clen;
956 FL_SET(ecp->iflags, E_C_FORCE);
958 continue;
961 /* Skip leading <blank>s. */
962 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
963 if (!ISBLANK(*ecp->cp))
964 break;
965 if (ecp->clen == 0)
966 break;
968 switch (*np) {
969 case '1': /* +, -, #, l, p */
971 * !!!
972 * Historically, some flags were ignored depending
973 * on where they occurred in the command line. For
974 * example, in the command, ":3+++p--#", historic vi
975 * acted on the '#' flag, but ignored the '-' flags.
976 * It's unambiguous what the flags mean, so we just
977 * handle them regardless of the stupidity of their
978 * location.
980 for (; ecp->clen; --ecp->clen, ++ecp->cp)
981 switch (*ecp->cp) {
982 case '+':
983 ++ecp->flagoff;
984 break;
985 case '-':
986 case '^':
987 --ecp->flagoff;
988 break;
989 case '#':
990 F_CLR(ecp, E_OPTNUM);
991 FL_SET(ecp->iflags, E_C_HASH);
992 exp->fdef |= E_C_HASH;
993 break;
994 case 'l':
995 FL_SET(ecp->iflags, E_C_LIST);
996 exp->fdef |= E_C_LIST;
997 break;
998 case 'p':
999 FL_SET(ecp->iflags, E_C_PRINT);
1000 exp->fdef |= E_C_PRINT;
1001 break;
1002 default:
1003 goto end_case1;
1005 end_case1: break;
1006 case '2': /* -, ., +, ^ */
1007 case '3': /* -, ., +, ^, = */
1008 for (; ecp->clen; --ecp->clen, ++ecp->cp)
1009 switch (*ecp->cp) {
1010 case '-':
1011 FL_SET(ecp->iflags, E_C_DASH);
1012 break;
1013 case '.':
1014 FL_SET(ecp->iflags, E_C_DOT);
1015 break;
1016 case '+':
1017 FL_SET(ecp->iflags, E_C_PLUS);
1018 break;
1019 case '^':
1020 FL_SET(ecp->iflags, E_C_CARAT);
1021 break;
1022 case '=':
1023 if (*np == '3') {
1024 FL_SET(ecp->iflags, E_C_EQUAL);
1025 break;
1027 /* FALLTHROUGH */
1028 default:
1029 goto end_case23;
1031 end_case23: break;
1032 case 'b': /* buffer */
1034 * !!!
1035 * Historically, "d #" was a delete with a flag, not a
1036 * delete into the '#' buffer. If the current command
1037 * permits a flag, don't use one as a buffer. However,
1038 * the 'l' and 'p' flags were legal buffer names in the
1039 * historic ex, and were used as buffers, not flags.
1041 if ((ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1042 ecp->cp[0] == '^' || ecp->cp[0] == '#') &&
1043 strchr(np, '1') != NULL)
1044 break;
1046 * !!!
1047 * Digits can't be buffer names in ex commands, or the
1048 * command "d2" would be a delete into buffer '2', and
1049 * not a two-line deletion.
1051 if (!ISDIGIT((UCHAR_T)ecp->cp[0])) {
1052 ecp->buffer = (UCHAR_T)*ecp->cp;
1053 ++ecp->cp;
1054 --ecp->clen;
1055 FL_SET(ecp->iflags, E_C_BUFFER);
1057 break;
1058 case 'c': /* count [01+a] */
1059 ++np;
1060 /* Validate any signed value. */
1061 if (!ISDIGIT((UCHAR_T)*ecp->cp) && (*np != '+' ||
1062 (*ecp->cp != '+' && *ecp->cp != '-')))
1063 break;
1064 /* If a signed value, set appropriate flags. */
1065 if (*ecp->cp == '-')
1066 FL_SET(ecp->iflags, E_C_COUNT_NEG);
1067 else if (*ecp->cp == '+')
1068 FL_SET(ecp->iflags, E_C_COUNT_POS);
1069 if ((nret =
1070 nget_slong(sp, &ltmp, ecp->cp, &t, 10)) != NUM_OK) {
1071 ex_badaddr(sp, NULL, A_NOTSET, nret);
1072 goto err;
1074 if (ltmp == 0 && *np != '0') {
1075 msgq(sp, M_ERR, "083|Count may not be zero");
1076 goto err;
1078 ecp->clen -= (t - ecp->cp);
1079 ecp->cp = t;
1082 * Counts as address offsets occur in commands taking
1083 * two addresses. Historic vi practice was to use
1084 * the count as an offset from the *second* address.
1086 * Set a count flag; some underlying commands (see
1087 * join) do different things with counts than with
1088 * line addresses.
1090 if (*np == 'a') {
1091 ecp->addr1 = ecp->addr2;
1092 ecp->addr2.lno = ecp->addr1.lno + ltmp - 1;
1093 } else
1094 ecp->count = ltmp;
1095 FL_SET(ecp->iflags, E_C_COUNT);
1096 break;
1097 case 'f': /* file */
1098 if (argv_exp2(sp, ecp, ecp->cp, ecp->clen))
1099 goto err;
1100 goto arg_cnt_chk;
1101 case 'l': /* line */
1103 * Get a line specification.
1105 * If the line was a search expression, we may have
1106 * changed state during the call, and we're now
1107 * searching the file. Push ourselves onto the state
1108 * stack.
1110 if (ex_line(sp, ecp, &cur, &isaddr, &tmp))
1111 goto rfail;
1112 if (tmp)
1113 goto err;
1115 /* Line specifications are always required. */
1116 if (!isaddr) {
1117 msgq_wstr(sp, M_ERR, ecp->cp,
1118 "084|%s: bad line specification");
1119 goto err;
1122 * The target line should exist for these commands,
1123 * but 0 is legal for them as well.
1125 if (cur.lno != 0 && !db_exist(sp, cur.lno)) {
1126 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1127 goto err;
1129 ecp->lineno = cur.lno;
1130 break;
1131 case 'S': /* string, file exp. */
1132 if (ecp->clen != 0) {
1133 if (argv_exp1(sp, ecp, ecp->cp,
1134 ecp->clen, ecp->cmd == &cmds[C_BANG]))
1135 goto err;
1136 goto addr_verify;
1138 /* FALLTHROUGH */
1139 case 's': /* string */
1140 if (argv_exp0(sp, ecp, ecp->cp, ecp->clen))
1141 goto err;
1142 goto addr_verify;
1143 case 'W': /* word string */
1145 * QUOTING NOTE:
1147 * Literal next characters escape the following
1148 * character. Quoting characters are stripped here
1149 * since they are no longer useful.
1151 * First there was the word.
1153 for (p = t = ecp->cp;
1154 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
1155 ch = (UCHAR_T)*ecp->cp;
1156 if (IS_ESCAPE(sp,
1157 ecp, ch) && ecp->clen > 1) {
1158 --ecp->clen;
1159 *p++ = *++ecp->cp;
1160 } else if (ISBLANK(ch)) {
1161 ++ecp->cp;
1162 --ecp->clen;
1163 break;
1164 } else
1165 *p++ = ch;
1167 if (argv_exp0(sp, ecp, t, p - t))
1168 goto err;
1170 /* Delete intervening whitespace. */
1171 for (; ecp->clen > 0;
1172 --ecp->clen, ++ecp->cp) {
1173 ch = (UCHAR_T)*ecp->cp;
1174 if (!ISBLANK(ch))
1175 break;
1177 if (ecp->clen == 0)
1178 goto usage;
1180 /* Followed by the string. */
1181 for (p = t = ecp->cp; ecp->clen > 0;
1182 --ecp->clen, ++ecp->cp, ++p) {
1183 ch = (UCHAR_T)*ecp->cp;
1184 if (IS_ESCAPE(sp,
1185 ecp, ch) && ecp->clen > 1) {
1186 --ecp->clen;
1187 *p = *++ecp->cp;
1188 } else
1189 *p = ch;
1191 if (argv_exp0(sp, ecp, t, p - t))
1192 goto err;
1193 goto addr_verify;
1194 case 'w': /* word */
1195 if (argv_exp3(sp, ecp, ecp->cp, ecp->clen))
1196 goto err;
1197 arg_cnt_chk: if (*++np != 'N') { /* N */
1199 * If a number is specified, must either be
1200 * 0 or that number, if optional, and that
1201 * number, if required.
1203 tmp = *np - '0';
1204 if ((*++np != 'o' || exp->argsoff != 0) &&
1205 exp->argsoff != tmp)
1206 goto usage;
1208 goto addr_verify;
1209 default: {
1210 const char *nstr;
1211 size_t nlen;
1212 INT2CHAR(sp, ecp->cmd->name, STRLEN(ecp->cmd->name) + 1,
1213 nstr, nlen);
1214 msgq(sp, M_ERR,
1215 "085|Internal syntax table error (%s: %s)",
1216 nstr, KEY_NAME(sp, *np));
1221 /* Skip trailing whitespace. */
1222 for (; ecp->clen > 0; --ecp->clen) {
1223 ch = (UCHAR_T)*ecp->cp++;
1224 if (!ISBLANK(ch))
1225 break;
1229 * There shouldn't be anything left, and no more required fields,
1230 * i.e neither 'l' or 'r' in the syntax string.
1232 if (ecp->clen != 0 || strpbrk(np, "lr")) {
1233 usage: msgq(sp, M_ERR, "086|Usage: %s", ecp->cmd->usage);
1234 goto err;
1238 * Verify that the addresses are legal. Check the addresses here,
1239 * because this is a place where all ex addresses pass through.
1240 * (They don't all pass through ex_line(), for instance.) We're
1241 * assuming that any non-existent line doesn't exist because it's
1242 * past the end-of-file. That's a pretty good guess.
1244 * If it's a "default vi command", an address of zero is okay.
1246 addr_verify:
1247 switch (ecp->addrcnt) {
1248 case 2:
1250 * Historic ex/vi permitted commands with counts to go past
1251 * EOF. So, for example, if the file only had 5 lines, the
1252 * ex command "1,6>" would fail, but the command ">300"
1253 * would succeed. Since we don't want to have to make all
1254 * of the underlying commands handle random line numbers,
1255 * fix it here.
1257 if (ecp->addr2.lno == 0) {
1258 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1259 (F_ISSET(sp, SC_EX) ||
1260 !F_ISSET(ecp, E_USELASTCMD))) {
1261 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1262 goto err;
1264 } else if (!db_exist(sp, ecp->addr2.lno)) {
1265 if (FL_ISSET(ecp->iflags, E_C_COUNT)) {
1266 if (db_last(sp, &lno))
1267 goto err;
1268 ecp->addr2.lno = lno;
1269 } else {
1270 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1271 goto err;
1274 /* FALLTHROUGH */
1275 case 1:
1276 if (ecp->addr1.lno == 0) {
1277 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1278 (F_ISSET(sp, SC_EX) ||
1279 !F_ISSET(ecp, E_USELASTCMD))) {
1280 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1281 goto err;
1283 } else if (!db_exist(sp, ecp->addr1.lno)) {
1284 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1285 goto err;
1287 break;
1291 * If doing a default command and there's nothing left on the line,
1292 * vi just moves to the line. For example, ":3" and ":'a,'b" just
1293 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1295 * !!!
1296 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1297 * the line.
1299 * !!!
1300 * This is done before the absolute mark gets set; historically,
1301 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1303 if ((F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_NOPRDEF)) &&
1304 F_ISSET(ecp, E_USELASTCMD) && vi_address == 0) {
1305 switch (ecp->addrcnt) {
1306 case 2:
1307 if (sp->lno !=
1308 (ecp->addr2.lno ? ecp->addr2.lno : 1)) {
1309 sp->lno =
1310 ecp->addr2.lno ? ecp->addr2.lno : 1;
1311 sp->cno = 0;
1312 (void)nonblank(sp, sp->lno, &sp->cno);
1314 break;
1315 case 1:
1316 if (sp->lno !=
1317 (ecp->addr1.lno ? ecp->addr1.lno : 1)) {
1318 sp->lno =
1319 ecp->addr1.lno ? ecp->addr1.lno : 1;
1320 sp->cno = 0;
1321 (void)nonblank(sp, sp->lno, &sp->cno);
1323 break;
1325 ecp->cp = ecp->save_cmd;
1326 ecp->clen = ecp->save_cmdlen;
1327 goto loop;
1331 * Set the absolute mark -- we have to set it for vi here, in case
1332 * it's a compound command, e.g. ":5p|6" should set the absolute
1333 * mark for vi.
1335 if (F_ISSET(ecp, E_ABSMARK)) {
1336 cur.lno = sp->lno;
1337 cur.cno = sp->cno;
1338 F_CLR(ecp, E_ABSMARK);
1339 if (mark_set(sp, ABSMARK1, &cur, 1))
1340 goto err;
1343 #if defined(DEBUG) && defined(COMLOG)
1344 ex_comlog(sp, ecp);
1345 #endif
1346 /* Increment the command count if not called from vi. */
1347 if (F_ISSET(sp, SC_EX))
1348 ++sp->ccnt;
1351 * If file state available, and not doing a global command,
1352 * log the start of an action.
1354 if (sp->ep != NULL && !F_ISSET(sp, SC_EX_GLOBAL))
1355 (void)log_cursor(sp);
1358 * !!!
1359 * There are two special commands for the purposes of this code: the
1360 * default command (<carriage-return>) or the scrolling commands (^D
1361 * and <EOF>) as the first non-<blank> characters in the line.
1363 * If this is the first command in the command line, we received the
1364 * command from the ex command loop and we're talking to a tty, and
1365 * and there's nothing else on the command line, and it's one of the
1366 * special commands, we move back up to the previous line, and erase
1367 * the prompt character with the output. Since ex runs in canonical
1368 * mode, we don't have to do anything else, a <newline> has already
1369 * been echoed by the tty driver. It's OK if vi calls us -- we won't
1370 * be in ex mode so we'll do nothing.
1372 if (F_ISSET(ecp, E_NRSEP)) {
1373 if (sp->ep != NULL &&
1374 F_ISSET(sp, SC_EX) && !F_ISSET(gp, G_SCRIPTED) &&
1375 (F_ISSET(ecp, E_USELASTCMD) || ecp->cmd == &cmds[C_SCROLL]))
1376 gp->scr_ex_adjust(sp, EX_TERM_SCROLL);
1377 F_CLR(ecp, E_NRSEP);
1381 * Call the underlying function for the ex command.
1383 * XXX
1384 * Interrupts behave like errors, for now.
1386 if (ecp->cmd->fn(sp, ecp) || INTERRUPTED(sp)) {
1387 if (F_ISSET(gp, G_SCRIPTED))
1388 F_SET(sp, SC_EXIT_FORCE);
1389 goto err;
1392 #ifdef DEBUG
1393 /* Make sure no function left global temporary space locked. */
1394 if (F_ISSET(wp, W_TMP_INUSE)) {
1395 F_CLR(wp, W_TMP_INUSE);
1396 msgq(sp, M_ERR, "087|%s: temporary buffer not released",
1397 ecp->cmd->name);
1399 #endif
1401 * Ex displayed the number of lines modified immediately after each
1402 * command, so the command "1,10d|1,10d" would display:
1404 * 10 lines deleted
1405 * 10 lines deleted
1406 * <autoprint line>
1408 * Executing ex commands from vi only reported the final modified
1409 * lines message -- that's wrong enough that we don't match it.
1411 if (F_ISSET(sp, SC_EX))
1412 mod_rpt(sp);
1415 * Integrate any offset parsed by the underlying command, and make
1416 * sure the referenced line exists.
1418 * XXX
1419 * May not match historic practice (which I've never been able to
1420 * completely figure out.) For example, the '=' command from vi
1421 * mode often got the offset wrong, and complained it was too large,
1422 * but didn't seem to have a problem with the cursor. If anyone
1423 * complains, ask them how it's supposed to work, they might know.
1425 if (sp->ep != NULL && ecp->flagoff) {
1426 if (ecp->flagoff < 0) {
1427 if (sp->lno <= (db_recno_t)(-ecp->flagoff)) {
1428 msgq(sp, M_ERR,
1429 "088|Flag offset to before line 1");
1430 goto err;
1432 } else {
1433 if (!NPFITS(DB_MAX_RECORDS, sp->lno, (db_recno_t)ecp->flagoff)) {
1434 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1435 goto err;
1437 if (!db_exist(sp, sp->lno + ecp->flagoff)) {
1438 msgq(sp, M_ERR,
1439 "089|Flag offset past end-of-file");
1440 goto err;
1443 sp->lno += ecp->flagoff;
1447 * If the command executed successfully, we may want to display a line
1448 * based on the autoprint option or an explicit print flag. (Make sure
1449 * that there's a line to display.) Also, the autoprint edit option is
1450 * turned off for the duration of global commands.
1452 if (F_ISSET(sp, SC_EX) && sp->ep != NULL && sp->lno != 0) {
1454 * The print commands have already handled the `print' flags.
1455 * If so, clear them.
1457 if (FL_ISSET(ecp->iflags, E_CLRFLAG))
1458 FL_CLR(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT);
1460 /* If hash set only because of the number option, discard it. */
1461 if (F_ISSET(ecp, E_OPTNUM))
1462 FL_CLR(ecp->iflags, E_C_HASH);
1465 * If there was an explicit flag to display the new cursor line,
1466 * or autoprint is set and a change was made, display the line.
1467 * If any print flags were set use them, else default to print.
1469 LF_INIT(FL_ISSET(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT));
1470 if (!LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT | E_NOAUTO) &&
1471 !F_ISSET(sp, SC_EX_GLOBAL) &&
1472 O_ISSET(sp, O_AUTOPRINT) && F_ISSET(ecp, E_AUTOPRINT))
1473 LF_INIT(E_C_PRINT);
1475 if (LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT)) {
1476 cur.lno = sp->lno;
1477 cur.cno = 0;
1478 (void)ex_print(sp, ecp, &cur, &cur, flags);
1483 * If the command had an associated "+cmd", it has to be executed
1484 * before we finish executing any more of this ex command. For
1485 * example, consider a .exrc file that contains the following lines:
1487 * :set all
1488 * :edit +25 file.c|s/abc/ABC/|1
1489 * :3,5 print
1491 * This can happen more than once -- the historic vi simply hung or
1492 * dropped core, of course. Prepend the + command back into the
1493 * current command and continue. We may have to add an additional
1494 * <literal next> character. We know that it will fit because we
1495 * discarded at least one space and the + character.
1497 if (arg1_len != 0) {
1499 * If the last character of the + command was a <literal next>
1500 * character, it would be treated differently because of the
1501 * append. Quote it, if necessary.
1503 if (IS_ESCAPE(sp, ecp, arg1[arg1_len - 1])) {
1504 *--ecp->save_cmd = CH_LITERAL;
1505 ++ecp->save_cmdlen;
1508 ecp->save_cmd -= arg1_len;
1509 ecp->save_cmdlen += arg1_len;
1510 MEMCPYW(ecp->save_cmd, arg1, arg1_len);
1513 * Any commands executed from a +cmd are executed starting at
1514 * the first column of the last line of the file -- NOT the
1515 * first nonblank.) The main file startup code doesn't know
1516 * that a +cmd was set, however, so it may have put us at the
1517 * top of the file. (Note, this is safe because we must have
1518 * switched files to get here.)
1520 F_SET(ecp, E_MOVETOEND);
1523 /* Update the current command. */
1524 ecp->cp = ecp->save_cmd;
1525 ecp->clen = ecp->save_cmdlen;
1528 * !!!
1529 * If we've changed screens or underlying files, any pending global or
1530 * v command, or @ buffer that has associated addresses, has to be
1531 * discarded. This is historic practice for globals, and necessary for
1532 * @ buffers that had associated addresses.
1534 * Otherwise, if we've changed underlying files, it's not a problem,
1535 * we continue with the rest of the ex command(s), operating on the
1536 * new file. However, if we switch screens (either by exiting or by
1537 * an explicit command), we have no way of knowing where to put output
1538 * messages, and, since we don't control screens here, we could screw
1539 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1540 * times). So, return and continue after we've got a new screen.
1542 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH)) {
1543 at_found = gv_found = 0;
1544 LIST_FOREACH(ecp, &wp->ecq, q)
1545 switch (ecp->agv_flags) {
1546 case 0:
1547 case AGV_AT_NORANGE:
1548 break;
1549 case AGV_AT:
1550 if (!at_found) {
1551 at_found = 1;
1552 msgq(sp, M_ERR,
1553 "090|@ with range running when the file/screen changed");
1555 break;
1556 case AGV_GLOBAL:
1557 case AGV_V:
1558 if (!gv_found) {
1559 gv_found = 1;
1560 msgq(sp, M_ERR,
1561 "091|Global/v command running when the file/screen changed");
1563 break;
1564 default:
1565 abort();
1567 if (at_found || gv_found)
1568 goto discard;
1569 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_SSWITCH))
1570 goto rsuccess;
1573 goto loop;
1574 /* NOTREACHED */
1576 err: /*
1577 * On command failure, we discard keys and pending commands remaining,
1578 * as well as any keys that were mapped and waiting. The save_cmdlen
1579 * test is not necessarily correct. If we fail early enough we don't
1580 * know if the entire string was a single command or not. Guess, as
1581 * it's useful to know if commands other than the current one are being
1582 * discarded.
1584 if (ecp->save_cmdlen == 0)
1585 for (; ecp->clen; --ecp->clen) {
1586 ch = (UCHAR_T)*ecp->cp++;
1587 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
1588 --ecp->clen;
1589 ++ecp->cp;
1590 } else if (ch == '\n' || ch == '|') {
1591 if (ecp->clen > 1)
1592 ecp->save_cmdlen = 1;
1593 break;
1596 if (ecp->save_cmdlen != 0 || LIST_FIRST(&wp->ecq) != &wp->excmd) {
1597 discard: msgq(sp, M_BERR,
1598 "092|Ex command failed: pending commands discarded");
1599 ex_discard(sp);
1601 if (v_event_flush(sp, CH_MAPPED))
1602 msgq(sp, M_BERR,
1603 "093|Ex command failed: mapped keys discarded");
1605 rfail: tmp = 1;
1606 if (0)
1607 rsuccess: tmp = 0;
1609 /* Turn off any file name error information. */
1610 wp->if_name = NULL;
1612 /* Turn off the global bit. */
1613 F_CLR(sp, SC_EX_GLOBAL);
1615 return (tmp);
1619 * ex_range --
1620 * Get a line range for ex commands, or perform a vi ex address search.
1622 * PUBLIC: int ex_range __P((SCR *, EXCMD *, int *));
1625 ex_range(SCR *sp, EXCMD *ecp, int *errp)
1627 enum { ADDR_FOUND, ADDR_NEED, ADDR_NONE } addr;
1628 MARK m;
1629 int isaddr;
1631 *errp = 0;
1634 * Parse comma or semi-colon delimited line specs.
1636 * Semi-colon delimiters update the current address to be the last
1637 * address. For example, the command
1639 * :3;/pattern/ecp->cp
1641 * will search for pattern from line 3. In addition, if ecp->cp
1642 * is not a valid command, the current line will be left at 3, not
1643 * at the original address.
1645 * Extra addresses are discarded, starting with the first.
1647 * !!!
1648 * If any addresses are missing, they default to the current line.
1649 * This was historically true for both leading and trailing comma
1650 * delimited addresses as well as for trailing semicolon delimited
1651 * addresses. For consistency, we make it true for leading semicolon
1652 * addresses as well.
1654 for (addr = ADDR_NONE, ecp->addrcnt = 0; ecp->clen > 0;)
1655 switch (*ecp->cp) {
1656 case '%': /* Entire file. */
1657 /* Vi ex address searches didn't permit % signs. */
1658 if (F_ISSET(ecp, E_VISEARCH))
1659 goto ret;
1661 /* It's an error if the file is empty. */
1662 if (sp->ep == NULL) {
1663 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1664 *errp = 1;
1665 return (0);
1668 * !!!
1669 * A percent character addresses all of the lines in
1670 * the file. Historically, it couldn't be followed by
1671 * any other address. We do it as a text substitution
1672 * for simplicity. POSIX 1003.2 is expected to follow
1673 * this practice.
1675 * If it's an empty file, the first line is 0, not 1.
1677 if (addr == ADDR_FOUND) {
1678 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1679 *errp = 1;
1680 return (0);
1682 if (db_last(sp, &ecp->addr2.lno))
1683 return (1);
1684 ecp->addr1.lno = ecp->addr2.lno == 0 ? 0 : 1;
1685 ecp->addr1.cno = ecp->addr2.cno = 0;
1686 ecp->addrcnt = 2;
1687 addr = ADDR_FOUND;
1688 ++ecp->cp;
1689 --ecp->clen;
1690 break;
1691 case ',': /* Comma delimiter. */
1692 /* Vi ex address searches didn't permit commas. */
1693 if (F_ISSET(ecp, E_VISEARCH))
1694 goto ret;
1695 /* FALLTHROUGH */
1696 case ';': /* Semi-colon delimiter. */
1697 if (sp->ep == NULL) {
1698 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1699 *errp = 1;
1700 return (0);
1702 if (addr != ADDR_FOUND)
1703 switch (ecp->addrcnt) {
1704 case 0:
1705 ecp->addr1.lno = sp->lno;
1706 ecp->addr1.cno = sp->cno;
1707 ecp->addrcnt = 1;
1708 break;
1709 case 2:
1710 ecp->addr1 = ecp->addr2;
1711 /* FALLTHROUGH */
1712 case 1:
1713 ecp->addr2.lno = sp->lno;
1714 ecp->addr2.cno = sp->cno;
1715 ecp->addrcnt = 2;
1716 break;
1718 if (*ecp->cp == ';')
1719 switch (ecp->addrcnt) {
1720 case 0:
1721 abort();
1722 /* NOTREACHED */
1723 case 1:
1724 sp->lno = ecp->addr1.lno;
1725 sp->cno = ecp->addr1.cno;
1726 break;
1727 case 2:
1728 sp->lno = ecp->addr2.lno;
1729 sp->cno = ecp->addr2.cno;
1730 break;
1732 addr = ADDR_NEED;
1733 /* FALLTHROUGH */
1734 case ' ': /* Whitespace. */
1735 case '\t': /* Whitespace. */
1736 ++ecp->cp;
1737 --ecp->clen;
1738 break;
1739 default:
1740 /* Get a line specification. */
1741 if (ex_line(sp, ecp, &m, &isaddr, errp))
1742 return (1);
1743 if (*errp)
1744 return (0);
1745 if (!isaddr)
1746 goto ret;
1747 if (addr == ADDR_FOUND) {
1748 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1749 *errp = 1;
1750 return (0);
1752 switch (ecp->addrcnt) {
1753 case 0:
1754 ecp->addr1 = m;
1755 ecp->addrcnt = 1;
1756 break;
1757 case 1:
1758 ecp->addr2 = m;
1759 ecp->addrcnt = 2;
1760 break;
1761 case 2:
1762 ecp->addr1 = ecp->addr2;
1763 ecp->addr2 = m;
1764 break;
1766 addr = ADDR_FOUND;
1767 break;
1771 * !!!
1772 * Vi ex address searches are indifferent to order or trailing
1773 * semi-colons.
1775 ret: if (F_ISSET(ecp, E_VISEARCH))
1776 return (0);
1778 if (addr == ADDR_NEED)
1779 switch (ecp->addrcnt) {
1780 case 0:
1781 ecp->addr1.lno = sp->lno;
1782 ecp->addr1.cno = sp->cno;
1783 ecp->addrcnt = 1;
1784 break;
1785 case 2:
1786 ecp->addr1 = ecp->addr2;
1787 /* FALLTHROUGH */
1788 case 1:
1789 ecp->addr2.lno = sp->lno;
1790 ecp->addr2.cno = sp->cno;
1791 ecp->addrcnt = 2;
1792 break;
1795 if (ecp->addrcnt == 2 && ecp->addr2.lno < ecp->addr1.lno) {
1796 msgq(sp, M_ERR,
1797 "094|The second address is smaller than the first");
1798 *errp = 1;
1800 return (0);
1804 * ex_line --
1805 * Get a single line address specifier.
1807 * The way the "previous context" mark worked was that any "non-relative"
1808 * motion set it. While ex/vi wasn't totally consistent about this, ANY
1809 * numeric address, search pattern, '$', or mark reference in an address
1810 * was considered non-relative, and set the value. Which should explain
1811 * why we're hacking marks down here. The problem was that the mark was
1812 * only set if the command was called, i.e. we have to set a flag and test
1813 * it later.
1815 * XXX
1816 * This is probably still not exactly historic practice, although I think
1817 * it's fairly close.
1819 static int
1820 ex_line(SCR *sp, EXCMD *ecp, MARK *mp, int *isaddrp, int *errp)
1822 enum nresult nret;
1823 long total, val;
1824 unsigned long uval;
1825 int isneg;
1826 int (*sf) __P((SCR *, MARK *, MARK *, CHAR_T *, size_t, CHAR_T **, u_int));
1827 CHAR_T *endp;
1829 *isaddrp = *errp = 0;
1830 F_CLR(ecp, E_DELTA);
1832 /* No addresses permitted until a file has been read in. */
1833 if (sp->ep == NULL && STRCHR(L("$0123456789'\\/?.+-^"), *ecp->cp)) {
1834 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1835 *errp = 1;
1836 return (0);
1839 switch (*ecp->cp) {
1840 case '$': /* Last line in the file. */
1841 *isaddrp = 1;
1842 F_SET(ecp, E_ABSMARK);
1844 mp->cno = 0;
1845 if (db_last(sp, &mp->lno))
1846 return (1);
1847 ++ecp->cp;
1848 --ecp->clen;
1849 break; /* Absolute line number. */
1850 case '0': case '1': case '2': case '3': case '4':
1851 case '5': case '6': case '7': case '8': case '9':
1852 *isaddrp = 1;
1853 F_SET(ecp, E_ABSMARK);
1855 if ((nret = nget_uslong(sp, &uval, ecp->cp, &endp, 10)) != NUM_OK) {
1856 ex_badaddr(sp, NULL, A_NOTSET, nret);
1857 *errp = 1;
1858 return (0);
1860 if (!NPFITS(DB_MAX_RECORDS, 0, uval)) {
1861 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1862 *errp = 1;
1863 return (0);
1865 mp->lno = uval;
1866 mp->cno = 0;
1867 ecp->clen -= (endp - ecp->cp);
1868 ecp->cp = endp;
1869 break;
1870 case '\'': /* Use a mark. */
1871 *isaddrp = 1;
1872 F_SET(ecp, E_ABSMARK);
1874 if (ecp->clen == 1) {
1875 msgq(sp, M_ERR, "095|No mark name supplied");
1876 *errp = 1;
1877 return (0);
1879 if (mark_get(sp, ecp->cp[1], mp, M_ERR)) {
1880 *errp = 1;
1881 return (0);
1883 ecp->cp += 2;
1884 ecp->clen -= 2;
1885 break;
1886 case '\\': /* Search: forward/backward. */
1888 * !!!
1889 * I can't find any difference between // and \/ or between
1890 * ?? and \?. Mark Horton doesn't remember there being any
1891 * difference. C'est la vie.
1893 if (ecp->clen < 2 ||
1894 (ecp->cp[1] != '/' && ecp->cp[1] != '?')) {
1895 msgq(sp, M_ERR, "096|\\ not followed by / or ?");
1896 *errp = 1;
1897 return (0);
1899 ++ecp->cp;
1900 --ecp->clen;
1901 sf = ecp->cp[0] == '/' ? f_search : b_search;
1902 goto search;
1903 case '/': /* Search forward. */
1904 sf = f_search;
1905 goto search;
1906 case '?': /* Search backward. */
1907 sf = b_search;
1909 search: mp->lno = sp->lno;
1910 mp->cno = sp->cno;
1911 if (sf(sp, mp, mp, ecp->cp, ecp->clen, &endp,
1912 SEARCH_MSG | SEARCH_PARSE | SEARCH_SET |
1913 (F_ISSET(ecp, E_SEARCH_WMSG) ? SEARCH_WMSG : 0))) {
1914 *errp = 1;
1915 return (0);
1918 /* Fix up the command pointers. */
1919 ecp->clen -= (endp - ecp->cp);
1920 ecp->cp = endp;
1922 *isaddrp = 1;
1923 F_SET(ecp, E_ABSMARK);
1924 break;
1925 case '.': /* Current position. */
1926 *isaddrp = 1;
1927 mp->cno = sp->cno;
1929 /* If an empty file, then '.' is 0, not 1. */
1930 if (sp->lno == 1) {
1931 if (db_last(sp, &mp->lno))
1932 return (1);
1933 if (mp->lno != 0)
1934 mp->lno = 1;
1935 } else
1936 mp->lno = sp->lno;
1939 * !!!
1940 * Historically, .<number> was the same as .+<number>, i.e.
1941 * the '+' could be omitted. (This feature is found in ed
1942 * as well.)
1944 if (ecp->clen > 1 && ISDIGIT((UCHAR_T)ecp->cp[1]))
1945 *ecp->cp = '+';
1946 else {
1947 ++ecp->cp;
1948 --ecp->clen;
1950 break;
1953 /* Skip trailing <blank>s. */
1954 for (; ecp->clen > 0 &&
1955 ISBLANK((UCHAR_T)ecp->cp[0]); ++ecp->cp, --ecp->clen);
1958 * Evaluate any offset. If no address yet found, the offset
1959 * is relative to ".".
1961 total = 0;
1962 if (ecp->clen != 0 && (ISDIGIT((UCHAR_T)ecp->cp[0]) ||
1963 ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1964 ecp->cp[0] == '^')) {
1965 if (!*isaddrp) {
1966 *isaddrp = 1;
1967 mp->lno = sp->lno;
1968 mp->cno = sp->cno;
1971 * Evaluate an offset, defined as:
1973 * [+-^<blank>]*[<blank>]*[0-9]*
1975 * The rough translation is any number of signs, optionally
1976 * followed by numbers, or a number by itself, all <blank>
1977 * separated.
1979 * !!!
1980 * All address offsets were additive, e.g. "2 2 3p" was the
1981 * same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2".
1982 * Note, however, "2 /ZZZ/" was an error. It was also legal
1983 * to insert signs without numbers, so "3 - 2" was legal, and
1984 * equal to 4.
1986 * !!!
1987 * Offsets were historically permitted for any line address,
1988 * e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after
1989 * line 8.
1991 * !!!
1992 * Offsets were historically permitted for search commands,
1993 * and handled as addresses: "/pattern/2 2 2" was legal, and
1994 * referenced the 6th line after pattern.
1996 F_SET(ecp, E_DELTA);
1997 for (;;) {
1998 for (; ecp->clen > 0 && ISBLANK((UCHAR_T)ecp->cp[0]);
1999 ++ecp->cp, --ecp->clen);
2000 if (ecp->clen == 0 || (!ISDIGIT((UCHAR_T)ecp->cp[0]) &&
2001 ecp->cp[0] != '+' && ecp->cp[0] != '-' &&
2002 ecp->cp[0] != '^'))
2003 break;
2004 if (!ISDIGIT((UCHAR_T)ecp->cp[0]) &&
2005 !ISDIGIT((UCHAR_T)ecp->cp[1])) {
2006 total += ecp->cp[0] == '+' ? 1 : -1;
2007 --ecp->clen;
2008 ++ecp->cp;
2009 } else {
2010 if (ecp->cp[0] == '-' ||
2011 ecp->cp[0] == '^') {
2012 ++ecp->cp;
2013 --ecp->clen;
2014 isneg = 1;
2015 } else
2016 isneg = 0;
2018 /* Get a signed long, add it to the total. */
2019 if ((nret = nget_slong(sp, &val,
2020 ecp->cp, &endp, 10)) != NUM_OK ||
2021 (nret = NADD_SLONG(sp,
2022 total, val)) != NUM_OK) {
2023 ex_badaddr(sp, NULL, A_NOTSET, nret);
2024 *errp = 1;
2025 return (0);
2027 total += isneg ? -val : val;
2028 ecp->clen -= (endp - ecp->cp);
2029 ecp->cp = endp;
2035 * Any value less than 0 is an error. Make sure that the new value
2036 * will fit into a db_recno_t.
2038 if (*isaddrp && total != 0) {
2039 if (total < 0) {
2040 if ((db_recno_t)-total > mp->lno) {
2041 msgq(sp, M_ERR,
2042 "097|Reference to a line number less than 0");
2043 *errp = 1;
2044 return (0);
2046 } else
2047 if (!NPFITS(DB_MAX_RECORDS, mp->lno, (unsigned long)total)) {
2048 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
2049 *errp = 1;
2050 return (0);
2052 mp->lno += total;
2054 return (0);
2059 * ex_load --
2060 * Load up the next command, which may be an @ buffer or global command.
2062 static int
2063 ex_load(SCR *sp)
2065 WIN *wp;
2066 EXCMD *ecp;
2067 RANGE *rp;
2069 F_CLR(sp, SC_EX_GLOBAL);
2072 * Lose any exhausted commands. We know that the first command
2073 * can't be an AGV command, which makes things a bit easier.
2075 for (wp = sp->wp;;) {
2077 * If we're back to the original structure, leave it around,
2078 * but discard any allocated source name, we've returned to
2079 * the beginning of the command stack.
2081 if ((ecp = LIST_FIRST(&wp->ecq)) == &wp->excmd) {
2082 if (F_ISSET(ecp, E_NAMEDISCARD)) {
2083 free(ecp->if_name);
2084 ecp->if_name = NULL;
2086 return (0);
2090 * ecp->clen will be 0 for the first discarded command, but
2091 * may not be 0 for subsequent ones, e.g. if the original
2092 * command was ":g/xx/@a|s/b/c/", then when we discard the
2093 * command pushed on the stack by the @a, we have to resume
2094 * the global command which included the substitute command.
2096 if (ecp->clen != 0)
2097 return (0);
2100 * If it's an @, global or v command, we may need to continue
2101 * the command on a different line.
2103 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2104 /* Discard any exhausted ranges. */
2105 while ((rp = TAILQ_FIRST(&ecp->rq)) != NULL)
2106 if (rp->start > rp->stop) {
2107 TAILQ_REMOVE(&ecp->rq, rp, q);
2108 free(rp);
2109 } else
2110 break;
2112 /* If there's another range, continue with it. */
2113 if (rp != NULL)
2114 break;
2116 /* If it's a global/v command, fix up the last line. */
2117 if (FL_ISSET(ecp->agv_flags,
2118 AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO) {
2119 if (db_exist(sp, ecp->range_lno))
2120 sp->lno = ecp->range_lno;
2121 else {
2122 if (db_last(sp, &sp->lno))
2123 return (1);
2124 if (sp->lno == 0)
2125 sp->lno = 1;
2128 free(ecp->o_cp);
2131 /* Discard the EXCMD. */
2132 LIST_REMOVE(ecp, q);
2133 free(ecp);
2137 * We only get here if it's an active @, global or v command. Set
2138 * the current line number, and get a new copy of the command for
2139 * the parser. Note, the original pointer almost certainly moved,
2140 * so we have play games.
2142 ecp->cp = ecp->o_cp;
2143 MEMCPYW(ecp->cp, ecp->cp + ecp->o_clen, ecp->o_clen);
2144 ecp->clen = ecp->o_clen;
2145 ecp->range_lno = sp->lno = rp->start++;
2147 if (FL_ISSET(ecp->agv_flags, AGV_GLOBAL | AGV_V))
2148 F_SET(sp, SC_EX_GLOBAL);
2149 return (0);
2153 * ex_discard --
2154 * Discard any pending ex commands.
2156 static int
2157 ex_discard(SCR *sp)
2159 WIN *wp;
2160 EXCMD *ecp;
2161 RANGE *rp;
2164 * We know the first command can't be an AGV command, so we don't
2165 * process it specially. We do, however, nail the command itself.
2167 for (wp = sp->wp; (ecp = LIST_FIRST(&wp->ecq)) != &wp->excmd;) {
2168 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2169 while ((rp = TAILQ_FIRST(&ecp->rq)) != NULL) {
2170 TAILQ_REMOVE(&ecp->rq, rp, q);
2171 free(rp);
2173 free(ecp->o_cp);
2175 LIST_REMOVE(ecp, q);
2176 free(ecp);
2178 LIST_FIRST(&wp->ecq)->clen = 0;
2179 return (0);
2183 * ex_unknown --
2184 * Display an unknown command name.
2186 static void
2187 ex_unknown(SCR *sp, CHAR_T *cmd, size_t len)
2189 size_t blen;
2190 CHAR_T *bp;
2192 GET_SPACE_GOTOW(sp, bp, blen, len + 1);
2193 bp[len] = '\0';
2194 MEMCPYW(bp, cmd, len);
2195 msgq_wstr(sp, M_ERR, bp, "098|The %s command is unknown");
2196 FREE_SPACEW(sp, bp, blen);
2198 alloc_err:
2199 return;
2203 * ex_is_abbrev -
2204 * The vi text input routine needs to know if ex thinks this is an
2205 * [un]abbreviate command, so it can turn off abbreviations. See
2206 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2208 * PUBLIC: int ex_is_abbrev __P((SCR *, CHAR_T *, size_t));
2211 ex_is_abbrev(SCR *sp, CHAR_T *name, size_t len)
2213 EXCMDLIST const *cp;
2215 return ((cp = ex_comm_search(sp, name, len)) != NULL &&
2216 (cp == &cmds[C_ABBR] || cp == &cmds[C_UNABBREVIATE]));
2220 * ex_is_unmap -
2221 * The vi text input routine needs to know if ex thinks this is an
2222 * unmap command, so it can turn off input mapping. See the usual
2223 * ranting in the vi/v_txt_ev.c:txt_unmap() routine.
2225 * PUBLIC: int ex_is_unmap __P((SCR *, CHAR_T *, size_t));
2228 ex_is_unmap(SCR *sp, CHAR_T *name, size_t len)
2230 EXCMDLIST const *cp;
2233 * The command the vi input routines are really interested in
2234 * is "unmap!", not just unmap.
2236 if (name[len - 1] != '!')
2237 return (0);
2238 --len;
2239 return ((cp = ex_comm_search(sp, name, len)) != NULL &&
2240 cp == &cmds[C_UNMAP]);
2244 * ex_comm_search --
2245 * Search for a command name.
2247 static EXCMDLIST const *
2248 ex_comm_search(SCR *sp, CHAR_T *name, size_t len)
2250 EXCMDLIST const *cp;
2252 for (cp = cmds; cp->name != NULL; ++cp) {
2253 if (cp->name[0] > name[0])
2254 return (NULL);
2255 if (cp->name[0] != name[0])
2256 continue;
2257 if (!MEMCMP(name, cp->name, len))
2258 return (cp);
2260 return (NULL);
2264 * ex_badaddr --
2265 * Display a bad address message.
2267 * PUBLIC: void ex_badaddr
2268 * PUBLIC: __P((SCR *, EXCMDLIST const *, enum badaddr, enum nresult));
2270 void
2271 ex_badaddr(SCR *sp, const EXCMDLIST *cp, enum badaddr ba, enum nresult nret)
2273 db_recno_t lno;
2275 switch (nret) {
2276 case NUM_OK:
2277 break;
2278 case NUM_ERR:
2279 msgq(sp, M_SYSERR, NULL);
2280 return;
2281 case NUM_OVER:
2282 msgq(sp, M_ERR, "099|Address value overflow");
2283 return;
2284 case NUM_UNDER:
2285 msgq(sp, M_ERR, "100|Address value underflow");
2286 return;
2290 * When encountering an address error, tell the user if there's no
2291 * underlying file, that's the real problem.
2293 if (sp->ep == NULL) {
2294 ex_wemsg(sp, cp ? cp->name : NULL, EXM_NOFILEYET);
2295 return;
2298 switch (ba) {
2299 case A_COMBO:
2300 msgq(sp, M_ERR, "101|Illegal address combination");
2301 break;
2302 case A_EOF:
2303 if (db_last(sp, &lno))
2304 return;
2305 if (lno != 0) {
2306 msgq(sp, M_ERR,
2307 "102|Illegal address: only %lu lines in the file",
2308 (unsigned long)lno);
2309 break;
2311 /* FALLTHROUGH */
2312 case A_EMPTY:
2313 msgq(sp, M_ERR, "103|Illegal address: the file is empty");
2314 break;
2315 case A_NOTSET:
2316 abort();
2317 /* NOTREACHED */
2318 case A_ZERO:
2319 msgq_wstr(sp, M_ERR, cp->name,
2320 "104|The %s command doesn't permit an address of 0");
2321 break;
2323 return;
2326 #if defined(DEBUG) && defined(COMLOG)
2328 * ex_comlog --
2329 * Log ex commands.
2331 static void
2332 ex_comlog(sp, ecp)
2333 SCR *sp;
2334 EXCMD *ecp;
2336 vtrace(sp, "ecmd: %s", ecp->cmd->name);
2337 if (ecp->addrcnt > 0) {
2338 vtrace(sp, " a1 %d", ecp->addr1.lno);
2339 if (ecp->addrcnt > 1)
2340 vtrace(sp, " a2: %d", ecp->addr2.lno);
2342 if (ecp->lineno)
2343 vtrace(sp, " line %d", ecp->lineno);
2344 if (ecp->flags)
2345 vtrace(sp, " flags 0x%x", ecp->flags);
2346 if (F_ISSET(&exc, E_BUFFER))
2347 vtrace(sp, " buffer "WC, ecp->buffer);
2348 if (ecp->argc)
2349 for (cnt = 0; cnt < ecp->argc; ++cnt)
2350 vtrace(sp, " arg %d: {%s}", cnt, ecp->argv[cnt]->bp);
2351 vtrace(sp, "\n");
2353 #endif