2 * Copyright (C) 1984-2009 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.
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 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}} */
57 public int hilite_search
; /* Highlight matched search patterns? */
60 public int less_is_more
= 0; /* Make compatible with POSIX more */
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
};
71 static struct optname D__optname
= { "color", NULL
};
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
};
77 static struct optname g_optname
= { "hilite-search", NULL
};
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
};
84 static struct optname k_optname
= { "lesskey-file", NULL
};
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
};
91 static struct optname o_optname
= { "log-file", NULL
};
92 static struct optname O__optname
= { "LOG-FILE", NULL
};
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
};
102 static struct optname t_optname
= { "tag", NULL
};
103 static struct optname T__optname
= { "tag-file", NULL
};
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
};
122 * Table of all options and their semantics.
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.
133 static struct loption option
[] =
136 BOOL
, OPT_OFF
, &how_search
, NULL
,
138 "Search includes displayed screen",
139 "Search skips displayed screen",
145 NUMBER
|INIT_HANDLER
, 64, &bufspace
, opt_b
,
147 "Max buffer space per file (K): ",
148 "Max buffer space per file: %dK",
153 BOOL
, OPT_ON
, &autobuf
, NULL
,
155 "Don't automatically allocate buffers",
156 "Automatically allocate buffers when needed",
161 TRIPLE
, OPT_OFF
, &top_scroll
, NULL
,
163 "Repaint by scrolling from bottom of screen",
164 "Repaint by painting from top of screen",
165 "Repaint by painting from top of screen"
169 BOOL
|NO_TOGGLE
, OPT_OFF
, &know_dumb
, NULL
,
171 "Assume intelligent terminal",
172 "Assume dumb terminal",
178 STRING
|REPAINT
|NO_QUERY
, 0, NULL
, opt_D
,
187 TRIPLE
, OPT_OFF
, &quit_at_eof
, NULL
,
189 "Don't quit at end-of-file",
190 "Quit at end-of-file",
191 "Quit immediately at end-of-file"
195 BOOL
, OPT_OFF
, &force_open
, NULL
,
197 "Open only regular files",
198 "Open even non-regular files",
203 BOOL
, OPT_OFF
, &quit_if_one_screen
, NULL
,
205 "Don't quit if end-of-file on first screen",
206 "Quit if end-of-file on first screen",
212 TRIPLE
|HL_REPAINT
, OPT_ONPLUS
, &hilite_search
, NULL
,
214 "Don't highlight search matches",
215 "Highlight matches for previous search only",
216 "Highlight all matches for previous search pattern",
221 NUMBER
, -1, &back_scroll
, NULL
,
223 "Backwards scroll limit: ",
224 "Backwards scroll limit is %d lines",
229 TRIPLE
|HL_REPAINT
, OPT_OFF
, &caseless
, opt_i
,
231 "Case is significant in searches",
232 "Ignore case in searches",
233 "Ignore case in searches and in patterns"
237 STRING
, 0, NULL
, opt_j
,
245 BOOL
|REPAINT
, OPT_OFF
, &status_col
, NULL
,
247 "Don't display a status column",
248 "Display a status column",
254 STRING
|NO_TOGGLE
|NO_QUERY
, 0, NULL
, opt_k
,
259 BOOL
, OPT_OFF
, &quit_on_intr
, NULL
,
261 "Interrupt (ctrl-C) returns to prompt",
262 "Interrupt (ctrl-C) exits less",
267 STRING
|NO_TOGGLE
|NO_QUERY
, 0, NULL
, opt_l
,
271 BOOL
, OPT_ON
, &use_lessopen
, NULL
,
273 "Don't use the LESSOPEN filter",
274 "Use the LESSOPEN filter",
279 TRIPLE
, OPT_OFF
, &pr_type
, NULL
,
287 TRIPLE
|REPAINT
, OPT_ON
, &linenums
, NULL
,
289 "Don't use line numbers",
291 "Constantly display line numbers"
296 STRING
, 0, NULL
, opt_o
,
297 { "log file: ", NULL
, NULL
}
300 STRING
, 0, NULL
, opt__O
,
301 { "Log file: ", NULL
, NULL
}
305 STRING
|NO_TOGGLE
|NO_QUERY
, 0, NULL
, opt_p
,
309 STRING
, 0, NULL
, opt__P
,
310 { "prompt: ", NULL
, NULL
}
313 TRIPLE
, OPT_OFF
, &quiet
, NULL
,
315 "Ring the bell for errors AND at eof/bof",
316 "Ring the bell for errors but not at eof/bof",
317 "Never ring the bell"
321 TRIPLE
|REPAINT
, OPT_OFF
, &ctldisp
, NULL
,
323 "Display control characters as ^X",
324 "Display control characters directly",
325 "Display control characters directly, processing ANSI sequences"
329 BOOL
|REPAINT
, OPT_OFF
, &squeeze
, NULL
,
331 "Display all blank lines",
332 "Squeeze multiple blank lines",
337 BOOL
|REPAINT
, OPT_OFF
, &chopline
, NULL
,
346 STRING
|NO_QUERY
, 0, NULL
, opt_t
,
347 { "tag: ", NULL
, NULL
}
350 STRING
, 0, NULL
, opt__T
,
351 { "tags file: ", NULL
, NULL
}
355 TRIPLE
|REPAINT
, OPT_OFF
, &bs_mode
, NULL
,
357 "Display underlined text in underline mode",
358 "Backspaces cause overstrike",
359 "Print backspace as ^H"
363 NOVAR
, 0, NULL
, opt__V
,
367 TRIPLE
|REPAINT
, OPT_OFF
, &show_attn
, NULL
,
369 "Don't highlight first unread line",
370 "Highlight first unread line after forward-screen",
371 "Highlight first unread line after any forward movement",
375 STRING
|REPAINT
, 0, NULL
, opt_x
,
383 BOOL
|NO_TOGGLE
, OPT_OFF
, &no_init
, NULL
,
385 "Send init/deinit strings to terminal",
386 "Don't use init/deinit strings",
391 NUMBER
, -1, &forw_scroll
, NULL
,
393 "Forward scroll limit: ",
394 "Forward scroll limit is %d lines",
399 NUMBER
, -1, &swindow
, NULL
,
401 "Scroll window size: ",
402 "Scroll window size is %d lines",
406 { '"', "e_optname
,
407 STRING
, 0, NULL
, opt_quote
,
408 { "quotes: ", NULL
, NULL
}
410 { '~', &tilde_optname
,
411 BOOL
|REPAINT
, OPT_ON
, &twiddle
, NULL
,
413 "Don't show tildes after end of file",
414 "Show tildes after end of file",
418 { '?', &query_optname
,
419 NOVAR
, 0, NULL
, opt_query
,
422 { '#', £_optname
,
423 STRING
, 0, NULL
, opt_shift
,
425 "Horizontal shift: ",
430 { '.', &keypad_optname
,
431 BOOL
|NO_TOGGLE
, OPT_OFF
, &no_keypad
, NULL
,
434 "Don't use keypad mode",
438 { '.', &oldbot_optname
,
439 BOOL
, OPT_OFF
, &oldbot
, NULL
,
441 "Use new bottom of screen behavior",
442 "Use old bottom of screen behavior",
446 { '.', &follow_optname
,
447 BOOL
, FOLLOW_DESC
, &follow_mode
, NULL
,
449 "F command Follows file descriptor",
450 "F command Follows file name",
454 { '\0', NULL
, NOVAR
, 0, NULL
, NULL
, { NULL
, NULL
, NULL
} }
459 * Initialize each option to its default value.
464 register struct loption
*o
;
467 p
= lgetenv("LESS_IS_MORE");
468 if (p
!= NULL
&& *p
!= '\0')
471 for (o
= option
; o
->oletter
!= '\0'; o
++)
474 * Set each variable to its default.
477 *(o
->ovar
) = o
->odefault
;
478 if (o
->otype
& INIT_HANDLER
)
479 (*(o
->ofunc
))(INIT
, (char *) NULL
);
484 * Find an option in the option table, given its option letter.
486 public struct loption
*
490 register struct loption
*o
;
492 for (o
= option
; o
->oletter
!= '\0'; o
++)
496 if ((o
->otype
& TRIPLE
) && ASCII_TO_UPPER(o
->oletter
) == c
)
509 if (ASCII_IS_UPPER(c
))
511 if (ASCII_IS_LOWER(c
))
519 * Find an option in the option table, given its option name.
520 * p_optname is the (possibly partial) name to look for, and
521 * is updated to point after the matched name.
522 * p_oname if non-NULL is set to point to the full option name.
524 public struct loption
*
525 findopt_name(p_optname
, p_oname
, p_err
)
530 char *optname
= *p_optname
;
531 register struct loption
*o
;
532 register struct optname
*oname
;
535 struct loption
*maxo
= NULL
;
536 struct optname
*maxoname
= NULL
;
544 for (o
= option
; o
->oletter
!= '\0'; o
++)
547 * Check all names for this option.
549 for (oname
= o
->onames
; oname
!= NULL
; oname
= oname
->onext
)
552 * Try normal match first (uppercase == 0),
553 * then, then if it's a TRIPLE option,
554 * try uppercase match (uppercase == 1).
556 for (uppercase
= 0; uppercase
<= 1; uppercase
++)
558 len
= sprefix(optname
, oname
->oname
, uppercase
);
559 if (len
<= 0 || is_optchar(optname
[len
]))
562 * We didn't use all of the option name.
566 if (!exact
&& len
== maxlen
)
568 * Already had a partial match,
569 * and now there's another one that
570 * matches the same length.
573 else if (len
> maxlen
)
576 * Found a better match than
583 exact
= (len
== (int)strlen(oname
->oname
));
585 if (!(o
->otype
& TRIPLE
))
593 * Name matched more than one option.
599 *p_optname
= optname
+ maxlen
;
601 *p_oname
= maxoname
== NULL
? NULL
: maxoname
->oname
;