2 * Copyright (C) 1984-2000 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 option *pendopt;
25 public int plusoption = FALSE;
28 static char *optstring();
29 static int flip_triple();
31 extern int screen_trashed;
32 extern char *every_first_cmd;
35 * Scan an argument (either from the command line or from the
36 * LESS environment variable) and process it.
42 register struct option *o;
56 * If we have a pending option which requires an argument,
58 * This happens if the previous option was, for example, "-P"
59 * without a following string. In that case, the current
60 * option is simply the argument for the previous option.
64 switch (pendopt->otype & OTYPE)
67 (*pendopt->ofunc)(INIT, s);
70 printopt = propt(pendopt->oletter);
71 *(pendopt->ovar) = getnum(&s, printopt, (int*)NULL);
84 * Check some special cases first.
90 case END_OPTION_STRING:
94 * "--" indicates an option name instead of a letter.
102 * "-+" means set these options back to their defaults.
103 * (They may have been set otherwise by previous
106 set_default = (*s == '+');
112 * An option prefixed by a "+" is ungotten, so
113 * that it is interpreted as less commands
114 * processed at the start of the first input file.
115 * "++" means process the commands at the start of
120 s = optstring(s, propt('+'), NULL);
122 every_first_cmd = save(++str);
126 case '0': case '1': case '2': case '3': case '4':
127 case '5': case '6': case '7': case '8': case '9':
129 * Special "more" compatibility form "-<number>"
130 * instead of -z<number> to set the scrolling
139 * Not a special case.
140 * Look up the option letter in the option table.
145 printopt = propt(optc);
146 lc = SIMPLE_IS_LOWER(optc);
151 lc = SIMPLE_IS_LOWER(optname[0]);
152 o = findopt_name(&optname, NULL, &err);
155 if (*s == '\0' || *s == ' ')
158 * The option name matches exactly.
161 } else if (*s == '=')
164 * The option name is followed by "=value".
167 (o->otype & OTYPE) != STRING &&
168 (o->otype & OTYPE) != NUMBER)
170 parg.p_string = printopt;
171 error("The %s option should not be followed by =",
179 * The specified name is longer than the
187 parg.p_string = printopt;
188 if (err == OPT_AMBIG)
189 error("%s is an ambiguous abbreviation (\"less --help\" for help)",
192 error("There is no %s option (\"less --help\" for help)",
198 switch (o->otype & OTYPE)
202 *(o->ovar) = o->odefault;
204 *(o->ovar) = ! o->odefault;
208 *(o->ovar) = o->odefault;
210 *(o->ovar) = flip_triple(o->odefault, lc);
216 * Set pendopt and return.
217 * We will get the string next time
218 * scan_option is called.
224 * Don't do anything here.
225 * All processing of STRING options is done by
226 * the handling function.
229 s = optstring(s, printopt, o->odesc[1]);
237 *(o->ovar) = getnum(&s, printopt, (int*)NULL);
241 * If the option has a handling function, call it.
243 if (o->ofunc != NULL)
244 (*o->ofunc)(INIT, str);
249 * Toggle command line flags from within the program.
250 * Used by the "-" and "_" commands.
252 * OPT_NO_TOGGLE just report the current setting, without changing it.
253 * OPT_TOGGLE invert the current setting
254 * OPT_UNSET set to the default value
255 * OPT_SET set to the inverse of the default value
258 toggle_option(c, s, how_toggle)
263 register struct option *o;
269 no_prompt = (how_toggle & OPT_NO_PROMPT);
270 how_toggle &= ~OPT_NO_PROMPT;
273 * Look up the option letter in the option table.
278 parg.p_string = propt(c);
279 error("There is no %s option", &parg);
283 if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE))
285 parg.p_string = propt(c);
286 error("Cannot change the %s option", &parg);
290 if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY))
292 parg.p_string = propt(c);
293 error("Cannot query the %s option", &parg);
298 * Check for something which appears to be a do_toggle
299 * (because the "-" command was used), but really is not.
300 * This could be a string option with no string, or
301 * a number option with no number.
303 switch (o->otype & OTYPE)
307 if (how_toggle == OPT_TOGGLE && *s == '\0')
308 how_toggle = OPT_NO_TOGGLE;
313 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
318 * Now actually toggle (change) the variable.
320 if (how_toggle != OPT_NO_TOGGLE)
322 switch (o->otype & OTYPE)
331 *(o->ovar) = ! *(o->ovar);
334 *(o->ovar) = o->odefault;
337 *(o->ovar) = ! o->odefault;
344 * If user gave the lower case letter, then switch
345 * to 1 unless already 1, in which case make it 0.
346 * If user gave the upper case letter, then switch
347 * to 2 unless already 2, in which case make it 0.
352 *(o->ovar) = flip_triple(*(o->ovar),
356 *(o->ovar) = o->odefault;
359 *(o->ovar) = flip_triple(o->odefault,
366 * String: don't do anything here.
367 * The handling function will do everything.
373 error("Cannot use \"-+\" or \"--\" for a string option",
380 * Number: set the variable to the given number.
385 num = getnum(&s, '\0', &err);
390 *(o->ovar) = o->odefault;
393 error("Can't use \"-!\" for a numeric option",
402 * Call the handling function for any special action
403 * specific to this option.
405 if (o->ofunc != NULL)
406 (*o->ofunc)((how_toggle==OPT_NO_TOGGLE) ? QUERY : TOGGLE, s);
409 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
416 * Print a message describing the new setting.
418 switch (o->otype & OTYPE)
423 * Print the odesc message.
425 error(o->odesc[*(o->ovar)], NULL_PARG);
429 * The message is in odesc[1] and has a %d for
430 * the value of the variable.
432 parg.p_int = *(o->ovar);
433 error(o->odesc[1], &parg);
437 * Message was already printed by the handling function.
443 if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
444 screen_trashed = TRUE;
448 * "Toggle" a triple-valued option.
456 return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
458 return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
462 * Return a string suitable for printing as the "name" of an option.
463 * For example, if the option letter is 'x', just return "-x".
471 sprintf(buf, "-%s", prchar(c));
476 * Determine if an option is a single character option (BOOL or TRIPLE),
477 * or if it a multi-character option (NUMBER).
480 single_char_option(c)
483 register struct option *o;
488 return ((o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE)) != 0);
492 * Return the prompt to be used for a given option letter.
493 * Only string and number valued options have prompts.
499 register struct option *o;
502 if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
504 return (o->odesc[0]);
508 * Return whether or not there is a string option pending;
509 * that is, if the previous option was a string-valued option letter
510 * (like -P) without a following string.
511 * In that case, the current option is taken to be the string for
512 * the previous option.
517 return (pendopt != NULL);
521 * Print error message about missing string.
528 parg.p_string = printopt;
529 error("Value is required after %s", &parg);
533 * Print error message if a STRING type option is not followed by a string.
538 nostring(propt(pendopt->oletter));
542 * Scan to end of string or to an END_OPTION_STRING character.
543 * In the latter case, replace the char with a null char.
544 * Return a pointer to the remainder of the string, if any.
547 optstring(s, printopt, validchars)
560 for (p = s; *p != '\0'; p++)
561 if (*p == END_OPTION_STRING ||
562 (validchars != NULL && strchr(validchars, *p) == NULL))
566 case END_OPTION_STRING:
567 case ' ': case '\t': case '-':
571 error("Option string needs delimiter before %s", &parg);
581 * Translate a string into a number.
582 * Like atoi(), but takes a pointer to a char *, and updates
583 * the char * to point after the translated number.
586 getnum(sp, printopt, errp)
603 if (*s < '0' || *s > '9')
610 parg.p_string = printopt;
611 error("Number is required after %s", &parg);
616 while (*s >= '0' && *s <= '9')
617 n = 10 * n + *s++ - '0';