* Process command line options.
* Each option is a single letter which controls a program variable.
* The options have defaults which may be changed via
* the command line option, toggled via the "-" command,
* or queried via the "_" command.
static struct option
*pendopt
;
static char *optstring();
static int flip_triple();
extern int screen_trashed
;
extern char *every_first_cmd
;
* Scan an argument (either from the command line or from the
* LESS environment variable) and process it.
register struct option
*o
;
* If we have a pending string-valued option, handle it now.
* This happens if the previous option was, for example, "-P"
* without a following string. In that case, the current
* option is simply the string for the previous option.
(*pendopt
->ofunc
)(INIT
, s
);
* Check some special cases first.
* "-+" means set these options back to their defaults.
* (They may have been set otherwise by previous
if (set_default
= (*s
== '+'))
* An option prefixed by a "+" is ungotten, so
* that it is interpreted as less commands
* processed at the start of the first input file.
* "++" means process the commands at the start of
every_first_cmd
= save(++s
);
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
* Special "more" compatibility form "-<number>"
* instead of -z<number> to set the scrolling
* Look up the option letter in the option table.
parg
.p_string
= propt(c
);
error("There is no %s flag (\"less -\\?\" for help)",
switch (o
->otype
& OTYPE
)
*(o
->ovar
) = o
->odefault
;
*(o
->ovar
) = ! o
->odefault
;
*(o
->ovar
) = o
->odefault
;
*(o
->ovar
) = flip_triple(o
->odefault
,
* Set pendopt and return.
* We will get the string next time
* Don't do anything here.
* All processing of STRING options is done by
*(o
->ovar
) = getnum(&s
, c
, (int*)NULL
);
* If the option has a handling function, call it.
* Toggle command line flags from within the program.
* Used by the "-" and "_" commands.
* OPT_NO_TOGGLE just report the current setting, without changing it.
* OPT_TOGGLE invert the current setting
* OPT_UNSET set to the default value
* OPT_SET set to the inverse of the default value
toggle_option(c
, s
, how_toggle
)
register struct option
*o
;
* Look up the option letter in the option table.
parg
.p_string
= propt(c
);
error("There is no %s flag", &parg
);
if (how_toggle
== OPT_TOGGLE
&& (o
->otype
& NO_TOGGLE
))
parg
.p_string
= propt(c
);
error("Cannot change the %s flag", &parg
);
* Check for something which appears to be a do_toggle
* (because the "-" command was used), but really is not.
* This could be a string option with no string, or
* a number option with no number.
switch (o
->otype
& OTYPE
)
if (how_toggle
== OPT_TOGGLE
&& *s
== '\0')
how_toggle
= OPT_NO_TOGGLE
;
* Now actually toggle (change) the variable.
if (how_toggle
!= OPT_NO_TOGGLE
)
switch (o
->otype
& OTYPE
)
*(o
->ovar
) = ! *(o
->ovar
);
*(o
->ovar
) = o
->odefault
;
*(o
->ovar
) = ! o
->odefault
;
* If user gave the lower case letter, then switch
* to 1 unless already 1, in which case make it 0.
* If user gave the upper case letter, then switch
* to 2 unless already 2, in which case make it 0.
*(o
->ovar
) = flip_triple(*(o
->ovar
),
*(o
->ovar
) = o
->odefault
;
*(o
->ovar
) = flip_triple(o
->odefault
,
* String: don't do anything here.
* The handling function will do everything.
error("Can't use \"-+\" or \"--\" for a string flag",
* Number: set the variable to the given number.
num
= getnum(&s
, '\0', &err
);
*(o
->ovar
) = o
->odefault
;
error("Can't use \"--\" for a numeric flag",
* Call the handling function for any special action
* specific to this option.
(*o
->ofunc
)((how_toggle
==OPT_NO_TOGGLE
) ? QUERY
: TOGGLE
, s
);
* Print a message describing the new setting.
switch (o
->otype
& OTYPE
)
* Print the odesc message.
error(o
->odesc
[*(o
->ovar
)], NULL_PARG
);
* The message is in odesc[1] and has a %d for
* the value of the variable.
error(o
->odesc
[1], &parg
);
* Message was already printed by the handling function.
if (how_toggle
!= OPT_NO_TOGGLE
&& (o
->otype
& REPAINT
))
* "Toggle" a triple-valued option.
return ((val
== 1) ? 0 : 1);
return ((val
== 2) ? 0 : 2);
* Return a string suitable for printing as the "name" of an option.
* For example, if the option letter is 'x', just return "-x".
sprintf(buf
, "-%s", prchar(c
));
* Determine if an option is a single character option (BOOL or TRIPLE),
* or if it a multi-character option (NUMBER).
register struct option
*o
;
return (o
->otype
& (BOOL
|TRIPLE
|NOVAR
|NO_TOGGLE
));
* Return the prompt to be used for a given option letter.
* Only string and number valued options have prompts.
register struct option
*o
;
if (o
== NULL
|| (o
->otype
& (STRING
|NUMBER
)) == 0)
* Return whether or not there is a string option pending;
* that is, if the previous option was a string-valued option letter
* (like -P) without a following string.
* In that case, the current option is taken to be the string for
return (pendopt
!= NULL
);
* Print error message about missing string.
parg
.p_string
= propt(c
);
error("String is required after %s", &parg
);
* Print error message if a STRING type option is not followed by a string.
nostring(pendopt
->oletter
);
* Scan to end of string or to an END_OPTION_STRING character.
* In the latter case, replace the char with a null char.
* Return a pointer to the remainder of the string, if any.
for (p
= s
; *p
!= '\0'; p
++)
if (*p
== END_OPTION_STRING
)
* Translate a string into a number.
* Like atoi(), but takes a pointer to a char *, and updates
* the char * to point after the translated number.
if (*s
< '0' || *s
> '9')
parg
.p_string
= propt(c
);
error("Number is required after %s", &parg
);
while (*s
>= '0' && *s
<= '9')