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