1 /* $NetBSD: option.c,v 1.4 2013/09/04 19:44:21 tron Exp $ */
4 * Copyright (C) 1984-2012 Mark Nudelman
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
14 * Process command line options.
16 * Each option is a single letter which controls a program variable.
17 * The options have defaults which may be changed via
18 * the command line option, toggled via the "-" command,
19 * or queried via the "_" command.
25 static struct loption
*pendopt
;
26 public int plusoption
= FALSE
;
28 static char *optstring
__P((char *, char **, char *, char *));
29 static int flip_triple
__P((int, int));
30 static void nostring
__P((char *));
32 extern int screen_trashed
;
33 extern int less_is_more
;
34 extern int quit_at_eof
;
35 extern char *every_first_cmd
;
36 extern int opt_use_backslash
;
39 * Return a printable description of an option.
45 static char buf
[OPTNAME_MAX
+ 10];
46 if (o
->oletter
== OLETTER_NONE
)
47 SNPRINTF1(buf
, sizeof(buf
), "--%s", o
->onames
->oname
);
49 SNPRINTF2(buf
, sizeof(buf
), "-%c (--%s)", o
->oletter
, o
->onames
->oname
);
54 * Return a string suitable for printing as the "name" of an option.
55 * For example, if the option letter is 'x', just return "-x".
63 sprintf(buf
, "-%s", prchar(c
));
68 * Scan an argument (either from the command line or from the
69 * LESS environment variable) and process it.
75 register struct loption
*o
;
89 * If we have a pending option which requires an argument,
91 * This happens if the previous option was, for example, "-P"
92 * without a following string. In that case, the current
93 * option is simply the argument for the previous option.
97 switch (pendopt
->otype
& OTYPE
)
100 (*pendopt
->ofunc
)(INIT
, s
);
103 printopt
= opt_desc(pendopt
);
104 *(pendopt
->ovar
) = getnum(&s
, printopt
, (int*)NULL
);
117 * Check some special cases first.
123 case END_OPTION_STRING
:
127 * "--" indicates an option name instead of a letter.
135 * "-+" means set these options back to their defaults.
136 * (They may have been set otherwise by previous
139 set_default
= (*s
== '+');
145 * An option prefixed by a "+" is ungotten, so
146 * that it is interpreted as less commands
147 * processed at the start of the first input file.
148 * "++" means process the commands at the start of
152 s
= optstring(s
, &str
, propt('+'), NULL
);
156 every_first_cmd
= save(str
+1);
161 case '0': case '1': case '2': case '3': case '4':
162 case '5': case '6': case '7': case '8': case '9':
164 * Special "more" compatibility form "-<number>"
165 * instead of -z<number> to set the scrolling
178 * Not a special case.
179 * Look up the option letter in the option table.
184 printopt
= propt(optc
);
185 lc
= ASCII_IS_LOWER(optc
);
190 lc
= ASCII_IS_LOWER(optname
[0]);
191 o
= findopt_name(&optname
, NULL
, &err
);
194 if (*s
== '\0' || *s
== ' ')
197 * The option name matches exactly.
200 } else if (*s
== '=')
203 * The option name is followed by "=value".
206 (o
->otype
& OTYPE
) != STRING
&&
207 (o
->otype
& OTYPE
) != NUMBER
)
209 parg
.p_string
= printopt
;
210 error("The %s option should not be followed by =",
218 * The specified name is longer than the
226 parg
.p_string
= printopt
;
227 if (err
== OPT_AMBIG
)
228 error("%s is an ambiguous abbreviation (\"less --help\" for help)",
231 error("There is no %s option (\"less --help\" for help)",
237 switch (o
->otype
& OTYPE
)
241 *(o
->ovar
) = o
->odefault
;
243 *(o
->ovar
) = ! o
->odefault
;
247 *(o
->ovar
) = o
->odefault
;
249 *(o
->ovar
) = flip_triple(o
->odefault
, lc
);
255 * Set pendopt and return.
256 * We will get the string next time
257 * scan_option is called.
263 * Don't do anything here.
264 * All processing of STRING options is done by
265 * the handling function.
269 s
= optstring(s
, &str
, printopt
, o
->odesc
[1]);
279 *(o
->ovar
) = getnum(&s
, printopt
, (int*)NULL
);
283 * If the option has a handling function, call it.
285 if (o
->ofunc
!= NULL
)
286 (*o
->ofunc
)(INIT
, str
);
293 * Toggle command line flags from within the program.
294 * Used by the "-" and "_" commands.
296 * OPT_NO_TOGGLE just report the current setting, without changing it.
297 * OPT_TOGGLE invert the current setting
298 * OPT_UNSET set to the default value
299 * OPT_SET set to the inverse of the default value
302 toggle_option(o
, lower
, s
, how_toggle
)
313 no_prompt
= (how_toggle
& OPT_NO_PROMPT
);
314 how_toggle
&= ~OPT_NO_PROMPT
;
318 error("No such option", NULL_PARG
);
322 if (how_toggle
== OPT_TOGGLE
&& (o
->otype
& NO_TOGGLE
))
324 parg
.p_string
= opt_desc(o
);
325 error("Cannot change the %s option", &parg
);
329 if (how_toggle
== OPT_NO_TOGGLE
&& (o
->otype
& NO_QUERY
))
331 parg
.p_string
= opt_desc(o
);
332 error("Cannot query the %s option", &parg
);
337 * Check for something which appears to be a do_toggle
338 * (because the "-" command was used), but really is not.
339 * This could be a string option with no string, or
340 * a number option with no number.
342 switch (o
->otype
& OTYPE
)
346 if (how_toggle
== OPT_TOGGLE
&& *s
== '\0')
347 how_toggle
= OPT_NO_TOGGLE
;
352 if (how_toggle
!= OPT_NO_TOGGLE
&& (o
->otype
& HL_REPAINT
))
357 * Now actually toggle (change) the variable.
359 if (how_toggle
!= OPT_NO_TOGGLE
)
361 switch (o
->otype
& OTYPE
)
370 *(o
->ovar
) = ! *(o
->ovar
);
373 *(o
->ovar
) = o
->odefault
;
376 *(o
->ovar
) = ! o
->odefault
;
383 * If user gave the lower case letter, then switch
384 * to 1 unless already 1, in which case make it 0.
385 * If user gave the upper case letter, then switch
386 * to 2 unless already 2, in which case make it 0.
391 *(o
->ovar
) = flip_triple(*(o
->ovar
), lower
);
394 *(o
->ovar
) = o
->odefault
;
397 *(o
->ovar
) = flip_triple(o
->odefault
, lower
);
403 * String: don't do anything here.
404 * The handling function will do everything.
410 error("Cannot use \"-+\" or \"--\" for a string option",
417 * Number: set the variable to the given number.
422 num
= getnum(&s
, NULL
, &err
);
427 *(o
->ovar
) = o
->odefault
;
430 error("Can't use \"-!\" for a numeric option",
439 * Call the handling function for any special action
440 * specific to this option.
442 if (o
->ofunc
!= NULL
)
443 (*o
->ofunc
)((how_toggle
==OPT_NO_TOGGLE
) ? QUERY
: TOGGLE
, s
);
446 if (how_toggle
!= OPT_NO_TOGGLE
&& (o
->otype
& HL_REPAINT
))
453 * Print a message describing the new setting.
455 switch (o
->otype
& OTYPE
)
460 error("Negative option is invalid", NULL_PARG
);
462 * Print the odesc message.
464 error(o
->odesc
[*(o
->ovar
)], NULL_PARG
);
468 * The message is in odesc[1] and has a %d for
469 * the value of the variable.
471 parg
.p_int
= *(o
->ovar
);
472 error(o
->odesc
[1], &parg
);
476 * Message was already printed by the handling function.
482 if (how_toggle
!= OPT_NO_TOGGLE
&& (o
->otype
& REPAINT
))
483 screen_trashed
= TRUE
;
487 * "Toggle" a triple-valued option.
495 return ((val
== OPT_ON
) ? OPT_OFF
: OPT_ON
);
497 return ((val
== OPT_ONPLUS
) ? OPT_OFF
: OPT_ONPLUS
);
501 * Determine if an option takes a parameter.
509 if (o
->otype
& (BOOL
|TRIPLE
|NOVAR
|NO_TOGGLE
))
515 * Return the prompt to be used for a given option letter.
516 * Only string and number valued options have prompts.
522 if (o
== NULL
|| (o
->otype
& (STRING
|NUMBER
)) == 0)
524 return (o
->odesc
[0]);
528 * Return whether or not there is a string option pending;
529 * that is, if the previous option was a string-valued option letter
530 * (like -P) without a following string.
531 * In that case, the current option is taken to be the string for
532 * the previous option.
537 return (pendopt
!= NULL
);
541 * Print error message about missing string.
548 parg
.p_string
= printopt
;
549 error("Value is required after %s", &parg
);
553 * Print error message if a STRING type option is not followed by a string.
558 nostring(opt_desc(pendopt
));
562 * Scan to end of string or to an END_OPTION_STRING character.
563 * In the latter case, replace the char with a null char.
564 * Return a pointer to the remainder of the string, if any.
567 optstring(s
, p_str
, printopt
, validchars
)
581 /* Alloc could be more than needed, but not worth trimming. */
582 *p_str
= (char *) ecalloc(strlen(s
)+1, sizeof(char));
585 for (p
= s
; *p
!= '\0'; p
++)
587 if (opt_use_backslash
&& *p
== '\\' && p
[1] != '\0')
589 /* Take next char literally. */
593 if (*p
== END_OPTION_STRING
||
594 (validchars
!= NULL
&& strchr(validchars
, *p
) == NULL
))
595 /* End of option string. */
607 num_error(printopt
, errp
)
618 if (printopt
!= NULL
)
620 parg
.p_string
= printopt
;
621 error("Number is required after %s", &parg
);
627 * Translate a string into a number.
628 * Like atoi(), but takes a pointer to a char *, and updates
629 * the char * to point after the translated number.
632 getnum(sp
, printopt
, errp
)
648 if (*s
< '0' || *s
> '9')
649 return (num_error(printopt
, errp
));
652 while (*s
>= '0' && *s
<= '9')
653 n
= 10 * n
+ *s
++ - '0';
663 * Translate a string into a fraction, represented by the part of a
664 * number which would follow a decimal point.
665 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
666 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
669 getfraction(sp
, printopt
, errp
)
679 if (*s
< '0' || *s
> '9')
680 return (num_error(printopt
, errp
));
682 for ( ; *s
>= '0' && *s
<= '9'; s
++)
684 frac
= (frac
* 10) + (*s
- '0');
687 if (fraclen
> NUM_LOG_FRAC_DENOM
)
688 while (fraclen
-- > NUM_LOG_FRAC_DENOM
)
691 while (fraclen
++ < NUM_LOG_FRAC_DENOM
)
701 * Get the value of the -e flag.
708 /* When less_is_more is set, the -e flag semantics are different. */
709 return quit_at_eof
? OPT_ON
: OPT_ONPLUS
;