2 * Copyright (C) 1984-2002 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 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 loption *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
119 s = optstring(s, &str, propt('+'), NULL);
121 every_first_cmd = save(++str);
125 case '0': case '1': case '2': case '3': case '4':
126 case '5': case '6': case '7': case '8': case '9':
128 * Special "more" compatibility form "-<number>"
129 * instead of -z<number> to set the scrolling
138 * Not a special case.
139 * Look up the option letter in the option table.
144 printopt = propt(optc);
145 lc = SIMPLE_IS_LOWER(optc);
150 lc = SIMPLE_IS_LOWER(optname[0]);
151 o = findopt_name(&optname, NULL, &err);
154 if (*s == '\0' || *s == ' ')
157 * The option name matches exactly.
160 } else if (*s == '=')
163 * The option name is followed by "=value".
166 (o->otype & OTYPE) != STRING &&
167 (o->otype & OTYPE) != NUMBER)
169 parg.p_string = printopt;
170 error("The %s option should not be followed by =",
178 * The specified name is longer than the
186 parg.p_string = printopt;
187 if (err == OPT_AMBIG)
188 error("%s is an ambiguous abbreviation (\"less --help\" for help)",
191 error("There is no %s option (\"less --help\" for help)",
197 switch (o->otype & OTYPE)
201 *(o->ovar) = o->odefault;
203 *(o->ovar) = ! o->odefault;
207 *(o->ovar) = o->odefault;
209 *(o->ovar) = flip_triple(o->odefault, lc);
215 * Set pendopt and return.
216 * We will get the string next time
217 * scan_option is called.
223 * Don't do anything here.
224 * All processing of STRING options is done by
225 * the handling function.
229 s = optstring(s, &str, 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 loption *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, NULL, &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 loption *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 loption *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, p_str, printopt, validchars)
561 for (p = s; *p != '\0'; p++)
563 if (*p == END_OPTION_STRING ||
564 (validchars != NULL && strchr(validchars, *p) == NULL))
568 case END_OPTION_STRING:
569 case ' ': case '\t': case '-':
570 /* Replace the char with a null to terminate string. */
574 /* Cannot replace char; make a copy of the string. */
575 *p_str = (char *) ecalloc(p-s+1, sizeof(char));
576 strncpy(*p_str, s, p-s);
577 (*p_str)[p-s] = '\0';
587 * Translate a string into a number.
588 * Like atoi(), but takes a pointer to a char *, and updates
589 * the char * to point after the translated number.
592 getnum(sp, printopt, errp)
609 if (*s < '0' || *s > '9')
616 if (printopt != NULL)
618 parg.p_string = printopt;
619 error("Number is required after %s", &parg);
625 while (*s >= '0' && *s <= '9')
626 n = 10 * n + *s++ - '0';