opttbl.c revision 221715
160786Sps/*
2221715Sdelphij * Copyright (C) 1984-2011  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;
44195941Sdelphijpublic long shift_count_fraction = -1;
4560786Spspublic int chopline;		/* Truncate displayed lines at screen width */
4660786Spspublic int no_init;		/* Disable sending ti/te termcap strings */
4789019Spspublic int no_keypad;		/* Disable sending ks/ke termcap strings */
4860786Spspublic int twiddle;             /* Show tildes after EOF */
4960786Spspublic int show_attn;		/* Hilite first unread line */
5063128Spspublic int shift_count;		/* Number of positions to shift horizontally */
5163128Spspublic int status_col;		/* Display a status column */
52128345Stjrpublic int use_lessopen;	/* Use the LESSOPEN filter */
53161475Sdelphijpublic int quit_on_intr;	/* Quit on interrupt */
54173682Sdelphijpublic int follow_mode;		/* F cmd Follows file desc or file name? */
55173682Sdelphijpublic int oldbot;		/* Old bottom of screen behavior {{REMOVE}} */
5660786Sps#if HILITE_SEARCH
5760786Spspublic int hilite_search;	/* Highlight matched search patterns? */
5860786Sps#endif
5960786Sps
60161475Sdelphijpublic int less_is_more = 0;	/* Make compatible with POSIX more */
61161475Sdelphij
6260786Sps/*
6360786Sps * Long option names.
6460786Sps */
6560786Spsstatic struct optname a_optname      = { "search-skip-screen",   NULL };
6660786Spsstatic struct optname b_optname      = { "buffers",              NULL };
6760786Spsstatic struct optname B__optname     = { "auto-buffers",         NULL };
6860786Spsstatic struct optname c_optname      = { "clear-screen",         NULL };
6960786Spsstatic struct optname d_optname      = { "dumb",                 NULL };
7060786Sps#if MSDOS_COMPILER
7160786Spsstatic struct optname D__optname     = { "color",                NULL };
7260786Sps#endif
7360786Spsstatic struct optname e_optname      = { "quit-at-eof",          NULL };
7460786Spsstatic struct optname f_optname      = { "force",                NULL };
7560786Spsstatic struct optname F__optname     = { "quit-if-one-screen",   NULL };
7660786Sps#if HILITE_SEARCH
7760786Spsstatic struct optname g_optname      = { "hilite-search",        NULL };
7860786Sps#endif
7960786Spsstatic struct optname h_optname      = { "max-back-scroll",      NULL };
8060786Spsstatic struct optname i_optname      = { "ignore-case",          NULL };
8160786Spsstatic struct optname j_optname      = { "jump-target",          NULL };
8263128Spsstatic struct optname J__optname     = { "status-column",        NULL };
8360786Sps#if USERFILE
8460786Spsstatic struct optname k_optname      = { "lesskey-file",         NULL };
8560786Sps#endif
86161475Sdelphijstatic struct optname K__optname     = { "quit-on-intr",         NULL };
87128345Stjrstatic struct optname L__optname     = { "no-lessopen",          NULL };
8860786Spsstatic struct optname m_optname      = { "long-prompt",          NULL };
8960786Spsstatic struct optname n_optname      = { "line-numbers",         NULL };
9060786Sps#if LOGFILE
9160786Spsstatic struct optname o_optname      = { "log-file",             NULL };
9260786Spsstatic struct optname O__optname     = { "LOG-FILE",             NULL };
9360786Sps#endif
9460786Spsstatic struct optname p_optname      = { "pattern",              NULL };
9560786Spsstatic struct optname P__optname     = { "prompt",               NULL };
9660786Spsstatic struct optname q2_optname     = { "silent",               NULL };
9760786Spsstatic struct optname q_optname      = { "quiet",                &q2_optname };
9860786Spsstatic struct optname r_optname      = { "raw-control-chars",    NULL };
9960786Spsstatic struct optname s_optname      = { "squeeze-blank-lines",  NULL };
10060786Spsstatic struct optname S__optname     = { "chop-long-lines",      NULL };
10160786Sps#if TAGS
10260786Spsstatic struct optname t_optname      = { "tag",                  NULL };
10360786Spsstatic struct optname T__optname     = { "tag-file",             NULL };
10460786Sps#endif
10560786Spsstatic struct optname u_optname      = { "underline-special",    NULL };
10660786Spsstatic struct optname V__optname     = { "version",              NULL };
10760786Spsstatic struct optname w_optname      = { "hilite-unread",        NULL };
10860786Spsstatic struct optname x_optname      = { "tabs",                 NULL };
10960786Spsstatic struct optname X__optname     = { "no-init",              NULL };
11060786Spsstatic struct optname y_optname      = { "max-forw-scroll",      NULL };
11160786Spsstatic struct optname z_optname      = { "window",               NULL };
11260786Spsstatic struct optname quote_optname  = { "quotes",               NULL };
11360786Spsstatic struct optname tilde_optname  = { "tilde",                NULL };
11460786Spsstatic struct optname query_optname  = { "help",                 NULL };
11563128Spsstatic struct optname pound_optname  = { "shift",                NULL };
11689019Spsstatic struct optname keypad_optname = { "no-keypad",            NULL };
117170256Sdelphijstatic struct optname oldbot_optname = { "old-bot",              NULL };
118173682Sdelphijstatic struct optname follow_optname = { "follow-name",          NULL };
11960786Sps
12060786Sps
12160786Sps/*
12260786Sps * Table of all options and their semantics.
12389019Sps *
12489019Sps * For BOOL and TRIPLE options, odesc[0], odesc[1], odesc[2] are
12589019Sps * the description of the option when set to 0, 1 or 2, respectively.
12689019Sps * For NUMBER options, odesc[0] is the prompt to use when entering
12789019Sps * a new value, and odesc[1] is the description, which should contain
12889019Sps * one %d which is replaced by the value of the number.
12989019Sps * For STRING options, odesc[0] is the prompt to use when entering
13089019Sps * a new value, and odesc[1], if not NULL, is the set of characters
13189019Sps * that are valid in the string.
13260786Sps */
133128345Stjrstatic struct loption option[] =
13460786Sps{
13560786Sps	{ 'a', &a_optname,
136221715Sdelphij		TRIPLE, OPT_ONPLUS, &how_search, NULL,
137128345Stjr		{
138128345Stjr			"Search includes displayed screen",
139128345Stjr			"Search skips displayed screen",
140221715Sdelphij			"Search includes all of displayed screen"
141128345Stjr		}
14260786Sps	},
14360786Sps
14460786Sps	{ 'b', &b_optname,
145128345Stjr		NUMBER|INIT_HANDLER, 64, &bufspace, opt_b,
146128345Stjr		{
147128345Stjr			"Max buffer space per file (K): ",
148128345Stjr			"Max buffer space per file: %dK",
149128345Stjr			NULL
150128345Stjr		}
15160786Sps	},
15260786Sps	{ 'B', &B__optname,
15360786Sps		BOOL, OPT_ON, &autobuf, NULL,
154128345Stjr		{
155128345Stjr			"Don't automatically allocate buffers",
156128345Stjr			"Automatically allocate buffers when needed",
157128345Stjr			NULL
158128345Stjr		}
15960786Sps	},
16060786Sps	{ 'c', &c_optname,
16160786Sps		TRIPLE, OPT_OFF, &top_scroll, NULL,
162128345Stjr		{
163128345Stjr			"Repaint by scrolling from bottom of screen",
164170256Sdelphij			"Repaint by painting from top of screen",
165128345Stjr			"Repaint by painting from top of screen"
166128345Stjr		}
16760786Sps	},
16860786Sps	{ 'd', &d_optname,
16960786Sps		BOOL|NO_TOGGLE, OPT_OFF, &know_dumb, NULL,
170128345Stjr		{
171128345Stjr			"Assume intelligent terminal",
172128345Stjr			"Assume dumb terminal",
173128345Stjr			NULL
174128345Stjr		}
17560786Sps	},
17660786Sps#if MSDOS_COMPILER
17760786Sps	{ 'D', &D__optname,
17860786Sps		STRING|REPAINT|NO_QUERY, 0, NULL, opt_D,
179128345Stjr		{
180128345Stjr			"color desc: ",
181128345Stjr			"Ddknsu0123456789.",
182128345Stjr			NULL
183128345Stjr		}
18460786Sps	},
18560786Sps#endif
18660786Sps	{ 'e', &e_optname,
18760786Sps		TRIPLE, OPT_OFF, &quit_at_eof, NULL,
188128345Stjr		{
189128345Stjr			"Don't quit at end-of-file",
190128345Stjr			"Quit at end-of-file",
191128345Stjr			"Quit immediately at end-of-file"
192128345Stjr		}
19360786Sps	},
19460786Sps	{ 'f', &f_optname,
19560786Sps		BOOL, OPT_OFF, &force_open, NULL,
196128345Stjr		{
197128345Stjr			"Open only regular files",
198128345Stjr			"Open even non-regular files",
199128345Stjr			NULL
200128345Stjr		}
20160786Sps	},
20260786Sps	{ 'F', &F__optname,
20360786Sps		BOOL, OPT_OFF, &quit_if_one_screen, NULL,
204128345Stjr		{
205128345Stjr			"Don't quit if end-of-file on first screen",
206128345Stjr			"Quit if end-of-file on first screen",
207128345Stjr			NULL
208128345Stjr		}
20960786Sps	},
21060786Sps#if HILITE_SEARCH
21160786Sps	{ 'g', &g_optname,
21260786Sps		TRIPLE|HL_REPAINT, OPT_ONPLUS, &hilite_search, NULL,
213128345Stjr		{
214128345Stjr			"Don't highlight search matches",
215128345Stjr			"Highlight matches for previous search only",
216128345Stjr			"Highlight all matches for previous search pattern",
217128345Stjr		}
21860786Sps	},
21960786Sps#endif
22060786Sps	{ 'h', &h_optname,
22160786Sps		NUMBER, -1, &back_scroll, NULL,
222128345Stjr		{
223128345Stjr			"Backwards scroll limit: ",
224128345Stjr			"Backwards scroll limit is %d lines",
225128345Stjr			NULL
226128345Stjr		}
22760786Sps	},
22860786Sps	{ 'i', &i_optname,
22960786Sps		TRIPLE|HL_REPAINT, OPT_OFF, &caseless, opt_i,
230128345Stjr		{
231128345Stjr			"Case is significant in searches",
232128345Stjr			"Ignore case in searches",
233128345Stjr			"Ignore case in searches and in patterns"
234128345Stjr		}
23560786Sps	},
23660786Sps	{ 'j', &j_optname,
237170256Sdelphij		STRING, 0, NULL, opt_j,
238128345Stjr		{
239128345Stjr			"Target line: ",
240221715Sdelphij			"0123456789.-",
241128345Stjr			NULL
242128345Stjr		}
24360786Sps	},
24463128Sps	{ 'J', &J__optname,
24563128Sps		BOOL|REPAINT, OPT_OFF, &status_col, NULL,
246128345Stjr		{
247128345Stjr			"Don't display a status column",
248128345Stjr			"Display a status column",
249128345Stjr			NULL
250128345Stjr		}
25163128Sps	},
25260786Sps#if USERFILE
25360786Sps	{ 'k', &k_optname,
25460786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k,
255128345Stjr		{ NULL, NULL, NULL }
25660786Sps	},
25760786Sps#endif
258161475Sdelphij	{ 'K', &K__optname,
259161475Sdelphij		BOOL, OPT_OFF, &quit_on_intr, NULL,
260161475Sdelphij		{
261161475Sdelphij			"Interrupt (ctrl-C) returns to prompt",
262161475Sdelphij			"Interrupt (ctrl-C) exits less",
263161475Sdelphij			NULL
264161475Sdelphij		}
265161475Sdelphij	},
26660786Sps	{ 'l', NULL,
26760786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_l,
268128345Stjr		{ NULL, NULL, NULL }
26960786Sps	},
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	},
454128345Stjr	{ '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
45560786Sps};
45660786Sps
45760786Sps
45860786Sps/*
45960786Sps * Initialize each option to its default value.
46060786Sps */
46160786Sps	public void
46260786Spsinit_option()
46360786Sps{
464128345Stjr	register struct loption *o;
465161475Sdelphij	char *p;
46660786Sps
467161475Sdelphij	p = lgetenv("LESS_IS_MORE");
468161475Sdelphij	if (p != NULL && *p != '\0')
469161475Sdelphij		less_is_more = 1;
470161475Sdelphij
47160786Sps	for (o = option;  o->oletter != '\0';  o++)
47260786Sps	{
47360786Sps		/*
47460786Sps		 * Set each variable to its default.
47560786Sps		 */
47660786Sps		if (o->ovar != NULL)
47760786Sps			*(o->ovar) = o->odefault;
478128345Stjr		if (o->otype & INIT_HANDLER)
479128345Stjr			(*(o->ofunc))(INIT, (char *) NULL);
48060786Sps	}
48160786Sps}
48260786Sps
48360786Sps/*
48460786Sps * Find an option in the option table, given its option letter.
48560786Sps */
486128345Stjr	public struct loption *
48760786Spsfindopt(c)
48860786Sps	int c;
48960786Sps{
490128345Stjr	register struct loption *o;
49160786Sps
49260786Sps	for (o = option;  o->oletter != '\0';  o++)
49360786Sps	{
49460786Sps		if (o->oletter == c)
49560786Sps			return (o);
496161475Sdelphij		if ((o->otype & TRIPLE) && ASCII_TO_UPPER(o->oletter) == c)
49760786Sps			return (o);
49860786Sps	}
49960786Sps	return (NULL);
50060786Sps}
50160786Sps
50260786Sps/*
503128345Stjr *
504128345Stjr */
505128345Stjr	static int
506128345Stjris_optchar(c)
507128345Stjr	char c;
508128345Stjr{
509161475Sdelphij	if (ASCII_IS_UPPER(c))
510128345Stjr		return 1;
511161475Sdelphij	if (ASCII_IS_LOWER(c))
512128345Stjr		return 1;
513128345Stjr	if (c == '-')
514128345Stjr		return 1;
515128345Stjr	return 0;
516128345Stjr}
517128345Stjr
518128345Stjr/*
51960786Sps * Find an option in the option table, given its option name.
52060786Sps * p_optname is the (possibly partial) name to look for, and
52160786Sps * is updated to point after the matched name.
52260786Sps * p_oname if non-NULL is set to point to the full option name.
52360786Sps */
524128345Stjr	public struct loption *
52560786Spsfindopt_name(p_optname, p_oname, p_err)
52660786Sps	char **p_optname;
52760786Sps	char **p_oname;
52860786Sps	int *p_err;
52960786Sps{
53060786Sps	char *optname = *p_optname;
531128345Stjr	register struct loption *o;
53260786Sps	register struct optname *oname;
53360786Sps	register int len;
53460786Sps	int uppercase;
535128345Stjr	struct loption *maxo = NULL;
53660786Sps	struct optname *maxoname = NULL;
53760786Sps	int maxlen = 0;
53860786Sps	int ambig = 0;
53960786Sps	int exact = 0;
54060786Sps
54160786Sps	/*
54260786Sps	 * Check all options.
54360786Sps	 */
54460786Sps	for (o = option;  o->oletter != '\0';  o++)
54560786Sps	{
54660786Sps		/*
54760786Sps		 * Check all names for this option.
54860786Sps		 */
54960786Sps		for (oname = o->onames;  oname != NULL;  oname = oname->onext)
55060786Sps		{
55160786Sps			/*
55260786Sps			 * Try normal match first (uppercase == 0),
55360786Sps			 * then, then if it's a TRIPLE option,
55460786Sps			 * try uppercase match (uppercase == 1).
55560786Sps			 */
55660786Sps			for (uppercase = 0;  uppercase <= 1;  uppercase++)
55760786Sps			{
55860786Sps				len = sprefix(optname, oname->oname, uppercase);
559128345Stjr				if (len <= 0 || is_optchar(optname[len]))
560128345Stjr				{
561128345Stjr					/*
562128345Stjr					 * We didn't use all of the option name.
563128345Stjr					 */
564128345Stjr					continue;
565128345Stjr				}
56660786Sps				if (!exact && len == maxlen)
56760786Sps					/*
56860786Sps					 * Already had a partial match,
56960786Sps					 * and now there's another one that
57060786Sps					 * matches the same length.
57160786Sps					 */
57260786Sps					ambig = 1;
57360786Sps				else if (len > maxlen)
57460786Sps				{
57560786Sps					/*
57660786Sps					 * Found a better match than
57760786Sps					 * the one we had.
57860786Sps					 */
57960786Sps					maxo = o;
58060786Sps					maxoname = oname;
58160786Sps					maxlen = len;
58260786Sps					ambig = 0;
58389019Sps					exact = (len == (int)strlen(oname->oname));
58460786Sps				}
58560786Sps				if (!(o->otype & TRIPLE))
58660786Sps					break;
58760786Sps			}
58860786Sps		}
58960786Sps	}
59060786Sps	if (ambig)
59160786Sps	{
59260786Sps		/*
59360786Sps		 * Name matched more than one option.
59460786Sps		 */
59560786Sps		if (p_err != NULL)
59660786Sps			*p_err = OPT_AMBIG;
59760786Sps		return (NULL);
59860786Sps	}
59960786Sps	*p_optname = optname + maxlen;
60060786Sps	if (p_oname != NULL)
601128345Stjr		*p_oname = maxoname == NULL ? NULL : maxoname->oname;
60260786Sps	return (maxo);
60360786Sps}
604