1 /* $NetBSD: command.c,v 1.4 2011/07/04 12:31:53 joerg Exp $ */ 2 3 /* 4 * Copyright (C) 1984-2011 Mark Nudelman 5 * 6 * You may distribute under the terms of either the GNU General Public 7 * License or the Less License, as specified in the README file. 8 * 9 * For more information about less, or for information on how to 10 * contact the author, see the README file. 11 */ 12 13 14 /* 15 * User-level command processor. 16 */ 17 18 #include "less.h" 19 #if MSDOS_COMPILER==WIN32C 20 #include <windows.h> 21 #endif 22 #include "position.h" 23 #include "option.h" 24 #include "cmd.h" 25 26 extern int erase_char, erase2_char, kill_char; 27 extern int sigs; 28 extern int quit_if_one_screen; 29 extern int squished; 30 extern int sc_width; 31 extern int sc_height; 32 extern int swindow; 33 extern int jump_sline; 34 extern int quitting; 35 extern int wscroll; 36 extern int top_scroll; 37 extern int ignore_eoi; 38 extern int secure; 39 extern int hshift; 40 extern int show_attn; 41 extern char *every_first_cmd; 42 extern char *curr_altfilename; 43 extern char version[]; 44 extern struct scrpos initial_scrpos; 45 extern IFILE curr_ifile; 46 extern void constant *ml_search; 47 extern void * constant ml_examine; 48 #if SHELL_ESCAPE || PIPEC 49 extern void * constant ml_shell; 50 #endif 51 #if EDITOR 52 extern char *editor; 53 extern char *editproto; 54 #endif 55 extern int screen_trashed; /* The screen has been overwritten */ 56 extern int shift_count; 57 extern int oldbot; 58 extern int forw_prompt; 59 extern int be_helpful; 60 extern int more_mode; 61 62 static int helpprompt; 63 64 #if SHELL_ESCAPE 65 static char *shellcmd = NULL; /* For holding last shell command for "!!" */ 66 #endif 67 static int mca; /* The multicharacter command (action) */ 68 static int search_type; /* The previous type of search */ 69 static LINENUM number; /* The number typed by the user */ 70 static long fraction; /* The fractional part of the number */ 71 static struct loption *curropt; 72 static int opt_lower; 73 static int optflag; 74 static int optgetname; 75 static POSITION bottompos; 76 static int save_hshift; 77 #if PIPEC 78 static char pipec; 79 #endif 80 81 struct ungot { 82 struct ungot *ug_next; 83 char ug_char; 84 }; 85 static struct ungot* ungot = NULL; 86 static int unget_end = 0; 87 88 static void multi_search(); 89 90 /* 91 * Move the cursor to start of prompt line before executing a command. 92 * This looks nicer if the command takes a long time before 93 * updating the screen. 94 */ 95 static void 96 cmd_exec() 97 { 98 #if HILITE_SEARCH 99 clear_attn(); 100 #endif 101 clear_bot(); 102 flush(); 103 } 104 105 /* 106 * Set up the display to start a new multi-character command. 107 */ 108 static void 109 start_mca(action, prompt, mlist, cmdflags) 110 int action; 111 char *prompt; 112 void *mlist; 113 int cmdflags; 114 { 115 mca = action; 116 clear_bot(); 117 clear_cmd(); 118 cmd_putstr(prompt); 119 set_mlist(mlist, cmdflags); 120 } 121 122 public int 123 in_mca() 124 { 125 return (mca != 0 && mca != A_PREFIX); 126 } 127 128 /* 129 * Set up the display to start a new search command. 130 */ 131 static void 132 mca_search() 133 { 134 #if HILITE_SEARCH 135 if (search_type & SRCH_FILTER) 136 mca = A_FILTER; 137 else 138 #endif 139 if (search_type & SRCH_FORW) 140 mca = A_F_SEARCH; 141 else 142 mca = A_B_SEARCH; 143 144 clear_bot(); 145 clear_cmd(); 146 147 if (search_type & SRCH_NO_MATCH) 148 cmd_putstr("Non-match "); 149 if (search_type & SRCH_FIRST_FILE) 150 cmd_putstr("First-file "); 151 if (search_type & SRCH_PAST_EOF) 152 cmd_putstr("EOF-ignore "); 153 if (search_type & SRCH_NO_MOVE) 154 cmd_putstr("Keep-pos "); 155 if (search_type & SRCH_NO_REGEX) 156 cmd_putstr("Regex-off "); 157 158 #if HILITE_SEARCH 159 if (search_type & SRCH_FILTER) 160 cmd_putstr("&/"); 161 else 162 #endif 163 if (search_type & SRCH_FORW) 164 cmd_putstr("/"); 165 else 166 cmd_putstr("?"); 167 set_mlist(ml_search, 0); 168 } 169 170 /* 171 * Set up the display to start a new toggle-option command. 172 */ 173 static void 174 mca_opt_toggle() 175 { 176 int no_prompt; 177 int flag; 178 char *dash; 179 180 no_prompt = (optflag & OPT_NO_PROMPT); 181 flag = (optflag & ~OPT_NO_PROMPT); 182 dash = (flag == OPT_NO_TOGGLE) ? "_" : "-"; 183 184 mca = A_OPT_TOGGLE; 185 clear_bot(); 186 clear_cmd(); 187 cmd_putstr(dash); 188 if (optgetname) 189 cmd_putstr(dash); 190 if (no_prompt) 191 cmd_putstr("(P)"); 192 switch (flag) 193 { 194 case OPT_UNSET: 195 cmd_putstr("+"); 196 break; 197 case OPT_SET: 198 cmd_putstr("!"); 199 break; 200 } 201 set_mlist(NULL, 0); 202 } 203 204 /* 205 * Execute a multicharacter command. 206 */ 207 static void 208 exec_mca() 209 { 210 register char *cbuf; 211 212 cmd_exec(); 213 cbuf = get_cmdbuf(); 214 215 switch (mca) 216 { 217 case A_F_SEARCH: 218 case A_B_SEARCH: 219 multi_search(cbuf, (int) number); 220 break; 221 #if HILITE_SEARCH 222 case A_FILTER: 223 search_type ^= SRCH_NO_MATCH; 224 set_filter_pattern(cbuf, search_type); 225 break; 226 #endif 227 case A_FIRSTCMD: 228 /* 229 * Skip leading spaces or + signs in the string. 230 */ 231 while (*cbuf == '+' || *cbuf == ' ') 232 cbuf++; 233 if (every_first_cmd != NULL) 234 free(every_first_cmd); 235 if (*cbuf == '\0') 236 every_first_cmd = NULL; 237 else 238 every_first_cmd = save(cbuf); 239 break; 240 case A_OPT_TOGGLE: 241 toggle_option(curropt, opt_lower, cbuf, optflag); 242 curropt = NULL; 243 break; 244 case A_F_BRACKET: 245 match_brac(cbuf[0], cbuf[1], 1, (int) number); 246 break; 247 case A_B_BRACKET: 248 match_brac(cbuf[1], cbuf[0], 0, (int) number); 249 break; 250 #if EXAMINE 251 case A_EXAMINE: 252 if (secure) 253 break; 254 edit_list(cbuf); 255 #if TAGS 256 /* If tag structure is loaded then clean it up. */ 257 cleantags(); 258 #endif 259 break; 260 #endif 261 #if SHELL_ESCAPE 262 case A_SHELL: 263 /* 264 * !! just uses whatever is in shellcmd. 265 * Otherwise, copy cmdbuf to shellcmd, 266 * expanding any special characters ("%" or "#"). 267 */ 268 if (*cbuf != '!') 269 { 270 if (shellcmd != NULL) 271 free(shellcmd); 272 shellcmd = fexpand(cbuf); 273 } 274 275 if (secure) 276 break; 277 if (shellcmd == NULL) 278 lsystem("", "!done"); 279 else 280 lsystem(shellcmd, "!done"); 281 break; 282 #endif 283 #if PIPEC 284 case A_PIPE: 285 if (secure) 286 break; 287 (void) pipe_mark(pipec, cbuf); 288 error("|done", NULL_PARG); 289 break; 290 #endif 291 } 292 } 293 294 /* 295 * Is a character an erase or kill char? 296 */ 297 static int 298 is_erase_char(c) 299 int c; 300 { 301 return (c == erase_char || c == erase2_char || c == kill_char); 302 } 303 304 /* 305 * Handle the first char of an option (after the initial dash). 306 */ 307 static int 308 mca_opt_first_char(c) 309 int c; 310 { 311 int flag = (optflag & ~OPT_NO_PROMPT); 312 if (flag == OPT_NO_TOGGLE) 313 { 314 switch (c) 315 { 316 case '_': 317 /* "__" = long option name. */ 318 optgetname = TRUE; 319 mca_opt_toggle(); 320 return (MCA_MORE); 321 } 322 } else 323 { 324 switch (c) 325 { 326 case '+': 327 /* "-+" = UNSET. */ 328 optflag = (flag == OPT_UNSET) ? 329 OPT_TOGGLE : OPT_UNSET; 330 mca_opt_toggle(); 331 return (MCA_MORE); 332 case '!': 333 /* "-!" = SET */ 334 optflag = (flag == OPT_SET) ? 335 OPT_TOGGLE : OPT_SET; 336 mca_opt_toggle(); 337 return (MCA_MORE); 338 case CONTROL('P'): 339 optflag ^= OPT_NO_PROMPT; 340 mca_opt_toggle(); 341 return (MCA_MORE); 342 case '-': 343 /* "--" = long option name. */ 344 optgetname = TRUE; 345 mca_opt_toggle(); 346 return (MCA_MORE); 347 } 348 } 349 /* Char was not handled here. */ 350 return (NO_MCA); 351 } 352 353 /* 354 * Add a char to a long option name. 355 * See if we've got a match for an option name yet. 356 * If so, display the complete name and stop 357 * accepting chars until user hits RETURN. 358 */ 359 static int 360 mca_opt_nonfirst_char(c) 361 int c; 362 { 363 char *p; 364 char *oname; 365 366 if (curropt != NULL) 367 { 368 /* 369 * Already have a match for the name. 370 * Don't accept anything but erase/kill. 371 */ 372 if (is_erase_char(c)) 373 return (MCA_DONE); 374 return (MCA_MORE); 375 } 376 /* 377 * Add char to cmd buffer and try to match 378 * the option name. 379 */ 380 if (cmd_char(c) == CC_QUIT) 381 return (MCA_DONE); 382 p = get_cmdbuf(); 383 opt_lower = ASCII_IS_LOWER(p[0]); 384 curropt = findopt_name(&p, &oname, NULL); 385 if (curropt != NULL) 386 { 387 /* 388 * Got a match. 389 * Remember the option and 390 * display the full option name. 391 */ 392 cmd_reset(); 393 mca_opt_toggle(); 394 for (p = oname; *p != '\0'; p++) 395 { 396 c = *p; 397 if (!opt_lower && ASCII_IS_LOWER(c)) 398 c = ASCII_TO_UPPER(c); 399 if (cmd_char(c) != CC_OK) 400 return (MCA_DONE); 401 } 402 } 403 return (MCA_MORE); 404 } 405 406 /* 407 * Handle a char of an option toggle command. 408 */ 409 static int 410 mca_opt_char(c) 411 int c; 412 { 413 PARG parg; 414 415 /* 416 * This may be a short option (single char), 417 * or one char of a long option name, 418 * or one char of the option parameter. 419 */ 420 if (curropt == NULL && len_cmdbuf() == 0) 421 { 422 int ret = mca_opt_first_char(c); 423 if (ret != NO_MCA) 424 return (ret); 425 } 426 if (optgetname) 427 { 428 /* We're getting a long option name. */ 429 if (c != '\n' && c != '\r') 430 return (mca_opt_nonfirst_char(c)); 431 if (curropt == NULL) 432 { 433 parg.p_string = get_cmdbuf(); 434 error("There is no --%s option", &parg); 435 return (MCA_DONE); 436 } 437 optgetname = FALSE; 438 cmd_reset(); 439 } else 440 { 441 if (is_erase_char(c)) 442 return (NO_MCA); 443 if (curropt != NULL) 444 /* We're getting the option parameter. */ 445 return (NO_MCA); 446 curropt = findopt(c); 447 if (curropt == NULL) 448 { 449 parg.p_string = propt(c); 450 error("There is no %s option", &parg); 451 return (MCA_DONE); 452 } 453 } 454 /* 455 * If the option which was entered does not take a 456 * parameter, toggle the option immediately, 457 * so user doesn't have to hit RETURN. 458 */ 459 if ((optflag & ~OPT_NO_PROMPT) != OPT_TOGGLE || 460 !opt_has_param(curropt)) 461 { 462 toggle_option(curropt, ASCII_IS_LOWER(c), "", optflag); 463 return (MCA_DONE); 464 } 465 /* 466 * Display a prompt appropriate for the option parameter. 467 */ 468 start_mca(A_OPT_TOGGLE, opt_prompt(curropt), (void*)NULL, 0); 469 return (MCA_MORE); 470 } 471 472 /* 473 * Handle a char of a search command. 474 */ 475 static int 476 mca_search_char(c) 477 int c; 478 { 479 int flag = 0; 480 481 /* 482 * Certain characters as the first char of 483 * the pattern have special meaning: 484 * ! Toggle the NO_MATCH flag 485 * * Toggle the PAST_EOF flag (less extension) 486 * @ Toggle the FIRST_FILE flag (less extension) 487 */ 488 if (len_cmdbuf() > 0) 489 return (NO_MCA); 490 491 switch (c) 492 { 493 case '*': 494 if (more_mode) 495 break; 496 case CONTROL('E'): /* ignore END of file */ 497 if (mca != A_FILTER) 498 flag = SRCH_PAST_EOF; 499 break; 500 case '@': 501 if (more_mode) 502 break; 503 case CONTROL('F'): /* FIRST file */ 504 if (mca != A_FILTER) 505 flag = SRCH_FIRST_FILE; 506 break; 507 case CONTROL('K'): /* KEEP position */ 508 if (mca != A_FILTER) 509 flag = SRCH_NO_MOVE; 510 break; 511 case CONTROL('R'): /* Don't use REGULAR EXPRESSIONS */ 512 flag = SRCH_NO_REGEX; 513 break; 514 case CONTROL('N'): /* NOT match */ 515 case '!': 516 flag = SRCH_NO_MATCH; 517 break; 518 } 519 520 if (flag != 0) 521 { 522 search_type ^= flag; 523 mca_search(); 524 return (MCA_MORE); 525 } 526 return (NO_MCA); 527 } 528 529 /* 530 * Handle a character of a multi-character command. 531 */ 532 static int 533 mca_char(c) 534 int c; 535 { 536 int ret; 537 538 switch (mca) 539 { 540 case 0: 541 /* 542 * We're not in a multicharacter command. 543 */ 544 return (NO_MCA); 545 546 case A_PREFIX: 547 /* 548 * In the prefix of a command. 549 * This not considered a multichar command 550 * (even tho it uses cmdbuf, etc.). 551 * It is handled in the commands() switch. 552 */ 553 return (NO_MCA); 554 555 case A_DIGIT: 556 /* 557 * Entering digits of a number. 558 * Terminated by a non-digit. 559 */ 560 if (!((c >= '0' && c <= '9') || c == '.') && 561 editchar(c, EC_PEEK|EC_NOHISTORY|EC_NOCOMPLETE|EC_NORIGHTLEFT) == A_INVALID) 562 { 563 /* 564 * Not part of the number. 565 * End the number and treat this char 566 * as a normal command character. 567 */ 568 number = cmd_int(&fraction); 569 mca = 0; 570 cmd_accept(); 571 return (NO_MCA); 572 } 573 break; 574 575 case A_OPT_TOGGLE: 576 ret = mca_opt_char(c); 577 if (ret != NO_MCA) 578 return (ret); 579 break; 580 581 case A_F_SEARCH: 582 case A_B_SEARCH: 583 case A_FILTER: 584 ret = mca_search_char(c); 585 if (ret != NO_MCA) 586 return (ret); 587 break; 588 589 default: 590 /* Other multicharacter command. */ 591 break; 592 } 593 594 /* 595 * The multichar command is terminated by a newline. 596 */ 597 if (c == '\n' || c == '\r') 598 { 599 /* 600 * Execute the command. 601 */ 602 exec_mca(); 603 return (MCA_DONE); 604 } 605 606 /* 607 * Append the char to the command buffer. 608 */ 609 if (cmd_char(c) == CC_QUIT) 610 /* 611 * Abort the multi-char command. 612 */ 613 return (MCA_DONE); 614 615 if ((mca == A_F_BRACKET || mca == A_B_BRACKET) && len_cmdbuf() >= 2) 616 { 617 /* 618 * Special case for the bracket-matching commands. 619 * Execute the command after getting exactly two 620 * characters from the user. 621 */ 622 exec_mca(); 623 return (MCA_DONE); 624 } 625 626 /* 627 * Need another character. 628 */ 629 return (MCA_MORE); 630 } 631 632 /* 633 * Discard any buffered file data. 634 */ 635 static void 636 clear_buffers() 637 { 638 if (!(ch_getflags() & CH_CANSEEK)) 639 return; 640 ch_flush(); 641 clr_linenum(); 642 #if HILITE_SEARCH 643 clr_hilite(); 644 #endif 645 } 646 647 /* 648 * Make sure the screen is displayed. 649 */ 650 static void 651 make_display() 652 { 653 /* 654 * If nothing is displayed yet, display starting from initial_scrpos. 655 */ 656 if (empty_screen()) 657 { 658 if (initial_scrpos.pos == NULL_POSITION) 659 /* 660 * {{ Maybe this should be: 661 * jump_loc(ch_zero(), jump_sline); 662 * but this behavior seems rather unexpected 663 * on the first screen. }} 664 */ 665 jump_loc(ch_zero(), 1); 666 else 667 jump_loc(initial_scrpos.pos, initial_scrpos.ln); 668 } else if (screen_trashed) 669 { 670 int save_top_scroll = top_scroll; 671 int save_ignore_eoi = ignore_eoi; 672 top_scroll = 1; 673 ignore_eoi = 0; 674 if (screen_trashed == 2) 675 { 676 /* Special case used by ignore_eoi: re-open the input file 677 * and jump to the end of the file. */ 678 reopen_curr_ifile(); 679 jump_forw(); 680 } 681 repaint(); 682 top_scroll = save_top_scroll; 683 ignore_eoi = save_ignore_eoi; 684 } 685 } 686 687 /* 688 * Display the appropriate prompt. 689 */ 690 static void 691 prompt() 692 { 693 register char *p; 694 695 if (ungot != NULL) 696 { 697 /* 698 * No prompt necessary if commands are from 699 * ungotten chars rather than from the user. 700 */ 701 return; 702 } 703 704 /* 705 * Make sure the screen is displayed. 706 */ 707 make_display(); 708 bottompos = position(BOTTOM_PLUS_ONE); 709 710 /* 711 * If we've hit EOF on the last file and the -E flag is set, quit. 712 */ 713 if (get_quit_at_eof() == OPT_ONPLUS && 714 eof_displayed() && !(ch_getflags() & CH_HELPFILE) && 715 next_ifile(curr_ifile) == NULL_IFILE) 716 quit(QUIT_OK); 717 718 /* 719 * If the entire file is displayed and the -F flag is set, quit. 720 */ 721 if (quit_if_one_screen && 722 entire_file_displayed() && !(ch_getflags() & CH_HELPFILE) && 723 next_ifile(curr_ifile) == NULL_IFILE) 724 quit(QUIT_OK); 725 726 #if MSDOS_COMPILER==WIN32C 727 /* 728 * In Win32, display the file name in the window title. 729 */ 730 if (!(ch_getflags() & CH_HELPFILE)) 731 SetConsoleTitle(pr_expand("Less?f - %f.", 0)); 732 #endif 733 /* 734 * Select the proper prompt and display it. 735 */ 736 /* 737 * If the previous action was a forward movement, 738 * don't clear the bottom line of the display; 739 * just print the prompt since the forward movement guarantees 740 * that we're in the right position to display the prompt. 741 * Clearing the line could cause a problem: for example, if the last 742 * line displayed ended at the right screen edge without a newline, 743 * then clearing would clear the last displayed line rather than 744 * the prompt line. 745 */ 746 if (!forw_prompt) 747 clear_bot(); 748 clear_cmd(); 749 forw_prompt = 0; 750 if (helpprompt) { 751 at_enter(AT_STANDOUT); 752 putstr("[Press 'h' for instructions.]"); 753 at_exit(); 754 helpprompt = 0; 755 } else { 756 p = pr_string(); 757 if (is_filtering()) 758 putstr("& "); 759 if (p == NULL || *p == '\0') 760 putchr(':'); 761 else 762 { 763 at_enter(AT_STANDOUT); 764 putstr(p); 765 at_exit(); 766 } 767 } 768 clear_eol(); 769 } 770 771 /* 772 * Display the less version message. 773 */ 774 public void 775 dispversion() 776 { 777 PARG parg; 778 779 parg.p_string = version; 780 error("less %s", &parg); 781 } 782 783 /* 784 * Get command character. 785 * The character normally comes from the keyboard, 786 * but may come from ungotten characters 787 * (characters previously given to ungetcc or ungetsc). 788 */ 789 public int 790 getcc() 791 { 792 if (unget_end) 793 { 794 /* 795 * We have just run out of ungotten chars. 796 */ 797 unget_end = 0; 798 if (len_cmdbuf() == 0 || !empty_screen()) 799 return (getchr()); 800 /* 801 * Command is incomplete, so try to complete it. 802 */ 803 switch (mca) 804 { 805 case A_DIGIT: 806 /* 807 * We have a number but no command. Treat as #g. 808 */ 809 return ('g'); 810 811 case A_F_SEARCH: 812 case A_B_SEARCH: 813 /* 814 * We have "/string" but no newline. Add the \n. 815 */ 816 return ('\n'); 817 818 default: 819 /* 820 * Some other incomplete command. Let user complete it. 821 */ 822 return (getchr()); 823 } 824 } 825 826 if (ungot == NULL) 827 { 828 /* 829 * Normal case: no ungotten chars, so get one from the user. 830 */ 831 return (getchr()); 832 } 833 834 /* 835 * Return the next ungotten char. 836 */ 837 { 838 struct ungot *ug = ungot; 839 char c = ug->ug_char; 840 ungot = ug->ug_next; 841 free(ug); 842 unget_end = (ungot == NULL); 843 return (c); 844 } 845 } 846 847 /* 848 * "Unget" a command character. 849 * The next getcc() will return this character. 850 */ 851 public void 852 ungetcc(c) 853 int c; 854 { 855 struct ungot *ug = (struct ungot *) ecalloc(1, sizeof(struct ungot)); 856 857 ug->ug_char = c; 858 ug->ug_next = ungot; 859 ungot = ug; 860 unget_end = 0; 861 } 862 863 /* 864 * Unget a whole string of command characters. 865 * The next sequence of getcc()'s will return this string. 866 */ 867 public void 868 ungetsc(s) 869 char *s; 870 { 871 register char *p; 872 873 for (p = s + strlen(s) - 1; p >= s; p--) 874 ungetcc(*p); 875 } 876 877 /* 878 * Search for a pattern, possibly in multiple files. 879 * If SRCH_FIRST_FILE is set, begin searching at the first file. 880 * If SRCH_PAST_EOF is set, continue the search thru multiple files. 881 */ 882 static void 883 multi_search(pattern, n) 884 char *pattern; 885 int n; 886 { 887 register int nomore; 888 IFILE save_ifile; 889 int changed_file; 890 891 changed_file = 0; 892 save_ifile = save_curr_ifile(); 893 894 if (search_type & SRCH_FIRST_FILE) 895 { 896 /* 897 * Start at the first (or last) file 898 * in the command line list. 899 */ 900 if (search_type & SRCH_FORW) 901 nomore = edit_first(); 902 else 903 nomore = edit_last(); 904 if (nomore) 905 { 906 unsave_ifile(save_ifile); 907 return; 908 } 909 changed_file = 1; 910 search_type &= ~SRCH_FIRST_FILE; 911 } 912 913 for (;;) 914 { 915 n = search(search_type, pattern, n); 916 /* 917 * The SRCH_NO_MOVE flag doesn't "stick": it gets cleared 918 * after being used once. This allows "n" to work after 919 * using a /@@ search. 920 */ 921 search_type &= ~SRCH_NO_MOVE; 922 if (n == 0) 923 { 924 /* 925 * Found it. 926 */ 927 unsave_ifile(save_ifile); 928 return; 929 } 930 931 if (n < 0) 932 /* 933 * Some kind of error in the search. 934 * Error message has been printed by search(). 935 */ 936 break; 937 938 if ((search_type & SRCH_PAST_EOF) == 0) 939 /* 940 * We didn't find a match, but we're 941 * supposed to search only one file. 942 */ 943 break; 944 /* 945 * Move on to the next file. 946 */ 947 if (search_type & SRCH_FORW) 948 nomore = edit_next(1); 949 else 950 nomore = edit_prev(1); 951 if (nomore) 952 break; 953 changed_file = 1; 954 } 955 956 /* 957 * Didn't find it. 958 * Print an error message if we haven't already. 959 */ 960 if (n > 0) 961 error("Pattern not found", NULL_PARG); 962 963 if (changed_file) 964 { 965 /* 966 * Restore the file we were originally viewing. 967 */ 968 reedit_ifile(save_ifile); 969 } else 970 { 971 unsave_ifile(save_ifile); 972 } 973 } 974 975 /* 976 * Main command processor. 977 * Accept and execute commands until a quit command. 978 */ 979 public void 980 commands() 981 { 982 register int c; 983 register int action; 984 register char *cbuf; 985 int newaction; 986 int save_search_type; 987 char *extra; 988 char tbuf[2]; 989 PARG parg; 990 IFILE old_ifile; 991 IFILE new_ifile; 992 char *tagfile; 993 994 search_type = SRCH_FORW; 995 wscroll = (sc_height + 1) / 2; 996 newaction = A_NOACTION; 997 998 for (;;) 999 { 1000 mca = 0; 1001 cmd_accept(); 1002 number = 0; 1003 curropt = NULL; 1004 1005 /* 1006 * See if any signals need processing. 1007 */ 1008 if (sigs) 1009 { 1010 psignals(); 1011 if (quitting) 1012 quit(QUIT_SAVED_STATUS); 1013 } 1014 1015 /* 1016 * See if window size changed, for systems that don't 1017 * generate SIGWINCH. 1018 */ 1019 check_winch(); 1020 1021 /* 1022 * Display prompt and accept a character. 1023 */ 1024 cmd_reset(); 1025 prompt(); 1026 if (sigs) 1027 continue; 1028 if (newaction == A_NOACTION) 1029 c = getcc(); 1030 1031 again: 1032 if (sigs) 1033 continue; 1034 1035 if (newaction != A_NOACTION) 1036 { 1037 action = newaction; 1038 newaction = A_NOACTION; 1039 } else 1040 { 1041 /* 1042 * If we are in a multicharacter command, call mca_char. 1043 * Otherwise we call fcmd_decode to determine the 1044 * action to be performed. 1045 */ 1046 if (mca) 1047 switch (mca_char(c)) 1048 { 1049 case MCA_MORE: 1050 /* 1051 * Need another character. 1052 */ 1053 c = getcc(); 1054 goto again; 1055 case MCA_DONE: 1056 /* 1057 * Command has been handled by mca_char. 1058 * Start clean with a prompt. 1059 */ 1060 continue; 1061 case NO_MCA: 1062 /* 1063 * Not a multi-char command 1064 * (at least, not anymore). 1065 */ 1066 break; 1067 } 1068 1069 /* 1070 * Decode the command character and decide what to do. 1071 */ 1072 if (mca) 1073 { 1074 /* 1075 * We're in a multichar command. 1076 * Add the character to the command buffer 1077 * and display it on the screen. 1078 * If the user backspaces past the start 1079 * of the line, abort the command. 1080 */ 1081 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0) 1082 continue; 1083 cbuf = get_cmdbuf(); 1084 } else 1085 { 1086 /* 1087 * Don't use cmd_char if we're starting fresh 1088 * at the beginning of a command, because we 1089 * don't want to echo the command until we know 1090 * it is a multichar command. We also don't 1091 * want erase_char/kill_char to be treated 1092 * as line editing characters. 1093 */ 1094 tbuf[0] = c; 1095 tbuf[1] = '\0'; 1096 cbuf = tbuf; 1097 } 1098 extra = NULL; 1099 action = fcmd_decode(cbuf, &extra); 1100 /* 1101 * If an "extra" string was returned, 1102 * process it as a string of command characters. 1103 */ 1104 if (extra != NULL) 1105 ungetsc(extra); 1106 } 1107 /* 1108 * Clear the cmdbuf string. 1109 * (But not if we're in the prefix of a command, 1110 * because the partial command string is kept there.) 1111 */ 1112 if (action != A_PREFIX) 1113 cmd_reset(); 1114 1115 switch (action) 1116 { 1117 case A_DIGIT: 1118 /* 1119 * First digit of a number. 1120 */ 1121 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE); 1122 goto again; 1123 1124 case A_F_WINDOW: 1125 /* 1126 * Forward one window (and set the window size). 1127 */ 1128 if (number > 0) 1129 swindow = (int) number; 1130 /* FALLTHRU */ 1131 case A_F_SCREEN: 1132 /* 1133 * Forward one screen. 1134 */ 1135 if (number <= 0) 1136 number = get_swindow(); 1137 cmd_exec(); 1138 if (show_attn) 1139 set_attnpos(bottompos); 1140 forward((int) number, 0, 1); 1141 break; 1142 1143 case A_B_WINDOW: 1144 /* 1145 * Backward one window (and set the window size). 1146 */ 1147 if (number > 0) 1148 swindow = (int) number; 1149 /* FALLTHRU */ 1150 case A_B_SCREEN: 1151 /* 1152 * Backward one screen. 1153 */ 1154 if (number <= 0) 1155 number = get_swindow(); 1156 cmd_exec(); 1157 backward((int) number, 0, 1); 1158 break; 1159 1160 case A_F_LINE: 1161 /* 1162 * Forward N (default 1) line. 1163 */ 1164 if (number <= 0) 1165 number = 1; 1166 cmd_exec(); 1167 if (show_attn == OPT_ONPLUS && number > 1) 1168 set_attnpos(bottompos); 1169 forward((int) number, 0, 0); 1170 break; 1171 1172 case A_B_LINE: 1173 /* 1174 * Backward N (default 1) line. 1175 */ 1176 if (number <= 0) 1177 number = 1; 1178 cmd_exec(); 1179 backward((int) number, 0, 0); 1180 break; 1181 1182 case A_FF_LINE: 1183 /* 1184 * Force forward N (default 1) line. 1185 */ 1186 if (number <= 0) 1187 number = 1; 1188 cmd_exec(); 1189 if (show_attn == OPT_ONPLUS && number > 1) 1190 set_attnpos(bottompos); 1191 forward((int) number, 1, 0); 1192 break; 1193 1194 case A_BF_LINE: 1195 /* 1196 * Force backward N (default 1) line. 1197 */ 1198 if (number <= 0) 1199 number = 1; 1200 cmd_exec(); 1201 backward((int) number, 1, 0); 1202 break; 1203 1204 case A_FF_SCREEN: 1205 /* 1206 * Force forward one screen. 1207 */ 1208 if (number <= 0) 1209 number = get_swindow(); 1210 cmd_exec(); 1211 if (show_attn == OPT_ONPLUS) 1212 set_attnpos(bottompos); 1213 forward((int) number, 1, 0); 1214 break; 1215 1216 case A_F_FOREVER: 1217 /* 1218 * Forward forever, ignoring EOF. 1219 */ 1220 if (ch_getflags() & CH_HELPFILE) 1221 break; 1222 cmd_exec(); 1223 jump_forw(); 1224 ignore_eoi = 1; 1225 while (!sigs) 1226 { 1227 make_display(); 1228 forward(1, 0, 0); 1229 } 1230 ignore_eoi = 0; 1231 /* 1232 * This gets us back in "F mode" after processing 1233 * a non-abort signal (e.g. window-change). 1234 */ 1235 if (sigs && !ABORT_SIGS()) 1236 newaction = A_F_FOREVER; 1237 break; 1238 1239 case A_F_SCROLL: 1240 /* 1241 * Forward N lines 1242 * (default same as last 'd' or 'u' command). 1243 */ 1244 if (number > 0) 1245 wscroll = (int) number; 1246 cmd_exec(); 1247 if (show_attn == OPT_ONPLUS) 1248 set_attnpos(bottompos); 1249 forward(wscroll, 0, 0); 1250 break; 1251 1252 case A_B_SCROLL: 1253 /* 1254 * Forward N lines 1255 * (default same as last 'd' or 'u' command). 1256 */ 1257 if (number > 0) 1258 wscroll = (int) number; 1259 cmd_exec(); 1260 backward(wscroll, 0, 0); 1261 break; 1262 1263 case A_FREPAINT: 1264 /* 1265 * Flush buffers, then repaint screen. 1266 * Don't flush the buffers on a pipe! 1267 */ 1268 clear_buffers(); 1269 /* FALLTHRU */ 1270 case A_REPAINT: 1271 /* 1272 * Repaint screen. 1273 */ 1274 cmd_exec(); 1275 repaint(); 1276 break; 1277 1278 case A_GOLINE: 1279 /* 1280 * Go to line N, default beginning of file. 1281 */ 1282 if (number <= 0) 1283 number = 1; 1284 cmd_exec(); 1285 jump_back(number); 1286 break; 1287 1288 case A_PERCENT: 1289 /* 1290 * Go to a specified percentage into the file. 1291 */ 1292 if (number < 0) 1293 { 1294 number = 0; 1295 fraction = 0; 1296 } 1297 if (number > 100) 1298 { 1299 number = 100; 1300 fraction = 0; 1301 } 1302 cmd_exec(); 1303 jump_percent((int) number, fraction); 1304 break; 1305 1306 case A_GOEND: 1307 /* 1308 * Go to line N, default end of file. 1309 */ 1310 cmd_exec(); 1311 if (number <= 0) 1312 jump_forw(); 1313 else 1314 jump_back(number); 1315 break; 1316 1317 case A_GOPOS: 1318 /* 1319 * Go to a specified byte position in the file. 1320 */ 1321 cmd_exec(); 1322 if (number < 0) 1323 number = 0; 1324 jump_line_loc((POSITION) number, jump_sline); 1325 break; 1326 1327 case A_STAT: 1328 /* 1329 * Print file name, etc. 1330 */ 1331 if (ch_getflags() & CH_HELPFILE) 1332 break; 1333 cmd_exec(); 1334 parg.p_string = eq_message(); 1335 error("%s", &parg); 1336 break; 1337 1338 case A_VERSION: 1339 /* 1340 * Print version number, without the "@(#)". 1341 */ 1342 cmd_exec(); 1343 dispversion(); 1344 break; 1345 1346 case A_QUIT: 1347 /* 1348 * Exit. 1349 */ 1350 if (curr_ifile != NULL_IFILE && 1351 ch_getflags() & CH_HELPFILE) 1352 { 1353 /* 1354 * Quit while viewing the help file 1355 * just means return to viewing the 1356 * previous file. 1357 */ 1358 hshift = save_hshift; 1359 if (edit_prev(1) == 0) 1360 break; 1361 } 1362 if (extra != NULL) 1363 quit(*extra); 1364 quit(QUIT_OK); 1365 break; 1366 1367 /* 1368 * Define abbreviation for a commonly used sequence below. 1369 */ 1370 #define DO_SEARCH() \ 1371 if (number <= 0) number = 1; \ 1372 mca_search(); \ 1373 cmd_exec(); \ 1374 multi_search((char *)NULL, (int) number); 1375 1376 1377 case A_F_SEARCH: 1378 /* 1379 * Search forward for a pattern. 1380 * Get the first char of the pattern. 1381 */ 1382 search_type = SRCH_FORW; 1383 if (number <= 0) 1384 number = 1; 1385 mca_search(); 1386 c = getcc(); 1387 goto again; 1388 1389 case A_B_SEARCH: 1390 /* 1391 * Search backward for a pattern. 1392 * Get the first char of the pattern. 1393 */ 1394 search_type = SRCH_BACK; 1395 if (number <= 0) 1396 number = 1; 1397 mca_search(); 1398 c = getcc(); 1399 goto again; 1400 1401 case A_FILTER: 1402 #if HILITE_SEARCH 1403 search_type = SRCH_FORW | SRCH_FILTER; 1404 mca_search(); 1405 c = getcc(); 1406 goto again; 1407 #else 1408 error("Command not available", NULL_PARG); 1409 break; 1410 #endif 1411 1412 case A_AGAIN_SEARCH: 1413 /* 1414 * Repeat previous search. 1415 */ 1416 DO_SEARCH(); 1417 break; 1418 1419 case A_T_AGAIN_SEARCH: 1420 /* 1421 * Repeat previous search, multiple files. 1422 */ 1423 search_type |= SRCH_PAST_EOF; 1424 DO_SEARCH(); 1425 break; 1426 1427 case A_REVERSE_SEARCH: 1428 /* 1429 * Repeat previous search, in reverse direction. 1430 */ 1431 save_search_type = search_type; 1432 search_type = SRCH_REVERSE(search_type); 1433 DO_SEARCH(); 1434 search_type = save_search_type; 1435 break; 1436 1437 case A_T_REVERSE_SEARCH: 1438 /* 1439 * Repeat previous search, 1440 * multiple files in reverse direction. 1441 */ 1442 save_search_type = search_type; 1443 search_type = SRCH_REVERSE(search_type); 1444 search_type |= SRCH_PAST_EOF; 1445 DO_SEARCH(); 1446 search_type = save_search_type; 1447 break; 1448 1449 case A_UNDO_SEARCH: 1450 undo_search(); 1451 break; 1452 1453 case A_HELP: 1454 /* 1455 * Help. 1456 */ 1457 if (ch_getflags() & CH_HELPFILE) 1458 break; 1459 cmd_exec(); 1460 save_hshift = hshift; 1461 hshift = 0; 1462 (void) edit(FAKE_HELPFILE); 1463 break; 1464 1465 case A_EXAMINE: 1466 #if EXAMINE 1467 /* 1468 * Edit a new file. Get the filename. 1469 */ 1470 if (secure) 1471 { 1472 error("Command not available", NULL_PARG); 1473 break; 1474 } 1475 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0); 1476 c = getcc(); 1477 goto again; 1478 #else 1479 error("Command not available", NULL_PARG); 1480 break; 1481 #endif 1482 1483 case A_VISUAL: 1484 /* 1485 * Invoke an editor on the input file. 1486 */ 1487 #if EDITOR 1488 if (secure) 1489 { 1490 error("Command not available", NULL_PARG); 1491 break; 1492 } 1493 if (ch_getflags() & CH_HELPFILE) 1494 break; 1495 if (strcmp(get_filename(curr_ifile), "-") == 0) 1496 { 1497 error("Cannot edit standard input", NULL_PARG); 1498 break; 1499 } 1500 if (curr_altfilename != NULL) 1501 { 1502 error("WARNING: This file was viewed via LESSOPEN", 1503 NULL_PARG); 1504 } 1505 start_mca(A_SHELL, "!", ml_shell, 0); 1506 /* 1507 * Expand the editor prototype string 1508 * and pass it to the system to execute. 1509 * (Make sure the screen is displayed so the 1510 * expansion of "+%lm" works.) 1511 */ 1512 make_display(); 1513 cmd_exec(); 1514 lsystem(pr_expand(editproto, 0), (char*)NULL); 1515 break; 1516 #else 1517 error("Command not available", NULL_PARG); 1518 break; 1519 #endif 1520 1521 case A_NEXT_FILE: 1522 /* 1523 * Examine next file. 1524 */ 1525 #if TAGS 1526 if (ntags()) 1527 { 1528 error("No next file", NULL_PARG); 1529 break; 1530 } 1531 #endif 1532 if (number <= 0) 1533 number = 1; 1534 if (edit_next((int) number)) 1535 { 1536 if (get_quit_at_eof() && eof_displayed() && 1537 !(ch_getflags() & CH_HELPFILE)) 1538 quit(QUIT_OK); 1539 parg.p_string = (number > 1) ? "(N-th) " : ""; 1540 error("No %snext file", &parg); 1541 } 1542 break; 1543 1544 case A_PREV_FILE: 1545 /* 1546 * Examine previous file. 1547 */ 1548 #if TAGS 1549 if (ntags()) 1550 { 1551 error("No previous file", NULL_PARG); 1552 break; 1553 } 1554 #endif 1555 if (number <= 0) 1556 number = 1; 1557 if (edit_prev((int) number)) 1558 { 1559 parg.p_string = (number > 1) ? "(N-th) " : ""; 1560 error("No %sprevious file", &parg); 1561 } 1562 break; 1563 1564 case A_NEXT_TAG: 1565 #if TAGS 1566 if (number <= 0) 1567 number = 1; 1568 tagfile = nexttag((int) number); 1569 if (tagfile == NULL) 1570 { 1571 error("No next tag", NULL_PARG); 1572 break; 1573 } 1574 if (edit(tagfile) == 0) 1575 { 1576 POSITION pos = tagsearch(); 1577 if (pos != NULL_POSITION) 1578 jump_loc(pos, jump_sline); 1579 } 1580 #else 1581 error("Command not available", NULL_PARG); 1582 #endif 1583 break; 1584 1585 case A_PREV_TAG: 1586 #if TAGS 1587 if (number <= 0) 1588 number = 1; 1589 tagfile = prevtag((int) number); 1590 if (tagfile == NULL) 1591 { 1592 error("No previous tag", NULL_PARG); 1593 break; 1594 } 1595 if (edit(tagfile) == 0) 1596 { 1597 POSITION pos = tagsearch(); 1598 if (pos != NULL_POSITION) 1599 jump_loc(pos, jump_sline); 1600 } 1601 #else 1602 error("Command not available", NULL_PARG); 1603 #endif 1604 break; 1605 1606 case A_INDEX_FILE: 1607 /* 1608 * Examine a particular file. 1609 */ 1610 if (number <= 0) 1611 number = 1; 1612 if (edit_index((int) number)) 1613 error("No such file", NULL_PARG); 1614 break; 1615 1616 case A_REMOVE_FILE: 1617 if (ch_getflags() & CH_HELPFILE) 1618 break; 1619 old_ifile = curr_ifile; 1620 new_ifile = getoff_ifile(curr_ifile); 1621 if (new_ifile == NULL_IFILE) 1622 { 1623 bell(); 1624 break; 1625 } 1626 if (edit_ifile(new_ifile) != 0) 1627 { 1628 reedit_ifile(old_ifile); 1629 break; 1630 } 1631 del_ifile(old_ifile); 1632 break; 1633 1634 case A_OPT_TOGGLE: 1635 optflag = OPT_TOGGLE; 1636 optgetname = FALSE; 1637 mca_opt_toggle(); 1638 c = getcc(); 1639 goto again; 1640 1641 case A_DISP_OPTION: 1642 /* 1643 * Report a flag setting. 1644 */ 1645 optflag = OPT_NO_TOGGLE; 1646 optgetname = FALSE; 1647 mca_opt_toggle(); 1648 c = getcc(); 1649 goto again; 1650 1651 case A_FIRSTCMD: 1652 /* 1653 * Set an initial command for new files. 1654 */ 1655 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0); 1656 c = getcc(); 1657 goto again; 1658 1659 case A_SHELL: 1660 /* 1661 * Shell escape. 1662 */ 1663 #if SHELL_ESCAPE 1664 if (secure) 1665 { 1666 error("Command not available", NULL_PARG); 1667 break; 1668 } 1669 start_mca(A_SHELL, "!", ml_shell, 0); 1670 c = getcc(); 1671 goto again; 1672 #else 1673 error("Command not available", NULL_PARG); 1674 break; 1675 #endif 1676 1677 case A_SETMARK: 1678 /* 1679 * Set a mark. 1680 */ 1681 if (ch_getflags() & CH_HELPFILE) 1682 break; 1683 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0); 1684 c = getcc(); 1685 if (c == erase_char || c == erase2_char || 1686 c == kill_char || c == '\n' || c == '\r') 1687 break; 1688 setmark(c); 1689 break; 1690 1691 case A_GOMARK: 1692 /* 1693 * Go to a mark. 1694 */ 1695 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0); 1696 c = getcc(); 1697 if (c == erase_char || c == erase2_char || 1698 c == kill_char || c == '\n' || c == '\r') 1699 break; 1700 cmd_exec(); 1701 gomark(c); 1702 break; 1703 1704 case A_PIPE: 1705 #if PIPEC 1706 if (secure) 1707 { 1708 error("Command not available", NULL_PARG); 1709 break; 1710 } 1711 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0); 1712 c = getcc(); 1713 if (c == erase_char || c == erase2_char || c == kill_char) 1714 break; 1715 if (c == '\n' || c == '\r') 1716 c = '.'; 1717 if (badmark(c)) 1718 break; 1719 pipec = c; 1720 start_mca(A_PIPE, "!", ml_shell, 0); 1721 c = getcc(); 1722 goto again; 1723 #else 1724 error("Command not available", NULL_PARG); 1725 break; 1726 #endif 1727 1728 case A_B_BRACKET: 1729 case A_F_BRACKET: 1730 start_mca(action, "Brackets: ", (void*)NULL, 0); 1731 c = getcc(); 1732 goto again; 1733 1734 case A_LSHIFT: 1735 if (number > 0) 1736 shift_count = number; 1737 else 1738 number = (shift_count > 0) ? 1739 shift_count : sc_width / 2; 1740 if (number > hshift) 1741 number = hshift; 1742 hshift -= number; 1743 screen_trashed = 1; 1744 break; 1745 1746 case A_RSHIFT: 1747 if (number > 0) 1748 shift_count = number; 1749 else 1750 number = (shift_count > 0) ? 1751 shift_count : sc_width / 2; 1752 hshift += number; 1753 screen_trashed = 1; 1754 break; 1755 1756 case A_PREFIX: 1757 /* 1758 * The command is incomplete (more chars are needed). 1759 * Display the current char, so the user knows 1760 * what's going on, and get another character. 1761 */ 1762 if (mca != A_PREFIX) 1763 { 1764 cmd_reset(); 1765 start_mca(A_PREFIX, " ", (void*)NULL, 1766 CF_QUIT_ON_ERASE); 1767 (void) cmd_char(c); 1768 } 1769 c = getcc(); 1770 goto again; 1771 1772 case A_NOACTION: 1773 break; 1774 1775 default: 1776 if (be_helpful) 1777 helpprompt = 1; 1778 else 1779 bell(); 1780 break; 1781 } 1782 } 1783 } 1784