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