2 * Copyright (C) 1984-2015 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(char *s, char **p_str, char *printopt, char *validchars);
27 static int flip_triple(int val, int lc);
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.
39 opt_desc(struct loption *o)
41 static char buf[OPTNAME_MAX + 10];
42 if (o->oletter == OLETTER_NONE)
43 SNPRINTF1(buf, sizeof(buf), "--%s", o->onames->oname);
45 SNPRINTF2(buf, sizeof(buf), "-%c (--%s)", o->oletter, o->onames->oname);
50 * Return a string suitable for printing as the "name" of an option.
51 * For example, if the option letter is 'x', just return "-x".
58 sprintf(buf, "-%s", prchar(c));
63 * Scan an argument (either from the command line or from the
64 * LESS environment variable) and process it.
83 * If we have a pending option which requires an argument,
85 * This happens if the previous option was, for example, "-P"
86 * without a following string. In that case, the current
87 * option is simply the argument for the previous option.
91 switch (pendopt->otype & OTYPE)
94 (*pendopt->ofunc)(INIT, s);
97 printopt = opt_desc(pendopt);
98 *(pendopt->ovar) = getnum(&s, printopt, (int*)NULL);
111 * Check some special cases first.
117 case END_OPTION_STRING:
121 * "--" indicates an option name instead of a letter.
129 * "-+" means set these options back to their defaults.
130 * (They may have been set otherwise by previous
133 set_default = (*s == '+');
139 * An option prefixed by a "+" is ungotten, so
140 * that it is interpreted as less commands
141 * processed at the start of the first input file.
142 * "++" means process the commands at the start of
146 s = optstring(s, &str, propt('+'), NULL);
150 every_first_cmd = save(str+1);
153 ungetcc(CHAR_END_COMMAND);
158 case '0': case '1': case '2': case '3': case '4':
159 case '5': case '6': case '7': case '8': case '9':
161 * Special "more" compatibility form "-<number>"
162 * instead of -z<number> to set the scrolling
175 * Not a special case.
176 * Look up the option letter in the option table.
181 printopt = propt(optc);
182 lc = ASCII_IS_LOWER(optc);
187 lc = ASCII_IS_LOWER(optname[0]);
188 o = findopt_name(&optname, NULL, &err);
191 if (*s == '\0' || *s == ' ')
194 * The option name matches exactly.
197 } else if (*s == '=')
200 * The option name is followed by "=value".
203 (o->otype & OTYPE) != STRING &&
204 (o->otype & OTYPE) != NUMBER)
206 parg.p_string = printopt;
207 error("The %s option should not be followed by =",
215 * The specified name is longer than the
223 parg.p_string = printopt;
224 if (err == OPT_AMBIG)
225 error("%s is an ambiguous abbreviation (\"less --help\" for help)",
228 error("There is no %s option (\"less --help\" for help)",
234 switch (o->otype & OTYPE)
238 *(o->ovar) = o->odefault;
240 *(o->ovar) = ! o->odefault;
244 *(o->ovar) = o->odefault;
246 *(o->ovar) = flip_triple(o->odefault, lc);
252 * Set pendopt and return.
253 * We will get the string next time
254 * scan_option is called.
260 * Don't do anything here.
261 * All processing of STRING options is done by
262 * the handling function.
266 s = optstring(s, &str, printopt, o->odesc[1]);
276 *(o->ovar) = getnum(&s, printopt, (int*)NULL);
280 * If the option has a handling function, call it.
282 if (o->ofunc != NULL)
283 (*o->ofunc)(INIT, str);
290 * Toggle command line flags from within the program.
291 * Used by the "-" and "_" commands.
293 * OPT_NO_TOGGLE just report the current setting, without changing it.
294 * OPT_TOGGLE invert the current setting
295 * OPT_UNSET set to the default value
296 * OPT_SET set to the inverse of the default value
299 toggle_option(struct loption *o, int lower, char *s, int how_toggle)
306 no_prompt = (how_toggle & OPT_NO_PROMPT);
307 how_toggle &= ~OPT_NO_PROMPT;
311 error("No such option", NULL_PARG);
315 if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE))
317 parg.p_string = opt_desc(o);
318 error("Cannot change the %s option", &parg);
322 if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY))
324 parg.p_string = opt_desc(o);
325 error("Cannot query the %s option", &parg);
330 * Check for something which appears to be a do_toggle
331 * (because the "-" command was used), but really is not.
332 * This could be a string option with no string, or
333 * a number option with no number.
335 switch (o->otype & OTYPE)
339 if (how_toggle == OPT_TOGGLE && *s == '\0')
340 how_toggle = OPT_NO_TOGGLE;
345 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
350 * Now actually toggle (change) the variable.
352 if (how_toggle != OPT_NO_TOGGLE)
354 switch (o->otype & OTYPE)
363 *(o->ovar) = ! *(o->ovar);
366 *(o->ovar) = o->odefault;
369 *(o->ovar) = ! o->odefault;
376 * If user gave the lower case letter, then switch
377 * to 1 unless already 1, in which case make it 0.
378 * If user gave the upper case letter, then switch
379 * to 2 unless already 2, in which case make it 0.
384 *(o->ovar) = flip_triple(*(o->ovar), lower);
387 *(o->ovar) = o->odefault;
390 *(o->ovar) = flip_triple(o->odefault, lower);
396 * String: don't do anything here.
397 * The handling function will do everything.
403 error("Cannot use \"-+\" or \"--\" for a string option",
410 * Number: set the variable to the given number.
415 num = getnum(&s, NULL, &err);
420 *(o->ovar) = o->odefault;
423 error("Can't use \"-!\" for a numeric option",
432 * Call the handling function for any special action
433 * specific to this option.
435 if (o->ofunc != NULL)
436 (*o->ofunc)((how_toggle==OPT_NO_TOGGLE) ? QUERY : TOGGLE, s);
439 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
446 * Print a message describing the new setting.
448 switch (o->otype & OTYPE)
453 * Print the odesc message.
455 error(o->odesc[*(o->ovar)], NULL_PARG);
459 * The message is in odesc[1] and has a %d for
460 * the value of the variable.
462 parg.p_int = *(o->ovar);
463 error(o->odesc[1], &parg);
467 * Message was already printed by the handling function.
473 if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
474 screen_trashed = TRUE;
478 * "Toggle" a triple-valued option.
481 flip_triple(int val, int lc)
484 return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
486 return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
490 * Determine if an option takes a parameter.
493 opt_has_param(struct loption *o)
497 if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE))
503 * Return the prompt to be used for a given option letter.
504 * Only string and number valued options have prompts.
507 opt_prompt(struct loption *o)
509 if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
511 return (o->odesc[0]);
515 * Return whether or not there is a string option pending;
516 * that is, if the previous option was a string-valued option letter
517 * (like -P) without a following string.
518 * In that case, the current option is taken to be the string for
519 * the previous option.
524 return (pendopt != NULL);
528 * Print error message about missing string.
531 nostring(char *printopt)
534 parg.p_string = printopt;
535 error("Value is required after %s", &parg);
539 * Print error message if a STRING type option is not followed by a string.
544 nostring(opt_desc(pendopt));
548 * Scan to end of string or to an END_OPTION_STRING character.
549 * In the latter case, replace the char with a null char.
550 * Return a pointer to the remainder of the string, if any.
553 optstring(char *s, char **p_str, char *printopt, char *validchars)
563 /* Alloc could be more than needed, but not worth trimming. */
564 *p_str = (char *) ecalloc(strlen(s)+1, sizeof(char));
567 for (p = s; *p != '\0'; p++)
569 if (opt_use_backslash && *p == '\\' && p[1] != '\0')
571 /* Take next char literally. */
575 if (*p == END_OPTION_STRING ||
576 (validchars != NULL && strchr(validchars, *p) == NULL))
577 /* End of option string. */
589 num_error(char *printopt, int *errp)
598 if (printopt != NULL)
600 parg.p_string = printopt;
601 error("Number is required after %s", &parg);
607 * Translate a string into a number.
608 * Like atoi(), but takes a pointer to a char *, and updates
609 * the char * to point after the translated number.
612 getnum(char **sp, char *printopt, int *errp)
625 if (*s < '0' || *s > '9')
626 return (num_error(printopt, errp));
629 while (*s >= '0' && *s <= '9')
630 n = 10 * n + *s++ - '0';
640 * Translate a string into a fraction, represented by the part of a
641 * number which would follow a decimal point.
642 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
643 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
646 getfraction(char **sp, char *printopt, int *errp)
653 if (*s < '0' || *s > '9')
654 return (num_error(printopt, errp));
656 for ( ; *s >= '0' && *s <= '9'; s++)
658 frac = (frac * 10) + (*s - '0');
661 if (fraclen > NUM_LOG_FRAC_DENOM)
662 while (fraclen-- > NUM_LOG_FRAC_DENOM)
665 while (fraclen++ < NUM_LOG_FRAC_DENOM)
675 * Get the value of the -e flag.
678 get_quit_at_eof(void)
682 /* When less_is_more is set, the -e flag semantics are different. */
683 return quit_at_eof ? OPT_ONPLUS : OPT_ON;