1 /* main.c: This file contains the main control and user-interface routines 2 for the ed line editor. */ 3 /*- 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 1993 Andrew Moore, Talke Studio. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* 32 * CREDITS 33 * 34 * This program is based on the editor algorithm described in 35 * Brian W. Kernighan and P. J. Plauger's book "Software Tools 36 * in Pascal," Addison-Wesley, 1981. 37 * 38 * The buffering algorithm is attributed to Rodney Ruddock of 39 * the University of Guelph, Guelph, Ontario. 40 * 41 */ 42 43 #include <sys/types.h> 44 45 #include <sys/ioctl.h> 46 #include <sys/wait.h> 47 #include <ctype.h> 48 #include <locale.h> 49 #include <pwd.h> 50 #include <setjmp.h> 51 52 #include "ed.h" 53 54 55 #ifdef _POSIX_SOURCE 56 static sigjmp_buf env; 57 #else 58 static jmp_buf env; 59 #endif 60 61 /* static buffers */ 62 char stdinbuf[1]; /* stdin buffer */ 63 static char *shcmd; /* shell command buffer */ 64 static int shcmdsz; /* shell command buffer size */ 65 static int shcmdi; /* shell command buffer index */ 66 char *ibuf; /* ed command-line buffer */ 67 int ibufsz; /* ed command-line buffer size */ 68 char *ibufp; /* pointer to ed command-line buffer */ 69 70 /* global flags */ 71 static int garrulous = 0; /* if set, print all error messages */ 72 int isbinary; /* if set, buffer contains ASCII NULs */ 73 int isglobal; /* if set, doing a global command */ 74 int modified; /* if set, buffer modified since last write */ 75 int mutex = 0; /* if set, signals set "sigflags" */ 76 static int red = 0; /* if set, restrict shell/directory access */ 77 int scripted = 0; /* if set, suppress diagnostics */ 78 int sigflags = 0; /* if set, signals received while mutex set */ 79 static int sigactive = 0; /* if set, signal handlers are enabled */ 80 81 static char old_filename[PATH_MAX] = ""; /* default filename */ 82 long current_addr; /* current address in editor buffer */ 83 long addr_last; /* last address in editor buffer */ 84 int lineno; /* script line number */ 85 static const char *prompt; /* command-line prompt */ 86 static const char *dps = "*"; /* default command-line prompt */ 87 88 static const char *usage = "usage: %s [-] [-sx] [-p string] [file]\n"; 89 90 /* ed: line editor */ 91 int 92 main(volatile int argc, char ** volatile argv) 93 { 94 int c, n; 95 long status = 0; 96 97 (void)setlocale(LC_ALL, ""); 98 99 red = (n = strlen(argv[0])) > 2 && argv[0][n - 3] == 'r'; 100 top: 101 while ((c = getopt(argc, argv, "p:sx")) != -1) 102 switch(c) { 103 case 'p': /* set prompt */ 104 prompt = optarg; 105 break; 106 case 's': /* run script */ 107 scripted = 1; 108 break; 109 case 'x': /* use crypt */ 110 fprintf(stderr, "crypt unavailable\n?\n"); 111 break; 112 113 default: 114 fprintf(stderr, usage, red ? "red" : "ed"); 115 exit(1); 116 } 117 argv += optind; 118 argc -= optind; 119 if (argc && **argv == '-') { 120 scripted = 1; 121 if (argc > 1) { 122 optind = 1; 123 goto top; 124 } 125 argv++; 126 argc--; 127 } 128 /* assert: reliable signals! */ 129 #ifdef SIGWINCH 130 handle_winch(SIGWINCH); 131 if (isatty(0)) signal(SIGWINCH, handle_winch); 132 #endif 133 signal(SIGHUP, signal_hup); 134 signal(SIGQUIT, SIG_IGN); 135 signal(SIGINT, signal_int); 136 #ifdef _POSIX_SOURCE 137 if ((status = sigsetjmp(env, 1))) 138 #else 139 if ((status = setjmp(env))) 140 #endif 141 { 142 fputs("\n?\n", stderr); 143 errmsg = "interrupt"; 144 } else { 145 init_buffers(); 146 sigactive = 1; /* enable signal handlers */ 147 if (argc && **argv && is_legal_filename(*argv)) { 148 if (read_file(*argv, 0) < 0 && !isatty(0)) 149 quit(2); 150 else if (**argv != '!') 151 if (strlcpy(old_filename, *argv, sizeof(old_filename)) 152 >= sizeof(old_filename)) 153 quit(2); 154 } else if (argc) { 155 fputs("?\n", stderr); 156 if (**argv == '\0') 157 errmsg = "invalid filename"; 158 if (!isatty(0)) 159 quit(2); 160 } 161 } 162 for (;;) { 163 if (status < 0 && garrulous) 164 fprintf(stderr, "%s\n", errmsg); 165 if (prompt) { 166 printf("%s", prompt); 167 fflush(stdout); 168 } 169 if ((n = get_tty_line()) < 0) { 170 status = ERR; 171 continue; 172 } else if (n == 0) { 173 if (modified && !scripted) { 174 fputs("?\n", stderr); 175 errmsg = "warning: file modified"; 176 if (!isatty(0)) { 177 if (garrulous) 178 fprintf(stderr, 179 "script, line %d: %s\n", 180 lineno, errmsg); 181 quit(2); 182 } 183 clearerr(stdin); 184 modified = 0; 185 status = EMOD; 186 continue; 187 } else 188 quit(0); 189 } else if (ibuf[n - 1] != '\n') { 190 /* discard line */ 191 errmsg = "unexpected end-of-file"; 192 clearerr(stdin); 193 status = ERR; 194 continue; 195 } 196 isglobal = 0; 197 if ((status = extract_addr_range()) >= 0 && 198 (status = exec_command()) >= 0) 199 if (!status || 200 (status = display_lines(current_addr, current_addr, 201 status)) >= 0) 202 continue; 203 switch (status) { 204 case EOF: 205 quit(0); 206 case EMOD: 207 modified = 0; 208 fputs("?\n", stderr); /* give warning */ 209 errmsg = "warning: file modified"; 210 if (!isatty(0)) { 211 if (garrulous) 212 fprintf(stderr, "script, line %d: %s\n", 213 lineno, errmsg); 214 quit(2); 215 } 216 break; 217 case FATAL: 218 if (!isatty(0)) { 219 if (garrulous) 220 fprintf(stderr, "script, line %d: %s\n", 221 lineno, errmsg); 222 } else if (garrulous) 223 fprintf(stderr, "%s\n", errmsg); 224 quit(3); 225 default: 226 fputs("?\n", stderr); 227 if (!isatty(0)) { 228 if (garrulous) 229 fprintf(stderr, "script, line %d: %s\n", 230 lineno, errmsg); 231 quit(2); 232 } 233 break; 234 } 235 } 236 /*NOTREACHED*/ 237 } 238 239 long first_addr, second_addr; 240 static long addr_cnt; 241 242 /* extract_addr_range: get line addresses from the command buffer until an 243 illegal address is seen; return status */ 244 int 245 extract_addr_range(void) 246 { 247 long addr; 248 249 addr_cnt = 0; 250 first_addr = second_addr = current_addr; 251 while ((addr = next_addr()) >= 0) { 252 addr_cnt++; 253 first_addr = second_addr; 254 second_addr = addr; 255 if (*ibufp != ',' && *ibufp != ';') 256 break; 257 else if (*ibufp++ == ';') 258 current_addr = addr; 259 } 260 if ((addr_cnt = min(addr_cnt, 2)) == 1 || second_addr != addr) 261 first_addr = second_addr; 262 return (addr == ERR) ? ERR : 0; 263 } 264 265 266 #define SKIP_BLANKS() while (isspace((unsigned char)*ibufp) && *ibufp != '\n') ibufp++ 267 268 #define MUST_BE_FIRST() do { \ 269 if (!first) { \ 270 errmsg = "invalid address"; \ 271 return ERR; \ 272 } \ 273 } while (0) 274 275 /* next_addr: return the next line address in the command buffer */ 276 long 277 next_addr(void) 278 { 279 const char *hd; 280 long addr = current_addr; 281 long n; 282 int first = 1; 283 int c; 284 285 SKIP_BLANKS(); 286 for (hd = ibufp;; first = 0) 287 switch (c = *ibufp) { 288 case '+': 289 case '\t': 290 case ' ': 291 case '-': 292 case '^': 293 ibufp++; 294 SKIP_BLANKS(); 295 if (isdigit((unsigned char)*ibufp)) { 296 STRTOL(n, ibufp); 297 addr += (c == '-' || c == '^') ? -n : n; 298 } else if (!isspace((unsigned char)c)) 299 addr += (c == '-' || c == '^') ? -1 : 1; 300 break; 301 case '0': case '1': case '2': 302 case '3': case '4': case '5': 303 case '6': case '7': case '8': case '9': 304 MUST_BE_FIRST(); 305 STRTOL(addr, ibufp); 306 break; 307 case '.': 308 case '$': 309 MUST_BE_FIRST(); 310 ibufp++; 311 addr = (c == '.') ? current_addr : addr_last; 312 break; 313 case '/': 314 case '?': 315 MUST_BE_FIRST(); 316 if ((addr = get_matching_node_addr( 317 get_compiled_pattern(), c == '/')) < 0) 318 return ERR; 319 else if (c == *ibufp) 320 ibufp++; 321 break; 322 case '\'': 323 MUST_BE_FIRST(); 324 ibufp++; 325 if ((addr = get_marked_node_addr(*ibufp++)) < 0) 326 return ERR; 327 break; 328 case '%': 329 case ',': 330 case ';': 331 if (first) { 332 ibufp++; 333 addr_cnt++; 334 second_addr = (c == ';') ? current_addr : 1; 335 if ((addr = next_addr()) < 0) 336 addr = addr_last; 337 break; 338 } 339 /* FALLTHROUGH */ 340 default: 341 if (ibufp == hd) 342 return EOF; 343 else if (addr < 0 || addr_last < addr) { 344 errmsg = "invalid address"; 345 return ERR; 346 } else 347 return addr; 348 } 349 /* NOTREACHED */ 350 } 351 352 353 #ifdef BACKWARDS 354 /* GET_THIRD_ADDR: get a legal address from the command buffer */ 355 #define GET_THIRD_ADDR(addr) \ 356 { \ 357 long ol1, ol2; \ 358 \ 359 ol1 = first_addr, ol2 = second_addr; \ 360 if (extract_addr_range() < 0) \ 361 return ERR; \ 362 else if (addr_cnt == 0) { \ 363 errmsg = "destination expected"; \ 364 return ERR; \ 365 } else if (second_addr < 0 || addr_last < second_addr) { \ 366 errmsg = "invalid address"; \ 367 return ERR; \ 368 } \ 369 addr = second_addr; \ 370 first_addr = ol1, second_addr = ol2; \ 371 } 372 #else /* BACKWARDS */ 373 /* GET_THIRD_ADDR: get a legal address from the command buffer */ 374 #define GET_THIRD_ADDR(addr) \ 375 { \ 376 long ol1, ol2; \ 377 \ 378 ol1 = first_addr, ol2 = second_addr; \ 379 if (extract_addr_range() < 0) \ 380 return ERR; \ 381 if (second_addr < 0 || addr_last < second_addr) { \ 382 errmsg = "invalid address"; \ 383 return ERR; \ 384 } \ 385 addr = second_addr; \ 386 first_addr = ol1, second_addr = ol2; \ 387 } 388 #endif 389 390 391 /* GET_COMMAND_SUFFIX: verify the command suffix in the command buffer */ 392 #define GET_COMMAND_SUFFIX() { \ 393 int done = 0; \ 394 do { \ 395 switch(*ibufp) { \ 396 case 'p': \ 397 gflag |= GPR, ibufp++; \ 398 break; \ 399 case 'l': \ 400 gflag |= GLS, ibufp++; \ 401 break; \ 402 case 'n': \ 403 gflag |= GNP, ibufp++; \ 404 break; \ 405 default: \ 406 done++; \ 407 } \ 408 } while (!done); \ 409 if (*ibufp++ != '\n') { \ 410 errmsg = "invalid command suffix"; \ 411 return ERR; \ 412 } \ 413 } 414 415 416 /* sflags */ 417 #define SGG 001 /* complement previous global substitute suffix */ 418 #define SGP 002 /* complement previous print suffix */ 419 #define SGR 004 /* use last regex instead of last pat */ 420 #define SGF 010 /* repeat last substitution */ 421 422 int patlock = 0; /* if set, pattern not freed by get_compiled_pattern() */ 423 424 long rows = 22; /* scroll length: ws_row - 2 */ 425 426 /* exec_command: execute the next command in command buffer; return print 427 request, if any */ 428 int 429 exec_command(void) 430 { 431 static pattern_t *pat = NULL; 432 static int sgflag = 0; 433 static long sgnum = 0; 434 435 pattern_t *tpat; 436 char *fnp; 437 int gflag = 0; 438 int sflags = 0; 439 long addr = 0; 440 int n = 0; 441 int c; 442 443 SKIP_BLANKS(); 444 switch(c = *ibufp++) { 445 case 'a': 446 GET_COMMAND_SUFFIX(); 447 if (!isglobal) clear_undo_stack(); 448 if (append_lines(second_addr) < 0) 449 return ERR; 450 break; 451 case 'c': 452 if (check_addr_range(current_addr, current_addr) < 0) 453 return ERR; 454 GET_COMMAND_SUFFIX(); 455 if (!isglobal) clear_undo_stack(); 456 if (delete_lines(first_addr, second_addr) < 0 || 457 append_lines(current_addr) < 0) 458 return ERR; 459 break; 460 case 'd': 461 if (check_addr_range(current_addr, current_addr) < 0) 462 return ERR; 463 GET_COMMAND_SUFFIX(); 464 if (!isglobal) clear_undo_stack(); 465 if (delete_lines(first_addr, second_addr) < 0) 466 return ERR; 467 else if ((addr = INC_MOD(current_addr, addr_last)) != 0) 468 current_addr = addr; 469 break; 470 case 'e': 471 if (modified && !scripted) 472 return EMOD; 473 /* FALLTHROUGH */ 474 case 'E': 475 if (addr_cnt > 0) { 476 errmsg = "unexpected address"; 477 return ERR; 478 } else if (!isspace((unsigned char)*ibufp)) { 479 errmsg = "unexpected command suffix"; 480 return ERR; 481 } else if ((fnp = get_filename()) == NULL) 482 return ERR; 483 GET_COMMAND_SUFFIX(); 484 if (delete_lines(1, addr_last) < 0) 485 return ERR; 486 clear_undo_stack(); 487 if (close_sbuf() < 0) 488 return ERR; 489 else if (open_sbuf() < 0) 490 return FATAL; 491 if (*fnp && *fnp != '!') 492 strlcpy(old_filename, fnp, PATH_MAX); 493 #ifdef BACKWARDS 494 if (*fnp == '\0' && *old_filename == '\0') { 495 errmsg = "no current filename"; 496 return ERR; 497 } 498 #endif 499 if (read_file(*fnp ? fnp : old_filename, 0) < 0) 500 return ERR; 501 clear_undo_stack(); 502 modified = 0; 503 u_current_addr = u_addr_last = -1; 504 break; 505 case 'f': 506 if (addr_cnt > 0) { 507 errmsg = "unexpected address"; 508 return ERR; 509 } else if (!isspace((unsigned char)*ibufp)) { 510 errmsg = "unexpected command suffix"; 511 return ERR; 512 } else if ((fnp = get_filename()) == NULL) 513 return ERR; 514 else if (*fnp == '!') { 515 errmsg = "invalid redirection"; 516 return ERR; 517 } 518 GET_COMMAND_SUFFIX(); 519 if (*fnp) 520 strlcpy(old_filename, fnp, PATH_MAX); 521 printf("%s\n", strip_escapes(old_filename)); 522 break; 523 case 'g': 524 case 'v': 525 case 'G': 526 case 'V': 527 if (isglobal) { 528 errmsg = "cannot nest global commands"; 529 return ERR; 530 } else if (check_addr_range(1, addr_last) < 0) 531 return ERR; 532 else if (build_active_list(c == 'g' || c == 'G') < 0) 533 return ERR; 534 else if ((n = (c == 'G' || c == 'V'))) 535 GET_COMMAND_SUFFIX(); 536 isglobal++; 537 if (exec_global(n, gflag) < 0) 538 return ERR; 539 break; 540 case 'h': 541 if (addr_cnt > 0) { 542 errmsg = "unexpected address"; 543 return ERR; 544 } 545 GET_COMMAND_SUFFIX(); 546 if (*errmsg) fprintf(stderr, "%s\n", errmsg); 547 break; 548 case 'H': 549 if (addr_cnt > 0) { 550 errmsg = "unexpected address"; 551 return ERR; 552 } 553 GET_COMMAND_SUFFIX(); 554 if ((garrulous = 1 - garrulous) && *errmsg) 555 fprintf(stderr, "%s\n", errmsg); 556 break; 557 case 'i': 558 if (second_addr == 0) { 559 errmsg = "invalid address"; 560 return ERR; 561 } 562 GET_COMMAND_SUFFIX(); 563 if (!isglobal) clear_undo_stack(); 564 if (append_lines(second_addr - 1) < 0) 565 return ERR; 566 break; 567 case 'j': 568 if (check_addr_range(current_addr, current_addr + 1) < 0) 569 return ERR; 570 GET_COMMAND_SUFFIX(); 571 if (!isglobal) clear_undo_stack(); 572 if (first_addr != second_addr && 573 join_lines(first_addr, second_addr) < 0) 574 return ERR; 575 break; 576 case 'k': 577 c = *ibufp++; 578 if (second_addr == 0) { 579 errmsg = "invalid address"; 580 return ERR; 581 } 582 GET_COMMAND_SUFFIX(); 583 if (mark_line_node(get_addressed_line_node(second_addr), c) < 0) 584 return ERR; 585 break; 586 case 'l': 587 if (check_addr_range(current_addr, current_addr) < 0) 588 return ERR; 589 GET_COMMAND_SUFFIX(); 590 if (display_lines(first_addr, second_addr, gflag | GLS) < 0) 591 return ERR; 592 gflag = 0; 593 break; 594 case 'm': 595 if (check_addr_range(current_addr, current_addr) < 0) 596 return ERR; 597 GET_THIRD_ADDR(addr); 598 if (first_addr <= addr && addr < second_addr) { 599 errmsg = "invalid destination"; 600 return ERR; 601 } 602 GET_COMMAND_SUFFIX(); 603 if (!isglobal) clear_undo_stack(); 604 if (move_lines(addr) < 0) 605 return ERR; 606 break; 607 case 'n': 608 if (check_addr_range(current_addr, current_addr) < 0) 609 return ERR; 610 GET_COMMAND_SUFFIX(); 611 if (display_lines(first_addr, second_addr, gflag | GNP) < 0) 612 return ERR; 613 gflag = 0; 614 break; 615 case 'p': 616 if (check_addr_range(current_addr, current_addr) < 0) 617 return ERR; 618 GET_COMMAND_SUFFIX(); 619 if (display_lines(first_addr, second_addr, gflag | GPR) < 0) 620 return ERR; 621 gflag = 0; 622 break; 623 case 'P': 624 if (addr_cnt > 0) { 625 errmsg = "unexpected address"; 626 return ERR; 627 } 628 GET_COMMAND_SUFFIX(); 629 prompt = prompt ? NULL : optarg ? optarg : dps; 630 break; 631 case 'q': 632 case 'Q': 633 if (addr_cnt > 0) { 634 errmsg = "unexpected address"; 635 return ERR; 636 } 637 GET_COMMAND_SUFFIX(); 638 gflag = (modified && !scripted && c == 'q') ? EMOD : EOF; 639 break; 640 case 'r': 641 if (!isspace((unsigned char)*ibufp)) { 642 errmsg = "unexpected command suffix"; 643 return ERR; 644 } else if (addr_cnt == 0) 645 second_addr = addr_last; 646 if ((fnp = get_filename()) == NULL) 647 return ERR; 648 GET_COMMAND_SUFFIX(); 649 if (!isglobal) clear_undo_stack(); 650 if (*old_filename == '\0' && *fnp != '!') 651 strlcpy(old_filename, fnp, PATH_MAX); 652 #ifdef BACKWARDS 653 if (*fnp == '\0' && *old_filename == '\0') { 654 errmsg = "no current filename"; 655 return ERR; 656 } 657 #endif 658 if ((addr = read_file(*fnp ? fnp : old_filename, second_addr)) < 0) 659 return ERR; 660 else if (addr && addr != addr_last) 661 modified = 1; 662 break; 663 case 's': 664 do { 665 switch(*ibufp) { 666 case '\n': 667 sflags |=SGF; 668 break; 669 case 'g': 670 sflags |= SGG; 671 ibufp++; 672 break; 673 case 'p': 674 sflags |= SGP; 675 ibufp++; 676 break; 677 case 'r': 678 sflags |= SGR; 679 ibufp++; 680 break; 681 case '0': case '1': case '2': case '3': case '4': 682 case '5': case '6': case '7': case '8': case '9': 683 STRTOL(sgnum, ibufp); 684 sflags |= SGF; 685 sgflag &= ~GSG; /* override GSG */ 686 break; 687 default: 688 if (sflags) { 689 errmsg = "invalid command suffix"; 690 return ERR; 691 } 692 } 693 } while (sflags && *ibufp != '\n'); 694 if (sflags && !pat) { 695 errmsg = "no previous substitution"; 696 return ERR; 697 } else if (sflags & SGG) 698 sgnum = 0; /* override numeric arg */ 699 if (*ibufp != '\n' && *(ibufp + 1) == '\n') { 700 errmsg = "invalid pattern delimiter"; 701 return ERR; 702 } 703 tpat = pat; 704 SPL1(); 705 if ((!sflags || (sflags & SGR)) && 706 (tpat = get_compiled_pattern()) == NULL) { 707 SPL0(); 708 return ERR; 709 } else if (tpat != pat) { 710 if (pat) { 711 regfree(pat); 712 free(pat); 713 } 714 pat = tpat; 715 patlock = 1; /* reserve pattern */ 716 } 717 SPL0(); 718 if (!sflags && extract_subst_tail(&sgflag, &sgnum) < 0) 719 return ERR; 720 else if (isglobal) 721 sgflag |= GLB; 722 else 723 sgflag &= ~GLB; 724 if (sflags & SGG) 725 sgflag ^= GSG; 726 if (sflags & SGP) 727 sgflag ^= GPR, sgflag &= ~(GLS | GNP); 728 do { 729 switch(*ibufp) { 730 case 'p': 731 sgflag |= GPR, ibufp++; 732 break; 733 case 'l': 734 sgflag |= GLS, ibufp++; 735 break; 736 case 'n': 737 sgflag |= GNP, ibufp++; 738 break; 739 default: 740 n++; 741 } 742 } while (!n); 743 if (check_addr_range(current_addr, current_addr) < 0) 744 return ERR; 745 GET_COMMAND_SUFFIX(); 746 if (!isglobal) clear_undo_stack(); 747 if (search_and_replace(pat, sgflag, sgnum) < 0) 748 return ERR; 749 break; 750 case 't': 751 if (check_addr_range(current_addr, current_addr) < 0) 752 return ERR; 753 GET_THIRD_ADDR(addr); 754 GET_COMMAND_SUFFIX(); 755 if (!isglobal) clear_undo_stack(); 756 if (copy_lines(addr) < 0) 757 return ERR; 758 break; 759 case 'u': 760 if (addr_cnt > 0) { 761 errmsg = "unexpected address"; 762 return ERR; 763 } 764 GET_COMMAND_SUFFIX(); 765 if (pop_undo_stack() < 0) 766 return ERR; 767 break; 768 case 'w': 769 case 'W': 770 if ((n = *ibufp) == 'q' || n == 'Q') { 771 gflag = EOF; 772 ibufp++; 773 } 774 if (!isspace((unsigned char)*ibufp)) { 775 errmsg = "unexpected command suffix"; 776 return ERR; 777 } else if ((fnp = get_filename()) == NULL) 778 return ERR; 779 if (addr_cnt == 0 && !addr_last) 780 first_addr = second_addr = 0; 781 else if (check_addr_range(1, addr_last) < 0) 782 return ERR; 783 GET_COMMAND_SUFFIX(); 784 if (*old_filename == '\0' && *fnp != '!') 785 strlcpy(old_filename, fnp, PATH_MAX); 786 #ifdef BACKWARDS 787 if (*fnp == '\0' && *old_filename == '\0') { 788 errmsg = "no current filename"; 789 return ERR; 790 } 791 #endif 792 if ((addr = write_file(*fnp ? fnp : old_filename, 793 (c == 'W') ? "a" : "w", first_addr, second_addr)) < 0) 794 return ERR; 795 else if (addr == addr_last && *fnp != '!') 796 modified = 0; 797 else if (modified && !scripted && n == 'q') 798 gflag = EMOD; 799 break; 800 case 'x': 801 if (addr_cnt > 0) { 802 errmsg = "unexpected address"; 803 return ERR; 804 } 805 GET_COMMAND_SUFFIX(); 806 errmsg = "crypt unavailable"; 807 return ERR; 808 case 'z': 809 #ifdef BACKWARDS 810 if (check_addr_range(first_addr = 1, current_addr + 1) < 0) 811 #else 812 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0) 813 #endif 814 return ERR; 815 else if ('0' < *ibufp && *ibufp <= '9') 816 STRTOL(rows, ibufp); 817 GET_COMMAND_SUFFIX(); 818 if (display_lines(second_addr, min(addr_last, 819 second_addr + rows), gflag) < 0) 820 return ERR; 821 gflag = 0; 822 break; 823 case '=': 824 GET_COMMAND_SUFFIX(); 825 printf("%ld\n", addr_cnt ? second_addr : addr_last); 826 break; 827 case '!': 828 if (addr_cnt > 0) { 829 errmsg = "unexpected address"; 830 return ERR; 831 } else if ((sflags = get_shell_command()) < 0) 832 return ERR; 833 GET_COMMAND_SUFFIX(); 834 if (sflags) printf("%s\n", shcmd + 1); 835 system(shcmd + 1); 836 if (!scripted) printf("!\n"); 837 break; 838 case '\n': 839 #ifdef BACKWARDS 840 if (check_addr_range(first_addr = 1, current_addr + 1) < 0 841 #else 842 if (check_addr_range(first_addr = 1, current_addr + !isglobal) < 0 843 #endif 844 || display_lines(second_addr, second_addr, 0) < 0) 845 return ERR; 846 break; 847 default: 848 errmsg = "unknown command"; 849 return ERR; 850 } 851 return gflag; 852 } 853 854 855 /* check_addr_range: return status of address range check */ 856 int 857 check_addr_range(long n, long m) 858 { 859 if (addr_cnt == 0) { 860 first_addr = n; 861 second_addr = m; 862 } 863 if (first_addr > second_addr || 1 > first_addr || 864 second_addr > addr_last) { 865 errmsg = "invalid address"; 866 return ERR; 867 } 868 return 0; 869 } 870 871 872 /* get_matching_node_addr: return the address of the next line matching a 873 pattern in a given direction. wrap around begin/end of editor buffer if 874 necessary */ 875 long 876 get_matching_node_addr(pattern_t *pat, int dir) 877 { 878 char *s; 879 long n = current_addr; 880 line_t *lp; 881 882 if (!pat) return ERR; 883 do { 884 if ((n = dir ? INC_MOD(n, addr_last) : DEC_MOD(n, addr_last))) { 885 lp = get_addressed_line_node(n); 886 if ((s = get_sbuf_line(lp)) == NULL) 887 return ERR; 888 if (isbinary) 889 NUL_TO_NEWLINE(s, lp->len); 890 if (!regexec(pat, s, 0, NULL, 0)) 891 return n; 892 } 893 } while (n != current_addr); 894 errmsg = "no match"; 895 return ERR; 896 } 897 898 899 /* get_filename: return pointer to copy of filename in the command buffer */ 900 char * 901 get_filename(void) 902 { 903 static char *file = NULL; 904 static int filesz = 0; 905 906 int n; 907 908 if (*ibufp != '\n') { 909 SKIP_BLANKS(); 910 if (*ibufp == '\n') { 911 errmsg = "invalid filename"; 912 return NULL; 913 } else if ((ibufp = get_extended_line(&n, 1)) == NULL) 914 return NULL; 915 else if (*ibufp == '!') { 916 ibufp++; 917 if ((n = get_shell_command()) < 0) 918 return NULL; 919 if (n) 920 printf("%s\n", shcmd + 1); 921 return shcmd; 922 } else if (n > PATH_MAX - 1) { 923 errmsg = "filename too long"; 924 return NULL; 925 } 926 } 927 #ifndef BACKWARDS 928 else if (*old_filename == '\0') { 929 errmsg = "no current filename"; 930 return NULL; 931 } 932 #endif 933 REALLOC(file, filesz, PATH_MAX, NULL); 934 for (n = 0; *ibufp != '\n';) 935 file[n++] = *ibufp++; 936 file[n] = '\0'; 937 return is_legal_filename(file) ? file : NULL; 938 } 939 940 941 /* get_shell_command: read a shell command from stdin; return substitution 942 status */ 943 int 944 get_shell_command(void) 945 { 946 static char *buf = NULL; 947 static int n = 0; 948 949 char *s; /* substitution char pointer */ 950 int i = 0; 951 int j = 0; 952 953 if (red) { 954 errmsg = "shell access restricted"; 955 return ERR; 956 } else if ((s = ibufp = get_extended_line(&j, 1)) == NULL) 957 return ERR; 958 REALLOC(buf, n, j + 1, ERR); 959 buf[i++] = '!'; /* prefix command w/ bang */ 960 while (*ibufp != '\n') 961 switch (*ibufp) { 962 default: 963 REALLOC(buf, n, i + 2, ERR); 964 buf[i++] = *ibufp; 965 if (*ibufp++ == '\\') 966 buf[i++] = *ibufp++; 967 break; 968 case '!': 969 if (s != ibufp) { 970 REALLOC(buf, n, i + 1, ERR); 971 buf[i++] = *ibufp++; 972 } 973 #ifdef BACKWARDS 974 else if (shcmd == NULL || *(shcmd + 1) == '\0') 975 #else 976 else if (shcmd == NULL) 977 #endif 978 { 979 errmsg = "no previous command"; 980 return ERR; 981 } else { 982 REALLOC(buf, n, i + shcmdi, ERR); 983 for (s = shcmd + 1; s < shcmd + shcmdi;) 984 buf[i++] = *s++; 985 s = ibufp++; 986 } 987 break; 988 case '%': 989 if (*old_filename == '\0') { 990 errmsg = "no current filename"; 991 return ERR; 992 } 993 j = strlen(s = strip_escapes(old_filename)); 994 REALLOC(buf, n, i + j, ERR); 995 while (j--) 996 buf[i++] = *s++; 997 s = ibufp++; 998 break; 999 } 1000 REALLOC(shcmd, shcmdsz, i + 1, ERR); 1001 memcpy(shcmd, buf, i); 1002 shcmd[shcmdi = i] = '\0'; 1003 return *s == '!' || *s == '%'; 1004 } 1005 1006 1007 /* append_lines: insert text from stdin to after line n; stop when either a 1008 single period is read or EOF; return status */ 1009 int 1010 append_lines(long n) 1011 { 1012 int l; 1013 const char *lp = ibuf; 1014 const char *eot; 1015 undo_t *up = NULL; 1016 1017 for (current_addr = n;;) { 1018 if (!isglobal) { 1019 if ((l = get_tty_line()) < 0) 1020 return ERR; 1021 else if (l == 0 || ibuf[l - 1] != '\n') { 1022 clearerr(stdin); 1023 return l ? EOF : 0; 1024 } 1025 lp = ibuf; 1026 } else if (*(lp = ibufp) == '\0') 1027 return 0; 1028 else { 1029 while (*ibufp++ != '\n') 1030 ; 1031 l = ibufp - lp; 1032 } 1033 if (l == 2 && lp[0] == '.' && lp[1] == '\n') { 1034 return 0; 1035 } 1036 eot = lp + l; 1037 SPL1(); 1038 do { 1039 if ((lp = put_sbuf_line(lp)) == NULL) { 1040 SPL0(); 1041 return ERR; 1042 } else if (up) 1043 up->t = get_addressed_line_node(current_addr); 1044 else if ((up = push_undo_stack(UADD, current_addr, 1045 current_addr)) == NULL) { 1046 SPL0(); 1047 return ERR; 1048 } 1049 } while (lp != eot); 1050 modified = 1; 1051 SPL0(); 1052 } 1053 /* NOTREACHED */ 1054 } 1055 1056 1057 /* join_lines: replace a range of lines with the joined text of those lines */ 1058 int 1059 join_lines(long from, long to) 1060 { 1061 static char *buf = NULL; 1062 static int n; 1063 1064 char *s; 1065 int size = 0; 1066 line_t *bp, *ep; 1067 1068 ep = get_addressed_line_node(INC_MOD(to, addr_last)); 1069 bp = get_addressed_line_node(from); 1070 for (; bp != ep; bp = bp->q_forw) { 1071 if ((s = get_sbuf_line(bp)) == NULL) 1072 return ERR; 1073 REALLOC(buf, n, size + bp->len, ERR); 1074 memcpy(buf + size, s, bp->len); 1075 size += bp->len; 1076 } 1077 REALLOC(buf, n, size + 2, ERR); 1078 memcpy(buf + size, "\n", 2); 1079 if (delete_lines(from, to) < 0) 1080 return ERR; 1081 current_addr = from - 1; 1082 SPL1(); 1083 if (put_sbuf_line(buf) == NULL || 1084 push_undo_stack(UADD, current_addr, current_addr) == NULL) { 1085 SPL0(); 1086 return ERR; 1087 } 1088 modified = 1; 1089 SPL0(); 1090 return 0; 1091 } 1092 1093 1094 /* move_lines: move a range of lines */ 1095 int 1096 move_lines(long addr) 1097 { 1098 line_t *b1, *a1, *b2, *a2; 1099 long n = INC_MOD(second_addr, addr_last); 1100 long p = first_addr - 1; 1101 int done = (addr == first_addr - 1 || addr == second_addr); 1102 1103 SPL1(); 1104 if (done) { 1105 a2 = get_addressed_line_node(n); 1106 b2 = get_addressed_line_node(p); 1107 current_addr = second_addr; 1108 } else if (push_undo_stack(UMOV, p, n) == NULL || 1109 push_undo_stack(UMOV, addr, INC_MOD(addr, addr_last)) == NULL) { 1110 SPL0(); 1111 return ERR; 1112 } else { 1113 a1 = get_addressed_line_node(n); 1114 if (addr < first_addr) { 1115 b1 = get_addressed_line_node(p); 1116 b2 = get_addressed_line_node(addr); 1117 /* this get_addressed_line_node last! */ 1118 } else { 1119 b2 = get_addressed_line_node(addr); 1120 b1 = get_addressed_line_node(p); 1121 /* this get_addressed_line_node last! */ 1122 } 1123 a2 = b2->q_forw; 1124 REQUE(b2, b1->q_forw); 1125 REQUE(a1->q_back, a2); 1126 REQUE(b1, a1); 1127 current_addr = addr + ((addr < first_addr) ? 1128 second_addr - first_addr + 1 : 0); 1129 } 1130 if (isglobal) 1131 unset_active_nodes(b2->q_forw, a2); 1132 modified = 1; 1133 SPL0(); 1134 return 0; 1135 } 1136 1137 1138 /* copy_lines: copy a range of lines; return status */ 1139 int 1140 copy_lines(long addr) 1141 { 1142 line_t *lp, *np = get_addressed_line_node(first_addr); 1143 undo_t *up = NULL; 1144 long n = second_addr - first_addr + 1; 1145 long m = 0; 1146 1147 current_addr = addr; 1148 if (first_addr <= addr && addr < second_addr) { 1149 n = addr - first_addr + 1; 1150 m = second_addr - addr; 1151 } 1152 for (; n > 0; n=m, m=0, np = get_addressed_line_node(current_addr + 1)) 1153 for (; n-- > 0; np = np->q_forw) { 1154 SPL1(); 1155 if ((lp = dup_line_node(np)) == NULL) { 1156 SPL0(); 1157 return ERR; 1158 } 1159 add_line_node(lp); 1160 if (up) 1161 up->t = lp; 1162 else if ((up = push_undo_stack(UADD, current_addr, 1163 current_addr)) == NULL) { 1164 SPL0(); 1165 return ERR; 1166 } 1167 modified = 1; 1168 SPL0(); 1169 } 1170 return 0; 1171 } 1172 1173 1174 /* delete_lines: delete a range of lines */ 1175 int 1176 delete_lines(long from, long to) 1177 { 1178 line_t *n, *p; 1179 1180 SPL1(); 1181 if (push_undo_stack(UDEL, from, to) == NULL) { 1182 SPL0(); 1183 return ERR; 1184 } 1185 n = get_addressed_line_node(INC_MOD(to, addr_last)); 1186 p = get_addressed_line_node(from - 1); 1187 /* this get_addressed_line_node last! */ 1188 if (isglobal) 1189 unset_active_nodes(p->q_forw, n); 1190 REQUE(p, n); 1191 addr_last -= to - from + 1; 1192 current_addr = from - 1; 1193 modified = 1; 1194 SPL0(); 1195 return 0; 1196 } 1197 1198 1199 /* display_lines: print a range of lines to stdout */ 1200 int 1201 display_lines(long from, long to, int gflag) 1202 { 1203 line_t *bp; 1204 line_t *ep; 1205 char *s; 1206 1207 if (!from) { 1208 errmsg = "invalid address"; 1209 return ERR; 1210 } 1211 ep = get_addressed_line_node(INC_MOD(to, addr_last)); 1212 bp = get_addressed_line_node(from); 1213 for (; bp != ep; bp = bp->q_forw) { 1214 if ((s = get_sbuf_line(bp)) == NULL) 1215 return ERR; 1216 if (put_tty_line(s, bp->len, current_addr = from++, gflag) < 0) 1217 return ERR; 1218 } 1219 return 0; 1220 } 1221 1222 1223 #define MAXMARK 26 /* max number of marks */ 1224 1225 static line_t *mark[MAXMARK]; /* line markers */ 1226 static int markno; /* line marker count */ 1227 1228 /* mark_line_node: set a line node mark */ 1229 int 1230 mark_line_node(line_t *lp, int n) 1231 { 1232 if (!islower((unsigned char)n)) { 1233 errmsg = "invalid mark character"; 1234 return ERR; 1235 } else if (mark[n - 'a'] == NULL) 1236 markno++; 1237 mark[n - 'a'] = lp; 1238 return 0; 1239 } 1240 1241 1242 /* get_marked_node_addr: return address of a marked line */ 1243 long 1244 get_marked_node_addr(int n) 1245 { 1246 if (!islower((unsigned char)n)) { 1247 errmsg = "invalid mark character"; 1248 return ERR; 1249 } 1250 return get_line_node_addr(mark[n - 'a']); 1251 } 1252 1253 1254 /* unmark_line_node: clear line node mark */ 1255 void 1256 unmark_line_node(line_t *lp) 1257 { 1258 int i; 1259 1260 for (i = 0; markno && i < MAXMARK; i++) 1261 if (mark[i] == lp) { 1262 mark[i] = NULL; 1263 markno--; 1264 } 1265 } 1266 1267 1268 /* dup_line_node: return a pointer to a copy of a line node */ 1269 line_t * 1270 dup_line_node(line_t *lp) 1271 { 1272 line_t *np; 1273 1274 if ((np = (line_t *) malloc(sizeof(line_t))) == NULL) { 1275 fprintf(stderr, "%s\n", strerror(errno)); 1276 errmsg = "out of memory"; 1277 return NULL; 1278 } 1279 np->seek = lp->seek; 1280 np->len = lp->len; 1281 return np; 1282 } 1283 1284 1285 /* has_trailing_escape: return the parity of escapes preceding a character 1286 in a string */ 1287 int 1288 has_trailing_escape(char *s, char *t) 1289 { 1290 return (s == t || *(t - 1) != '\\') ? 0 : !has_trailing_escape(s, t - 1); 1291 } 1292 1293 1294 /* strip_escapes: return a copy of escaped string of at most length PATH_MAX */ 1295 char * 1296 strip_escapes(char *s) 1297 { 1298 static char *file = NULL; 1299 static int filesz = 0; 1300 1301 int i = 0; 1302 1303 REALLOC(file, filesz, PATH_MAX, NULL); 1304 while (i < filesz - 1 /* Worry about a possible trailing escape */ 1305 && (file[i++] = (*s == '\\') ? *++s : *s)) 1306 s++; 1307 return file; 1308 } 1309 1310 1311 void 1312 signal_hup(int signo) 1313 { 1314 if (mutex) 1315 sigflags |= (1 << (signo - 1)); 1316 else 1317 handle_hup(signo); 1318 } 1319 1320 1321 void 1322 signal_int(int signo) 1323 { 1324 if (mutex) 1325 sigflags |= (1 << (signo - 1)); 1326 else 1327 handle_int(signo); 1328 } 1329 1330 1331 void 1332 handle_hup(int signo) 1333 { 1334 char *hup = NULL; /* hup filename */ 1335 char *s; 1336 char ed_hup[] = "ed.hup"; 1337 size_t n; 1338 1339 if (!sigactive) 1340 quit(1); 1341 sigflags &= ~(1 << (signo - 1)); 1342 if (addr_last && write_file(ed_hup, "w", 1, addr_last) < 0 && 1343 (s = getenv("HOME")) != NULL && 1344 (n = strlen(s)) + 8 <= PATH_MAX && /* "ed.hup" + '/' */ 1345 (hup = (char *) malloc(n + 10)) != NULL) { 1346 strcpy(hup, s); 1347 if (hup[n - 1] != '/') 1348 hup[n] = '/', hup[n+1] = '\0'; 1349 strcat(hup, "ed.hup"); 1350 write_file(hup, "w", 1, addr_last); 1351 } 1352 quit(2); 1353 } 1354 1355 1356 void 1357 handle_int(int signo) 1358 { 1359 if (!sigactive) 1360 quit(1); 1361 sigflags &= ~(1 << (signo - 1)); 1362 #ifdef _POSIX_SOURCE 1363 siglongjmp(env, -1); 1364 #else 1365 longjmp(env, -1); 1366 #endif 1367 } 1368 1369 1370 int cols = 72; /* wrap column */ 1371 1372 void 1373 handle_winch(int signo) 1374 { 1375 int save_errno = errno; 1376 1377 struct winsize ws; /* window size structure */ 1378 1379 sigflags &= ~(1 << (signo - 1)); 1380 if (ioctl(0, TIOCGWINSZ, (char *) &ws) >= 0) { 1381 if (ws.ws_row > 2) rows = ws.ws_row - 2; 1382 if (ws.ws_col > 8) cols = ws.ws_col - 8; 1383 } 1384 errno = save_errno; 1385 } 1386 1387 1388 /* is_legal_filename: return a legal filename */ 1389 int 1390 is_legal_filename(char *s) 1391 { 1392 if (red && (*s == '!' || !strcmp(s, "..") || strchr(s, '/'))) { 1393 errmsg = "shell access restricted"; 1394 return 0; 1395 } 1396 return 1; 1397 } 1398