1238730Sdelphij/*
2330571Sdelphij * Copyright (C) 1984-2017  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 */
2160786Spspublic int quiet;		/* Should we suppress the audible bell? */
2260786Spspublic int how_search;		/* Where should forward searches start? */
2360786Spspublic int top_scroll;		/* Repaint screen from top?
2460786Sps				   (alternative is scroll from bottom) */
2560786Spspublic int pr_type;		/* Type of prompt (short, medium, long) */
2660786Spspublic int bs_mode;		/* How to process backspaces */
2760786Spspublic int know_dumb;		/* Don't complain about dumb terminals */
2860786Spspublic int quit_at_eof;		/* Quit after hitting end of file twice */
2960786Spspublic int quit_if_one_screen;	/* Quit if EOF on first screen */
3060786Spspublic int squeeze;		/* Squeeze multiple blank lines into one */
3160786Spspublic int tabstop;		/* Tab settings */
3260786Spspublic int back_scroll;		/* Repaint screen on backwards movement */
3360786Spspublic int forw_scroll;		/* Repaint screen on forward movement */
3460786Spspublic int caseless;		/* Do "caseless" searches */
3560786Spspublic int linenums;		/* Use line numbers */
3660786Spspublic int autobuf;		/* Automatically allocate buffers as needed */
37128345Stjrpublic int bufspace;		/* Max buffer space per file (K) */
3860786Spspublic int ctldisp;		/* Send control chars to screen untranslated */
3960786Spspublic int force_open;		/* Open the file even if not regular file */
4060786Spspublic int swindow;		/* Size of scrolling window */
4160786Spspublic int jump_sline;		/* Screen line of "jump target" */
42170256Sdelphijpublic long jump_sline_fraction = -1;
43195941Sdelphijpublic long shift_count_fraction = -1;
4460786Spspublic int chopline;		/* Truncate displayed lines at screen width */
4560786Spspublic int no_init;		/* Disable sending ti/te termcap strings */
4689019Spspublic int no_keypad;		/* Disable sending ks/ke termcap strings */
4760786Spspublic int twiddle;             /* Show tildes after EOF */
4860786Spspublic int show_attn;		/* Hilite first unread line */
4963128Spspublic int shift_count;		/* Number of positions to shift horizontally */
5063128Spspublic int status_col;		/* Display a status column */
51128345Stjrpublic int use_lessopen;	/* Use the LESSOPEN filter */
52161475Sdelphijpublic int quit_on_intr;	/* Quit on interrupt */
53173682Sdelphijpublic int follow_mode;		/* F cmd Follows file desc or file name? */
54173682Sdelphijpublic int oldbot;		/* Old bottom of screen behavior {{REMOVE}} */
55250592Sdelphijpublic int opt_use_backslash;	/* Use backslash escaping in option parsing */
56330571Sdelphijpublic LWCHAR rscroll_char;	/* Char which marks chopped lines with -S */
57330571Sdelphijpublic int rscroll_attr;	/* Attribute of rscroll_char */
5860786Sps#if HILITE_SEARCH
5960786Spspublic int hilite_search;	/* Highlight matched search patterns? */
6060786Sps#endif
6160786Sps
62161475Sdelphijpublic int less_is_more = 0;	/* Make compatible with POSIX more */
63161475Sdelphij
6460786Sps/*
6560786Sps * Long option names.
6660786Sps */
6760786Spsstatic struct optname a_optname      = { "search-skip-screen",   NULL };
6860786Spsstatic struct optname b_optname      = { "buffers",              NULL };
6960786Spsstatic struct optname B__optname     = { "auto-buffers",         NULL };
7060786Spsstatic struct optname c_optname      = { "clear-screen",         NULL };
7160786Spsstatic struct optname d_optname      = { "dumb",                 NULL };
7260786Sps#if MSDOS_COMPILER
7360786Spsstatic struct optname D__optname     = { "color",                NULL };
7460786Sps#endif
7560786Spsstatic struct optname e_optname      = { "quit-at-eof",          NULL };
7660786Spsstatic struct optname f_optname      = { "force",                NULL };
7760786Spsstatic struct optname F__optname     = { "quit-if-one-screen",   NULL };
7860786Sps#if HILITE_SEARCH
7960786Spsstatic struct optname g_optname      = { "hilite-search",        NULL };
8060786Sps#endif
8160786Spsstatic struct optname h_optname      = { "max-back-scroll",      NULL };
8260786Spsstatic struct optname i_optname      = { "ignore-case",          NULL };
8360786Spsstatic struct optname j_optname      = { "jump-target",          NULL };
8463128Spsstatic struct optname J__optname     = { "status-column",        NULL };
8560786Sps#if USERFILE
8660786Spsstatic struct optname k_optname      = { "lesskey-file",         NULL };
8760786Sps#endif
88161475Sdelphijstatic struct optname K__optname     = { "quit-on-intr",         NULL };
89128345Stjrstatic struct optname L__optname     = { "no-lessopen",          NULL };
9060786Spsstatic struct optname m_optname      = { "long-prompt",          NULL };
9160786Spsstatic struct optname n_optname      = { "line-numbers",         NULL };
9260786Sps#if LOGFILE
9360786Spsstatic struct optname o_optname      = { "log-file",             NULL };
9460786Spsstatic struct optname O__optname     = { "LOG-FILE",             NULL };
9560786Sps#endif
9660786Spsstatic struct optname p_optname      = { "pattern",              NULL };
9760786Spsstatic struct optname P__optname     = { "prompt",               NULL };
9860786Spsstatic struct optname q2_optname     = { "silent",               NULL };
9960786Spsstatic struct optname q_optname      = { "quiet",                &q2_optname };
10060786Spsstatic struct optname r_optname      = { "raw-control-chars",    NULL };
10160786Spsstatic struct optname s_optname      = { "squeeze-blank-lines",  NULL };
10260786Spsstatic struct optname S__optname     = { "chop-long-lines",      NULL };
10360786Sps#if TAGS
10460786Spsstatic struct optname t_optname      = { "tag",                  NULL };
10560786Spsstatic struct optname T__optname     = { "tag-file",             NULL };
10660786Sps#endif
10760786Spsstatic struct optname u_optname      = { "underline-special",    NULL };
10860786Spsstatic struct optname V__optname     = { "version",              NULL };
10960786Spsstatic struct optname w_optname      = { "hilite-unread",        NULL };
11060786Spsstatic struct optname x_optname      = { "tabs",                 NULL };
11160786Spsstatic struct optname X__optname     = { "no-init",              NULL };
11260786Spsstatic struct optname y_optname      = { "max-forw-scroll",      NULL };
11360786Spsstatic struct optname z_optname      = { "window",               NULL };
11460786Spsstatic struct optname quote_optname  = { "quotes",               NULL };
11560786Spsstatic struct optname tilde_optname  = { "tilde",                NULL };
11660786Spsstatic struct optname query_optname  = { "help",                 NULL };
11763128Spsstatic struct optname pound_optname  = { "shift",                NULL };
11889019Spsstatic struct optname keypad_optname = { "no-keypad",            NULL };
119170256Sdelphijstatic struct optname oldbot_optname = { "old-bot",              NULL };
120173682Sdelphijstatic struct optname follow_optname = { "follow-name",          NULL };
121250592Sdelphijstatic struct optname use_backslash_optname = { "use-backslash", NULL };
122330571Sdelphijstatic struct optname rscroll_optname = { "rscroll", NULL };
12360786Sps
12460786Sps
12560786Sps/*
12660786Sps * Table of all options and their semantics.
12789019Sps *
12889019Sps * For BOOL and TRIPLE options, odesc[0], odesc[1], odesc[2] are
12989019Sps * the description of the option when set to 0, 1 or 2, respectively.
13089019Sps * For NUMBER options, odesc[0] is the prompt to use when entering
13189019Sps * a new value, and odesc[1] is the description, which should contain
13289019Sps * one %d which is replaced by the value of the number.
13389019Sps * For STRING options, odesc[0] is the prompt to use when entering
13489019Sps * a new value, and odesc[1], if not NULL, is the set of characters
13589019Sps * that are valid in the string.
13660786Sps */
137128345Stjrstatic struct loption option[] =
13860786Sps{
13960786Sps	{ 'a', &a_optname,
140221715Sdelphij		TRIPLE, OPT_ONPLUS, &how_search, NULL,
141128345Stjr		{
142128345Stjr			"Search includes displayed screen",
143128345Stjr			"Search skips displayed screen",
144221715Sdelphij			"Search includes all of displayed screen"
145128345Stjr		}
14660786Sps	},
14760786Sps
14860786Sps	{ 'b', &b_optname,
149128345Stjr		NUMBER|INIT_HANDLER, 64, &bufspace, opt_b,
150128345Stjr		{
151128345Stjr			"Max buffer space per file (K): ",
152128345Stjr			"Max buffer space per file: %dK",
153128345Stjr			NULL
154128345Stjr		}
15560786Sps	},
15660786Sps	{ 'B', &B__optname,
15760786Sps		BOOL, OPT_ON, &autobuf, NULL,
158128345Stjr		{
159128345Stjr			"Don't automatically allocate buffers",
160128345Stjr			"Automatically allocate buffers when needed",
161128345Stjr			NULL
162128345Stjr		}
16360786Sps	},
16460786Sps	{ 'c', &c_optname,
16560786Sps		TRIPLE, OPT_OFF, &top_scroll, NULL,
166128345Stjr		{
167128345Stjr			"Repaint by scrolling from bottom of screen",
168170256Sdelphij			"Repaint by painting from top of screen",
169128345Stjr			"Repaint by painting from top of screen"
170128345Stjr		}
17160786Sps	},
17260786Sps	{ 'd', &d_optname,
17360786Sps		BOOL|NO_TOGGLE, OPT_OFF, &know_dumb, NULL,
174128345Stjr		{
175128345Stjr			"Assume intelligent terminal",
176128345Stjr			"Assume dumb terminal",
177128345Stjr			NULL
178128345Stjr		}
17960786Sps	},
18060786Sps#if MSDOS_COMPILER
18160786Sps	{ 'D', &D__optname,
182330571Sdelphij		STRING|REPAINT, 0, NULL, opt_D,
183128345Stjr		{
184128345Stjr			"color desc: ",
185330571Sdelphij			"Dadknsu0123456789.",
186128345Stjr			NULL
187128345Stjr		}
18860786Sps	},
18960786Sps#endif
19060786Sps	{ 'e', &e_optname,
19160786Sps		TRIPLE, OPT_OFF, &quit_at_eof, NULL,
192128345Stjr		{
193128345Stjr			"Don't quit at end-of-file",
194128345Stjr			"Quit at end-of-file",
195128345Stjr			"Quit immediately at end-of-file"
196128345Stjr		}
19760786Sps	},
19860786Sps	{ 'f', &f_optname,
19960786Sps		BOOL, OPT_OFF, &force_open, NULL,
200128345Stjr		{
201128345Stjr			"Open only regular files",
202128345Stjr			"Open even non-regular files",
203128345Stjr			NULL
204128345Stjr		}
20560786Sps	},
20660786Sps	{ 'F', &F__optname,
20760786Sps		BOOL, OPT_OFF, &quit_if_one_screen, NULL,
208128345Stjr		{
209128345Stjr			"Don't quit if end-of-file on first screen",
210128345Stjr			"Quit if end-of-file on first screen",
211128345Stjr			NULL
212128345Stjr		}
21360786Sps	},
21460786Sps#if HILITE_SEARCH
21560786Sps	{ 'g', &g_optname,
21660786Sps		TRIPLE|HL_REPAINT, OPT_ONPLUS, &hilite_search, NULL,
217128345Stjr		{
218128345Stjr			"Don't highlight search matches",
219128345Stjr			"Highlight matches for previous search only",
220128345Stjr			"Highlight all matches for previous search pattern",
221128345Stjr		}
22260786Sps	},
22360786Sps#endif
22460786Sps	{ 'h', &h_optname,
22560786Sps		NUMBER, -1, &back_scroll, NULL,
226128345Stjr		{
227128345Stjr			"Backwards scroll limit: ",
228128345Stjr			"Backwards scroll limit is %d lines",
229128345Stjr			NULL
230128345Stjr		}
23160786Sps	},
23260786Sps	{ 'i', &i_optname,
23360786Sps		TRIPLE|HL_REPAINT, OPT_OFF, &caseless, opt_i,
234128345Stjr		{
235128345Stjr			"Case is significant in searches",
236128345Stjr			"Ignore case in searches",
237128345Stjr			"Ignore case in searches and in patterns"
238128345Stjr		}
23960786Sps	},
24060786Sps	{ 'j', &j_optname,
241170256Sdelphij		STRING, 0, NULL, opt_j,
242128345Stjr		{
243128345Stjr			"Target line: ",
244221715Sdelphij			"0123456789.-",
245128345Stjr			NULL
246128345Stjr		}
24760786Sps	},
24863128Sps	{ 'J', &J__optname,
24963128Sps		BOOL|REPAINT, OPT_OFF, &status_col, NULL,
250128345Stjr		{
251128345Stjr			"Don't display a status column",
252128345Stjr			"Display a status column",
253128345Stjr			NULL
254128345Stjr		}
25563128Sps	},
25660786Sps#if USERFILE
25760786Sps	{ 'k', &k_optname,
25860786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k,
259128345Stjr		{ NULL, NULL, NULL }
26060786Sps	},
26160786Sps#endif
262161475Sdelphij	{ 'K', &K__optname,
263161475Sdelphij		BOOL, OPT_OFF, &quit_on_intr, NULL,
264161475Sdelphij		{
265161475Sdelphij			"Interrupt (ctrl-C) returns to prompt",
266161475Sdelphij			"Interrupt (ctrl-C) exits less",
267161475Sdelphij			NULL
268161475Sdelphij		}
269161475Sdelphij	},
270128345Stjr	{ 'L', &L__optname,
271128345Stjr		BOOL, OPT_ON, &use_lessopen, NULL,
272128345Stjr		{
273128345Stjr			"Don't use the LESSOPEN filter",
274128345Stjr			"Use the LESSOPEN filter",
275128345Stjr			NULL
276128345Stjr		}
277128345Stjr	},
27860786Sps	{ 'm', &m_optname,
27960786Sps		TRIPLE, OPT_OFF, &pr_type, NULL,
280128345Stjr		{
281128345Stjr			"Short prompt",
282128345Stjr			"Medium prompt",
283128345Stjr			"Long prompt"
284128345Stjr		}
28560786Sps	},
28660786Sps	{ 'n', &n_optname,
28760786Sps		TRIPLE|REPAINT, OPT_ON, &linenums, NULL,
288128345Stjr		{
289128345Stjr			"Don't use line numbers",
290128345Stjr			"Use line numbers",
291128345Stjr			"Constantly display line numbers"
292128345Stjr		}
29360786Sps	},
29460786Sps#if LOGFILE
29560786Sps	{ 'o', &o_optname,
29660786Sps		STRING, 0, NULL, opt_o,
297128345Stjr		{ "log file: ", NULL, NULL }
29860786Sps	},
29960786Sps	{ 'O', &O__optname,
30060786Sps		STRING, 0, NULL, opt__O,
301128345Stjr		{ "Log file: ", NULL, NULL }
30260786Sps	},
30360786Sps#endif
30460786Sps	{ 'p', &p_optname,
30560786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_p,
306128345Stjr		{ NULL, NULL, NULL }
30760786Sps	},
30860786Sps	{ 'P', &P__optname,
30960786Sps		STRING, 0, NULL, opt__P,
310128345Stjr		{ "prompt: ", NULL, NULL }
31160786Sps	},
31260786Sps	{ 'q', &q_optname,
31360786Sps		TRIPLE, OPT_OFF, &quiet, NULL,
314128345Stjr		{
315128345Stjr			"Ring the bell for errors AND at eof/bof",
316128345Stjr			"Ring the bell for errors but not at eof/bof",
317128345Stjr			"Never ring the bell"
318128345Stjr		}
31960786Sps	},
32060786Sps	{ 'r', &r_optname,
32160786Sps		TRIPLE|REPAINT, OPT_OFF, &ctldisp, NULL,
322128345Stjr		{
323128345Stjr			"Display control characters as ^X",
324128345Stjr			"Display control characters directly",
325128345Stjr			"Display control characters directly, processing ANSI sequences"
326128345Stjr		}
32760786Sps	},
32860786Sps	{ 's', &s_optname,
32960786Sps		BOOL|REPAINT, OPT_OFF, &squeeze, NULL,
330128345Stjr		{
331128345Stjr			"Display all blank lines",
332128345Stjr			"Squeeze multiple blank lines",
333128345Stjr			NULL
334128345Stjr		}
33560786Sps	},
33660786Sps	{ 'S', &S__optname,
33760786Sps		BOOL|REPAINT, OPT_OFF, &chopline, NULL,
338128345Stjr		{
339128345Stjr			"Fold long lines",
340128345Stjr			"Chop long lines",
341128345Stjr			NULL
342128345Stjr		}
34360786Sps	},
34460786Sps#if TAGS
34560786Sps	{ 't', &t_optname,
34660786Sps		STRING|NO_QUERY, 0, NULL, opt_t,
347128345Stjr		{ "tag: ", NULL, NULL }
34860786Sps	},
34960786Sps	{ 'T', &T__optname,
35060786Sps		STRING, 0, NULL, opt__T,
351128345Stjr		{ "tags file: ", NULL, NULL }
35260786Sps	},
35360786Sps#endif
35460786Sps	{ 'u', &u_optname,
35560786Sps		TRIPLE|REPAINT, OPT_OFF, &bs_mode, NULL,
356128345Stjr		{
357128345Stjr			"Display underlined text in underline mode",
358128345Stjr			"Backspaces cause overstrike",
359128345Stjr			"Print backspace as ^H"
360128345Stjr		}
36160786Sps	},
36260786Sps	{ 'V', &V__optname,
36360786Sps		NOVAR, 0, NULL, opt__V,
364128345Stjr		{ NULL, NULL, NULL }
36560786Sps	},
36660786Sps	{ 'w', &w_optname,
36760786Sps		TRIPLE|REPAINT, OPT_OFF, &show_attn, NULL,
368128345Stjr		{
369128345Stjr			"Don't highlight first unread line",
370128345Stjr			"Highlight first unread line after forward-screen",
371128345Stjr			"Highlight first unread line after any forward movement",
372128345Stjr		}
37360786Sps	},
37460786Sps	{ 'x', &x_optname,
37589019Sps		STRING|REPAINT, 0, NULL, opt_x,
376128345Stjr		{
377128345Stjr			"Tab stops: ",
378128345Stjr			"0123456789,",
379128345Stjr			NULL
380128345Stjr		}
38160786Sps	},
38260786Sps	{ 'X', &X__optname,
38360786Sps		BOOL|NO_TOGGLE, OPT_OFF, &no_init, NULL,
384128345Stjr		{
385128345Stjr			"Send init/deinit strings to terminal",
386128345Stjr			"Don't use init/deinit strings",
387128345Stjr			NULL
388128345Stjr		}
38960786Sps	},
39060786Sps	{ 'y', &y_optname,
39160786Sps		NUMBER, -1, &forw_scroll, NULL,
392128345Stjr		{
393128345Stjr			"Forward scroll limit: ",
394128345Stjr			"Forward scroll limit is %d lines",
395128345Stjr			NULL
396128345Stjr		}
39760786Sps	},
39860786Sps	{ 'z', &z_optname,
39960786Sps		NUMBER, -1, &swindow, NULL,
400128345Stjr		{
401128345Stjr			"Scroll window size: ",
402128345Stjr			"Scroll window size is %d lines",
403128345Stjr			NULL
404128345Stjr		}
40560786Sps	},
40660786Sps	{ '"', &quote_optname,
40760786Sps		STRING, 0, NULL, opt_quote,
408128345Stjr		{ "quotes: ", NULL, NULL }
40960786Sps	},
41060786Sps	{ '~', &tilde_optname,
41160786Sps		BOOL|REPAINT, OPT_ON, &twiddle, NULL,
412128345Stjr		{
413128345Stjr			"Don't show tildes after end of file",
414128345Stjr			"Show tildes after end of file",
415128345Stjr			NULL
416128345Stjr		}
41760786Sps	},
41860786Sps	{ '?', &query_optname,
41960786Sps		NOVAR, 0, NULL, opt_query,
420128345Stjr		{ NULL, NULL, NULL }
42160786Sps	},
42263128Sps	{ '#', &pound_optname,
423195941Sdelphij		STRING, 0, NULL, opt_shift,
424128345Stjr		{
425128345Stjr			"Horizontal shift: ",
426195941Sdelphij			"0123456789.",
427128345Stjr			NULL
428128345Stjr		}
42963128Sps	},
430221715Sdelphij	{ OLETTER_NONE, &keypad_optname,
43189019Sps		BOOL|NO_TOGGLE, OPT_OFF, &no_keypad, NULL,
432128345Stjr		{
433128345Stjr			"Use keypad mode",
434128345Stjr			"Don't use keypad mode",
435128345Stjr			NULL
436128345Stjr		}
43789019Sps	},
438221715Sdelphij	{ OLETTER_NONE, &oldbot_optname,
439170256Sdelphij		BOOL, OPT_OFF, &oldbot, NULL,
440170256Sdelphij		{
441170256Sdelphij			"Use new bottom of screen behavior",
442170256Sdelphij			"Use old bottom of screen behavior",
443170256Sdelphij			NULL
444170256Sdelphij		}
445170256Sdelphij	},
446221715Sdelphij	{ OLETTER_NONE, &follow_optname,
447173682Sdelphij		BOOL, FOLLOW_DESC, &follow_mode, NULL,
448173682Sdelphij		{
449221715Sdelphij			"F command follows file descriptor",
450221715Sdelphij			"F command follows file name",
451173682Sdelphij			NULL
452173682Sdelphij		}
453173682Sdelphij	},
454250592Sdelphij	{ OLETTER_NONE, &use_backslash_optname,
455250592Sdelphij		BOOL, OPT_OFF, &opt_use_backslash, NULL,
456250592Sdelphij		{
457250592Sdelphij			"Use backslash escaping in command line parameters",
458250592Sdelphij			"Don't use backslash escaping in command line parameters",
459250592Sdelphij			NULL
460250592Sdelphij		}
461250592Sdelphij	},
462330571Sdelphij	{ OLETTER_NONE, &rscroll_optname,
463330571Sdelphij		STRING|REPAINT|INIT_HANDLER, 0, NULL, opt_rscroll,
464330571Sdelphij		{ "right scroll character: ", NULL, NULL }
465330571Sdelphij	},
466128345Stjr	{ '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
46760786Sps};
46860786Sps
46960786Sps
47060786Sps/*
47160786Sps * Initialize each option to its default value.
47260786Sps */
47360786Sps	public void
47460786Spsinit_option()
47560786Sps{
476330571Sdelphij	struct loption *o;
477161475Sdelphij	char *p;
47860786Sps
479161475Sdelphij	p = lgetenv("LESS_IS_MORE");
480161475Sdelphij	if (p != NULL && *p != '\0')
481161475Sdelphij		less_is_more = 1;
482161475Sdelphij
48360786Sps	for (o = option;  o->oletter != '\0';  o++)
48460786Sps	{
48560786Sps		/*
48660786Sps		 * Set each variable to its default.
48760786Sps		 */
48860786Sps		if (o->ovar != NULL)
48960786Sps			*(o->ovar) = o->odefault;
490128345Stjr		if (o->otype & INIT_HANDLER)
491128345Stjr			(*(o->ofunc))(INIT, (char *) NULL);
49260786Sps	}
49360786Sps}
49460786Sps
49560786Sps/*
49660786Sps * Find an option in the option table, given its option letter.
49760786Sps */
498128345Stjr	public struct loption *
49960786Spsfindopt(c)
50060786Sps	int c;
50160786Sps{
502330571Sdelphij	struct loption *o;
50360786Sps
50460786Sps	for (o = option;  o->oletter != '\0';  o++)
50560786Sps	{
50660786Sps		if (o->oletter == c)
50760786Sps			return (o);
508161475Sdelphij		if ((o->otype & TRIPLE) && ASCII_TO_UPPER(o->oletter) == c)
50960786Sps			return (o);
51060786Sps	}
51160786Sps	return (NULL);
51260786Sps}
51360786Sps
51460786Sps/*
515128345Stjr *
516128345Stjr */
517128345Stjr	static int
518128345Stjris_optchar(c)
519128345Stjr	char c;
520128345Stjr{
521161475Sdelphij	if (ASCII_IS_UPPER(c))
522128345Stjr		return 1;
523161475Sdelphij	if (ASCII_IS_LOWER(c))
524128345Stjr		return 1;
525128345Stjr	if (c == '-')
526128345Stjr		return 1;
527128345Stjr	return 0;
528128345Stjr}
529128345Stjr
530128345Stjr/*
53160786Sps * Find an option in the option table, given its option name.
53260786Sps * p_optname is the (possibly partial) name to look for, and
53360786Sps * is updated to point after the matched name.
53460786Sps * p_oname if non-NULL is set to point to the full option name.
53560786Sps */
536128345Stjr	public struct loption *
53760786Spsfindopt_name(p_optname, p_oname, p_err)
53860786Sps	char **p_optname;
53960786Sps	char **p_oname;
54060786Sps	int *p_err;
54160786Sps{
54260786Sps	char *optname = *p_optname;
543330571Sdelphij	struct loption *o;
544330571Sdelphij	struct optname *oname;
545330571Sdelphij	int len;
54660786Sps	int uppercase;
547128345Stjr	struct loption *maxo = NULL;
54860786Sps	struct optname *maxoname = NULL;
54960786Sps	int maxlen = 0;
55060786Sps	int ambig = 0;
55160786Sps	int exact = 0;
55260786Sps
55360786Sps	/*
55460786Sps	 * Check all options.
55560786Sps	 */
55660786Sps	for (o = option;  o->oletter != '\0';  o++)
55760786Sps	{
55860786Sps		/*
55960786Sps		 * Check all names for this option.
56060786Sps		 */
56160786Sps		for (oname = o->onames;  oname != NULL;  oname = oname->onext)
56260786Sps		{
56360786Sps			/*
56460786Sps			 * Try normal match first (uppercase == 0),
56560786Sps			 * then, then if it's a TRIPLE option,
56660786Sps			 * try uppercase match (uppercase == 1).
56760786Sps			 */
56860786Sps			for (uppercase = 0;  uppercase <= 1;  uppercase++)
56960786Sps			{
57060786Sps				len = sprefix(optname, oname->oname, uppercase);
571128345Stjr				if (len <= 0 || is_optchar(optname[len]))
572128345Stjr				{
573128345Stjr					/*
574128345Stjr					 * We didn't use all of the option name.
575128345Stjr					 */
576128345Stjr					continue;
577128345Stjr				}
57860786Sps				if (!exact && len == maxlen)
57960786Sps					/*
58060786Sps					 * Already had a partial match,
58160786Sps					 * and now there's another one that
58260786Sps					 * matches the same length.
58360786Sps					 */
58460786Sps					ambig = 1;
58560786Sps				else if (len > maxlen)
58660786Sps				{
58760786Sps					/*
58860786Sps					 * Found a better match than
58960786Sps					 * the one we had.
59060786Sps					 */
59160786Sps					maxo = o;
59260786Sps					maxoname = oname;
59360786Sps					maxlen = len;
59460786Sps					ambig = 0;
59589019Sps					exact = (len == (int)strlen(oname->oname));
59660786Sps				}
59760786Sps				if (!(o->otype & TRIPLE))
59860786Sps					break;
59960786Sps			}
60060786Sps		}
60160786Sps	}
60260786Sps	if (ambig)
60360786Sps	{
60460786Sps		/*
60560786Sps		 * Name matched more than one option.
60660786Sps		 */
60760786Sps		if (p_err != NULL)
60860786Sps			*p_err = OPT_AMBIG;
60960786Sps		return (NULL);
61060786Sps	}
61160786Sps	*p_optname = optname + maxlen;
61260786Sps	if (p_oname != NULL)
613128345Stjr		*p_oname = maxoname == NULL ? NULL : maxoname->oname;
61460786Sps	return (maxo);
61560786Sps}
616