1 /* 2 * Copyright (C) 1984-2021 Mark Nudelman 3 * 4 * You may distribute under the terms of either the GNU General Public 5 * License or the Less License, as specified in the README file. 6 * 7 * For more information, see the README file. 8 */ 9 10 11 /* 12 * The option table. 13 */ 14 15 #include "less.h" 16 #include "option.h" 17 18 /* 19 * Variables controlled by command line options. 20 */ 21 public int quiet; /* Should we suppress the audible bell? */ 22 public int how_search; /* Where should forward searches start? */ 23 public int top_scroll; /* Repaint screen from top? 24 (alternative is scroll from bottom) */ 25 public int pr_type; /* Type of prompt (short, medium, long) */ 26 public int bs_mode; /* How to process backspaces */ 27 public int know_dumb; /* Don't complain about dumb terminals */ 28 public int quit_at_eof; /* Quit after hitting end of file twice */ 29 public int quit_if_one_screen; /* Quit if EOF on first screen */ 30 public int squeeze; /* Squeeze multiple blank lines into one */ 31 public int tabstop; /* Tab settings */ 32 public int back_scroll; /* Repaint screen on backwards movement */ 33 public int forw_scroll; /* Repaint screen on forward movement */ 34 public int caseless; /* Do "caseless" searches */ 35 public int linenums; /* Use line numbers */ 36 public int autobuf; /* Automatically allocate buffers as needed */ 37 public int bufspace; /* Max buffer space per file (K) */ 38 public int ctldisp; /* Send control chars to screen untranslated */ 39 public int force_open; /* Open the file even if not regular file */ 40 public int swindow; /* Size of scrolling window */ 41 public int jump_sline; /* Screen line of "jump target" */ 42 public long jump_sline_fraction = -1; 43 public long shift_count_fraction = -1; 44 public int chopline; /* Truncate displayed lines at screen width */ 45 public int no_init; /* Disable sending ti/te termcap strings */ 46 public int no_keypad; /* Disable sending ks/ke termcap strings */ 47 public int twiddle; /* Show tildes after EOF */ 48 public int show_attn; /* Hilite first unread line */ 49 public int shift_count; /* Number of positions to shift horizontally */ 50 public int status_col; /* Display a status column */ 51 public int use_lessopen; /* Use the LESSOPEN filter */ 52 public int quit_on_intr; /* Quit on interrupt */ 53 public int follow_mode; /* F cmd Follows file desc or file name? */ 54 public int oldbot; /* Old bottom of screen behavior {{REMOVE}} */ 55 public int opt_use_backslash; /* Use backslash escaping in option parsing */ 56 public char rscroll_char; /* Char which marks chopped lines with -S */ 57 public int rscroll_attr; /* Attribute of rscroll_char */ 58 public int no_hist_dups; /* Remove dups from history list */ 59 public int mousecap; /* Allow mouse for scrolling */ 60 public int wheel_lines; /* Number of lines to scroll on mouse wheel scroll */ 61 public int perma_marks; /* Save marks in history file */ 62 public int linenum_width; /* Width of line numbers */ 63 public int status_col_width; /* Width of status column */ 64 public int incr_search; /* Incremental search */ 65 public int use_color; /* Use UI color */ 66 #if HILITE_SEARCH 67 public int hilite_search; /* Highlight matched search patterns? */ 68 #endif 69 70 public int less_is_more = 0; /* Make compatible with POSIX more */ 71 72 /* 73 * Long option names. 74 */ 75 static struct optname a_optname = { "search-skip-screen", NULL }; 76 static struct optname b_optname = { "buffers", NULL }; 77 static struct optname B__optname = { "auto-buffers", NULL }; 78 static struct optname c_optname = { "clear-screen", NULL }; 79 static struct optname d_optname = { "dumb", NULL }; 80 static struct optname D__optname = { "color", NULL }; 81 static struct optname e_optname = { "quit-at-eof", NULL }; 82 static struct optname f_optname = { "force", NULL }; 83 static struct optname F__optname = { "quit-if-one-screen", NULL }; 84 #if HILITE_SEARCH 85 static struct optname g_optname = { "hilite-search", NULL }; 86 #endif 87 static struct optname h_optname = { "max-back-scroll", NULL }; 88 static struct optname i_optname = { "ignore-case", NULL }; 89 static struct optname j_optname = { "jump-target", NULL }; 90 static struct optname J__optname = { "status-column", NULL }; 91 #if USERFILE 92 static struct optname k_optname = { "lesskey-file", NULL }; 93 #endif 94 static struct optname K__optname = { "quit-on-intr", NULL }; 95 static struct optname L__optname = { "no-lessopen", NULL }; 96 static struct optname m_optname = { "long-prompt", NULL }; 97 static struct optname n_optname = { "line-numbers", NULL }; 98 #if LOGFILE 99 static struct optname o_optname = { "log-file", NULL }; 100 static struct optname O__optname = { "LOG-FILE", NULL }; 101 #endif 102 static struct optname p_optname = { "pattern", NULL }; 103 static struct optname P__optname = { "prompt", NULL }; 104 static struct optname q2_optname = { "silent", NULL }; 105 static struct optname q_optname = { "quiet", &q2_optname }; 106 static struct optname r_optname = { "raw-control-chars", NULL }; 107 static struct optname s_optname = { "squeeze-blank-lines", NULL }; 108 static struct optname S__optname = { "chop-long-lines", NULL }; 109 #if TAGS 110 static struct optname t_optname = { "tag", NULL }; 111 static struct optname T__optname = { "tag-file", NULL }; 112 #endif 113 static struct optname u_optname = { "underline-special", NULL }; 114 static struct optname V__optname = { "version", NULL }; 115 static struct optname w_optname = { "hilite-unread", NULL }; 116 static struct optname x_optname = { "tabs", NULL }; 117 static struct optname X__optname = { "no-init", NULL }; 118 static struct optname y_optname = { "max-forw-scroll", NULL }; 119 static struct optname z_optname = { "window", NULL }; 120 static struct optname quote_optname = { "quotes", NULL }; 121 static struct optname tilde_optname = { "tilde", NULL }; 122 static struct optname query_optname = { "help", NULL }; 123 static struct optname pound_optname = { "shift", NULL }; 124 static struct optname keypad_optname = { "no-keypad", NULL }; 125 static struct optname oldbot_optname = { "old-bot", NULL }; 126 static struct optname follow_optname = { "follow-name", NULL }; 127 static struct optname use_backslash_optname = { "use-backslash", NULL }; 128 static struct optname rscroll_optname = { "rscroll", NULL }; 129 static struct optname nohistdups_optname = { "no-histdups", NULL }; 130 static struct optname mousecap_optname = { "mouse", NULL }; 131 static struct optname wheel_lines_optname = { "wheel-lines", NULL }; 132 static struct optname perma_marks_optname = { "save-marks", NULL }; 133 static struct optname linenum_width_optname = { "line-num-width", NULL }; 134 static struct optname status_col_width_optname = { "status-col-width", NULL }; 135 static struct optname incr_search_optname = { "incsearch", NULL }; 136 static struct optname use_color_optname = { "use-color", NULL }; 137 #if LESSTEST 138 static struct optname ttyin_name_optname = { "tty", NULL }; 139 static struct optname rstat_optname = { "rstat", NULL }; 140 #endif /*LESSTEST*/ 141 142 143 /* 144 * Table of all options and their semantics. 145 * 146 * For BOOL and TRIPLE options, odesc[0], odesc[1], odesc[2] are 147 * the description of the option when set to 0, 1 or 2, respectively. 148 * For NUMBER options, odesc[0] is the prompt to use when entering 149 * a new value, and odesc[1] is the description, which should contain 150 * one %d which is replaced by the value of the number. 151 * For STRING options, odesc[0] is the prompt to use when entering 152 * a new value, and odesc[1], if not NULL, is the set of characters 153 * that are valid in the string. 154 */ 155 static struct loption option[] = 156 { 157 { 'a', &a_optname, 158 TRIPLE, OPT_ONPLUS, &how_search, NULL, 159 { 160 "Search includes displayed screen", 161 "Search skips displayed screen", 162 "Search includes all of displayed screen" 163 } 164 }, 165 166 { 'b', &b_optname, 167 NUMBER|INIT_HANDLER, 64, &bufspace, opt_b, 168 { 169 "Max buffer space per file (K): ", 170 "Max buffer space per file: %dK", 171 NULL 172 } 173 }, 174 { 'B', &B__optname, 175 BOOL, OPT_ON, &autobuf, NULL, 176 { 177 "Don't automatically allocate buffers", 178 "Automatically allocate buffers when needed", 179 NULL 180 } 181 }, 182 { 'c', &c_optname, 183 TRIPLE, OPT_OFF, &top_scroll, NULL, 184 { 185 "Repaint by scrolling from bottom of screen", 186 "Repaint by painting from top of screen", 187 "Repaint by painting from top of screen" 188 } 189 }, 190 { 'd', &d_optname, 191 BOOL|NO_TOGGLE, OPT_OFF, &know_dumb, NULL, 192 { 193 "Assume intelligent terminal", 194 "Assume dumb terminal", 195 NULL 196 } 197 }, 198 { 'D', &D__optname, 199 STRING|REPAINT|NO_QUERY, 0, NULL, opt_D, 200 { 201 "color desc: ", 202 NULL, 203 NULL 204 } 205 }, 206 { 'e', &e_optname, 207 TRIPLE, OPT_OFF, &quit_at_eof, NULL, 208 { 209 "Don't quit at end-of-file", 210 "Quit at end-of-file", 211 "Quit immediately at end-of-file" 212 } 213 }, 214 { 'f', &f_optname, 215 BOOL, OPT_OFF, &force_open, NULL, 216 { 217 "Open only regular files", 218 "Open even non-regular files", 219 NULL 220 } 221 }, 222 { 'F', &F__optname, 223 BOOL, OPT_OFF, &quit_if_one_screen, NULL, 224 { 225 "Don't quit if end-of-file on first screen", 226 "Quit if end-of-file on first screen", 227 NULL 228 } 229 }, 230 #if HILITE_SEARCH 231 { 'g', &g_optname, 232 TRIPLE|HL_REPAINT, OPT_ONPLUS, &hilite_search, NULL, 233 { 234 "Don't highlight search matches", 235 "Highlight matches for previous search only", 236 "Highlight all matches for previous search pattern", 237 } 238 }, 239 #endif 240 { 'h', &h_optname, 241 NUMBER, -1, &back_scroll, NULL, 242 { 243 "Backwards scroll limit: ", 244 "Backwards scroll limit is %d lines", 245 NULL 246 } 247 }, 248 { 'i', &i_optname, 249 TRIPLE|HL_REPAINT, OPT_OFF, &caseless, opt_i, 250 { 251 "Case is significant in searches", 252 "Ignore case in searches", 253 "Ignore case in searches and in patterns" 254 } 255 }, 256 { 'j', &j_optname, 257 STRING, 0, NULL, opt_j, 258 { 259 "Target line: ", 260 "0123456789.-", 261 NULL 262 } 263 }, 264 { 'J', &J__optname, 265 BOOL|REPAINT, OPT_OFF, &status_col, NULL, 266 { 267 "Don't display a status column", 268 "Display a status column", 269 NULL 270 } 271 }, 272 #if USERFILE 273 { 'k', &k_optname, 274 STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k, 275 { NULL, NULL, NULL } 276 }, 277 #endif 278 { 'K', &K__optname, 279 BOOL, OPT_OFF, &quit_on_intr, NULL, 280 { 281 "Interrupt (ctrl-C) returns to prompt", 282 "Interrupt (ctrl-C) exits less", 283 NULL 284 } 285 }, 286 { 'L', &L__optname, 287 BOOL, OPT_ON, &use_lessopen, NULL, 288 { 289 "Don't use the LESSOPEN filter", 290 "Use the LESSOPEN filter", 291 NULL 292 } 293 }, 294 { 'm', &m_optname, 295 TRIPLE, OPT_OFF, &pr_type, NULL, 296 { 297 "Short prompt", 298 "Medium prompt", 299 "Long prompt" 300 } 301 }, 302 { 'n', &n_optname, 303 TRIPLE|REPAINT, OPT_ON, &linenums, NULL, 304 { 305 "Don't use line numbers", 306 "Use line numbers", 307 "Constantly display line numbers" 308 } 309 }, 310 #if LOGFILE 311 { 'o', &o_optname, 312 STRING, 0, NULL, opt_o, 313 { "log file: ", NULL, NULL } 314 }, 315 { 'O', &O__optname, 316 STRING, 0, NULL, opt__O, 317 { "Log file: ", NULL, NULL } 318 }, 319 #endif 320 { 'p', &p_optname, 321 STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_p, 322 { NULL, NULL, NULL } 323 }, 324 { 'P', &P__optname, 325 STRING, 0, NULL, opt__P, 326 { "prompt: ", NULL, NULL } 327 }, 328 { 'q', &q_optname, 329 TRIPLE, OPT_OFF, &quiet, NULL, 330 { 331 "Ring the bell for errors AND at eof/bof", 332 "Ring the bell for errors but not at eof/bof", 333 "Never ring the bell" 334 } 335 }, 336 { 'r', &r_optname, 337 TRIPLE|REPAINT, OPT_OFF, &ctldisp, NULL, 338 { 339 "Display control characters as ^X", 340 "Display control characters directly", 341 "Display control characters directly, processing ANSI sequences" 342 } 343 }, 344 { 's', &s_optname, 345 BOOL|REPAINT, OPT_OFF, &squeeze, NULL, 346 { 347 "Display all blank lines", 348 "Squeeze multiple blank lines", 349 NULL 350 } 351 }, 352 { 'S', &S__optname, 353 BOOL|REPAINT, OPT_OFF, &chopline, NULL, 354 { 355 "Fold long lines", 356 "Chop long lines", 357 NULL 358 } 359 }, 360 #if TAGS 361 { 't', &t_optname, 362 STRING|NO_QUERY, 0, NULL, opt_t, 363 { "tag: ", NULL, NULL } 364 }, 365 { 'T', &T__optname, 366 STRING, 0, NULL, opt__T, 367 { "tags file: ", NULL, NULL } 368 }, 369 #endif 370 { 'u', &u_optname, 371 TRIPLE|REPAINT, OPT_OFF, &bs_mode, NULL, 372 { 373 "Display underlined text in underline mode", 374 "Backspaces cause overstrike", 375 "Print backspace as ^H" 376 } 377 }, 378 { 'V', &V__optname, 379 NOVAR, 0, NULL, opt__V, 380 { NULL, NULL, NULL } 381 }, 382 { 'w', &w_optname, 383 TRIPLE|REPAINT, OPT_OFF, &show_attn, NULL, 384 { 385 "Don't highlight first unread line", 386 "Highlight first unread line after forward-screen", 387 "Highlight first unread line after any forward movement", 388 } 389 }, 390 { 'x', &x_optname, 391 STRING|REPAINT, 0, NULL, opt_x, 392 { 393 "Tab stops: ", 394 "0123456789,", 395 NULL 396 } 397 }, 398 { 'X', &X__optname, 399 BOOL|NO_TOGGLE, OPT_OFF, &no_init, NULL, 400 { 401 "Send init/deinit strings to terminal", 402 "Don't use init/deinit strings", 403 NULL 404 } 405 }, 406 { 'y', &y_optname, 407 NUMBER, -1, &forw_scroll, NULL, 408 { 409 "Forward scroll limit: ", 410 "Forward scroll limit is %d lines", 411 NULL 412 } 413 }, 414 { 'z', &z_optname, 415 NUMBER, -1, &swindow, NULL, 416 { 417 "Scroll window size: ", 418 "Scroll window size is %d lines", 419 NULL 420 } 421 }, 422 { '"', "e_optname, 423 STRING, 0, NULL, opt_quote, 424 { "quotes: ", NULL, NULL } 425 }, 426 { '~', &tilde_optname, 427 BOOL|REPAINT, OPT_ON, &twiddle, NULL, 428 { 429 "Don't show tildes after end of file", 430 "Show tildes after end of file", 431 NULL 432 } 433 }, 434 { '?', &query_optname, 435 NOVAR, 0, NULL, opt_query, 436 { NULL, NULL, NULL } 437 }, 438 { '#', £_optname, 439 STRING, 0, NULL, opt_shift, 440 { 441 "Horizontal shift: ", 442 "0123456789.", 443 NULL 444 } 445 }, 446 { OLETTER_NONE, &keypad_optname, 447 BOOL|NO_TOGGLE, OPT_OFF, &no_keypad, NULL, 448 { 449 "Use keypad mode", 450 "Don't use keypad mode", 451 NULL 452 } 453 }, 454 { OLETTER_NONE, &oldbot_optname, 455 BOOL, OPT_OFF, &oldbot, NULL, 456 { 457 "Use new bottom of screen behavior", 458 "Use old bottom of screen behavior", 459 NULL 460 } 461 }, 462 { OLETTER_NONE, &follow_optname, 463 BOOL, FOLLOW_DESC, &follow_mode, NULL, 464 { 465 "F command follows file descriptor", 466 "F command follows file name", 467 NULL 468 } 469 }, 470 { OLETTER_NONE, &use_backslash_optname, 471 BOOL, OPT_OFF, &opt_use_backslash, NULL, 472 { 473 "Use backslash escaping in command line parameters", 474 "Don't use backslash escaping in command line parameters", 475 NULL 476 } 477 }, 478 { OLETTER_NONE, &rscroll_optname, 479 STRING|REPAINT|INIT_HANDLER, 0, NULL, opt_rscroll, 480 { "right scroll character: ", NULL, NULL } 481 }, 482 { OLETTER_NONE, &nohistdups_optname, 483 BOOL, OPT_OFF, &no_hist_dups, NULL, 484 { 485 "Allow duplicates in history list", 486 "Remove duplicates from history list", 487 NULL 488 } 489 }, 490 { OLETTER_NONE, &mousecap_optname, 491 TRIPLE, OPT_OFF, &mousecap, opt_mousecap, 492 { 493 "Ignore mouse input", 494 "Use the mouse for scrolling", 495 "Use the mouse for scrolling (reverse)" 496 } 497 }, 498 { OLETTER_NONE, &wheel_lines_optname, 499 NUMBER|INIT_HANDLER, 0, &wheel_lines, opt_wheel_lines, 500 { 501 "Lines to scroll on mouse wheel: ", 502 "Scroll %d line(s) on mouse wheel", 503 NULL 504 } 505 }, 506 { OLETTER_NONE, &perma_marks_optname, 507 BOOL, OPT_OFF, &perma_marks, NULL, 508 { 509 "Don't save marks in history file", 510 "Save marks in history file", 511 NULL 512 } 513 }, 514 { OLETTER_NONE, &linenum_width_optname, 515 NUMBER|REPAINT, MIN_LINENUM_WIDTH, &linenum_width, opt_linenum_width, 516 { 517 "Line number width: ", 518 "Line number width is %d chars", 519 NULL 520 } 521 }, 522 { OLETTER_NONE, &status_col_width_optname, 523 NUMBER|REPAINT, 2, &status_col_width, opt_status_col_width, 524 { 525 "Status column width: ", 526 "Status column width is %d chars", 527 NULL 528 } 529 }, 530 { OLETTER_NONE, &incr_search_optname, 531 BOOL, OPT_OFF, &incr_search, NULL, 532 { 533 "Incremental search is off", 534 "Incremental search is on", 535 NULL 536 } 537 }, 538 { OLETTER_NONE, &use_color_optname, 539 BOOL|REPAINT, OPT_OFF, &use_color, NULL, 540 { 541 "Don't use color", 542 "Use color", 543 NULL 544 } 545 }, 546 #if LESSTEST 547 { OLETTER_NONE, &ttyin_name_optname, 548 STRING|NO_TOGGLE, 0, NULL, opt_ttyin_name, 549 { 550 NULL, 551 NULL, 552 NULL 553 } 554 }, 555 { OLETTER_NONE, &rstat_optname, 556 STRING|NO_TOGGLE, 0, NULL, opt_rstat, 557 { 558 NULL, 559 NULL, 560 NULL 561 } 562 }, 563 #endif /*LESSTEST*/ 564 { '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } } 565 }; 566 567 568 /* 569 * Initialize each option to its default value. 570 */ 571 public void 572 init_option(VOID_PARAM) 573 { 574 struct loption *o; 575 char *p; 576 577 p = lgetenv("LESS_IS_MORE"); 578 if (!isnullenv(p)) 579 less_is_more = 1; 580 581 for (o = option; o->oletter != '\0'; o++) 582 { 583 /* 584 * Set each variable to its default. 585 */ 586 if (o->ovar != NULL) 587 *(o->ovar) = o->odefault; 588 if (o->otype & INIT_HANDLER) 589 (*(o->ofunc))(INIT, (char *) NULL); 590 } 591 } 592 593 /* 594 * Find an option in the option table, given its option letter. 595 */ 596 public struct loption * 597 findopt(c) 598 int c; 599 { 600 struct loption *o; 601 602 for (o = option; o->oletter != '\0'; o++) 603 { 604 if (o->oletter == c) 605 return (o); 606 if ((o->otype & TRIPLE) && ASCII_TO_UPPER(o->oletter) == c) 607 return (o); 608 } 609 return (NULL); 610 } 611 612 /* 613 * 614 */ 615 static int 616 is_optchar(c) 617 char c; 618 { 619 if (ASCII_IS_UPPER(c)) 620 return 1; 621 if (ASCII_IS_LOWER(c)) 622 return 1; 623 if (c == '-') 624 return 1; 625 return 0; 626 } 627 628 /* 629 * Find an option in the option table, given its option name. 630 * p_optname is the (possibly partial) name to look for, and 631 * is updated to point after the matched name. 632 * p_oname if non-NULL is set to point to the full option name. 633 */ 634 public struct loption * 635 findopt_name(p_optname, p_oname, p_err) 636 char **p_optname; 637 char **p_oname; 638 int *p_err; 639 { 640 char *optname = *p_optname; 641 struct loption *o; 642 struct optname *oname; 643 int len; 644 int uppercase; 645 struct loption *maxo = NULL; 646 struct optname *maxoname = NULL; 647 int maxlen = 0; 648 int ambig = 0; 649 int exact = 0; 650 651 /* 652 * Check all options. 653 */ 654 for (o = option; o->oletter != '\0'; o++) 655 { 656 /* 657 * Check all names for this option. 658 */ 659 for (oname = o->onames; oname != NULL; oname = oname->onext) 660 { 661 /* 662 * Try normal match first (uppercase == 0), 663 * then, then if it's a TRIPLE option, 664 * try uppercase match (uppercase == 1). 665 */ 666 for (uppercase = 0; uppercase <= 1; uppercase++) 667 { 668 len = sprefix(optname, oname->oname, uppercase); 669 if (len <= 0 || is_optchar(optname[len])) 670 { 671 /* 672 * We didn't use all of the option name. 673 */ 674 continue; 675 } 676 if (!exact && len == maxlen) 677 /* 678 * Already had a partial match, 679 * and now there's another one that 680 * matches the same length. 681 */ 682 ambig = 1; 683 else if (len > maxlen) 684 { 685 /* 686 * Found a better match than 687 * the one we had. 688 */ 689 maxo = o; 690 maxoname = oname; 691 maxlen = len; 692 ambig = 0; 693 exact = (len == (int)strlen(oname->oname)); 694 } 695 if (!(o->otype & TRIPLE)) 696 break; 697 } 698 } 699 } 700 if (ambig) 701 { 702 /* 703 * Name matched more than one option. 704 */ 705 if (p_err != NULL) 706 *p_err = OPT_AMBIG; 707 return (NULL); 708 } 709 *p_optname = optname + maxlen; 710 if (p_oname != NULL) 711 *p_oname = maxoname == NULL ? NULL : maxoname->oname; 712 return (maxo); 713 } 714