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