Add our READMEs.
[dragonfly/vkernel-mp.git] / contrib / less-403 / opttbl.c
blob2514463ea6e9697e03f3ff501594bfa2334bc6e0
1 /*
2 * Copyright (C) 1984-2007 Mark Nudelman
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.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * The option table.
16 #include "less.h"
17 #include "option.h"
20 * Variables controlled by command line options.
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 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 oldbot; /* Old bottom of screen behavior */
54 #if HILITE_SEARCH
55 public int hilite_search; /* Highlight matched search patterns? */
56 #endif
58 public int less_is_more = 0; /* Make compatible with POSIX more */
61 * Long option names.
63 static struct optname a_optname = { "search-skip-screen", NULL };
64 static struct optname b_optname = { "buffers", NULL };
65 static struct optname B__optname = { "auto-buffers", NULL };
66 static struct optname c_optname = { "clear-screen", NULL };
67 static struct optname d_optname = { "dumb", NULL };
68 #if MSDOS_COMPILER
69 static struct optname D__optname = { "color", NULL };
70 #endif
71 static struct optname e_optname = { "quit-at-eof", NULL };
72 static struct optname f_optname = { "force", NULL };
73 static struct optname F__optname = { "quit-if-one-screen", NULL };
74 #if HILITE_SEARCH
75 static struct optname g_optname = { "hilite-search", NULL };
76 #endif
77 static struct optname h_optname = { "max-back-scroll", NULL };
78 static struct optname i_optname = { "ignore-case", NULL };
79 static struct optname j_optname = { "jump-target", NULL };
80 static struct optname J__optname = { "status-column", NULL };
81 #if USERFILE
82 static struct optname k_optname = { "lesskey-file", NULL };
83 #endif
84 static struct optname K__optname = { "quit-on-intr", NULL };
85 static struct optname L__optname = { "no-lessopen", NULL };
86 static struct optname m_optname = { "long-prompt", NULL };
87 static struct optname n_optname = { "line-numbers", NULL };
88 #if LOGFILE
89 static struct optname o_optname = { "log-file", NULL };
90 static struct optname O__optname = { "LOG-FILE", NULL };
91 #endif
92 static struct optname p_optname = { "pattern", NULL };
93 static struct optname P__optname = { "prompt", NULL };
94 static struct optname q2_optname = { "silent", NULL };
95 static struct optname q_optname = { "quiet", &q2_optname };
96 static struct optname r_optname = { "raw-control-chars", NULL };
97 static struct optname s_optname = { "squeeze-blank-lines", NULL };
98 static struct optname S__optname = { "chop-long-lines", NULL };
99 #if TAGS
100 static struct optname t_optname = { "tag", NULL };
101 static struct optname T__optname = { "tag-file", NULL };
102 #endif
103 static struct optname u_optname = { "underline-special", NULL };
104 static struct optname V__optname = { "version", NULL };
105 static struct optname w_optname = { "hilite-unread", NULL };
106 static struct optname x_optname = { "tabs", NULL };
107 static struct optname X__optname = { "no-init", NULL };
108 static struct optname y_optname = { "max-forw-scroll", NULL };
109 static struct optname z_optname = { "window", NULL };
110 static struct optname quote_optname = { "quotes", NULL };
111 static struct optname tilde_optname = { "tilde", NULL };
112 static struct optname query_optname = { "help", NULL };
113 static struct optname pound_optname = { "shift", NULL };
114 static struct optname keypad_optname = { "no-keypad", NULL };
115 static struct optname oldbot_optname = { "old-bot", NULL };
119 * Table of all options and their semantics.
121 * For BOOL and TRIPLE options, odesc[0], odesc[1], odesc[2] are
122 * the description of the option when set to 0, 1 or 2, respectively.
123 * For NUMBER options, odesc[0] is the prompt to use when entering
124 * a new value, and odesc[1] is the description, which should contain
125 * one %d which is replaced by the value of the number.
126 * For STRING options, odesc[0] is the prompt to use when entering
127 * a new value, and odesc[1], if not NULL, is the set of characters
128 * that are valid in the string.
130 static struct loption option[] =
132 { 'a', &a_optname,
133 BOOL, OPT_OFF, &how_search, NULL,
135 "Search includes displayed screen",
136 "Search skips displayed screen",
137 NULL
141 { 'b', &b_optname,
142 NUMBER|INIT_HANDLER, 64, &bufspace, opt_b,
144 "Max buffer space per file (K): ",
145 "Max buffer space per file: %dK",
146 NULL
149 { 'B', &B__optname,
150 BOOL, OPT_ON, &autobuf, NULL,
152 "Don't automatically allocate buffers",
153 "Automatically allocate buffers when needed",
154 NULL
157 { 'c', &c_optname,
158 TRIPLE, OPT_OFF, &top_scroll, NULL,
160 "Repaint by scrolling from bottom of screen",
161 "Repaint by painting from top of screen",
162 "Repaint by painting from top of screen"
165 { 'd', &d_optname,
166 BOOL|NO_TOGGLE, OPT_OFF, &know_dumb, NULL,
168 "Assume intelligent terminal",
169 "Assume dumb terminal",
170 NULL
173 #if MSDOS_COMPILER
174 { 'D', &D__optname,
175 STRING|REPAINT|NO_QUERY, 0, NULL, opt_D,
177 "color desc: ",
178 "Ddknsu0123456789.",
179 NULL
182 #endif
183 { 'e', &e_optname,
184 TRIPLE, OPT_OFF, &quit_at_eof, NULL,
186 "Don't quit at end-of-file",
187 "Quit at end-of-file",
188 "Quit immediately at end-of-file"
191 { 'f', &f_optname,
192 BOOL, OPT_OFF, &force_open, NULL,
194 "Open only regular files",
195 "Open even non-regular files",
196 NULL
199 { 'F', &F__optname,
200 BOOL, OPT_OFF, &quit_if_one_screen, NULL,
202 "Don't quit if end-of-file on first screen",
203 "Quit if end-of-file on first screen",
204 NULL
207 #if HILITE_SEARCH
208 { 'g', &g_optname,
209 TRIPLE|HL_REPAINT, OPT_ONPLUS, &hilite_search, NULL,
211 "Don't highlight search matches",
212 "Highlight matches for previous search only",
213 "Highlight all matches for previous search pattern",
216 #endif
217 { 'h', &h_optname,
218 NUMBER, -1, &back_scroll, NULL,
220 "Backwards scroll limit: ",
221 "Backwards scroll limit is %d lines",
222 NULL
225 { 'i', &i_optname,
226 TRIPLE|HL_REPAINT, OPT_OFF, &caseless, opt_i,
228 "Case is significant in searches",
229 "Ignore case in searches",
230 "Ignore case in searches and in patterns"
233 { 'j', &j_optname,
234 STRING, 0, NULL, opt_j,
236 "Target line: ",
237 "0123456789.",
238 NULL
241 { 'J', &J__optname,
242 BOOL|REPAINT, OPT_OFF, &status_col, NULL,
244 "Don't display a status column",
245 "Display a status column",
246 NULL
249 #if USERFILE
250 { 'k', &k_optname,
251 STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k,
252 { NULL, NULL, NULL }
254 #endif
255 { 'K', &K__optname,
256 BOOL, OPT_OFF, &quit_on_intr, NULL,
258 "Interrupt (ctrl-C) returns to prompt",
259 "Interrupt (ctrl-C) exits less",
260 NULL
263 { 'l', NULL,
264 STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_l,
265 { NULL, NULL, NULL }
267 { 'L', &L__optname,
268 BOOL, OPT_ON, &use_lessopen, NULL,
270 "Don't use the LESSOPEN filter",
271 "Use the LESSOPEN filter",
272 NULL
275 { 'm', &m_optname,
276 TRIPLE, OPT_OFF, &pr_type, NULL,
278 "Short prompt",
279 "Medium prompt",
280 "Long prompt"
283 { 'n', &n_optname,
284 TRIPLE|REPAINT, OPT_ON, &linenums, NULL,
286 "Don't use line numbers",
287 "Use line numbers",
288 "Constantly display line numbers"
291 #if LOGFILE
292 { 'o', &o_optname,
293 STRING, 0, NULL, opt_o,
294 { "log file: ", NULL, NULL }
296 { 'O', &O__optname,
297 STRING, 0, NULL, opt__O,
298 { "Log file: ", NULL, NULL }
300 #endif
301 { 'p', &p_optname,
302 STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_p,
303 { NULL, NULL, NULL }
305 { 'P', &P__optname,
306 STRING, 0, NULL, opt__P,
307 { "prompt: ", NULL, NULL }
309 { 'q', &q_optname,
310 TRIPLE, OPT_OFF, &quiet, NULL,
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"
317 { 'r', &r_optname,
318 TRIPLE|REPAINT, OPT_OFF, &ctldisp, NULL,
320 "Display control characters as ^X",
321 "Display control characters directly",
322 "Display control characters directly, processing ANSI sequences"
325 { 's', &s_optname,
326 BOOL|REPAINT, OPT_OFF, &squeeze, NULL,
328 "Display all blank lines",
329 "Squeeze multiple blank lines",
330 NULL
333 { 'S', &S__optname,
334 BOOL|REPAINT, OPT_OFF, &chopline, NULL,
336 "Fold long lines",
337 "Chop long lines",
338 NULL
341 #if TAGS
342 { 't', &t_optname,
343 STRING|NO_QUERY, 0, NULL, opt_t,
344 { "tag: ", NULL, NULL }
346 { 'T', &T__optname,
347 STRING, 0, NULL, opt__T,
348 { "tags file: ", NULL, NULL }
350 #endif
351 { 'u', &u_optname,
352 TRIPLE|REPAINT, OPT_OFF, &bs_mode, NULL,
354 "Display underlined text in underline mode",
355 "Backspaces cause overstrike",
356 "Print backspace as ^H"
359 { 'V', &V__optname,
360 NOVAR, 0, NULL, opt__V,
361 { NULL, NULL, NULL }
363 { 'w', &w_optname,
364 TRIPLE|REPAINT, OPT_OFF, &show_attn, NULL,
366 "Don't highlight first unread line",
367 "Highlight first unread line after forward-screen",
368 "Highlight first unread line after any forward movement",
371 { 'x', &x_optname,
372 STRING|REPAINT, 0, NULL, opt_x,
374 "Tab stops: ",
375 "0123456789,",
376 NULL
379 { 'X', &X__optname,
380 BOOL|NO_TOGGLE, OPT_OFF, &no_init, NULL,
382 "Send init/deinit strings to terminal",
383 "Don't use init/deinit strings",
384 NULL
387 { 'y', &y_optname,
388 NUMBER, -1, &forw_scroll, NULL,
390 "Forward scroll limit: ",
391 "Forward scroll limit is %d lines",
392 NULL
395 { 'z', &z_optname,
396 NUMBER, -1, &swindow, NULL,
398 "Scroll window size: ",
399 "Scroll window size is %d lines",
400 NULL
403 { '"', &quote_optname,
404 STRING, 0, NULL, opt_quote,
405 { "quotes: ", NULL, NULL }
407 { '~', &tilde_optname,
408 BOOL|REPAINT, OPT_ON, &twiddle, NULL,
410 "Don't show tildes after end of file",
411 "Show tildes after end of file",
412 NULL
415 { '?', &query_optname,
416 NOVAR, 0, NULL, opt_query,
417 { NULL, NULL, NULL }
419 { '#', &pound_optname,
420 NUMBER, 0, &shift_count, NULL,
422 "Horizontal shift: ",
423 "Horizontal shift %d positions",
424 NULL
427 { '.', &keypad_optname,
428 BOOL|NO_TOGGLE, OPT_OFF, &no_keypad, NULL,
430 "Use keypad mode",
431 "Don't use keypad mode",
432 NULL
435 { '.', &oldbot_optname,
436 BOOL, OPT_OFF, &oldbot, NULL,
438 "Use new bottom of screen behavior",
439 "Use old bottom of screen behavior",
440 NULL
443 { '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
448 * Initialize each option to its default value.
450 public void
451 init_option()
453 register struct loption *o;
454 char *p;
456 p = lgetenv("LESS_IS_MORE");
457 if (p != NULL && *p != '\0')
458 less_is_more = 1;
460 for (o = option; o->oletter != '\0'; o++)
463 * Set each variable to its default.
465 if (o->ovar != NULL)
466 *(o->ovar) = o->odefault;
467 if (o->otype & INIT_HANDLER)
468 (*(o->ofunc))(INIT, (char *) NULL);
473 * Find an option in the option table, given its option letter.
475 public struct loption *
476 findopt(c)
477 int c;
479 register struct loption *o;
481 for (o = option; o->oletter != '\0'; o++)
483 if (o->oletter == c)
484 return (o);
485 if ((o->otype & TRIPLE) && ASCII_TO_UPPER(o->oletter) == c)
486 return (o);
488 return (NULL);
494 static int
495 is_optchar(c)
496 char c;
498 if (ASCII_IS_UPPER(c))
499 return 1;
500 if (ASCII_IS_LOWER(c))
501 return 1;
502 if (c == '-')
503 return 1;
504 return 0;
508 * Find an option in the option table, given its option name.
509 * p_optname is the (possibly partial) name to look for, and
510 * is updated to point after the matched name.
511 * p_oname if non-NULL is set to point to the full option name.
513 public struct loption *
514 findopt_name(p_optname, p_oname, p_err)
515 char **p_optname;
516 char **p_oname;
517 int *p_err;
519 char *optname = *p_optname;
520 register struct loption *o;
521 register struct optname *oname;
522 register int len;
523 int uppercase;
524 struct loption *maxo = NULL;
525 struct optname *maxoname = NULL;
526 int maxlen = 0;
527 int ambig = 0;
528 int exact = 0;
531 * Check all options.
533 for (o = option; o->oletter != '\0'; o++)
536 * Check all names for this option.
538 for (oname = o->onames; oname != NULL; oname = oname->onext)
541 * Try normal match first (uppercase == 0),
542 * then, then if it's a TRIPLE option,
543 * try uppercase match (uppercase == 1).
545 for (uppercase = 0; uppercase <= 1; uppercase++)
547 len = sprefix(optname, oname->oname, uppercase);
548 if (len <= 0 || is_optchar(optname[len]))
551 * We didn't use all of the option name.
553 continue;
555 if (!exact && len == maxlen)
557 * Already had a partial match,
558 * and now there's another one that
559 * matches the same length.
561 ambig = 1;
562 else if (len > maxlen)
565 * Found a better match than
566 * the one we had.
568 maxo = o;
569 maxoname = oname;
570 maxlen = len;
571 ambig = 0;
572 exact = (len == (int)strlen(oname->oname));
574 if (!(o->otype & TRIPLE))
575 break;
579 if (ambig)
582 * Name matched more than one option.
584 if (p_err != NULL)
585 *p_err = OPT_AMBIG;
586 return (NULL);
588 *p_optname = optname + maxlen;
589 if (p_oname != NULL)
590 *p_oname = maxoname == NULL ? NULL : maxoname->oname;
591 return (maxo);