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