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}} */
55251154Sdelphijpublic int opt_use_backslash;	/* Use backslash escaping in option parsing */
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 };
119251154Sdelphijstatic struct optname use_backslash_optname = { "use-backslash", NULL };
12060786Sps
12160786Sps
12260786Sps/*
12360786Sps * Table of all options and their semantics.
12489019Sps *
12589019Sps * For BOOL and TRIPLE options, odesc[0], odesc[1], odesc[2] are
12689019Sps * the description of the option when set to 0, 1 or 2, respectively.
12789019Sps * For NUMBER options, odesc[0] is the prompt to use when entering
12889019Sps * a new value, and odesc[1] is the description, which should contain
12989019Sps * one %d which is replaced by the value of the number.
13089019Sps * For STRING options, odesc[0] is the prompt to use when entering
13189019Sps * a new value, and odesc[1], if not NULL, is the set of characters
13289019Sps * that are valid in the string.
13360786Sps */
134128345Stjrstatic struct loption option[] =
13560786Sps{
13660786Sps	{ 'a', &a_optname,
137221715Sdelphij		TRIPLE, OPT_ONPLUS, &how_search, NULL,
138128345Stjr		{
139128345Stjr			"Search includes displayed screen",
140128345Stjr			"Search skips displayed screen",
141221715Sdelphij			"Search includes all of displayed screen"
142128345Stjr		}
14360786Sps	},
14460786Sps
14560786Sps	{ 'b', &b_optname,
146128345Stjr		NUMBER|INIT_HANDLER, 64, &bufspace, opt_b,
147128345Stjr		{
148128345Stjr			"Max buffer space per file (K): ",
149128345Stjr			"Max buffer space per file: %dK",
150128345Stjr			NULL
151128345Stjr		}
15260786Sps	},
15360786Sps	{ 'B', &B__optname,
15460786Sps		BOOL, OPT_ON, &autobuf, NULL,
155128345Stjr		{
156128345Stjr			"Don't automatically allocate buffers",
157128345Stjr			"Automatically allocate buffers when needed",
158128345Stjr			NULL
159128345Stjr		}
16060786Sps	},
16160786Sps	{ 'c', &c_optname,
16260786Sps		TRIPLE, OPT_OFF, &top_scroll, NULL,
163128345Stjr		{
164128345Stjr			"Repaint by scrolling from bottom of screen",
165170256Sdelphij			"Repaint by painting from top of screen",
166128345Stjr			"Repaint by painting from top of screen"
167128345Stjr		}
16860786Sps	},
16960786Sps	{ 'd', &d_optname,
17060786Sps		BOOL|NO_TOGGLE, OPT_OFF, &know_dumb, NULL,
171128345Stjr		{
172128345Stjr			"Assume intelligent terminal",
173128345Stjr			"Assume dumb terminal",
174128345Stjr			NULL
175128345Stjr		}
17660786Sps	},
17760786Sps#if MSDOS_COMPILER
17860786Sps	{ 'D', &D__optname,
17960786Sps		STRING|REPAINT|NO_QUERY, 0, NULL, opt_D,
180128345Stjr		{
181128345Stjr			"color desc: ",
182128345Stjr			"Ddknsu0123456789.",
183128345Stjr			NULL
184128345Stjr		}
18560786Sps	},
18660786Sps#endif
18760786Sps	{ 'e', &e_optname,
18860786Sps		TRIPLE, OPT_OFF, &quit_at_eof, NULL,
189128345Stjr		{
190128345Stjr			"Don't quit at end-of-file",
191128345Stjr			"Quit at end-of-file",
192128345Stjr			"Quit immediately at end-of-file"
193128345Stjr		}
19460786Sps	},
19560786Sps	{ 'f', &f_optname,
19660786Sps		BOOL, OPT_OFF, &force_open, NULL,
197128345Stjr		{
198128345Stjr			"Open only regular files",
199128345Stjr			"Open even non-regular files",
200128345Stjr			NULL
201128345Stjr		}
20260786Sps	},
20360786Sps	{ 'F', &F__optname,
20460786Sps		BOOL, OPT_OFF, &quit_if_one_screen, NULL,
205128345Stjr		{
206128345Stjr			"Don't quit if end-of-file on first screen",
207128345Stjr			"Quit if end-of-file on first screen",
208128345Stjr			NULL
209128345Stjr		}
21060786Sps	},
21160786Sps#if HILITE_SEARCH
21260786Sps	{ 'g', &g_optname,
21360786Sps		TRIPLE|HL_REPAINT, OPT_ONPLUS, &hilite_search, NULL,
214128345Stjr		{
215128345Stjr			"Don't highlight search matches",
216128345Stjr			"Highlight matches for previous search only",
217128345Stjr			"Highlight all matches for previous search pattern",
218128345Stjr		}
21960786Sps	},
22060786Sps#endif
22160786Sps	{ 'h', &h_optname,
22260786Sps		NUMBER, -1, &back_scroll, NULL,
223128345Stjr		{
224128345Stjr			"Backwards scroll limit: ",
225128345Stjr			"Backwards scroll limit is %d lines",
226128345Stjr			NULL
227128345Stjr		}
22860786Sps	},
22960786Sps	{ 'i', &i_optname,
23060786Sps		TRIPLE|HL_REPAINT, OPT_OFF, &caseless, opt_i,
231128345Stjr		{
232128345Stjr			"Case is significant in searches",
233128345Stjr			"Ignore case in searches",
234128345Stjr			"Ignore case in searches and in patterns"
235128345Stjr		}
23660786Sps	},
23760786Sps	{ 'j', &j_optname,
238170256Sdelphij		STRING, 0, NULL, opt_j,
239128345Stjr		{
240128345Stjr			"Target line: ",
241221715Sdelphij			"0123456789.-",
242128345Stjr			NULL
243128345Stjr		}
24460786Sps	},
24563128Sps	{ 'J', &J__optname,
24663128Sps		BOOL|REPAINT, OPT_OFF, &status_col, NULL,
247128345Stjr		{
248128345Stjr			"Don't display a status column",
249128345Stjr			"Display a status column",
250128345Stjr			NULL
251128345Stjr		}
25263128Sps	},
25360786Sps#if USERFILE
25460786Sps	{ 'k', &k_optname,
25560786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k,
256128345Stjr		{ NULL, NULL, NULL }
25760786Sps	},
25860786Sps#endif
259161475Sdelphij	{ 'K', &K__optname,
260161475Sdelphij		BOOL, OPT_OFF, &quit_on_intr, NULL,
261161475Sdelphij		{
262161475Sdelphij			"Interrupt (ctrl-C) returns to prompt",
263161475Sdelphij			"Interrupt (ctrl-C) exits less",
264161475Sdelphij			NULL
265161475Sdelphij		}
266161475Sdelphij	},
267128345Stjr	{ 'L', &L__optname,
268128345Stjr		BOOL, OPT_ON, &use_lessopen, NULL,
269128345Stjr		{
270128345Stjr			"Don't use the LESSOPEN filter",
271128345Stjr			"Use the LESSOPEN filter",
272128345Stjr			NULL
273128345Stjr		}
274128345Stjr	},
27560786Sps	{ 'm', &m_optname,
27660786Sps		TRIPLE, OPT_OFF, &pr_type, NULL,
277128345Stjr		{
278128345Stjr			"Short prompt",
279128345Stjr			"Medium prompt",
280128345Stjr			"Long prompt"
281128345Stjr		}
28260786Sps	},
28360786Sps	{ 'n', &n_optname,
28460786Sps		TRIPLE|REPAINT, OPT_ON, &linenums, NULL,
285128345Stjr		{
286128345Stjr			"Don't use line numbers",
287128345Stjr			"Use line numbers",
288128345Stjr			"Constantly display line numbers"
289128345Stjr		}
29060786Sps	},
29160786Sps#if LOGFILE
29260786Sps	{ 'o', &o_optname,
29360786Sps		STRING, 0, NULL, opt_o,
294128345Stjr		{ "log file: ", NULL, NULL }
29560786Sps	},
29660786Sps	{ 'O', &O__optname,
29760786Sps		STRING, 0, NULL, opt__O,
298128345Stjr		{ "Log file: ", NULL, NULL }
29960786Sps	},
30060786Sps#endif
30160786Sps	{ 'p', &p_optname,
30260786Sps		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_p,
303128345Stjr		{ NULL, NULL, NULL }
30460786Sps	},
30560786Sps	{ 'P', &P__optname,
30660786Sps		STRING, 0, NULL, opt__P,
307128345Stjr		{ "prompt: ", NULL, NULL }
30860786Sps	},
30960786Sps	{ 'q', &q_optname,
31060786Sps		TRIPLE, OPT_OFF, &quiet, NULL,
311128345Stjr		{
312128345Stjr			"Ring the bell for errors AND at eof/bof",
313128345Stjr			"Ring the bell for errors but not at eof/bof",
314128345Stjr			"Never ring the bell"
315128345Stjr		}
31660786Sps	},
31760786Sps	{ 'r', &r_optname,
31860786Sps		TRIPLE|REPAINT, OPT_OFF, &ctldisp, NULL,
319128345Stjr		{
320128345Stjr			"Display control characters as ^X",
321128345Stjr			"Display control characters directly",
322128345Stjr			"Display control characters directly, processing ANSI sequences"
323128345Stjr		}
32460786Sps	},
32560786Sps	{ 's', &s_optname,
32660786Sps		BOOL|REPAINT, OPT_OFF, &squeeze, NULL,
327128345Stjr		{
328128345Stjr			"Display all blank lines",
329128345Stjr			"Squeeze multiple blank lines",
330128345Stjr			NULL
331128345Stjr		}
33260786Sps	},
33360786Sps	{ 'S', &S__optname,
33460786Sps		BOOL|REPAINT, OPT_OFF, &chopline, NULL,
335128345Stjr		{
336128345Stjr			"Fold long lines",
337128345Stjr			"Chop long lines",
338128345Stjr			NULL
339128345Stjr		}
34060786Sps	},
34160786Sps#if TAGS
34260786Sps	{ 't', &t_optname,
34360786Sps		STRING|NO_QUERY, 0, NULL, opt_t,
344128345Stjr		{ "tag: ", NULL, NULL }
34560786Sps	},
34660786Sps	{ 'T', &T__optname,
34760786Sps		STRING, 0, NULL, opt__T,
348128345Stjr		{ "tags file: ", NULL, NULL }
34960786Sps	},
35060786Sps#endif
35160786Sps	{ 'u', &u_optname,
35260786Sps		TRIPLE|REPAINT, OPT_OFF, &bs_mode, NULL,
353128345Stjr		{
354128345Stjr			"Display underlined text in underline mode",
355128345Stjr			"Backspaces cause overstrike",
356128345Stjr			"Print backspace as ^H"
357128345Stjr		}
35860786Sps	},
35960786Sps	{ 'V', &V__optname,
36060786Sps		NOVAR, 0, NULL, opt__V,
361128345Stjr		{ NULL, NULL, NULL }
36260786Sps	},
36360786Sps	{ 'w', &w_optname,
36460786Sps		TRIPLE|REPAINT, OPT_OFF, &show_attn, NULL,
365128345Stjr		{
366128345Stjr			"Don't highlight first unread line",
367128345Stjr			"Highlight first unread line after forward-screen",
368128345Stjr			"Highlight first unread line after any forward movement",
369128345Stjr		}
37060786Sps	},
37160786Sps	{ 'x', &x_optname,
37289019Sps		STRING|REPAINT, 0, NULL, opt_x,
373128345Stjr		{
374128345Stjr			"Tab stops: ",
375128345Stjr			"0123456789,",
376128345Stjr			NULL
377128345Stjr		}
37860786Sps	},
37960786Sps	{ 'X', &X__optname,
38060786Sps		BOOL|NO_TOGGLE, OPT_OFF, &no_init, NULL,
381128345Stjr		{
382128345Stjr			"Send init/deinit strings to terminal",
383128345Stjr			"Don't use init/deinit strings",
384128345Stjr			NULL
385128345Stjr		}
38660786Sps	},
38760786Sps	{ 'y', &y_optname,
38860786Sps		NUMBER, -1, &forw_scroll, NULL,
389128345Stjr		{
390128345Stjr			"Forward scroll limit: ",
391128345Stjr			"Forward scroll limit is %d lines",
392128345Stjr			NULL
393128345Stjr		}
39460786Sps	},
39560786Sps	{ 'z', &z_optname,
39660786Sps		NUMBER, -1, &swindow, NULL,
397128345Stjr		{
398128345Stjr			"Scroll window size: ",
399128345Stjr			"Scroll window size is %d lines",
400128345Stjr			NULL
401128345Stjr		}
40260786Sps	},
40360786Sps	{ '"', &quote_optname,
40460786Sps		STRING, 0, NULL, opt_quote,
405128345Stjr		{ "quotes: ", NULL, NULL }
40660786Sps	},
40760786Sps	{ '~', &tilde_optname,
40860786Sps		BOOL|REPAINT, OPT_ON, &twiddle, NULL,
409128345Stjr		{
410128345Stjr			"Don't show tildes after end of file",
411128345Stjr			"Show tildes after end of file",
412128345Stjr			NULL
413128345Stjr		}
41460786Sps	},
41560786Sps	{ '?', &query_optname,
41660786Sps		NOVAR, 0, NULL, opt_query,
417128345Stjr		{ NULL, NULL, NULL }
41860786Sps	},
41963128Sps	{ '#', &pound_optname,
420195941Sdelphij		STRING, 0, NULL, opt_shift,
421128345Stjr		{
422128345Stjr			"Horizontal shift: ",
423195941Sdelphij			"0123456789.",
424128345Stjr			NULL
425128345Stjr		}
42663128Sps	},
427221715Sdelphij	{ OLETTER_NONE, &keypad_optname,
42889019Sps		BOOL|NO_TOGGLE, OPT_OFF, &no_keypad, NULL,
429128345Stjr		{
430128345Stjr			"Use keypad mode",
431128345Stjr			"Don't use keypad mode",
432128345Stjr			NULL
433128345Stjr		}
43489019Sps	},
435221715Sdelphij	{ OLETTER_NONE, &oldbot_optname,
436170256Sdelphij		BOOL, OPT_OFF, &oldbot, NULL,
437170256Sdelphij		{
438170256Sdelphij			"Use new bottom of screen behavior",
439170256Sdelphij			"Use old bottom of screen behavior",
440170256Sdelphij			NULL
441170256Sdelphij		}
442170256Sdelphij	},
443221715Sdelphij	{ OLETTER_NONE, &follow_optname,
444173682Sdelphij		BOOL, FOLLOW_DESC, &follow_mode, NULL,
445173682Sdelphij		{
446221715Sdelphij			"F command follows file descriptor",
447221715Sdelphij			"F command follows file name",
448173682Sdelphij			NULL
449173682Sdelphij		}
450173682Sdelphij	},
451251154Sdelphij	{ OLETTER_NONE, &use_backslash_optname,
452251154Sdelphij		BOOL, OPT_OFF, &opt_use_backslash, NULL,
453251154Sdelphij		{
454251154Sdelphij			"Use backslash escaping in command line parameters",
455251154Sdelphij			"Don't use backslash escaping in command line parameters",
456251154Sdelphij			NULL
457251154Sdelphij		}
458251154Sdelphij	},
459128345Stjr	{ '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
46060786Sps};
46160786Sps
46260786Sps
46360786Sps/*
46460786Sps * Initialize each option to its default value.
46560786Sps */
46660786Sps	public void
46760786Spsinit_option()
46860786Sps{
469128345Stjr	register struct loption *o;
470161475Sdelphij	char *p;
47160786Sps
472161475Sdelphij	p = lgetenv("LESS_IS_MORE");
473161475Sdelphij	if (p != NULL && *p != '\0')
474161475Sdelphij		less_is_more = 1;
475161475Sdelphij
47660786Sps	for (o = option;  o->oletter != '\0';  o++)
47760786Sps	{
47860786Sps		/*
47960786Sps		 * Set each variable to its default.
48060786Sps		 */
48160786Sps		if (o->ovar != NULL)
48260786Sps			*(o->ovar) = o->odefault;
483128345Stjr		if (o->otype & INIT_HANDLER)
484128345Stjr			(*(o->ofunc))(INIT, (char *) NULL);
48560786Sps	}
48660786Sps}
48760786Sps
48860786Sps/*
48960786Sps * Find an option in the option table, given its option letter.
49060786Sps */
491128345Stjr	public struct loption *
49260786Spsfindopt(c)
49360786Sps	int c;
49460786Sps{
495128345Stjr	register struct loption *o;
49660786Sps
49760786Sps	for (o = option;  o->oletter != '\0';  o++)
49860786Sps	{
49960786Sps		if (o->oletter == c)
50060786Sps			return (o);
501161475Sdelphij		if ((o->otype & TRIPLE) && ASCII_TO_UPPER(o->oletter) == c)
50260786Sps			return (o);
50360786Sps	}
50460786Sps	return (NULL);
50560786Sps}
50660786Sps
50760786Sps/*
508128345Stjr *
509128345Stjr */
510128345Stjr	static int
511128345Stjris_optchar(c)
512128345Stjr	char c;
513128345Stjr{
514161475Sdelphij	if (ASCII_IS_UPPER(c))
515128345Stjr		return 1;
516161475Sdelphij	if (ASCII_IS_LOWER(c))
517128345Stjr		return 1;
518128345Stjr	if (c == '-')
519128345Stjr		return 1;
520128345Stjr	return 0;
521128345Stjr}
522128345Stjr
523128345Stjr/*
52460786Sps * Find an option in the option table, given its option name.
52560786Sps * p_optname is the (possibly partial) name to look for, and
52660786Sps * is updated to point after the matched name.
52760786Sps * p_oname if non-NULL is set to point to the full option name.
52860786Sps */
529128345Stjr	public struct loption *
53060786Spsfindopt_name(p_optname, p_oname, p_err)
53160786Sps	char **p_optname;
53260786Sps	char **p_oname;
53360786Sps	int *p_err;
53460786Sps{
53560786Sps	char *optname = *p_optname;
536128345Stjr	register struct loption *o;
53760786Sps	register struct optname *oname;
53860786Sps	register int len;
53960786Sps	int uppercase;
540128345Stjr	struct loption *maxo = NULL;
54160786Sps	struct optname *maxoname = NULL;
54260786Sps	int maxlen = 0;
54360786Sps	int ambig = 0;
54460786Sps	int exact = 0;
54560786Sps
54660786Sps	/*
54760786Sps	 * Check all options.
54860786Sps	 */
54960786Sps	for (o = option;  o->oletter != '\0';  o++)
55060786Sps	{
55160786Sps		/*
55260786Sps		 * Check all names for this option.
55360786Sps		 */
55460786Sps		for (oname = o->onames;  oname != NULL;  oname = oname->onext)
55560786Sps		{
55660786Sps			/*
55760786Sps			 * Try normal match first (uppercase == 0),
55860786Sps			 * then, then if it's a TRIPLE option,
55960786Sps			 * try uppercase match (uppercase == 1).
56060786Sps			 */
56160786Sps			for (uppercase = 0;  uppercase <= 1;  uppercase++)
56260786Sps			{
56360786Sps				len = sprefix(optname, oname->oname, uppercase);
564128345Stjr				if (len <= 0 || is_optchar(optname[len]))
565128345Stjr				{
566128345Stjr					/*
567128345Stjr					 * We didn't use all of the option name.
568128345Stjr					 */
569128345Stjr					continue;
570128345Stjr				}
57160786Sps				if (!exact && len == maxlen)
57260786Sps					/*
57360786Sps					 * Already had a partial match,
57460786Sps					 * and now there's another one that
57560786Sps					 * matches the same length.
57660786Sps					 */
57760786Sps					ambig = 1;
57860786Sps				else if (len > maxlen)
57960786Sps				{
58060786Sps					/*
58160786Sps					 * Found a better match than
58260786Sps					 * the one we had.
58360786Sps					 */
58460786Sps					maxo = o;
58560786Sps					maxoname = oname;
58660786Sps					maxlen = len;
58760786Sps					ambig = 0;
58889019Sps					exact = (len == (int)strlen(oname->oname));
58960786Sps				}
59060786Sps				if (!(o->otype & TRIPLE))
59160786Sps					break;
59260786Sps			}
59360786Sps		}
59460786Sps	}
59560786Sps	if (ambig)
59660786Sps	{
59760786Sps		/*
59860786Sps		 * Name matched more than one option.
59960786Sps		 */
60060786Sps		if (p_err != NULL)
60160786Sps			*p_err = OPT_AMBIG;
60260786Sps		return (NULL);
60360786Sps	}
60460786Sps	*p_optname = optname + maxlen;
60560786Sps	if (p_oname != NULL)
606128345Stjr		*p_oname = maxoname == NULL ? NULL : maxoname->oname;
60760786Sps	return (maxo);
60860786Sps}
609