2 * Copyright (C) 1984-2020 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 LESSPARAMS((char *s, char **p_str, char *printopt,
28 static int flip_triple LESSPARAMS((int val, int lc));
30 extern int screen_trashed;
31 extern int less_is_more;
32 extern int quit_at_eof;
33 extern char *every_first_cmd;
34 extern int opt_use_backslash;
37 * Return a printable description of an option.
43 static char buf[OPTNAME_MAX + 10];
44 if (o->oletter == OLETTER_NONE)
45 SNPRINTF1(buf, sizeof(buf), "--%s", o->onames->oname);
47 SNPRINTF2(buf, sizeof(buf), "-%c (--%s)", o->oletter, o->onames->oname);
52 * Return a string suitable for printing as the "name" of an option.
53 * For example, if the option letter is 'x', just return "-x".
61 sprintf(buf, "-%s", prchar(c));
66 * Scan an argument (either from the command line or from the
67 * LESS environment variable) and process it.
87 * If we have a pending option which requires an argument,
89 * This happens if the previous option was, for example, "-P"
90 * without a following string. In that case, the current
91 * option is simply the argument for the previous option.
95 switch (pendopt->otype & OTYPE)
98 (*pendopt->ofunc)(INIT, s);
101 printopt = opt_desc(pendopt);
102 *(pendopt->ovar) = getnum(&s, printopt, (int*)NULL);
115 * Check some special cases first.
121 case END_OPTION_STRING:
125 * "--" indicates an option name instead of a letter.
133 * "-+" means set these options back to their defaults.
134 * (They may have been set otherwise by previous
137 set_default = (*s == '+');
143 * An option prefixed by a "+" is ungotten, so
144 * that it is interpreted as less commands
145 * processed at the start of the first input file.
146 * "++" means process the commands at the start of
150 s = optstring(s, &str, propt('+'), NULL);
155 if (every_first_cmd != NULL)
156 free(every_first_cmd);
157 every_first_cmd = save(str+1);
160 ungetcc(CHAR_END_COMMAND);
165 case '0': case '1': case '2': case '3': case '4':
166 case '5': case '6': case '7': case '8': case '9':
168 * Special "more" compatibility form "-<number>"
169 * instead of -z<number> to set the scrolling
182 * Not a special case.
183 * Look up the option letter in the option table.
188 printopt = propt(optc);
189 lc = ASCII_IS_LOWER(optc);
194 lc = ASCII_IS_LOWER(optname[0]);
195 o = findopt_name(&optname, NULL, &err);
198 if (*s == '\0' || *s == ' ')
201 * The option name matches exactly.
204 } else if (*s == '=')
207 * The option name is followed by "=value".
210 (o->otype & OTYPE) != STRING &&
211 (o->otype & OTYPE) != NUMBER)
213 parg.p_string = printopt;
214 error("The %s option should not be followed by =",
222 * The specified name is longer than the
230 parg.p_string = printopt;
231 if (err == OPT_AMBIG)
232 error("%s is an ambiguous abbreviation (\"less --help\" for help)",
235 error("There is no %s option (\"less --help\" for help)",
241 switch (o->otype & OTYPE)
245 *(o->ovar) = o->odefault;
247 *(o->ovar) = ! o->odefault;
251 *(o->ovar) = o->odefault;
253 *(o->ovar) = flip_triple(o->odefault, lc);
259 * Set pendopt and return.
260 * We will get the string next time
261 * scan_option is called.
267 * Don't do anything here.
268 * All processing of STRING options is done by
269 * the handling function.
273 s = optstring(s, &str, printopt, o->odesc[1]);
283 *(o->ovar) = getnum(&s, printopt, (int*)NULL);
287 * If the option has a handling function, call it.
289 if (o->ofunc != NULL)
290 (*o->ofunc)(INIT, str);
297 * Toggle command line flags from within the program.
298 * Used by the "-" and "_" commands.
300 * OPT_NO_TOGGLE just report the current setting, without changing it.
301 * OPT_TOGGLE invert the current setting
302 * OPT_UNSET set to the default value
303 * OPT_SET set to the inverse of the default value
306 toggle_option(o, lower, s, how_toggle)
317 no_prompt = (how_toggle & OPT_NO_PROMPT);
318 how_toggle &= ~OPT_NO_PROMPT;
322 error("No such option", NULL_PARG);
326 if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE))
328 parg.p_string = opt_desc(o);
329 error("Cannot change the %s option", &parg);
333 if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY))
335 parg.p_string = opt_desc(o);
336 error("Cannot query the %s option", &parg);
341 * Check for something which appears to be a do_toggle
342 * (because the "-" command was used), but really is not.
343 * This could be a string option with no string, or
344 * a number option with no number.
346 switch (o->otype & OTYPE)
350 if (how_toggle == OPT_TOGGLE && *s == '\0')
351 how_toggle = OPT_NO_TOGGLE;
356 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
361 * Now actually toggle (change) the variable.
363 if (how_toggle != OPT_NO_TOGGLE)
365 switch (o->otype & OTYPE)
374 *(o->ovar) = ! *(o->ovar);
377 *(o->ovar) = o->odefault;
380 *(o->ovar) = ! o->odefault;
387 * If user gave the lower case letter, then switch
388 * to 1 unless already 1, in which case make it 0.
389 * If user gave the upper case letter, then switch
390 * to 2 unless already 2, in which case make it 0.
395 *(o->ovar) = flip_triple(*(o->ovar), lower);
398 *(o->ovar) = o->odefault;
401 *(o->ovar) = flip_triple(o->odefault, lower);
407 * String: don't do anything here.
408 * The handling function will do everything.
414 error("Cannot use \"-+\" or \"--\" for a string option",
421 * Number: set the variable to the given number.
426 num = getnum(&s, NULL, &err);
431 *(o->ovar) = o->odefault;
434 error("Can't use \"-!\" for a numeric option",
443 * Call the handling function for any special action
444 * specific to this option.
446 if (o->ofunc != NULL)
447 (*o->ofunc)((how_toggle==OPT_NO_TOGGLE) ? QUERY : TOGGLE, s);
450 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
457 * Print a message describing the new setting.
459 switch (o->otype & OTYPE)
464 * Print the odesc message.
466 error(o->odesc[*(o->ovar)], NULL_PARG);
470 * The message is in odesc[1] and has a %d for
471 * the value of the variable.
473 parg.p_int = *(o->ovar);
474 error(o->odesc[1], &parg);
478 * Message was already printed by the handling function.
484 if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
485 screen_trashed = TRUE;
489 * "Toggle" a triple-valued option.
497 return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
499 return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
503 * Determine if an option takes a parameter.
511 if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE))
517 * Return the prompt to be used for a given option letter.
518 * Only string and number valued options have prompts.
524 if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
526 return (o->odesc[0]);
530 * If the specified option can be toggled, return NULL.
531 * Otherwise return an appropriate error message.
534 opt_toggle_disallowed(c)
540 if (ch_getflags() & CH_CANSEEK)
541 return "Input is not a pipe";
548 * Return whether or not there is a string option pending;
549 * that is, if the previous option was a string-valued option letter
550 * (like -P) without a following string.
551 * In that case, the current option is taken to be the string for
552 * the previous option.
555 isoptpending(VOID_PARAM)
557 return (pendopt != NULL);
561 * Print error message about missing string.
568 parg.p_string = printopt;
569 error("Value is required after %s", &parg);
573 * Print error message if a STRING type option is not followed by a string.
576 nopendopt(VOID_PARAM)
578 nostring(opt_desc(pendopt));
582 * Scan to end of string or to an END_OPTION_STRING character.
583 * In the latter case, replace the char with a null char.
584 * Return a pointer to the remainder of the string, if any.
587 optstring(s, p_str, printopt, validchars)
601 /* Alloc could be more than needed, but not worth trimming. */
602 *p_str = (char *) ecalloc(strlen(s)+1, sizeof(char));
605 for (p = s; *p != '\0'; p++)
607 if (opt_use_backslash && *p == '\\' && p[1] != '\0')
609 /* Take next char literally. */
613 if (*p == END_OPTION_STRING ||
614 (validchars != NULL && strchr(validchars, *p) == NULL))
615 /* End of option string. */
627 num_error(printopt, errp)
638 if (printopt != NULL)
640 parg.p_string = printopt;
641 error("Number is required after %s", &parg);
647 * Translate a string into a number.
648 * Like atoi(), but takes a pointer to a char *, and updates
649 * the char * to point after the translated number.
652 getnum(sp, printopt, errp)
668 if (*s < '0' || *s > '9')
669 return (num_error(printopt, errp));
672 while (*s >= '0' && *s <= '9')
673 n = 10 * n + *s++ - '0';
683 * Translate a string into a fraction, represented by the part of a
684 * number which would follow a decimal point.
685 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
686 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
689 getfraction(sp, printopt, errp)
699 if (*s < '0' || *s > '9')
700 return (num_error(printopt, errp));
702 for ( ; *s >= '0' && *s <= '9'; s++)
704 frac = (frac * 10) + (*s - '0');
707 if (fraclen > NUM_LOG_FRAC_DENOM)
708 while (fraclen-- > NUM_LOG_FRAC_DENOM)
711 while (fraclen++ < NUM_LOG_FRAC_DENOM)
721 * Get the value of the -e flag.
724 get_quit_at_eof(VOID_PARAM)
728 /* When less_is_more is set, the -e flag semantics are different. */
729 return quit_at_eof ? OPT_ONPLUS : OPT_ON;