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