First test with an external encoding.
[nvi.git] / ex / ex.c
blob9ba01eb12f0540434c2b260f8eb85a7c34d0ec0c
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.68 2000/07/16 20:49:30 skimo Exp $ (Berkeley) $Date: 2000/07/16 20:49:30 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "../common/common.h"
32 #include "../vi/vi.h"
34 #if defined(DEBUG) && defined(COMLOG)
35 static void ex_comlog __P((SCR *, EXCMD *));
36 #endif
37 static EXCMDLIST const *
38 ex_comm_search __P((SCR *, CHAR_T *, size_t));
39 static int ex_discard __P((SCR *));
40 static int ex_line __P((SCR *, EXCMD *, MARK *, int *, int *));
41 static int ex_load __P((SCR *));
42 static void ex_unknown __P((SCR *, CHAR_T *, size_t));
45 * ex --
46 * Main ex loop.
48 * PUBLIC: int ex __P((SCR **));
50 int
51 ex(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 static CHAR_T space;
128 wp->excmd.cp = &space; /* __TK__ why not |? */
129 wp->excmd.clen = 1;
130 } else {
131 wp->excmd.cp = tp->lb;
132 wp->excmd.clen = tp->len;
134 F_INIT(&wp->excmd, E_NRSEP);
136 if (ex_cmd(sp) && F_ISSET(gp, G_SCRIPTED))
137 return (1);
139 if (INTERRUPTED(sp)) {
140 CLR_INTERRUPT(sp);
141 msgq(sp, M_ERR, "170|Interrupted");
145 * If the last command caused a restart, or switched screens
146 * or into vi, return.
148 if (F_ISSET(gp, G_SRESTART) || F_ISSET(sp, SC_SSWITCH | SC_VI)) {
149 *spp = sp;
150 break;
153 /* If the last command switched files, we don't care. */
154 F_CLR(sp, SC_FSWITCH);
157 * If we're exiting this screen, move to the next one. By
158 * definition, this means returning into vi, so return to the
159 * main editor loop. The ordering is careful, don't discard
160 * the contents of sp until the end.
162 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE)) {
163 if (file_end(sp, NULL, F_ISSET(sp, SC_EXIT_FORCE)))
164 return (1);
165 *spp = screen_next(sp);
166 return (screen_end(sp));
169 return (0);
173 * ex_cmd --
174 * The guts of the ex parser: parse and execute a string containing
175 * ex commands.
177 * !!!
178 * This code MODIFIES the string that gets passed in, to delete quoting
179 * characters, etc. The string cannot be readonly/text space, nor should
180 * you expect to use it again after ex_cmd() returns.
182 * !!!
183 * For the fun of it, if you want to see if a vi clone got the ex argument
184 * parsing right, try:
186 * echo 'foo|bar' > file1; echo 'foo/bar' > file2;
187 * vi
188 * :edit +1|s/|/PIPE/|w file1| e file2|1 | s/\//SLASH/|wq
190 * or: vi
191 * :set|file|append|set|file
193 * For extra credit, try them in a startup .exrc file.
195 * PUBLIC: int ex_cmd __P((SCR *));
198 ex_cmd(sp)
199 SCR *sp;
201 enum nresult nret;
202 EX_PRIVATE *exp;
203 EXCMD *ecp;
204 GS *gp;
205 WIN *wp;
206 MARK cur;
207 db_recno_t lno;
208 size_t arg1_len, discard, len;
209 u_int32_t flags;
210 long ltmp;
211 int at_found, gv_found;
212 int ch, cnt, delim, isaddr, namelen;
213 int newscreen, notempty, tmp, vi_address;
214 CHAR_T *arg1, *s, *p, *t;
215 char *n;
217 gp = sp->gp;
218 wp = sp->wp;
219 exp = EXP(sp);
222 * We always start running the command on the top of the stack.
223 * This means that *everything* must be resolved when we leave
224 * this function for any reason.
226 loop: ecp = wp->ecq.lh_first;
228 /* If we're reading a command from a file, set up error information. */
229 if (ecp->if_name != NULL) {
230 wp->if_lno = ecp->if_lno;
231 wp->if_name = ecp->if_name;
235 * If a move to the end of the file is scheduled for this command,
236 * do it now.
238 if (F_ISSET(ecp, E_MOVETOEND)) {
239 if (db_last(sp, &sp->lno))
240 goto rfail;
241 sp->cno = 0;
242 F_CLR(ecp, E_MOVETOEND);
245 /* If we found a newline, increment the count now. */
246 if (F_ISSET(ecp, E_NEWLINE)) {
247 ++wp->if_lno;
248 ++ecp->if_lno;
249 F_CLR(ecp, E_NEWLINE);
252 /* (Re)initialize the EXCMD structure, preserving some flags. */
253 CLEAR_EX_CMD(ecp);
255 /* Initialize the argument structures. */
256 if (argv_init(sp, ecp))
257 goto err;
259 /* Initialize +cmd, saved command information. */
260 arg1 = NULL;
261 ecp->save_cmdlen = 0;
263 /* Skip <blank>s, empty lines. */
264 for (notempty = 0; ecp->clen > 0; ++ecp->cp, --ecp->clen)
265 if ((ch = *ecp->cp) == '\n') {
266 ++wp->if_lno;
267 ++ecp->if_lno;
268 } else if (isblank(ch))
269 notempty = 1;
270 else
271 break;
274 * !!!
275 * Permit extra colons at the start of the line. Historically,
276 * ex/vi allowed a single extra one. It's simpler not to count.
277 * The stripping is done here because, historically, any command
278 * could have preceding colons, e.g. ":g/pattern/:p" worked.
280 if (ecp->clen != 0 && ch == ':') {
281 notempty = 1;
282 while (--ecp->clen > 0 && (ch = *++ecp->cp) == ':');
286 * Command lines that start with a double-quote are comments.
288 * !!!
289 * Historically, there was no escape or delimiter for a comment, e.g.
290 * :"foo|set was a single comment and nothing was output. Since nvi
291 * permits users to escape <newline> characters into command lines, we
292 * have to check for that case.
294 if (ecp->clen != 0 && ch == '"') {
295 while (--ecp->clen > 0 && *++ecp->cp != '\n');
296 if (*ecp->cp == '\n') {
297 F_SET(ecp, E_NEWLINE);
298 ++ecp->cp;
299 --ecp->clen;
301 goto loop;
304 /* Skip whitespace. */
305 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
306 ch = *ecp->cp;
307 if (!isblank(ch))
308 break;
312 * The last point at which an empty line can mean do nothing.
314 * !!!
315 * Historically, in ex mode, lines containing only <blank> characters
316 * were the same as a single <carriage-return>, i.e. a default command.
317 * In vi mode, they were ignored. In .exrc files this was a serious
318 * annoyance, as vi kept trying to treat them as print commands. We
319 * ignore backward compatibility in this case, discarding lines that
320 * contain only <blank> characters from .exrc files.
322 * !!!
323 * This is where you end up when you're done a command, i.e. clen has
324 * gone to zero. Continue if there are more commands to run.
326 if (ecp->clen == 0 &&
327 (!notempty || F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_BLIGNORE))) {
328 if (ex_load(sp))
329 goto rfail;
330 ecp = wp->ecq.lh_first;
331 if (ecp->clen == 0)
332 goto rsuccess;
333 goto loop;
337 * Check to see if this is a command for which we may want to move
338 * the cursor back up to the previous line. (The command :1<CR>
339 * wants a <newline> separator, but the command :<CR> wants to erase
340 * the command line.) If the line is empty except for <blank>s,
341 * <carriage-return> or <eof>, we'll probably want to move up. I
342 * don't think there's any way to get <blank> characters *after* the
343 * command character, but this is the ex parser, and I've been wrong
344 * before.
346 if (F_ISSET(ecp, E_NRSEP) &&
347 ecp->clen != 0 && (ecp->clen != 1 || ecp->cp[0] != '\004'))
348 F_CLR(ecp, E_NRSEP);
350 /* Parse command addresses. */
351 if (ex_range(sp, ecp, &tmp))
352 goto rfail;
353 if (tmp)
354 goto err;
357 * Skip <blank>s and any more colons (the command :3,5:print
358 * worked, historically).
360 for (; ecp->clen > 0; ++ecp->cp, --ecp->clen) {
361 ch = *ecp->cp;
362 if (!isblank(ch) && ch != ':')
363 break;
367 * If no command, ex does the last specified of p, l, or #, and vi
368 * moves to the line. Otherwise, determine the length of the command
369 * name by looking for the first non-alphabetic character. (There
370 * are a few non-alphabetic characters in command names, but they're
371 * all single character commands.) This isn't a great test, because
372 * it means that, for the command ":e +cut.c file", we'll report that
373 * the command "cut" wasn't known. However, it makes ":e+35 file" work
374 * correctly.
376 * !!!
377 * Historically, lines with multiple adjacent (or <blank> separated)
378 * command separators were very strange. For example, the command
379 * |||<carriage-return>, when the cursor was on line 1, displayed
380 * lines 2, 3 and 5 of the file. In addition, the command " | "
381 * would only display the line after the next line, instead of the
382 * next two lines. No ideas why. It worked reasonably when executed
383 * from vi mode, and displayed lines 2, 3, and 4, so we do a default
384 * command for each separator.
386 #define SINGLE_CHAR_COMMANDS "\004!#&*<=>@~"
387 newscreen = 0;
388 if (ecp->clen != 0 && ecp->cp[0] != '|' && ecp->cp[0] != '\n') {
389 if (strchr(SINGLE_CHAR_COMMANDS, *ecp->cp)) {
390 p = ecp->cp;
391 ++ecp->cp;
392 --ecp->clen;
393 namelen = 1;
394 } else {
395 for (p = ecp->cp;
396 ecp->clen > 0; --ecp->clen, ++ecp->cp)
397 if (!isalpha(*ecp->cp))
398 break;
399 if ((namelen = ecp->cp - p) == 0) {
400 msgq(sp, M_ERR, "080|Unknown command name");
401 goto err;
406 * !!!
407 * Historic vi permitted flags to immediately follow any
408 * subset of the 'delete' command, but then did not permit
409 * further arguments (flag, buffer, count). Make it work.
410 * Permit further arguments for the few shreds of dignity
411 * it offers.
413 * Adding commands that start with 'd', and match "delete"
414 * up to a l, p, +, - or # character can break this code.
416 * !!!
417 * Capital letters beginning the command names ex, edit,
418 * next, previous, tag and visual (in vi mode) indicate the
419 * command should happen in a new screen.
421 switch (p[0]) {
422 case 'd':
423 for (s = p,
424 n = cmds[C_DELETE].name; *s == *n; ++s, ++n);
425 if (s[0] == 'l' || s[0] == 'p' || s[0] == '+' ||
426 s[0] == '-' || s[0] == '^' || s[0] == '#') {
427 len = (ecp->cp - p) - (s - p);
428 ecp->cp -= len;
429 ecp->clen += len;
430 ecp->rcmd = cmds[C_DELETE];
431 ecp->rcmd.syntax = "1bca1";
432 ecp->cmd = &ecp->rcmd;
433 goto skip_srch;
435 break;
436 case 'E': case 'F': case 'N': case 'P': case 'T': case 'V':
437 newscreen = 1;
438 p[0] = tolower(p[0]);
439 break;
443 * Search the table for the command.
445 * !!!
446 * Historic vi permitted the mark to immediately follow the
447 * 'k' in the 'k' command. Make it work.
449 * !!!
450 * Historic vi permitted any flag to follow the s command, e.g.
451 * "s/e/E/|s|sgc3p" was legal. Make the command "sgc" work.
452 * Since the following characters all have to be flags, i.e.
453 * alphabetics, we can let the s command routine return errors
454 * if it was some illegal command string. This code will break
455 * if an "sg" or similar command is ever added. The substitute
456 * code doesn't care if it's a "cgr" flag or a "#lp" flag that
457 * follows the 's', but we limit the choices here to "cgr" so
458 * that we get unknown command messages for wrong combinations.
460 if ((ecp->cmd = ex_comm_search(sp, p, namelen)) == NULL)
461 switch (p[0]) {
462 case 'k':
463 if (namelen == 2) {
464 ecp->cp -= namelen - 1;
465 ecp->clen += namelen - 1;
466 ecp->cmd = &cmds[C_K];
467 break;
469 goto unknown;
470 case 's':
471 for (s = p + 1, cnt = namelen; --cnt; ++s)
472 if (s[0] != 'c' &&
473 s[0] != 'g' && s[0] != 'r')
474 break;
475 if (cnt == 0) {
476 ecp->cp -= namelen - 1;
477 ecp->clen += namelen - 1;
478 ecp->rcmd = cmds[C_SUBSTITUTE];
479 ecp->rcmd.fn = ex_subagain;
480 ecp->cmd = &ecp->rcmd;
481 break;
483 /* FALLTHROUGH */
484 default:
485 unknown: if (newscreen)
486 p[0] = toupper(p[0]);
487 ex_unknown(sp, p, namelen);
488 goto err;
492 * The visual command has a different syntax when called
493 * from ex than when called from a vi colon command. FMH.
494 * Make the change now, before we test for the newscreen
495 * semantic, so that we're testing the right one.
497 skip_srch: if (ecp->cmd == &cmds[C_VISUAL_EX] && F_ISSET(sp, SC_VI))
498 ecp->cmd = &cmds[C_VISUAL_VI];
501 * !!!
502 * Historic vi permitted a capital 'P' at the beginning of
503 * any command that started with 'p'. Probably wanted the
504 * P[rint] command for backward compatibility, and the code
505 * just made Preserve and Put work by accident. Nvi uses
506 * Previous to mean previous-in-a-new-screen, so be careful.
508 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN) &&
509 (ecp->cmd == &cmds[C_PRINT] ||
510 ecp->cmd == &cmds[C_PRESERVE]))
511 newscreen = 0;
513 /* Test for a newscreen associated with this command. */
514 if (newscreen && !F_ISSET(ecp->cmd, E_NEWSCREEN))
515 goto unknown;
517 /* Secure means no shell access. */
518 if (F_ISSET(ecp->cmd, E_SECURE) && O_ISSET(sp, O_SECURE)) {
519 ex_emsg(sp, ecp->cmd->name, EXM_SECURE);
520 goto err;
524 * Multiple < and > characters; another "feature". Note,
525 * The string passed to the underlying function may not be
526 * nul terminated in this case.
528 if ((ecp->cmd == &cmds[C_SHIFTL] && *p == '<') ||
529 (ecp->cmd == &cmds[C_SHIFTR] && *p == '>')) {
530 for (ch = *p;
531 ecp->clen > 0; --ecp->clen, ++ecp->cp)
532 if (*ecp->cp != ch)
533 break;
534 if (argv_exp0(sp, ecp, p, ecp->cp - p))
535 goto err;
538 /* Set the format style flags for the next command. */
539 if (ecp->cmd == &cmds[C_HASH])
540 exp->fdef = E_C_HASH;
541 else if (ecp->cmd == &cmds[C_LIST])
542 exp->fdef = E_C_LIST;
543 else if (ecp->cmd == &cmds[C_PRINT])
544 exp->fdef = E_C_PRINT;
545 F_CLR(ecp, E_USELASTCMD);
546 } else {
547 /* Print is the default command. */
548 ecp->cmd = &cmds[C_PRINT];
550 /* Set the saved format flags. */
551 F_SET(ecp, exp->fdef);
554 * !!!
555 * If no address was specified, and it's not a global command,
556 * we up the address by one. (I have no idea why globals are
557 * exempted, but it's (ahem) historic practice.)
559 if (ecp->addrcnt == 0 && !F_ISSET(sp, SC_EX_GLOBAL)) {
560 ecp->addrcnt = 1;
561 ecp->addr1.lno = sp->lno + 1;
562 ecp->addr1.cno = sp->cno;
565 F_SET(ecp, E_USELASTCMD);
569 * !!!
570 * Historically, the number option applied to both ex and vi. One
571 * strangeness was that ex didn't switch display formats until a
572 * command was entered, e.g. <CR>'s after the set didn't change to
573 * the new format, but :1p would.
575 if (O_ISSET(sp, O_NUMBER)) {
576 F_SET(ecp, E_OPTNUM);
577 FL_SET(ecp->iflags, E_C_HASH);
578 } else
579 F_CLR(ecp, E_OPTNUM);
581 /* Check for ex mode legality. */
582 if (F_ISSET(sp, SC_EX) && (F_ISSET(ecp->cmd, E_VIONLY) || newscreen)) {
583 msgq(sp, M_ERR,
584 "082|%s: command not available in ex mode", ecp->cmd->name);
585 goto err;
588 /* Add standard command flags. */
589 F_SET(ecp, ecp->cmd->flags);
590 if (!newscreen)
591 F_CLR(ecp, E_NEWSCREEN);
594 * There are three normal termination cases for an ex command. They
595 * are the end of the string (ecp->clen), or unescaped (by <literal
596 * next> characters) <newline> or '|' characters. As we're now past
597 * possible addresses, we can determine how long the command is, so we
598 * don't have to look for all the possible terminations. Naturally,
599 * there are some exciting special cases:
601 * 1: The bang, global, v and the filter versions of the read and
602 * write commands are delimited by <newline>s (they can contain
603 * shell pipes).
604 * 2: The ex, edit, next and visual in vi mode commands all take ex
605 * commands as their first arguments.
606 * 3: The s command takes an RE as its first argument, and wants it
607 * to be specially delimited.
609 * Historically, '|' characters in the first argument of the ex, edit,
610 * next, vi visual, and s commands didn't delimit the command. And,
611 * in the filter cases for read and write, and the bang, global and v
612 * commands, they did not delimit the command at all.
614 * For example, the following commands were legal:
616 * :edit +25|s/abc/ABC/ file.c
617 * :s/|/PIPE/
618 * :read !spell % | columnate
619 * :global/pattern/p|l
621 * It's not quite as simple as it sounds, however. The command:
623 * :s/a/b/|s/c/d|set
625 * was also legal, i.e. the historic ex parser (using the word loosely,
626 * since "parser" implies some regularity of syntax) delimited the RE's
627 * based on its delimiter and not anything so irretrievably vulgar as a
628 * command syntax.
630 * Anyhow, the following code makes this all work. First, for the
631 * special cases we move past their special argument(s). Then, we
632 * do normal command processing on whatever is left. Barf-O-Rama.
634 discard = 0; /* Characters discarded from the command. */
635 arg1_len = 0;
636 ecp->save_cmd = ecp->cp;
637 if (ecp->cmd == &cmds[C_EDIT] || ecp->cmd == &cmds[C_EX] ||
638 ecp->cmd == &cmds[C_NEXT] || ecp->cmd == &cmds[C_VISUAL_VI] ||
639 ecp->cmd == &cmds[C_VSPLIT]) {
641 * Move to the next non-whitespace character. A '!'
642 * immediately following the command is eaten as a
643 * force flag.
645 if (ecp->clen > 0 && *ecp->cp == '!') {
646 ++ecp->cp;
647 --ecp->clen;
648 FL_SET(ecp->iflags, E_C_FORCE);
650 /* Reset, don't reparse. */
651 ecp->save_cmd = ecp->cp;
653 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
654 if (!isblank(*ecp->cp))
655 break;
657 * QUOTING NOTE:
659 * The historic implementation ignored all escape characters
660 * so there was no way to put a space or newline into the +cmd
661 * field. We do a simplistic job of fixing it by moving to the
662 * first whitespace character that isn't escaped. The escaping
663 * characters are stripped as no longer useful.
665 if (ecp->clen > 0 && *ecp->cp == '+') {
666 ++ecp->cp;
667 --ecp->clen;
668 for (arg1 = p = ecp->cp;
669 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
670 ch = *ecp->cp;
671 if (IS_ESCAPE(sp, ecp, ch) &&
672 ecp->clen > 1) {
673 ++discard;
674 --ecp->clen;
675 ch = *++ecp->cp;
676 } else if (isblank(ch))
677 break;
678 *p++ = ch;
680 arg1_len = ecp->cp - arg1;
682 /* Reset, so the first argument isn't reparsed. */
683 ecp->save_cmd = ecp->cp;
685 } else if (ecp->cmd == &cmds[C_BANG] ||
686 ecp->cmd == &cmds[C_GLOBAL] || ecp->cmd == &cmds[C_V]) {
688 * QUOTING NOTE:
690 * We use backslashes to escape <newline> characters, although
691 * this wasn't historic practice for the bang command. It was
692 * for the global and v commands, and it's common usage when
693 * doing text insert during the command. Escaping characters
694 * are stripped as no longer useful.
696 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
697 ch = *ecp->cp;
698 if (ch == '\\' && ecp->clen > 1 && ecp->cp[1] == '\n') {
699 ++discard;
700 --ecp->clen;
701 ch = *++ecp->cp;
703 ++wp->if_lno;
704 ++ecp->if_lno;
705 } else if (ch == '\n')
706 break;
707 *p++ = ch;
709 } else if (ecp->cmd == &cmds[C_READ] || ecp->cmd == &cmds[C_WRITE]) {
711 * For write commands, if the next character is a <blank>, and
712 * the next non-blank character is a '!', it's a filter command
713 * and we want to eat everything up to the <newline>. For read
714 * commands, if the next non-blank character is a '!', it's a
715 * filter command and we want to eat everything up to the next
716 * <newline>. Otherwise, we're done.
718 for (tmp = 0; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
719 ch = *ecp->cp;
720 if (isblank(ch))
721 tmp = 1;
722 else
723 break;
725 if (ecp->clen > 0 && ch == '!' &&
726 (ecp->cmd == &cmds[C_READ] || tmp))
727 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
728 if (ecp->cp[0] == '\n')
729 break;
730 } else if (ecp->cmd == &cmds[C_SUBSTITUTE]) {
732 * Move to the next non-whitespace character, we'll use it as
733 * the delimiter. If the character isn't an alphanumeric or
734 * a '|', it's the delimiter, so parse it. Otherwise, we're
735 * into something like ":s g", so use the special s command.
737 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
738 if (!isblank(ecp->cp[0]))
739 break;
741 if (isalnum(ecp->cp[0]) || ecp->cp[0] == '|') {
742 ecp->rcmd = cmds[C_SUBSTITUTE];
743 ecp->rcmd.fn = ex_subagain;
744 ecp->cmd = &ecp->rcmd;
745 } else if (ecp->clen > 0) {
747 * QUOTING NOTE:
749 * Backslashes quote delimiter characters for RE's.
750 * The backslashes are NOT removed since they'll be
751 * used by the RE code. Move to the third delimiter
752 * that's not escaped (or the end of the command).
754 delim = *ecp->cp;
755 ++ecp->cp;
756 --ecp->clen;
757 for (cnt = 2; ecp->clen > 0 &&
758 cnt != 0; --ecp->clen, ++ecp->cp)
759 if (ecp->cp[0] == '\\' &&
760 ecp->clen > 1) {
761 ++ecp->cp;
762 --ecp->clen;
763 } else if (ecp->cp[0] == delim)
764 --cnt;
769 * Use normal quoting and termination rules to find the end of this
770 * command.
772 * QUOTING NOTE:
774 * Historically, vi permitted ^V's to escape <newline>'s in the .exrc
775 * file. It was almost certainly a bug, but that's what bug-for-bug
776 * compatibility means, Grasshopper. Also, ^V's escape the command
777 * delimiters. Literal next quote characters in front of the newlines,
778 * '|' characters or literal next characters are stripped as they're
779 * no longer useful.
781 vi_address = ecp->clen != 0 && ecp->cp[0] != '\n';
782 for (p = ecp->cp; ecp->clen > 0; --ecp->clen, ++ecp->cp) {
783 ch = ecp->cp[0];
784 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
785 tmp = ecp->cp[1];
786 if (tmp == '\n' || tmp == '|') {
787 if (tmp == '\n') {
788 ++wp->if_lno;
789 ++ecp->if_lno;
791 ++discard;
792 --ecp->clen;
793 ++ecp->cp;
794 ch = tmp;
796 } else if (ch == '\n' || ch == '|') {
797 if (ch == '\n')
798 F_SET(ecp, E_NEWLINE);
799 --ecp->clen;
800 break;
802 *p++ = ch;
806 * Save off the next command information, go back to the
807 * original start of the command.
809 p = ecp->cp + 1;
810 ecp->cp = ecp->save_cmd;
811 ecp->save_cmd = p;
812 ecp->save_cmdlen = ecp->clen;
813 ecp->clen = ((ecp->save_cmd - ecp->cp) - 1) - discard;
816 * QUOTING NOTE:
818 * The "set tags" command historically used a backslash, not the
819 * user's literal next character, to escape whitespace. Handle
820 * it here instead of complicating the argv_exp3() code. Note,
821 * this isn't a particularly complex trap, and if backslashes were
822 * legal in set commands, this would have to be much more complicated.
824 if (ecp->cmd == &cmds[C_SET])
825 for (p = ecp->cp, len = ecp->clen; len > 0; --len, ++p)
826 if (*p == '\\')
827 *p = CH_LITERAL;
830 * Set the default addresses. It's an error to specify an address for
831 * a command that doesn't take them. If two addresses are specified
832 * for a command that only takes one, lose the first one. Two special
833 * cases here, some commands take 0 or 2 addresses. For most of them
834 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one
835 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines.
837 * Also, if the file is empty, some commands want to use an address of
838 * 0, i.e. the entire file is 0 to 0, and the default first address is
839 * 0. Otherwise, an entire file is 1 to N and the default line is 1.
840 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the
841 * case where the 0 address is only valid if it's a default address.
843 * Also, set a flag if we set the default addresses. Some commands
844 * (ex: z) care if the user specified an address or if we just used
845 * the current cursor.
847 switch (F_ISSET(ecp, E_ADDR1 | E_ADDR2 | E_ADDR2_ALL | E_ADDR2_NONE)) {
848 case E_ADDR1: /* One address: */
849 switch (ecp->addrcnt) {
850 case 0: /* Default cursor/empty file. */
851 ecp->addrcnt = 1;
852 F_SET(ecp, E_ADDR_DEF);
853 if (F_ISSET(ecp, E_ADDR_ZERODEF)) {
854 if (db_last(sp, &lno))
855 goto err;
856 if (lno == 0) {
857 ecp->addr1.lno = 0;
858 F_SET(ecp, E_ADDR_ZERO);
859 } else
860 ecp->addr1.lno = sp->lno;
861 } else
862 ecp->addr1.lno = sp->lno;
863 ecp->addr1.cno = sp->cno;
864 break;
865 case 1:
866 break;
867 case 2: /* Lose the first address. */
868 ecp->addrcnt = 1;
869 ecp->addr1 = ecp->addr2;
871 break;
872 case E_ADDR2_NONE: /* Zero/two addresses: */
873 if (ecp->addrcnt == 0) /* Default to nothing. */
874 break;
875 goto two_addr;
876 case E_ADDR2_ALL: /* Zero/two addresses: */
877 if (ecp->addrcnt == 0) { /* Default entire/empty file. */
878 F_SET(ecp, E_ADDR_DEF);
879 ecp->addrcnt = 2;
880 if (sp->ep == NULL)
881 ecp->addr2.lno = 0;
882 else if (db_last(sp, &ecp->addr2.lno))
883 goto err;
884 if (F_ISSET(ecp, E_ADDR_ZERODEF) &&
885 ecp->addr2.lno == 0) {
886 ecp->addr1.lno = 0;
887 F_SET(ecp, E_ADDR_ZERO);
888 } else
889 ecp->addr1.lno = 1;
890 ecp->addr1.cno = ecp->addr2.cno = 0;
891 F_SET(ecp, E_ADDR2_ALL);
892 break;
894 /* FALLTHROUGH */
895 case E_ADDR2: /* Two addresses: */
896 two_addr: switch (ecp->addrcnt) {
897 case 0: /* Default cursor/empty file. */
898 ecp->addrcnt = 2;
899 F_SET(ecp, E_ADDR_DEF);
900 if (sp->lno == 1 &&
901 F_ISSET(ecp, E_ADDR_ZERODEF)) {
902 if (db_last(sp, &lno))
903 goto err;
904 if (lno == 0) {
905 ecp->addr1.lno = ecp->addr2.lno = 0;
906 F_SET(ecp, E_ADDR_ZERO);
907 } else
908 ecp->addr1.lno =
909 ecp->addr2.lno = sp->lno;
910 } else
911 ecp->addr1.lno = ecp->addr2.lno = sp->lno;
912 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
913 break;
914 case 1: /* Default to first address. */
915 ecp->addrcnt = 2;
916 ecp->addr2 = ecp->addr1;
917 break;
918 case 2:
919 break;
921 break;
922 default:
923 if (ecp->addrcnt) /* Error. */
924 goto usage;
928 * !!!
929 * The ^D scroll command historically scrolled the value of the scroll
930 * option or to EOF. It was an error if the cursor was already at EOF.
931 * (Leading addresses were permitted, but were then ignored.)
933 if (ecp->cmd == &cmds[C_SCROLL]) {
934 ecp->addrcnt = 2;
935 ecp->addr1.lno = sp->lno + 1;
936 ecp->addr2.lno = sp->lno + O_VAL(sp, O_SCROLL);
937 ecp->addr1.cno = ecp->addr2.cno = sp->cno;
938 if (db_last(sp, &lno))
939 goto err;
940 if (lno != 0 && lno > sp->lno && ecp->addr2.lno > lno)
941 ecp->addr2.lno = lno;
944 ecp->flagoff = 0;
945 for (n = ecp->cmd->syntax; *n != '\0'; ++n) {
947 * The force flag is sensitive to leading whitespace, i.e.
948 * "next !" is different from "next!". Handle it before
949 * skipping leading <blank>s.
951 if (*n == '!') {
952 if (ecp->clen > 0 && *ecp->cp == '!') {
953 ++ecp->cp;
954 --ecp->clen;
955 FL_SET(ecp->iflags, E_C_FORCE);
957 continue;
960 /* Skip leading <blank>s. */
961 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp)
962 if (!isblank(*ecp->cp))
963 break;
964 if (ecp->clen == 0)
965 break;
967 switch (*n) {
968 case '1': /* +, -, #, l, p */
970 * !!!
971 * Historically, some flags were ignored depending
972 * on where they occurred in the command line. For
973 * example, in the command, ":3+++p--#", historic vi
974 * acted on the '#' flag, but ignored the '-' flags.
975 * It's unambiguous what the flags mean, so we just
976 * handle them regardless of the stupidity of their
977 * location.
979 for (; ecp->clen; --ecp->clen, ++ecp->cp)
980 switch (*ecp->cp) {
981 case '+':
982 ++ecp->flagoff;
983 break;
984 case '-':
985 case '^':
986 --ecp->flagoff;
987 break;
988 case '#':
989 F_CLR(ecp, E_OPTNUM);
990 FL_SET(ecp->iflags, E_C_HASH);
991 exp->fdef |= E_C_HASH;
992 break;
993 case 'l':
994 FL_SET(ecp->iflags, E_C_LIST);
995 exp->fdef |= E_C_LIST;
996 break;
997 case 'p':
998 FL_SET(ecp->iflags, E_C_PRINT);
999 exp->fdef |= E_C_PRINT;
1000 break;
1001 default:
1002 goto end_case1;
1004 end_case1: break;
1005 case '2': /* -, ., +, ^ */
1006 case '3': /* -, ., +, ^, = */
1007 for (; ecp->clen; --ecp->clen, ++ecp->cp)
1008 switch (*ecp->cp) {
1009 case '-':
1010 FL_SET(ecp->iflags, E_C_DASH);
1011 break;
1012 case '.':
1013 FL_SET(ecp->iflags, E_C_DOT);
1014 break;
1015 case '+':
1016 FL_SET(ecp->iflags, E_C_PLUS);
1017 break;
1018 case '^':
1019 FL_SET(ecp->iflags, E_C_CARAT);
1020 break;
1021 case '=':
1022 if (*n == '3') {
1023 FL_SET(ecp->iflags, E_C_EQUAL);
1024 break;
1026 /* FALLTHROUGH */
1027 default:
1028 goto end_case23;
1030 end_case23: break;
1031 case 'b': /* buffer */
1033 * !!!
1034 * Historically, "d #" was a delete with a flag, not a
1035 * delete into the '#' buffer. If the current command
1036 * permits a flag, don't use one as a buffer. However,
1037 * the 'l' and 'p' flags were legal buffer names in the
1038 * historic ex, and were used as buffers, not flags.
1040 if ((ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1041 ecp->cp[0] == '^' || ecp->cp[0] == '#') &&
1042 strchr(n, '1') != NULL)
1043 break;
1045 * !!!
1046 * Digits can't be buffer names in ex commands, or the
1047 * command "d2" would be a delete into buffer '2', and
1048 * not a two-line deletion.
1050 if (!isdigit(ecp->cp[0])) {
1051 ecp->buffer = *ecp->cp;
1052 ++ecp->cp;
1053 --ecp->clen;
1054 FL_SET(ecp->iflags, E_C_BUFFER);
1056 break;
1057 case 'c': /* count [01+a] */
1058 ++n;
1059 /* Validate any signed value. */
1060 if (!isdigit(*ecp->cp) && (*n != '+' ||
1061 (*ecp->cp != '+' && *ecp->cp != '-')))
1062 break;
1063 /* If a signed value, set appropriate flags. */
1064 if (*ecp->cp == '-')
1065 FL_SET(ecp->iflags, E_C_COUNT_NEG);
1066 else if (*ecp->cp == '+')
1067 FL_SET(ecp->iflags, E_C_COUNT_POS);
1068 if ((nret =
1069 nget_slong(sp, &ltmp, ecp->cp, &t, 10)) != NUM_OK) {
1070 ex_badaddr(sp, NULL, A_NOTSET, nret);
1071 goto err;
1073 if (ltmp == 0 && *n != '0') {
1074 msgq(sp, M_ERR, "083|Count may not be zero");
1075 goto err;
1077 ecp->clen -= (t - ecp->cp);
1078 ecp->cp = t;
1081 * Counts as address offsets occur in commands taking
1082 * two addresses. Historic vi practice was to use
1083 * the count as an offset from the *second* address.
1085 * Set a count flag; some underlying commands (see
1086 * join) do different things with counts than with
1087 * line addresses.
1089 if (*n == 'a') {
1090 ecp->addr1 = ecp->addr2;
1091 ecp->addr2.lno = ecp->addr1.lno + ltmp - 1;
1092 } else
1093 ecp->count = ltmp;
1094 FL_SET(ecp->iflags, E_C_COUNT);
1095 break;
1096 case 'f': /* file */
1097 if (argv_exp2(sp, ecp, ecp->cp, ecp->clen))
1098 goto err;
1099 goto arg_cnt_chk;
1100 case 'l': /* line */
1102 * Get a line specification.
1104 * If the line was a search expression, we may have
1105 * changed state during the call, and we're now
1106 * searching the file. Push ourselves onto the state
1107 * stack.
1109 if (ex_line(sp, ecp, &cur, &isaddr, &tmp))
1110 goto rfail;
1111 if (tmp)
1112 goto err;
1114 /* Line specifications are always required. */
1115 if (!isaddr) {
1116 msgq_wstr(sp, M_ERR, ecp->cp,
1117 "084|%s: bad line specification");
1118 goto err;
1121 * The target line should exist for these commands,
1122 * but 0 is legal for them as well.
1124 if (cur.lno != 0 && !db_exist(sp, cur.lno)) {
1125 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1126 goto err;
1128 ecp->lineno = cur.lno;
1129 break;
1130 case 'S': /* string, file exp. */
1131 if (ecp->clen != 0) {
1132 if (argv_exp1(sp, ecp, ecp->cp,
1133 ecp->clen, ecp->cmd == &cmds[C_BANG]))
1134 goto err;
1135 goto addr_verify;
1137 /* FALLTHROUGH */
1138 case 's': /* string */
1139 if (argv_exp0(sp, ecp, ecp->cp, ecp->clen))
1140 goto err;
1141 goto addr_verify;
1142 case 'W': /* word string */
1144 * QUOTING NOTE:
1146 * Literal next characters escape the following
1147 * character. Quoting characters are stripped here
1148 * since they are no longer useful.
1150 * First there was the word.
1152 for (p = t = ecp->cp;
1153 ecp->clen > 0; --ecp->clen, ++ecp->cp) {
1154 ch = *ecp->cp;
1155 if (IS_ESCAPE(sp,
1156 ecp, ch) && ecp->clen > 1) {
1157 --ecp->clen;
1158 *p++ = *++ecp->cp;
1159 } else if (isblank(ch)) {
1160 ++ecp->cp;
1161 --ecp->clen;
1162 break;
1163 } else
1164 *p++ = ch;
1166 if (argv_exp0(sp, ecp, t, p - t))
1167 goto err;
1169 /* Delete intervening whitespace. */
1170 for (; ecp->clen > 0;
1171 --ecp->clen, ++ecp->cp) {
1172 ch = *ecp->cp;
1173 if (!isblank(ch))
1174 break;
1176 if (ecp->clen == 0)
1177 goto usage;
1179 /* Followed by the string. */
1180 for (p = t = ecp->cp; ecp->clen > 0;
1181 --ecp->clen, ++ecp->cp, ++p) {
1182 ch = *ecp->cp;
1183 if (IS_ESCAPE(sp,
1184 ecp, ch) && ecp->clen > 1) {
1185 --ecp->clen;
1186 *p = *++ecp->cp;
1187 } else
1188 *p = ch;
1190 if (argv_exp0(sp, ecp, t, p - t))
1191 goto err;
1192 goto addr_verify;
1193 case 'w': /* word */
1194 if (argv_exp3(sp, ecp, ecp->cp, ecp->clen))
1195 goto err;
1196 arg_cnt_chk: if (*++n != 'N') { /* N */
1198 * If a number is specified, must either be
1199 * 0 or that number, if optional, and that
1200 * number, if required.
1202 tmp = *n - '0';
1203 if ((*++n != 'o' || exp->argsoff != 0) &&
1204 exp->argsoff != tmp)
1205 goto usage;
1207 goto addr_verify;
1208 default:
1209 msgq(sp, M_ERR,
1210 "085|Internal syntax table error (%s: %s)",
1211 ecp->cmd->name, KEY_NAME(sp, *n));
1215 /* Skip trailing whitespace. */
1216 for (; ecp->clen > 0; --ecp->clen) {
1217 ch = *ecp->cp++;
1218 if (!isblank(ch))
1219 break;
1223 * There shouldn't be anything left, and no more required fields,
1224 * i.e neither 'l' or 'r' in the syntax string.
1226 if (ecp->clen != 0 || strpbrk(n, "lr")) {
1227 usage: msgq(sp, M_ERR, "086|Usage: %s", ecp->cmd->usage);
1228 goto err;
1232 * Verify that the addresses are legal. Check the addresses here,
1233 * because this is a place where all ex addresses pass through.
1234 * (They don't all pass through ex_line(), for instance.) We're
1235 * assuming that any non-existent line doesn't exist because it's
1236 * past the end-of-file. That's a pretty good guess.
1238 * If it's a "default vi command", an address of zero is okay.
1240 addr_verify:
1241 switch (ecp->addrcnt) {
1242 case 2:
1244 * Historic ex/vi permitted commands with counts to go past
1245 * EOF. So, for example, if the file only had 5 lines, the
1246 * ex command "1,6>" would fail, but the command ">300"
1247 * would succeed. Since we don't want to have to make all
1248 * of the underlying commands handle random line numbers,
1249 * fix it here.
1251 if (ecp->addr2.lno == 0) {
1252 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1253 (F_ISSET(sp, SC_EX) ||
1254 !F_ISSET(ecp, E_USELASTCMD))) {
1255 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1256 goto err;
1258 } else if (!db_exist(sp, ecp->addr2.lno))
1259 if (FL_ISSET(ecp->iflags, E_C_COUNT)) {
1260 if (db_last(sp, &lno))
1261 goto err;
1262 ecp->addr2.lno = lno;
1263 } else {
1264 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1265 goto err;
1267 /* FALLTHROUGH */
1268 case 1:
1269 if (ecp->addr1.lno == 0) {
1270 if (!F_ISSET(ecp, E_ADDR_ZERO) &&
1271 (F_ISSET(sp, SC_EX) ||
1272 !F_ISSET(ecp, E_USELASTCMD))) {
1273 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK);
1274 goto err;
1276 } else if (!db_exist(sp, ecp->addr1.lno)) {
1277 ex_badaddr(sp, NULL, A_EOF, NUM_OK);
1278 goto err;
1280 break;
1284 * If doing a default command and there's nothing left on the line,
1285 * vi just moves to the line. For example, ":3" and ":'a,'b" just
1286 * move to line 3 and line 'b, respectively, but ":3|" prints line 3.
1288 * !!!
1289 * In addition, IF THE LINE CHANGES, move to the first nonblank of
1290 * the line.
1292 * !!!
1293 * This is done before the absolute mark gets set; historically,
1294 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did.
1296 if ((F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_NOPRDEF)) &&
1297 F_ISSET(ecp, E_USELASTCMD) && vi_address == 0) {
1298 switch (ecp->addrcnt) {
1299 case 2:
1300 if (sp->lno !=
1301 (ecp->addr2.lno ? ecp->addr2.lno : 1)) {
1302 sp->lno =
1303 ecp->addr2.lno ? ecp->addr2.lno : 1;
1304 sp->cno = 0;
1305 (void)nonblank(sp, sp->lno, &sp->cno);
1307 break;
1308 case 1:
1309 if (sp->lno !=
1310 (ecp->addr1.lno ? ecp->addr1.lno : 1)) {
1311 sp->lno =
1312 ecp->addr1.lno ? ecp->addr1.lno : 1;
1313 sp->cno = 0;
1314 (void)nonblank(sp, sp->lno, &sp->cno);
1316 break;
1318 ecp->cp = ecp->save_cmd;
1319 ecp->clen = ecp->save_cmdlen;
1320 goto loop;
1324 * Set the absolute mark -- we have to set it for vi here, in case
1325 * it's a compound command, e.g. ":5p|6" should set the absolute
1326 * mark for vi.
1328 if (F_ISSET(ecp, E_ABSMARK)) {
1329 cur.lno = sp->lno;
1330 cur.cno = sp->cno;
1331 F_CLR(ecp, E_ABSMARK);
1332 if (mark_set(sp, ABSMARK1, &cur, 1))
1333 goto err;
1336 #if defined(DEBUG) && defined(COMLOG)
1337 ex_comlog(sp, ecp);
1338 #endif
1339 /* Increment the command count if not called from vi. */
1340 if (F_ISSET(sp, SC_EX))
1341 ++sp->ccnt;
1344 * If file state available, and not doing a global command,
1345 * log the start of an action.
1347 if (sp->ep != NULL && !F_ISSET(sp, SC_EX_GLOBAL))
1348 (void)log_cursor(sp);
1351 * !!!
1352 * There are two special commands for the purposes of this code: the
1353 * default command (<carriage-return>) or the scrolling commands (^D
1354 * and <EOF>) as the first non-<blank> characters in the line.
1356 * If this is the first command in the command line, we received the
1357 * command from the ex command loop and we're talking to a tty, and
1358 * and there's nothing else on the command line, and it's one of the
1359 * special commands, we move back up to the previous line, and erase
1360 * the prompt character with the output. Since ex runs in canonical
1361 * mode, we don't have to do anything else, a <newline> has already
1362 * been echoed by the tty driver. It's OK if vi calls us -- we won't
1363 * be in ex mode so we'll do nothing.
1365 if (F_ISSET(ecp, E_NRSEP)) {
1366 if (sp->ep != NULL &&
1367 F_ISSET(sp, SC_EX) && !F_ISSET(gp, G_SCRIPTED) &&
1368 (F_ISSET(ecp, E_USELASTCMD) || ecp->cmd == &cmds[C_SCROLL]))
1369 gp->scr_ex_adjust(sp, EX_TERM_SCROLL);
1370 F_CLR(ecp, E_NRSEP);
1374 * Call the underlying function for the ex command.
1376 * XXX
1377 * Interrupts behave like errors, for now.
1379 if (ecp->cmd->fn(sp, ecp) || INTERRUPTED(sp)) {
1380 if (F_ISSET(gp, G_SCRIPTED))
1381 F_SET(sp, SC_EXIT_FORCE);
1382 goto err;
1385 #ifdef DEBUG
1386 /* Make sure no function left global temporary space locked. */
1387 if (F_ISSET(gp, W_TMP_INUSE)) {
1388 F_CLR(gp, W_TMP_INUSE);
1389 msgq(sp, M_ERR, "087|%s: temporary buffer not released",
1390 ecp->cmd->name);
1392 #endif
1394 * Ex displayed the number of lines modified immediately after each
1395 * command, so the command "1,10d|1,10d" would display:
1397 * 10 lines deleted
1398 * 10 lines deleted
1399 * <autoprint line>
1401 * Executing ex commands from vi only reported the final modified
1402 * lines message -- that's wrong enough that we don't match it.
1404 if (F_ISSET(sp, SC_EX))
1405 mod_rpt(sp);
1408 * Integrate any offset parsed by the underlying command, and make
1409 * sure the referenced line exists.
1411 * XXX
1412 * May not match historic practice (which I've never been able to
1413 * completely figure out.) For example, the '=' command from vi
1414 * mode often got the offset wrong, and complained it was too large,
1415 * but didn't seem to have a problem with the cursor. If anyone
1416 * complains, ask them how it's supposed to work, they might know.
1418 if (sp->ep != NULL && ecp->flagoff) {
1419 if (ecp->flagoff < 0) {
1420 if (sp->lno <= -ecp->flagoff) {
1421 msgq(sp, M_ERR,
1422 "088|Flag offset to before line 1");
1423 goto err;
1425 } else {
1426 if (!NPFITS(DB_MAX_RECORDS, sp->lno, ecp->flagoff)) {
1427 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1428 goto err;
1430 if (!db_exist(sp, sp->lno + ecp->flagoff)) {
1431 msgq(sp, M_ERR,
1432 "089|Flag offset past end-of-file");
1433 goto err;
1436 sp->lno += ecp->flagoff;
1440 * If the command executed successfully, we may want to display a line
1441 * based on the autoprint option or an explicit print flag. (Make sure
1442 * that there's a line to display.) Also, the autoprint edit option is
1443 * turned off for the duration of global commands.
1445 if (F_ISSET(sp, SC_EX) && sp->ep != NULL && sp->lno != 0) {
1447 * The print commands have already handled the `print' flags.
1448 * If so, clear them.
1450 if (FL_ISSET(ecp->iflags, E_CLRFLAG))
1451 FL_CLR(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT);
1453 /* If hash set only because of the number option, discard it. */
1454 if (F_ISSET(ecp, E_OPTNUM))
1455 FL_CLR(ecp->iflags, E_C_HASH);
1458 * If there was an explicit flag to display the new cursor line,
1459 * or autoprint is set and a change was made, display the line.
1460 * If any print flags were set use them, else default to print.
1462 LF_INIT(FL_ISSET(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT));
1463 if (!LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT | E_NOAUTO) &&
1464 !F_ISSET(sp, SC_EX_GLOBAL) &&
1465 O_ISSET(sp, O_AUTOPRINT) && F_ISSET(ecp, E_AUTOPRINT))
1466 LF_INIT(E_C_PRINT);
1468 if (LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT)) {
1469 cur.lno = sp->lno;
1470 cur.cno = 0;
1471 (void)ex_print(sp, ecp, &cur, &cur, flags);
1476 * If the command had an associated "+cmd", it has to be executed
1477 * before we finish executing any more of this ex command. For
1478 * example, consider a .exrc file that contains the following lines:
1480 * :set all
1481 * :edit +25 file.c|s/abc/ABC/|1
1482 * :3,5 print
1484 * This can happen more than once -- the historic vi simply hung or
1485 * dropped core, of course. Prepend the + command back into the
1486 * current command and continue. We may have to add an additional
1487 * <literal next> character. We know that it will fit because we
1488 * discarded at least one space and the + character.
1490 if (arg1_len != 0) {
1492 * If the last character of the + command was a <literal next>
1493 * character, it would be treated differently because of the
1494 * append. Quote it, if necessary.
1496 if (IS_ESCAPE(sp, ecp, arg1[arg1_len - 1])) {
1497 *--ecp->save_cmd = CH_LITERAL;
1498 ++ecp->save_cmdlen;
1501 ecp->save_cmd -= arg1_len;
1502 ecp->save_cmdlen += arg1_len;
1503 MEMCPYW(ecp->save_cmd, arg1, arg1_len);
1506 * Any commands executed from a +cmd are executed starting at
1507 * the first column of the last line of the file -- NOT the
1508 * first nonblank.) The main file startup code doesn't know
1509 * that a +cmd was set, however, so it may have put us at the
1510 * top of the file. (Note, this is safe because we must have
1511 * switched files to get here.)
1513 F_SET(ecp, E_MOVETOEND);
1516 /* Update the current command. */
1517 ecp->cp = ecp->save_cmd;
1518 ecp->clen = ecp->save_cmdlen;
1521 * !!!
1522 * If we've changed screens or underlying files, any pending global or
1523 * v command, or @ buffer that has associated addresses, has to be
1524 * discarded. This is historic practice for globals, and necessary for
1525 * @ buffers that had associated addresses.
1527 * Otherwise, if we've changed underlying files, it's not a problem,
1528 * we continue with the rest of the ex command(s), operating on the
1529 * new file. However, if we switch screens (either by exiting or by
1530 * an explicit command), we have no way of knowing where to put output
1531 * messages, and, since we don't control screens here, we could screw
1532 * up the upper layers, (e.g. we could exit/reenter a screen multiple
1533 * times). So, return and continue after we've got a new screen.
1535 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH)) {
1536 at_found = gv_found = 0;
1537 for (ecp = wp->ecq.lh_first;
1538 ecp != NULL; ecp = ecp->q.le_next)
1539 switch (ecp->agv_flags) {
1540 case 0:
1541 case AGV_AT_NORANGE:
1542 break;
1543 case AGV_AT:
1544 if (!at_found) {
1545 at_found = 1;
1546 msgq(sp, M_ERR,
1547 "090|@ with range running when the file/screen changed");
1549 break;
1550 case AGV_GLOBAL:
1551 case AGV_V:
1552 if (!gv_found) {
1553 gv_found = 1;
1554 msgq(sp, M_ERR,
1555 "091|Global/v command running when the file/screen changed");
1557 break;
1558 default:
1559 abort();
1561 if (at_found || gv_found)
1562 goto discard;
1563 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_SSWITCH))
1564 goto rsuccess;
1567 goto loop;
1568 /* NOTREACHED */
1570 err: /*
1571 * On command failure, we discard keys and pending commands remaining,
1572 * as well as any keys that were mapped and waiting. The save_cmdlen
1573 * test is not necessarily correct. If we fail early enough we don't
1574 * know if the entire string was a single command or not. Guess, as
1575 * it's useful to know if commands other than the current one are being
1576 * discarded.
1578 if (ecp->save_cmdlen == 0)
1579 for (; ecp->clen; --ecp->clen) {
1580 ch = *ecp->cp++;
1581 if (IS_ESCAPE(sp, ecp, ch) && ecp->clen > 1) {
1582 --ecp->clen;
1583 ++ecp->cp;
1584 } else if (ch == '\n' || ch == '|') {
1585 if (ecp->clen > 1)
1586 ecp->save_cmdlen = 1;
1587 break;
1590 if (ecp->save_cmdlen != 0 || wp->ecq.lh_first != &wp->excmd) {
1591 discard: msgq(sp, M_BERR,
1592 "092|Ex command failed: pending commands discarded");
1593 ex_discard(sp);
1595 if (v_event_flush(sp, CH_MAPPED))
1596 msgq(sp, M_BERR,
1597 "093|Ex command failed: mapped keys discarded");
1599 rfail: tmp = 1;
1600 if (0)
1601 rsuccess: tmp = 0;
1603 /* Turn off any file name error information. */
1604 wp->if_name = NULL;
1606 /* Turn off the global bit. */
1607 F_CLR(sp, SC_EX_GLOBAL);
1609 return (tmp);
1613 * ex_range --
1614 * Get a line range for ex commands, or perform a vi ex address search.
1616 * PUBLIC: int ex_range __P((SCR *, EXCMD *, int *));
1619 ex_range(sp, ecp, errp)
1620 SCR *sp;
1621 EXCMD *ecp;
1622 int *errp;
1624 enum { ADDR_FOUND, ADDR_NEED, ADDR_NONE } addr;
1625 GS *gp;
1626 EX_PRIVATE *exp;
1627 MARK m;
1628 int isaddr;
1630 *errp = 0;
1633 * Parse comma or semi-colon delimited line specs.
1635 * Semi-colon delimiters update the current address to be the last
1636 * address. For example, the command
1638 * :3;/pattern/ecp->cp
1640 * will search for pattern from line 3. In addition, if ecp->cp
1641 * is not a valid command, the current line will be left at 3, not
1642 * at the original address.
1644 * Extra addresses are discarded, starting with the first.
1646 * !!!
1647 * If any addresses are missing, they default to the current line.
1648 * This was historically true for both leading and trailing comma
1649 * delimited addresses as well as for trailing semicolon delimited
1650 * addresses. For consistency, we make it true for leading semicolon
1651 * addresses as well.
1653 gp = sp->gp;
1654 exp = EXP(sp);
1655 for (addr = ADDR_NONE, ecp->addrcnt = 0; ecp->clen > 0;)
1656 switch (*ecp->cp) {
1657 case '%': /* Entire file. */
1658 /* Vi ex address searches didn't permit % signs. */
1659 if (F_ISSET(ecp, E_VISEARCH))
1660 goto ret;
1662 /* It's an error if the file is empty. */
1663 if (sp->ep == NULL) {
1664 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1665 *errp = 1;
1666 return (0);
1669 * !!!
1670 * A percent character addresses all of the lines in
1671 * the file. Historically, it couldn't be followed by
1672 * any other address. We do it as a text substitution
1673 * for simplicity. POSIX 1003.2 is expected to follow
1674 * this practice.
1676 * If it's an empty file, the first line is 0, not 1.
1678 if (addr == ADDR_FOUND) {
1679 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1680 *errp = 1;
1681 return (0);
1683 if (db_last(sp, &ecp->addr2.lno))
1684 return (1);
1685 ecp->addr1.lno = ecp->addr2.lno == 0 ? 0 : 1;
1686 ecp->addr1.cno = ecp->addr2.cno = 0;
1687 ecp->addrcnt = 2;
1688 addr = ADDR_FOUND;
1689 ++ecp->cp;
1690 --ecp->clen;
1691 break;
1692 case ',': /* Comma delimiter. */
1693 /* Vi ex address searches didn't permit commas. */
1694 if (F_ISSET(ecp, E_VISEARCH))
1695 goto ret;
1696 /* FALLTHROUGH */
1697 case ';': /* Semi-colon delimiter. */
1698 if (sp->ep == NULL) {
1699 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1700 *errp = 1;
1701 return (0);
1703 if (addr != ADDR_FOUND)
1704 switch (ecp->addrcnt) {
1705 case 0:
1706 ecp->addr1.lno = sp->lno;
1707 ecp->addr1.cno = sp->cno;
1708 ecp->addrcnt = 1;
1709 break;
1710 case 2:
1711 ecp->addr1 = ecp->addr2;
1712 /* FALLTHROUGH */
1713 case 1:
1714 ecp->addr2.lno = sp->lno;
1715 ecp->addr2.cno = sp->cno;
1716 ecp->addrcnt = 2;
1717 break;
1719 if (*ecp->cp == ';')
1720 switch (ecp->addrcnt) {
1721 case 0:
1722 abort();
1723 /* NOTREACHED */
1724 case 1:
1725 sp->lno = ecp->addr1.lno;
1726 sp->cno = ecp->addr1.cno;
1727 break;
1728 case 2:
1729 sp->lno = ecp->addr2.lno;
1730 sp->cno = ecp->addr2.cno;
1731 break;
1733 addr = ADDR_NEED;
1734 /* FALLTHROUGH */
1735 case ' ': /* Whitespace. */
1736 case '\t': /* Whitespace. */
1737 ++ecp->cp;
1738 --ecp->clen;
1739 break;
1740 default:
1741 /* Get a line specification. */
1742 if (ex_line(sp, ecp, &m, &isaddr, errp))
1743 return (1);
1744 if (*errp)
1745 return (0);
1746 if (!isaddr)
1747 goto ret;
1748 if (addr == ADDR_FOUND) {
1749 ex_badaddr(sp, NULL, A_COMBO, NUM_OK);
1750 *errp = 1;
1751 return (0);
1753 switch (ecp->addrcnt) {
1754 case 0:
1755 ecp->addr1 = m;
1756 ecp->addrcnt = 1;
1757 break;
1758 case 1:
1759 ecp->addr2 = m;
1760 ecp->addrcnt = 2;
1761 break;
1762 case 2:
1763 ecp->addr1 = ecp->addr2;
1764 ecp->addr2 = m;
1765 break;
1767 addr = ADDR_FOUND;
1768 break;
1772 * !!!
1773 * Vi ex address searches are indifferent to order or trailing
1774 * semi-colons.
1776 ret: if (F_ISSET(ecp, E_VISEARCH))
1777 return (0);
1779 if (addr == ADDR_NEED)
1780 switch (ecp->addrcnt) {
1781 case 0:
1782 ecp->addr1.lno = sp->lno;
1783 ecp->addr1.cno = sp->cno;
1784 ecp->addrcnt = 1;
1785 break;
1786 case 2:
1787 ecp->addr1 = ecp->addr2;
1788 /* FALLTHROUGH */
1789 case 1:
1790 ecp->addr2.lno = sp->lno;
1791 ecp->addr2.cno = sp->cno;
1792 ecp->addrcnt = 2;
1793 break;
1796 if (ecp->addrcnt == 2 && ecp->addr2.lno < ecp->addr1.lno) {
1797 msgq(sp, M_ERR,
1798 "094|The second address is smaller than the first");
1799 *errp = 1;
1801 return (0);
1805 * ex_line --
1806 * Get a single line address specifier.
1808 * The way the "previous context" mark worked was that any "non-relative"
1809 * motion set it. While ex/vi wasn't totally consistent about this, ANY
1810 * numeric address, search pattern, '$', or mark reference in an address
1811 * was considered non-relative, and set the value. Which should explain
1812 * why we're hacking marks down here. The problem was that the mark was
1813 * only set if the command was called, i.e. we have to set a flag and test
1814 * it later.
1816 * XXX
1817 * This is probably still not exactly historic practice, although I think
1818 * it's fairly close.
1820 static int
1821 ex_line(sp, ecp, mp, isaddrp, errp)
1822 SCR *sp;
1823 EXCMD *ecp;
1824 MARK *mp;
1825 int *isaddrp, *errp;
1827 enum nresult nret;
1828 EX_PRIVATE *exp;
1829 GS *gp;
1830 long total, val;
1831 int isneg;
1832 int (*sf) __P((SCR *, MARK *, MARK *, CHAR_T *, size_t, CHAR_T **, u_int));
1833 CHAR_T *endp;
1835 gp = sp->gp;
1836 exp = EXP(sp);
1838 *isaddrp = *errp = 0;
1839 F_CLR(ecp, E_DELTA);
1841 /* No addresses permitted until a file has been read in. */
1842 if (sp->ep == NULL && strchr("$0123456789'\\/?.+-^", *ecp->cp)) {
1843 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK);
1844 *errp = 1;
1845 return (0);
1848 switch (*ecp->cp) {
1849 case '$': /* Last line in the file. */
1850 *isaddrp = 1;
1851 F_SET(ecp, E_ABSMARK);
1853 mp->cno = 0;
1854 if (db_last(sp, &mp->lno))
1855 return (1);
1856 ++ecp->cp;
1857 --ecp->clen;
1858 break; /* Absolute line number. */
1859 case '0': case '1': case '2': case '3': case '4':
1860 case '5': case '6': case '7': case '8': case '9':
1861 *isaddrp = 1;
1862 F_SET(ecp, E_ABSMARK);
1864 if ((nret = nget_slong(sp, &val, ecp->cp, &endp, 10)) != NUM_OK) {
1865 ex_badaddr(sp, NULL, A_NOTSET, nret);
1866 *errp = 1;
1867 return (0);
1869 if (!NPFITS(DB_MAX_RECORDS, 0, val)) {
1870 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
1871 *errp = 1;
1872 return (0);
1874 mp->lno = val;
1875 mp->cno = 0;
1876 ecp->clen -= (endp - ecp->cp);
1877 ecp->cp = endp;
1878 break;
1879 case '\'': /* Use a mark. */
1880 *isaddrp = 1;
1881 F_SET(ecp, E_ABSMARK);
1883 if (ecp->clen == 1) {
1884 msgq(sp, M_ERR, "095|No mark name supplied");
1885 *errp = 1;
1886 return (0);
1888 if (mark_get(sp, ecp->cp[1], mp, M_ERR)) {
1889 *errp = 1;
1890 return (0);
1892 ecp->cp += 2;
1893 ecp->clen -= 2;
1894 break;
1895 case '\\': /* Search: forward/backward. */
1897 * !!!
1898 * I can't find any difference between // and \/ or between
1899 * ?? and \?. Mark Horton doesn't remember there being any
1900 * difference. C'est la vie.
1902 if (ecp->clen < 2 ||
1903 ecp->cp[1] != '/' && ecp->cp[1] != '?') {
1904 msgq(sp, M_ERR, "096|\\ not followed by / or ?");
1905 *errp = 1;
1906 return (0);
1908 ++ecp->cp;
1909 --ecp->clen;
1910 sf = ecp->cp[0] == '/' ? f_search : b_search;
1911 goto search;
1912 case '/': /* Search forward. */
1913 sf = f_search;
1914 goto search;
1915 case '?': /* Search backward. */
1916 sf = b_search;
1918 search: mp->lno = sp->lno;
1919 mp->cno = sp->cno;
1920 if (sf(sp, mp, mp, ecp->cp, ecp->clen, &endp,
1921 SEARCH_MSG | SEARCH_PARSE | SEARCH_SET |
1922 (F_ISSET(ecp, E_SEARCH_WMSG) ? SEARCH_WMSG : 0))) {
1923 *errp = 1;
1924 return (0);
1927 /* Fix up the command pointers. */
1928 ecp->clen -= (endp - ecp->cp);
1929 ecp->cp = endp;
1931 *isaddrp = 1;
1932 F_SET(ecp, E_ABSMARK);
1933 break;
1934 case '.': /* Current position. */
1935 *isaddrp = 1;
1936 mp->cno = sp->cno;
1938 /* If an empty file, then '.' is 0, not 1. */
1939 if (sp->lno == 1) {
1940 if (db_last(sp, &mp->lno))
1941 return (1);
1942 if (mp->lno != 0)
1943 mp->lno = 1;
1944 } else
1945 mp->lno = sp->lno;
1948 * !!!
1949 * Historically, .<number> was the same as .+<number>, i.e.
1950 * the '+' could be omitted. (This feature is found in ed
1951 * as well.)
1953 if (ecp->clen > 1 && isdigit(ecp->cp[1]))
1954 *ecp->cp = '+';
1955 else {
1956 ++ecp->cp;
1957 --ecp->clen;
1959 break;
1962 /* Skip trailing <blank>s. */
1963 for (; ecp->clen > 0 &&
1964 isblank(ecp->cp[0]); ++ecp->cp, --ecp->clen);
1967 * Evaluate any offset. If no address yet found, the offset
1968 * is relative to ".".
1970 total = 0;
1971 if (ecp->clen != 0 && (isdigit(ecp->cp[0]) ||
1972 ecp->cp[0] == '+' || ecp->cp[0] == '-' ||
1973 ecp->cp[0] == '^')) {
1974 if (!*isaddrp) {
1975 *isaddrp = 1;
1976 mp->lno = sp->lno;
1977 mp->cno = sp->cno;
1980 * Evaluate an offset, defined as:
1982 * [+-^<blank>]*[<blank>]*[0-9]*
1984 * The rough translation is any number of signs, optionally
1985 * followed by numbers, or a number by itself, all <blank>
1986 * separated.
1988 * !!!
1989 * All address offsets were additive, e.g. "2 2 3p" was the
1990 * same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2".
1991 * Note, however, "2 /ZZZ/" was an error. It was also legal
1992 * to insert signs without numbers, so "3 - 2" was legal, and
1993 * equal to 4.
1995 * !!!
1996 * Offsets were historically permitted for any line address,
1997 * e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after
1998 * line 8.
2000 * !!!
2001 * Offsets were historically permitted for search commands,
2002 * and handled as addresses: "/pattern/2 2 2" was legal, and
2003 * referenced the 6th line after pattern.
2005 F_SET(ecp, E_DELTA);
2006 for (;;) {
2007 for (; ecp->clen > 0 && isblank(ecp->cp[0]);
2008 ++ecp->cp, --ecp->clen);
2009 if (ecp->clen == 0 || !isdigit(ecp->cp[0]) &&
2010 ecp->cp[0] != '+' && ecp->cp[0] != '-' &&
2011 ecp->cp[0] != '^')
2012 break;
2013 if (!isdigit(ecp->cp[0]) &&
2014 !isdigit(ecp->cp[1])) {
2015 total += ecp->cp[0] == '+' ? 1 : -1;
2016 --ecp->clen;
2017 ++ecp->cp;
2018 } else {
2019 if (ecp->cp[0] == '-' ||
2020 ecp->cp[0] == '^') {
2021 ++ecp->cp;
2022 --ecp->clen;
2023 isneg = 1;
2024 } else
2025 isneg = 0;
2027 /* Get a signed long, add it to the total. */
2028 if ((nret = nget_slong(sp, &val,
2029 ecp->cp, &endp, 10)) != NUM_OK ||
2030 (nret = NADD_SLONG(sp,
2031 total, val)) != NUM_OK) {
2032 ex_badaddr(sp, NULL, A_NOTSET, nret);
2033 *errp = 1;
2034 return (0);
2036 total += isneg ? -val : val;
2037 ecp->clen -= (endp - ecp->cp);
2038 ecp->cp = endp;
2044 * Any value less than 0 is an error. Make sure that the new value
2045 * will fit into a db_recno_t.
2047 if (*isaddrp && total != 0) {
2048 if (total < 0) {
2049 if (-total > mp->lno) {
2050 msgq(sp, M_ERR,
2051 "097|Reference to a line number less than 0");
2052 *errp = 1;
2053 return (0);
2055 } else
2056 if (!NPFITS(DB_MAX_RECORDS, mp->lno, total)) {
2057 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER);
2058 *errp = 1;
2059 return (0);
2061 mp->lno += total;
2063 return (0);
2068 * ex_load --
2069 * Load up the next command, which may be an @ buffer or global command.
2071 static int
2072 ex_load(sp)
2073 SCR *sp;
2075 WIN *wp;
2076 EXCMD *ecp;
2077 RANGE *rp;
2079 F_CLR(sp, SC_EX_GLOBAL);
2082 * Lose any exhausted commands. We know that the first command
2083 * can't be an AGV command, which makes things a bit easier.
2085 for (wp = sp->wp;;) {
2087 * If we're back to the original structure, leave it around,
2088 * but discard any allocated source name, we've returned to
2089 * the beginning of the command stack.
2091 if ((ecp = wp->ecq.lh_first) == &wp->excmd) {
2092 if (F_ISSET(ecp, E_NAMEDISCARD)) {
2093 free(ecp->if_name);
2094 ecp->if_name = NULL;
2096 return (0);
2100 * ecp->clen will be 0 for the first discarded command, but
2101 * may not be 0 for subsequent ones, e.g. if the original
2102 * command was ":g/xx/@a|s/b/c/", then when we discard the
2103 * command pushed on the stack by the @a, we have to resume
2104 * the global command which included the substitute command.
2106 if (ecp->clen != 0)
2107 return (0);
2110 * If it's an @, global or v command, we may need to continue
2111 * the command on a different line.
2113 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2114 /* Discard any exhausted ranges. */
2115 while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq)
2116 if (rp->start > rp->stop) {
2117 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
2118 free(rp);
2119 } else
2120 break;
2122 /* If there's another range, continue with it. */
2123 if (rp != (void *)&ecp->rq)
2124 break;
2126 /* If it's a global/v command, fix up the last line. */
2127 if (FL_ISSET(ecp->agv_flags,
2128 AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO)
2129 if (db_exist(sp, ecp->range_lno))
2130 sp->lno = ecp->range_lno;
2131 else {
2132 if (db_last(sp, &sp->lno))
2133 return (1);
2134 if (sp->lno == 0)
2135 sp->lno = 1;
2137 free(ecp->o_cp);
2140 /* Discard the EXCMD. */
2141 LIST_REMOVE(ecp, q);
2142 free(ecp);
2146 * We only get here if it's an active @, global or v command. Set
2147 * the current line number, and get a new copy of the command for
2148 * the parser. Note, the original pointer almost certainly moved,
2149 * so we have play games.
2151 ecp->cp = ecp->o_cp;
2152 MEMCPYW(ecp->cp, ecp->cp + ecp->o_clen, ecp->o_clen);
2153 ecp->clen = ecp->o_clen;
2154 ecp->range_lno = sp->lno = rp->start++;
2156 if (FL_ISSET(ecp->agv_flags, AGV_GLOBAL | AGV_V))
2157 F_SET(sp, SC_EX_GLOBAL);
2158 return (0);
2162 * ex_discard --
2163 * Discard any pending ex commands.
2165 static int
2166 ex_discard(sp)
2167 SCR *sp;
2169 WIN *wp;
2170 EXCMD *ecp;
2171 RANGE *rp;
2174 * We know the first command can't be an AGV command, so we don't
2175 * process it specially. We do, however, nail the command itself.
2177 for (wp = sp->wp; (ecp = wp->ecq.lh_first) != &wp->excmd;) {
2178 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) {
2179 while ((rp = ecp->rq.cqh_first) != (void *)&ecp->rq) {
2180 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
2181 free(rp);
2183 free(ecp->o_cp);
2185 LIST_REMOVE(ecp, q);
2186 free(ecp);
2188 wp->ecq.lh_first->clen = 0;
2189 return (0);
2193 * ex_unknown --
2194 * Display an unknown command name.
2196 static void
2197 ex_unknown(sp, cmd, len)
2198 SCR *sp;
2199 CHAR_T *cmd;
2200 size_t len;
2202 size_t blen;
2203 CHAR_T *bp;
2205 GET_SPACE_GOTOW(sp, bp, blen, len + 1);
2206 bp[len] = '\0';
2207 MEMCPYW(bp, cmd, len);
2208 msgq_wstr(sp, M_ERR, bp, "098|The %s command is unknown");
2209 FREE_SPACEW(sp, bp, blen);
2211 alloc_err:
2212 return;
2216 * ex_is_abbrev -
2217 * The vi text input routine needs to know if ex thinks this is an
2218 * [un]abbreviate command, so it can turn off abbreviations. See
2219 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine.
2221 * PUBLIC: int ex_is_abbrev __P((SCR *, CHAR_T *, size_t));
2224 ex_is_abbrev(SCR *sp, CHAR_T *name, size_t len)
2226 EXCMDLIST const *cp;
2228 return ((cp = ex_comm_search(sp, 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((SCR *, CHAR_T *, size_t));
2241 ex_is_unmap(SCR *sp, CHAR_T *name, size_t len)
2243 EXCMDLIST const *cp;
2246 * The command the vi input routines are really interested in
2247 * is "unmap!", not just unmap.
2249 if (name[len - 1] != '!')
2250 return (0);
2251 --len;
2252 return ((cp = ex_comm_search(sp, name, len)) != NULL &&
2253 cp == &cmds[C_UNMAP]);
2257 * ex_comm_search --
2258 * Search for a command name.
2260 static EXCMDLIST const *
2261 ex_comm_search(SCR *sp, CHAR_T *name, size_t len)
2263 EXCMDLIST const *cp;
2264 size_t nlen;
2265 char *nname;
2267 INT2CHAR(sp, name, len, nname, nlen);
2269 for (cp = cmds; cp->name != NULL; ++cp) {
2270 if (cp->name[0] > nname[0])
2271 return (NULL);
2272 if (cp->name[0] != nname[0])
2273 continue;
2274 if (!memcmp(nname, 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