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