]>
diplodocus.org Git - nmh/blob - docs/historical/mh-6.8.5/miscellany/less-177/option.c
2 * Process command line options.
4 * Each option is a single letter which controls a program variable.
5 * The options have defaults which may be changed via
6 * the command line option, toggled via the "-" command,
7 * or queried via the "_" command.
13 static struct option
*pendopt
;
14 public int plusoption
;
17 static char *optstring();
18 static int flip_triple();
20 extern int screen_trashed
;
21 extern char *every_first_cmd
;
24 * Scan an argument (either from the command line or from the
25 * LESS environment variable) and process it.
31 register struct option
*o
;
41 * If we have a pending string-valued option, handle it now.
42 * This happens if the previous option was, for example, "-P"
43 * without a following string. In that case, the current
44 * option is simply the string for the previous option.
48 (*pendopt
->ofunc
)(INIT
, s
);
58 * Check some special cases first.
64 case END_OPTION_STRING
:
68 * "-+" means set these options back to their defaults.
69 * (They may have been set otherwise by previous
72 if (set_default
= (*s
== '+'))
77 * An option prefixed by a "+" is ungotten, so
78 * that it is interpreted as less commands
79 * processed at the start of the first input file.
80 * "++" means process the commands at the start of
85 every_first_cmd
= save(++s
);
90 case '0': case '1': case '2': case '3': case '4':
91 case '5': case '6': case '7': case '8': case '9':
93 * Special "more" compatibility form "-<number>"
94 * instead of -z<number> to set the scrolling
103 * Not a special case.
104 * Look up the option letter in the option table.
109 parg
.p_string
= propt(c
);
110 error("There is no %s flag (\"less -\\?\" for help)",
115 switch (o
->otype
& OTYPE
)
119 *(o
->ovar
) = o
->odefault
;
121 *(o
->ovar
) = ! o
->odefault
;
125 *(o
->ovar
) = o
->odefault
;
127 *(o
->ovar
) = flip_triple(o
->odefault
,
134 * Set pendopt and return.
135 * We will get the string next time
136 * scan_option is called.
142 * Don't do anything here.
143 * All processing of STRING options is done by
144 * the handling function.
150 *(o
->ovar
) = getnum(&s
, c
, (int*)NULL
);
154 * If the option has a handling function, call it.
156 if (o
->ofunc
!= NULL
)
157 (*o
->ofunc
)(INIT
, str
);
162 * Toggle command line flags from within the program.
163 * Used by the "-" and "_" commands.
165 * OPT_NO_TOGGLE just report the current setting, without changing it.
166 * OPT_TOGGLE invert the current setting
167 * OPT_UNSET set to the default value
168 * OPT_SET set to the inverse of the default value
171 toggle_option(c
, s
, how_toggle
)
176 register struct option
*o
;
182 * Look up the option letter in the option table.
187 parg
.p_string
= propt(c
);
188 error("There is no %s flag", &parg
);
192 if (how_toggle
== OPT_TOGGLE
&& (o
->otype
& NO_TOGGLE
))
194 parg
.p_string
= propt(c
);
195 error("Cannot change the %s flag", &parg
);
200 * Check for something which appears to be a do_toggle
201 * (because the "-" command was used), but really is not.
202 * This could be a string option with no string, or
203 * a number option with no number.
205 switch (o
->otype
& OTYPE
)
209 if (how_toggle
== OPT_TOGGLE
&& *s
== '\0')
210 how_toggle
= OPT_NO_TOGGLE
;
215 * Now actually toggle (change) the variable.
217 if (how_toggle
!= OPT_NO_TOGGLE
)
219 switch (o
->otype
& OTYPE
)
228 *(o
->ovar
) = ! *(o
->ovar
);
231 *(o
->ovar
) = o
->odefault
;
234 *(o
->ovar
) = ! o
->odefault
;
241 * If user gave the lower case letter, then switch
242 * to 1 unless already 1, in which case make it 0.
243 * If user gave the upper case letter, then switch
244 * to 2 unless already 2, in which case make it 0.
249 *(o
->ovar
) = flip_triple(*(o
->ovar
),
253 *(o
->ovar
) = o
->odefault
;
256 *(o
->ovar
) = flip_triple(o
->odefault
,
263 * String: don't do anything here.
264 * The handling function will do everything.
270 error("Can't use \"-+\" or \"--\" for a string flag",
277 * Number: set the variable to the given number.
282 num
= getnum(&s
, '\0', &err
);
287 *(o
->ovar
) = o
->odefault
;
290 error("Can't use \"--\" for a numeric flag",
299 * Call the handling function for any special action
300 * specific to this option.
302 if (o
->ofunc
!= NULL
)
303 (*o
->ofunc
)((how_toggle
==OPT_NO_TOGGLE
) ? QUERY
: TOGGLE
, s
);
306 * Print a message describing the new setting.
308 switch (o
->otype
& OTYPE
)
313 * Print the odesc message.
315 error(o
->odesc
[*(o
->ovar
)], NULL_PARG
);
319 * The message is in odesc[1] and has a %d for
320 * the value of the variable.
322 parg
.p_int
= *(o
->ovar
);
323 error(o
->odesc
[1], &parg
);
327 * Message was already printed by the handling function.
332 if (how_toggle
!= OPT_NO_TOGGLE
&& (o
->otype
& REPAINT
))
337 * "Toggle" a triple-valued option.
345 return ((val
== 1) ? 0 : 1);
347 return ((val
== 2) ? 0 : 2);
351 * Return a string suitable for printing as the "name" of an option.
352 * For example, if the option letter is 'x', just return "-x".
360 sprintf(buf
, "-%s", prchar(c
));
365 * Determine if an option is a single character option (BOOL or TRIPLE),
366 * or if it a multi-character option (NUMBER).
369 single_char_option(c
)
372 register struct option
*o
;
377 return (o
->otype
& (BOOL
|TRIPLE
|NOVAR
|NO_TOGGLE
));
381 * Return the prompt to be used for a given option letter.
382 * Only string and number valued options have prompts.
388 register struct option
*o
;
391 if (o
== NULL
|| (o
->otype
& (STRING
|NUMBER
)) == 0)
393 return (o
->odesc
[0]);
397 * Return whether or not there is a string option pending;
398 * that is, if the previous option was a string-valued option letter
399 * (like -P) without a following string.
400 * In that case, the current option is taken to be the string for
401 * the previous option.
406 return (pendopt
!= NULL
);
410 * Print error message about missing string.
417 parg
.p_string
= propt(c
);
418 error("String is required after %s", &parg
);
422 * Print error message if a STRING type option is not followed by a string.
427 nostring(pendopt
->oletter
);
431 * Scan to end of string or to an END_OPTION_STRING character.
432 * In the latter case, replace the char with a null char.
433 * Return a pointer to the remainder of the string, if any.
447 for (p
= s
; *p
!= '\0'; p
++)
448 if (*p
== END_OPTION_STRING
)
457 * Translate a string into a number.
458 * Like atoi(), but takes a pointer to a char *, and updates
459 * the char * to point after the translated number.
479 if (*s
< '0' || *s
> '9')
486 parg
.p_string
= propt(c
);
487 error("Number is required after %s", &parg
);
492 while (*s
>= '0' && *s
<= '9')
493 n
= 10 * n
+ *s
++ - '0';