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