2 * Copyright (C) 1984-2021 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;
63 extern char *namelogfile;
64 extern int force_logfile;
68 public char *tagoption = NULL;
73 extern char *ttyin_name;
74 extern int rstat_file;
77 extern int nm_fg_color, nm_bg_color;
78 extern int bo_fg_color, bo_bg_color;
79 extern int ul_fg_color, ul_bg_color;
80 extern int so_fg_color, so_bg_color;
81 extern int bl_fg_color, bl_bg_color;
83 #if MSDOS_COMPILER==WIN32C
84 #ifndef COMMON_LVB_UNDERSCORE
85 #define COMMON_LVB_UNDERSCORE 0x8000
93 * Handler for -o option.
105 error("log file support is not available", NULL_PARG);
111 namelogfile = save(s);
114 if (ch_getflags() & CH_CANSEEK)
116 error("Input is not a pipe", NULL_PARG);
121 error("Log file is already in use", NULL_PARG);
125 if (namelogfile != NULL)
128 namelogfile = shell_unquote(filename);
130 use_logfile(namelogfile);
135 error("No log file", NULL_PARG);
138 parg.p_string = namelogfile;
139 error("Log file \"%s\"", &parg);
146 * Handler for -O option.
153 force_logfile = TRUE;
159 * Handlers for -j option.
178 jump_sline_fraction = getfraction(&s, "j", &err);
180 error("Invalid line fraction", NULL_PARG);
185 int sline = getnum(&s, "j", &err);
187 error("Invalid line number", NULL_PARG);
191 jump_sline_fraction = -1;
196 if (jump_sline_fraction < 0)
198 parg.p_int = jump_sline;
199 error("Position target at screen line %d", &parg);
203 SNPRINTF1(buf, sizeof(buf), ".%06ld", jump_sline_fraction);
204 len = (int) strlen(buf);
205 while (len > 2 && buf[len-1] == '0')
209 error("Position target at screen position %s", &parg);
216 calc_jump_sline(VOID_PARAM)
218 if (jump_sline_fraction < 0)
220 jump_sline = sc_height * jump_sline_fraction / NUM_FRAC_DENOM;
224 * Handlers for -# option.
243 shift_count_fraction = getfraction(&s, "#", &err);
245 error("Invalid column fraction", NULL_PARG);
250 int hs = getnum(&s, "#", &err);
252 error("Invalid column number", NULL_PARG);
256 shift_count_fraction = -1;
261 if (shift_count_fraction < 0)
263 parg.p_int = shift_count;
264 error("Horizontal shift %d columns", &parg);
268 SNPRINTF1(buf, sizeof(buf), ".%06ld", shift_count_fraction);
269 len = (int) strlen(buf);
270 while (len > 2 && buf[len-1] == '0')
274 error("Horizontal shift %s of screen width", &parg);
281 calc_shift_count(VOID_PARAM)
283 if (shift_count_fraction < 0)
285 shift_count = sc_width * shift_count_fraction / NUM_FRAC_DENOM;
302 error("Cannot use lesskey file \"%s\"", &parg);
319 if (lesskey_src(s, 0))
322 error("Cannot use lesskey source file \"%s\"", &parg);
327 #endif /* HAVE_LESSKEYSRC */
328 #endif /* USERFILE */
332 * Handler for -t option.
346 /* Do the rest in main() */
351 error("tags support is not available", NULL_PARG);
355 save_ifile = save_curr_ifile();
357 * Try to open the file containing the tag
358 * and search for the tag in that file.
360 if (edit_tagfile() || (pos = tagsearch()) == NULL_POSITION)
362 /* Failed: reopen the old file. */
363 reedit_ifile(save_ifile);
366 unsave_ifile(save_ifile);
367 jump_loc(pos, jump_sline);
373 * Handler for -T option.
390 if (tags != NULL && tags != ztags)
393 tags = shell_unquote(filename);
397 parg.p_string = tags;
398 error("Tags file \"%s\"", &parg);
405 * Handler for -p option.
416 * Unget a command for the specified string.
421 * In "more" mode, the -p argument is a command,
422 * not a search string, so we don't need a slash.
424 every_first_cmd = save(s);
429 * {{ This won't work if the "/" command is
430 * changed or invalidated by a .lesskey file. }}
434 ungetcc_back(CHAR_END_COMMAND);
441 * Handler for -P option.
456 * Figure out which prototype string should be changed.
460 case 's': proto = &prproto[PR_SHORT]; s++; break;
461 case 'm': proto = &prproto[PR_MEDIUM]; s++; break;
462 case 'M': proto = &prproto[PR_LONG]; s++; break;
463 case '=': proto = &eqproto; s++; break;
464 case 'h': proto = &hproto; s++; break;
465 case 'w': proto = &wproto; s++; break;
466 default: proto = &prproto[PR_SHORT]; break;
472 parg.p_string = prproto[pr_type];
479 * Handler for the -b option.
492 * Set the new number of buffers.
494 ch_setbufspace(bufspace);
502 * Handler for the -i option.
522 * Handler for the -V option.
537 set_output(1); /* Force output to stdout per GNU standard for --version output. */
541 putstr(pattern_lib_name());
542 putstr(" regular expressions)\n");
544 char constant *copyright =
545 "Copyright (C) 1984-2021 Mark Nudelman\n\n";
548 if (version[strlen(version)-1] == 'x')
550 putstr("** This is an EXPERIMENTAL build of the 'less' software,\n");
551 putstr("** and may not function correctly.\n");
552 putstr("** Obtain release builds from the web page below.\n\n");
554 putstr("less comes with NO WARRANTY, to the extent permitted by law.\n");
555 putstr("For information about the terms of redistribution,\n");
556 putstr("see the file named README in the less distribution.\n");
557 putstr("Home page: https://greenwoodsoftware.com/less\n");
565 * Parse an MSDOS color descriptor.
568 colordesc(s, fg_color, bg_color)
574 #if MSDOS_COMPILER==WIN32C
579 ul = COMMON_LVB_UNDERSCORE;
583 *fg_color = nm_fg_color | ul;
584 *bg_color = nm_bg_color;
589 if (parse_color(s, &fg, &bg) == CT_NULL)
593 error("Invalid color string \"%s\"", &p);
596 if (fg == CV_NOCHANGE)
598 if (bg == CV_NOCHANGE)
600 #if MSDOS_COMPILER==WIN32C
610 color_from_namechar(namechar)
615 case 'W': case 'A': return AT_COLOR_ATTN;
616 case 'B': return AT_COLOR_BIN;
617 case 'C': return AT_COLOR_CTRL;
618 case 'E': return AT_COLOR_ERROR;
619 case 'M': return AT_COLOR_MARK;
620 case 'N': return AT_COLOR_LINENUM;
621 case 'P': return AT_COLOR_PROMPT;
622 case 'R': return AT_COLOR_RSCROLL;
623 case 'S': return AT_COLOR_SEARCH;
624 case 'n': return AT_NORMAL;
625 case 's': return AT_STANDOUT;
626 case 'd': return AT_BOLD;
627 case 'u': return AT_UNDERLINE;
628 case 'k': return AT_BLINK;
634 * Handler for the -D option.
652 sgr_mode = !sgr_mode;
656 attr = color_from_namechar(s[0]);
660 error("Invalid color specifier '%c'", &p);
663 if (!use_color && (attr & AT_COLOR))
665 error("Set --use-color before changing colors", NULL_PARG);
670 if (!(attr & AT_COLOR))
675 colordesc(s, &nm_fg_color, &nm_bg_color);
678 colordesc(s, &bo_fg_color, &bo_bg_color);
681 colordesc(s, &ul_fg_color, &ul_bg_color);
684 colordesc(s, &bl_fg_color, &bl_bg_color);
687 colordesc(s, &so_fg_color, &so_bg_color);
692 at_enter(AT_STANDOUT);
697 if (set_color_map(attr, s) < 0)
700 error("Invalid color string \"%s\"", &p);
706 p.p_string = (sgr_mode) ? "on" : "off";
707 error("SGR mode is %s", &p);
714 * Handler for the -x option.
721 extern int tabstops[];
722 extern int ntabstops;
723 extern int tabdefault;
724 char msg[60+(4*TABSTOP_MAX)];
732 /* Start at 1 because tabstops[0] is always zero. */
733 for (i = 1; i < TABSTOP_MAX; )
737 while (*s >= '0' && *s <= '9')
738 n = (10 * n) + (*s++ - '0');
739 if (n > tabstops[i-1])
748 tabdefault = tabstops[ntabstops-1] - tabstops[ntabstops-2];
751 strcpy(msg, "Tab stops ");
754 for (i = 1; i < ntabstops; i++)
758 sprintf(msg+strlen(msg), "%d", tabstops[i]);
760 sprintf(msg+strlen(msg), " and then ");
762 sprintf(msg+strlen(msg), "every %d spaces",
772 * Handler for the -" option.
788 openquote = closequote = '\0';
791 if (s[1] != '\0' && s[2] != '\0')
793 error("-\" must be followed by 1 or 2 chars", NULL_PARG);
798 closequote = openquote;
807 error("quotes %s", &parg);
813 * Handler for the --rscroll option.
828 int attr = AT_STANDOUT;
829 setfmt(s, &fmt, &attr, "*s>");
830 if (strcmp(fmt, "-") == 0)
835 rscroll_char = *fmt ? *fmt : '>';
836 rscroll_attr = attr|AT_COLOR_RSCROLL;
840 p.p_string = rscroll_char ? prchar(rscroll_char) : "-";
841 error("rscroll char is %s", &p);
847 * "-?" means display a help message.
848 * If from the command line, exit immediately.
860 error("Use \"h\" for help", NULL_PARG);
868 * Handler for the --mouse option.
872 opt_mousecap(type, s)
879 if (mousecap == OPT_OFF)
891 * Handler for the --wheel-lines option.
895 opt_wheel_lines(type, s)
903 if (wheel_lines <= 0)
904 wheel_lines = default_wheel_lines();
912 * Handler for the --line-number-width option.
916 opt_linenum_width(type, s)
926 if (linenum_width > MAX_LINENUM_WIDTH)
928 parg.p_int = MAX_LINENUM_WIDTH;
929 error("Line number width must not be larger than %d", &parg);
930 linenum_width = MIN_LINENUM_WIDTH;
939 * Handler for the --status-column-width option.
943 opt_status_col_width(type, s)
953 if (status_col_width > MAX_STATUSCOL_WIDTH)
955 parg.p_int = MAX_STATUSCOL_WIDTH;
956 error("Status column width must not be larger than %d", &parg);
957 status_col_width = 2;
966 * Handler for the --file-size option.
970 opt_filesize(type, s)
978 if (want_filesize && curr_ifile != NULL && ch_length() == NULL_POSITION)
988 * Handler for the --tty option.
992 opt_ttyin_name(type, s)
1006 * Handler for the --rstat option.
1017 rstat_file = open(s, O_WRONLY|O_CREAT, 0664);
1022 error("Cannot create rstat file \"%s\"", &parg);
1030 * Get the "screen window" size.
1033 get_swindow(VOID_PARAM)
1037 return (sc_height + swindow);