option.c (009e81b16465ea457c0e63fd49fe77f47cc27a5a) option.c (1ea316270f1f75922ac53976d5d8808a41442f46)
1/*
2 * Copyright (C) 1984-2015 Mark Nudelman
3 *
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.
6 *
7 * For more information, see the README file.
8 */

--- 9 unchanged lines hidden (view full) ---

18 */
19
20#include "less.h"
21#include "option.h"
22
23static struct loption *pendopt;
24public int plusoption = FALSE;
25
1/*
2 * Copyright (C) 1984-2015 Mark Nudelman
3 *
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.
6 *
7 * For more information, see the README file.
8 */

--- 9 unchanged lines hidden (view full) ---

18 */
19
20#include "less.h"
21#include "option.h"
22
23static struct loption *pendopt;
24public int plusoption = FALSE;
25
26static char *optstring();
27static int flip_triple();
26static char *optstring(char *s, char **p_str, char *printopt, char *validchars);
27static int flip_triple(int val, int lc);
28
29extern int screen_trashed;
30extern int less_is_more;
31extern int quit_at_eof;
32extern char *every_first_cmd;
33extern int opt_use_backslash;
34
35/*
36 * Return a printable description of an option.
37 */
38 static char *
28
29extern int screen_trashed;
30extern int less_is_more;
31extern int quit_at_eof;
32extern char *every_first_cmd;
33extern int opt_use_backslash;
34
35/*
36 * Return a printable description of an option.
37 */
38 static char *
39opt_desc(o)
40 struct loption *o;
39opt_desc(struct loption *o)
41{
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);
48}
49
50/*
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".
53 */
54 public char *
40{
41 static char buf[OPTNAME_MAX + 10];
42 if (o->oletter == OLETTER_NONE)
43 SNPRINTF1(buf, sizeof(buf), "--%s", o->onames->oname);
44 else
45 SNPRINTF2(buf, sizeof(buf), "-%c (--%s)", o->oletter, o->onames->oname);
46 return (buf);
47}
48
49/*
50 * Return a string suitable for printing as the "name" of an option.
51 * For example, if the option letter is 'x', just return "-x".
52 */
53 public char *
55propt(c)
56 int c;
54propt(int c)
57{
58 static char buf[8];
59
60 sprintf(buf, "-%s", prchar(c));
61 return (buf);
62}
63
64/*
65 * Scan an argument (either from the command line or from the
66 * LESS environment variable) and process it.
67 */
68 public void
55{
56 static char buf[8];
57
58 sprintf(buf, "-%s", prchar(c));
59 return (buf);
60}
61
62/*
63 * Scan an argument (either from the command line or from the
64 * LESS environment variable) and process it.
65 */
66 public void
69scan_option(s)
70 char *s;
67scan_option(char *s)
71{
68{
72 register struct loption *o;
73 register int optc;
69 struct loption *o;
70 int optc;
74 char *optname;
75 char *printopt;
76 char *str;
77 int set_default;
78 int lc;
79 int err;
80 PARG parg;
81

--- 212 unchanged lines hidden (view full) ---

294 * Used by the "-" and "_" commands.
295 * how_toggle may be:
296 * OPT_NO_TOGGLE just report the current setting, without changing it.
297 * OPT_TOGGLE invert the current setting
298 * OPT_UNSET set to the default value
299 * OPT_SET set to the inverse of the default value
300 */
301 public void
71 char *optname;
72 char *printopt;
73 char *str;
74 int set_default;
75 int lc;
76 int err;
77 PARG parg;
78

--- 212 unchanged lines hidden (view full) ---

291 * Used by the "-" and "_" commands.
292 * how_toggle may be:
293 * OPT_NO_TOGGLE just report the current setting, without changing it.
294 * OPT_TOGGLE invert the current setting
295 * OPT_UNSET set to the default value
296 * OPT_SET set to the inverse of the default value
297 */
298 public void
302toggle_option(o, lower, s, how_toggle)
303 struct loption *o;
304 int lower;
305 char *s;
306 int how_toggle;
299toggle_option(struct loption *o, int lower, char *s, int how_toggle)
307{
300{
308 register int num;
301 int num;
309 int no_prompt;
310 int err;
311 PARG parg;
312
313 no_prompt = (how_toggle & OPT_NO_PROMPT);
314 how_toggle &= ~OPT_NO_PROMPT;
315
316 if (o == NULL)

--- 163 unchanged lines hidden (view full) ---

480 if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
481 screen_trashed = TRUE;
482}
483
484/*
485 * "Toggle" a triple-valued option.
486 */
487 static int
302 int no_prompt;
303 int err;
304 PARG parg;
305
306 no_prompt = (how_toggle & OPT_NO_PROMPT);
307 how_toggle &= ~OPT_NO_PROMPT;
308
309 if (o == NULL)

--- 163 unchanged lines hidden (view full) ---

473 if (how_toggle != OPT_NO_TOGGLE && (o->otype & REPAINT))
474 screen_trashed = TRUE;
475}
476
477/*
478 * "Toggle" a triple-valued option.
479 */
480 static int
488flip_triple(val, lc)
489 int val;
490 int lc;
481flip_triple(int val, int lc)
491{
492 if (lc)
493 return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
494 else
495 return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
496}
497
498/*
499 * Determine if an option takes a parameter.
500 */
501 public int
482{
483 if (lc)
484 return ((val == OPT_ON) ? OPT_OFF : OPT_ON);
485 else
486 return ((val == OPT_ONPLUS) ? OPT_OFF : OPT_ONPLUS);
487}
488
489/*
490 * Determine if an option takes a parameter.
491 */
492 public int
502opt_has_param(o)
503 struct loption *o;
493opt_has_param(struct loption *o)
504{
505 if (o == NULL)
506 return (0);
507 if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE))
508 return (0);
509 return (1);
510}
511
512/*
513 * Return the prompt to be used for a given option letter.
514 * Only string and number valued options have prompts.
515 */
516 public char *
494{
495 if (o == NULL)
496 return (0);
497 if (o->otype & (BOOL|TRIPLE|NOVAR|NO_TOGGLE))
498 return (0);
499 return (1);
500}
501
502/*
503 * Return the prompt to be used for a given option letter.
504 * Only string and number valued options have prompts.
505 */
506 public char *
517opt_prompt(o)
518 struct loption *o;
507opt_prompt(struct loption *o)
519{
520 if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
521 return ("?");
522 return (o->odesc[0]);
523}
524
525/*
526 * Return whether or not there is a string option pending;
527 * that is, if the previous option was a string-valued option letter
528 * (like -P) without a following string.
529 * In that case, the current option is taken to be the string for
530 * the previous option.
531 */
532 public int
508{
509 if (o == NULL || (o->otype & (STRING|NUMBER)) == 0)
510 return ("?");
511 return (o->odesc[0]);
512}
513
514/*
515 * Return whether or not there is a string option pending;
516 * that is, if the previous option was a string-valued option letter
517 * (like -P) without a following string.
518 * In that case, the current option is taken to be the string for
519 * the previous option.
520 */
521 public int
533isoptpending()
522isoptpending(void)
534{
535 return (pendopt != NULL);
536}
537
538/*
539 * Print error message about missing string.
540 */
541 static void
523{
524 return (pendopt != NULL);
525}
526
527/*
528 * Print error message about missing string.
529 */
530 static void
542nostring(printopt)
543 char *printopt;
531nostring(char *printopt)
544{
545 PARG parg;
546 parg.p_string = printopt;
547 error("Value is required after %s", &parg);
548}
549
550/*
551 * Print error message if a STRING type option is not followed by a string.
552 */
553 public void
532{
533 PARG parg;
534 parg.p_string = printopt;
535 error("Value is required after %s", &parg);
536}
537
538/*
539 * Print error message if a STRING type option is not followed by a string.
540 */
541 public void
554nopendopt()
542nopendopt(void)
555{
556 nostring(opt_desc(pendopt));
557}
558
559/*
560 * Scan to end of string or to an END_OPTION_STRING character.
561 * In the latter case, replace the char with a null char.
562 * Return a pointer to the remainder of the string, if any.
563 */
564 static char *
543{
544 nostring(opt_desc(pendopt));
545}
546
547/*
548 * Scan to end of string or to an END_OPTION_STRING character.
549 * In the latter case, replace the char with a null char.
550 * Return a pointer to the remainder of the string, if any.
551 */
552 static char *
565optstring(s, p_str, printopt, validchars)
566 char *s;
567 char **p_str;
568 char *printopt;
569 char *validchars;
553optstring(char *s, char **p_str, char *printopt, char *validchars)
570{
554{
571 register char *p;
572 register char *out;
555 char *p;
556 char *out;
573
574 if (*s == '\0')
575 {
576 nostring(printopt);
577 return (NULL);
578 }
579 /* Alloc could be more than needed, but not worth trimming. */
580 *p_str = (char *) ecalloc(strlen(s)+1, sizeof(char));

--- 16 unchanged lines hidden (view full) ---

597 }
598 *out = '\0';
599 return (p);
600}
601
602/*
603 */
604 static int
557
558 if (*s == '\0')
559 {
560 nostring(printopt);
561 return (NULL);
562 }
563 /* Alloc could be more than needed, but not worth trimming. */
564 *p_str = (char *) ecalloc(strlen(s)+1, sizeof(char));

