2 * Copyright (C) 1984-2012 Mark Nudelman
3 * Modified for use with illumos by Garrett D'Amore.
4 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
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.
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 int plusoption
= FALSE
;
27 static char *optstring(char *, char **, char *, char *);
28 static int flip_triple(int, int);
30 extern int screen_trashed
;
31 extern int less_is_more
;
32 extern int quit_at_eof
;
33 extern char *every_first_cmd
;
34 extern int opt_use_backslash
;
37 * Return a printable description of an option.
40 opt_desc(struct loption
*o
)
42 static char buf
[OPTNAME_MAX
+ 10];
43 if (o
->oletter
== OLETTER_NONE
)
44 (void) snprintf(buf
, sizeof (buf
), "--%s", o
->onames
->oname
);
46 (void) snprintf(buf
, sizeof (buf
), "-%c (--%s)",
47 o
->oletter
, o
->onames
->oname
);
52 * Return a string suitable for printing as the "name" of an option.
53 * For example, if the option letter is 'x', just return "-x".
60 (void) snprintf(buf
, sizeof (buf
), "-%s", prchar(c
));
65 * Scan an argument (either from the command line or from the
66 * LESS environment variable) and process it.
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.
92 if (pendopt
!= NULL
) {
93 switch (pendopt
->otype
& OTYPE
) {
95 (*pendopt
->ofunc
)(INIT
, s
);
98 printopt
= opt_desc(pendopt
);
99 *(pendopt
->ovar
) = getnum(&s
, printopt
, NULL
);
113 * Check some special cases first.
115 switch (optc
= *s
++) {
118 case END_OPTION_STRING
:
122 * "--" indicates an option name instead of a letter.
131 * "-+" means set these options back to their defaults.
132 * (They may have been set otherwise by previous
136 set_default
= (*s
== '+');
143 * An option prefixed by a "+" is ungotten, so
144 * that it is interpreted as less commands
145 * processed at the start of the first input file.
146 * "++" means process the commands at the start of
150 s
= optstring(s
, &str
, propt('+'), NULL
);
154 every_first_cmd
= estrdup(str
+1);
159 case '0': case '1': case '2': case '3': case '4':
160 case '5': case '6': case '7': case '8': case '9':
162 * Special "more" compatibility form "-<number>"
163 * instead of -z<number> to set the scrolling
209 * Not a special case.
210 * Look up the option letter in the option table.
213 if (optname
== NULL
) {
214 printopt
= propt(optc
);
217 if (less_is_more
&& (!moreopt
) && (o
!= NULL
) &&
218 ((o
->otype
& MORE_OK
) == 0)) {
223 lc
= islower(optname
[0]);
224 o
= findopt_name(&optname
, NULL
, &err
);
228 case ' ': /* name matches exactly */
232 case '=': /* name followed by "=value" */
234 (o
->otype
& OTYPE
) != STRING
&&
235 (o
->otype
& OTYPE
) != NUMBER
) {
236 parg
.p_string
= printopt
;
237 error("The %s option should not be "
238 "followed by =", &parg
);
243 default: /* name longer than option, bad */
248 parg
.p_string
= printopt
;
250 error("Illegal option %s (more -h for help)",
252 } else if (err
== OPT_AMBIG
) {
253 error("%s is an ambiguous abbreviation "
254 "(\"less --help\" for help)", &parg
);
256 error("There is no %s option "
257 "(\"less --help\" for help)", &parg
);
263 switch (o
->otype
& OTYPE
) {
266 *(o
->ovar
) = o
->odefault
;
268 *(o
->ovar
) = ! o
->odefault
;
272 *(o
->ovar
) = o
->odefault
;
274 *(o
->ovar
) = flip_triple(o
->odefault
, lc
);
279 * Set pendopt and return.
280 * We will get the string next time
281 * scan_option is called.
287 * Don't do anything here.
288 * All processing of STRING options is done by
289 * the handling function.
293 s
= optstring(s
, &str
, printopt
, o
->odesc
[1]);
302 *(o
->ovar
) = getnum(&s
, printopt
, NULL
);
306 * If the option has a handling function, call it.
308 if (o
->ofunc
!= NULL
)
309 (*o
->ofunc
)(INIT
, str
);
315 * Toggle command line flags from within the program.
316 * Used by the "-" and "_" commands.
318 * OPT_NO_TOGGLE just report the current setting, without changing it.
319 * OPT_TOGGLE invert the current setting
320 * OPT_UNSET set to the default value
321 * OPT_SET set to the inverse of the default value
324 toggle_option(struct loption
*o
, int lower
, char *s
, int how_toggle
)
331 no_prompt
= (how_toggle
& OPT_NO_PROMPT
);
332 how_toggle
&= ~OPT_NO_PROMPT
;
335 error("No such option", NULL
);
339 if (how_toggle
== OPT_TOGGLE
&& (o
->otype
& NO_TOGGLE
)) {
340 parg
.p_string
= opt_desc(o
);
341 error("Cannot change the %s option", &parg
);
345 if (how_toggle
== OPT_NO_TOGGLE
&& (o
->otype
& NO_QUERY
)) {
346 parg
.p_string
= opt_desc(o
);
347 error("Cannot query the %s option", &parg
);
352 * Check for something which appears to be a do_toggle
353 * (because the "-" command was used), but really is not.
354 * This could be a string option with no string, or
355 * a number option with no number.
357 switch (o
->otype
& OTYPE
) {
360 if (how_toggle
== OPT_TOGGLE
&& *s
== '\0')
361 how_toggle
= OPT_NO_TOGGLE
;
365 if (how_toggle
!= OPT_NO_TOGGLE
&& (o
->otype
& HL_REPAINT
))
369 * Now actually toggle (change) the variable.
371 if (how_toggle
!= OPT_NO_TOGGLE
) {
372 switch (o
->otype
& OTYPE
) {
377 switch (how_toggle
) {
379 *(o
->ovar
) = ! *(o
->ovar
);
382 *(o
->ovar
) = o
->odefault
;
385 *(o
->ovar
) = ! o
->odefault
;
392 * If user gave the lower case letter, then switch
393 * to 1 unless already 1, in which case make it 0.
394 * If user gave the upper case letter, then switch
395 * to 2 unless already 2, in which case make it 0.
397 switch (how_toggle
) {
399 *(o
->ovar
) = flip_triple(*(o
->ovar
), lower
);
402 *(o
->ovar
) = o
->odefault
;
405 *(o
->ovar
) = flip_triple(o
->odefault
, lower
);
411 * String: don't do anything here.
412 * The handling function will do everything.
414 switch (how_toggle
) {
417 error("Cannot use \"-+\" or \"--\" "
418 "for a string option", NULL
);
424 * Number: set the variable to the given number.
426 switch (how_toggle
) {
428 num
= getnum(&s
, NULL
, &err
);
433 *(o
->ovar
) = o
->odefault
;
436 error("Can't use \"-!\" for a numeric option",
445 * Call the handling function for any special action
446 * specific to this option.
448 if (o
->ofunc
!= NULL
)
449 (*o
->ofunc
)((how_toggle
== OPT_NO_TOGGLE
) ? QUERY
: TOGGLE
, s
);
451 if (how_toggle
!= OPT_NO_TOGGLE
&& (o
->otype
& HL_REPAINT
))
456 * Print a message describing the new setting.
458 switch (o
->otype
& OTYPE
) {
462 * Print the odesc message.
464 error(o
->odesc
[*(o
->ovar
)], NULL
);
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.
490 flip_triple(int val
, int lc
)
493 return ((val
== OPT_ON
) ? OPT_OFF
: OPT_ON
);
495 return ((val
== OPT_ONPLUS
) ? OPT_OFF
: OPT_ONPLUS
);
499 * Determine if an option takes a parameter.
502 opt_has_param(struct loption
*o
)
506 if (o
->otype
& (BOOL
|TRIPLE
|NOVAR
|NO_TOGGLE
))
512 * Return the prompt to be used for a given option letter.
513 * Only string and number valued options have prompts.
516 opt_prompt(struct loption
*o
)
518 if (o
== NULL
|| (o
->otype
& (STRING
|NUMBER
)) == 0)
520 return (o
->odesc
[0]);
524 * Return whether or not there is a string option pending;
525 * that is, if the previous option was a string-valued option letter
526 * (like -P) without a following string.
527 * In that case, the current option is taken to be the string for
528 * the previous option.
533 return (pendopt
!= NULL
);
537 * Print error message about missing string.
540 nostring(char *printopt
)
543 parg
.p_string
= printopt
;
544 error("Value is required after %s", &parg
);
548 * Print error message if a STRING type option is not followed by a string.
553 nostring(opt_desc(pendopt
));
557 * Scan to end of string or to an END_OPTION_STRING character.
558 * In the latter case, replace the char with a null char.
559 * Return a pointer to the remainder of the string, if any.
562 optstring(char *s
, char **p_str
, char *printopt
, char *validchars
)
571 /* Alloc could be more than needed, but not worth trimming. */
572 *p_str
= ecalloc(strlen(s
)+1, sizeof (char));
575 for (p
= s
; *p
!= '\0'; p
++) {
576 if (opt_use_backslash
&& *p
== '\\' && p
[1] != '\0') {
577 /* Take next char literally. */
580 if (*p
== END_OPTION_STRING
||
581 (validchars
!= NULL
&&
582 strchr(validchars
, *p
) == NULL
))
583 /* End of option string. */
595 num_error(char *printopt
, int *errp
)
603 if (printopt
!= NULL
) {
604 parg
.p_string
= printopt
;
605 error("Number is required after %s", &parg
);
611 * Translate a string into a number.
612 * Like atoi(), but takes a pointer to a char *, and updates
613 * the char * to point after the translated number.
616 getnum(char **sp
, char *printopt
, int *errp
)
628 if (*s
< '0' || *s
> '9')
629 return (num_error(printopt
, errp
));
632 while (*s
>= '0' && *s
<= '9')
633 n
= 10 * n
+ *s
++ - '0';
643 * Translate a string into a fraction, represented by the part of a
644 * number which would follow a decimal point.
645 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
646 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
649 getfraction(char **sp
, char *printopt
, int *errp
)
656 if (*s
< '0' || *s
> '9')
657 return (num_error(printopt
, errp
));
659 for (; *s
>= '0' && *s
<= '9'; s
++) {
660 frac
= (frac
* 10) + (*s
- '0');
663 if (fraclen
> NUM_LOG_FRAC_DENOM
)
664 while (fraclen
-- > NUM_LOG_FRAC_DENOM
)
667 while (fraclen
++ < NUM_LOG_FRAC_DENOM
)
677 * Get the value of the -e flag.
680 get_quit_at_eof(void)
682 return (quit_at_eof
);