opttbl.c revision 240121
160786Sps/*
2240121Sdelphij * Copyright (C) 1984-2012  Mark Nudelman
360786Sps *
460786Sps * You may distribute under the terms of either the GNU General Public
560786Sps * License or the Less License, as specified in the README file.
660786Sps *
7240121Sdelphij * For more information, see the README file.
860786Sps */
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}} */
5560786Sps#if HILITE_SEARCH
5660786Spspublic int hilite_search;	/* Highlight matched search patterns? */
5760786Sps#endif
5860786Sps
59161475Sdelphijpublic int less_is_more = 0;	/* Make compatible with POSIX more */
60161475Sdelphij
6160786Sps/*
6260786Sps * Long option names.
6360786Sps */
6460786Spsstatic struct optname a_optname      = { "search-skip-screen",   NULL };
6560786Spsstatic struct optname b_optname      = { "buffers",              NULL };
6660786Spsstatic struct optname B__optname     = { "auto-buffers",         NULL };
6760786Spsstatic struct optname c_optname      = { "clear-screen",         NULL };
6860786Spsstatic struct optname d_optname      = { "dumb",                 NULL };
6960786Sps#if MSDOS_COMPILER
7060786Spsstatic struct optname D__optname     = { "color",                NULL };
7160786Sps#endif
7260786Spsstatic struct optname e_optname      = { "quit-at-eof",          NULL };
7360786Spsstatic struct optname f_optname      = { "force",                NULL };
7460786Spsstatic struct optname F__optname     = { "quit-if-one-screen",   NULL };
7560786Sps#if HILITE_SEARCH
7660786Spsstatic struct optname g_optname      = { "hilite-search",        NULL };
7760786Sps#endif
7860786Spsstatic struct optname h_optname      = { "max-back-scroll",      NULL };
7960786Spsstatic struct optname i_optname      = { "ignore-case",          NULL };
8060786Spsstatic struct optname j_optname      = { "jump-target",          NULL };
8163128Spsstatic struct optname J__optname     = { "status-column",        NULL };
8260786Sps#if USERFILE
8360786Spsstatic struct optname k_optname      = { "lesskey-file",         NULL };
8460786Sps#endif
85161475Sdelphijstatic struct optname K__optname     = { "quit-on-intr",         NULL };
86128345Stjrstatic struct optname L__optname     = { "no-lessopen",          NULL };
8760786Spsstatic struct optname m_optname      = { "long-prompt",          NULL };
8860786Spsstatic struct optname n_optname      = { "line-numbers",         NULL };
8960786Sps#if LOGFILE
9060786Spsstatic struct optname o_optname      = { "log-file",             NULL };
9160786Spsstatic struct optname O__optname     = { "LOG-FILE",             NULL };
9260786Sps#endif
9360786Spsstatic struct optname p_optname      = { "pattern",              NULL };
9460786Spsstatic struct optname P__optname     = { "prompt",               NULL };
9560786Spsstatic struct optname q2_optname     = { "silent",               NULL };
9660786Spsstatic struct optname q_optname      = { "quiet",                &q2_optname };
9760786Spsstatic struct optname r_optname      = { "raw-control-chars",    NULL };
9860786Spsstatic struct optname s_optname      = { "squeeze-blank-lines",  NULL };
9960786Spsstatic struct optname S__optname     = { "chop-long-lines",      NULL };
10060786Sps#if TAGS
10160786Spsstatic struct optname t_optname      = { "tag",                  NULL };
10260786Spsstatic struct optname T__optname     = { "tag-file",             NULL };
10360786Sps#endif
10460786Spsstatic struct optname u_optname      = { "underline-special",    NULL };
10560786Spsstatic struct optname V__optname     = { "version",              NULL };
10660786Spsstatic struct optname w_optname      = { "hilite-unread",        NULL };
10760786Spsstatic struct optname x_optname      = { "tabs",                 NULL };
10860786Spsstatic struct optname X__optname     = { "no-init",              NULL };
10960786Spsstatic struct optname y_optname      = { "max-forw-scroll",      NULL };
11060786Spsstatic struct optname z_optname      = { "window",               NULL };
11160786Spsstatic struct optname quote_optname  = { "quotes",               NULL };
11260786Spsstatic struct optname tilde_optname  = { "tilde",                NULL };
11360786Spsstatic struct optname query_optname  = { "help",                 NULL };
11463128Spsstatic struct optname pound_optname  = { "shift",                NULL };
11589019Spsstatic struct optname keypad_optname = { "no-keypad",            NULL };
116170256Sdelphijstatic struct optname oldbot_optname = { "old-bot",              NULL };
117173682Sdelphijstatic struct optname follow_optname = { "follow-name",          NULL };
11860786Sps
11960786Sps
12060786Sps/*
12160786Sps * Table of all options and their semantics.
12289019Sps *
12389019Sps * For BOOL and TRIPLE options, odesc[0], odesc[1], odesc[2] are
12489019Sps * the description of the option when set to 0, 1 or 2, respectively.
12589019Sps * For NUMBER options, odesc[0] is the prompt to use when entering
12689019Sps * a new value, and odesc[1] is the description, which should contain
12789019Sps * one %d which is replaced by the value of the number.
12889019Sps * For STRING options, odesc[0] is the prompt to use when entering
12989019Sps * a new value, and odesc[1], if not NULL, is the set of characters
13089019Sps * that are valid in the string.
13160786Sps */
132128345Stjrstatic struct loption option[] =
13360786Sps{
13460786Sps	{ 'a', &a_optname,
135221715Sdelphij		TRIPLE, OPT_ONPLUS, &how_search, NULL,
136128345Stjr		{
137128345Stjr			"Search includes displayed screen",
138128345Stjr			"Search skips displayed screen",
139221715Sdelphij			"Search includes all of displayed screen"
140128345Stjr		}
14160786Sps	},
14260786Sps
14360786Sps	{ 'b', &b_optname,
144128345Stjr		NUMBER|INIT_HANDLER, 64, &bufspace, opt_b,
145128345Stjr		{
146128345Stjr			"Max buffer space per file (K): ",
147128345Stjr			"Max buffer space per file: %dK",
148128345Stjr			NULL
149128345Stjr		}
15060786Sps	},
15160786Sps	{ 'B', &B__optname,
15260786Sps		BOOL, OPT_ON, &autobuf, NULL,
153128345Stjr		{
154128345Stjr			"Don't automatically allocate buffers",
155128345Stjr			"Automatically allocate buffers when needed",
156128345Stjr			NULL
157128345Stjr		}
15860786Sps	},
15960786Sps	{ 'c', &c_optname,
16060786Sps		TRIPLE, OPT_OFF, &top_scroll, NULL,
161128345Stjr		{
162128345Stjr			"Repaint by scrolling from bottom of screen",
163170256Sdelphij			"Repaint by painting from top of screen",
164128345Stjr			"Repaint by painting from top of screen"
165128345Stjr		}
16660786Sps	},
16760786Sps	{ 'd', &d_optname,
16860786Sps		BOOL|NO_TOGGLE, OPT_OFF, &know_dumb, NULL,
169128345Stjr		{
170128345Stjr			"Assume intelligent terminal",
171128345Stjr			"Assume dumb terminal",
172128345Stjr			NULL
173128345Stjr		}
17460786Sps	},
17560786Sps#if MSDOS_COMPILER
17660786Sps	{ 'D', &D__optname,
17760786Sps		STRING|REPAINT|NO_QUERY, 0, NULL, opt_D,
178128345Stjr		{
179128345Stjr			"color desc: ",
180128345Stjr			"Ddknsu0123456789.",
181128345Stjr			NULL
182128345Stjr		}
18360786Sps	},
18460786Sps#endif
18560786Sps	{ 'e', &e_optname,
18660786Sps		TRIPLE, OPT_OFF, &quit_at_eof, NULL,
187128345Stjr		{
188128345Stjr			"Don't quit at end-of-file",
189128345Stjr			"Quit at end-of-file",
190128345Stjr			"Quit immediately at end-of-file"
191128345Stjr		}
19260786Sps	},
19360786Sps	{ 'f', &f_optname,
19460786Sps		BOOL, OPT_OFF, &force_open, NULL,
195128345Stjr		{
196128345Stjr			"Open only regular files",
197128345Stjr			"Open even non-regular files",
198128345Stjr			NULL
199128345Stjr		}
20060786Sps	},
20160786Sps	{ 'F', &F__optname,
20260786Sps		BOOL, OPT_OFF, &quit_if_one_screen, NULL,
203128345Stjr		{
204128345Stjr			"Don't quit if end-of-file on first screen",
205128345Stjr			"Quit if end-of-file on first screen",
206128345Stjr			NULL
207128345Stjr		}
20860786Sps	},
20960786Sps#if HILITE_SEARCH
21060786Sps	{ 'g', &g_optname,
21160786Sps		TRIPLE|HL_REPAINT, OPT_ONPLUS, &hilite_search, NULL,
212128345Stjr		{
213128345Stjr			"Don't highlight search matches",
214128345Stjr			"Highlight matches for previous search only",
215128345Stjr			"Highlight all matches for previous search pattern",
216128345Stjr		}
21760786Sps	},
21860786Sps#endif
21960786Sps	{ 'h', &h_optname,
22060786Sps		NUMBER, -1, &back_scroll, NULL,
221128345Stjr		{
222128345Stjr			"Backwards scroll limit: ",
223128345Stjr			"Backwards scroll limit is %d lines",
224128345Stjr			NULL
225128345Stjr		}
22660786Sps	},
22760786Sps	{ 'i', &i_optname,
22860786Sps		TRIPLE|HL_REPAINT, OPT_OFF, &caseless, opt_i,
229128345Stjr		{
230128345Stjr			"Case is significant in searches",
231128345Stjr			"Ignore case in searches",
232128345Stjr			"Ignore case in searches and in patterns"
233128345Stjr		}
23460786Sps	},
23560786Sps	{ 'j', &j_optname,
236170256Sdelphij		STRING, 0, NULL, opt_j,
237128345Stjr		{
238128345Stjr			"Target line: ",
239221715Sdelphij			"0123456789.-",
240128345Stjr			NULL
241128345Stjr		}
24260786Sps	},
24363128Sps	{ 'J', &J__optname,
24463128Sps		BOOL|REPAINT, OPT_OFF, &status_col, NULL,
245128345Stjr		{
246128345Stjr			"Don't display a status column",
247128345Stjr			"Display a status column",
248128345Stjr			NULL
249128345Stjr		}
25063128Sps	},
25160786Sps#if USERFILE
25260786Sps	{ 'k', &k_optname,
25360786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k,
254128345Stjr		{ NULL, NULL, NULL }
25560786Sps	},
25660786Sps#endif
257161475Sdelphij	{ 'K', &K__optname,
258161475Sdelphij		BOOL, OPT_OFF, &quit_on_intr, NULL,
259161475Sdelphij		{
260161475Sdelphij			"Interrupt (ctrl-C) returns to prompt",
261161475Sdelphij			"Interrupt (ctrl-C) exits less",
262161475Sdelphij			NULL
263161475Sdelphij		}
264161475Sdelphij	},
265128345Stjr	{ 'L', &L__optname,
266128345Stjr		BOOL, OPT_ON, &use_lessopen, NULL,
267128345Stjr		{
268128345Stjr			"Don't use the LESSOPEN filter",
269128345Stjr			"Use the LESSOPEN filter",
270128345Stjr			NULL
271128345Stjr		}
272128345Stjr	},
27360786Sps	{ 'm', &m_optname,
27460786Sps		TRIPLE, OPT_OFF, &pr_type, NULL,
275128345Stjr		{
276128345Stjr			"Short prompt",
277128345Stjr			"Medium prompt",
278128345Stjr			"Long prompt"
279128345Stjr		}
28060786Sps	},
28160786Sps	{ 'n', &n_optname,
28260786Sps		TRIPLE|REPAINT, OPT_ON, &linenums, NULL,
283128345Stjr		{
284128345Stjr			"Don't use line numbers",
285128345Stjr			"Use line numbers",
286128345Stjr			"Constantly display line numbers"
287128345Stjr		}
28860786Sps	},
28960786Sps#if LOGFILE
29060786Sps	{ 'o', &o_optname,
29160786Sps		STRING, 0, NULL, opt_o,
292128345Stjr		{ "log file: ", NULL, NULL }
29360786Sps	},
29460786Sps	{ 'O', &O__optname,
29560786Sps		STRING, 0, NULL, opt__O,
296128345Stjr		{ "Log file: ", NULL, NULL }
29760786Sps	},
29860786Sps#endif
29960786Sps	{ 'p', &p_optname,
30060786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_p,
301128345Stjr		{ NULL, NULL, NULL }
30260786Sps	},
30360786Sps	{ 'P', &P__optname,
30460786Sps		STRING, 0, NULL, opt__P,
305128345Stjr		{ "prompt: ", NULL, NULL }
30660786Sps	},
30760786Sps	{ 'q', &q_optname,
30860786Sps		TRIPLE, OPT_OFF, &quiet, NULL,
309128345Stjr		{
310128345Stjr			"Ring the bell for errors AND at eof/bof",
311128345Stjr			"Ring the bell for errors but not at eof/bof",
312128345Stjr			"Never ring the bell"
313128345Stjr		}
31460786Sps	},
31560786Sps	{ 'r', &r_optname,
31660786Sps		TRIPLE|REPAINT, OPT_OFF, &ctldisp, NULL,
317128345Stjr		{
318128345Stjr			"Display control characters as ^X",
319128345Stjr			"Display control characters directly",
320128345Stjr			"Display control characters directly, processing ANSI sequences"
321128345Stjr		}
32260786Sps	},
32360786Sps	{ 's', &s_optname,
32460786Sps		BOOL|REPAINT, OPT_OFF, &squeeze, NULL,
325128345Stjr		{
326128345Stjr			"Display all blank lines",
327128345Stjr			"Squeeze multiple blank lines",
328128345Stjr			NULL
329128345Stjr		}
33060786Sps	},
33160786Sps	{ 'S', &S__optname,
33260786Sps		BOOL|REPAINT, OPT_OFF, &chopline, NULL,
333128345Stjr		{
334128345Stjr			"Fold long lines",
335128345Stjr			"Chop long lines",
336128345Stjr			NULL
337128345Stjr		}
33860786Sps	},
33960786Sps#if TAGS
34060786Sps	{ 't', &t_optname,
34160786Sps		STRING|NO_QUERY, 0, NULL, opt_t,
342128345Stjr		{ "tag: ", NULL, NULL }
34360786Sps	},
34460786Sps	{ 'T', &T__optname,
34560786Sps		STRING, 0, NULL, opt__T,
346128345Stjr		{ "tags file: ", NULL, NULL }
34760786Sps	},
34860786Sps#endif
34960786Sps	{ 'u', &u_optname,
35060786Sps		TRIPLE|REPAINT, OPT_OFF, &bs_mode, NULL,
351128345Stjr		{
352128345Stjr			"Display underlined text in underline mode",
353128345Stjr			"Backspaces cause overstrike",
354128345Stjr			"Print backspace as ^H"
355128345Stjr		}
35660786Sps	},
35760786Sps	{ 'V', &V__optname,
35860786Sps		NOVAR, 0, NULL, opt__V,
359128345Stjr		{ NULL, NULL, NULL }
36060786Sps	},
36160786Sps	{ 'w', &w_optname,
36260786Sps		TRIPLE|REPAINT, OPT_OFF, &show_attn, NULL,
363128345Stjr		{
364128345Stjr			"Don't highlight first unread line",
365128345Stjr			"Highlight first unread line after forward-screen",
366128345Stjr			"Highlight first unread line after any forward movement",
367128345Stjr		}
36860786Sps	},
36960786Sps	{ 'x', &x_optname,
37089019Sps		STRING|REPAINT, 0, NULL, opt_x,
371128345Stjr		{
372128345Stjr			"Tab stops: ",
373128345Stjr			"0123456789,",
374128345Stjr			NULL
375128345Stjr		}
37660786Sps	},
37760786Sps	{ 'X', &X__optname,
37860786Sps		BOOL|NO_TOGGLE, OPT_OFF, &no_init, NULL,
379128345Stjr		{
380128345Stjr			"Send init/deinit strings to terminal",
381128345Stjr			"Don't use init/deinit strings",
382128345Stjr			NULL
383128345Stjr		}
38460786Sps	},
38560786Sps	{ 'y', &y_optname,
38660786Sps		NUMBER, -1, &forw_scroll, NULL,
387128345Stjr		{
388128345Stjr			"Forward scroll limit: ",
389128345Stjr			"Forward scroll limit is %d lines",
390128345Stjr			NULL
391128345Stjr		}
39260786Sps	},
39360786Sps	{ 'z', &z_optname,
39460786Sps		NUMBER, -1, &swindow, NULL,
395128345Stjr		{
396128345Stjr			"Scroll window size: ",
397128345Stjr			"Scroll window size is %d lines",
398128345Stjr			NULL
399128345Stjr		}
40060786Sps	},
40160786Sps	{ '"', &quote_optname,
40260786Sps		STRING, 0, NULL, opt_quote,
403128345Stjr		{ "quotes: ", NULL, NULL }
40460786Sps	},
40560786Sps	{ '~', &tilde_optname,
40660786Sps		BOOL|REPAINT, OPT_ON, &twiddle, NULL,
407128345Stjr		{
408128345Stjr			"Don't show tildes after end of file",
409128345Stjr			"Show tildes after end of file",
410128345Stjr			NULL
411128345Stjr		}
41260786Sps	},
41360786Sps	{ '?', &query_optname,
41460786Sps		NOVAR, 0, NULL, opt_query,
415128345Stjr		{ NULL, NULL, NULL }
41660786Sps	},
41763128Sps	{ '#', &pound_optname,
418195941Sdelphij		STRING, 0, NULL, opt_shift,
419128345Stjr		{
420128345Stjr			"Horizontal shift: ",
421195941Sdelphij			"0123456789.",
422128345Stjr			NULL
423128345Stjr		}
42463128Sps	},
425221715Sdelphij	{ OLETTER_NONE, &keypad_optname,
42689019Sps		BOOL|NO_TOGGLE, OPT_OFF, &no_keypad, NULL,
427128345Stjr		{
428128345Stjr			"Use keypad mode",
429128345Stjr			"Don't use keypad mode",
430128345Stjr			NULL
431128345Stjr		}
43289019Sps	},
433221715Sdelphij	{ OLETTER_NONE, &oldbot_optname,
434170256Sdelphij		BOOL, OPT_OFF, &oldbot, NULL,
435170256Sdelphij		{
436170256Sdelphij			"Use new bottom of screen behavior",
437170256Sdelphij			"Use old bottom of screen behavior",
438170256Sdelphij			NULL
439170256Sdelphij		}
440170256Sdelphij	},
441221715Sdelphij	{ OLETTER_NONE, &follow_optname,
442173682Sdelphij		BOOL, FOLLOW_DESC, &follow_mode, NULL,
443173682Sdelphij		{
444221715Sdelphij			"F command follows file descriptor",
445221715Sdelphij			"F command follows file name",
446173682Sdelphij			NULL
447173682Sdelphij		}
448173682Sdelphij	},
449128345Stjr	{ '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
45060786Sps};
45160786Sps
45260786Sps
45360786Sps/*
45460786Sps * Initialize each option to its default value.
45560786Sps */
45660786Sps	public void
45760786Spsinit_option()
45860786Sps{
459128345Stjr	register struct loption *o;
460161475Sdelphij	char *p;
46160786Sps
462161475Sdelphij	p = lgetenv("LESS_IS_MORE");
463161475Sdelphij	if (p != NULL && *p != '\0')
464161475Sdelphij		less_is_more = 1;
465161475Sdelphij
46660786Sps	for (o = option;  o->oletter != '\0';  o++)
46760786Sps	{
46860786Sps		/*
46960786Sps		 * Set each variable to its default.
47060786Sps		 */
47160786Sps		if (o->ovar != NULL)
47260786Sps			*(o->ovar) = o->odefault;
473128345Stjr		if (o->otype & INIT_HANDLER)
474128345Stjr			(*(o->ofunc))(INIT, (char *) NULL);
47560786Sps	}
47660786Sps}
47760786Sps
47860786Sps/*
47960786Sps * Find an option in the option table, given its option letter.
48060786Sps */
481128345Stjr	public struct loption *
48260786Spsfindopt(c)
48360786Sps	int c;
48460786Sps{
485128345Stjr	register struct loption *o;
48660786Sps
48760786Sps	for (o = option;  o->oletter != '\0';  o++)
48860786Sps	{
48960786Sps		if (o->oletter == c)
49060786Sps			return (o);
491161475Sdelphij		if ((o->otype & TRIPLE) && ASCII_TO_UPPER(o->oletter) == c)
49260786Sps			return (o);
49360786Sps	}
49460786Sps	return (NULL);
49560786Sps}
49660786Sps
49760786Sps/*
498128345Stjr *
499128345Stjr */
500128345Stjr	static int
501128345Stjris_optchar(c)
502128345Stjr	char c;
503128345Stjr{
504161475Sdelphij	if (ASCII_IS_UPPER(c))
505128345Stjr		return 1;
506161475Sdelphij	if (ASCII_IS_LOWER(c))
507128345Stjr		return 1;
508128345Stjr	if (c == '-')
509128345Stjr		return 1;
510128345Stjr	return 0;
511128345Stjr}
512128345Stjr
513128345Stjr/*
51460786Sps * Find an option in the option table, given its option name.
51560786Sps * p_optname is the (possibly partial) name to look for, and
51660786Sps * is updated to point after the matched name.
51760786Sps * p_oname if non-NULL is set to point to the full option name.
51860786Sps */
519128345Stjr	public struct loption *
52060786Spsfindopt_name(p_optname, p_oname, p_err)
52160786Sps	char **p_optname;
52260786Sps	char **p_oname;
52360786Sps	int *p_err;
52460786Sps{
52560786Sps	char *optname = *p_optname;
526128345Stjr	register struct loption *o;
52760786Sps	register struct optname *oname;
52860786Sps	register int len;
52960786Sps	int uppercase;
530128345Stjr	struct loption *maxo = NULL;
53160786Sps	struct optname *maxoname = NULL;
53260786Sps	int maxlen = 0;
53360786Sps	int ambig = 0;
53460786Sps	int exact = 0;
53560786Sps
53660786Sps	/*
53760786Sps	 * Check all options.
53860786Sps	 */
53960786Sps	for (o = option;  o->oletter != '\0';  o++)
54060786Sps	{
54160786Sps		/*
54260786Sps		 * Check all names for this option.
54360786Sps		 */
54460786Sps		for (oname = o->onames;  oname != NULL;  oname = oname->onext)
54560786Sps		{
54660786Sps			/*
54760786Sps			 * Try normal match first (uppercase == 0),
54860786Sps			 * then, then if it's a TRIPLE option,
54960786Sps			 * try uppercase match (uppercase == 1).
55060786Sps			 */
55160786Sps			for (uppercase = 0;  uppercase <= 1;  uppercase++)
55260786Sps			{
55360786Sps				len = sprefix(optname, oname->oname, uppercase);
554128345Stjr				if (len <= 0 || is_optchar(optname[len]))
555128345Stjr				{
556128345Stjr					/*
557128345Stjr					 * We didn't use all of the option name.
558128345Stjr					 */
559128345Stjr					continue;
560128345Stjr				}
56160786Sps				if (!exact && len == maxlen)
56260786Sps					/*
56360786Sps					 * Already had a partial match,
56460786Sps					 * and now there's another one that
56560786Sps					 * matches the same length.
56660786Sps					 */
56760786Sps					ambig = 1;
56860786Sps				else if (len > maxlen)
56960786Sps				{
57060786Sps					/*
57160786Sps					 * Found a better match than
57260786Sps					 * the one we had.
57360786Sps					 */
57460786Sps					maxo = o;
57560786Sps					maxoname = oname;
57660786Sps					maxlen = len;
57760786Sps					ambig = 0;
57889019Sps					exact = (len == (int)strlen(oname->oname));
57960786Sps				}
58060786Sps				if (!(o->otype & TRIPLE))
58160786Sps					break;
58260786Sps			}
58360786Sps		}
58460786Sps	}
58560786Sps	if (ambig)
58660786Sps	{
58760786Sps		/*
58860786Sps		 * Name matched more than one option.
58960786Sps		 */
59060786Sps		if (p_err != NULL)
59160786Sps			*p_err = OPT_AMBIG;
59260786Sps		return (NULL);
59360786Sps	}
59460786Sps	*p_optname = optname + maxlen;
59560786Sps	if (p_oname != NULL)
596128345Stjr		*p_oname = maxoname == NULL ? NULL : maxoname->oname;
59760786Sps	return (maxo);
59860786Sps}
599