--- 16 unchanged lines hidden (view full) ---

581 }
582 *out = '\0';
583 return (p);
584}
585
586/*
587 */
588 static int
605num_error(printopt, errp)
606 char *printopt;
607 int *errp;
589num_error(char *printopt, int *errp)
608{
609 PARG parg;
610
611 if (errp != NULL)
612 {
613 *errp = TRUE;
614 return (-1);
615 }

--- 6 unchanged lines hidden (view full) ---

622}
623
624/*
625 * Translate a string into a number.
626 * Like atoi(), but takes a pointer to a char *, and updates
627 * the char * to point after the translated number.
628 */
629 public int
590{
591 PARG parg;
592
593 if (errp != NULL)
594 {
595 *errp = TRUE;
596 return (-1);
597 }

--- 6 unchanged lines hidden (view full) ---

604}
605
606/*
607 * Translate a string into a number.
608 * Like atoi(), but takes a pointer to a char *, and updates
609 * the char * to point after the translated number.
610 */
611 public int
630getnum(sp, printopt, errp)
631 char **sp;
632 char *printopt;
633 int *errp;
612getnum(char **sp, char *printopt, int *errp)
634{
613{
635 register char *s;
636 register int n;
637 register int neg;
614 char *s;
615 int n;
616 int neg;
638
639 s = skipsp(*sp);
640 neg = FALSE;
641 if (*s == '-')
642 {
643 neg = TRUE;
644 s++;
645 }

--- 13 unchanged lines hidden (view full) ---

659
660/*
661 * Translate a string into a fraction, represented by the part of a
662 * number which would follow a decimal point.
663 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
664 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
665 */
666 public long
617
618 s = skipsp(*sp);
619 neg = FALSE;
620 if (*s == '-')
621 {
622 neg = TRUE;
623 s++;
624 }

--- 13 unchanged lines hidden (view full) ---

638
639/*
640 * Translate a string into a fraction, represented by the part of a
641 * number which would follow a decimal point.
642 * The value of the fraction is returned as parts per NUM_FRAC_DENOM.
643 * That is, if "n" is returned, the fraction intended is n/NUM_FRAC_DENOM.
644 */
645 public long
667getfraction(sp, printopt, errp)
668 char **sp;
669 char *printopt;
670 int *errp;
646getfraction(char **sp, char *printopt, int *errp)
671{
647{
672 register char *s;
648 char *s;
673 long frac = 0;
674 int fraclen = 0;
675
676 s = skipsp(*sp);
677 if (*s < '0' || *s > '9')
678 return (num_error(printopt, errp));
679
680 for ( ; *s >= '0' && *s <= '9'; s++)

--- 13 unchanged lines hidden (view full) ---

694 return (frac);
695}
696
697
698/*
699 * Get the value of the -e flag.
700 */
701 public int
649 long frac = 0;
650 int fraclen = 0;
651
652 s = skipsp(*sp);
653 if (*s < '0' || *s > '9')
654 return (num_error(printopt, errp));
655
656 for ( ; *s >= '0' && *s <= '9'; s++)

--- 13 unchanged lines hidden (view full) ---

670 return (frac);
671}
672
673
674/*
675 * Get the value of the -e flag.
676 */
677 public int
702get_quit_at_eof()
678get_quit_at_eof(void)
703{
704 if (!less_is_more)
705 return quit_at_eof;
706 /* When less_is_more is set, the -e flag semantics are different. */
707 return quit_at_eof ? OPT_ONPLUS : OPT_ON;
708}
679{
680 if (!less_is_more)
681 return quit_at_eof;
682 /* When less_is_more is set, the -e flag semantics are different. */
683 return quit_at_eof ? OPT_ONPLUS : OPT_ON;
684}