2 * Copyright (C) 1984-2012 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;
35 * Return a printable description of an option.
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".
59 sprintf(buf, "-%s", prchar(c));
64 * Scan an argument (either from the command line or from the
65 * LESS environment variable) and process it.
71 register struct loption *o;
85 * If we have a pending option which requires an argument,
87 * This happens if the previous option was, for example, "-P"
88 * without a following string. In that case, the current
89 * option is simply the argument for the previous option.
93 switch (pendopt->otype & OTYPE)
96 (*pendopt->ofunc)(INIT, s);
99 printopt = opt_desc(pendopt);
100 *(pendopt->ovar) = getnum(&s, printopt, (int*)NULL);
113 * Check some special cases first.
119 case END_OPTION_STRING:
123 * "--" indicates an option name instead of a letter.
131 * "-+" means set these options back to their defaults.
132 * (They may have been set otherwise by previous
135 set_default = (*s == '+');
141 * An option prefixed by a "+" is ungotten, so
142 * that it is interpreted as less commands
143 * processed at the start of the first input file.
144 * "++" means process the commands at the start of
148 s = optstring(s, &str, propt('+'), NULL);
152 every_first_cmd = save(++str);
157 case '0': case '1': case '2': case '3': case '4':
158 case '5': case '6': case '7': case '8': case '9':
160 * Special "more" compatibility form "-<number>"
161 * instead of -z<number> to set the scrolling
174 * Not a special case.
175 * Look up the option letter in the option table.
180 printopt = propt(optc);
181 lc = ASCII_IS_LOWER(optc);
186 lc = ASCII_IS_LOWER(optname[0]);
187 o = findopt_name(&optname, NULL, &err);
190 if (*s == '\0' || *s == ' ')
193 * The option name matches exactly.
196 } else if (*s == '=')
199 * The option name is followed by "=value".
202 (o->otype & OTYPE) != STRING &&
203 (o->otype & OTYPE) != NUMBER)
205 parg.p_string = printopt;
206 error("The %s option should not be followed by =",
214 * The specified name is longer than the
222 parg.p_string = printopt;
223 if (err == OPT_AMBIG)
224 error("%s is an ambiguous abbreviation (\"less --help\" for help)",
227 error("There is no %s option (\"less --help\" for help)",
233 switch (o->otype & OTYPE)
237 *(o->ovar) = o->odefault;
239 *(o->ovar) = ! o->odefault;
243 *(o->ovar) = o->odefault;
245 *(o->ovar) = flip_triple(o->odefault, lc);
251 * Set pendopt and return.
252 * We will get the string next time
253 * scan_option is called.
259 * Don't do anything here.
260 * All processing of STRING options is done by
261 * the handling function.
265 s = optstring(s, &str, printopt, o->odesc[1]);
275 *(o->ovar) = getnum(&s, printopt, (int*)NULL);
279 * If the option has a handling function, call it.
281 if (o->ofunc != NULL)
282 (*o->ofunc)(INIT, str);
289 * Toggle command line flags from within the program.
290 * Used by the "-" and "_" commands.
292 * OPT_NO_TOGGLE just report the current setting, without changing it.
293 * OPT_TOGGLE invert the current setting
294 * OPT_UNSET set to the default value
295 * OPT_SET set to the inverse of the default value
298 toggle_option(o, lower, s, how_toggle)
309 no_prompt = (how_toggle & OPT_NO_PROMPT);
310 how_toggle &= ~OPT_NO_PROMPT;
314 error("No such option", NULL_PARG);
318 if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE))
320 parg.p_string = opt_desc(o);
321 error("Cannot change the %s option", &parg);
325 if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY))
327 parg.p_string = opt_desc(o);
328 error("Cannot query the %s option", &parg);
333 * Check for something which appears to be a do_toggle
334 * (because the "-" command was used), but really is not.
335 * This could be a string option with no string, or
336 * a number option with no number.
338 switch (o->otype & OTYPE)
342 if (how_toggle == OPT_TOGGLE && *s == '\0')
343 how_toggle = OPT_NO_TOGGLE;
348 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
353 * Now actually toggle (change) the variable.
355 if (how_toggle != OPT_NO_TOGGLE)
357 switch (o->otype & OTYPE)
366 *(o->ovar) = ! *(o->ovar);
369 *(o->ovar) = o->odefault;
372 *(o->ovar) = ! o->odefault;
379 * If user gave the lower case letter, then switch
380 * to 1 unless already 1, in which case make it 0.
381 * If user gave the upper case letter, then switch
382 * to 2 unless already 2, in which case make it 0.
387 *(o->ovar) = flip_triple(*(o->ovar), lower);
390 *(o->ovar) = o->odefault;
393 *(o->ovar) = flip_triple(o->odefault, lower);
399 * String: don't do anything here.
400 * The handling function will do everything.
406 error("Cannot use \"-+\" or \"--\" for a string option",
413 * Number: set the variable to the given number.
418 num = getnum(&s, NULL, &err);
423 *(o->ovar) = o->odefault;
426 error("Can't use \"-!\" for a numeric option",
435 * Call the handling function for any special action
436 * specific to this option.
438 if (o->ofunc != NULL)
439 (*o->ofunc)((how_toggle==OPT_NO_TOGGLE) ? QUERY : TOGGLE, s);
442 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
449 * Print a message describing the new setting.
451 switch (o->otype & OTYPE)
456 * Print the odesc message.
458 error(o->odesc[*(o->ovar)], NULL_PARG);
462 * The message is in odesc[1] and has a %d for
463 * the value of the variable.
465 parg.p_int = *(o->ovar);
466 error(o->odesc[1], &parg);
470 * Message was already printed by the handling function.
476 if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
477 screen_trashed = TRUE;
481 * "Toggle" a triple-valued option.
489 return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
491 return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
495 * Determine if an option takes a parameter.
503 if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE))
509 * Return the prompt to be used for a given option letter.
510 * Only string and number valued options have prompts.
516 if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
518 return (o->odesc[0]);
522 * Return whether or not there is a string option pending;
523 * that is, if the previous option was a string-valued option letter
524 * (like -P) without a following string.
525 * In that case, the current option is taken to be the string for
526 * the previous option.
531 return (pendopt != NULL);
535 * Print error message about missing string.
542 parg.p_string = printopt;
543 error("Value is required after %s", &parg);
547 * Print error message if a STRING type option is not followed by a string.
552 nostring(opt_desc(pendopt));
556 * Scan to end of string or to an END_OPTION_STRING character.
557 * In the latter case, replace the char with a null char.
558 * Return a pointer to the remainder of the string, if any.
561 optstring(s, p_str, printopt, validchars)
575 /* Alloc could be more than needed, but not worth trimming. */
576 *p_str = (char *) ecalloc(strlen(s)+1, sizeof(char));
579 for (p = s; *p != '\0'; p++)
581 if (*p == '\\' && p[1] != '\0')
583 /* Take next char literally. */
587 if (*p == END_OPTION_STRING ||
588 *p == ' ' || *p == '\t' || *p == '-' ||
589 (validchars != NULL && strchr(validchars, *p) == NULL))
590 /* End of option string. */
602 num_error(printopt, errp)
613 if (printopt != NULL)
615 parg.p_string = printopt;
616 error("Number is required after %s", &parg);
622 * Translate a string into a number.
623 * Like atoi(), but takes a pointer to a char *, and updates
624 * the char * to point after the translated number.
627 getnum(sp, printopt, errp)
643 if (*s < '0' || *s > '9')
644 return (num_error(printopt, errp));
647 while (*s >= '0' && *s <= '9')
648 n = 10 * n + *s++ - '0';
658 * Translate a string into a fraction, represented by the part of a
659 * number which would follow a decimal point.
660 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
661 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
664 getfraction(sp, printopt, errp)
674 if (*s < '0' || *s > '9')
675 return (num_error(printopt, errp));
677 for ( ; *s >= '0' && *s <= '9'; s++)
679 frac = (frac * 10) + (*s - '0');
682 if (fraclen > NUM_LOG_FRAC_DENOM)
683 while (fraclen-- > NUM_LOG_FRAC_DENOM)
686 while (fraclen++ < NUM_LOG_FRAC_DENOM)
696 * Get the value of the -e flag.
703 /* When less_is_more is set, the -e flag semantics are different. */
704 return quit_at_eof ? OPT_ON : OPT_ONPLUS;