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