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