2 * Copyright (C) 1984-2009 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;
28 static char *optstring();
29 static int flip_triple();
31 extern int screen_trashed;
32 extern int less_is_more;
33 extern int quit_at_eof;
34 extern char *every_first_cmd;
37 * Scan an argument (either from the command line or from the
38 * LESS environment variable) and process it.
44 register struct loption *o;
58 * If we have a pending option which requires an argument,
60 * This happens if the previous option was, for example, "-P"
61 * without a following string. In that case, the current
62 * option is simply the argument for the previous option.
66 switch (pendopt->otype & OTYPE)
69 (*pendopt->ofunc)(INIT, s);
72 printopt = propt(pendopt->oletter);
73 *(pendopt->ovar) = getnum(&s, printopt, (int*)NULL);
86 * Check some special cases first.
92 case END_OPTION_STRING:
96 * "--" indicates an option name instead of a letter.
104 * "-+" means set these options back to their defaults.
105 * (They may have been set otherwise by previous
108 set_default = (*s == '+');
114 * An option prefixed by a "+" is ungotten, so
115 * that it is interpreted as less commands
116 * processed at the start of the first input file.
117 * "++" means process the commands at the start of
121 s = optstring(s, &str, propt('+'), NULL);
123 every_first_cmd = save(++str);
127 case '0': case '1': case '2': case '3': case '4':
128 case '5': case '6': case '7': case '8': case '9':
130 * Special "more" compatibility form "-<number>"
131 * instead of -z<number> to set the scrolling
144 * Not a special case.
145 * Look up the option letter in the option table.
150 printopt = propt(optc);
151 lc = ASCII_IS_LOWER(optc);
156 lc = ASCII_IS_LOWER(optname[0]);
157 o = findopt_name(&optname, NULL, &err);
160 if (*s == '\0' || *s == ' ')
163 * The option name matches exactly.
166 } else if (*s == '=')
169 * The option name is followed by "=value".
172 (o->otype & OTYPE) != STRING &&
173 (o->otype & OTYPE) != NUMBER)
175 parg.p_string = printopt;
176 error("The %s option should not be followed by =",
184 * The specified name is longer than the
192 parg.p_string = printopt;
193 if (err == OPT_AMBIG)
194 error("%s is an ambiguous abbreviation (\"less --help\" for help)",
197 error("There is no %s option (\"less --help\" for help)",
203 switch (o->otype & OTYPE)
207 *(o->ovar) = o->odefault;
209 *(o->ovar) = ! o->odefault;
213 *(o->ovar) = o->odefault;
215 *(o->ovar) = flip_triple(o->odefault, lc);
221 * Set pendopt and return.
222 * We will get the string next time
223 * scan_option is called.
229 * Don't do anything here.
230 * All processing of STRING options is done by
231 * the handling function.
235 s = optstring(s, &str, printopt, o->odesc[1]);
243 *(o->ovar) = getnum(&s, printopt, (int*)NULL);
247 * If the option has a handling function, call it.
249 if (o->ofunc != NULL)
250 (*o->ofunc)(INIT, str);
255 * Toggle command line flags from within the program.
256 * Used by the "-" and "_" commands.
258 * OPT_NO_TOGGLE just report the current setting, without changing it.
259 * OPT_TOGGLE invert the current setting
260 * OPT_UNSET set to the default value
261 * OPT_SET set to the inverse of the default value
264 toggle_option(c, s, how_toggle)
269 register struct loption *o;
275 no_prompt = (how_toggle & OPT_NO_PROMPT);
276 how_toggle &= ~OPT_NO_PROMPT;
279 * Look up the option letter in the option table.
284 parg.p_string = propt(c);
285 error("There is no %s option", &parg);
289 if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE))
291 parg.p_string = propt(c);
292 error("Cannot change the %s option", &parg);
296 if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY))
298 parg.p_string = propt(c);
299 error("Cannot query the %s option", &parg);
304 * Check for something which appears to be a do_toggle
305 * (because the "-" command was used), but really is not.
306 * This could be a string option with no string, or
307 * a number option with no number.
309 switch (o->otype & OTYPE)
313 if (how_toggle == OPT_TOGGLE && *s == '\0')
314 how_toggle = OPT_NO_TOGGLE;
319 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
324 * Now actually toggle (change) the variable.
326 if (how_toggle != OPT_NO_TOGGLE)
328 switch (o->otype & OTYPE)
337 *(o->ovar) = ! *(o->ovar);
340 *(o->ovar) = o->odefault;
343 *(o->ovar) = ! o->odefault;
350 * If user gave the lower case letter, then switch
351 * to 1 unless already 1, in which case make it 0.
352 * If user gave the upper case letter, then switch
353 * to 2 unless already 2, in which case make it 0.
358 *(o->ovar) = flip_triple(*(o->ovar),
362 *(o->ovar) = o->odefault;
365 *(o->ovar) = flip_triple(o->odefault,
372 * String: don't do anything here.
373 * The handling function will do everything.
379 error("Cannot use \"-+\" or \"--\" for a string option",
386 * Number: set the variable to the given number.
391 num = getnum(&s, NULL, &err);
396 *(o->ovar) = o->odefault;
399 error("Can't use \"-!\" for a numeric option",
408 * Call the handling function for any special action
409 * specific to this option.
411 if (o->ofunc != NULL)
412 (*o->ofunc)((how_toggle==OPT_NO_TOGGLE) ? QUERY : TOGGLE, s);
415 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
422 * Print a message describing the new setting.
424 switch (o->otype & OTYPE)
429 * Print the odesc message.
431 error(o->odesc[*(o->ovar)], NULL_PARG);
435 * The message is in odesc[1] and has a %d for
436 * the value of the variable.
438 parg.p_int = *(o->ovar);
439 error(o->odesc[1], &parg);
443 * Message was already printed by the handling function.
449 if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
450 screen_trashed = TRUE;
454 * "Toggle" a triple-valued option.
462 return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
464 return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
468 * Return a string suitable for printing as the "name" of an option.
469 * For example, if the option letter is 'x', just return "-x".
477 sprintf(buf, "-%s", prchar(c));
482 * Determine if an option is a single character option (BOOL or TRIPLE),
483 * or if it a multi-character option (NUMBER).
486 single_char_option(c)
489 register struct loption *o;
494 return ((o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE)) != 0);
498 * Return the prompt to be used for a given option letter.
499 * Only string and number valued options have prompts.
505 register struct loption *o;
508 if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
510 return (o->odesc[0]);
514 * Return whether or not there is a string option pending;
515 * that is, if the previous option was a string-valued option letter
516 * (like -P) without a following string.
517 * In that case, the current option is taken to be the string for
518 * the previous option.
523 return (pendopt != NULL);
527 * Print error message about missing string.
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(propt(pendopt->oletter));
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(s, p_str, printopt, validchars)
567 for (p = s; *p != '\0'; p++)
569 if (*p == END_OPTION_STRING ||
570 (validchars != NULL && strchr(validchars, *p) == NULL))
574 case END_OPTION_STRING:
575 case ' ': case '\t': case '-':
576 /* Replace the char with a null to terminate string. */
580 /* Cannot replace char; make a copy of the string. */
581 *p_str = (char *) ecalloc(p-s+1, sizeof(char));
582 strncpy(*p_str, s, p-s);
583 (*p_str)[p-s] = '\0';
595 num_error(printopt, errp)
606 if (printopt != NULL)
608 parg.p_string = printopt;
609 error("Number is required after %s", &parg);
617 * Translate a string into a number.
618 * Like atoi(), but takes a pointer to a char *, and updates
619 * the char * to point after the translated number.
622 getnum(sp, printopt, errp)
638 if (*s < '0' || *s > '9')
639 return (num_error(printopt, errp));
642 while (*s >= '0' && *s <= '9')
643 n = 10 * n + *s++ - '0';
653 * Translate a string into a fraction, represented by the part of a
654 * number which would follow a decimal point.
655 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
656 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
659 getfraction(sp, printopt, errp)
669 if (*s < '0' || *s > '9')
670 return (num_error(printopt, errp));
672 for ( ; *s >= '0' && *s <= '9'; s++)
674 frac = (frac * 10) + (*s - '0');
677 if (fraclen > NUM_LOG_FRAC_DENOM)
678 while (fraclen-- > NUM_LOG_FRAC_DENOM)
681 while (fraclen++ < NUM_LOG_FRAC_DENOM)
691 * Get the value of the -e flag.
698 /* When less_is_more is set, the -e flag semantics are different. */
699 return quit_at_eof ? OPT_ON : OPT_ONPLUS;