2
* Copyright (C) 1984-2000 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 about less, or for information on how to
8
* contact the author, 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
public int plusoption = FALSE;
28
static char *optstring();
29
static int flip_triple();
31
extern int screen_trashed;
32
extern char *every_first_cmd;
35
* Scan an argument (either from the command line or from the
36
* LESS environment variable) and process it.
42
register struct loption *o;
56
* If we have a pending option which requires an argument,
58
* This happens if the previous option was, for example, "-P"
59
* without a following string. In that case, the current
60
* option is simply the argument for the previous option.
64
switch (pendopt->otype & OTYPE)
67
(*pendopt->ofunc)(INIT, s);
70
printopt = propt(pendopt->oletter);
71
*(pendopt->ovar) = getnum(&s, printopt, (int*)NULL);
84
* Check some special cases first.
90
case END_OPTION_STRING:
94
* "--" indicates an option name instead of a letter.
102
* "-+" means set these options back to their defaults.
103
* (They may have been set otherwise by previous
106
set_default = (*s == '+');
112
* An option prefixed by a "+" is ungotten, so
113
* that it is interpreted as less commands
114
* processed at the start of the first input file.
115
* "++" means process the commands at the start of
119
s = optstring(s, &str, propt('+'), NULL);
121
every_first_cmd = save(++str);
125
case '0': case '1': case '2': case '3': case '4':
126
case '5': case '6': case '7': case '8': case '9':
128
* Special "more" compatibility form "-<number>"
129
* instead of -z<number> to set the scrolling
138
* Not a special case.
139
* Look up the option letter in the option table.
144
printopt = propt(optc);
145
lc = SIMPLE_IS_LOWER(optc);
150
lc = SIMPLE_IS_LOWER(optname[0]);
151
o = findopt_name(&optname, NULL, &err);
154
if (*s == '\0' || *s == ' ')
157
* The option name matches exactly.
160
} else if (*s == '=')
163
* The option name is followed by "=value".
166
(o->otype & OTYPE) != STRING &&
167
(o->otype & OTYPE) != NUMBER)
169
parg.p_string = printopt;
170
error("The %s option should not be followed by =",
178
* The specified name is longer than the
186
parg.p_string = printopt;
187
if (err == OPT_AMBIG)
188
error("%s is an ambiguous abbreviation (\"less --help\" for help)",
191
error("There is no %s option (\"less --help\" for help)",
197
switch (o->otype & OTYPE)
201
*(o->ovar) = o->odefault;
203
*(o->ovar) = ! o->odefault;
207
*(o->ovar) = o->odefault;
209
*(o->ovar) = flip_triple(o->odefault, lc);
215
* Set pendopt and return.
216
* We will get the string next time
217
* scan_option is called.
223
* Don't do anything here.
224
* All processing of STRING options is done by
225
* the handling function.
227
s = optstring(s, &str, printopt, o->odesc[1]);
235
*(o->ovar) = getnum(&s, printopt, (int*)NULL);
239
* If the option has a handling function, call it.
241
if (o->ofunc != NULL)
242
(*o->ofunc)(INIT, str);
247
* Toggle command line flags from within the program.
248
* Used by the "-" and "_" commands.
250
* OPT_NO_TOGGLE just report the current setting, without changing it.
251
* OPT_TOGGLE invert the current setting
252
* OPT_UNSET set to the default value
253
* OPT_SET set to the inverse of the default value
256
toggle_option(c, s, how_toggle)
261
register struct loption *o;
267
no_prompt = (how_toggle & OPT_NO_PROMPT);
268
how_toggle &= ~OPT_NO_PROMPT;
271
* Look up the option letter in the option table.
276
parg.p_string = propt(c);
277
error("There is no %s option", &parg);
281
if (how_toggle == OPT_TOGGLE && (o->otype & NO_TOGGLE))
283
parg.p_string = propt(c);
284
error("Cannot change the %s option", &parg);
288
if (how_toggle == OPT_NO_TOGGLE && (o->otype & NO_QUERY))
290
parg.p_string = propt(c);
291
error("Cannot query the %s option", &parg);
296
* Check for something which appears to be a do_toggle
297
* (because the "-" command was used), but really is not.
298
* This could be a string option with no string, or
299
* a number option with no number.
301
switch (o->otype & OTYPE)
305
if (how_toggle == OPT_TOGGLE && *s == '\0')
306
how_toggle = OPT_NO_TOGGLE;
311
if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
316
* Now actually toggle (change) the variable.
318
if (how_toggle != OPT_NO_TOGGLE)
320
switch (o->otype & OTYPE)
329
*(o->ovar) = ! *(o->ovar);
332
*(o->ovar) = o->odefault;
335
*(o->ovar) = ! o->odefault;
342
* If user gave the lower case letter, then switch
343
* to 1 unless already 1, in which case make it 0.
344
* If user gave the upper case letter, then switch
345
* to 2 unless already 2, in which case make it 0.
350
*(o->ovar) = flip_triple(*(o->ovar),
354
*(o->ovar) = o->odefault;
357
*(o->ovar) = flip_triple(o->odefault,
364
* String: don't do anything here.
365
* The handling function will do everything.
371
error("Cannot use \"-+\" or \"--\" for a string option",
378
* Number: set the variable to the given number.
383
num = getnum(&s, '\0', &err);
388
*(o->ovar) = o->odefault;
391
error("Can't use \"-!\" for a numeric option",
400
* Call the handling function for any special action
401
* specific to this option.
403
if (o->ofunc != NULL)
404
(*o->ofunc)((how_toggle==OPT_NO_TOGGLE) ? QUERY : TOGGLE, s);
407
if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
414
* Print a message describing the new setting.
416
switch (o->otype & OTYPE)
421
* Print the odesc message.
423
error(o->odesc[*(o->ovar)], NULL_PARG);
427
* The message is in odesc[1] and has a %d for
428
* the value of the variable.
430
parg.p_int = *(o->ovar);
431
error(o->odesc[1], &parg);
435
* Message was already printed by the handling function.
441
if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
442
screen_trashed = TRUE;
446
* "Toggle" a triple-valued option.
454
return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
456
return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
460
* Return a string suitable for printing as the "name" of an option.
461
* For example, if the option letter is 'x', just return "-x".
469
sprintf(buf, "-%s", prchar(c));
474
* Determine if an option is a single character option (BOOL or TRIPLE),
475
* or if it a multi-character option (NUMBER).
478
single_char_option(c)
481
register struct loption *o;
486
return ((o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE)) != 0);
490
* Return the prompt to be used for a given option letter.
491
* Only string and number valued options have prompts.
497
register struct loption *o;
500
if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
502
return (o->odesc[0]);
506
* Return whether or not there is a string option pending;
507
* that is, if the previous option was a string-valued option letter
508
* (like -P) without a following string.
509
* In that case, the current option is taken to be the string for
510
* the previous option.
515
return (pendopt != NULL);
519
* Print error message about missing string.
526
parg.p_string = printopt;
527
error("Value is required after %s", &parg);
531
* Print error message if a STRING type option is not followed by a string.
536
nostring(propt(pendopt->oletter));
540
* Scan to end of string or to an END_OPTION_STRING character.
541
* In the latter case, replace the char with a null char.
542
* Return a pointer to the remainder of the string, if any.
545
optstring(s, p_str, printopt, validchars)
559
for (p = s; *p != '\0'; p++)
561
if (*p == END_OPTION_STRING ||
562
(validchars != NULL && strchr(validchars, *p) == NULL))
566
case END_OPTION_STRING:
567
case ' ': case '\t': case '-':
568
/* Replace the char with a null to terminate string. */
572
/* Cannot replace char; make a copy of the string. */
573
*p_str = (char *) ecalloc(p-s+1, sizeof(char));
574
strncpy(*p_str, s, p-s);
575
(*p_str)[p-s] = '\0';
585
* Translate a string into a number.
586
* Like atoi(), but takes a pointer to a char *, and updates
587
* the char * to point after the translated number.
590
getnum(sp, printopt, errp)
607
if (*s < '0' || *s > '9')
614
parg.p_string = printopt;
615
error("Number is required after %s", &parg);
620
while (*s >= '0' && *s <= '9')
621
n = 10 * n + *s++ - '0';