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