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 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 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 /* 824 * Set the default addresses. It's an error to specify an address for 825 * a command that doesn't take them. If two addresses are specified 826 * for a command that only takes one, lose the first one. Two special 827 * cases here, some commands take 0 or 2 addresses. For most of them 828 * (the E_ADDR2_ALL flag), 0 defaults to the entire file. For one 829 * (the `!' command, the E_ADDR2_NONE flag), 0 defaults to no lines. 830 * 831 * Also, if the file is empty, some commands want to use an address of 832 * 0, i.e. the entire file is 0 to 0, and the default first address is 833 * 0. Otherwise, an entire file is 1 to N and the default line is 1. 834 * Note, we also add the E_ADDR_ZERO flag to the command flags, for the 835 * case where the 0 address is only valid if it's a default address. 836 * 837 * Also, set a flag if we set the default addresses. Some commands 838 * (ex: z) care if the user specified an address or if we just used 839 * the current cursor. 840 */ 841 switch (F_ISSET(ecp, E_ADDR1 | E_ADDR2 | E_ADDR2_ALL | E_ADDR2_NONE)) { 842 case E_ADDR1: /* One address: */ 843 switch (ecp->addrcnt) { 844 case 0: /* Default cursor/empty file. */ 845 ecp->addrcnt = 1; 846 F_SET(ecp, E_ADDR_DEF); 847 if (F_ISSET(ecp, E_ADDR_ZERODEF)) { 848 if (db_last(sp, &lno)) 849 goto err; 850 if (lno == 0) { 851 ecp->addr1.lno = 0; 852 F_SET(ecp, E_ADDR_ZERO); 853 } else 854 ecp->addr1.lno = sp->lno; 855 } else 856 ecp->addr1.lno = sp->lno; 857 ecp->addr1.cno = sp->cno; 858 break; 859 case 1: 860 break; 861 case 2: /* Lose the first address. */ 862 ecp->addrcnt = 1; 863 ecp->addr1 = ecp->addr2; 864 } 865 break; 866 case E_ADDR2_NONE: /* Zero/two addresses: */ 867 if (ecp->addrcnt == 0) /* Default to nothing. */ 868 break; 869 goto two_addr; 870 case E_ADDR2_ALL: /* Zero/two addresses: */ 871 if (ecp->addrcnt == 0) { /* Default entire/empty file. */ 872 F_SET(ecp, E_ADDR_DEF); 873 ecp->addrcnt = 2; 874 if (sp->ep == NULL) 875 ecp->addr2.lno = 0; 876 else if (db_last(sp, &ecp->addr2.lno)) 877 goto err; 878 if (F_ISSET(ecp, E_ADDR_ZERODEF) && 879 ecp->addr2.lno == 0) { 880 ecp->addr1.lno = 0; 881 F_SET(ecp, E_ADDR_ZERO); 882 } else 883 ecp->addr1.lno = 1; 884 ecp->addr1.cno = ecp->addr2.cno = 0; 885 F_SET(ecp, E_ADDR2_ALL); 886 break; 887 } 888 /* FALLTHROUGH */ 889 case E_ADDR2: /* Two addresses: */ 890 two_addr: switch (ecp->addrcnt) { 891 case 0: /* Default cursor/empty file. */ 892 ecp->addrcnt = 2; 893 F_SET(ecp, E_ADDR_DEF); 894 if (sp->lno == 1 && 895 F_ISSET(ecp, E_ADDR_ZERODEF)) { 896 if (db_last(sp, &lno)) 897 goto err; 898 if (lno == 0) { 899 ecp->addr1.lno = ecp->addr2.lno = 0; 900 F_SET(ecp, E_ADDR_ZERO); 901 } else 902 ecp->addr1.lno = 903 ecp->addr2.lno = sp->lno; 904 } else 905 ecp->addr1.lno = ecp->addr2.lno = sp->lno; 906 ecp->addr1.cno = ecp->addr2.cno = sp->cno; 907 break; 908 case 1: /* Default to first address. */ 909 ecp->addrcnt = 2; 910 ecp->addr2 = ecp->addr1; 911 break; 912 case 2: 913 break; 914 } 915 break; 916 default: 917 if (ecp->addrcnt) /* Error. */ 918 goto usage; 919 } 920 921 /* 922 * !!! 923 * The ^D scroll command historically scrolled the value of the scroll 924 * option or to EOF. It was an error if the cursor was already at EOF. 925 * (Leading addresses were permitted, but were then ignored.) 926 */ 927 if (ecp->cmd == &cmds[C_SCROLL]) { 928 ecp->addrcnt = 2; 929 ecp->addr1.lno = sp->lno + 1; 930 ecp->addr2.lno = sp->lno + O_VAL(sp, O_SCROLL); 931 ecp->addr1.cno = ecp->addr2.cno = sp->cno; 932 if (db_last(sp, &lno)) 933 goto err; 934 if (lno != 0 && lno > sp->lno && ecp->addr2.lno > lno) 935 ecp->addr2.lno = lno; 936 } 937 938 ecp->flagoff = 0; 939 for (np = ecp->cmd->syntax; *np != '\0'; ++np) { 940 /* 941 * The force flag is sensitive to leading whitespace, i.e. 942 * "next !" is different from "next!". Handle it before 943 * skipping leading <blank>s. 944 */ 945 if (*np == '!') { 946 if (ecp->clen > 0 && *ecp->cp == '!') { 947 ++ecp->cp; 948 --ecp->clen; 949 FL_SET(ecp->iflags, E_C_FORCE); 950 } 951 continue; 952 } 953 954 /* Skip leading <blank>s. */ 955 for (; ecp->clen > 0; --ecp->clen, ++ecp->cp) 956 if (!cmdskip(*ecp->cp)) 957 break; 958 if (ecp->clen == 0) 959 break; 960 961 switch (*np) { 962 case '1': /* +, -, #, l, p */ 963 /* 964 * !!! 965 * Historically, some flags were ignored depending 966 * on where they occurred in the command line. For 967 * example, in the command, ":3+++p--#", historic vi 968 * acted on the '#' flag, but ignored the '-' flags. 969 * It's unambiguous what the flags mean, so we just 970 * handle them regardless of the stupidity of their 971 * location. 972 */ 973 for (; ecp->clen; --ecp->clen, ++ecp->cp) 974 switch (*ecp->cp) { 975 case '+': 976 ++ecp->flagoff; 977 break; 978 case '-': 979 case '^': 980 --ecp->flagoff; 981 break; 982 case '#': 983 F_CLR(ecp, E_OPTNUM); 984 FL_SET(ecp->iflags, E_C_HASH); 985 exp->fdef |= E_C_HASH; 986 break; 987 case 'l': 988 FL_SET(ecp->iflags, E_C_LIST); 989 exp->fdef |= E_C_LIST; 990 break; 991 case 'p': 992 FL_SET(ecp->iflags, E_C_PRINT); 993 exp->fdef |= E_C_PRINT; 994 break; 995 default: 996 goto end_case1; 997 } 998 end_case1: break; 999 case '2': /* -, ., +, ^ */ 1000 case '3': /* -, ., +, ^, = */ 1001 for (; ecp->clen; --ecp->clen, ++ecp->cp) 1002 switch (*ecp->cp) { 1003 case '-': 1004 FL_SET(ecp->iflags, E_C_DASH); 1005 break; 1006 case '.': 1007 FL_SET(ecp->iflags, E_C_DOT); 1008 break; 1009 case '+': 1010 FL_SET(ecp->iflags, E_C_PLUS); 1011 break; 1012 case '^': 1013 FL_SET(ecp->iflags, E_C_CARAT); 1014 break; 1015 case '=': 1016 if (*np == '3') { 1017 FL_SET(ecp->iflags, E_C_EQUAL); 1018 break; 1019 } 1020 /* FALLTHROUGH */ 1021 default: 1022 goto end_case23; 1023 } 1024 end_case23: break; 1025 case 'b': /* buffer */ 1026 /* 1027 * !!! 1028 * Historically, "d #" was a delete with a flag, not a 1029 * delete into the '#' buffer. If the current command 1030 * permits a flag, don't use one as a buffer. However, 1031 * the 'l' and 'p' flags were legal buffer names in the 1032 * historic ex, and were used as buffers, not flags. 1033 */ 1034 if ((ecp->cp[0] == '+' || ecp->cp[0] == '-' || 1035 ecp->cp[0] == '^' || ecp->cp[0] == '#') && 1036 strchr(np, '1') != NULL) 1037 break; 1038 /* 1039 * !!! 1040 * Digits can't be buffer names in ex commands, or the 1041 * command "d2" would be a delete into buffer '2', and 1042 * not a two-line deletion. 1043 */ 1044 if (!ISDIGIT(ecp->cp[0])) { 1045 ecp->buffer = *ecp->cp; 1046 ++ecp->cp; 1047 --ecp->clen; 1048 FL_SET(ecp->iflags, E_C_BUFFER); 1049 } 1050 break; 1051 case 'c': /* count [01+a] */ 1052 ++np; 1053 /* Validate any signed value. */ 1054 if (!ISDIGIT(*ecp->cp) && (*np != '+' || 1055 (*ecp->cp != '+' && *ecp->cp != '-'))) 1056 break; 1057 /* If a signed value, set appropriate flags. */ 1058 if (*ecp->cp == '-') 1059 FL_SET(ecp->iflags, E_C_COUNT_NEG); 1060 else if (*ecp->cp == '+') 1061 FL_SET(ecp->iflags, E_C_COUNT_POS); 1062 if ((nret = 1063 nget_slong(<mp, ecp->cp, &t, 10)) != NUM_OK) { 1064 ex_badaddr(sp, NULL, A_NOTSET, nret); 1065 goto err; 1066 } 1067 if (ltmp == 0 && *np != '0') { 1068 msgq(sp, M_ERR, "083|Count may not be zero"); 1069 goto err; 1070 } 1071 ecp->clen -= (t - ecp->cp); 1072 ecp->cp = t; 1073 1074 /* 1075 * Counts as address offsets occur in commands taking 1076 * two addresses. Historic vi practice was to use 1077 * the count as an offset from the *second* address. 1078 * 1079 * Set a count flag; some underlying commands (see 1080 * join) do different things with counts than with 1081 * line addresses. 1082 */ 1083 if (*np == 'a') { 1084 ecp->addr1 = ecp->addr2; 1085 ecp->addr2.lno = ecp->addr1.lno + ltmp - 1; 1086 } else 1087 ecp->count = ltmp; 1088 FL_SET(ecp->iflags, E_C_COUNT); 1089 break; 1090 case 'f': /* file */ 1091 if (argv_exp2(sp, ecp, ecp->cp, ecp->clen)) 1092 goto err; 1093 goto arg_cnt_chk; 1094 case 'l': /* line */ 1095 /* 1096 * Get a line specification. 1097 * 1098 * If the line was a search expression, we may have 1099 * changed state during the call, and we're now 1100 * searching the file. Push ourselves onto the state 1101 * stack. 1102 */ 1103 if (ex_line(sp, ecp, &cur, &isaddr, &tmp)) 1104 goto rfail; 1105 if (tmp) 1106 goto err; 1107 1108 /* Line specifications are always required. */ 1109 if (!isaddr) { 1110 msgq_wstr(sp, M_ERR, ecp->cp, 1111 "084|%s: bad line specification"); 1112 goto err; 1113 } 1114 /* 1115 * The target line should exist for these commands, 1116 * but 0 is legal for them as well. 1117 */ 1118 if (cur.lno != 0 && !db_exist(sp, cur.lno)) { 1119 ex_badaddr(sp, NULL, A_EOF, NUM_OK); 1120 goto err; 1121 } 1122 ecp->lineno = cur.lno; 1123 break; 1124 case 'S': /* string, file exp. */ 1125 if (ecp->clen != 0) { 1126 if (argv_exp1(sp, ecp, ecp->cp, 1127 ecp->clen, ecp->cmd == &cmds[C_BANG])) 1128 goto err; 1129 goto addr_verify; 1130 } 1131 /* FALLTHROUGH */ 1132 case 's': /* string */ 1133 if (argv_exp0(sp, ecp, ecp->cp, ecp->clen)) 1134 goto err; 1135 goto addr_verify; 1136 case 'W': /* word string */ 1137 /* 1138 * QUOTING NOTE: 1139 * 1140 * Literal next characters escape the following 1141 * character. Quoting characters are stripped here 1142 * since they are no longer useful. 1143 * 1144 * First there was the word. 1145 */ 1146 for (p = t = ecp->cp; 1147 ecp->clen > 0; --ecp->clen, ++ecp->cp) { 1148 ch = *ecp->cp; 1149 if (IS_ESCAPE(sp, 1150 ecp, ch) && ecp->clen > 1) { 1151 --ecp->clen; 1152 *p++ = *++ecp->cp; 1153 } else if (cmdskip(ch)) { 1154 ++ecp->cp; 1155 --ecp->clen; 1156 break; 1157 } else 1158 *p++ = ch; 1159 } 1160 if (argv_exp0(sp, ecp, t, p - t)) 1161 goto err; 1162 1163 /* Delete intervening whitespace. */ 1164 for (; ecp->clen > 0; 1165 --ecp->clen, ++ecp->cp) { 1166 ch = *ecp->cp; 1167 if (!cmdskip(ch)) 1168 break; 1169 } 1170 if (ecp->clen == 0) 1171 goto usage; 1172 1173 /* Followed by the string. */ 1174 for (p = t = ecp->cp; ecp->clen > 0; 1175 --ecp->clen, ++ecp->cp, ++p) { 1176 ch = *ecp->cp; 1177 if (IS_ESCAPE(sp, 1178 ecp, ch) && ecp->clen > 1) { 1179 --ecp->clen; 1180 *p = *++ecp->cp; 1181 } else 1182 *p = ch; 1183 } 1184 if (argv_exp0(sp, ecp, t, p - t)) 1185 goto err; 1186 goto addr_verify; 1187 case 'w': /* word */ 1188 if (argv_exp3(sp, ecp, ecp->cp, ecp->clen)) 1189 goto err; 1190 arg_cnt_chk: if (*++np != 'N') { /* N */ 1191 /* 1192 * If a number is specified, must either be 1193 * 0 or that number, if optional, and that 1194 * number, if required. 1195 */ 1196 tmp = *np - '0'; 1197 if ((*++np != 'o' || exp->argsoff != 0) && 1198 exp->argsoff != tmp) 1199 goto usage; 1200 } 1201 goto addr_verify; 1202 default: { 1203 size_t nlen; 1204 char *nstr; 1205 1206 INT2CHAR(sp, ecp->cmd->name, STRLEN(ecp->cmd->name) + 1, 1207 nstr, nlen); 1208 msgq(sp, M_ERR, 1209 "085|Internal syntax table error (%s: %s)", 1210 nstr, KEY_NAME(sp, *np)); 1211 } 1212 } 1213 } 1214 1215 /* Skip trailing whitespace. */ 1216 for (; ecp->clen > 0; --ecp->clen) { 1217 ch = *ecp->cp++; 1218 if (!cmdskip(ch)) 1219 break; 1220 } 1221 1222 /* 1223 * There shouldn't be anything left, and no more required fields, 1224 * i.e neither 'l' or 'r' in the syntax string. 1225 */ 1226 if (ecp->clen != 0 || strpbrk(np, "lr")) { 1227 usage: msgq(sp, M_ERR, "086|Usage: %s", ecp->cmd->usage); 1228 goto err; 1229 } 1230 1231 /* 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. 1237 * 1238 * If it's a "default vi command", an address of zero is okay. 1239 */ 1240 addr_verify: 1241 switch (ecp->addrcnt) { 1242 case 2: 1243 /* 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. 1250 */ 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; 1257 } 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; 1266 } 1267 } 1268 /* FALLTHROUGH */ 1269 case 1: 1270 if (ecp->addr1.lno == 0) { 1271 if (!F_ISSET(ecp, E_ADDR_ZERO) && 1272 (F_ISSET(sp, SC_EX) || 1273 !F_ISSET(ecp, E_USELASTCMD))) { 1274 ex_badaddr(sp, ecp->cmd, A_ZERO, NUM_OK); 1275 goto err; 1276 } 1277 } else if (!db_exist(sp, ecp->addr1.lno)) { 1278 ex_badaddr(sp, NULL, A_EOF, NUM_OK); 1279 goto err; 1280 } 1281 break; 1282 } 1283 1284 /* 1285 * If doing a default command and there's nothing left on the line, 1286 * vi just moves to the line. For example, ":3" and ":'a,'b" just 1287 * move to line 3 and line 'b, respectively, but ":3|" prints line 3. 1288 * 1289 * !!! 1290 * In addition, IF THE LINE CHANGES, move to the first nonblank of 1291 * the line. 1292 * 1293 * !!! 1294 * This is done before the absolute mark gets set; historically, 1295 * "/a/,/b/" did NOT set vi's absolute mark, but "/a/,/b/d" did. 1296 */ 1297 if ((F_ISSET(sp, SC_VI) || F_ISSET(ecp, E_NOPRDEF)) && 1298 F_ISSET(ecp, E_USELASTCMD) && vi_address == 0) { 1299 switch (ecp->addrcnt) { 1300 case 2: 1301 if (sp->lno != 1302 (ecp->addr2.lno ? ecp->addr2.lno : 1)) { 1303 sp->lno = 1304 ecp->addr2.lno ? ecp->addr2.lno : 1; 1305 sp->cno = 0; 1306 (void)nonblank(sp, sp->lno, &sp->cno); 1307 } 1308 break; 1309 case 1: 1310 if (sp->lno != 1311 (ecp->addr1.lno ? ecp->addr1.lno : 1)) { 1312 sp->lno = 1313 ecp->addr1.lno ? ecp->addr1.lno : 1; 1314 sp->cno = 0; 1315 (void)nonblank(sp, sp->lno, &sp->cno); 1316 } 1317 break; 1318 } 1319 ecp->cp = ecp->save_cmd; 1320 ecp->clen = ecp->save_cmdlen; 1321 goto loop; 1322 } 1323 1324 /* 1325 * Set the absolute mark -- we have to set it for vi here, in case 1326 * it's a compound command, e.g. ":5p|6" should set the absolute 1327 * mark for vi. 1328 */ 1329 if (F_ISSET(ecp, E_ABSMARK)) { 1330 cur.lno = sp->lno; 1331 cur.cno = sp->cno; 1332 F_CLR(ecp, E_ABSMARK); 1333 if (mark_set(sp, ABSMARK1, &cur, 1)) 1334 goto err; 1335 } 1336 1337 #if defined(DEBUG) && defined(COMLOG) 1338 ex_comlog(sp, ecp); 1339 #endif 1340 /* Increment the command count if not called from vi. */ 1341 if (F_ISSET(sp, SC_EX)) 1342 ++sp->ccnt; 1343 1344 /* 1345 * If file state available, and not doing a global command, 1346 * log the start of an action. 1347 */ 1348 if (sp->ep != NULL && !F_ISSET(sp, SC_EX_GLOBAL)) 1349 (void)log_cursor(sp); 1350 1351 /* 1352 * !!! 1353 * There are two special commands for the purposes of this code: the 1354 * default command (<carriage-return>) or the scrolling commands (^D 1355 * and <EOF>) as the first non-<blank> characters in the line. 1356 * 1357 * If this is the first command in the command line, we received the 1358 * command from the ex command loop and we're talking to a tty, and 1359 * and there's nothing else on the command line, and it's one of the 1360 * special commands, we move back up to the previous line, and erase 1361 * the prompt character with the output. Since ex runs in canonical 1362 * mode, we don't have to do anything else, a <newline> has already 1363 * been echoed by the tty driver. It's OK if vi calls us -- we won't 1364 * be in ex mode so we'll do nothing. 1365 */ 1366 if (F_ISSET(ecp, E_NRSEP)) { 1367 if (sp->ep != NULL && 1368 F_ISSET(sp, SC_EX) && !F_ISSET(gp, G_SCRIPTED) && 1369 (F_ISSET(ecp, E_USELASTCMD) || ecp->cmd == &cmds[C_SCROLL])) 1370 gp->scr_ex_adjust(sp, EX_TERM_SCROLL); 1371 F_CLR(ecp, E_NRSEP); 1372 } 1373 1374 /* 1375 * Call the underlying function for the ex command. 1376 * 1377 * XXX 1378 * Interrupts behave like errors, for now. 1379 */ 1380 if (ecp->cmd->fn(sp, ecp) || INTERRUPTED(sp)) { 1381 if (F_ISSET(gp, G_SCRIPTED)) 1382 F_SET(sp, SC_EXIT_FORCE); 1383 goto err; 1384 } 1385 1386 #ifdef DEBUG 1387 /* Make sure no function left global temporary space locked. */ 1388 if (F_ISSET(gp, G_TMP_INUSE)) { 1389 F_CLR(gp, G_TMP_INUSE); 1390 msgq_wstr(sp, M_ERR, ecp->cmd->name, 1391 "087|%s: temporary buffer not released"); 1392 } 1393 #endif 1394 /* 1395 * Ex displayed the number of lines modified immediately after each 1396 * command, so the command "1,10d|1,10d" would display: 1397 * 1398 * 10 lines deleted 1399 * 10 lines deleted 1400 * <autoprint line> 1401 * 1402 * Executing ex commands from vi only reported the final modified 1403 * lines message -- that's wrong enough that we don't match it. 1404 */ 1405 if (F_ISSET(sp, SC_EX)) 1406 mod_rpt(sp); 1407 1408 /* 1409 * Integrate any offset parsed by the underlying command, and make 1410 * sure the referenced line exists. 1411 * 1412 * XXX 1413 * May not match historic practice (which I've never been able to 1414 * completely figure out.) For example, the '=' command from vi 1415 * mode often got the offset wrong, and complained it was too large, 1416 * but didn't seem to have a problem with the cursor. If anyone 1417 * complains, ask them how it's supposed to work, they might know. 1418 */ 1419 if (sp->ep != NULL && ecp->flagoff) { 1420 if (ecp->flagoff < 0) { 1421 if (sp->lno <= -ecp->flagoff) { 1422 msgq(sp, M_ERR, 1423 "088|Flag offset to before line 1"); 1424 goto err; 1425 } 1426 } else { 1427 if (!NPFITS(MAX_REC_NUMBER, sp->lno, ecp->flagoff)) { 1428 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER); 1429 goto err; 1430 } 1431 if (!db_exist(sp, sp->lno + ecp->flagoff)) { 1432 msgq(sp, M_ERR, 1433 "089|Flag offset past end-of-file"); 1434 goto err; 1435 } 1436 } 1437 sp->lno += ecp->flagoff; 1438 } 1439 1440 /* 1441 * If the command executed successfully, we may want to display a line 1442 * based on the autoprint option or an explicit print flag. (Make sure 1443 * that there's a line to display.) Also, the autoprint edit option is 1444 * turned off for the duration of global commands. 1445 */ 1446 if (F_ISSET(sp, SC_EX) && sp->ep != NULL && sp->lno != 0) { 1447 /* 1448 * The print commands have already handled the `print' flags. 1449 * If so, clear them. 1450 */ 1451 if (FL_ISSET(ecp->iflags, E_CLRFLAG)) 1452 FL_CLR(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT); 1453 1454 /* If hash set only because of the number option, discard it. */ 1455 if (F_ISSET(ecp, E_OPTNUM)) 1456 FL_CLR(ecp->iflags, E_C_HASH); 1457 1458 /* 1459 * If there was an explicit flag to display the new cursor line, 1460 * or autoprint is set and a change was made, display the line. 1461 * If any print flags were set use them, else default to print. 1462 */ 1463 LF_INIT(FL_ISSET(ecp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT)); 1464 if (!LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT | E_NOAUTO) && 1465 !F_ISSET(sp, SC_EX_GLOBAL) && 1466 O_ISSET(sp, O_AUTOPRINT) && F_ISSET(ecp, E_AUTOPRINT)) 1467 LF_INIT(E_C_PRINT); 1468 1469 if (LF_ISSET(E_C_HASH | E_C_LIST | E_C_PRINT)) { 1470 cur.lno = sp->lno; 1471 cur.cno = 0; 1472 (void)ex_print(sp, ecp, &cur, &cur, flags); 1473 } 1474 } 1475 1476 /* 1477 * If the command had an associated "+cmd", it has to be executed 1478 * before we finish executing any more of this ex command. For 1479 * example, consider a .exrc file that contains the following lines: 1480 * 1481 * :set all 1482 * :edit +25 file.c|s/abc/ABC/|1 1483 * :3,5 print 1484 * 1485 * This can happen more than once -- the historic vi simply hung or 1486 * dropped core, of course. Prepend the + command back into the 1487 * current command and continue. We may have to add an additional 1488 * <literal next> character. We know that it will fit because we 1489 * discarded at least one space and the + character. 1490 */ 1491 if (arg1_len != 0) { 1492 /* 1493 * If the last character of the + command was a <literal next> 1494 * character, it would be treated differently because of the 1495 * append. Quote it, if necessary. 1496 */ 1497 if (IS_ESCAPE(sp, ecp, arg1[arg1_len - 1])) { 1498 *--ecp->save_cmd = CH_LITERAL; 1499 ++ecp->save_cmdlen; 1500 } 1501 1502 ecp->save_cmd -= arg1_len; 1503 ecp->save_cmdlen += arg1_len; 1504 MEMMOVE(ecp->save_cmd, arg1, arg1_len); 1505 1506 /* 1507 * Any commands executed from a +cmd are executed starting at 1508 * the first column of the last line of the file -- NOT the 1509 * first nonblank.) The main file startup code doesn't know 1510 * that a +cmd was set, however, so it may have put us at the 1511 * top of the file. (Note, this is safe because we must have 1512 * switched files to get here.) 1513 */ 1514 F_SET(ecp, E_MOVETOEND); 1515 } 1516 1517 /* Update the current command. */ 1518 ecp->cp = ecp->save_cmd; 1519 ecp->clen = ecp->save_cmdlen; 1520 1521 /* 1522 * !!! 1523 * If we've changed screens or underlying files, any pending global or 1524 * v command, or @ buffer that has associated addresses, has to be 1525 * discarded. This is historic practice for globals, and necessary for 1526 * @ buffers that had associated addresses. 1527 * 1528 * Otherwise, if we've changed underlying files, it's not a problem, 1529 * we continue with the rest of the ex command(s), operating on the 1530 * new file. However, if we switch screens (either by exiting or by 1531 * an explicit command), we have no way of knowing where to put output 1532 * messages, and, since we don't control screens here, we could screw 1533 * up the upper layers, (e.g. we could exit/reenter a screen multiple 1534 * times). So, return and continue after we've got a new screen. 1535 */ 1536 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_FSWITCH | SC_SSWITCH)) { 1537 at_found = gv_found = 0; 1538 SLIST_FOREACH(ecp, sp->gp->ecq, q) 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"); 1548 } 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"); 1556 } 1557 break; 1558 default: 1559 abort(); 1560 } 1561 if (at_found || gv_found) 1562 goto discard; 1563 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE | SC_SSWITCH)) 1564 goto rsuccess; 1565 } 1566 1567 goto loop; 1568 /* NOTREACHED */ 1569 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. 1577 */ 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; 1588 } 1589 } 1590 if (ecp->save_cmdlen != 0 || SLIST_FIRST(gp->ecq) != &gp->excmd) { 1591 discard: msgq(sp, M_BERR, 1592 "092|Ex command failed: pending commands discarded"); 1593 ex_discard(sp); 1594 } 1595 if (v_event_flush(sp, CH_MAPPED)) 1596 msgq(sp, M_BERR, 1597 "093|Ex command failed: mapped keys discarded"); 1598 1599 rfail: tmp = 1; 1600 if (0) 1601 rsuccess: tmp = 0; 1602 1603 /* Turn off any file name error information. */ 1604 gp->if_name = NULL; 1605 1606 /* Turn off the global bit. */ 1607 F_CLR(sp, SC_EX_GLOBAL); 1608 1609 return (tmp); 1610 } 1611 1612 /* 1613 * ex_range -- 1614 * Get a line range for ex commands, or perform a vi ex address search. 1615 * 1616 * PUBLIC: int ex_range(SCR *, EXCMD *, int *); 1617 */ 1618 int 1619 ex_range(SCR *sp, EXCMD *ecp, int *errp) 1620 { 1621 enum { ADDR_FOUND, ADDR_NEED, ADDR_NONE } addr; 1622 GS *gp; 1623 EX_PRIVATE *exp; 1624 MARK m; 1625 int isaddr; 1626 1627 *errp = 0; 1628 1629 /* 1630 * Parse comma or semi-colon delimited line specs. 1631 * 1632 * Semi-colon delimiters update the current address to be the last 1633 * address. For example, the command 1634 * 1635 * :3;/pattern/ecp->cp 1636 * 1637 * will search for pattern from line 3. In addition, if ecp->cp 1638 * is not a valid command, the current line will be left at 3, not 1639 * at the original address. 1640 * 1641 * Extra addresses are discarded, starting with the first. 1642 * 1643 * !!! 1644 * If any addresses are missing, they default to the current line. 1645 * This was historically true for both leading and trailing comma 1646 * delimited addresses as well as for trailing semicolon delimited 1647 * addresses. For consistency, we make it true for leading semicolon 1648 * addresses as well. 1649 */ 1650 gp = sp->gp; 1651 exp = EXP(sp); 1652 for (addr = ADDR_NONE, ecp->addrcnt = 0; ecp->clen > 0;) 1653 switch (*ecp->cp) { 1654 case '%': /* Entire file. */ 1655 /* Vi ex address searches didn't permit % signs. */ 1656 if (F_ISSET(ecp, E_VISEARCH)) 1657 goto ret; 1658 1659 /* It's an error if the file is empty. */ 1660 if (sp->ep == NULL) { 1661 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK); 1662 *errp = 1; 1663 return (0); 1664 } 1665 /* 1666 * !!! 1667 * A percent character addresses all of the lines in 1668 * the file. Historically, it couldn't be followed by 1669 * any other address. We do it as a text substitution 1670 * for simplicity. POSIX 1003.2 is expected to follow 1671 * this practice. 1672 * 1673 * If it's an empty file, the first line is 0, not 1. 1674 */ 1675 if (addr == ADDR_FOUND) { 1676 ex_badaddr(sp, NULL, A_COMBO, NUM_OK); 1677 *errp = 1; 1678 return (0); 1679 } 1680 if (db_last(sp, &ecp->addr2.lno)) 1681 return (1); 1682 ecp->addr1.lno = ecp->addr2.lno == 0 ? 0 : 1; 1683 ecp->addr1.cno = ecp->addr2.cno = 0; 1684 ecp->addrcnt = 2; 1685 addr = ADDR_FOUND; 1686 ++ecp->cp; 1687 --ecp->clen; 1688 break; 1689 case ',': /* Comma delimiter. */ 1690 /* Vi ex address searches didn't permit commas. */ 1691 if (F_ISSET(ecp, E_VISEARCH)) 1692 goto ret; 1693 /* FALLTHROUGH */ 1694 case ';': /* Semi-colon delimiter. */ 1695 if (sp->ep == NULL) { 1696 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK); 1697 *errp = 1; 1698 return (0); 1699 } 1700 if (addr != ADDR_FOUND) 1701 switch (ecp->addrcnt) { 1702 case 0: 1703 ecp->addr1.lno = sp->lno; 1704 ecp->addr1.cno = sp->cno; 1705 ecp->addrcnt = 1; 1706 break; 1707 case 2: 1708 ecp->addr1 = ecp->addr2; 1709 /* FALLTHROUGH */ 1710 case 1: 1711 ecp->addr2.lno = sp->lno; 1712 ecp->addr2.cno = sp->cno; 1713 ecp->addrcnt = 2; 1714 break; 1715 } 1716 if (*ecp->cp == ';') 1717 switch (ecp->addrcnt) { 1718 case 0: 1719 abort(); 1720 /* NOTREACHED */ 1721 case 1: 1722 sp->lno = ecp->addr1.lno; 1723 sp->cno = ecp->addr1.cno; 1724 break; 1725 case 2: 1726 sp->lno = ecp->addr2.lno; 1727 sp->cno = ecp->addr2.cno; 1728 break; 1729 } 1730 addr = ADDR_NEED; 1731 /* FALLTHROUGH */ 1732 case ' ': /* Whitespace. */ 1733 case '\t': /* Whitespace. */ 1734 ++ecp->cp; 1735 --ecp->clen; 1736 break; 1737 default: 1738 /* Get a line specification. */ 1739 if (ex_line(sp, ecp, &m, &isaddr, errp)) 1740 return (1); 1741 if (*errp) 1742 return (0); 1743 if (!isaddr) 1744 goto ret; 1745 if (addr == ADDR_FOUND) { 1746 ex_badaddr(sp, NULL, A_COMBO, NUM_OK); 1747 *errp = 1; 1748 return (0); 1749 } 1750 switch (ecp->addrcnt) { 1751 case 0: 1752 ecp->addr1 = m; 1753 ecp->addrcnt = 1; 1754 break; 1755 case 1: 1756 ecp->addr2 = m; 1757 ecp->addrcnt = 2; 1758 break; 1759 case 2: 1760 ecp->addr1 = ecp->addr2; 1761 ecp->addr2 = m; 1762 break; 1763 } 1764 addr = ADDR_FOUND; 1765 break; 1766 } 1767 1768 /* 1769 * !!! 1770 * Vi ex address searches are indifferent to order or trailing 1771 * semi-colons. 1772 */ 1773 ret: if (F_ISSET(ecp, E_VISEARCH)) 1774 return (0); 1775 1776 if (addr == ADDR_NEED) 1777 switch (ecp->addrcnt) { 1778 case 0: 1779 ecp->addr1.lno = sp->lno; 1780 ecp->addr1.cno = sp->cno; 1781 ecp->addrcnt = 1; 1782 break; 1783 case 2: 1784 ecp->addr1 = ecp->addr2; 1785 /* FALLTHROUGH */ 1786 case 1: 1787 ecp->addr2.lno = sp->lno; 1788 ecp->addr2.cno = sp->cno; 1789 ecp->addrcnt = 2; 1790 break; 1791 } 1792 1793 if (ecp->addrcnt == 2 && ecp->addr2.lno < ecp->addr1.lno) { 1794 msgq(sp, M_ERR, 1795 "094|The second address is smaller than the first"); 1796 *errp = 1; 1797 } 1798 return (0); 1799 } 1800 1801 /* 1802 * ex_line -- 1803 * Get a single line address specifier. 1804 * 1805 * The way the "previous context" mark worked was that any "non-relative" 1806 * motion set it. While ex/vi wasn't totally consistent about this, ANY 1807 * numeric address, search pattern, '$', or mark reference in an address 1808 * was considered non-relative, and set the value. Which should explain 1809 * why we're hacking marks down here. The problem was that the mark was 1810 * only set if the command was called, i.e. we have to set a flag and test 1811 * it later. 1812 * 1813 * XXX 1814 * This is probably still not exactly historic practice, although I think 1815 * it's fairly close. 1816 */ 1817 static int 1818 ex_line(SCR *sp, EXCMD *ecp, MARK *mp, int *isaddrp, int *errp) 1819 { 1820 enum nresult nret; 1821 EX_PRIVATE *exp; 1822 GS *gp; 1823 long total, val; 1824 int isneg; 1825 int (*sf)(SCR *, MARK *, MARK *, CHAR_T *, size_t, CHAR_T **, u_int); 1826 CHAR_T *endp; 1827 1828 gp = sp->gp; 1829 exp = EXP(sp); 1830 1831 *isaddrp = *errp = 0; 1832 F_CLR(ecp, E_DELTA); 1833 1834 /* No addresses permitted until a file has been read in. */ 1835 if (sp->ep == NULL && STRCHR(L("$0123456789'\\/?.+-^"), *ecp->cp)) { 1836 ex_badaddr(sp, NULL, A_EMPTY, NUM_OK); 1837 *errp = 1; 1838 return (0); 1839 } 1840 1841 switch (*ecp->cp) { 1842 case '$': /* Last line in the file. */ 1843 *isaddrp = 1; 1844 F_SET(ecp, E_ABSMARK); 1845 1846 mp->cno = 0; 1847 if (db_last(sp, &mp->lno)) 1848 return (1); 1849 ++ecp->cp; 1850 --ecp->clen; 1851 break; /* Absolute line number. */ 1852 case '0': case '1': case '2': case '3': case '4': 1853 case '5': case '6': case '7': case '8': case '9': 1854 *isaddrp = 1; 1855 F_SET(ecp, E_ABSMARK); 1856 1857 if ((nret = nget_slong(&val, ecp->cp, &endp, 10)) != NUM_OK) { 1858 ex_badaddr(sp, NULL, A_NOTSET, nret); 1859 *errp = 1; 1860 return (0); 1861 } 1862 if (!NPFITS(MAX_REC_NUMBER, 0, val)) { 1863 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER); 1864 *errp = 1; 1865 return (0); 1866 } 1867 mp->lno = val; 1868 mp->cno = 0; 1869 ecp->clen -= (endp - ecp->cp); 1870 ecp->cp = endp; 1871 break; 1872 case '\'': /* Use a mark. */ 1873 *isaddrp = 1; 1874 F_SET(ecp, E_ABSMARK); 1875 1876 if (ecp->clen == 1) { 1877 msgq(sp, M_ERR, "095|No mark name supplied"); 1878 *errp = 1; 1879 return (0); 1880 } 1881 if (mark_get(sp, ecp->cp[1], mp, M_ERR)) { 1882 *errp = 1; 1883 return (0); 1884 } 1885 ecp->cp += 2; 1886 ecp->clen -= 2; 1887 break; 1888 case '\\': /* Search: forward/backward. */ 1889 /* 1890 * !!! 1891 * I can't find any difference between // and \/ or between 1892 * ?? and \?. Mark Horton doesn't remember there being any 1893 * difference. C'est la vie. 1894 */ 1895 if (ecp->clen < 2 || 1896 (ecp->cp[1] != '/' && ecp->cp[1] != '?')) { 1897 msgq(sp, M_ERR, "096|\\ not followed by / or ?"); 1898 *errp = 1; 1899 return (0); 1900 } 1901 ++ecp->cp; 1902 --ecp->clen; 1903 sf = ecp->cp[0] == '/' ? f_search : b_search; 1904 goto search; 1905 case '/': /* Search forward. */ 1906 sf = f_search; 1907 goto search; 1908 case '?': /* Search backward. */ 1909 sf = b_search; 1910 1911 search: mp->lno = sp->lno; 1912 mp->cno = sp->cno; 1913 if (sf(sp, mp, mp, ecp->cp, ecp->clen, &endp, 1914 SEARCH_MSG | SEARCH_PARSE | SEARCH_SET | 1915 (F_ISSET(ecp, E_SEARCH_WMSG) ? SEARCH_WMSG : 0))) { 1916 *errp = 1; 1917 return (0); 1918 } 1919 1920 /* Fix up the command pointers. */ 1921 ecp->clen -= (endp - ecp->cp); 1922 ecp->cp = endp; 1923 1924 *isaddrp = 1; 1925 F_SET(ecp, E_ABSMARK); 1926 break; 1927 case '.': /* Current position. */ 1928 *isaddrp = 1; 1929 mp->cno = sp->cno; 1930 1931 /* If an empty file, then '.' is 0, not 1. */ 1932 if (sp->lno == 1) { 1933 if (db_last(sp, &mp->lno)) 1934 return (1); 1935 if (mp->lno != 0) 1936 mp->lno = 1; 1937 } else 1938 mp->lno = sp->lno; 1939 1940 /* 1941 * !!! 1942 * Historically, .<number> was the same as .+<number>, i.e. 1943 * the '+' could be omitted. (This feature is found in ed 1944 * as well.) 1945 */ 1946 if (ecp->clen > 1 && ISDIGIT(ecp->cp[1])) 1947 *ecp->cp = '+'; 1948 else { 1949 ++ecp->cp; 1950 --ecp->clen; 1951 } 1952 break; 1953 } 1954 1955 /* Skip trailing <blank>s. */ 1956 for (; ecp->clen > 0 && 1957 cmdskip(ecp->cp[0]); ++ecp->cp, --ecp->clen); 1958 1959 /* 1960 * Evaluate any offset. If no address yet found, the offset 1961 * is relative to ".". 1962 */ 1963 total = 0; 1964 if (ecp->clen != 0 && (ISDIGIT(ecp->cp[0]) || 1965 ecp->cp[0] == '+' || ecp->cp[0] == '-' || 1966 ecp->cp[0] == '^')) { 1967 if (!*isaddrp) { 1968 *isaddrp = 1; 1969 mp->lno = sp->lno; 1970 mp->cno = sp->cno; 1971 } 1972 /* 1973 * Evaluate an offset, defined as: 1974 * 1975 * [+-^<blank>]*[<blank>]*[0-9]* 1976 * 1977 * The rough translation is any number of signs, optionally 1978 * followed by numbers, or a number by itself, all <blank> 1979 * separated. 1980 * 1981 * !!! 1982 * All address offsets were additive, e.g. "2 2 3p" was the 1983 * same as "7p", or, "/ZZZ/ 2" was the same as "/ZZZ/+2". 1984 * Note, however, "2 /ZZZ/" was an error. It was also legal 1985 * to insert signs without numbers, so "3 - 2" was legal, and 1986 * equal to 4. 1987 * 1988 * !!! 1989 * Offsets were historically permitted for any line address, 1990 * e.g. the command "1,2 copy 2 2 2 2" copied lines 1,2 after 1991 * line 8. 1992 * 1993 * !!! 1994 * Offsets were historically permitted for search commands, 1995 * and handled as addresses: "/pattern/2 2 2" was legal, and 1996 * referenced the 6th line after pattern. 1997 */ 1998 F_SET(ecp, E_DELTA); 1999 for (;;) { 2000 for (; ecp->clen > 0 && cmdskip(ecp->cp[0]); 2001 ++ecp->cp, --ecp->clen); 2002 if (ecp->clen == 0 || (!ISDIGIT(ecp->cp[0]) && 2003 ecp->cp[0] != '+' && ecp->cp[0] != '-' && 2004 ecp->cp[0] != '^')) 2005 break; 2006 if (!ISDIGIT(ecp->cp[0]) && 2007 !ISDIGIT(ecp->cp[1])) { 2008 total += ecp->cp[0] == '+' ? 1 : -1; 2009 --ecp->clen; 2010 ++ecp->cp; 2011 } else { 2012 if (ecp->cp[0] == '-' || 2013 ecp->cp[0] == '^') { 2014 ++ecp->cp; 2015 --ecp->clen; 2016 isneg = 1; 2017 } else 2018 isneg = 0; 2019 2020 /* Get a signed long, add it to the total. */ 2021 if ((nret = nget_slong(&val, 2022 ecp->cp, &endp, 10)) != NUM_OK || 2023 (nret = NADD_SLONG(sp, 2024 total, val)) != NUM_OK) { 2025 ex_badaddr(sp, NULL, A_NOTSET, nret); 2026 *errp = 1; 2027 return (0); 2028 } 2029 total += isneg ? -val : val; 2030 ecp->clen -= (endp - ecp->cp); 2031 ecp->cp = endp; 2032 } 2033 } 2034 } 2035 2036 /* 2037 * Any value less than 0 is an error. Make sure that the new value 2038 * will fit into a recno_t. 2039 */ 2040 if (*isaddrp && total != 0) { 2041 if (total < 0) { 2042 if (-total > mp->lno) { 2043 msgq(sp, M_ERR, 2044 "097|Reference to a line number less than 0"); 2045 *errp = 1; 2046 return (0); 2047 } 2048 } else 2049 if (!NPFITS(MAX_REC_NUMBER, mp->lno, total)) { 2050 ex_badaddr(sp, NULL, A_NOTSET, NUM_OVER); 2051 *errp = 1; 2052 return (0); 2053 } 2054 mp->lno += total; 2055 } 2056 return (0); 2057 } 2058 2059 2060 /* 2061 * ex_load -- 2062 * Load up the next command, which may be an @ buffer or global command. 2063 */ 2064 static int 2065 ex_load(SCR *sp) 2066 { 2067 GS *gp; 2068 EXCMD *ecp; 2069 RANGE *rp; 2070 2071 F_CLR(sp, SC_EX_GLOBAL); 2072 2073 /* 2074 * Lose any exhausted commands. We know that the first command 2075 * can't be an AGV command, which makes things a bit easier. 2076 */ 2077 for (gp = sp->gp;;) { 2078 ecp = SLIST_FIRST(gp->ecq); 2079 2080 /* Discard the allocated source name as requested. */ 2081 if (F_ISSET(ecp, E_NAMEDISCARD)) 2082 free(ecp->if_name); 2083 2084 /* 2085 * If we're back to the original structure, leave it around, 2086 * since we've returned to the beginning of the command stack. 2087 */ 2088 if (ecp == &gp->excmd) { 2089 ecp->if_name = NULL; 2090 return (0); 2091 } 2092 2093 /* 2094 * ecp->clen will be 0 for the first discarded command, but 2095 * may not be 0 for subsequent ones, e.g. if the original 2096 * command was ":g/xx/@a|s/b/c/", then when we discard the 2097 * command pushed on the stack by the @a, we have to resume 2098 * the global command which included the substitute command. 2099 */ 2100 if (ecp->clen != 0) 2101 return (0); 2102 2103 /* 2104 * If it's an @, global or v command, we may need to continue 2105 * the command on a different line. 2106 */ 2107 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) { 2108 /* Discard any exhausted ranges. */ 2109 while ((rp = TAILQ_FIRST(ecp->rq)) != NULL) 2110 if (rp->start > rp->stop) { 2111 TAILQ_REMOVE(ecp->rq, rp, q); 2112 free(rp); 2113 } else 2114 break; 2115 2116 /* If there's another range, continue with it. */ 2117 if (rp != NULL) 2118 break; 2119 2120 /* If it's a global/v command, fix up the last line. */ 2121 if (FL_ISSET(ecp->agv_flags, 2122 AGV_GLOBAL | AGV_V) && ecp->range_lno != OOBLNO) { 2123 if (db_exist(sp, ecp->range_lno)) 2124 sp->lno = ecp->range_lno; 2125 else { 2126 if (db_last(sp, &sp->lno)) 2127 return (1); 2128 if (sp->lno == 0) 2129 sp->lno = 1; 2130 } 2131 } 2132 free(ecp->o_cp); 2133 } 2134 2135 /* Discard the EXCMD. */ 2136 SLIST_REMOVE_HEAD(gp->ecq, q); 2137 free(ecp); 2138 } 2139 2140 /* 2141 * We only get here if it's an active @, global or v command. Set 2142 * the current line number, and get a new copy of the command for 2143 * the parser. Note, the original pointer almost certainly moved, 2144 * so we have play games. 2145 */ 2146 ecp->cp = ecp->o_cp; 2147 MEMCPY(ecp->cp, ecp->cp + ecp->o_clen, ecp->o_clen); 2148 ecp->clen = ecp->o_clen; 2149 ecp->range_lno = sp->lno = rp->start++; 2150 2151 if (FL_ISSET(ecp->agv_flags, AGV_GLOBAL | AGV_V)) 2152 F_SET(sp, SC_EX_GLOBAL); 2153 return (0); 2154 } 2155 2156 /* 2157 * ex_discard -- 2158 * Discard any pending ex commands. 2159 */ 2160 static int 2161 ex_discard(SCR *sp) 2162 { 2163 GS *gp; 2164 EXCMD *ecp; 2165 RANGE *rp; 2166 2167 /* 2168 * We know the first command can't be an AGV command, so we don't 2169 * process it specially. We do, however, nail the command itself. 2170 */ 2171 for (gp = sp->gp;;) { 2172 ecp = SLIST_FIRST(gp->ecq); 2173 if (F_ISSET(ecp, E_NAMEDISCARD)) 2174 free(ecp->if_name); 2175 /* Reset the last command without dropping it. */ 2176 if (ecp == &gp->excmd) 2177 break; 2178 if (FL_ISSET(ecp->agv_flags, AGV_ALL)) { 2179 while ((rp = TAILQ_FIRST(ecp->rq)) != NULL) { 2180 TAILQ_REMOVE(ecp->rq, rp, q); 2181 free(rp); 2182 } 2183 free(ecp->o_cp); 2184 } 2185 SLIST_REMOVE_HEAD(gp->ecq, q); 2186 free(ecp); 2187 } 2188 2189 ecp->if_name = NULL; 2190 ecp->clen = 0; 2191 return (0); 2192 } 2193 2194 /* 2195 * ex_unknown -- 2196 * Display an unknown command name. 2197 */ 2198 static void 2199 ex_unknown(SCR *sp, CHAR_T *cmd, size_t len) 2200 { 2201 size_t blen; 2202 CHAR_T *bp; 2203 2204 GET_SPACE_GOTOW(sp, bp, blen, len + 1); 2205 bp[len] = '\0'; 2206 MEMCPY(bp, cmd, len); 2207 msgq_wstr(sp, M_ERR, bp, "098|The %s command is unknown"); 2208 FREE_SPACEW(sp, bp, blen); 2209 2210 alloc_err: 2211 return; 2212 } 2213 2214 /* 2215 * ex_is_abbrev - 2216 * The vi text input routine needs to know if ex thinks this is an 2217 * [un]abbreviate command, so it can turn off abbreviations. See 2218 * the usual ranting in the vi/v_txt_ev.c:txt_abbrev() routine. 2219 * 2220 * PUBLIC: int ex_is_abbrev(CHAR_T *, size_t); 2221 */ 2222 int 2223 ex_is_abbrev(CHAR_T *name, size_t len) 2224 { 2225 EXCMDLIST const *cp; 2226 2227 return ((cp = ex_comm_search(name, len)) != NULL && 2228 (cp == &cmds[C_ABBR] || cp == &cmds[C_UNABBREVIATE])); 2229 } 2230 2231 /* 2232 * ex_is_unmap - 2233 * The vi text input routine needs to know if ex thinks this is an 2234 * unmap command, so it can turn off input mapping. See the usual 2235 * ranting in the vi/v_txt_ev.c:txt_unmap() routine. 2236 * 2237 * PUBLIC: int ex_is_unmap(CHAR_T *, size_t); 2238 */ 2239 int 2240 ex_is_unmap(CHAR_T *name, size_t len) 2241 { 2242 EXCMDLIST const *cp; 2243 2244 /* 2245 * The command the vi input routines are really interested in 2246 * is "unmap!", not just unmap. 2247 */ 2248 if (name[len - 1] != '!') 2249 return (0); 2250 --len; 2251 return ((cp = ex_comm_search(name, len)) != NULL && 2252 cp == &cmds[C_UNMAP]); 2253 } 2254 2255 /* 2256 * ex_comm_search -- 2257 * Search for a command name. 2258 */ 2259 static EXCMDLIST const * 2260 ex_comm_search(CHAR_T *name, size_t len) 2261 { 2262 EXCMDLIST const *cp; 2263 2264 for (cp = cmds; cp->name != NULL; ++cp) { 2265 if (cp->name[0] > name[0]) 2266 return (NULL); 2267 if (cp->name[0] != name[0]) 2268 continue; 2269 if (!MEMCMP(name, cp->name, len)) 2270 return (cp); 2271 } 2272 return (NULL); 2273 } 2274 2275 /* 2276 * ex_badaddr -- 2277 * Display a bad address message. 2278 * 2279 * PUBLIC: void ex_badaddr 2280 * PUBLIC: (SCR *, EXCMDLIST const *, enum badaddr, enum nresult); 2281 */ 2282 void 2283 ex_badaddr(SCR *sp, const EXCMDLIST *cp, enum badaddr ba, enum nresult nret) 2284 { 2285 recno_t lno; 2286 2287 switch (nret) { 2288 case NUM_OK: 2289 break; 2290 case NUM_ERR: 2291 msgq(sp, M_SYSERR, NULL); 2292 return; 2293 case NUM_OVER: 2294 msgq(sp, M_ERR, "099|Address value overflow"); 2295 return; 2296 case NUM_UNDER: 2297 msgq(sp, M_ERR, "100|Address value underflow"); 2298 return; 2299 } 2300 2301 /* 2302 * When encountering an address error, tell the user if there's no 2303 * underlying file, that's the real problem. 2304 */ 2305 if (sp->ep == NULL) { 2306 ex_wemsg(sp, cp ? cp->name : NULL, EXM_NOFILEYET); 2307 return; 2308 } 2309 2310 switch (ba) { 2311 case A_COMBO: 2312 msgq(sp, M_ERR, "101|Illegal address combination"); 2313 break; 2314 case A_EOF: 2315 if (db_last(sp, &lno)) 2316 return; 2317 if (lno != 0) { 2318 msgq(sp, M_ERR, 2319 "102|Illegal address: only %lu lines in the file", 2320 (u_long)lno); 2321 break; 2322 } 2323 /* FALLTHROUGH */ 2324 case A_EMPTY: 2325 msgq(sp, M_ERR, "103|Illegal address: the file is empty"); 2326 break; 2327 case A_NOTSET: 2328 abort(); 2329 /* NOTREACHED */ 2330 case A_ZERO: 2331 msgq_wstr(sp, M_ERR, cp->name, 2332 "104|The %s command doesn't permit an address of 0"); 2333 break; 2334 } 2335 return; 2336 } 2337 2338 #if defined(DEBUG) && defined(COMLOG) 2339 /* 2340 * ex_comlog -- 2341 * Log ex commands. 2342 */ 2343 static void 2344 ex_comlog(sp, ecp) 2345 SCR *sp; 2346 EXCMD *ecp; 2347 { 2348 TRACE(sp, "ecmd: "WS, ecp->cmd->name); 2349 if (ecp->addrcnt > 0) { 2350 TRACE(sp, " a1 %d", ecp->addr1.lno); 2351 if (ecp->addrcnt > 1) 2352 TRACE(sp, " a2: %d", ecp->addr2.lno); 2353 } 2354 if (ecp->lineno) 2355 TRACE(sp, " line %d", ecp->lineno); 2356 if (ecp->flags) 2357 TRACE(sp, " flags 0x%x", ecp->flags); 2358 if (FL_ISSET(ecp->iflags, E_C_BUFFER)) 2359 TRACE(sp, " buffer "WC, ecp->buffer); 2360 if (ecp->argc) { 2361 int cnt; 2362 for (cnt = 0; cnt < ecp->argc; ++cnt) 2363 TRACE(sp, " arg %d: {"WS"}", cnt, ecp->argv[cnt]->bp); 2364 } 2365 TRACE(sp, "\n"); 2366 } 2367 #endif 2368