opttbl.c revision 173682
160786Sps/*
2170256Sdelphij * Copyright (C) 1984-2007  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 *
760786Sps * For more information about less, or for information on how to
860786Sps * contact the author, see the README file.
960786Sps */
1060786Sps
1160786Sps
1260786Sps/*
1360786Sps * The option table.
1460786Sps */
1560786Sps
1660786Sps#include "less.h"
1760786Sps#include "option.h"
1860786Sps
1960786Sps/*
2060786Sps * Variables controlled by command line options.
2160786Sps */
2260786Spspublic int quiet;		/* Should we suppress the audible bell? */
2360786Spspublic int how_search;		/* Where should forward searches start? */
2460786Spspublic int top_scroll;		/* Repaint screen from top?
2560786Sps				   (alternative is scroll from bottom) */
2660786Spspublic int pr_type;		/* Type of prompt (short, medium, long) */
2760786Spspublic int bs_mode;		/* How to process backspaces */
2860786Spspublic int know_dumb;		/* Don't complain about dumb terminals */
2960786Spspublic int quit_at_eof;		/* Quit after hitting end of file twice */
3060786Spspublic int quit_if_one_screen;	/* Quit if EOF on first screen */
3160786Spspublic int squeeze;		/* Squeeze multiple blank lines into one */
3260786Spspublic int tabstop;		/* Tab settings */
3360786Spspublic int back_scroll;		/* Repaint screen on backwards movement */
3460786Spspublic int forw_scroll;		/* Repaint screen on forward movement */
3560786Spspublic int caseless;		/* Do "caseless" searches */
3660786Spspublic int linenums;		/* Use line numbers */
3760786Spspublic int autobuf;		/* Automatically allocate buffers as needed */
38128345Stjrpublic int bufspace;		/* Max buffer space per file (K) */
3960786Spspublic int ctldisp;		/* Send control chars to screen untranslated */
4060786Spspublic int force_open;		/* Open the file even if not regular file */
4160786Spspublic int swindow;		/* Size of scrolling window */
4260786Spspublic int jump_sline;		/* Screen line of "jump target" */
43170256Sdelphijpublic long jump_sline_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,
13560786Sps		BOOL, OPT_OFF, &how_search, NULL,
136128345Stjr		{
137128345Stjr			"Search includes displayed screen",
138128345Stjr			"Search skips displayed screen",
139128345Stjr			NULL
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: ",
239170256Sdelphij			"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	},
26560786Sps	{ 'l', NULL,
26660786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_l,
267128345Stjr		{ NULL, NULL, NULL }
26860786Sps	},
269128345Stjr	{ 'L', &L__optname,
270128345Stjr		BOOL, OPT_ON, &use_lessopen, NULL,
271128345Stjr		{
272128345Stjr			"Don't use the LESSOPEN filter",
273128345Stjr			"Use the LESSOPEN filter",
274128345Stjr			NULL
275128345Stjr		}
276128345Stjr	},
27760786Sps	{ 'm', &m_optname,
27860786Sps		TRIPLE, OPT_OFF, &pr_type, NULL,
279128345Stjr		{
280128345Stjr			"Short prompt",
281128345Stjr			"Medium prompt",
282128345Stjr			"Long prompt"
283128345Stjr		}
28460786Sps	},
28560786Sps	{ 'n', &n_optname,
28660786Sps		TRIPLE|REPAINT, OPT_ON, &linenums, NULL,
287128345Stjr		{
288128345Stjr			"Don't use line numbers",
289128345Stjr			"Use line numbers",
290128345Stjr			"Constantly display line numbers"
291128345Stjr		}
29260786Sps	},
29360786Sps#if LOGFILE
29460786Sps	{ 'o', &o_optname,
29560786Sps		STRING, 0, NULL, opt_o,
296128345Stjr		{ "log file: ", NULL, NULL }
29760786Sps	},
29860786Sps	{ 'O', &O__optname,
29960786Sps		STRING, 0, NULL, opt__O,
300128345Stjr		{ "Log file: ", NULL, NULL }
30160786Sps	},
30260786Sps#endif
30360786Sps	{ 'p', &p_optname,
30460786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_p,
305128345Stjr		{ NULL, NULL, NULL }
30660786Sps	},
30760786Sps	{ 'P', &P__optname,
30860786Sps		STRING, 0, NULL, opt__P,
309128345Stjr		{ "prompt: ", NULL, NULL }
31060786Sps	},
31160786Sps	{ 'q', &q_optname,
31260786Sps		TRIPLE, OPT_OFF, &quiet, NULL,
313128345Stjr		{
314128345Stjr			"Ring the bell for errors AND at eof/bof",
315128345Stjr			"Ring the bell for errors but not at eof/bof",
316128345Stjr			"Never ring the bell"
317128345Stjr		}
31860786Sps	},
31960786Sps	{ 'r', &r_optname,
32060786Sps		TRIPLE|REPAINT, OPT_OFF, &ctldisp, NULL,
321128345Stjr		{
322128345Stjr			"Display control characters as ^X",
323128345Stjr			"Display control characters directly",
324128345Stjr			"Display control characters directly, processing ANSI sequences"
325128345Stjr		}
32660786Sps	},
32760786Sps	{ 's', &s_optname,
32860786Sps		BOOL|REPAINT, OPT_OFF, &squeeze, NULL,
329128345Stjr		{
330128345Stjr			"Display all blank lines",
331128345Stjr			"Squeeze multiple blank lines",
332128345Stjr			NULL
333128345Stjr		}
33460786Sps	},
33560786Sps	{ 'S', &S__optname,
33660786Sps		BOOL|REPAINT, OPT_OFF, &chopline, NULL,
337128345Stjr		{
338128345Stjr			"Fold long lines",
339128345Stjr			"Chop long lines",
340128345Stjr			NULL
341128345Stjr		}
34260786Sps	},
34360786Sps#if TAGS
34460786Sps	{ 't', &t_optname,
34560786Sps		STRING|NO_QUERY, 0, NULL, opt_t,
346128345Stjr		{ "tag: ", NULL, NULL }
34760786Sps	},
34860786Sps	{ 'T', &T__optname,
34960786Sps		STRING, 0, NULL, opt__T,
350128345Stjr		{ "tags file: ", NULL, NULL }
35160786Sps	},
35260786Sps#endif
35360786Sps	{ 'u', &u_optname,
35460786Sps		TRIPLE|REPAINT, OPT_OFF, &bs_mode, NULL,
355128345Stjr		{
356128345Stjr			"Display underlined text in underline mode",
357128345Stjr			"Backspaces cause overstrike",
358128345Stjr			"Print backspace as ^H"
359128345Stjr		}
36060786Sps	},
36160786Sps	{ 'V', &V__optname,
36260786Sps		NOVAR, 0, NULL, opt__V,
363128345Stjr		{ NULL, NULL, NULL }
36460786Sps	},
36560786Sps	{ 'w', &w_optname,
36660786Sps		TRIPLE|REPAINT, OPT_OFF, &show_attn, NULL,
367128345Stjr		{
368128345Stjr			"Don't highlight first unread line",
369128345Stjr			"Highlight first unread line after forward-screen",
370128345Stjr			"Highlight first unread line after any forward movement",
371128345Stjr		}
37260786Sps	},
37360786Sps	{ 'x', &x_optname,
37489019Sps		STRING|REPAINT, 0, NULL, opt_x,
375128345Stjr		{
376128345Stjr			"Tab stops: ",
377128345Stjr			"0123456789,",
378128345Stjr			NULL
379128345Stjr		}
38060786Sps	},
38160786Sps	{ 'X', &X__optname,
38260786Sps		BOOL|NO_TOGGLE, OPT_OFF, &no_init, NULL,
383128345Stjr		{
384128345Stjr			"Send init/deinit strings to terminal",
385128345Stjr			"Don't use init/deinit strings",
386128345Stjr			NULL
387128345Stjr		}
38860786Sps	},
38960786Sps	{ 'y', &y_optname,
39060786Sps		NUMBER, -1, &forw_scroll, NULL,
391128345Stjr		{
392128345Stjr			"Forward scroll limit: ",
393128345Stjr			"Forward scroll limit is %d lines",
394128345Stjr			NULL
395128345Stjr		}
39660786Sps	},
39760786Sps	{ 'z', &z_optname,
39860786Sps		NUMBER, -1, &swindow, NULL,
399128345Stjr		{
400128345Stjr			"Scroll window size: ",
401128345Stjr			"Scroll window size is %d lines",
402128345Stjr			NULL
403128345Stjr		}
40460786Sps	},
40560786Sps	{ '"', &quote_optname,
40660786Sps		STRING, 0, NULL, opt_quote,
407128345Stjr		{ "quotes: ", NULL, NULL }
40860786Sps	},
40960786Sps	{ '~', &tilde_optname,
41060786Sps		BOOL|REPAINT, OPT_ON, &twiddle, NULL,
411128345Stjr		{
412128345Stjr			"Don't show tildes after end of file",
413128345Stjr			"Show tildes after end of file",
414128345Stjr			NULL
415128345Stjr		}
41660786Sps	},
41760786Sps	{ '?', &query_optname,
41860786Sps		NOVAR, 0, NULL, opt_query,
419128345Stjr		{ NULL, NULL, NULL }
42060786Sps	},
42163128Sps	{ '#', &pound_optname,
42263128Sps		NUMBER, 0, &shift_count, NULL,
423128345Stjr		{
424128345Stjr			"Horizontal shift: ",
425128345Stjr			"Horizontal shift %d positions",
426128345Stjr			NULL
427128345Stjr		}
42863128Sps	},
42989019Sps	{ '.', &keypad_optname,
43089019Sps		BOOL|NO_TOGGLE, OPT_OFF, &no_keypad, NULL,
431128345Stjr		{
432128345Stjr			"Use keypad mode",
433128345Stjr			"Don't use keypad mode",
434128345Stjr			NULL
435128345Stjr		}
43689019Sps	},
437170256Sdelphij	{ '.', &oldbot_optname,
438170256Sdelphij		BOOL, OPT_OFF, &oldbot, NULL,
439170256Sdelphij		{
440170256Sdelphij			"Use new bottom of screen behavior",
441170256Sdelphij			"Use old bottom of screen behavior",
442170256Sdelphij			NULL
443170256Sdelphij		}
444170256Sdelphij	},
445173682Sdelphij	{ '.', &follow_optname,
446173682Sdelphij		BOOL, FOLLOW_DESC, &follow_mode, NULL,
447173682Sdelphij		{
448173682Sdelphij			"F command Follows file descriptor",
449173682Sdelphij			"F command Follows file name",
450173682Sdelphij			NULL
451173682Sdelphij		}
452173682Sdelphij	},
453128345Stjr	{ '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
45460786Sps};
45560786Sps
45660786Sps
45760786Sps/*
45860786Sps * Initialize each option to its default value.
45960786Sps */
46060786Sps	public void
46160786Spsinit_option()
46260786Sps{
463128345Stjr	register struct loption *o;
464161475Sdelphij	char *p;
46560786Sps
466161475Sdelphij	p = lgetenv("LESS_IS_MORE");
467161475Sdelphij	if (p != NULL && *p != '\0')
468161475Sdelphij		less_is_more = 1;
469161475Sdelphij
47060786Sps	for (o = option;  o->oletter != '\0';  o++)
47160786Sps	{
47260786Sps		/*
47360786Sps		 * Set each variable to its default.
47460786Sps		 */
47560786Sps		if (o->ovar != NULL)
47660786Sps			*(o->ovar) = o->odefault;
477128345Stjr		if (o->otype & INIT_HANDLER)
478128345Stjr			(*(o->ofunc))(INIT, (char *) NULL);
47960786Sps	}
48060786Sps}
48160786Sps
48260786Sps/*
48360786Sps * Find an option in the option table, given its option letter.
48460786Sps */
485128345Stjr	public struct loption *
48660786Spsfindopt(c)
48760786Sps	int c;
48860786Sps{
489128345Stjr	register struct loption *o;
49060786Sps
49160786Sps	for (o = option;  o->oletter != '\0';  o++)
49260786Sps	{
49360786Sps		if (o->oletter == c)
49460786Sps			return (o);
495161475Sdelphij		if ((o->otype & TRIPLE) && ASCII_TO_UPPER(o->oletter) == c)
49660786Sps			return (o);
49760786Sps	}
49860786Sps	return (NULL);
49960786Sps}
50060786Sps
50160786Sps/*
502128345Stjr *
503128345Stjr */
504128345Stjr	static int
505128345Stjris_optchar(c)
506128345Stjr	char c;
507128345Stjr{
508161475Sdelphij	if (ASCII_IS_UPPER(c))
509128345Stjr		return 1;
510161475Sdelphij	if (ASCII_IS_LOWER(c))
511128345Stjr		return 1;
512128345Stjr	if (c == '-')
513128345Stjr		return 1;
514128345Stjr	return 0;
515128345Stjr}
516128345Stjr
517128345Stjr/*
51860786Sps * Find an option in the option table, given its option name.
51960786Sps * p_optname is the (possibly partial) name to look for, and
52060786Sps * is updated to point after the matched name.
52160786Sps * p_oname if non-NULL is set to point to the full option name.
52260786Sps */
523128345Stjr	public struct loption *
52460786Spsfindopt_name(p_optname, p_oname, p_err)
52560786Sps	char **p_optname;
52660786Sps	char **p_oname;
52760786Sps	int *p_err;
52860786Sps{
52960786Sps	char *optname = *p_optname;
530128345Stjr	register struct loption *o;
53160786Sps	register struct optname *oname;
53260786Sps	register int len;
53360786Sps	int uppercase;
534128345Stjr	struct loption *maxo = NULL;
53560786Sps	struct optname *maxoname = NULL;
53660786Sps	int maxlen = 0;
53760786Sps	int ambig = 0;
53860786Sps	int exact = 0;
53960786Sps
54060786Sps	/*
54160786Sps	 * Check all options.
54260786Sps	 */
54360786Sps	for (o = option;  o->oletter != '\0';  o++)
54460786Sps	{
54560786Sps		/*
54660786Sps		 * Check all names for this option.
54760786Sps		 */
54860786Sps		for (oname = o->onames;  oname != NULL;  oname = oname->onext)
54960786Sps		{
55060786Sps			/*
55160786Sps			 * Try normal match first (uppercase == 0),
55260786Sps			 * then, then if it's a TRIPLE option,
55360786Sps			 * try uppercase match (uppercase == 1).
55460786Sps			 */
55560786Sps			for (uppercase = 0;  uppercase <= 1;  uppercase++)
55660786Sps			{
55760786Sps				len = sprefix(optname, oname->oname, uppercase);
558128345Stjr				if (len <= 0 || is_optchar(optname[len]))
559128345Stjr				{
560128345Stjr					/*
561128345Stjr					 * We didn't use all of the option name.
562128345Stjr					 */
563128345Stjr					continue;
564128345Stjr				}
56560786Sps				if (!exact && len == maxlen)
56660786Sps					/*
56760786Sps					 * Already had a partial match,
56860786Sps					 * and now there's another one that
56960786Sps					 * matches the same length.
57060786Sps					 */
57160786Sps					ambig = 1;
57260786Sps				else if (len > maxlen)
57360786Sps				{
57460786Sps					/*
57560786Sps					 * Found a better match than
57660786Sps					 * the one we had.
57760786Sps					 */
57860786Sps					maxo = o;
57960786Sps					maxoname = oname;
58060786Sps					maxlen = len;
58160786Sps					ambig = 0;
58289019Sps					exact = (len == (int)strlen(oname->oname));
58360786Sps				}
58460786Sps				if (!(o->otype & TRIPLE))
58560786Sps					break;
58660786Sps			}
58760786Sps		}
58860786Sps	}
58960786Sps	if (ambig)
59060786Sps	{
59160786Sps		/*
59260786Sps		 * Name matched more than one option.
59360786Sps		 */
59460786Sps		if (p_err != NULL)
59560786Sps			*p_err = OPT_AMBIG;
59660786Sps		return (NULL);
59760786Sps	}
59860786Sps	*p_optname = optname + maxlen;
59960786Sps	if (p_oname != NULL)
600128345Stjr		*p_oname = maxoname == NULL ? NULL : maxoname->oname;
60160786Sps	return (maxo);
60260786Sps}
603