xref: /freebsd/contrib/less/opttbl.c (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1 /*
2  * Copyright (C) 1984-2021  Mark Nudelman
3  *
4  * You may distribute under the terms of either the GNU General Public
5  * License or the Less License, as specified in the README file.
6  *
7  * For more information, see the README file.
8  */
9 
10 
11 /*
12  * The option table.
13  */
14 
15 #include "less.h"
16 #include "option.h"
17 
18 /*
19  * Variables controlled by command line options.
20  */
21 public int quiet;               /* Should we suppress the audible bell? */
22 public int how_search;          /* Where should forward searches start? */
23 public int top_scroll;          /* Repaint screen from top?
24                                    (alternative is scroll from bottom) */
25 public int pr_type;             /* Type of prompt (short, medium, long) */
26 public int bs_mode;             /* How to process backspaces */
27 public int know_dumb;           /* Don't complain about dumb terminals */
28 public int quit_at_eof;         /* Quit after hitting end of file twice */
29 public int quit_if_one_screen;  /* Quit if EOF on first screen */
30 public int squeeze;             /* Squeeze multiple blank lines into one */
31 public int tabstop;             /* Tab settings */
32 public int back_scroll;         /* Repaint screen on backwards movement */
33 public int forw_scroll;         /* Repaint screen on forward movement */
34 public int caseless;            /* Do "caseless" searches */
35 public int linenums;            /* Use line numbers */
36 public int autobuf;             /* Automatically allocate buffers as needed */
37 public int bufspace;            /* Max buffer space per file (K) */
38 public int ctldisp;             /* Send control chars to screen untranslated */
39 public int force_open;          /* Open the file even if not regular file */
40 public int swindow;             /* Size of scrolling window */
41 public int jump_sline;          /* Screen line of "jump target" */
42 public long jump_sline_fraction = -1;
43 public long shift_count_fraction = -1;
44 public int chopline;            /* Truncate displayed lines at screen width */
45 public int no_init;             /* Disable sending ti/te termcap strings */
46 public int no_keypad;           /* Disable sending ks/ke termcap strings */
47 public int twiddle;             /* Show tildes after EOF */
48 public int show_attn;           /* Hilite first unread line */
49 public int shift_count;         /* Number of positions to shift horizontally */
50 public int status_col;          /* Display a status column */
51 public int use_lessopen;        /* Use the LESSOPEN filter */
52 public int quit_on_intr;        /* Quit on interrupt */
53 public int follow_mode;         /* F cmd Follows file desc or file name? */
54 public int oldbot;              /* Old bottom of screen behavior {{REMOVE}} */
55 public int opt_use_backslash;   /* Use backslash escaping in option parsing */
56 public char rscroll_char;       /* Char which marks chopped lines with -S */
57 public int rscroll_attr;        /* Attribute of rscroll_char */
58 public int no_hist_dups;        /* Remove dups from history list */
59 public int mousecap;            /* Allow mouse for scrolling */
60 public int wheel_lines;         /* Number of lines to scroll on mouse wheel scroll */
61 public int perma_marks;         /* Save marks in history file */
62 public int linenum_width;       /* Width of line numbers */
63 public int status_col_width;    /* Width of status column */
64 public int incr_search;         /* Incremental search */
65 public int use_color;           /* Use UI color */
66 public int want_filesize;       /* */
67 #if HILITE_SEARCH
68 public int hilite_search;       /* Highlight matched search patterns? */
69 #endif
70 
71 public int less_is_more = 0;    /* Make compatible with POSIX more */
72 
73 /*
74  * Long option names.
75  */
76 static struct optname a_optname      = { "search-skip-screen",   NULL };
77 static struct optname b_optname      = { "buffers",              NULL };
78 static struct optname B__optname     = { "auto-buffers",         NULL };
79 static struct optname c_optname      = { "clear-screen",         NULL };
80 static struct optname d_optname      = { "dumb",                 NULL };
81 static struct optname D__optname     = { "color",                NULL };
82 static struct optname e_optname      = { "quit-at-eof",          NULL };
83 static struct optname f_optname      = { "force",                NULL };
84 static struct optname F__optname     = { "quit-if-one-screen",   NULL };
85 #if HILITE_SEARCH
86 static struct optname g_optname      = { "hilite-search",        NULL };
87 #endif
88 static struct optname h_optname      = { "max-back-scroll",      NULL };
89 static struct optname i_optname      = { "ignore-case",          NULL };
90 static struct optname j_optname      = { "jump-target",          NULL };
91 static struct optname J__optname     = { "status-column",        NULL };
92 #if USERFILE
93 static struct optname k_optname      = { "lesskey-file",         NULL };
94 #if HAVE_LESSKEYSRC
95 static struct optname ks_optname     = { "lesskey-src",          NULL };
96 #endif /* HAVE_LESSKEYSRC */
97 #endif
98 static struct optname K__optname     = { "quit-on-intr",         NULL };
99 static struct optname L__optname     = { "no-lessopen",          NULL };
100 static struct optname m_optname      = { "long-prompt",          NULL };
101 static struct optname n_optname      = { "line-numbers",         NULL };
102 #if LOGFILE
103 static struct optname o_optname      = { "log-file",             NULL };
104 static struct optname O__optname     = { "LOG-FILE",             NULL };
105 #endif
106 static struct optname p_optname      = { "pattern",              NULL };
107 static struct optname P__optname     = { "prompt",               NULL };
108 static struct optname q2_optname     = { "silent",               NULL };
109 static struct optname q_optname      = { "quiet",                &q2_optname };
110 static struct optname r_optname      = { "raw-control-chars",    NULL };
111 static struct optname s_optname      = { "squeeze-blank-lines",  NULL };
112 static struct optname S__optname     = { "chop-long-lines",      NULL };
113 #if TAGS
114 static struct optname t_optname      = { "tag",                  NULL };
115 static struct optname T__optname     = { "tag-file",             NULL };
116 #endif
117 static struct optname u_optname      = { "underline-special",    NULL };
118 static struct optname V__optname     = { "version",              NULL };
119 static struct optname w_optname      = { "hilite-unread",        NULL };
120 static struct optname x_optname      = { "tabs",                 NULL };
121 static struct optname X__optname     = { "no-init",              NULL };
122 static struct optname y_optname      = { "max-forw-scroll",      NULL };
123 static struct optname z_optname      = { "window",               NULL };
124 static struct optname quote_optname  = { "quotes",               NULL };
125 static struct optname tilde_optname  = { "tilde",                NULL };
126 static struct optname query_optname  = { "help",                 NULL };
127 static struct optname pound_optname  = { "shift",                NULL };
128 static struct optname keypad_optname = { "no-keypad",            NULL };
129 static struct optname oldbot_optname = { "old-bot",              NULL };
130 static struct optname follow_optname = { "follow-name",          NULL };
131 static struct optname use_backslash_optname = { "use-backslash", NULL };
132 static struct optname rscroll_optname = { "rscroll", NULL };
133 static struct optname nohistdups_optname = { "no-histdups",      NULL };
134 static struct optname mousecap_optname = { "mouse",              NULL };
135 static struct optname wheel_lines_optname = { "wheel-lines",     NULL };
136 static struct optname perma_marks_optname = { "save-marks",      NULL };
137 static struct optname linenum_width_optname = { "line-num-width", NULL };
138 static struct optname status_col_width_optname = { "status-col-width", NULL };
139 static struct optname incr_search_optname = { "incsearch",       NULL };
140 static struct optname use_color_optname = { "use-color",         NULL };
141 static struct optname want_filesize_optname = { "file-size",     NULL };
142 #if LESSTEST
143 static struct optname ttyin_name_optname = { "tty",              NULL };
144 static struct optname rstat_optname  = { "rstat",                NULL };
145 #endif /*LESSTEST*/
146 
147 
148 /*
149  * Table of all options and their semantics.
150  *
151  * For BOOL and TRIPLE options, odesc[0], odesc[1], odesc[2] are
152  * the description of the option when set to 0, 1 or 2, respectively.
153  * For NUMBER options, odesc[0] is the prompt to use when entering
154  * a new value, and odesc[1] is the description, which should contain
155  * one %d which is replaced by the value of the number.
156  * For STRING options, odesc[0] is the prompt to use when entering
157  * a new value, and odesc[1], if not NULL, is the set of characters
158  * that are valid in the string.
159  */
160 static struct loption option[] =
161 {
162 	{ 'a', &a_optname,
163 		TRIPLE, OPT_ONPLUS, &how_search, NULL,
164 		{
165 			"Search includes displayed screen",
166 			"Search skips displayed screen",
167 			"Search includes all of displayed screen"
168 		}
169 	},
170 
171 	{ 'b', &b_optname,
172 		NUMBER|INIT_HANDLER, 64, &bufspace, opt_b,
173 		{
174 			"Max buffer space per file (K): ",
175 			"Max buffer space per file: %dK",
176 			NULL
177 		}
178 	},
179 	{ 'B', &B__optname,
180 		BOOL, OPT_ON, &autobuf, NULL,
181 		{
182 			"Don't automatically allocate buffers",
183 			"Automatically allocate buffers when needed",
184 			NULL
185 		}
186 	},
187 	{ 'c', &c_optname,
188 		TRIPLE, OPT_OFF, &top_scroll, NULL,
189 		{
190 			"Repaint by scrolling from bottom of screen",
191 			"Repaint by painting from top of screen",
192 			"Repaint by painting from top of screen"
193 		}
194 	},
195 	{ 'd', &d_optname,
196 		BOOL|NO_TOGGLE, OPT_OFF, &know_dumb, NULL,
197 		{
198 			"Assume intelligent terminal",
199 			"Assume dumb terminal",
200 			NULL
201 		}
202 	},
203 	{ 'D', &D__optname,
204 		STRING|REPAINT|NO_QUERY, 0, NULL, opt_D,
205 		{
206 			"color desc: ",
207 			NULL,
208 			NULL
209 		}
210 	},
211 	{ 'e', &e_optname,
212 		TRIPLE, OPT_OFF, &quit_at_eof, NULL,
213 		{
214 			"Don't quit at end-of-file",
215 			"Quit at end-of-file",
216 			"Quit immediately at end-of-file"
217 		}
218 	},
219 	{ 'f', &f_optname,
220 		BOOL, OPT_OFF, &force_open, NULL,
221 		{
222 			"Open only regular files",
223 			"Open even non-regular files",
224 			NULL
225 		}
226 	},
227 	{ 'F', &F__optname,
228 		BOOL, OPT_OFF, &quit_if_one_screen, NULL,
229 		{
230 			"Don't quit if end-of-file on first screen",
231 			"Quit if end-of-file on first screen",
232 			NULL
233 		}
234 	},
235 #if HILITE_SEARCH
236 	{ 'g', &g_optname,
237 		TRIPLE|HL_REPAINT, OPT_ONPLUS, &hilite_search, NULL,
238 		{
239 			"Don't highlight search matches",
240 			"Highlight matches for previous search only",
241 			"Highlight all matches for previous search pattern",
242 		}
243 	},
244 #endif
245 	{ 'h', &h_optname,
246 		NUMBER, -1, &back_scroll, NULL,
247 		{
248 			"Backwards scroll limit: ",
249 			"Backwards scroll limit is %d lines",
250 			NULL
251 		}
252 	},
253 	{ 'i', &i_optname,
254 		TRIPLE|HL_REPAINT, OPT_OFF, &caseless, opt_i,
255 		{
256 			"Case is significant in searches",
257 			"Ignore case in searches",
258 			"Ignore case in searches and in patterns"
259 		}
260 	},
261 	{ 'j', &j_optname,
262 		STRING, 0, NULL, opt_j,
263 		{
264 			"Target line: ",
265 			"0123456789.-",
266 			NULL
267 		}
268 	},
269 	{ 'J', &J__optname,
270 		BOOL|REPAINT, OPT_OFF, &status_col, NULL,
271 		{
272 			"Don't display a status column",
273 			"Display a status column",
274 			NULL
275 		}
276 	},
277 #if USERFILE
278 	{ 'k', &k_optname,
279 		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k,
280 		{ NULL, NULL, NULL }
281 	},
282 #if HAVE_LESSKEYSRC
283 	{ OLETTER_NONE, &ks_optname,
284 		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_ks,
285 		{ NULL, NULL, NULL }
286 	},
287 #endif /* HAVE_LESSKEYSRC */
288 #endif
289 	{ 'K', &K__optname,
290 		BOOL, OPT_OFF, &quit_on_intr, NULL,
291 		{
292 			"Interrupt (ctrl-C) returns to prompt",
293 			"Interrupt (ctrl-C) exits less",
294 			NULL
295 		}
296 	},
297 	{ 'L', &L__optname,
298 		BOOL, OPT_ON, &use_lessopen, NULL,
299 		{
300 			"Don't use the LESSOPEN filter",
301 			"Use the LESSOPEN filter",
302 			NULL
303 		}
304 	},
305 	{ 'm', &m_optname,
306 		TRIPLE, OPT_OFF, &pr_type, NULL,
307 		{
308 			"Short prompt",
309 			"Medium prompt",
310 			"Long prompt"
311 		}
312 	},
313 	{ 'n', &n_optname,
314 		TRIPLE|REPAINT, OPT_ON, &linenums, NULL,
315 		{
316 			"Don't use line numbers",
317 			"Use line numbers",
318 			"Constantly display line numbers"
319 		}
320 	},
321 #if LOGFILE
322 	{ 'o', &o_optname,
323 		STRING, 0, NULL, opt_o,
324 		{ "log file: ", NULL, NULL }
325 	},
326 	{ 'O', &O__optname,
327 		STRING, 0, NULL, opt__O,
328 		{ "Log file: ", NULL, NULL }
329 	},
330 #endif
331 	{ 'p', &p_optname,
332 		STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_p,
333 		{ NULL, NULL, NULL }
334 	},
335 	{ 'P', &P__optname,
336 		STRING, 0, NULL, opt__P,
337 		{ "prompt: ", NULL, NULL }
338 	},
339 	{ 'q', &q_optname,
340 		TRIPLE, OPT_OFF, &quiet, NULL,
341 		{
342 			"Ring the bell for errors AND at eof/bof",
343 			"Ring the bell for errors but not at eof/bof",
344 			"Never ring the bell"
345 		}
346 	},
347 	{ 'r', &r_optname,
348 		TRIPLE|REPAINT, OPT_OFF, &ctldisp, NULL,
349 		{
350 			"Display control characters as ^X",
351 			"Display control characters directly (not recommended)",
352 			"Display ANSI sequences directly, other control characters as ^X"
353 		}
354 	},
355 	{ 's', &s_optname,
356 		BOOL|REPAINT, OPT_OFF, &squeeze, NULL,
357 		{
358 			"Display all blank lines",
359 			"Squeeze multiple blank lines",
360 			NULL
361 		}
362 	},
363 	{ 'S', &S__optname,
364 		BOOL|REPAINT, OPT_OFF, &chopline, NULL,
365 		{
366 			"Fold long lines",
367 			"Chop long lines",
368 			NULL
369 		}
370 	},
371 #if TAGS
372 	{ 't', &t_optname,
373 		STRING|NO_QUERY, 0, NULL, opt_t,
374 		{ "tag: ", NULL, NULL }
375 	},
376 	{ 'T', &T__optname,
377 		STRING, 0, NULL, opt__T,
378 		{ "tags file: ", NULL, NULL }
379 	},
380 #endif
381 	{ 'u', &u_optname,
382 		TRIPLE|REPAINT, OPT_OFF, &bs_mode, NULL,
383 		{
384 			"Display underlined text in underline mode",
385 			"Backspaces cause overstrike",
386 			"Print backspace as ^H"
387 		}
388 	},
389 	{ 'V', &V__optname,
390 		NOVAR, 0, NULL, opt__V,
391 		{ NULL, NULL, NULL }
392 	},
393 	{ 'w', &w_optname,
394 		TRIPLE|REPAINT, OPT_OFF, &show_attn, NULL,
395 		{
396 			"Don't highlight first unread line",
397 			"Highlight first unread line after forward-screen",
398 			"Highlight first unread line after any forward movement",
399 		}
400 	},
401 	{ 'x', &x_optname,
402 		STRING|REPAINT, 0, NULL, opt_x,
403 		{
404 			"Tab stops: ",
405 			"0123456789,",
406 			NULL
407 		}
408 	},
409 	{ 'X', &X__optname,
410 		BOOL|NO_TOGGLE, OPT_OFF, &no_init, NULL,
411 		{
412 			"Send init/deinit strings to terminal",
413 			"Don't use init/deinit strings",
414 			NULL
415 		}
416 	},
417 	{ 'y', &y_optname,
418 		NUMBER, -1, &forw_scroll, NULL,
419 		{
420 			"Forward scroll limit: ",
421 			"Forward scroll limit is %d lines",
422 			NULL
423 		}
424 	},
425 	{ 'z', &z_optname,
426 		NUMBER, -1, &swindow, NULL,
427 		{
428 			"Scroll window size: ",
429 			"Scroll window size is %d lines",
430 			NULL
431 		}
432 	},
433 	{ '"', &quote_optname,
434 		STRING, 0, NULL, opt_quote,
435 		{ "quotes: ", NULL, NULL }
436 	},
437 	{ '~', &tilde_optname,
438 		BOOL|REPAINT, OPT_ON, &twiddle, NULL,
439 		{
440 			"Don't show tildes after end of file",
441 			"Show tildes after end of file",
442 			NULL
443 		}
444 	},
445 	{ '?', &query_optname,
446 		NOVAR, 0, NULL, opt_query,
447 		{ NULL, NULL, NULL }
448 	},
449 	{ '#', &pound_optname,
450 		STRING, 0, NULL, opt_shift,
451 		{
452 			"Horizontal shift: ",
453 			"0123456789.",
454 			NULL
455 		}
456 	},
457 	{ OLETTER_NONE, &keypad_optname,
458 		BOOL|NO_TOGGLE, OPT_OFF, &no_keypad, NULL,
459 		{
460 			"Use keypad mode",
461 			"Don't use keypad mode",
462 			NULL
463 		}
464 	},
465 	{ OLETTER_NONE, &oldbot_optname,
466 		BOOL, OPT_OFF, &oldbot, NULL,
467 		{
468 			"Use new bottom of screen behavior",
469 			"Use old bottom of screen behavior",
470 			NULL
471 		}
472 	},
473 	{ OLETTER_NONE, &follow_optname,
474 		BOOL, FOLLOW_DESC, &follow_mode, NULL,
475 		{
476 			"F command follows file descriptor",
477 			"F command follows file name",
478 			NULL
479 		}
480 	},
481 	{ OLETTER_NONE, &use_backslash_optname,
482 		BOOL, OPT_OFF, &opt_use_backslash, NULL,
483 		{
484 			"Use backslash escaping in command line parameters",
485 			"Don't use backslash escaping in command line parameters",
486 			NULL
487 		}
488 	},
489 	{ OLETTER_NONE, &rscroll_optname,
490 		STRING|REPAINT|INIT_HANDLER, 0, NULL, opt_rscroll,
491 		{ "right scroll character: ", NULL, NULL }
492 	},
493 	{ OLETTER_NONE, &nohistdups_optname,
494 		BOOL, OPT_OFF, &no_hist_dups, NULL,
495 		{
496 			"Allow duplicates in history list",
497 			"Remove duplicates from history list",
498 			NULL
499 		}
500 	},
501 	{ OLETTER_NONE, &mousecap_optname,
502 		TRIPLE, OPT_OFF, &mousecap, opt_mousecap,
503 		{
504 			"Ignore mouse input",
505 			"Use the mouse for scrolling",
506 			"Use the mouse for scrolling (reverse)"
507 		}
508 	},
509 	{ OLETTER_NONE, &wheel_lines_optname,
510 		NUMBER|INIT_HANDLER, 0, &wheel_lines, opt_wheel_lines,
511 		{
512 			"Lines to scroll on mouse wheel: ",
513 			"Scroll %d line(s) on mouse wheel",
514 			NULL
515 		}
516 	},
517 	{ OLETTER_NONE, &perma_marks_optname,
518 		BOOL, OPT_OFF, &perma_marks, NULL,
519 		{
520 			"Don't save marks in history file",
521 			"Save marks in history file",
522 			NULL
523 		}
524 	},
525 	{ OLETTER_NONE, &linenum_width_optname,
526 		NUMBER|REPAINT, MIN_LINENUM_WIDTH, &linenum_width, opt_linenum_width,
527 		{
528 			"Line number width: ",
529 			"Line number width is %d chars",
530 			NULL
531 		}
532 	},
533 	{ OLETTER_NONE, &status_col_width_optname,
534 		NUMBER|REPAINT, 2, &status_col_width, opt_status_col_width,
535 		{
536 			"Status column width: ",
537 			"Status column width is %d chars",
538 			NULL
539 		}
540 	},
541 	{ OLETTER_NONE, &incr_search_optname,
542 		BOOL, OPT_OFF, &incr_search, NULL,
543 		{
544 			"Incremental search is off",
545 			"Incremental search is on",
546 			NULL
547 		}
548 	},
549 	{ OLETTER_NONE, &use_color_optname,
550 		BOOL|REPAINT, OPT_OFF, &use_color, NULL,
551 		{
552 			"Don't use color",
553 			"Use color",
554 			NULL
555 		}
556 	},
557 	{ OLETTER_NONE, &want_filesize_optname,
558 		BOOL|REPAINT, OPT_OFF, &want_filesize, opt_filesize,
559 		{
560 			"Don't get size of each file",
561 			"Get size of each file",
562 			NULL
563 		}
564 	},
565 #if LESSTEST
566 	{ OLETTER_NONE, &ttyin_name_optname,
567 		STRING|NO_TOGGLE, 0, NULL, opt_ttyin_name,
568 		{
569 			NULL,
570 			NULL,
571 			NULL
572 		}
573 	},
574 	{ OLETTER_NONE, &rstat_optname,
575 		STRING|NO_TOGGLE, 0, NULL, opt_rstat,
576 		{
577 			NULL,
578 			NULL,
579 			NULL
580 		}
581 	},
582 #endif /*LESSTEST*/
583 	{ '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
584 };
585 
586 
587 /*
588  * Initialize each option to its default value.
589  */
590 	public void
591 init_option(VOID_PARAM)
592 {
593 	struct loption *o;
594 	char *p;
595 
596 	p = lgetenv("LESS_IS_MORE");
597 	if (!isnullenv(p))
598 		less_is_more = 1;
599 
600 	for (o = option;  o->oletter != '\0';  o++)
601 	{
602 		/*
603 		 * Set each variable to its default.
604 		 */
605 		if (o->ovar != NULL)
606 			*(o->ovar) = o->odefault;
607 		if (o->otype & INIT_HANDLER)
608 			(*(o->ofunc))(INIT, (char *) NULL);
609 	}
610 }
611 
612 /*
613  * Find an option in the option table, given its option letter.
614  */
615 	public struct loption *
616 findopt(c)
617 	int c;
618 {
619 	struct loption *o;
620 
621 	for (o = option;  o->oletter != '\0';  o++)
622 	{
623 		if (o->oletter == c)
624 			return (o);
625 		if ((o->otype & TRIPLE) && ASCII_TO_UPPER(o->oletter) == c)
626 			return (o);
627 	}
628 	return (NULL);
629 }
630 
631 /*
632  *
633  */
634 	static int
635 is_optchar(c)
636 	char c;
637 {
638 	if (ASCII_IS_UPPER(c))
639 		return 1;
640 	if (ASCII_IS_LOWER(c))
641 		return 1;
642 	if (c == '-')
643 		return 1;
644 	return 0;
645 }
646 
647 /*
648  * Find an option in the option table, given its option name.
649  * p_optname is the (possibly partial) name to look for, and
650  * is updated to point after the matched name.
651  * p_oname if non-NULL is set to point to the full option name.
652  */
653 	public struct loption *
654 findopt_name(p_optname, p_oname, p_err)
655 	char **p_optname;
656 	char **p_oname;
657 	int *p_err;
658 {
659 	char *optname = *p_optname;
660 	struct loption *o;
661 	struct optname *oname;
662 	int len;
663 	int uppercase;
664 	struct loption *maxo = NULL;
665 	struct optname *maxoname = NULL;
666 	int maxlen = 0;
667 	int ambig = 0;
668 	int exact = 0;
669 
670 	/*
671 	 * Check all options.
672 	 */
673 	for (o = option;  o->oletter != '\0';  o++)
674 	{
675 		/*
676 		 * Check all names for this option.
677 		 */
678 		for (oname = o->onames;  oname != NULL;  oname = oname->onext)
679 		{
680 			/*
681 			 * Try normal match first (uppercase == 0),
682 			 * then, then if it's a TRIPLE option,
683 			 * try uppercase match (uppercase == 1).
684 			 */
685 			for (uppercase = 0;  uppercase <= 1;  uppercase++)
686 			{
687 				len = sprefix(optname, oname->oname, uppercase);
688 				if (len <= 0 || is_optchar(optname[len]))
689 				{
690 					/*
691 					 * We didn't use all of the option name.
692 					 */
693 					continue;
694 				}
695 				if (!exact && len == maxlen)
696 					/*
697 					 * Already had a partial match,
698 					 * and now there's another one that
699 					 * matches the same length.
700 					 */
701 					ambig = 1;
702 				else if (len > maxlen)
703 				{
704 					/*
705 					 * Found a better match than
706 					 * the one we had.
707 					 */
708 					maxo = o;
709 					maxoname = oname;
710 					maxlen = len;
711 					ambig = 0;
712 					exact = (len == (int)strlen(oname->oname));
713 				}
714 				if (!(o->otype & TRIPLE))
715 					break;
716 			}
717 		}
718 	}
719 	if (ambig)
720 	{
721 		/*
722 		 * Name matched more than one option.
723 		 */
724 		if (p_err != NULL)
725 			*p_err = OPT_AMBIG;
726 		return (NULL);
727 	}
728 	*p_optname = optname + maxlen;
729 	if (p_oname != NULL)
730 		*p_oname = maxoname == NULL ? NULL : maxoname->oname;
731 	return (maxo);
732 }
733