2 * Copyright (C) 1984-2023 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.
12 * Handling functions for command line options.
14 * Most options are handled by the generic code in option.c.
15 * But all string options, and a few non-string options, require
16 * special handling specific to the particular option.
17 * This special processing is done by the "handling functions" in this file.
19 * Each handling function is passed a "type" and, if it is a string
20 * option, the string which should be "assigned" to the option.
21 * The type may be one of:
22 * INIT The option is being initialized from the command line.
23 * TOGGLE The option is being changed from within the program.
24 * QUERY The setting of the option is merely being queried.
33 extern int plusoption
;
40 extern char openquote
;
41 extern char closequote
;
42 extern char *prproto
[];
46 extern char *every_first_cmd
;
47 extern IFILE curr_ifile
;
48 extern char version
[];
49 extern int jump_sline
;
50 extern long jump_sline_fraction
;
51 extern int shift_count
;
52 extern long shift_count_fraction
;
53 extern char rscroll_char
;
54 extern int rscroll_attr
;
56 extern int wheel_lines
;
57 extern int less_is_more
;
58 extern int linenum_width
;
59 extern int status_col_width
;
61 extern int want_filesize
;
62 extern int header_lines
;
63 extern int header_cols
;
64 extern int def_search_type
;
66 extern int tabstops
[];
68 extern int tabdefault
;
69 extern char intr_char
;
71 extern char *namelogfile
;
72 extern int force_logfile
;
76 public char *tagoption
= NULL
;
81 extern char *ttyin_name
;
84 extern int nm_fg_color
, nm_bg_color
;
85 extern int bo_fg_color
, bo_bg_color
;
86 extern int ul_fg_color
, ul_bg_color
;
87 extern int so_fg_color
, so_bg_color
;
88 extern int bl_fg_color
, bl_bg_color
;
90 #if MSDOS_COMPILER==WIN32C
91 #ifndef COMMON_LVB_UNDERSCORE
92 #define COMMON_LVB_UNDERSCORE 0x8000
100 * Handler for -o option.
102 public void opt_o(int type
, char *s
)
109 error("log file support is not available", NULL_PARG
);
115 namelogfile
= save(s
);
118 if (ch_getflags() & CH_CANSEEK
)
120 error("Input is not a pipe", NULL_PARG
);
125 error("Log file is already in use", NULL_PARG
);
129 if (namelogfile
!= NULL
)
132 namelogfile
= shell_unquote(filename
);
134 use_logfile(namelogfile
);
139 error("No log file", NULL_PARG
);
142 parg
.p_string
= namelogfile
;
143 error("Log file \"%s\"", &parg
);
150 * Handler for -O option.
152 public void opt__O(int type
, char *s
)
154 force_logfile
= TRUE
;
160 * Handlers for -j option.
162 public void opt_j(int type
, char *s
)
175 jump_sline_fraction
= getfraction(&s
, "j", &err
);
177 error("Invalid line fraction", NULL_PARG
);
182 int sline
= getnum(&s
, "j", &err
);
184 error("Invalid line number", NULL_PARG
);
188 jump_sline_fraction
= -1;
193 if (jump_sline_fraction
< 0)
195 parg
.p_int
= jump_sline
;
196 error("Position target at screen line %d", &parg
);
199 char buf
[INT_STRLEN_BOUND(long)+2];
200 SNPRINTF1(buf
, sizeof(buf
), ".%06ld", jump_sline_fraction
);
201 len
= (int) strlen(buf
);
202 while (len
> 2 && buf
[len
-1] == '0')
206 error("Position target at screen position %s", &parg
);
212 public void calc_jump_sline(void)
214 if (jump_sline_fraction
< 0)
216 jump_sline
= (int) muldiv(sc_height
, jump_sline_fraction
, NUM_FRAC_DENOM
);
220 * Handlers for -# option.
222 public void opt_shift(int type
, char *s
)
235 shift_count_fraction
= getfraction(&s
, "#", &err
);
237 error("Invalid column fraction", NULL_PARG
);
242 int hs
= getnum(&s
, "#", &err
);
244 error("Invalid column number", NULL_PARG
);
248 shift_count_fraction
= -1;
253 if (shift_count_fraction
< 0)
255 parg
.p_int
= shift_count
;
256 error("Horizontal shift %d columns", &parg
);
259 char buf
[INT_STRLEN_BOUND(long)+2];
260 SNPRINTF1(buf
, sizeof(buf
), ".%06ld", shift_count_fraction
);
261 len
= (int) strlen(buf
);
262 while (len
> 2 && buf
[len
-1] == '0')
266 error("Horizontal shift %s of screen width", &parg
);
272 public void calc_shift_count(void)
274 if (shift_count_fraction
< 0)
276 shift_count
= (int) muldiv(sc_width
, shift_count_fraction
, NUM_FRAC_DENOM
);
280 public void opt_k(int type
, char *s
)
290 error("Cannot use lesskey file \"%s\"", &parg
);
297 public void opt_ks(int type
, char *s
)
304 if (lesskey_src(s
, 0))
307 error("Cannot use lesskey source file \"%s\"", &parg
);
312 #endif /* HAVE_LESSKEYSRC */
313 #endif /* USERFILE */
317 * Handler for -t option.
319 public void opt_t(int type
, char *s
)
328 /* Do the rest in main() */
333 error("tags support is not available", NULL_PARG
);
337 save_ifile
= save_curr_ifile();
339 * Try to open the file containing the tag
340 * and search for the tag in that file.
342 if (edit_tagfile() || (pos
= tagsearch()) == NULL_POSITION
)
344 /* Failed: reopen the old file. */
345 reedit_ifile(save_ifile
);
348 unsave_ifile(save_ifile
);
349 jump_loc(pos
, jump_sline
);
355 * Handler for -T option.
357 public void opt__T(int type
, char *s
)
369 if (tags
!= NULL
&& tags
!= ztags
)
372 tags
= shell_unquote(filename
);
376 parg
.p_string
= tags
;
377 error("Tags file \"%s\"", &parg
);
384 * Handler for -p option.
386 public void opt_p(int type
, char *s
)
392 * Unget a command for the specified string.
397 * In "more" mode, the -p argument is a command,
398 * not a search string, so we don't need a slash.
400 every_first_cmd
= save(s
);
405 * {{ This won't work if the "/" command is
406 * changed or invalidated by a .lesskey file. }}
410 ungetcc_back(CHAR_END_COMMAND
);
417 * Handler for -P option.
419 public void opt__P(int type
, char *s
)
429 * Figure out which prototype string should be changed.
433 case 's': proto
= &prproto
[PR_SHORT
]; s
++; break;
434 case 'm': proto
= &prproto
[PR_MEDIUM
]; s
++; break;
435 case 'M': proto
= &prproto
[PR_LONG
]; s
++; break;
436 case '=': proto
= &eqproto
; s
++; break;
437 case 'h': proto
= &hproto
; s
++; break;
438 case 'w': proto
= &wproto
; s
++; break;
439 default: proto
= &prproto
[PR_SHORT
]; break;
445 parg
.p_string
= prproto
[pr_type
];
452 * Handler for the -b option.
455 public void opt_b(int type
, char *s
)
462 * Set the new number of buffers.
464 ch_setbufspace(bufspace
);
472 * Handler for the -i option.
475 public void opt_i(int type
, char *s
)
489 * Handler for the -V option.
492 public void opt__V(int type
, char *s
)
501 set_output(1); /* Force output to stdout per GNU standard for --version output. */
505 putstr(pattern_lib_name());
506 putstr(" regular expressions)\n");
508 char constant
*copyright
=
509 "Copyright (C) 1984-2023 Mark Nudelman\n\n";
512 if (version
[strlen(version
)-1] == 'x')
514 putstr("** This is an EXPERIMENTAL build of the 'less' software,\n");
515 putstr("** and may not function correctly.\n");
516 putstr("** Obtain release builds from the web page below.\n\n");
519 putstr("This build supports LESSTEST.\n");
521 putstr("less comes with NO WARRANTY, to the extent permitted by law.\n");
522 putstr("For information about the terms of redistribution,\n");
523 putstr("see the file named README in the less distribution.\n");
524 putstr("Home page: https://greenwoodsoftware.com/less\n");
532 * Parse an MSDOS color descriptor.
534 static void colordesc(char *s
, int *fg_color
, int *bg_color
)
537 #if MSDOS_COMPILER==WIN32C
542 ul
= COMMON_LVB_UNDERSCORE
;
546 *fg_color
= nm_fg_color
| ul
;
547 *bg_color
= nm_bg_color
;
552 if (parse_color(s
, &fg
, &bg
) == CT_NULL
)
556 error("Invalid color string \"%s\"", &p
);
559 if (fg
== CV_NOCHANGE
)
561 if (bg
== CV_NOCHANGE
)
563 #if MSDOS_COMPILER==WIN32C
572 static int color_from_namechar(char namechar
)
576 case 'B': return AT_COLOR_BIN
;
577 case 'C': return AT_COLOR_CTRL
;
578 case 'E': return AT_COLOR_ERROR
;
579 case 'H': return AT_COLOR_HEADER
;
580 case 'M': return AT_COLOR_MARK
;
581 case 'N': return AT_COLOR_LINENUM
;
582 case 'P': return AT_COLOR_PROMPT
;
583 case 'R': return AT_COLOR_RSCROLL
;
584 case 'S': return AT_COLOR_SEARCH
;
585 case 'W': case 'A': return AT_COLOR_ATTN
;
586 case 'n': return AT_NORMAL
;
587 case 's': return AT_STANDOUT
;
588 case 'd': return AT_BOLD
;
589 case 'u': return AT_UNDERLINE
;
590 case 'k': return AT_BLINK
;
592 if (namechar
>= '1' && namechar
<= '0'+NUM_SEARCH_COLORS
)
593 return AT_COLOR_SUBSEARCH(namechar
-'0');
599 * Handler for the -D option.
602 public void opt_D(int type
, char *s
)
614 sgr_mode
= !sgr_mode
;
618 attr
= color_from_namechar(s
[0]);
622 error("Invalid color specifier '%c'", &p
);
625 if (!use_color
&& (attr
& AT_COLOR
))
627 error("Set --use-color before changing colors", NULL_PARG
);
632 if (!(attr
& AT_COLOR
))
637 colordesc(s
, &nm_fg_color
, &nm_bg_color
);
640 colordesc(s
, &bo_fg_color
, &bo_bg_color
);
643 colordesc(s
, &ul_fg_color
, &ul_bg_color
);
646 colordesc(s
, &bl_fg_color
, &bl_bg_color
);
649 colordesc(s
, &so_fg_color
, &so_bg_color
);
654 at_enter(AT_STANDOUT
);
659 if (set_color_map(attr
, s
) < 0)
662 error("Invalid color string \"%s\"", &p
);
668 p
.p_string
= (sgr_mode
) ? "on" : "off";
669 error("SGR mode is %s", &p
);
677 public void set_tabs(char *s
, int len
)
681 /* Start at 1 because tabstops[0] is always zero. */
682 for (i
= 1; i
< TABSTOP_MAX
; )
686 while (s
< es
&& *s
== ' ')
688 for (; s
< es
&& *s
>= '0' && *s
<= '9'; s
++)
690 v
|= ckd_mul(&n
, n
, 10);
691 v
|= ckd_add(&n
, n
, *s
- '0');
693 if (!v
&& n
> tabstops
[i
-1])
695 while (s
< es
&& *s
== ' ')
697 if (s
== es
|| *s
++ != ',')
703 tabdefault
= tabstops
[ntabstops
-1] - tabstops
[ntabstops
-2];
707 * Handler for the -x option.
709 public void opt_x(int type
, char *s
)
711 char msg
[60+((INT_STRLEN_BOUND(int)+1)*TABSTOP_MAX
)];
719 set_tabs(s
, strlen(s
));
722 strcpy(msg
, "Tab stops ");
725 for (i
= 1; i
< ntabstops
; i
++)
729 sprintf(msg
+strlen(msg
), "%d", tabstops
[i
]);
731 sprintf(msg
+strlen(msg
), " and then ");
733 sprintf(msg
+strlen(msg
), "every %d spaces",
743 * Handler for the -" option.
745 public void opt_quote(int type
, char *s
)
756 openquote
= closequote
= '\0';
759 if (s
[1] != '\0' && s
[2] != '\0')
761 error("-\" must be followed by 1 or 2 chars", NULL_PARG
);
766 closequote
= openquote
;
775 error("quotes %s", &parg
);
781 * Handler for the --rscroll option.
784 public void opt_rscroll(int type
, char *s
)
793 int attr
= AT_STANDOUT
;
794 setfmt(s
, &fmt
, &attr
, "*s>", FALSE
);
795 if (strcmp(fmt
, "-") == 0)
800 rscroll_char
= *fmt
? *fmt
: '>';
801 rscroll_attr
= attr
|AT_COLOR_RSCROLL
;
805 p
.p_string
= rscroll_char
? prchar(rscroll_char
) : "-";
806 error("rscroll character is %s", &p
);
812 * "-?" means display a help message.
813 * If from the command line, exit immediately.
816 public void opt_query(int type
, char *s
)
822 error("Use \"h\" for help", NULL_PARG
);
830 * Handler for the --mouse option.
833 public void opt_mousecap(int type
, char *s
)
838 if (mousecap
== OPT_OFF
)
850 * Handler for the --wheel-lines option.
853 public void opt_wheel_lines(int type
, char *s
)
859 if (wheel_lines
<= 0)
860 wheel_lines
= default_wheel_lines();
868 * Handler for the --line-number-width option.
871 public void opt_linenum_width(int type
, char *s
)
879 if (linenum_width
> MAX_LINENUM_WIDTH
)
881 parg
.p_int
= MAX_LINENUM_WIDTH
;
882 error("Line number width must not be larger than %d", &parg
);
883 linenum_width
= MIN_LINENUM_WIDTH
;
892 * Handler for the --status-column-width option.
895 public void opt_status_col_width(int type
, char *s
)
903 if (status_col_width
> MAX_STATUSCOL_WIDTH
)
905 parg
.p_int
= MAX_STATUSCOL_WIDTH
;
906 error("Status column width must not be larger than %d", &parg
);
907 status_col_width
= 2;
916 * Handler for the --file-size option.
919 public void opt_filesize(int type
, char *s
)
925 if (want_filesize
&& curr_ifile
!= NULL
&& ch_length() == NULL_POSITION
)
934 * Handler for the --intr option.
937 public void opt_intr(int type
, char *s
)
946 if (intr_char
== '^' && s
[1] != '\0')
947 intr_char
= CONTROL(s
[1]);
950 p
.p_string
= prchar(intr_char
);
951 error("interrupt character is %s", &p
);
957 * Handler for the --header option.
960 public void opt_header(int type
, char *s
)
973 n
= getnum(&s
, "header", &err
);
976 error("invalid number of lines", NULL_PARG
);
984 n
= getnum(&s
, "header", &err
);
986 error("invalid number of columns", NULL_PARG
);
993 char buf
[2*INT_STRLEN_BOUND(int)+2];
995 SNPRINTF2(buf
, sizeof(buf
), "%d,%d", header_lines
, header_cols
);
997 error("header (lines,columns) is %s", &parg
);
1004 * Handler for the --search-options option.
1007 public void opt_search_type(int type
, char *s
)
1020 for (; *s
!= '\0'; s
++)
1024 case 'E': case 'e': case CONTROL('E'): st
|= SRCH_PAST_EOF
; break;
1025 case 'F': case 'f': case CONTROL('F'): st
|= SRCH_FIRST_FILE
; break;
1026 case 'K': case 'k': case CONTROL('K'): st
|= SRCH_NO_MOVE
; break;
1027 case 'N': case 'n': case CONTROL('N'): st
|= SRCH_NO_MATCH
; break;
1028 case 'R': case 'r': case CONTROL('R'): st
|= SRCH_NO_REGEX
; break;
1029 case 'W': case 'w': case CONTROL('W'): st
|= SRCH_WRAP
; break;
1030 case '-': st
= 0; break;
1033 if (*s
>= '1' && *s
<= '0'+NUM_SEARCH_COLORS
)
1035 st
|= SRCH_SUBSEARCH(*s
-'0');
1039 error("invalid search option '%c'", &parg
);
1043 def_search_type
= norm_search_type(st
);
1047 if (def_search_type
& SRCH_PAST_EOF
) *bp
++ = 'E';
1048 if (def_search_type
& SRCH_FIRST_FILE
) *bp
++ = 'F';
1049 if (def_search_type
& SRCH_NO_MOVE
) *bp
++ = 'K';
1050 if (def_search_type
& SRCH_NO_MATCH
) *bp
++ = 'N';
1051 if (def_search_type
& SRCH_NO_REGEX
) *bp
++ = 'R';
1052 if (def_search_type
& SRCH_WRAP
) *bp
++ = 'W';
1053 for (i
= 1; i
<= NUM_SEARCH_COLORS
; i
++)
1054 if (def_search_type
& SRCH_SUBSEARCH(i
))
1059 parg
.p_string
= buf
;
1060 error("search options: %s", &parg
);
1067 * Handler for the --tty option.
1070 public void opt_ttyin_name(int type
, char *s
)
1082 public int chop_line(void)
1084 return (chopline
|| header_cols
> 0 || header_lines
> 0);
1088 * Get the "screen window" size.
1090 public int get_swindow(void)
1094 return (sc_height
- header_lines
+ swindow
);