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