2 * Copyright (C) 1984-2017 Mark Nudelman
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.
7 * For more information, see the README file.
12 * Process command line options.
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.
23 static struct loption *pendopt;
24 public int plusoption = FALSE;
26 static char *optstring();
27 static int flip_triple();
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;
36 * Return a printable description of an option.
42 static char buf[OPTNAME_MAX + 10];
43 if (o->oletter == OLETTER_NONE)
44 SNPRINTF1(buf, sizeof(buf), "--%s", o->onames->oname);
46 SNPRINTF2(buf, sizeof(buf), "-%c (--%s)", o->oletter, o->onames->oname);
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".
60 sprintf(buf, "-%s", prchar(c));
65 * Scan an argument (either from the command line or from the
66 * LESS environment variable) and process it.
86 * If we have a pending option which requires an argument,
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.
94 switch (pendopt->otype & OTYPE)
97 (*pendopt->ofunc)(INIT, s);
100 printopt = opt_desc(pendopt);
101 *(pendopt->ovar) = getnum(&s, printopt, (int*)NULL);
114 * Check some special cases first.
120 case END_OPTION_STRING:
124 * "--" indicates an option name instead of a letter.
132 * "-+" means set these options back to their defaults.
133 * (They may have been set otherwise by previous
136 set_default = (*s == '+');
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
149 s = optstring(s, &str, propt('+'), NULL);
153 every_first_cmd = save(str+1);
156 ungetcc(CHAR_END_COMMAND);
161 case '0': case '1': case '2': case '3': case '4':
162 case '5': case '6': case '7': case '8': case '9':
164 * Special "more" compatibility form "-<number>"
165 * instead of -z<number> to set the scrolling
178 * Not a special case.
179 * Look up the option letter in the option table.
184 printopt = propt(optc);
185 lc = ASCII_IS_LOWER(optc);
190 lc = ASCII_IS_LOWER(optname[0]);
191 o = findopt_name(&optname, NULL, &err);
194 if (*s == '\0' || *s == ' ')
197 * The option name matches exactly.
200 } else if (*s == '=')
203 * The option name is followed by "=value".
206 (o->otype & OTYPE) != STRING &&
207 (o->otype & OTYPE) != NUMBER)
209 parg.p_string = printopt;
210 error("The %s option should not be followed by =",
218 * The specified name is longer than the
226 parg.p_string = printopt;
227 if (err == OPT_AMBIG)
228 error("%s is an ambiguous abbreviation (\"less --help\" for help)",
231 error("There is no %s option (\"less --help\" for help)",
237 switch (o->otype & OTYPE)
241 *(o->ovar) = o->odefault;
243 *(o->ovar) = ! o->odefault;
247 *(o->ovar) = o->odefault;
249 *(o->ovar) = flip_triple(o->odefault, lc);
255 * Set pendopt and return.
256 * We will get the string next time
257 * scan_option is called.
263 * Don't do anything here.
264 * All processing of STRING options is done by
265 * the handling function.
269 s = optstring(s, &str, printopt, o->odesc[1]);
279 *(o->ovar) = getnum(&s, printopt, (int*)NULL);
283 * If the option has a handling function, call it.
285 if (o->ofunc != NULL)
286 (*o->ofunc)(INIT, str);
293 * Toggle command line flags from within the program.
294 * Used by the "-" and "_" commands.
296 * OPT_NO_TOGGLE just report the current setting, without changing it.
297 * OPT_TOGGLE invert the current setting
298 * OPT_UNSET set to the default value
299 * OPT_SET set to the inverse of the default value
302 toggle_option(o, lower, s, how_toggle)
313 no_prompt = (how_toggle & OPT_NO_PROMPT);
314 how_toggle &= ~OPT_NO_PROMPT;
318 error("No such option", NULL_PARG);
322 if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE))
324 parg.p_string = opt_desc(o);
325 error("Cannot change the %s option", &parg);
329 if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY))
331 parg.p_string = opt_desc(o);
332 error("Cannot query the %s option", &parg);
337 * Check for something which appears to be a do_toggle
338 * (because the "-" command was used), but really is not.
339 * This could be a string option with no string, or
340 * a number option with no number.
342 switch (o->otype & OTYPE)
346 if (how_toggle == OPT_TOGGLE && *s == '\0')
347 how_toggle = OPT_NO_TOGGLE;
352 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
357 * Now actually toggle (change) the variable.
359 if (how_toggle != OPT_NO_TOGGLE)
361 switch (o->otype & OTYPE)
370 *(o->ovar) = ! *(o->ovar);
373 *(o->ovar) = o->odefault;
376 *(o->ovar) = ! o->odefault;
383 * If user gave the lower case letter, then switch
384 * to 1 unless already 1, in which case make it 0.
385 * If user gave the upper case letter, then switch
386 * to 2 unless already 2, in which case make it 0.
391 *(o->ovar) = flip_triple(*(o->ovar), lower);
394 *(o->ovar) = o->odefault;
397 *(o->ovar) = flip_triple(o->odefault, lower);
403 * String: don't do anything here.
404 * The handling function will do everything.
410 error("Cannot use \"-+\" or \"--\" for a string option",
417 * Number: set the variable to the given number.
422 num = getnum(&s, NULL, &err);
427 *(o->ovar) = o->odefault;
430 error("Can't use \"-!\" for a numeric option",
439 * Call the handling function for any special action
440 * specific to this option.
442 if (o->ofunc != NULL)
443 (*o->ofunc)((how_toggle==OPT_NO_TOGGLE) ? QUERY : TOGGLE, s);
446 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
453 * Print a message describing the new setting.
455 switch (o->otype & OTYPE)
460 * Print the odesc message.
462 error(o->odesc[*(o->ovar)], NULL_PARG);
466 * The message is in odesc[1] and has a %d for
467 * the value of the variable.
469 parg.p_int = *(o->ovar);
470 error(o->odesc[1], &parg);
474 * Message was already printed by the handling function.
480 if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
481 screen_trashed = TRUE;
485 * "Toggle" a triple-valued option.
493 return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
495 return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
499 * Determine if an option takes a parameter.
507 if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE))
513 * Return the prompt to be used for a given option letter.
514 * Only string and number valued options have prompts.
520 if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
522 return (o->odesc[0]);
526 * Return whether or not there is a string option pending;
527 * that is, if the previous option was a string-valued option letter
528 * (like -P) without a following string.
529 * In that case, the current option is taken to be the string for
530 * the previous option.
535 return (pendopt != NULL);
539 * Print error message about missing string.
546 parg.p_string = printopt;
547 error("Value is required after %s", &parg);
551 * Print error message if a STRING type option is not followed by a string.
556 nostring(opt_desc(pendopt));
560 * Scan to end of string or to an END_OPTION_STRING character.
561 * In the latter case, replace the char with a null char.
562 * Return a pointer to the remainder of the string, if any.
565 optstring(s, p_str, printopt, validchars)
579 /* Alloc could be more than needed, but not worth trimming. */
580 *p_str = (char *) ecalloc(strlen(s)+1, sizeof(char));
583 for (p = s; *p != '\0'; p++)
585 if (opt_use_backslash && *p == '\\' && p[1] != '\0')
587 /* Take next char literally. */
591 if (*p == END_OPTION_STRING ||
592 (validchars != NULL && strchr(validchars, *p) == NULL))
593 /* End of option string. */
605 num_error(printopt, errp)
616 if (printopt != NULL)
618 parg.p_string = printopt;
619 error("Number is required after %s", &parg);
625 * Translate a string into a number.
626 * Like atoi(), but takes a pointer to a char *, and updates
627 * the char * to point after the translated number.
630 getnum(sp, printopt, errp)
646 if (*s < '0' || *s > '9')
647 return (num_error(printopt, errp));
650 while (*s >= '0' && *s <= '9')
651 n = 10 * n + *s++ - '0';
661 * Translate a string into a fraction, represented by the part of a
662 * number which would follow a decimal point.
663 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
664 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
667 getfraction(sp, printopt, errp)
677 if (*s < '0' || *s > '9')
678 return (num_error(printopt, errp));
680 for ( ; *s >= '0' && *s <= '9'; s++)
682 frac = (frac * 10) + (*s - '0');
685 if (fraclen > NUM_LOG_FRAC_DENOM)
686 while (fraclen-- > NUM_LOG_FRAC_DENOM)
689 while (fraclen++ < NUM_LOG_FRAC_DENOM)
699 * Get the value of the -e flag.
706 /* When less_is_more is set, the -e flag semantics are different. */
707 return quit_at_eof ? OPT_ONPLUS : OPT_ON;