2 * Copyright (C) 1984-2011 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 about less, or for information on how to
8 * contact the author, see the README file.
13 * Process command line options.
15 * Each option is a single letter which controls a program variable.
16 * The options have defaults which may be changed via
17 * the command line option, toggled via the "-" command,
18 * or queried via the "_" command.
24 static struct loption *pendopt;
25 public int plusoption = FALSE;
27 static char *optstring();
28 static int flip_triple();
30 extern int screen_trashed;
31 extern int less_is_more;
32 extern int quit_at_eof;
33 extern char *every_first_cmd;
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.
72 register struct loption *o;
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);
151 every_first_cmd = save(++str);
155 case '0': case '1': case '2': case '3': case '4':
156 case '5': case '6': case '7': case '8': case '9':
158 * Special "more" compatibility form "-<number>"
159 * instead of -z<number> to set the scrolling
172 * Not a special case.
173 * Look up the option letter in the option table.
178 printopt = propt(optc);
179 lc = ASCII_IS_LOWER(optc);
184 lc = ASCII_IS_LOWER(optname[0]);
185 o = findopt_name(&optname, NULL, &err);
188 if (*s == '\0' || *s == ' ')
191 * The option name matches exactly.
194 } else if (*s == '=')
197 * The option name is followed by "=value".
200 (o->otype & OTYPE) != STRING &&
201 (o->otype & OTYPE) != NUMBER)
203 parg.p_string = printopt;
204 error("The %s option should not be followed by =",
212 * The specified name is longer than the
220 parg.p_string = printopt;
221 if (err == OPT_AMBIG)
222 error("%s is an ambiguous abbreviation (\"less --help\" for help)",
225 error("There is no %s option (\"less --help\" for help)",
231 switch (o->otype & OTYPE)
235 *(o->ovar) = o->odefault;
237 *(o->ovar) = ! o->odefault;
241 *(o->ovar) = o->odefault;
243 *(o->ovar) = flip_triple(o->odefault, lc);
249 * Set pendopt and return.
250 * We will get the string next time
251 * scan_option is called.
257 * Don't do anything here.
258 * All processing of STRING options is done by
259 * the handling function.
263 s = optstring(s, &str, printopt, o->odesc[1]);
271 *(o->ovar) = getnum(&s, printopt, (int*)NULL);
275 * If the option has a handling function, call it.
277 if (o->ofunc != NULL)
278 (*o->ofunc)(INIT, str);
283 * Toggle command line flags from within the program.
284 * Used by the "-" and "_" commands.
286 * OPT_NO_TOGGLE just report the current setting, without changing it.
287 * OPT_TOGGLE invert the current setting
288 * OPT_UNSET set to the default value
289 * OPT_SET set to the inverse of the default value
292 toggle_option(o, lower, s, how_toggle)
303 no_prompt = (how_toggle & OPT_NO_PROMPT);
304 how_toggle &= ~OPT_NO_PROMPT;
308 error("No such option", NULL_PARG);
312 if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE))
314 parg.p_string = opt_desc(o);
315 error("Cannot change the %s option", &parg);
319 if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY))
321 parg.p_string = opt_desc(o);
322 error("Cannot query the %s option", &parg);
327 * Check for something which appears to be a do_toggle
328 * (because the "-" command was used), but really is not.
329 * This could be a string option with no string, or
330 * a number option with no number.
332 switch (o->otype & OTYPE)
336 if (how_toggle == OPT_TOGGLE && *s == '\0')
337 how_toggle = OPT_NO_TOGGLE;
342 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
347 * Now actually toggle (change) the variable.
349 if (how_toggle != OPT_NO_TOGGLE)
351 switch (o->otype & OTYPE)
360 *(o->ovar) = ! *(o->ovar);
363 *(o->ovar) = o->odefault;
366 *(o->ovar) = ! o->odefault;
373 * If user gave the lower case letter, then switch
374 * to 1 unless already 1, in which case make it 0.
375 * If user gave the upper case letter, then switch
376 * to 2 unless already 2, in which case make it 0.
381 *(o->ovar) = flip_triple(*(o->ovar), lower);
384 *(o->ovar) = o->odefault;
387 *(o->ovar) = flip_triple(o->odefault, lower);
393 * String: don't do anything here.
394 * The handling function will do everything.
400 error("Cannot use \"-+\" or \"--\" for a string option",
407 * Number: set the variable to the given number.
412 num = getnum(&s, NULL, &err);
417 *(o->ovar) = o->odefault;
420 error("Can't use \"-!\" for a numeric option",
429 * Call the handling function for any special action
430 * specific to this option.
432 if (o->ofunc != NULL)
433 (*o->ofunc)((how_toggle==OPT_NO_TOGGLE) ? QUERY : TOGGLE, s);
436 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
443 * Print a message describing the new setting.
445 switch (o->otype & OTYPE)
450 * Print the odesc message.
452 error(o->odesc[*(o->ovar)], NULL_PARG);
456 * The message is in odesc[1] and has a %d for
457 * the value of the variable.
459 parg.p_int = *(o->ovar);
460 error(o->odesc[1], &parg);
464 * Message was already printed by the handling function.
470 if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
471 screen_trashed = TRUE;
475 * "Toggle" a triple-valued option.
483 return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
485 return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
489 * Determine if an option takes a parameter.
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.
510 if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
512 return (o->odesc[0]);
516 * Return whether or not there is a string option pending;
517 * that is, if the previous option was a string-valued option letter
518 * (like -P) without a following string.
519 * In that case, the current option is taken to be the string for
520 * the previous option.
525 return (pendopt != NULL);
529 * Print error message about missing string.
536 parg.p_string = printopt;
537 error("Value is required after %s", &parg);
541 * Print error message if a STRING type option is not followed by a string.
546 nostring(opt_desc(pendopt));
550 * Scan to end of string or to an END_OPTION_STRING character.
551 * In the latter case, replace the char with a null char.
552 * Return a pointer to the remainder of the string, if any.
555 optstring(s, p_str, printopt, validchars)
569 for (p = s; *p != '\0'; p++)
571 if (*p == END_OPTION_STRING ||
572 (validchars != NULL && strchr(validchars, *p) == NULL))
576 case END_OPTION_STRING:
577 case ' ': case '\t': case '-':
578 /* Replace the char with a null to terminate string. */
582 /* Cannot replace char; make a copy of the string. */
583 *p_str = (char *) ecalloc(p-s+1, sizeof(char));
584 strncpy(*p_str, s, p-s);
585 (*p_str)[p-s] = '\0';
597 num_error(printopt, errp)
608 if (printopt != NULL)
610 parg.p_string = printopt;
611 error("Number is required after %s", &parg);
619 * Translate a string into a number.
620 * Like atoi(), but takes a pointer to a char *, and updates
621 * the char * to point after the translated number.
624 getnum(sp, printopt, errp)
640 if (*s < '0' || *s > '9')
641 return (num_error(printopt, errp));
644 while (*s >= '0' && *s <= '9')
645 n = 10 * n + *s++ - '0';
655 * Translate a string into a fraction, represented by the part of a
656 * number which would follow a decimal point.
657 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
658 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
661 getfraction(sp, printopt, errp)
671 if (*s < '0' || *s > '9')
672 return (num_error(printopt, errp));
674 for ( ; *s >= '0' && *s <= '9'; s++)
676 frac = (frac * 10) + (*s - '0');
679 if (fraclen > NUM_LOG_FRAC_DENOM)
680 while (fraclen-- > NUM_LOG_FRAC_DENOM)
683 while (fraclen++ < NUM_LOG_FRAC_DENOM)
693 * Get the value of the -e flag.
700 /* When less_is_more is set, the -e flag semantics are different. */
701 return quit_at_eof ? OPT_ON : OPT_ONPLUS;