1 /* $NetBSD: command.c,v 1.5 2013/09/04 19:44:21 tron Exp $ */ 2 3 /* 4 * Copyright (C) 1984-2012 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, see the README file. 10 */ 11 12 13 /* 14 * User-level command processor. 15 */ 16 17 #include "less.h" 18 #if MSDOS_COMPILER==WIN32C 19 #include <windows.h> 20 #endif 21 #include "position.h" 22 #include "option.h" 23 #include "cmd.h" 24 25 extern int erase_char, erase2_char, kill_char; 26 extern int sigs; 27 extern int quit_if_one_screen; 28 extern int squished; 29 extern int sc_width; 30 extern int sc_height; 31 extern int swindow; 32 extern int jump_sline; 33 extern int quitting; 34 extern int wscroll; 35 extern int top_scroll; 36 extern int ignore_eoi; 37 extern int secure; 38 extern int hshift; 39 extern int show_attn; 40 extern POSITION highest_hilite; 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 constant char *prompt; 112 constant 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 constant 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 * Forward forever, or until a highlighted line appears. 977 */ 978 static int 979 forw_loop(until_hilite) 980 int until_hilite; 981 { 982 POSITION curr_len; 983 984 if (ch_getflags() & CH_HELPFILE) 985 return (A_NOACTION); 986 987 cmd_exec(); 988 jump_forw(); 989 curr_len = ch_length(); 990 highest_hilite = until_hilite ? curr_len : NULL_POSITION; 991 ignore_eoi = 1; 992 while (!sigs) 993 { 994 if (until_hilite && highest_hilite > curr_len) 995 { 996 bell(); 997 break; 998 } 999 make_display(); 1000 forward(1, 0, 0); 1001 } 1002 ignore_eoi = 0; 1003 ch_set_eof(); 1004 1005 /* 1006 * This gets us back in "F mode" after processing 1007 * a non-abort signal (e.g. window-change). 1008 */ 1009 if (sigs && !ABORT_SIGS()) 1010 return (until_hilite ? A_F_UNTIL_HILITE : A_F_FOREVER); 1011 1012 return (A_NOACTION); 1013 } 1014 1015 /* 1016 * Main command processor. 1017 * Accept and execute commands until a quit command. 1018 */ 1019 public void 1020 commands() 1021 { 1022 register int c; 1023 register int action; 1024 register char *cbuf; 1025 int newaction; 1026 int save_search_type; 1027 char *extra; 1028 char tbuf[2]; 1029 PARG parg; 1030 IFILE old_ifile; 1031 IFILE new_ifile; 1032 char *tagfile; 1033 int until_hilite = 0; 1034 1035 search_type = SRCH_FORW; 1036 wscroll = (sc_height + 1) / 2; 1037 newaction = A_NOACTION; 1038 1039 for (;;) 1040 { 1041 mca = 0; 1042 cmd_accept(); 1043 number = 0; 1044 curropt = NULL; 1045 1046 /* 1047 * See if any signals need processing. 1048 */ 1049 if (sigs) 1050 { 1051 psignals(); 1052 if (quitting) 1053 quit(QUIT_SAVED_STATUS); 1054 } 1055 1056 /* 1057 * See if window size changed, for systems that don't 1058 * generate SIGWINCH. 1059 */ 1060 check_winch(); 1061 1062 /* 1063 * Display prompt and accept a character. 1064 */ 1065 cmd_reset(); 1066 prompt(); 1067 if (sigs) 1068 continue; 1069 if (newaction == A_NOACTION) 1070 c = getcc(); 1071 1072 again: 1073 if (sigs) 1074 continue; 1075 1076 if (newaction != A_NOACTION) 1077 { 1078 action = newaction; 1079 newaction = A_NOACTION; 1080 } else 1081 { 1082 /* 1083 * If we are in a multicharacter command, call mca_char. 1084 * Otherwise we call fcmd_decode to determine the 1085 * action to be performed. 1086 */ 1087 if (mca) 1088 switch (mca_char(c)) 1089 { 1090 case MCA_MORE: 1091 /* 1092 * Need another character. 1093 */ 1094 c = getcc(); 1095 goto again; 1096 case MCA_DONE: 1097 /* 1098 * Command has been handled by mca_char. 1099 * Start clean with a prompt. 1100 */ 1101 continue; 1102 case NO_MCA: 1103 /* 1104 * Not a multi-char command 1105 * (at least, not anymore). 1106 */ 1107 break; 1108 } 1109 1110 /* 1111 * Decode the command character and decide what to do. 1112 */ 1113 if (mca) 1114 { 1115 /* 1116 * We're in a multichar command. 1117 * Add the character to the command buffer 1118 * and display it on the screen. 1119 * If the user backspaces past the start 1120 * of the line, abort the command. 1121 */ 1122 if (cmd_char(c) == CC_QUIT || len_cmdbuf() == 0) 1123 continue; 1124 cbuf = get_cmdbuf(); 1125 } else 1126 { 1127 /* 1128 * Don't use cmd_char if we're starting fresh 1129 * at the beginning of a command, because we 1130 * don't want to echo the command until we know 1131 * it is a multichar command. We also don't 1132 * want erase_char/kill_char to be treated 1133 * as line editing characters. 1134 */ 1135 tbuf[0] = c; 1136 tbuf[1] = '\0'; 1137 cbuf = tbuf; 1138 } 1139 extra = NULL; 1140 action = fcmd_decode(cbuf, &extra); 1141 /* 1142 * If an "extra" string was returned, 1143 * process it as a string of command characters. 1144 */ 1145 if (extra != NULL) 1146 ungetsc(extra); 1147 } 1148 /* 1149 * Clear the cmdbuf string. 1150 * (But not if we're in the prefix of a command, 1151 * because the partial command string is kept there.) 1152 */ 1153 if (action != A_PREFIX) 1154 cmd_reset(); 1155 1156 switch (action) 1157 { 1158 case A_DIGIT: 1159 /* 1160 * First digit of a number. 1161 */ 1162 start_mca(A_DIGIT, ":", (void*)NULL, CF_QUIT_ON_ERASE); 1163 goto again; 1164 1165 case A_F_WINDOW: 1166 /* 1167 * Forward one window (and set the window size). 1168 */ 1169 if (number > 0) 1170 swindow = (int) number; 1171 /* FALLTHRU */ 1172 case A_F_SCREEN: 1173 /* 1174 * Forward one screen. 1175 */ 1176 if (number <= 0) 1177 number = get_swindow(); 1178 cmd_exec(); 1179 if (show_attn) 1180 set_attnpos(bottompos); 1181 forward((int) number, 0, 1); 1182 break; 1183 1184 case A_B_WINDOW: 1185 /* 1186 * Backward one window (and set the window size). 1187 */ 1188 if (number > 0) 1189 swindow = (int) number; 1190 /* FALLTHRU */ 1191 case A_B_SCREEN: 1192 /* 1193 * Backward one screen. 1194 */ 1195 if (number <= 0) 1196 number = get_swindow(); 1197 cmd_exec(); 1198 backward((int) number, 0, 1); 1199 break; 1200 1201 case A_F_LINE: 1202 /* 1203 * Forward N (default 1) line. 1204 */ 1205 if (number <= 0) 1206 number = 1; 1207 cmd_exec(); 1208 if (show_attn == OPT_ONPLUS && number > 1) 1209 set_attnpos(bottompos); 1210 forward((int) number, 0, 0); 1211 break; 1212 1213 case A_B_LINE: 1214 /* 1215 * Backward N (default 1) line. 1216 */ 1217 if (number <= 0) 1218 number = 1; 1219 cmd_exec(); 1220 backward((int) number, 0, 0); 1221 break; 1222 1223 case A_FF_LINE: 1224 /* 1225 * Force forward N (default 1) line. 1226 */ 1227 if (number <= 0) 1228 number = 1; 1229 cmd_exec(); 1230 if (show_attn == OPT_ONPLUS && number > 1) 1231 set_attnpos(bottompos); 1232 forward((int) number, 1, 0); 1233 break; 1234 1235 case A_BF_LINE: 1236 /* 1237 * Force backward N (default 1) line. 1238 */ 1239 if (number <= 0) 1240 number = 1; 1241 cmd_exec(); 1242 backward((int) number, 1, 0); 1243 break; 1244 1245 case A_FF_SCREEN: 1246 /* 1247 * Force forward one screen. 1248 */ 1249 if (number <= 0) 1250 number = get_swindow(); 1251 cmd_exec(); 1252 if (show_attn == OPT_ONPLUS) 1253 set_attnpos(bottompos); 1254 forward((int) number, 1, 0); 1255 break; 1256 1257 case A_F_FOREVER: 1258 /* 1259 * Forward forever, ignoring EOF. 1260 */ 1261 newaction = forw_loop(0); 1262 break; 1263 1264 case A_F_UNTIL_HILITE: 1265 newaction = forw_loop(1); 1266 break; 1267 1268 case A_F_SCROLL: 1269 /* 1270 * Forward N lines 1271 * (default same as last 'd' or 'u' command). 1272 */ 1273 if (number > 0) 1274 wscroll = (int) number; 1275 cmd_exec(); 1276 if (show_attn == OPT_ONPLUS) 1277 set_attnpos(bottompos); 1278 forward(wscroll, 0, 0); 1279 break; 1280 1281 case A_B_SCROLL: 1282 /* 1283 * Forward N lines 1284 * (default same as last 'd' or 'u' command). 1285 */ 1286 if (number > 0) 1287 wscroll = (int) number; 1288 cmd_exec(); 1289 backward(wscroll, 0, 0); 1290 break; 1291 1292 case A_FREPAINT: 1293 /* 1294 * Flush buffers, then repaint screen. 1295 * Don't flush the buffers on a pipe! 1296 */ 1297 clear_buffers(); 1298 /* FALLTHRU */ 1299 case A_REPAINT: 1300 /* 1301 * Repaint screen. 1302 */ 1303 cmd_exec(); 1304 repaint(); 1305 break; 1306 1307 case A_GOLINE: 1308 /* 1309 * Go to line N, default beginning of file. 1310 */ 1311 if (number <= 0) 1312 number = 1; 1313 cmd_exec(); 1314 jump_back(number); 1315 break; 1316 1317 case A_PERCENT: 1318 /* 1319 * Go to a specified percentage into the file. 1320 */ 1321 if (number < 0) 1322 { 1323 number = 0; 1324 fraction = 0; 1325 } 1326 if (number > 100) 1327 { 1328 number = 100; 1329 fraction = 0; 1330 } 1331 cmd_exec(); 1332 jump_percent((int) number, fraction); 1333 break; 1334 1335 case A_GOEND: 1336 /* 1337 * Go to line N, default end of file. 1338 */ 1339 cmd_exec(); 1340 if (number <= 0) 1341 jump_forw(); 1342 else 1343 jump_back(number); 1344 break; 1345 1346 case A_GOPOS: 1347 /* 1348 * Go to a specified byte position in the file. 1349 */ 1350 cmd_exec(); 1351 if (number < 0) 1352 number = 0; 1353 jump_line_loc((POSITION) number, jump_sline); 1354 break; 1355 1356 case A_STAT: 1357 /* 1358 * Print file name, etc. 1359 */ 1360 if (ch_getflags() & CH_HELPFILE) 1361 break; 1362 cmd_exec(); 1363 parg.p_string = eq_message(); 1364 error("%s", &parg); 1365 break; 1366 1367 case A_VERSION: 1368 /* 1369 * Print version number, without the "@(#)". 1370 */ 1371 cmd_exec(); 1372 dispversion(); 1373 break; 1374 1375 case A_QUIT: 1376 /* 1377 * Exit. 1378 */ 1379 if (curr_ifile != NULL_IFILE && 1380 ch_getflags() & CH_HELPFILE) 1381 { 1382 /* 1383 * Quit while viewing the help file 1384 * just means return to viewing the 1385 * previous file. 1386 */ 1387 hshift = save_hshift; 1388 if (edit_prev(1) == 0) 1389 break; 1390 } 1391 if (extra != NULL) 1392 quit(*extra); 1393 quit(QUIT_OK); 1394 break; 1395 1396 /* 1397 * Define abbreviation for a commonly used sequence below. 1398 */ 1399 #define DO_SEARCH() \ 1400 if (number <= 0) number = 1; \ 1401 mca_search(); \ 1402 cmd_exec(); \ 1403 multi_search((char *)NULL, (int) number); 1404 1405 1406 case A_F_SEARCH: 1407 /* 1408 * Search forward for a pattern. 1409 * Get the first char of the pattern. 1410 */ 1411 search_type = SRCH_FORW; 1412 if (number <= 0) 1413 number = 1; 1414 mca_search(); 1415 c = getcc(); 1416 goto again; 1417 1418 case A_B_SEARCH: 1419 /* 1420 * Search backward for a pattern. 1421 * Get the first char of the pattern. 1422 */ 1423 search_type = SRCH_BACK; 1424 if (number <= 0) 1425 number = 1; 1426 mca_search(); 1427 c = getcc(); 1428 goto again; 1429 1430 case A_FILTER: 1431 #if HILITE_SEARCH 1432 search_type = SRCH_FORW | SRCH_FILTER; 1433 mca_search(); 1434 c = getcc(); 1435 goto again; 1436 #else 1437 error("Command not available", NULL_PARG); 1438 break; 1439 #endif 1440 1441 case A_AGAIN_SEARCH: 1442 /* 1443 * Repeat previous search. 1444 */ 1445 DO_SEARCH(); 1446 break; 1447 1448 case A_T_AGAIN_SEARCH: 1449 /* 1450 * Repeat previous search, multiple files. 1451 */ 1452 search_type |= SRCH_PAST_EOF; 1453 DO_SEARCH(); 1454 break; 1455 1456 case A_REVERSE_SEARCH: 1457 /* 1458 * Repeat previous search, in reverse direction. 1459 */ 1460 save_search_type = search_type; 1461 search_type = SRCH_REVERSE(search_type); 1462 DO_SEARCH(); 1463 search_type = save_search_type; 1464 break; 1465 1466 case A_T_REVERSE_SEARCH: 1467 /* 1468 * Repeat previous search, 1469 * multiple files in reverse direction. 1470 */ 1471 save_search_type = search_type; 1472 search_type = SRCH_REVERSE(search_type); 1473 search_type |= SRCH_PAST_EOF; 1474 DO_SEARCH(); 1475 search_type = save_search_type; 1476 break; 1477 1478 case A_UNDO_SEARCH: 1479 undo_search(); 1480 break; 1481 1482 case A_HELP: 1483 /* 1484 * Help. 1485 */ 1486 if (ch_getflags() & CH_HELPFILE) 1487 break; 1488 cmd_exec(); 1489 save_hshift = hshift; 1490 hshift = 0; 1491 (void) edit(FAKE_HELPFILE); 1492 break; 1493 1494 case A_EXAMINE: 1495 #if EXAMINE 1496 /* 1497 * Edit a new file. Get the filename. 1498 */ 1499 if (secure) 1500 { 1501 error("Command not available", NULL_PARG); 1502 break; 1503 } 1504 start_mca(A_EXAMINE, "Examine: ", ml_examine, 0); 1505 c = getcc(); 1506 goto again; 1507 #else 1508 error("Command not available", NULL_PARG); 1509 break; 1510 #endif 1511 1512 case A_VISUAL: 1513 /* 1514 * Invoke an editor on the input file. 1515 */ 1516 #if EDITOR 1517 if (secure) 1518 { 1519 error("Command not available", NULL_PARG); 1520 break; 1521 } 1522 if (ch_getflags() & CH_HELPFILE) 1523 break; 1524 if (strcmp(get_filename(curr_ifile), "-") == 0) 1525 { 1526 error("Cannot edit standard input", NULL_PARG); 1527 break; 1528 } 1529 if (curr_altfilename != NULL) 1530 { 1531 error("WARNING: This file was viewed via LESSOPEN", 1532 NULL_PARG); 1533 } 1534 start_mca(A_SHELL, "!", ml_shell, 0); 1535 /* 1536 * Expand the editor prototype string 1537 * and pass it to the system to execute. 1538 * (Make sure the screen is displayed so the 1539 * expansion of "+%lm" works.) 1540 */ 1541 make_display(); 1542 cmd_exec(); 1543 lsystem(pr_expand(editproto, 0), (char*)NULL); 1544 break; 1545 #else 1546 error("Command not available", NULL_PARG); 1547 break; 1548 #endif 1549 1550 case A_NEXT_FILE: 1551 /* 1552 * Examine next file. 1553 */ 1554 #if TAGS 1555 if (ntags()) 1556 { 1557 error("No next file", NULL_PARG); 1558 break; 1559 } 1560 #endif 1561 if (number <= 0) 1562 number = 1; 1563 if (edit_next((int) number)) 1564 { 1565 if (get_quit_at_eof() && eof_displayed() && 1566 !(ch_getflags() & CH_HELPFILE)) 1567 quit(QUIT_OK); 1568 parg.p_string = (number > 1) ? "(N-th) " : ""; 1569 error("No %snext file", &parg); 1570 } 1571 break; 1572 1573 case A_PREV_FILE: 1574 /* 1575 * Examine previous file. 1576 */ 1577 #if TAGS 1578 if (ntags()) 1579 { 1580 error("No previous file", NULL_PARG); 1581 break; 1582 } 1583 #endif 1584 if (number <= 0) 1585 number = 1; 1586 if (edit_prev((int) number)) 1587 { 1588 parg.p_string = (number > 1) ? "(N-th) " : ""; 1589 error("No %sprevious file", &parg); 1590 } 1591 break; 1592 1593 case A_NEXT_TAG: 1594 #if TAGS 1595 if (number <= 0) 1596 number = 1; 1597 tagfile = nexttag((int) number); 1598 if (tagfile == NULL) 1599 { 1600 error("No next tag", NULL_PARG); 1601 break; 1602 } 1603 if (edit(tagfile) == 0) 1604 { 1605 POSITION pos = tagsearch(); 1606 if (pos != NULL_POSITION) 1607 jump_loc(pos, jump_sline); 1608 } 1609 #else 1610 error("Command not available", NULL_PARG); 1611 #endif 1612 break; 1613 1614 case A_PREV_TAG: 1615 #if TAGS 1616 if (number <= 0) 1617 number = 1; 1618 tagfile = prevtag((int) number); 1619 if (tagfile == NULL) 1620 { 1621 error("No previous tag", NULL_PARG); 1622 break; 1623 } 1624 if (edit(tagfile) == 0) 1625 { 1626 POSITION pos = tagsearch(); 1627 if (pos != NULL_POSITION) 1628 jump_loc(pos, jump_sline); 1629 } 1630 #else 1631 error("Command not available", NULL_PARG); 1632 #endif 1633 break; 1634 1635 case A_INDEX_FILE: 1636 /* 1637 * Examine a particular file. 1638 */ 1639 if (number <= 0) 1640 number = 1; 1641 if (edit_index((int) number)) 1642 error("No such file", NULL_PARG); 1643 break; 1644 1645 case A_REMOVE_FILE: 1646 if (ch_getflags() & CH_HELPFILE) 1647 break; 1648 old_ifile = curr_ifile; 1649 new_ifile = getoff_ifile(curr_ifile); 1650 if (new_ifile == NULL_IFILE) 1651 { 1652 bell(); 1653 break; 1654 } 1655 if (edit_ifile(new_ifile) != 0) 1656 { 1657 reedit_ifile(old_ifile); 1658 break; 1659 } 1660 del_ifile(old_ifile); 1661 break; 1662 1663 case A_OPT_TOGGLE: 1664 optflag = OPT_TOGGLE; 1665 optgetname = FALSE; 1666 mca_opt_toggle(); 1667 c = getcc(); 1668 goto again; 1669 1670 case A_DISP_OPTION: 1671 /* 1672 * Report a flag setting. 1673 */ 1674 optflag = OPT_NO_TOGGLE; 1675 optgetname = FALSE; 1676 mca_opt_toggle(); 1677 c = getcc(); 1678 goto again; 1679 1680 case A_FIRSTCMD: 1681 /* 1682 * Set an initial command for new files. 1683 */ 1684 start_mca(A_FIRSTCMD, "+", (void*)NULL, 0); 1685 c = getcc(); 1686 goto again; 1687 1688 case A_SHELL: 1689 /* 1690 * Shell escape. 1691 */ 1692 #if SHELL_ESCAPE 1693 if (secure) 1694 { 1695 error("Command not available", NULL_PARG); 1696 break; 1697 } 1698 start_mca(A_SHELL, "!", ml_shell, 0); 1699 c = getcc(); 1700 goto again; 1701 #else 1702 error("Command not available", NULL_PARG); 1703 break; 1704 #endif 1705 1706 case A_SETMARK: 1707 /* 1708 * Set a mark. 1709 */ 1710 if (ch_getflags() & CH_HELPFILE) 1711 break; 1712 start_mca(A_SETMARK, "mark: ", (void*)NULL, 0); 1713 c = getcc(); 1714 if (c == erase_char || c == erase2_char || 1715 c == kill_char || c == '\n' || c == '\r') 1716 break; 1717 setmark(c); 1718 break; 1719 1720 case A_GOMARK: 1721 /* 1722 * Go to a mark. 1723 */ 1724 start_mca(A_GOMARK, "goto mark: ", (void*)NULL, 0); 1725 c = getcc(); 1726 if (c == erase_char || c == erase2_char || 1727 c == kill_char || c == '\n' || c == '\r') 1728 break; 1729 cmd_exec(); 1730 gomark(c); 1731 break; 1732 1733 case A_PIPE: 1734 #if PIPEC 1735 if (secure) 1736 { 1737 error("Command not available", NULL_PARG); 1738 break; 1739 } 1740 start_mca(A_PIPE, "|mark: ", (void*)NULL, 0); 1741 c = getcc(); 1742 if (c == erase_char || c == erase2_char || c == kill_char) 1743 break; 1744 if (c == '\n' || c == '\r') 1745 c = '.'; 1746 if (badmark(c)) 1747 break; 1748 pipec = c; 1749 start_mca(A_PIPE, "!", ml_shell, 0); 1750 c = getcc(); 1751 goto again; 1752 #else 1753 error("Command not available", NULL_PARG); 1754 break; 1755 #endif 1756 1757 case A_B_BRACKET: 1758 case A_F_BRACKET: 1759 start_mca(action, "Brackets: ", (void*)NULL, 0); 1760 c = getcc(); 1761 goto again; 1762 1763 case A_LSHIFT: 1764 if (number > 0) 1765 shift_count = number; 1766 else 1767 number = (shift_count > 0) ? 1768 shift_count : sc_width / 2; 1769 if (number > hshift) 1770 number = hshift; 1771 hshift -= number; 1772 screen_trashed = 1; 1773 break; 1774 1775 case A_RSHIFT: 1776 if (number > 0) 1777 shift_count = number; 1778 else 1779 number = (shift_count > 0) ? 1780 shift_count : sc_width / 2; 1781 hshift += number; 1782 screen_trashed = 1; 1783 break; 1784 1785 case A_PREFIX: 1786 /* 1787 * The command is incomplete (more chars are needed). 1788 * Display the current char, so the user knows 1789 * what's going on, and get another character. 1790 */ 1791 if (mca != A_PREFIX) 1792 { 1793 cmd_reset(); 1794 start_mca(A_PREFIX, " ", (void*)NULL, 1795 CF_QUIT_ON_ERASE); 1796 (void) cmd_char(c); 1797 } 1798 c = getcc(); 1799 goto again; 1800 1801 case A_NOACTION: 1802 break; 1803 1804 default: 1805 if (be_helpful) 1806 helpprompt = 1; 1807 else 1808 bell(); 1809 break; 1810 } 1811 } 1812 } 1813