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