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