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