opttbl.c revision 1.15
1/*
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>
5 *
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.
8 *
9 * For more information, see the README file.
10 */
11
12/*
13 * The option table.
14 */
15
16#include "less.h"
17#include "option.h"
18
19/*
20 * Variables controlled by command line options.
21 */
22int quiet;		/* Should we suppress the audible bell? */
23int how_search;		/* Where should forward searches start? */
24int top_scroll;		/* Repaint screen from top? (vs scroll from bottom) */
25int pr_type;		/* Type of prompt (short, medium, long) */
26int bs_mode;		/* How to process backspaces */
27int know_dumb;		/* Don't complain about dumb terminals */
28int quit_at_eof;	/* Quit after hitting end of file twice */
29int quit_if_one_screen;	/* Quit if EOF on first screen */
30int squeeze;		/* Squeeze multiple blank lines into one */
31int back_scroll;	/* Repaint screen on backwards movement */
32int forw_scroll;	/* Repaint screen on forward movement */
33int caseless;		/* Do "caseless" searches */
34int linenums;		/* Use line numbers */
35int autobuf;		/* Automatically allocate buffers as needed */
36int bufspace;		/* Max buffer space per file (K) */
37int ctldisp;		/* Send control chars to screen untranslated */
38int force_open;		/* Open the file even if not regular file */
39int swindow;		/* Size of scrolling window */
40int jump_sline;		/* Screen line of "jump target" */
41long jump_sline_fraction = -1;
42int chopline;		/* Truncate displayed lines at screen width */
43int no_init;		/* Disable sending ti/te termcap strings */
44int no_keypad;		/* Disable sending ks/ke termcap strings */
45int twiddle;		/* Show tildes after EOF */
46int show_attn;		/* Hilite first unread line */
47int status_col;		/* Display a status column */
48int use_lessopen;	/* Use the LESSOPEN filter */
49int quit_on_intr;	/* Quit on interrupt */
50int follow_mode;	/* F cmd Follows file desc or file name? */
51int oldbot;		/* Old bottom of screen behavior {{REMOVE}} */
52int opt_use_backslash;	/* Use backslash escaping in option parsing */
53int hilite_search;	/* Highlight matched search patterns? */
54
55int less_is_more = 0;	/* Make compatible with POSIX more */
56
57/*
58 * Long option names.
59 */
60static struct optname a_optname		= { "search-skip-screen",	NULL };
61static struct optname b_optname		= { "buffers",			NULL };
62static struct optname B__optname	= { "auto-buffers",		NULL };
63static struct optname c_optname		= { "clear-screen",		NULL };
64static struct optname d_optname		= { "dumb",			NULL };
65static struct optname e_optname		= { "quit-at-eof",		NULL };
66static struct optname f_optname		= { "force",			NULL };
67static struct optname F__optname	= { "quit-if-one-screen",	NULL };
68static struct optname g_optname		= { "hilite-search",		NULL };
69static struct optname h_optname		= { "max-back-scroll",		NULL };
70static struct optname i_optname		= { "ignore-case",		NULL };
71static struct optname j_optname		= { "jump-target",		NULL };
72static struct optname J__optname	= { "status-column",		NULL };
73static struct optname k_optname		= { "lesskey-file",		NULL };
74static struct optname K__optname	= { "quit-on-intr",		NULL };
75static struct optname L__optname	= { "no-lessopen",		NULL };
76static struct optname m_optname		= { "long-prompt",		NULL };
77static struct optname n_optname		= { "line-numbers",		NULL };
78static struct optname o_optname		= { "log-file",			NULL };
79static struct optname O__optname	= { "LOG-FILE",			NULL };
80static struct optname p_optname		= { "pattern",			NULL };
81static struct optname P__optname	= { "prompt",			NULL };
82static struct optname q2_optname	= { "silent",			NULL };
83static struct optname q_optname		= { "quiet",		&q2_optname };
84static struct optname r_optname		= { "raw-control-chars",	NULL };
85static struct optname s_optname		= { "squeeze-blank-lines",	NULL };
86static struct optname S__optname	= { "chop-long-lines",		NULL };
87static struct optname t_optname		= { "tag",			NULL };
88static struct optname T__optname	= { "tag-file",			NULL };
89static struct optname u_optname		= { "underline-special",	NULL };
90static struct optname V__optname	= { "version",			NULL };
91static struct optname w_optname		= { "hilite-unread",		NULL };
92static struct optname x_optname		= { "tabs",			NULL };
93static struct optname X__optname	= { "no-init",			NULL };
94static struct optname y_optname		= { "max-forw-scroll",		NULL };
95static struct optname z_optname		= { "window",			NULL };
96static struct optname quote_optname	= { "quotes",			NULL };
97static struct optname tilde_optname	= { "tilde",			NULL };
98static struct optname query_optname	= { "help",			NULL };
99static struct optname pound_optname	= { "shift",			NULL };
100static struct optname keypad_optname	= { "no-keypad",		NULL };
101static struct optname oldbot_optname	= { "old-bot",			NULL };
102static struct optname follow_optname	= { "follow-name",		NULL };
103static struct optname use_backslash_optname = { "use-backslash",	NULL };
104
105
106/*
107 * Table of all options and their semantics.
108 *
109 * For BOOL and TRIPLE options, odesc[0], odesc[1], odesc[2] are
110 * the description of the option when set to 0, 1 or 2, respectively.
111 * For NUMBER options, odesc[0] is the prompt to use when entering
112 * a new value, and odesc[1] is the description, which should contain
113 * one %d which is replaced by the value of the number.
114 * For STRING options, odesc[0] is the prompt to use when entering
115 * a new value, and odesc[1], if not NULL, is the set of characters
116 * that are valid in the string.
117 */
118static struct loption option[] = {
119	{ 'a', &a_optname,
120		TRIPLE, OPT_ONPLUS, &how_search, NULL,
121		{
122			"Search includes displayed screen",
123			"Search skips displayed screen",
124			"Search includes all of displayed screen"
125		}
126	},
127
128	{ 'b', &b_optname,
129		NUMBER|INIT_HANDLER, 64, &bufspace, opt_b,
130		{
131			"Max buffer space per file (K): ",
132			"Max buffer space per file: %dK",
133			NULL
134		}
135	},
136	{ 'B', &B__optname,
137		BOOL, OPT_ON, &autobuf, NULL,
138		{
139			"Don't automatically allocate buffers",
140			"Automatically allocate buffers when needed",
141			NULL
142		}
143	},
144	{ 'c', &c_optname,
145		TRIPLE|MORE_OK, OPT_ON, &top_scroll, NULL,
146		{
147			"Repaint by scrolling from bottom of screen",
148			"Repaint by painting from top of screen",
149			"Repaint by painting from top of screen"
150		}
151	},
152	{ 'd', &d_optname,
153		BOOL|MORE_OK|NO_TOGGLE, OPT_OFF, &know_dumb, NULL,
154		{
155			"Assume intelligent terminal",
156			"Assume dumb terminal",
157			NULL
158		}
159	},
160	{ 'e', &e_optname,
161		TRIPLE, OPT_OFF, &quit_at_eof, NULL,
162		{
163			"Don't quit at end-of-file",
164			"Quit at end-of-file",
165			"Quit immediately at end-of-file"
166		}
167	},
168	{ 'f', &f_optname,
169		BOOL, OPT_OFF, &force_open, NULL,
170		{
171			"Open only regular files",
172			"Open even non-regular files",
173			NULL
174		}
175	},
176	{ 'F', &F__optname,
177		BOOL, OPT_OFF, &quit_if_one_screen, NULL,
178		{
179			"Don't quit if end-of-file on first screen",
180			"Quit if end-of-file on first screen",
181			NULL
182		}
183	},
184	{ 'g', &g_optname,
185		TRIPLE|HL_REPAINT, OPT_ONPLUS, &hilite_search, NULL,
186		{
187			"Don't highlight search matches",
188			"Highlight matches for previous search only",
189			"Highlight all matches for previous search pattern",
190		}
191	},
192	{ 'h', &h_optname,
193		NUMBER, -1, &back_scroll, NULL,
194		{
195			"Backwards scroll limit: ",
196			"Backwards scroll limit is %d lines",
197			NULL
198		}
199	},
200	{ 'i', &i_optname,
201		TRIPLE|HL_REPAINT, OPT_OFF, &caseless, opt_i,
202		{
203			"Case is significant in searches",
204			"Ignore case in searches",
205			"Ignore case in searches and in patterns"
206		}
207	},
208	{ 'j', &j_optname,
209		STRING, 0, NULL, opt_j,
210		{
211			"Target line: ",
212			"0123456789.-",
213			NULL
214		}
215	},
216	{ 'J', &J__optname,
217		BOOL|REPAINT, OPT_OFF, &status_col, NULL,
218		{
219			"Don't display a status column",
220			"Display a status column",
221			NULL
222		}
223	},
224	{ 'k', &k_optname,
225		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k,
226		{ NULL, NULL, NULL }
227	},
228	{ 'K', &K__optname,
229		BOOL, OPT_OFF, &quit_on_intr, NULL,
230		{
231			"Interrupt (ctrl-C) returns to prompt",
232			"Interrupt (ctrl-C) exits less",
233			NULL
234		}
235	},
236	{ 'L', &L__optname,
237		BOOL, OPT_ON, &use_lessopen, NULL,
238		{
239			"Don't use the LESSOPEN filter",
240			"Use the LESSOPEN filter",
241			NULL
242		}
243	},
244	{ 'm', &m_optname,
245		TRIPLE, OPT_OFF, &pr_type, NULL,
246		{
247			"Short prompt",
248			"Medium prompt",
249			"Long prompt"
250		}
251	},
252	{ 'n', &n_optname,
253		TRIPLE|REPAINT, OPT_ON, &linenums, NULL,
254		{
255			"Don't use line numbers",
256			"Use line numbers",
257			"Constantly display line numbers"
258		}
259	},
260	{ 'o', &o_optname,
261		STRING, 0, NULL, opt_o,
262		{ "log file: ", NULL, NULL }
263	},
264	{ 'O', &O__optname,
265		STRING, 0, NULL, opt__O,
266		{ "Log file: ", NULL, NULL }
267	},
268	{ 'p', &p_optname,
269		STRING|NO_TOGGLE|NO_QUERY|MORE_OK, 0, NULL, opt_p,
270		{ NULL, NULL, NULL }
271	},
272	{ 'P', &P__optname,
273		STRING, 0, NULL, opt__P,
274		{ "prompt: ", NULL, NULL }
275	},
276	{ 'q', &q_optname,
277		TRIPLE, OPT_OFF, &quiet, NULL,
278		{
279			"Ring the bell for errors AND at eof/bof",
280			"Ring the bell for errors but not at eof/bof",
281			"Never ring the bell"
282		}
283	},
284	{ 'r', &r_optname,
285		TRIPLE|REPAINT, OPT_OFF, &ctldisp, NULL,
286		{
287			"Display control characters as ^X",
288			"Display control characters directly",
289			"Display control characters directly, "
290			"processing ANSI sequences"
291		}
292	},
293	{ 's', &s_optname,
294		BOOL|REPAINT|MORE_OK, OPT_OFF, &squeeze, NULL,
295		{
296			"Display all blank lines",
297			"Squeeze multiple blank lines",
298			NULL
299		}
300	},
301	{ 'S', &S__optname,
302		BOOL|REPAINT, OPT_OFF, &chopline, NULL,
303		{
304			"Fold long lines",
305			"Chop long lines",
306			NULL
307		}
308	},
309	{ 't', &t_optname,
310		STRING|NO_QUERY|MORE_OK, 0, NULL, opt_t,
311		{ "tag: ", NULL, NULL }
312	},
313	{ 'T', &T__optname,
314		STRING|MORE_OK, 0, NULL, opt__T,
315		{ "tags file: ", NULL, NULL }
316	},
317	{ 'u', &u_optname,
318		TRIPLE|REPAINT, OPT_OFF, &bs_mode, NULL,
319		{
320			"Display underlined text in underline mode",
321			"Backspaces cause overstrike",
322			"Print backspace as ^H"
323		}
324	},
325	{ 'V', &V__optname,
326		NOVAR, 0, NULL, opt__V,
327		{ NULL, NULL, NULL }
328	},
329	{ 'w', &w_optname,
330		TRIPLE|REPAINT, OPT_OFF, &show_attn, NULL,
331		{
332			"Don't highlight first unread line",
333			"Highlight first unread line after forward-screen",
334			"Highlight first unread line after any "
335			"forward movement",
336		}
337	},
338	{ 'x', &x_optname,
339		STRING|REPAINT, 0, NULL, opt_x,
340		{
341			"Tab stops: ",
342			"0123456789,",
343			NULL
344		}
345	},
346	{ 'X', &X__optname,
347		BOOL|NO_TOGGLE, OPT_OFF, &no_init, NULL,
348		{
349			"Send init/deinit strings to terminal",
350			"Don't use init/deinit strings",
351			NULL
352		}
353	},
354	{ 'y', &y_optname,
355		NUMBER, -1, &forw_scroll, NULL,
356		{
357			"Forward scroll limit: ",
358			"Forward scroll limit is %d lines",
359			NULL
360		}
361	},
362	{ 'z', &z_optname,
363		NUMBER, -1, &swindow, NULL,
364		{
365			"Scroll window size: ",
366			"Scroll window size is %d lines",
367			NULL
368		}
369	},
370	{ '"', &quote_optname,
371		STRING, 0, NULL, opt_quote,
372		{ "quotes: ", NULL, NULL }
373	},
374	{ '~', &tilde_optname,
375		BOOL|REPAINT, OPT_ON, &twiddle, NULL,
376		{
377			"Don't show tildes after end of file",
378			"Show tildes after end of file",
379			NULL
380		}
381	},
382	{ '?', &query_optname,
383		NOVAR, 0, NULL, opt_query,
384		{ NULL, NULL, NULL }
385	},
386	{ '#', &pound_optname,
387		STRING, 0, NULL, opt_shift,
388		{
389			"Horizontal shift: ",
390			"0123456789.",
391			NULL
392		}
393	},
394	{ OLETTER_NONE, &keypad_optname,
395		BOOL|NO_TOGGLE, OPT_OFF, &no_keypad, NULL,
396		{
397			"Use keypad mode",
398			"Don't use keypad mode",
399			NULL
400		}
401	},
402	{ OLETTER_NONE, &oldbot_optname,
403		BOOL, OPT_OFF, &oldbot, NULL,
404		{
405			"Use new bottom of screen behavior",
406			"Use old bottom of screen behavior",
407			NULL
408		}
409	},
410	{ OLETTER_NONE, &follow_optname,
411		BOOL, FOLLOW_DESC, &follow_mode, NULL,
412		{
413			"F command follows file descriptor",
414			"F command follows file name",
415			NULL
416		}
417	},
418	{ OLETTER_NONE, &use_backslash_optname,
419		BOOL, OPT_OFF, &opt_use_backslash, NULL,
420		{
421			"Use backslash escaping in command line parameters",
422			"Don't use backslash escaping in command line "
423			"parameters",
424			NULL
425		}
426	},
427	{ '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
428};
429
430
431/*
432 * Initialize each option to its default value.
433 */
434void
435init_option(void)
436{
437	struct loption *o;
438
439	for (o = option;  o->oletter != '\0';  o++) {
440		/*
441		 * Set each variable to its default.
442		 */
443		if (o->ovar != NULL)
444			*(o->ovar) = o->odefault;
445		if (o->otype & INIT_HANDLER)
446			(*(o->ofunc))(INIT, NULL);
447	}
448}
449
450/*
451 * Find an option in the option table, given its option letter.
452 */
453struct loption *
454findopt(int c)
455{
456	struct loption *o;
457
458	for (o = option;  o->oletter != '\0';  o++) {
459		if (o->oletter == c)
460			return (o);
461		if ((o->otype & TRIPLE) && (toupper(o->oletter) == c))
462			return (o);
463	}
464	return (NULL);
465}
466
467/*
468 *
469 */
470static int
471is_optchar(char c)
472{
473	if (isupper(c))
474		return (1);
475	if (islower(c))
476		return (1);
477	if (c == '-')
478		return (1);
479	return (0);
480}
481
482/*
483 * Find an option in the option table, given its option name.
484 * p_optname is the (possibly partial) name to look for, and
485 * is updated to point after the matched name.
486 * p_oname if non-NULL is set to point to the full option name.
487 */
488struct loption *
489findopt_name(char **p_optname, char **p_oname, int *p_err)
490{
491	char *optname = *p_optname;
492	struct loption *o;
493	struct optname *oname;
494	int len;
495	int uppercase;
496	struct loption *maxo = NULL;
497	struct optname *maxoname = NULL;
498	int maxlen = 0;
499	int ambig = 0;
500	int exact = 0;
501
502	/*
503	 * Check all options.
504	 */
505	for (o = option;  o->oletter != '\0';  o++) {
506		/*
507		 * Check all names for this option.
508		 */
509		for (oname = o->onames;  oname != NULL;  oname = oname->onext) {
510			/*
511			 * Try normal match first (uppercase == 0),
512			 * then, then if it's a TRIPLE option,
513			 * try uppercase match (uppercase == 1).
514			 */
515			for (uppercase = 0;  uppercase <= 1;  uppercase++) {
516				len = sprefix(optname, oname->oname, uppercase);
517				if (len <= 0 || is_optchar(optname[len])) {
518					/*
519					 * We didn't use all of the option name.
520					 */
521					continue;
522				}
523				if (!exact && len == maxlen) {
524					/*
525					 * Already had a partial match,
526					 * and now there's another one that
527					 * matches the same length.
528					 */
529					ambig = 1;
530				} else if (len > maxlen) {
531					/*
532					 * Found a better match than
533					 * the one we had.
534					 */
535					maxo = o;
536					maxoname = oname;
537					maxlen = len;
538					ambig = 0;
539					exact = (len == strlen(oname->oname));
540				}
541				if (!(o->otype & TRIPLE))
542					break;
543			}
544		}
545	}
546	if (ambig) {
547		/*
548		 * Name matched more than one option.
549		 */
550		if (p_err != NULL)
551			*p_err = OPT_AMBIG;
552		return (NULL);
553	}
554	*p_optname = optname + maxlen;
555	if (p_oname != NULL)
556		*p_oname = maxoname == NULL ? NULL : maxoname->oname;
557	return (maxo);
558}
559