1238730Sdelphij/*
2369759Sgit2svn * Copyright (C) 1984-2021  Mark Nudelman
3238730Sdelphij *
4238730Sdelphij * You may distribute under the terms of either the GNU General Public
5238730Sdelphij * License or the Less License, as specified in the README file.
6238730Sdelphij *
7238730Sdelphij * For more information, see the README file.
8238730Sdelphij */
960786Sps
1060786Sps
1160786Sps/*
1260786Sps * The option table.
1360786Sps */
1460786Sps
1560786Sps#include "less.h"
1660786Sps#include "option.h"
1760786Sps
1860786Sps/*
1960786Sps * Variables controlled by command line options.
2060786Sps */
21369759Sgit2svnpublic int quiet;               /* Should we suppress the audible bell? */
22369759Sgit2svnpublic int how_search;          /* Where should forward searches start? */
23369759Sgit2svnpublic int top_scroll;          /* Repaint screen from top?
24369759Sgit2svn                                   (alternative is scroll from bottom) */
25369759Sgit2svnpublic int pr_type;             /* Type of prompt (short, medium, long) */
26369759Sgit2svnpublic int bs_mode;             /* How to process backspaces */
27369759Sgit2svnpublic int know_dumb;           /* Don't complain about dumb terminals */
28369759Sgit2svnpublic int quit_at_eof;         /* Quit after hitting end of file twice */
29369759Sgit2svnpublic int quit_if_one_screen;  /* Quit if EOF on first screen */
30369759Sgit2svnpublic int squeeze;             /* Squeeze multiple blank lines into one */
31369759Sgit2svnpublic int tabstop;             /* Tab settings */
32369759Sgit2svnpublic int back_scroll;         /* Repaint screen on backwards movement */
33369759Sgit2svnpublic int forw_scroll;         /* Repaint screen on forward movement */
34369759Sgit2svnpublic int caseless;            /* Do "caseless" searches */
35369759Sgit2svnpublic int linenums;            /* Use line numbers */
36369759Sgit2svnpublic int autobuf;             /* Automatically allocate buffers as needed */
37369759Sgit2svnpublic int bufspace;            /* Max buffer space per file (K) */
38369759Sgit2svnpublic int ctldisp;             /* Send control chars to screen untranslated */
39369759Sgit2svnpublic int force_open;          /* Open the file even if not regular file */
40369759Sgit2svnpublic int swindow;             /* Size of scrolling window */
41369759Sgit2svnpublic int jump_sline;          /* Screen line of "jump target" */
42170256Sdelphijpublic long jump_sline_fraction = -1;
43195941Sdelphijpublic long shift_count_fraction = -1;
44369759Sgit2svnpublic int chopline;            /* Truncate displayed lines at screen width */
45369759Sgit2svnpublic int no_init;             /* Disable sending ti/te termcap strings */
46369759Sgit2svnpublic int no_keypad;           /* Disable sending ks/ke termcap strings */
4760786Spspublic int twiddle;             /* Show tildes after EOF */
48369759Sgit2svnpublic int show_attn;           /* Hilite first unread line */
49369759Sgit2svnpublic int shift_count;         /* Number of positions to shift horizontally */
50369759Sgit2svnpublic int status_col;          /* Display a status column */
51369759Sgit2svnpublic int use_lessopen;        /* Use the LESSOPEN filter */
52369759Sgit2svnpublic int quit_on_intr;        /* Quit on interrupt */
53369759Sgit2svnpublic int follow_mode;         /* F cmd Follows file desc or file name? */
54369759Sgit2svnpublic int oldbot;              /* Old bottom of screen behavior {{REMOVE}} */
55369759Sgit2svnpublic int opt_use_backslash;   /* Use backslash escaping in option parsing */
56369759Sgit2svnpublic char rscroll_char;       /* Char which marks chopped lines with -S */
57369759Sgit2svnpublic int rscroll_attr;        /* Attribute of rscroll_char */
58369759Sgit2svnpublic int no_hist_dups;        /* Remove dups from history list */
59369759Sgit2svnpublic int mousecap;            /* Allow mouse for scrolling */
60369759Sgit2svnpublic int wheel_lines;         /* Number of lines to scroll on mouse wheel scroll */
61369759Sgit2svnpublic int perma_marks;         /* Save marks in history file */
62369759Sgit2svnpublic int linenum_width;       /* Width of line numbers */
63369759Sgit2svnpublic int status_col_width;    /* Width of status column */
64369759Sgit2svnpublic int incr_search;         /* Incremental search */
65369759Sgit2svnpublic int use_color;           /* Use UI color */
6660786Sps#if HILITE_SEARCH
67369759Sgit2svnpublic int hilite_search;       /* Highlight matched search patterns? */
6860786Sps#endif
6960786Sps
70369759Sgit2svnpublic int less_is_more = 0;    /* Make compatible with POSIX more */
71161475Sdelphij
7260786Sps/*
7360786Sps * Long option names.
7460786Sps */
7560786Spsstatic struct optname a_optname      = { "search-skip-screen",   NULL };
7660786Spsstatic struct optname b_optname      = { "buffers",              NULL };
7760786Spsstatic struct optname B__optname     = { "auto-buffers",         NULL };
7860786Spsstatic struct optname c_optname      = { "clear-screen",         NULL };
7960786Spsstatic struct optname d_optname      = { "dumb",                 NULL };
8060786Spsstatic struct optname D__optname     = { "color",                NULL };
8160786Spsstatic struct optname e_optname      = { "quit-at-eof",          NULL };
8260786Spsstatic struct optname f_optname      = { "force",                NULL };
8360786Spsstatic struct optname F__optname     = { "quit-if-one-screen",   NULL };
8460786Sps#if HILITE_SEARCH
8560786Spsstatic struct optname g_optname      = { "hilite-search",        NULL };
8660786Sps#endif
8760786Spsstatic struct optname h_optname      = { "max-back-scroll",      NULL };
8860786Spsstatic struct optname i_optname      = { "ignore-case",          NULL };
8960786Spsstatic struct optname j_optname      = { "jump-target",          NULL };
9063128Spsstatic struct optname J__optname     = { "status-column",        NULL };
9160786Sps#if USERFILE
9260786Spsstatic struct optname k_optname      = { "lesskey-file",         NULL };
9360786Sps#endif
94161475Sdelphijstatic struct optname K__optname     = { "quit-on-intr",         NULL };
95128345Stjrstatic struct optname L__optname     = { "no-lessopen",          NULL };
9660786Spsstatic struct optname m_optname      = { "long-prompt",          NULL };
9760786Spsstatic struct optname n_optname      = { "line-numbers",         NULL };
9860786Sps#if LOGFILE
9960786Spsstatic struct optname o_optname      = { "log-file",             NULL };
10060786Spsstatic struct optname O__optname     = { "LOG-FILE",             NULL };
10160786Sps#endif
10260786Spsstatic struct optname p_optname      = { "pattern",              NULL };
10360786Spsstatic struct optname P__optname     = { "prompt",               NULL };
10460786Spsstatic struct optname q2_optname     = { "silent",               NULL };
10560786Spsstatic struct optname q_optname      = { "quiet",                &q2_optname };
10660786Spsstatic struct optname r_optname      = { "raw-control-chars",    NULL };
10760786Spsstatic struct optname s_optname      = { "squeeze-blank-lines",  NULL };
10860786Spsstatic struct optname S__optname     = { "chop-long-lines",      NULL };
10960786Sps#if TAGS
11060786Spsstatic struct optname t_optname      = { "tag",                  NULL };
11160786Spsstatic struct optname T__optname     = { "tag-file",             NULL };
11260786Sps#endif
11360786Spsstatic struct optname u_optname      = { "underline-special",    NULL };
11460786Spsstatic struct optname V__optname     = { "version",              NULL };
11560786Spsstatic struct optname w_optname      = { "hilite-unread",        NULL };
11660786Spsstatic struct optname x_optname      = { "tabs",                 NULL };
11760786Spsstatic struct optname X__optname     = { "no-init",              NULL };
11860786Spsstatic struct optname y_optname      = { "max-forw-scroll",      NULL };
11960786Spsstatic struct optname z_optname      = { "window",               NULL };
12060786Spsstatic struct optname quote_optname  = { "quotes",               NULL };
12160786Spsstatic struct optname tilde_optname  = { "tilde",                NULL };
12260786Spsstatic struct optname query_optname  = { "help",                 NULL };
12363128Spsstatic struct optname pound_optname  = { "shift",                NULL };
12489019Spsstatic struct optname keypad_optname = { "no-keypad",            NULL };
125170256Sdelphijstatic struct optname oldbot_optname = { "old-bot",              NULL };
126173682Sdelphijstatic struct optname follow_optname = { "follow-name",          NULL };
127250592Sdelphijstatic struct optname use_backslash_optname = { "use-backslash", NULL };
128330570Sdelphijstatic struct optname rscroll_optname = { "rscroll", NULL };
129355504Sdelphijstatic struct optname nohistdups_optname = { "no-histdups",      NULL };
130355504Sdelphijstatic struct optname mousecap_optname = { "mouse",              NULL };
131355504Sdelphijstatic struct optname wheel_lines_optname = { "wheel-lines",     NULL };
132369759Sgit2svnstatic struct optname perma_marks_optname = { "save-marks",      NULL };
133369759Sgit2svnstatic struct optname linenum_width_optname = { "line-num-width", NULL };
134369759Sgit2svnstatic struct optname status_col_width_optname = { "status-col-width", NULL };
135369759Sgit2svnstatic struct optname incr_search_optname = { "incsearch",       NULL };
136369759Sgit2svnstatic struct optname use_color_optname = { "use-color",         NULL };
137369759Sgit2svn#if LESSTEST
138369759Sgit2svnstatic struct optname ttyin_name_optname = { "tty",              NULL };
139369759Sgit2svnstatic struct optname rstat_optname  = { "rstat",                NULL };
140369759Sgit2svn#endif /*LESSTEST*/
14160786Sps
14260786Sps
14360786Sps/*
14460786Sps * Table of all options and their semantics.
14589019Sps *
14689019Sps * For BOOL and TRIPLE options, odesc[0], odesc[1], odesc[2] are
14789019Sps * the description of the option when set to 0, 1 or 2, respectively.
14889019Sps * For NUMBER options, odesc[0] is the prompt to use when entering
14989019Sps * a new value, and odesc[1] is the description, which should contain
15089019Sps * one %d which is replaced by the value of the number.
15189019Sps * For STRING options, odesc[0] is the prompt to use when entering
15289019Sps * a new value, and odesc[1], if not NULL, is the set of characters
15389019Sps * that are valid in the string.
15460786Sps */
155128345Stjrstatic struct loption option[] =
15660786Sps{
15760786Sps	{ 'a', &a_optname,
158221715Sdelphij		TRIPLE, OPT_ONPLUS, &how_search, NULL,
159128345Stjr		{
160128345Stjr			"Search includes displayed screen",
161128345Stjr			"Search skips displayed screen",
162221715Sdelphij			"Search includes all of displayed screen"
163128345Stjr		}
16460786Sps	},
16560786Sps
16660786Sps	{ 'b', &b_optname,
167128345Stjr		NUMBER|INIT_HANDLER, 64, &bufspace, opt_b,
168128345Stjr		{
169128345Stjr			"Max buffer space per file (K): ",
170128345Stjr			"Max buffer space per file: %dK",
171128345Stjr			NULL
172128345Stjr		}
17360786Sps	},
17460786Sps	{ 'B', &B__optname,
17560786Sps		BOOL, OPT_ON, &autobuf, NULL,
176128345Stjr		{
177128345Stjr			"Don't automatically allocate buffers",
178128345Stjr			"Automatically allocate buffers when needed",
179128345Stjr			NULL
180128345Stjr		}
18160786Sps	},
18260786Sps	{ 'c', &c_optname,
18360786Sps		TRIPLE, OPT_OFF, &top_scroll, NULL,
184128345Stjr		{
185128345Stjr			"Repaint by scrolling from bottom of screen",
186170256Sdelphij			"Repaint by painting from top of screen",
187128345Stjr			"Repaint by painting from top of screen"
188128345Stjr		}
18960786Sps	},
19060786Sps	{ 'd', &d_optname,
19160786Sps		BOOL|NO_TOGGLE, OPT_OFF, &know_dumb, NULL,
192128345Stjr		{
193128345Stjr			"Assume intelligent terminal",
194128345Stjr			"Assume dumb terminal",
195128345Stjr			NULL
196128345Stjr		}
19760786Sps	},
19860786Sps	{ 'D', &D__optname,
199369759Sgit2svn		STRING|REPAINT|NO_QUERY, 0, NULL, opt_D,
200128345Stjr		{
201128345Stjr			"color desc: ",
202369759Sgit2svn			NULL,
203128345Stjr			NULL
204128345Stjr		}
20560786Sps	},
20660786Sps	{ 'e', &e_optname,
20760786Sps		TRIPLE, OPT_OFF, &quit_at_eof, NULL,
208128345Stjr		{
209128345Stjr			"Don't quit at end-of-file",
210128345Stjr			"Quit at end-of-file",
211128345Stjr			"Quit immediately at end-of-file"
212128345Stjr		}
21360786Sps	},
21460786Sps	{ 'f', &f_optname,
21560786Sps		BOOL, OPT_OFF, &force_open, NULL,
216128345Stjr		{
217128345Stjr			"Open only regular files",
218128345Stjr			"Open even non-regular files",
219128345Stjr			NULL
220128345Stjr		}
22160786Sps	},
22260786Sps	{ 'F', &F__optname,
22360786Sps		BOOL, OPT_OFF, &quit_if_one_screen, NULL,
224128345Stjr		{
225128345Stjr			"Don't quit if end-of-file on first screen",
226128345Stjr			"Quit if end-of-file on first screen",
227128345Stjr			NULL
228128345Stjr		}
22960786Sps	},
23060786Sps#if HILITE_SEARCH
23160786Sps	{ 'g', &g_optname,
23260786Sps		TRIPLE|HL_REPAINT, OPT_ONPLUS, &hilite_search, NULL,
233128345Stjr		{
234128345Stjr			"Don't highlight search matches",
235128345Stjr			"Highlight matches for previous search only",
236128345Stjr			"Highlight all matches for previous search pattern",
237128345Stjr		}
23860786Sps	},
23960786Sps#endif
24060786Sps	{ 'h', &h_optname,
24160786Sps		NUMBER, -1, &back_scroll, NULL,
242128345Stjr		{
243128345Stjr			"Backwards scroll limit: ",
244128345Stjr			"Backwards scroll limit is %d lines",
245128345Stjr			NULL
246128345Stjr		}
24760786Sps	},
24860786Sps	{ 'i', &i_optname,
24960786Sps		TRIPLE|HL_REPAINT, OPT_OFF, &caseless, opt_i,
250128345Stjr		{
251128345Stjr			"Case is significant in searches",
252128345Stjr			"Ignore case in searches",
253128345Stjr			"Ignore case in searches and in patterns"
254128345Stjr		}
25560786Sps	},
25660786Sps	{ 'j', &j_optname,
257170256Sdelphij		STRING, 0, NULL, opt_j,
258128345Stjr		{
259128345Stjr			"Target line: ",
260221715Sdelphij			"0123456789.-",
261128345Stjr			NULL
262128345Stjr		}
26360786Sps	},
26463128Sps	{ 'J', &J__optname,
26563128Sps		BOOL|REPAINT, OPT_OFF, &status_col, NULL,
266128345Stjr		{
267128345Stjr			"Don't display a status column",
268128345Stjr			"Display a status column",
269128345Stjr			NULL
270128345Stjr		}
27163128Sps	},
27260786Sps#if USERFILE
27360786Sps	{ 'k', &k_optname,
27460786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k,
275128345Stjr		{ NULL, NULL, NULL }
27660786Sps	},
27760786Sps#endif
278161475Sdelphij	{ 'K', &K__optname,
279161475Sdelphij		BOOL, OPT_OFF, &quit_on_intr, NULL,
280161475Sdelphij		{
281161475Sdelphij			"Interrupt (ctrl-C) returns to prompt",
282161475Sdelphij			"Interrupt (ctrl-C) exits less",
283161475Sdelphij			NULL
284161475Sdelphij		}
285161475Sdelphij	},
286128345Stjr	{ 'L', &L__optname,
287128345Stjr		BOOL, OPT_ON, &use_lessopen, NULL,
288128345Stjr		{
289128345Stjr			"Don't use the LESSOPEN filter",
290128345Stjr			"Use the LESSOPEN filter",
291128345Stjr			NULL
292128345Stjr		}
293128345Stjr	},
29460786Sps	{ 'm', &m_optname,
29560786Sps		TRIPLE, OPT_OFF, &pr_type, NULL,
296128345Stjr		{
297128345Stjr			"Short prompt",
298128345Stjr			"Medium prompt",
299128345Stjr			"Long prompt"
300128345Stjr		}
30160786Sps	},
30260786Sps	{ 'n', &n_optname,
30360786Sps		TRIPLE|REPAINT, OPT_ON, &linenums, NULL,
304128345Stjr		{
305128345Stjr			"Don't use line numbers",
306128345Stjr			"Use line numbers",
307128345Stjr			"Constantly display line numbers"
308128345Stjr		}
30960786Sps	},
31060786Sps#if LOGFILE
31160786Sps	{ 'o', &o_optname,
31260786Sps		STRING, 0, NULL, opt_o,
313128345Stjr		{ "log file: ", NULL, NULL }
31460786Sps	},
31560786Sps	{ 'O', &O__optname,
31660786Sps		STRING, 0, NULL, opt__O,
317128345Stjr		{ "Log file: ", NULL, NULL }
31860786Sps	},
31960786Sps#endif
32060786Sps	{ 'p', &p_optname,
32160786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_p,
322128345Stjr		{ NULL, NULL, NULL }
32360786Sps	},
32460786Sps	{ 'P', &P__optname,
32560786Sps		STRING, 0, NULL, opt__P,
326128345Stjr		{ "prompt: ", NULL, NULL }
32760786Sps	},
32860786Sps	{ 'q', &q_optname,
32960786Sps		TRIPLE, OPT_OFF, &quiet, NULL,
330128345Stjr		{
331128345Stjr			"Ring the bell for errors AND at eof/bof",
332128345Stjr			"Ring the bell for errors but not at eof/bof",
333128345Stjr			"Never ring the bell"
334128345Stjr		}
33560786Sps	},
33660786Sps	{ 'r', &r_optname,
33760786Sps		TRIPLE|REPAINT, OPT_OFF, &ctldisp, NULL,
338128345Stjr		{
339128345Stjr			"Display control characters as ^X",
340128345Stjr			"Display control characters directly",
341128345Stjr			"Display control characters directly, processing ANSI sequences"
342128345Stjr		}
34360786Sps	},
34460786Sps	{ 's', &s_optname,
34560786Sps		BOOL|REPAINT, OPT_OFF, &squeeze, NULL,
346128345Stjr		{
347128345Stjr			"Display all blank lines",
348128345Stjr			"Squeeze multiple blank lines",
349128345Stjr			NULL
350128345Stjr		}
35160786Sps	},
35260786Sps	{ 'S', &S__optname,
35360786Sps		BOOL|REPAINT, OPT_OFF, &chopline, NULL,
354128345Stjr		{
355128345Stjr			"Fold long lines",
356128345Stjr			"Chop long lines",
357128345Stjr			NULL
358128345Stjr		}
35960786Sps	},
36060786Sps#if TAGS
36160786Sps	{ 't', &t_optname,
36260786Sps		STRING|NO_QUERY, 0, NULL, opt_t,
363128345Stjr		{ "tag: ", NULL, NULL }
36460786Sps	},
36560786Sps	{ 'T', &T__optname,
36660786Sps		STRING, 0, NULL, opt__T,
367128345Stjr		{ "tags file: ", NULL, NULL }
36860786Sps	},
36960786Sps#endif
37060786Sps	{ 'u', &u_optname,
37160786Sps		TRIPLE|REPAINT, OPT_OFF, &bs_mode, NULL,
372128345Stjr		{
373128345Stjr			"Display underlined text in underline mode",
374128345Stjr			"Backspaces cause overstrike",
375128345Stjr			"Print backspace as ^H"
376128345Stjr		}
37760786Sps	},
37860786Sps	{ 'V', &V__optname,
37960786Sps		NOVAR, 0, NULL, opt__V,
380128345Stjr		{ NULL, NULL, NULL }
38160786Sps	},
38260786Sps	{ 'w', &w_optname,
38360786Sps		TRIPLE|REPAINT, OPT_OFF, &show_attn, NULL,
384128345Stjr		{
385128345Stjr			"Don't highlight first unread line",
386128345Stjr			"Highlight first unread line after forward-screen",
387128345Stjr			"Highlight first unread line after any forward movement",
388128345Stjr		}
38960786Sps	},
39060786Sps	{ 'x', &x_optname,
39189019Sps		STRING|REPAINT, 0, NULL, opt_x,
392128345Stjr		{
393128345Stjr			"Tab stops: ",
394128345Stjr			"0123456789,",
395128345Stjr			NULL
396128345Stjr		}
39760786Sps	},
39860786Sps	{ 'X', &X__optname,
39960786Sps		BOOL|NO_TOGGLE, OPT_OFF, &no_init, NULL,
400128345Stjr		{
401128345Stjr			"Send init/deinit strings to terminal",
402128345Stjr			"Don't use init/deinit strings",
403128345Stjr			NULL
404128345Stjr		}
40560786Sps	},
40660786Sps	{ 'y', &y_optname,
40760786Sps		NUMBER, -1, &forw_scroll, NULL,
408128345Stjr		{
409128345Stjr			"Forward scroll limit: ",
410128345Stjr			"Forward scroll limit is %d lines",
411128345Stjr			NULL
412128345Stjr		}
41360786Sps	},
41460786Sps	{ 'z', &z_optname,
41560786Sps		NUMBER, -1, &swindow, NULL,
416128345Stjr		{
417128345Stjr			"Scroll window size: ",
418128345Stjr			"Scroll window size is %d lines",
419128345Stjr			NULL
420128345Stjr		}
42160786Sps	},
42260786Sps	{ '"', &quote_optname,
42360786Sps		STRING, 0, NULL, opt_quote,
424128345Stjr		{ "quotes: ", NULL, NULL }
42560786Sps	},
42660786Sps	{ '~', &tilde_optname,
42760786Sps		BOOL|REPAINT, OPT_ON, &twiddle, NULL,
428128345Stjr		{
429128345Stjr			"Don't show tildes after end of file",
430128345Stjr			"Show tildes after end of file",
431128345Stjr			NULL
432128345Stjr		}
43360786Sps	},
43460786Sps	{ '?', &query_optname,
43560786Sps		NOVAR, 0, NULL, opt_query,
436128345Stjr		{ NULL, NULL, NULL }
43760786Sps	},
43863128Sps	{ '#', &pound_optname,
439195941Sdelphij		STRING, 0, NULL, opt_shift,
440128345Stjr		{
441128345Stjr			"Horizontal shift: ",
442195941Sdelphij			"0123456789.",
443128345Stjr			NULL
444128345Stjr		}
44563128Sps	},
446221715Sdelphij	{ OLETTER_NONE, &keypad_optname,
44789019Sps		BOOL|NO_TOGGLE, OPT_OFF, &no_keypad, NULL,
448128345Stjr		{
449128345Stjr			"Use keypad mode",
450128345Stjr			"Don't use keypad mode",
451128345Stjr			NULL
452128345Stjr		}
45389019Sps	},
454221715Sdelphij	{ OLETTER_NONE, &oldbot_optname,
455170256Sdelphij		BOOL, OPT_OFF, &oldbot, NULL,
456170256Sdelphij		{
457170256Sdelphij			"Use new bottom of screen behavior",
458170256Sdelphij			"Use old bottom of screen behavior",
459170256Sdelphij			NULL
460170256Sdelphij		}
461170256Sdelphij	},
462221715Sdelphij	{ OLETTER_NONE, &follow_optname,
463173682Sdelphij		BOOL, FOLLOW_DESC, &follow_mode, NULL,
464173682Sdelphij		{
465221715Sdelphij			"F command follows file descriptor",
466221715Sdelphij			"F command follows file name",
467173682Sdelphij			NULL
468173682Sdelphij		}
469173682Sdelphij	},
470250592Sdelphij	{ OLETTER_NONE, &use_backslash_optname,
471250592Sdelphij		BOOL, OPT_OFF, &opt_use_backslash, NULL,
472250592Sdelphij		{
473250592Sdelphij			"Use backslash escaping in command line parameters",
474250592Sdelphij			"Don't use backslash escaping in command line parameters",
475250592Sdelphij			NULL
476250592Sdelphij		}
477250592Sdelphij	},
478330570Sdelphij	{ OLETTER_NONE, &rscroll_optname,
479330570Sdelphij		STRING|REPAINT|INIT_HANDLER, 0, NULL, opt_rscroll,
480330570Sdelphij		{ "right scroll character: ", NULL, NULL }
481330570Sdelphij	},
482355504Sdelphij	{ OLETTER_NONE, &nohistdups_optname,
483355504Sdelphij		BOOL, OPT_OFF, &no_hist_dups, NULL,
484355504Sdelphij		{
485355504Sdelphij			"Allow duplicates in history list",
486355504Sdelphij			"Remove duplicates from history list",
487355504Sdelphij			NULL
488355504Sdelphij		}
489355504Sdelphij	},
490355504Sdelphij	{ OLETTER_NONE, &mousecap_optname,
491355504Sdelphij		TRIPLE, OPT_OFF, &mousecap, opt_mousecap,
492355504Sdelphij		{
493355504Sdelphij			"Ignore mouse input",
494355504Sdelphij			"Use the mouse for scrolling",
495355504Sdelphij			"Use the mouse for scrolling (reverse)"
496355504Sdelphij		}
497355504Sdelphij	},
498355504Sdelphij	{ OLETTER_NONE, &wheel_lines_optname,
499355504Sdelphij		NUMBER|INIT_HANDLER, 0, &wheel_lines, opt_wheel_lines,
500355504Sdelphij		{
501355504Sdelphij			"Lines to scroll on mouse wheel: ",
502355504Sdelphij			"Scroll %d line(s) on mouse wheel",
503355504Sdelphij			NULL
504355504Sdelphij		}
505355504Sdelphij	},
506355504Sdelphij	{ OLETTER_NONE, &perma_marks_optname,
507355504Sdelphij		BOOL, OPT_OFF, &perma_marks, NULL,
508355504Sdelphij		{
509355504Sdelphij			"Don't save marks in history file",
510355504Sdelphij			"Save marks in history file",
511355504Sdelphij			NULL
512355504Sdelphij		}
513355504Sdelphij	},
514369759Sgit2svn	{ OLETTER_NONE, &linenum_width_optname,
515369759Sgit2svn		NUMBER|REPAINT, MIN_LINENUM_WIDTH, &linenum_width, opt_linenum_width,
516369759Sgit2svn		{
517369759Sgit2svn			"Line number width: ",
518369759Sgit2svn			"Line number width is %d chars",
519369759Sgit2svn			NULL
520369759Sgit2svn		}
521369759Sgit2svn	},
522369759Sgit2svn	{ OLETTER_NONE, &status_col_width_optname,
523369759Sgit2svn		NUMBER|REPAINT, 2, &status_col_width, opt_status_col_width,
524369759Sgit2svn		{
525369759Sgit2svn			"Status column width: ",
526369759Sgit2svn			"Status column width is %d chars",
527369759Sgit2svn			NULL
528369759Sgit2svn		}
529369759Sgit2svn	},
530369759Sgit2svn	{ OLETTER_NONE, &incr_search_optname,
531369759Sgit2svn		BOOL, OPT_OFF, &incr_search, NULL,
532369759Sgit2svn		{
533369759Sgit2svn			"Incremental search is off",
534369759Sgit2svn			"Incremental search is on",
535369759Sgit2svn			NULL
536369759Sgit2svn		}
537369759Sgit2svn	},
538369759Sgit2svn	{ OLETTER_NONE, &use_color_optname,
539369759Sgit2svn		BOOL|REPAINT, OPT_OFF, &use_color, NULL,
540369759Sgit2svn		{
541369759Sgit2svn			"Don't use color",
542369759Sgit2svn			"Use color",
543369759Sgit2svn			NULL
544369759Sgit2svn		}
545369759Sgit2svn	},
546369759Sgit2svn#if LESSTEST
547369759Sgit2svn	{ OLETTER_NONE, &ttyin_name_optname,
548369759Sgit2svn		STRING|NO_TOGGLE, 0, NULL, opt_ttyin_name,
549369759Sgit2svn		{
550369759Sgit2svn			NULL,
551369759Sgit2svn			NULL,
552369759Sgit2svn			NULL
553369759Sgit2svn		}
554369759Sgit2svn	},
555369759Sgit2svn	{ OLETTER_NONE, &rstat_optname,
556369759Sgit2svn		STRING|NO_TOGGLE, 0, NULL, opt_rstat,
557369759Sgit2svn		{
558369759Sgit2svn			NULL,
559369759Sgit2svn			NULL,
560369759Sgit2svn			NULL
561369759Sgit2svn		}
562369759Sgit2svn	},
563369759Sgit2svn#endif /*LESSTEST*/
564128345Stjr	{ '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
56560786Sps};
56660786Sps
56760786Sps
56860786Sps/*
56960786Sps * Initialize each option to its default value.
57060786Sps */
57160786Sps	public void
572355504Sdelphijinit_option(VOID_PARAM)
57360786Sps{
574330570Sdelphij	struct loption *o;
575161475Sdelphij	char *p;
57660786Sps
577161475Sdelphij	p = lgetenv("LESS_IS_MORE");
578355504Sdelphij	if (!isnullenv(p))
579161475Sdelphij		less_is_more = 1;
580161475Sdelphij
58160786Sps	for (o = option;  o->oletter != '\0';  o++)
58260786Sps	{
58360786Sps		/*
58460786Sps		 * Set each variable to its default.
58560786Sps		 */
58660786Sps		if (o->ovar != NULL)
58760786Sps			*(o->ovar) = o->odefault;
588128345Stjr		if (o->otype & INIT_HANDLER)
589128345Stjr			(*(o->ofunc))(INIT, (char *) NULL);
59060786Sps	}
59160786Sps}
59260786Sps
59360786Sps/*
59460786Sps * Find an option in the option table, given its option letter.
59560786Sps */
596128345Stjr	public struct loption *
59760786Spsfindopt(c)
59860786Sps	int c;
59960786Sps{
600330570Sdelphij	struct loption *o;
60160786Sps
60260786Sps	for (o = option;  o->oletter != '\0';  o++)
60360786Sps	{
60460786Sps		if (o->oletter == c)
60560786Sps			return (o);
606161475Sdelphij		if ((o->otype & TRIPLE) && ASCII_TO_UPPER(o->oletter) == c)
60760786Sps			return (o);
60860786Sps	}
60960786Sps	return (NULL);
61060786Sps}
61160786Sps
61260786Sps/*
613128345Stjr *
614128345Stjr */
615128345Stjr	static int
616128345Stjris_optchar(c)
617128345Stjr	char c;
618128345Stjr{
619161475Sdelphij	if (ASCII_IS_UPPER(c))
620128345Stjr		return 1;
621161475Sdelphij	if (ASCII_IS_LOWER(c))
622128345Stjr		return 1;
623128345Stjr	if (c == '-')
624128345Stjr		return 1;
625128345Stjr	return 0;
626128345Stjr}
627128345Stjr
628128345Stjr/*
62960786Sps * Find an option in the option table, given its option name.
63060786Sps * p_optname is the (possibly partial) name to look for, and
63160786Sps * is updated to point after the matched name.
63260786Sps * p_oname if non-NULL is set to point to the full option name.
63360786Sps */
634128345Stjr	public struct loption *
63560786Spsfindopt_name(p_optname, p_oname, p_err)
63660786Sps	char **p_optname;
63760786Sps	char **p_oname;
63860786Sps	int *p_err;
63960786Sps{
64060786Sps	char *optname = *p_optname;
641330570Sdelphij	struct loption *o;
642330570Sdelphij	struct optname *oname;
643330570Sdelphij	int len;
64460786Sps	int uppercase;
645128345Stjr	struct loption *maxo = NULL;
64660786Sps	struct optname *maxoname = NULL;
64760786Sps	int maxlen = 0;
64860786Sps	int ambig = 0;
64960786Sps	int exact = 0;
65060786Sps
65160786Sps	/*
65260786Sps	 * Check all options.
65360786Sps	 */
65460786Sps	for (o = option;  o->oletter != '\0';  o++)
65560786Sps	{
65660786Sps		/*
65760786Sps		 * Check all names for this option.
65860786Sps		 */
65960786Sps		for (oname = o->onames;  oname != NULL;  oname = oname->onext)
66060786Sps		{
66160786Sps			/*
66260786Sps			 * Try normal match first (uppercase == 0),
66360786Sps			 * then, then if it's a TRIPLE option,
66460786Sps			 * try uppercase match (uppercase == 1).
66560786Sps			 */
66660786Sps			for (uppercase = 0;  uppercase <= 1;  uppercase++)
66760786Sps			{
66860786Sps				len = sprefix(optname, oname->oname, uppercase);
669128345Stjr				if (len <= 0 || is_optchar(optname[len]))
670128345Stjr				{
671128345Stjr					/*
672128345Stjr					 * We didn't use all of the option name.
673128345Stjr					 */
674128345Stjr					continue;
675128345Stjr				}
67660786Sps				if (!exact && len == maxlen)
67760786Sps					/*
67860786Sps					 * Already had a partial match,
67960786Sps					 * and now there's another one that
68060786Sps					 * matches the same length.
68160786Sps					 */
68260786Sps					ambig = 1;
68360786Sps				else if (len > maxlen)
68460786Sps				{
68560786Sps					/*
68660786Sps					 * Found a better match than
68760786Sps					 * the one we had.
68860786Sps					 */
68960786Sps					maxo = o;
69060786Sps					maxoname = oname;
69160786Sps					maxlen = len;
69260786Sps					ambig = 0;
69389019Sps					exact = (len == (int)strlen(oname->oname));
69460786Sps				}
69560786Sps				if (!(o->otype & TRIPLE))
69660786Sps					break;
69760786Sps			}
69860786Sps		}
69960786Sps	}
70060786Sps	if (ambig)
70160786Sps	{
70260786Sps		/*
70360786Sps		 * Name matched more than one option.
70460786Sps		 */
70560786Sps		if (p_err != NULL)
70660786Sps			*p_err = OPT_AMBIG;
70760786Sps		return (NULL);
70860786Sps	}
70960786Sps	*p_optname = optname + maxlen;
71060786Sps	if (p_oname != NULL)
711128345Stjr		*p_oname = maxoname == NULL ? NULL : maxoname->oname;
71260786Sps	return (maxo);
71360786Sps}
714