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