1 /* 2 * Copyright (C) 1984-2019 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 * Process command line options. 13 * 14 * Each option is a single letter which controls a program variable. 15 * The options have defaults which may be changed via 16 * the command line option, toggled via the "-" command, 17 * or queried via the "_" command. 18 */ 19 20 #include "less.h" 21 #include "option.h" 22 23 static struct loption *pendopt; 24 public int plusoption = FALSE; 25 26 static char *optstring(); 27 static int flip_triple(); 28 29 extern int screen_trashed; 30 extern int less_is_more; 31 extern int quit_at_eof; 32 extern char *every_first_cmd; 33 extern int opt_use_backslash; 34 35 /* 36 * Return a printable description of an option. 37 */ 38 static char * 39 opt_desc(o) 40 struct loption *o; 41 { 42 static char buf[OPTNAME_MAX + 10]; 43 if (o->oletter == OLETTER_NONE) 44 SNPRINTF1(buf, sizeof(buf), "--%s", o->onames->oname); 45 else 46 SNPRINTF2(buf, sizeof(buf), "-%c (--%s)", o->oletter, o->onames->oname); 47 return (buf); 48 } 49 50 /* 51 * Return a string suitable for printing as the "name" of an option. 52 * For example, if the option letter is 'x', just return "-x". 53 */ 54 public char * 55 propt(c) 56 int c; 57 { 58 static char buf[8]; 59 60 sprintf(buf, "-%s", prchar(c)); 61 return (buf); 62 } 63 64 /* 65 * Scan an argument (either from the command line or from the 66 * LESS environment variable) and process it. 67 */ 68 public void 69 scan_option(s) 70 char *s; 71 { 72 struct loption *o; 73 int optc; 74 char *optname; 75 char *printopt; 76 char *str; 77 int set_default; 78 int lc; 79 int err; 80 PARG parg; 81 82 if (s == NULL) 83 return; 84 85 /* 86 * If we have a pending option which requires an argument, 87 * handle it now. 88 * This happens if the previous option was, for example, "-P" 89 * without a following string. In that case, the current 90 * option is simply the argument for the previous option. 91 */ 92 if (pendopt != NULL) 93 { 94 switch (pendopt->otype & OTYPE) 95 { 96 case STRING: 97 (*pendopt->ofunc)(INIT, s); 98 break; 99 case NUMBER: 100 printopt = opt_desc(pendopt); 101 *(pendopt->ovar) = getnum(&s, printopt, (int*)NULL); 102 break; 103 } 104 pendopt = NULL; 105 return; 106 } 107 108 set_default = FALSE; 109 optname = NULL; 110 111 while (*s != '\0') 112 { 113 /* 114 * Check some special cases first. 115 */ 116 switch (optc = *s++) 117 { 118 case ' ': 119 case '\t': 120 case END_OPTION_STRING: 121 continue; 122 case '-': 123 /* 124 * "--" indicates an option name instead of a letter. 125 */ 126 if (*s == '-') 127 { 128 optname = ++s; 129 break; 130 } 131 /* 132 * "-+" means set these options back to their defaults. 133 * (They may have been set otherwise by previous 134 * options.) 135 */ 136 set_default = (*s == '+'); 137 if (set_default) 138 s++; 139 continue; 140 case '+': 141 /* 142 * An option prefixed by a "+" is ungotten, so 143 * that it is interpreted as less commands 144 * processed at the start of the first input file. 145 * "++" means process the commands at the start of 146 * EVERY input file. 147 */ 148 plusoption = TRUE; 149 s = optstring(s, &str, propt('+'), NULL); 150 if (s == NULL) 151 return; 152 if (*str == '+') 153 { 154 if (every_first_cmd != NULL) 155 free(every_first_cmd); 156 every_first_cmd = save(str+1); 157 } else 158 { 159 ungetcc(CHAR_END_COMMAND); 160 ungetsc(str); 161 } 162 free(str); 163 continue; 164 case '0': case '1': case '2': case '3': case '4': 165 case '5': case '6': case '7': case '8': case '9': 166 /* 167 * Special "more" compatibility form "-<number>" 168 * instead of -z<number> to set the scrolling 169 * window size. 170 */ 171 s--; 172 optc = 'z'; 173 break; 174 case 'n': 175 if (less_is_more) 176 optc = 'z'; 177 break; 178 } 179 180 /* 181 * Not a special case. 182 * Look up the option letter in the option table. 183 */ 184 err = 0; 185 if (optname == NULL) 186 { 187 printopt = propt(optc); 188 lc = ASCII_IS_LOWER(optc); 189 o = findopt(optc); 190 } else 191 { 192 printopt = optname; 193 lc = ASCII_IS_LOWER(optname[0]); 194 o = findopt_name(&optname, NULL, &err); 195 s = optname; 196 optname = NULL; 197 if (*s == '\0' || *s == ' ') 198 { 199 /* 200 * The option name matches exactly. 201 */ 202 ; 203 } else if (*s == '=') 204 { 205 /* 206 * The option name is followed by "=value". 207 */ 208 if (o != NULL && 209 (o->otype & OTYPE) != STRING && 210 (o->otype & OTYPE) != NUMBER) 211 { 212 parg.p_string = printopt; 213 error("The %s option should not be followed by =", 214 &parg); 215 return; 216 } 217 s++; 218 } else 219 { 220 /* 221 * The specified name is longer than the 222 * real option name. 223 */ 224 o = NULL; 225 } 226 } 227 if (o == NULL) 228 { 229 parg.p_string = printopt; 230 if (err == OPT_AMBIG) 231 error("%s is an ambiguous abbreviation (\"less --help\" for help)", 232 &parg); 233 else 234 error("There is no %s option (\"less --help\" for help)", 235 &parg); 236 return; 237 } 238 239 str = NULL; 240 switch (o->otype & OTYPE) 241 { 242 case BOOL: 243 if (set_default) 244 *(o->ovar) = o->odefault; 245 else 246 *(o->ovar) = ! o->odefault; 247 break; 248 case TRIPLE: 249 if (set_default) 250 *(o->ovar) = o->odefault; 251 else 252 *(o->ovar) = flip_triple(o->odefault, lc); 253 break; 254 case STRING: 255 if (*s == '\0') 256 { 257 /* 258 * Set pendopt and return. 259 * We will get the string next time 260 * scan_option is called. 261 */ 262 pendopt = o; 263 return; 264 } 265 /* 266 * Don't do anything here. 267 * All processing of STRING options is done by 268 * the handling function. 269 */ 270 while (*s == ' ') 271 s++; 272 s = optstring(s, &str, printopt, o->odesc[1]); 273 if (s == NULL) 274 return; 275 break; 276 case NUMBER: 277 if (*s == '\0') 278 { 279 pendopt = o; 280 return; 281 } 282 *(o->ovar) = getnum(&s, printopt, (int*)NULL); 283 break; 284 } 285 /* 286 * If the option has a handling function, call it. 287 */ 288 if (o->ofunc != NULL) 289 (*o->ofunc)(INIT, str); 290 if (str != NULL) 291 free(str); 292 } 293 } 294 295 /* 296 * Toggle command line flags from within the program. 297 * Used by the "-" and "_" commands. 298 * how_toggle may be: 299 * OPT_NO_TOGGLE just report the current setting, without changing it. 300 * OPT_TOGGLE invert the current setting 301 * OPT_UNSET set to the default value 302 * OPT_SET set to the inverse of the default value 303 */ 304 public void 305 toggle_option(o, lower, s, how_toggle) 306 struct loption *o; 307 int lower; 308 char *s; 309 int how_toggle; 310 { 311 int num; 312 int no_prompt; 313 int err; 314 PARG parg; 315 316 no_prompt = (how_toggle & OPT_NO_PROMPT); 317 how_toggle &= ~OPT_NO_PROMPT; 318 319 if (o == NULL) 320 { 321 error("No such option", NULL_PARG); 322 return; 323 } 324 325 if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE)) 326 { 327 parg.p_string = opt_desc(o); 328 error("Cannot change the %s option", &parg); 329 return; 330 } 331 332 if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY)) 333 { 334 parg.p_string = opt_desc(o); 335 error("Cannot query the %s option", &parg); 336 return; 337 } 338 339 /* 340 * Check for something which appears to be a do_toggle 341 * (because the "-" command was used), but really is not. 342 * This could be a string option with no string, or 343 * a number option with no number. 344 */ 345 switch (o->otype & OTYPE) 346 { 347 case STRING: 348 case NUMBER: 349 if (how_toggle == OPT_TOGGLE && *s == '\0') 350 how_toggle = OPT_NO_TOGGLE; 351 break; 352 } 353 354 #if HILITE_SEARCH 355 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT)) 356 repaint_hilite(0); 357 #endif 358 359 /* 360 * Now actually toggle (change) the variable. 361 */ 362 if (how_toggle != OPT_NO_TOGGLE) 363 { 364 switch (o->otype & OTYPE) 365 { 366 case BOOL: 367 /* 368 * Boolean. 369 */ 370 switch (how_toggle) 371 { 372 case OPT_TOGGLE: 373 *(o->ovar) = ! *(o->ovar); 374 break; 375 case OPT_UNSET: 376 *(o->ovar) = o->odefault; 377 break; 378 case OPT_SET: 379 *(o->ovar) = ! o->odefault; 380 break; 381 } 382 break; 383 case TRIPLE: 384 /* 385 * Triple: 386 * If user gave the lower case letter, then switch 387 * to 1 unless already 1, in which case make it 0. 388 * If user gave the upper case letter, then switch 389 * to 2 unless already 2, in which case make it 0. 390 */ 391 switch (how_toggle) 392 { 393 case OPT_TOGGLE: 394 *(o->ovar) = flip_triple(*(o->ovar), lower); 395 break; 396 case OPT_UNSET: 397 *(o->ovar) = o->odefault; 398 break; 399 case OPT_SET: 400 *(o->ovar) = flip_triple(o->odefault, lower); 401 break; 402 } 403 break; 404 case STRING: 405 /* 406 * String: don't do anything here. 407 * The handling function will do everything. 408 */ 409 switch (how_toggle) 410 { 411 case OPT_SET: 412 case OPT_UNSET: 413 error("Cannot use \"-+\" or \"--\" for a string option", 414 NULL_PARG); 415 return; 416 } 417 break; 418 case NUMBER: 419 /* 420 * Number: set the variable to the given number. 421 */ 422 switch (how_toggle) 423 { 424 case OPT_TOGGLE: 425 num = getnum(&s, NULL, &err); 426 if (!err) 427 *(o->ovar) = num; 428 break; 429 case OPT_UNSET: 430 *(o->ovar) = o->odefault; 431 break; 432 case OPT_SET: 433 error("Can't use \"-!\" for a numeric option", 434 NULL_PARG); 435 return; 436 } 437 break; 438 } 439 } 440 441 /* 442 * Call the handling function for any special action 443 * specific to this option. 444 */ 445 if (o->ofunc != NULL) 446 (*o->ofunc)((how_toggle==OPT_NO_TOGGLE) ? QUERY : TOGGLE, s); 447 448 #if HILITE_SEARCH 449 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT)) 450 chg_hilite(); 451 #endif 452 453 if (!no_prompt) 454 { 455 /* 456 * Print a message describing the new setting. 457 */ 458 switch (o->otype & OTYPE) 459 { 460 case BOOL: 461 case TRIPLE: 462 /* 463 * Print the odesc message. 464 */ 465 error(o->odesc[*(o->ovar)], NULL_PARG); 466 break; 467 case NUMBER: 468 /* 469 * The message is in odesc[1] and has a %d for 470 * the value of the variable. 471 */ 472 parg.p_int = *(o->ovar); 473 error(o->odesc[1], &parg); 474 break; 475 case STRING: 476 /* 477 * Message was already printed by the handling function. 478 */ 479 break; 480 } 481 } 482 483 if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT)) 484 screen_trashed = TRUE; 485 } 486 487 /* 488 * "Toggle" a triple-valued option. 489 */ 490 static int 491 flip_triple(val, lc) 492 int val; 493 int lc; 494 { 495 if (lc) 496 return ((val == OPT_ON) ? OPT_OFF : OPT_ON); 497 else 498 return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS); 499 } 500 501 /* 502 * Determine if an option takes a parameter. 503 */ 504 public int 505 opt_has_param(o) 506 struct loption *o; 507 { 508 if (o == NULL) 509 return (0); 510 if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE)) 511 return (0); 512 return (1); 513 } 514 515 /* 516 * Return the prompt to be used for a given option letter. 517 * Only string and number valued options have prompts. 518 */ 519 public char * 520 opt_prompt(o) 521 struct loption *o; 522 { 523 if (o == NULL || (o->otype & (STRING|NUMBER)) == 0) 524 return ("?"); 525 return (o->odesc[0]); 526 } 527 528 /* 529 * Return whether or not there is a string option pending; 530 * that is, if the previous option was a string-valued option letter 531 * (like -P) without a following string. 532 * In that case, the current option is taken to be the string for 533 * the previous option. 534 */ 535 public int 536 isoptpending(VOID_PARAM) 537 { 538 return (pendopt != NULL); 539 } 540 541 /* 542 * Print error message about missing string. 543 */ 544 static void 545 nostring(printopt) 546 char *printopt; 547 { 548 PARG parg; 549 parg.p_string = printopt; 550 error("Value is required after %s", &parg); 551 } 552 553 /* 554 * Print error message if a STRING type option is not followed by a string. 555 */ 556 public void 557 nopendopt(VOID_PARAM) 558 { 559 nostring(opt_desc(pendopt)); 560 } 561 562 /* 563 * Scan to end of string or to an END_OPTION_STRING character. 564 * In the latter case, replace the char with a null char. 565 * Return a pointer to the remainder of the string, if any. 566 */ 567 static char * 568 optstring(s, p_str, printopt, validchars) 569 char *s; 570 char **p_str; 571 char *printopt; 572 char *validchars; 573 { 574 char *p; 575 char *out; 576 577 if (*s == '\0') 578 { 579 nostring(printopt); 580 return (NULL); 581 } 582 /* Alloc could be more than needed, but not worth trimming. */ 583 *p_str = (char *) ecalloc(strlen(s)+1, sizeof(char)); 584 out = *p_str; 585 586 for (p = s; *p != '\0'; p++) 587 { 588 if (opt_use_backslash && *p == '\\' && p[1] != '\0') 589 { 590 /* Take next char literally. */ 591 ++p; 592 } else 593 { 594 if (*p == END_OPTION_STRING || 595 (validchars != NULL && strchr(validchars, *p) == NULL)) 596 /* End of option string. */ 597 break; 598 } 599 *out++ = *p; 600 } 601 *out = '\0'; 602 return (p); 603 } 604 605 /* 606 */ 607 static int 608 num_error(printopt, errp) 609 char *printopt; 610 int *errp; 611 { 612 PARG parg; 613 614 if (errp != NULL) 615 { 616 *errp = TRUE; 617 return (-1); 618 } 619 if (printopt != NULL) 620 { 621 parg.p_string = printopt; 622 error("Number is required after %s", &parg); 623 } 624 return (-1); 625 } 626 627 /* 628 * Translate a string into a number. 629 * Like atoi(), but takes a pointer to a char *, and updates 630 * the char * to point after the translated number. 631 */ 632 public int 633 getnum(sp, printopt, errp) 634 char **sp; 635 char *printopt; 636 int *errp; 637 { 638 char *s; 639 int n; 640 int neg; 641 642 s = skipsp(*sp); 643 neg = FALSE; 644 if (*s == '-') 645 { 646 neg = TRUE; 647 s++; 648 } 649 if (*s < '0' || *s > '9') 650 return (num_error(printopt, errp)); 651 652 n = 0; 653 while (*s >= '0' && *s <= '9') 654 n = 10 * n + *s++ - '0'; 655 *sp = s; 656 if (errp != NULL) 657 *errp = FALSE; 658 if (neg) 659 n = -n; 660 return (n); 661 } 662 663 /* 664 * Translate a string into a fraction, represented by the part of a 665 * number which would follow a decimal point. 666 * The value of the fraction is returned as parts per NUM_FRAC_DENOM. 667 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM. 668 */ 669 public long 670 getfraction(sp, printopt, errp) 671 char **sp; 672 char *printopt; 673 int *errp; 674 { 675 char *s; 676 long frac = 0; 677 int fraclen = 0; 678 679 s = skipsp(*sp); 680 if (*s < '0' || *s > '9') 681 return (num_error(printopt, errp)); 682 683 for ( ; *s >= '0' && *s <= '9'; s++) 684 { 685 frac = (frac * 10) + (*s - '0'); 686 fraclen++; 687 } 688 if (fraclen > NUM_LOG_FRAC_DENOM) 689 while (fraclen-- > NUM_LOG_FRAC_DENOM) 690 frac /= 10; 691 else 692 while (fraclen++ < NUM_LOG_FRAC_DENOM) 693 frac *= 10; 694 *sp = s; 695 if (errp != NULL) 696 *errp = FALSE; 697 return (frac); 698 } 699 700 701 /* 702 * Get the value of the -e flag. 703 */ 704 public int 705 get_quit_at_eof(VOID_PARAM) 706 { 707 if (!less_is_more) 708 return quit_at_eof; 709 /* When less_is_more is set, the -e flag semantics are different. */ 710 return quit_at_eof ? OPT_ONPLUS : OPT_ON; 711 } 712