sys/vfs/hammer2: Remove unused HMNT2_NOAUTOSNAP
[dragonfly.git] / contrib / less / option.c
blobd402e11752c1ab070564dc6944d8a090a8a24d6b
1 /*
2 * Copyright (C) 1984-2019 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.
8 */
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.
20 #include "less.h"
21 #include "option.h"
23 static struct loption *pendopt;
24 public int plusoption = FALSE;
26 static char *optstring();
27 static int flip_triple();
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.
38 static char *
39 opt_desc(o)
40 struct loption *o;
42 static char buf[OPTNAME_MAX + 10];
43 if (o->oletter == OLETTER_NONE)
44 SNPRINTF1(buf, sizeof(buf), "--%s", o->onames->oname);
45 else
46 SNPRINTF2(buf, sizeof(buf), "-%c (--%s)", o->oletter, o->onames->oname);
47 return (buf);
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".
54 public char *
55 propt(c)
56 int c;
58 static char buf[8];
60 sprintf(buf, "-%s", prchar(c));
61 return (buf);
64 /*
65 * Scan an argument (either from the command line or from the
66 * LESS environment variable) and process it.
68 public void
69 scan_option(s)
70 char *s;
72 struct loption *o;
73 int optc;
74 char *optname;
75 char *printopt;
76 char *str;
77 int set_default;
78 int lc;
79 int err;
80 PARG parg;
82 if (s == NULL)
83 return;
86 * If we have a pending option which requires an argument,
87 * handle it now.
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.
92 if (pendopt != NULL)
94 switch (pendopt->otype & OTYPE)
96 case STRING:
97 (*pendopt->ofunc)(INIT, s);
98 break;
99 case NUMBER:
100 printopt = opt_desc(pendopt);
101 *(pendopt->ovar) = getnum(&s, printopt, (int*)NULL);
102 break;
104 pendopt = NULL;
105 return;
108 set_default = FALSE;
109 optname = NULL;
111 while (*s != '\0')
114 * Check some special cases first.
116 switch (optc = *s++)
118 case ' ':
119 case '\t':
120 case END_OPTION_STRING:
121 continue;
122 case '-':
124 * "--" indicates an option name instead of a letter.
126 if (*s == '-')
128 optname = ++s;
129 break;
132 * "-+" means set these options back to their defaults.
133 * (They may have been set otherwise by previous
134 * options.)
136 set_default = (*s == '+');
137 if (set_default)
138 s++;
139 continue;
140 case '+':
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
146 * EVERY input file.
148 plusoption = TRUE;
149 s = optstring(s, &str, propt('+'), NULL);
150 if (s == NULL)
151 return;
152 if (*str == '+')
154 if (every_first_cmd != NULL)
155 free(every_first_cmd);
156 every_first_cmd = save(str+1);
157 } else
159 ungetcc(CHAR_END_COMMAND);
160 ungetsc(str);
162 free(str);
163 continue;
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
169 * window size.
171 s--;
172 optc = 'z';
173 break;
174 case 'n':
175 if (less_is_more)
176 optc = 'z';
177 break;
181 * Not a special case.
182 * Look up the option letter in the option table.
184 err = 0;
185 if (optname == NULL)
187 printopt = propt(optc);
188 lc = ASCII_IS_LOWER(optc);
189 o = findopt(optc);
190 } else
192 printopt = optname;
193 lc = ASCII_IS_LOWER(optname[0]);
194 o = findopt_name(&optname, NULL, &err);
195 s = optname;
196 optname = NULL;
197 if (*s == '\0' || *s == ' ')
200 * The option name matches exactly.
203 } else if (*s == '=')
206 * The option name is followed by "=value".
208 if (o != NULL &&
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 =",
214 &parg);
215 return;
217 s++;
218 } else
221 * The specified name is longer than the
222 * real option name.
224 o = NULL;
227 if (o == NULL)
229 parg.p_string = printopt;
230 if (err == OPT_AMBIG)
231 error("%s is an ambiguous abbreviation (\"less --help\" for help)",
232 &parg);
233 else
234 error("There is no %s option (\"less --help\" for help)",
235 &parg);
236 return;
239 str = NULL;
240 switch (o->otype & OTYPE)
242 case BOOL:
243 if (set_default)
244 *(o->ovar) = o->odefault;
245 else
246 *(o->ovar) = ! o->odefault;
247 break;
248 case TRIPLE:
249 if (set_default)
250 *(o->ovar) = o->odefault;
251 else
252 *(o->ovar) = flip_triple(o->odefault, lc);
253 break;
254 case STRING:
255 if (*s == '\0')
258 * Set pendopt and return.
259 * We will get the string next time
260 * scan_option is called.
262 pendopt = o;
263 return;
266 * Don't do anything here.
267 * All processing of STRING options is done by
268 * the handling function.
270 while (*s == ' ')
271 s++;
272 s = optstring(s, &str, printopt, o->odesc[1]);
273 if (s == NULL)
274 return;
275 break;
276 case NUMBER:
277 if (*s == '\0')
279 pendopt = o;
280 return;
282 *(o->ovar) = getnum(&s, printopt, (int*)NULL);
283 break;
286 * If the option has a handling function, call it.
288 if (o->ofunc != NULL)
289 (*o->ofunc)(INIT, str);
290 if (str != NULL)
291 free(str);
296 * Toggle command line flags from within the program.
297 * Used by the "-" and "_" commands.
298 * how_toggle may be:
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
304 public void
305 toggle_option(o, lower, s, how_toggle)
306 struct loption *o;
307 int lower;
308 char *s;
309 int how_toggle;
311 int num;
312 int no_prompt;
313 int err;
314 PARG parg;
316 no_prompt = (how_toggle & OPT_NO_PROMPT);
317 how_toggle &= ~OPT_NO_PROMPT;
319 if (o == NULL)
321 error("No such option", NULL_PARG);
322 return;
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);
329 return;
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);
336 return;
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)
347 case STRING:
348 case NUMBER:
349 if (how_toggle == OPT_TOGGLE && *s == '\0')
350 how_toggle = OPT_NO_TOGGLE;
351 break;
354 #if HILITE_SEARCH
355 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
356 repaint_hilite(0);
357 #endif
360 * Now actually toggle (change) the variable.
362 if (how_toggle != OPT_NO_TOGGLE)
364 switch (o->otype & OTYPE)
366 case BOOL:
368 * Boolean.
370 switch (how_toggle)
372 case OPT_TOGGLE:
373 *(o->ovar) = ! *(o->ovar);
374 break;
375 case OPT_UNSET:
376 *(o->ovar) = o->odefault;
377 break;
378 case OPT_SET:
379 *(o->ovar) = ! o->odefault;
380 break;
382 break;
383 case TRIPLE:
385 * Triple:
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.
391 switch (how_toggle)
393 case OPT_TOGGLE:
394 *(o->ovar) = flip_triple(*(o->ovar), lower);
395 break;
396 case OPT_UNSET:
397 *(o->ovar) = o->odefault;
398 break;
399 case OPT_SET:
400 *(o->ovar) = flip_triple(o->odefault, lower);
401 break;
403 break;
404 case STRING:
406 * String: don't do anything here.
407 * The handling function will do everything.
409 switch (how_toggle)
411 case OPT_SET:
412 case OPT_UNSET:
413 error("Cannot use \"-+\" or \"--\" for a string option",
414 NULL_PARG);
415 return;
417 break;
418 case NUMBER:
420 * Number: set the variable to the given number.
422 switch (how_toggle)
424 case OPT_TOGGLE:
425 num = getnum(&s, NULL, &err);
426 if (!err)
427 *(o->ovar) = num;
428 break;
429 case OPT_UNSET:
430 *(o->ovar) = o->odefault;
431 break;
432 case OPT_SET:
433 error("Can't use \"-!\" for a numeric option",
434 NULL_PARG);
435 return;
437 break;
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);
448 #if HILITE_SEARCH
449 if (how_toggle != OPT_NO_TOGGLE && (o->otype & HL_REPAINT))
450 chg_hilite();
451 #endif
453 if (!no_prompt)
456 * Print a message describing the new setting.
458 switch (o->otype & OTYPE)
460 case BOOL:
461 case TRIPLE:
463 * Print the odesc message.
465 error(o->odesc[*(o->ovar)], NULL_PARG);
466 break;
467 case NUMBER:
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);
474 break;
475 case STRING:
477 * Message was already printed by the handling function.
479 break;
483 if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
484 screen_trashed = TRUE;
488 * "Toggle" a triple-valued option.
490 static int
491 flip_triple(val, lc)
492 int val;
493 int lc;
495 if (lc)
496 return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
497 else
498 return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
502 * Determine if an option takes a parameter.
504 public int
505 opt_has_param(o)
506 struct loption *o;
508 if (o == NULL)
509 return (0);
510 if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE))
511 return (0);
512 return (1);
516 * Return the prompt to be used for a given option letter.
517 * Only string and number valued options have prompts.
519 public char *
520 opt_prompt(o)
521 struct loption *o;
523 if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
524 return ("?");
525 return (o->odesc[0]);
529 * Return whether or not there is a string option pending;
530 * that is, if the previous option was a string-valued option letter
531 * (like -P) without a following string.
532 * In that case, the current option is taken to be the string for
533 * the previous option.
535 public int
536 isoptpending(VOID_PARAM)
538 return (pendopt != NULL);
542 * Print error message about missing string.
544 static void
545 nostring(printopt)
546 char *printopt;
548 PARG parg;
549 parg.p_string = printopt;
550 error("Value is required after %s", &parg);
554 * Print error message if a STRING type option is not followed by a string.
556 public void
557 nopendopt(VOID_PARAM)
559 nostring(opt_desc(pendopt));
563 * Scan to end of string or to an END_OPTION_STRING character.
564 * In the latter case, replace the char with a null char.
565 * Return a pointer to the remainder of the string, if any.
567 static char *
568 optstring(s, p_str, printopt, validchars)
569 char *s;
570 char **p_str;
571 char *printopt;
572 char *validchars;
574 char *p;
575 char *out;
577 if (*s == '\0')
579 nostring(printopt);
580 return (NULL);
582 /* Alloc could be more than needed, but not worth trimming. */
583 *p_str = (char *) ecalloc(strlen(s)+1, sizeof(char));
584 out = *p_str;
586 for (p = s; *p != '\0'; p++)
588 if (opt_use_backslash && *p == '\\' && p[1] != '\0')
590 /* Take next char literally. */
591 ++p;
592 } else
594 if (*p == END_OPTION_STRING ||
595 (validchars != NULL && strchr(validchars, *p) == NULL))
596 /* End of option string. */
597 break;
599 *out++ = *p;
601 *out = '\0';
602 return (p);
607 static int
608 num_error(printopt, errp)
609 char *printopt;
610 int *errp;
612 PARG parg;
614 if (errp != NULL)
616 *errp = TRUE;
617 return (-1);
619 if (printopt != NULL)
621 parg.p_string = printopt;
622 error("Number is required after %s", &parg);
624 return (-1);
628 * Translate a string into a number.
629 * Like atoi(), but takes a pointer to a char *, and updates
630 * the char * to point after the translated number.
632 public int
633 getnum(sp, printopt, errp)
634 char **sp;
635 char *printopt;
636 int *errp;
638 char *s;
639 int n;
640 int neg;
642 s = skipsp(*sp);
643 neg = FALSE;
644 if (*s == '-')
646 neg = TRUE;
647 s++;
649 if (*s < '0' || *s > '9')
650 return (num_error(printopt, errp));
652 n = 0;
653 while (*s >= '0' && *s <= '9')
654 n = 10 * n + *s++ - '0';
655 *sp = s;
656 if (errp != NULL)
657 *errp = FALSE;
658 if (neg)
659 n = -n;
660 return (n);
664 * Translate a string into a fraction, represented by the part of a
665 * number which would follow a decimal point.
666 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
667 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
669 public long
670 getfraction(sp, printopt, errp)
671 char **sp;
672 char *printopt;
673 int *errp;
675 char *s;
676 long frac = 0;
677 int fraclen = 0;
679 s = skipsp(*sp);
680 if (*s < '0' || *s > '9')
681 return (num_error(printopt, errp));
683 for ( ; *s >= '0' && *s <= '9'; s++)
685 frac = (frac * 10) + (*s - '0');
686 fraclen++;
688 if (fraclen > NUM_LOG_FRAC_DENOM)
689 while (fraclen-- > NUM_LOG_FRAC_DENOM)
690 frac /= 10;
691 else
692 while (fraclen++ < NUM_LOG_FRAC_DENOM)
693 frac *= 10;
694 *sp = s;
695 if (errp != NULL)
696 *errp = FALSE;
697 return (frac);
702 * Get the value of the -e flag.
704 public int
705 get_quit_at_eof(VOID_PARAM)
707 if (!less_is_more)
708 return quit_at_eof;
709 /* When less_is_more is set, the -e flag semantics are different. */
710 return quit_at_eof ? OPT_ONPLUS : OPT_ON;