Changeset View
Changeset View
Standalone View
Standalone View
head/contrib/less/option.c
Show All 17 Lines | |||||
*/ | */ | ||||
#include "less.h" | #include "less.h" | ||||
#include "option.h" | #include "option.h" | ||||
static struct loption *pendopt; | static struct loption *pendopt; | ||||
public int plusoption = FALSE; | public int plusoption = FALSE; | ||||
static char *optstring(); | static char *optstring(char *s, char **p_str, char *printopt, char *validchars); | ||||
static int flip_triple(); | static int flip_triple(int val, int lc); | ||||
extern int screen_trashed; | extern int screen_trashed; | ||||
extern int less_is_more; | extern int less_is_more; | ||||
extern int quit_at_eof; | extern int quit_at_eof; | ||||
extern char *every_first_cmd; | extern char *every_first_cmd; | ||||
extern int opt_use_backslash; | extern int opt_use_backslash; | ||||
/* | /* | ||||
* Return a printable description of an option. | * Return a printable description of an option. | ||||
*/ | */ | ||||
static char * | static char * | ||||
opt_desc(o) | opt_desc(struct loption *o) | ||||
struct loption *o; | |||||
{ | { | ||||
static char buf[OPTNAME_MAX + 10]; | static char buf[OPTNAME_MAX + 10]; | ||||
if (o->oletter == OLETTER_NONE) | if (o->oletter == OLETTER_NONE) | ||||
SNPRINTF1(buf, sizeof(buf), "--%s", o->onames->oname); | SNPRINTF1(buf, sizeof(buf), "--%s", o->onames->oname); | ||||
else | else | ||||
SNPRINTF2(buf, sizeof(buf), "-%c (--%s)", o->oletter, o->onames->oname); | SNPRINTF2(buf, sizeof(buf), "-%c (--%s)", o->oletter, o->onames->oname); | ||||
return (buf); | return (buf); | ||||
} | } | ||||
/* | /* | ||||
* Return a string suitable for printing as the "name" of an option. | * Return a string suitable for printing as the "name" of an option. | ||||
* For example, if the option letter is 'x', just return "-x". | * For example, if the option letter is 'x', just return "-x". | ||||
*/ | */ | ||||
public char * | public char * | ||||
propt(c) | propt(int c) | ||||
int c; | |||||
{ | { | ||||
static char buf[8]; | static char buf[8]; | ||||
sprintf(buf, "-%s", prchar(c)); | sprintf(buf, "-%s", prchar(c)); | ||||
return (buf); | return (buf); | ||||
} | } | ||||
/* | /* | ||||
* Scan an argument (either from the command line or from the | * Scan an argument (either from the command line or from the | ||||
* LESS environment variable) and process it. | * LESS environment variable) and process it. | ||||
*/ | */ | ||||
public void | public void | ||||
scan_option(s) | scan_option(char *s) | ||||
char *s; | |||||
{ | { | ||||
register struct loption *o; | struct loption *o; | ||||
register int optc; | int optc; | ||||
char *optname; | char *optname; | ||||
char *printopt; | char *printopt; | ||||
char *str; | char *str; | ||||
int set_default; | int set_default; | ||||
int lc; | int lc; | ||||
int err; | int err; | ||||
PARG parg; | PARG parg; | ||||
▲ Show 20 Lines • Show All 212 Lines • ▼ Show 20 Lines | |||||
* Used by the "-" and "_" commands. | * Used by the "-" and "_" commands. | ||||
* how_toggle may be: | * how_toggle may be: | ||||
* OPT_NO_TOGGLE just report the current setting, without changing it. | * OPT_NO_TOGGLE just report the current setting, without changing it. | ||||
* OPT_TOGGLE invert the current setting | * OPT_TOGGLE invert the current setting | ||||
* OPT_UNSET set to the default value | * OPT_UNSET set to the default value | ||||
* OPT_SET set to the inverse of the default value | * OPT_SET set to the inverse of the default value | ||||
*/ | */ | ||||
public void | public void | ||||
toggle_option(o, lower, s, how_toggle) | toggle_option(struct loption *o, int lower, char *s, int how_toggle) | ||||
struct loption *o; | |||||
int lower; | |||||
char *s; | |||||
int how_toggle; | |||||
{ | { | ||||
register int num; | int num; | ||||
int no_prompt; | int no_prompt; | ||||
int err; | int err; | ||||
PARG parg; | PARG parg; | ||||
no_prompt = (how_toggle & OPT_NO_PROMPT); | no_prompt = (how_toggle & OPT_NO_PROMPT); | ||||
how_toggle &= ~OPT_NO_PROMPT; | how_toggle &= ~OPT_NO_PROMPT; | ||||
if (o == NULL) | if (o == NULL) | ||||
▲ Show 20 Lines • Show All 163 Lines • ▼ Show 20 Lines | #endif | ||||
if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT)) | if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT)) | ||||
screen_trashed = TRUE; | screen_trashed = TRUE; | ||||
} | } | ||||
/* | /* | ||||
* "Toggle" a triple-valued option. | * "Toggle" a triple-valued option. | ||||
*/ | */ | ||||
static int | static int | ||||
flip_triple(val, lc) | flip_triple(int val, int lc) | ||||
int val; | |||||
int lc; | |||||
{ | { | ||||
if (lc) | if (lc) | ||||
return ((val == OPT_ON) ? OPT_OFF : OPT_ON); | return ((val == OPT_ON) ? OPT_OFF : OPT_ON); | ||||
else | else | ||||
return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS); | return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS); | ||||
} | } | ||||
/* | /* | ||||
* Determine if an option takes a parameter. | * Determine if an option takes a parameter. | ||||
*/ | */ | ||||
public int | public int | ||||
opt_has_param(o) | opt_has_param(struct loption *o) | ||||
struct loption *o; | |||||
{ | { | ||||
if (o == NULL) | if (o == NULL) | ||||
return (0); | return (0); | ||||
if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE)) | if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE)) | ||||
return (0); | return (0); | ||||
return (1); | return (1); | ||||
} | } | ||||
/* | /* | ||||
* Return the prompt to be used for a given option letter. | * Return the prompt to be used for a given option letter. | ||||
* Only string and number valued options have prompts. | * Only string and number valued options have prompts. | ||||
*/ | */ | ||||
public char * | public char * | ||||
opt_prompt(o) | opt_prompt(struct loption *o) | ||||
struct loption *o; | |||||
{ | { | ||||
if (o == NULL || (o->otype & (STRING|NUMBER)) == 0) | if (o == NULL || (o->otype & (STRING|NUMBER)) == 0) | ||||
return ("?"); | return ("?"); | ||||
return (o->odesc[0]); | return (o->odesc[0]); | ||||
} | } | ||||
/* | /* | ||||
* Return whether or not there is a string option pending; | * Return whether or not there is a string option pending; | ||||
* that is, if the previous option was a string-valued option letter | * that is, if the previous option was a string-valued option letter | ||||
* (like -P) without a following string. | * (like -P) without a following string. | ||||
* In that case, the current option is taken to be the string for | * In that case, the current option is taken to be the string for | ||||
* the previous option. | * the previous option. | ||||
*/ | */ | ||||
public int | public int | ||||
isoptpending() | isoptpending(void) | ||||
{ | { | ||||
return (pendopt != NULL); | return (pendopt != NULL); | ||||
} | } | ||||
/* | /* | ||||
* Print error message about missing string. | * Print error message about missing string. | ||||
*/ | */ | ||||
static void | static void | ||||
nostring(printopt) | nostring(char *printopt) | ||||
char *printopt; | |||||
{ | { | ||||
PARG parg; | PARG parg; | ||||
parg.p_string = printopt; | parg.p_string = printopt; | ||||
error("Value is required after %s", &parg); | error("Value is required after %s", &parg); | ||||
} | } | ||||
/* | /* | ||||
* Print error message if a STRING type option is not followed by a string. | * Print error message if a STRING type option is not followed by a string. | ||||
*/ | */ | ||||
public void | public void | ||||
nopendopt() | nopendopt(void) | ||||
{ | { | ||||
nostring(opt_desc(pendopt)); | nostring(opt_desc(pendopt)); | ||||
} | } | ||||
/* | /* | ||||
* Scan to end of string or to an END_OPTION_STRING character. | * Scan to end of string or to an END_OPTION_STRING character. | ||||
* In the latter case, replace the char with a null char. | * In the latter case, replace the char with a null char. | ||||
* Return a pointer to the remainder of the string, if any. | * Return a pointer to the remainder of the string, if any. | ||||
*/ | */ | ||||
static char * | static char * | ||||
optstring(s, p_str, printopt, validchars) | optstring(char *s, char **p_str, char *printopt, char *validchars) | ||||
char *s; | |||||
char **p_str; | |||||
char *printopt; | |||||
char *validchars; | |||||
{ | { | ||||
register char *p; | char *p; | ||||
register char *out; | char *out; | ||||
if (*s == '\0') | if (*s == '\0') | ||||
{ | { | ||||
nostring(printopt); | nostring(printopt); | ||||
return (NULL); | return (NULL); | ||||
} | } | ||||
/* Alloc could be more than needed, but not worth trimming. */ | /* Alloc could be more than needed, but not worth trimming. */ | ||||
*p_str = (char *) ecalloc(strlen(s)+1, sizeof(char)); | *p_str = (char *) ecalloc(strlen(s)+1, sizeof(char)); | ||||
Show All 16 Lines | optstring(char *s, char **p_str, char *printopt, char *validchars) | ||||
} | } | ||||
*out = '\0'; | *out = '\0'; | ||||
return (p); | return (p); | ||||
} | } | ||||
/* | /* | ||||
*/ | */ | ||||
static int | static int | ||||
num_error(printopt, errp) | num_error(char *printopt, int *errp) | ||||
char *printopt; | |||||
int *errp; | |||||
{ | { | ||||
PARG parg; | PARG parg; | ||||
if (errp != NULL) | if (errp != NULL) | ||||
{ | { | ||||
*errp = TRUE; | *errp = TRUE; | ||||
return (-1); | return (-1); | ||||
} | } | ||||
if (printopt != NULL) | if (printopt != NULL) | ||||
{ | { | ||||
parg.p_string = printopt; | parg.p_string = printopt; | ||||
error("Number is required after %s", &parg); | error("Number is required after %s", &parg); | ||||
} | } | ||||
return (-1); | return (-1); | ||||
} | } | ||||
/* | /* | ||||
* Translate a string into a number. | * Translate a string into a number. | ||||
* Like atoi(), but takes a pointer to a char *, and updates | * Like atoi(), but takes a pointer to a char *, and updates | ||||
* the char * to point after the translated number. | * the char * to point after the translated number. | ||||
*/ | */ | ||||
public int | public int | ||||
getnum(sp, printopt, errp) | getnum(char **sp, char *printopt, int *errp) | ||||
char **sp; | |||||
char *printopt; | |||||
int *errp; | |||||
{ | { | ||||
register char *s; | char *s; | ||||
register int n; | int n; | ||||
register int neg; | int neg; | ||||
s = skipsp(*sp); | s = skipsp(*sp); | ||||
neg = FALSE; | neg = FALSE; | ||||
if (*s == '-') | if (*s == '-') | ||||
{ | { | ||||
neg = TRUE; | neg = TRUE; | ||||
s++; | s++; | ||||
} | } | ||||
Show All 13 Lines | |||||
/* | /* | ||||
* Translate a string into a fraction, represented by the part of a | * Translate a string into a fraction, represented by the part of a | ||||
* number which would follow a decimal point. | * number which would follow a decimal point. | ||||
* The value of the fraction is returned as parts per NUM_FRAC_DENOM. | * The value of the fraction is returned as parts per NUM_FRAC_DENOM. | ||||
* That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM. | * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM. | ||||
*/ | */ | ||||
public long | public long | ||||
getfraction(sp, printopt, errp) | getfraction(char **sp, char *printopt, int *errp) | ||||
char **sp; | |||||
char *printopt; | |||||
int *errp; | |||||
{ | { | ||||
register char *s; | char *s; | ||||
long frac = 0; | long frac = 0; | ||||
int fraclen = 0; | int fraclen = 0; | ||||
s = skipsp(*sp); | s = skipsp(*sp); | ||||
if (*s < '0' || *s > '9') | if (*s < '0' || *s > '9') | ||||
return (num_error(printopt, errp)); | return (num_error(printopt, errp)); | ||||
for ( ; *s >= '0' && *s <= '9'; s++) | for ( ; *s >= '0' && *s <= '9'; s++) | ||||
Show All 13 Lines | getfraction(char **sp, char *printopt, int *errp) | ||||
return (frac); | return (frac); | ||||
} | } | ||||
/* | /* | ||||
* Get the value of the -e flag. | * Get the value of the -e flag. | ||||
*/ | */ | ||||
public int | public int | ||||
get_quit_at_eof() | get_quit_at_eof(void) | ||||
{ | { | ||||
if (!less_is_more) | if (!less_is_more) | ||||
return quit_at_eof; | return quit_at_eof; | ||||
/* When less_is_more is set, the -e flag semantics are different. */ | /* When less_is_more is set, the -e flag semantics are different. */ | ||||
return quit_at_eof ? OPT_ONPLUS : OPT_ON; | return quit_at_eof ? OPT_ONPLUS : OPT_ON; | ||||
} | } |