2 * Copyright (C) 1984-2012 Mark Nudelman
3 * Modified for use with illumos by Garrett D'Amore.
4 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Less License, as specified in the README file.
9 * For more information, see the README file.
13 * Handling functions for command line options.
15 * Most options are handled by the generic code in option.c.
16 * But all string options, and a few non-string options, require
17 * special handling specific to the particular option.
18 * This special processing is done by the "handling functions" in this file.
20 * Each handling function is passed a "type" and, if it is a string
21 * option, the string which should be "assigned" to the option.
22 * The type may be one of:
23 * INIT The option is being initialized from the command line.
24 * TOGGLE The option is being changed from within the program.
25 * QUERY The setting of the option is merely being queried.
33 extern int plusoption
;
39 extern int any_display
;
40 extern char openquote
;
41 extern char closequote
;
42 extern char *prproto
[];
46 extern IFILE curr_ifile
;
47 extern char version
[];
48 extern int jump_sline
;
49 extern int jump_sline_fraction
;
50 extern int less_is_more
;
51 extern char *namelogfile
;
52 extern int force_logfile
;
54 char *tagoption
= NULL
;
57 int shift_count
; /* Number of positions to shift horizontally */
58 static int shift_count_fraction
= -1;
61 * Handler for -o option.
64 opt_o(int type
, char *s
)
69 error("log file support is not available", NULL
);
77 if (ch_getflags() & CH_CANSEEK
) {
78 error("Input is not a pipe", NULL
);
82 error("Log file is already in use", NULL
);
86 namelogfile
= lglob(s
);
87 use_logfile(namelogfile
);
92 error("No log file", NULL
);
94 parg
.p_string
= namelogfile
;
95 error("Log file \"%s\"", &parg
);
102 * Handler for -O option.
105 opt__O(int type
, char *s
)
107 force_logfile
= TRUE
;
112 * Handlers for -j option.
115 opt_j(int type
, char *s
)
127 jump_sline_fraction
= getfraction(&s
, "j", &err
);
129 error("Invalid line fraction", NULL
);
133 int sline
= getnum(&s
, "j", &err
);
135 error("Invalid line number", NULL
);
138 jump_sline_fraction
= -1;
143 if (jump_sline_fraction
< 0) {
144 parg
.p_int
= jump_sline
;
145 error("Position target at screen line %d", &parg
);
147 (void) snprintf(buf
, sizeof (buf
), ".%06d",
148 jump_sline_fraction
);
150 while (len
> 2 && buf
[len
-1] == '0')
154 error("Position target at screen position %s", &parg
);
161 calc_jump_sline(void)
163 if (jump_sline_fraction
< 0)
165 jump_sline
= sc_height
* jump_sline_fraction
/ NUM_FRAC_DENOM
;
169 * Handlers for -# option.
172 opt_shift(int type
, char *s
)
184 shift_count_fraction
= getfraction(&s
, "#", &err
);
186 error("Invalid column fraction", NULL
);
190 int hs
= getnum(&s
, "#", &err
);
192 error("Invalid column number", NULL
);
195 shift_count_fraction
= -1;
200 if (shift_count_fraction
< 0) {
201 parg
.p_int
= shift_count
;
202 error("Horizontal shift %d columns", &parg
);
205 (void) snprintf(buf
, sizeof (buf
), ".%06d",
206 shift_count_fraction
);
208 while (len
> 2 && buf
[len
-1] == '0')
212 error("Horizontal shift %s of screen width", &parg
);
219 calc_shift_count(void)
221 if (shift_count_fraction
< 0)
223 shift_count
= sc_width
* shift_count_fraction
/ NUM_FRAC_DENOM
;
227 opt_k(int type
, char *s
)
235 error("Cannot use lesskey file \"%s\"", &parg
);
242 * Handler for -t option.
245 opt_t(int type
, char *s
)
253 /* Do the rest in main() */
257 error("tags support is not available", NULL
);
261 save_ifile
= save_curr_ifile();
263 * Try to open the file containing the tag
264 * and search for the tag in that file.
266 if (edit_tagfile() || (pos
= tagsearch()) == -1) {
267 /* Failed: reopen the old file. */
268 reedit_ifile(save_ifile
);
271 unsave_ifile(save_ifile
);
272 jump_loc(pos
, jump_sline
);
278 * Handler for -T option.
281 opt__T(int type
, char *s
)
294 parg
.p_string
= tags
;
295 error("Tags file \"%s\"", &parg
);
301 * Handler for -p option.
304 opt_p(int type
, char *s
)
309 * Unget a search command for the specified string.
310 * {{ This won't work if the "/" command is
311 * changed or invalidated by a .lesskey file. }}
316 * In "more" mode, the -p argument is a command,
317 * not a search string, so we don't need a slash.
326 * Handler for -P option.
329 opt__P(int type
, char *s
)
338 * Figure out which prototype string should be changed.
341 case 's': proto
= &prproto
[PR_SHORT
]; s
++; break;
342 case 'm': proto
= &prproto
[PR_MEDIUM
]; s
++; break;
343 case 'M': proto
= &prproto
[PR_LONG
]; s
++; break;
344 case '=': proto
= &eqproto
; s
++; break;
345 case 'h': proto
= &hproto
; s
++; break;
346 case 'w': proto
= &wproto
; s
++; break;
347 default: proto
= &prproto
[PR_SHORT
]; break;
353 parg
.p_string
= prproto
[pr_type
];
360 * Handler for the -b option.
363 opt_b(int type
, char *s
)
369 * Set the new number of buffers.
371 ch_setbufspace(bufspace
);
379 * Handler for the -i option.
382 opt_i(int type
, char *s
)
395 * Handler for the -V option.
398 opt__V(int type
, char *s
)
407 * Force output to stdout per GNU standard for --version output.
414 putstr("regular expressions)\n");
415 putstr("Copyright (C) 1984-2012 Mark Nudelman\n");
416 putstr("Modified for use with illumos by Garrett D'Amore.\n");
417 putstr("Copyright 2014 Garrett D'Amore\n\n");
418 putstr("less comes with NO WARRANTY, ");
419 putstr("to the extent permitted by law.\n");
420 putstr("For information about the terms of redistribution,\n");
421 putstr("see the file named README in the less distribution.\n");
422 putstr("Homepage: http://www.greenwoodsoftware.com/less\n");
430 * Handler for the -x option.
433 opt_x(int type
, char *s
)
435 extern int tabstops
[];
436 extern int ntabstops
;
437 extern int tabdefault
;
438 char tabs
[60+(4*TABSTOP_MAX
)];
445 /* Start at 1 because tabstops[0] is always zero. */
446 for (i
= 1; i
< TABSTOP_MAX
; ) {
449 while (*s
>= '0' && *s
<= '9')
450 n
= (10 * n
) + (*s
++ - '0');
451 if (n
> tabstops
[i
-1])
460 tabdefault
= tabstops
[ntabstops
-1] - tabstops
[ntabstops
-2];
463 (void) strlcpy(tabs
, "Tab stops ", sizeof(tabs
));
465 for (i
= 1; i
< ntabstops
; i
++) {
467 strlcat(tabs
, ",", sizeof(tabs
));
468 (void) snprintf(tabs
+strlen(tabs
),
469 sizeof(tabs
)-strlen(tabs
),
472 (void) snprintf(tabs
+strlen(tabs
),
473 sizeof(tabs
)-strlen(tabs
), " and then ");
475 (void) snprintf(tabs
+strlen(tabs
), sizeof(tabs
)-strlen(tabs
),
476 "every %d spaces", tabdefault
);
485 * Handler for the -" option.
488 opt_quote(int type
, char *s
)
497 openquote
= closequote
= '\0';
500 if (s
[1] != '\0' && s
[2] != '\0') {
501 error("-\" must be followed by 1 or 2 chars",
507 closequote
= openquote
;
516 error("quotes %s", &parg
);
522 * "-?" means display a help message.
523 * If from the command line, exit immediately.
526 opt_query(int type
, char *s
)
531 error("Use \"h\" for help", NULL
);
539 * Get the "screen window" size.
546 return (sc_height
+ swindow
);