2 * Copyright (C) 1984-2022 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, see the README file.
12 * Process command line options.
14 * Each option is a single letter which controls a program variable.
15 * The options have defaults which may be changed via
16 * the command line option, toggled via the "-" command,
17 * or queried via the "_" command.
23 static struct loption
*pendopt
;
24 public int plusoption
= FALSE
;
26 static char *optstring
LESSPARAMS((char *s
, char **p_str
, char *printopt
, char *validchars
));
27 static int flip_triple
LESSPARAMS((int val
, int lc
));
29 extern int screen_trashed
;
30 extern int less_is_more
;
31 extern int quit_at_eof
;
32 extern char *every_first_cmd
;
33 extern int opt_use_backslash
;
36 * Return a printable description of an option.
42 static char buf
[OPTNAME_MAX
+ 10];
43 if (o
->oletter
== OLETTER_NONE
)
44 SNPRINTF1(buf
, sizeof(buf
), "--%s", o
->onames
->oname
);
46 SNPRINTF2(buf
, sizeof(buf
), "-%c (--%s)", o
->oletter
, o
->onames
->oname
);
51 * Return a string suitable for printing as the "name" of an option.
52 * For example, if the option letter is 'x', just return "-x".
58 static char buf
[MAX_PRCHAR_LEN
+2];
60 sprintf(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.
94 switch (pendopt
->otype
& OTYPE
)
97 (*pendopt
->ofunc
)(INIT
, s
);
100 printopt
= opt_desc(pendopt
);
101 *(pendopt
->ovar
) = getnum(&s
, printopt
, (int*)NULL
);
114 * Check some special cases first.
120 case END_OPTION_STRING
:
124 * "--" indicates an option name instead of a letter.
132 * "-+" means set these options back to their defaults.
133 * (They may have been set otherwise by previous
136 set_default
= (*s
== '+');
142 * An option prefixed by a "+" is ungotten, so
143 * that it is interpreted as less commands
144 * processed at the start of the first input file.
145 * "++" means process the commands at the start of
149 s
= optstring(s
, &str
, propt('+'), NULL
);
154 if (every_first_cmd
!= NULL
)
155 free(every_first_cmd
);
156 every_first_cmd
= save(str
+1);
160 ungetcc_back(CHAR_END_COMMAND
);
164 case '0': case '1': case '2': case '3': case '4':
165 case '5': case '6': case '7': case '8': case '9':
167 * Special "more" compatibility form "-<number>"
168 * instead of -z<number> to set the scrolling
181 * Not a special case.
182 * Look up the option letter in the option table.
187 printopt
= propt(optc
);
188 lc
= ASCII_IS_LOWER(optc
);
193 lc
= ASCII_IS_LOWER(optname
[0]);
194 o
= findopt_name(&optname
, NULL
, &err
);
197 if (*s
== '\0' || *s
== ' ')
200 * The option name matches exactly.
203 } else if (*s
== '=')
206 * The option name is followed by "=value".
209 (o
->otype
& OTYPE
) != STRING
&&
210 (o
->otype
& OTYPE
) != NUMBER
)
212 parg
.p_string
= printopt
;
213 error("The %s option should not be followed by =",
221 * The specified name is longer than the
229 parg
.p_string
= printopt
;
230 if (err
== OPT_AMBIG
)
231 error("%s is an ambiguous abbreviation (\"less --help\" for help)",
234 error("There is no %s option (\"less --help\" for help)",
240 switch (o
->otype
& OTYPE
)
244 *(o
->ovar
) = o
->odefault
;
246 *(o
->ovar
) = ! o
->odefault
;
250 *(o
->ovar
) = o
->odefault
;
252 *(o
->ovar
) = flip_triple(o
->odefault
, lc
);
258 * Set pendopt and return.
259 * We will get the string next time
260 * scan_option is called.
266 * Don't do anything here.
267 * All processing of STRING options is done by
268 * the handling function.
272 s
= optstring(s
, &str
, printopt
, o
->odesc
[1]);
282 *(o
->ovar
) = getnum(&s
, printopt
, (int*)NULL
);
286 * If the option has a handling function, call it.
288 if (o
->ofunc
!= NULL
)
289 (*o
->ofunc
)(INIT
, str
);
296 * Toggle command line flags from within the program.
297 * Used by the "-" and "_" commands.
299 * OPT_NO_TOGGLE just report the current setting, without changing it.
300 * OPT_TOGGLE invert the current setting
301 * OPT_UNSET set to the default value
302 * OPT_SET set to the inverse of the default value
305 toggle_option(o
, lower
, s
, how_toggle
)
316 no_prompt
= (how_toggle
& OPT_NO_PROMPT
);
317 how_toggle
&= ~OPT_NO_PROMPT
;
321 error("No such option", NULL_PARG
);
325 if (how_toggle
== OPT_TOGGLE
&& (o
->otype
& NO_TOGGLE
))
327 parg
.p_string
= opt_desc(o
);
328 error("Cannot change the %s option", &parg
);
332 if (how_toggle
== OPT_NO_TOGGLE
&& (o
->otype
& NO_QUERY
))
334 parg
.p_string
= opt_desc(o
);
335 error("Cannot query the %s option", &parg
);
340 * Check for something which appears to be a do_toggle
341 * (because the "-" command was used), but really is not.
342 * This could be a string option with no string, or
343 * a number option with no number.
345 switch (o
->otype
& OTYPE
)
349 if (how_toggle
== OPT_TOGGLE
&& *s
== '\0')
350 how_toggle
= OPT_NO_TOGGLE
;
355 if (how_toggle
!= OPT_NO_TOGGLE
&& (o
->otype
& HL_REPAINT
))
360 * Now actually toggle (change) the variable.
362 if (how_toggle
!= OPT_NO_TOGGLE
)
364 switch (o
->otype
& OTYPE
)
373 *(o
->ovar
) = ! *(o
->ovar
);
376 *(o
->ovar
) = o
->odefault
;
379 *(o
->ovar
) = ! o
->odefault
;
386 * If user gave the lower case letter, then switch
387 * to 1 unless already 1, in which case make it 0.
388 * If user gave the upper case letter, then switch
389 * to 2 unless already 2, in which case make it 0.
394 *(o
->ovar
) = flip_triple(*(o
->ovar
), lower
);
397 *(o
->ovar
) = o
->odefault
;
400 *(o
->ovar
) = flip_triple(o
->odefault
, lower
);
406 * String: don't do anything here.
407 * The handling function will do everything.
413 error("Cannot use \"-+\" or \"--\" for a string option",
420 * Number: set the variable to the given number.
425 num
= getnum(&s
, NULL
, &err
);
430 *(o
->ovar
) = o
->odefault
;
433 error("Can't use \"-!\" for a numeric option",
442 * Call the handling function for any special action
443 * specific to this option.
445 if (o
->ofunc
!= NULL
)
446 (*o
->ofunc
)((how_toggle
==OPT_NO_TOGGLE
) ? QUERY
: TOGGLE
, s
);
449 if (how_toggle
!= OPT_NO_TOGGLE
&& (o
->otype
& HL_REPAINT
))
456 * Print a message describing the new setting.
458 switch (o
->otype
& OTYPE
)
463 * Print the odesc message.
465 error(o
->odesc
[*(o
->ovar
)], NULL_PARG
);
469 * The message is in odesc[1] and has a %d for
470 * the value of the variable.
472 parg
.p_int
= *(o
->ovar
);
473 error(o
->odesc
[1], &parg
);
477 * Message was already printed by the handling function.
483 if (how_toggle
!= OPT_NO_TOGGLE
&& (o
->otype
& REPAINT
))
484 screen_trashed
= TRUE
;
488 * "Toggle" a triple-valued option.
496 return ((val
== OPT_ON
) ? OPT_OFF
: OPT_ON
);
498 return ((val
== OPT_ONPLUS
) ? OPT_OFF
: OPT_ONPLUS
);
502 * Determine if an option takes a parameter.
510 if (o
->otype
& (BOOL
|TRIPLE
|NOVAR
|NO_TOGGLE
))
516 * Return the prompt to be used for a given option letter.
517 * Only string and number valued options have prompts.
523 if (o
== NULL
|| (o
->otype
& (STRING
|NUMBER
)) == 0)
525 return (o
->odesc
[0]);
529 * If the specified option can be toggled, return NULL.
530 * Otherwise return an appropriate error message.
533 opt_toggle_disallowed(c
)
539 if (ch_getflags() & CH_CANSEEK
)
540 return "Input is not a pipe";
547 * Return whether or not there is a string option pending;
548 * that is, if the previous option was a string-valued option letter
549 * (like -P) without a following string.
550 * In that case, the current option is taken to be the string for
551 * the previous option.
554 isoptpending(VOID_PARAM
)
556 return (pendopt
!= NULL
);
560 * Print error message about missing string.
567 parg
.p_string
= printopt
;
568 error("Value is required after %s", &parg
);
572 * Print error message if a STRING type option is not followed by a string.
575 nopendopt(VOID_PARAM
)
577 nostring(opt_desc(pendopt
));
581 * Scan to end of string or to an END_OPTION_STRING character.
582 * In the latter case, replace the char with a null char.
583 * Return a pointer to the remainder of the string, if any.
586 optstring(s
, p_str
, printopt
, validchars
)
600 /* Alloc could be more than needed, but not worth trimming. */
601 *p_str
= (char *) ecalloc(strlen(s
)+1, sizeof(char));
604 for (p
= s
; *p
!= '\0'; p
++)
606 if (opt_use_backslash
&& *p
== '\\' && p
[1] != '\0')
608 /* Take next char literally. */
612 if (*p
== END_OPTION_STRING
||
613 (validchars
!= NULL
&& strchr(validchars
, *p
) == NULL
))
614 /* End of option string. */
626 num_error(printopt
, errp
)
637 if (printopt
!= NULL
)
639 parg
.p_string
= printopt
;
640 error("Number is required after %s", &parg
);
646 * Translate a string into a number.
647 * Like atoi(), but takes a pointer to a char *, and updates
648 * the char * to point after the translated number.
651 getnum(sp
, printopt
, errp
)
667 if (*s
< '0' || *s
> '9')
668 return (num_error(printopt
, errp
));
671 while (*s
>= '0' && *s
<= '9')
672 n
= 10 * n
+ *s
++ - '0';
682 * Translate a string into a fraction, represented by the part of a
683 * number which would follow a decimal point.
684 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
685 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
688 getfraction(sp
, printopt
, errp
)
698 if (*s
< '0' || *s
> '9')
699 return (num_error(printopt
, errp
));
701 for ( ; *s
>= '0' && *s
<= '9'; s
++)
703 frac
= (frac
* 10) + (*s
- '0');
706 if (fraclen
> NUM_LOG_FRAC_DENOM
)
707 while (fraclen
-- > NUM_LOG_FRAC_DENOM
)
710 while (fraclen
++ < NUM_LOG_FRAC_DENOM
)
720 * Get the value of the -e flag.
723 get_quit_at_eof(VOID_PARAM
)
727 /* When less_is_more is set, the -e flag semantics are different. */
728 return quit_at_eof
? OPT_ONPLUS
: OPT_ON
;