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