1 /*
2  * Copyright (c) 1984,1985,1989,1994,1995  Mark Nudelman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice in the documentation and/or other materials provided with
12  *    the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 
28 /*
29  * The option table.
30  */
31 
32 #include "less.h"
33 #include "option.h"
34 
35 /*
36  * Variables controlled by command line options.
37  */
38 public int quiet;		/* Should we suppress the audible bell? */
39 public int how_search;		/* Where should forward searches start? */
40 public int top_scroll;		/* Repaint screen from top?
41 				   (alternative is scroll from bottom) */
42 public int pr_type;		/* Type of prompt (short, medium, long) */
43 public int bs_mode;		/* How to process backspaces */
44 public int know_dumb;		/* Don't complain about dumb terminals */
45 public int quit_at_eof;		/* Quit after hitting end of file twice */
46 public int squeeze;		/* Squeeze multiple blank lines into one */
47 public int tabstop;		/* Tab settings */
48 public int back_scroll;		/* Repaint screen on backwards movement */
49 public int forw_scroll;		/* Repaint screen on forward movement */
50 public int twiddle;		/* Display "~" for lines after EOF */
51 public int caseless;		/* Do "caseless" searches */
52 public int linenums;		/* Use line numbers */
53 public int cbufs;		/* Current number of buffers */
54 public int autobuf;		/* Automatically allocate buffers as needed */
55 public int nohelp;		/* Disable the HELP command */
56 public int ctldisp;		/* Send control chars to screen untranslated */
57 public int force_open;		/* Open the file even if not regular file */
58 public int swindow;		/* Size of scrolling window */
59 public int jump_sline;		/* Screen line of "jump target" */
60 public int chopline;		/* Truncate displayed lines at screen width */
61 public int no_init;		/* Disable sending ti/te termcap strings */
62 #if HILITE_SEARCH
63 public int hilite_search;	/* Highlight matched search patterns? */
64 #endif
65 
66 /*
67  * Table of all options and their semantics.
68  */
69 static struct option option[] =
70 {
71 	{ 'a', BOOL, OPT_OFF, &how_search, NULL,
72 		"Search includes displayed screen",
73 		"Search skips displayed screen",
74 		NULL
75 	},
76 	{ 'b', NUMBER, 10, &cbufs, opt_b,
77 		"Buffers: ",
78 		"%d buffers",
79 		NULL
80 	},
81 	{ 'B', BOOL, OPT_ON, &autobuf, NULL,
82 		"Don't automatically allocate buffers",
83 		"Automatically allocate buffers when needed",
84 		NULL
85 	},
86 	{ 'c', TRIPLE, OPT_OFF, &top_scroll, NULL,
87 		"Repaint by scrolling from bottom of screen",
88 		"Repaint by clearing each line",
89 		"Repaint by painting from top of screen"
90 	},
91 	{ 'd', BOOL|NO_TOGGLE, OPT_OFF, &know_dumb, NULL,
92 		"Assume intelligent terminal",
93 		"Assume dumb terminal",
94 		NULL
95 	},
96 #if MSOFTC
97 	{ 'D', STRING|REPAINT, 0, NULL, opt_D,
98 		"color desc: ", NULL, NULL
99 	},
100 #endif
101 	{ 'e', TRIPLE, OPT_OFF, &quit_at_eof, NULL,
102 		"Don't quit at end-of-file",
103 		"Quit at end-of-file",
104 		"Quit immediately at end-of-file"
105 	},
106 	{ 'f', BOOL, OPT_OFF, &force_open, NULL,
107 		"Open only regular files",
108 		"Open even non-regular files",
109 		NULL
110 	},
111 #if HILITE_SEARCH
112 	{ 'g', TRIPLE|HL_REPAINT, OPT_ONPLUS, &hilite_search, NULL,
113 		"Don't highlight search matches",
114 		"Highlight matches for previous search only",
115 		"Highlight all matches for previous search pattern",
116 	},
117 #endif
118 	{ 'h', NUMBER, -1, &back_scroll, NULL,
119 		"Backwards scroll limit: ",
120 		"Backwards scroll limit is %d lines",
121 		NULL
122 	},
123 	{ 'H', BOOL|NO_TOGGLE, OPT_OFF, &nohelp, NULL,
124 		"Allow help command",
125 		"Don't allow help command",
126 		NULL
127 	},
128 	{ 'i', TRIPLE|HL_REPAINT, OPT_OFF, &caseless, opt_i,
129 		"Case is significant in searches",
130 		"Ignore case in searches",
131 		"Ignore case in searches and in patterns"
132 	},
133 	{ 'j', NUMBER, 1, &jump_sline, NULL,
134 		"Target line: ",
135 		"Position target at screen line %d",
136 		NULL
137 	},
138 #if USERFILE
139 	{ 'k', STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_k,
140 		NULL, NULL, NULL
141 	},
142 #endif
143 	{ 'l', STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_l,
144 		NULL, NULL, NULL
145 	},
146 	{ 'm', TRIPLE, OPT_OFF, &pr_type, NULL,
147 		"Short prompt",
148 		"Medium prompt",
149 		"Long prompt"
150 	},
151 	{ 'n', TRIPLE|REPAINT, OPT_ON, &linenums, NULL,
152 		"Don't use line numbers",
153 		"Use line numbers",
154 		"Constantly display line numbers"
155 	},
156 #if LOGFILE
157 	{ 'o', STRING, 0, NULL, opt_o,
158 		"log file: ", NULL, NULL
159 	},
160 	{ 'O', STRING, 0, NULL, opt__O,
161 		"Log file: ", NULL, NULL
162 	},
163 #endif
164 	{ 'p', STRING|NO_TOGGLE|NO_QUERY, 0, NULL, opt_p,
165 		NULL, NULL, NULL
166 	},
167 	{ 'P', STRING, 0, NULL, opt__P,
168 		"prompt: ", NULL, NULL
169 	},
170 	{ 'q', TRIPLE, OPT_OFF, &quiet, NULL,
171 		"Ring the bell for errors AND at eof/bof",
172 		"Ring the bell for errors but not at eof/bof",
173 		"Never ring the bell"
174 	},
175 	{ 'r', BOOL|REPAINT, OPT_ON, &ctldisp, NULL,
176 		"Display control characters directly",
177 		"Display control characters as ^X",
178 		NULL
179 	},
180 	{ 's', BOOL|REPAINT, OPT_OFF, &squeeze, NULL,
181 		"Display all blank lines",
182 		"Squeeze multiple blank lines",
183 		NULL
184 	},
185 	{ 'S', BOOL|REPAINT, OPT_OFF, &chopline, NULL,
186 		"Fold long lines",
187 		"Chop long lines",
188 		NULL
189 	},
190 #if TAGS
191 	{ 't', STRING|NO_QUERY, 0, NULL, opt_t,
192 		"tag: ", NULL, NULL
193 	},
194 	{ 'T', STRING, 0, NULL, opt__T,
195 		"tags file: ", NULL, NULL
196 	},
197 #endif
198 	{ 'u', TRIPLE|REPAINT, OPT_OFF, &bs_mode, NULL,
199 		"Display underlined text in underline mode",
200 		"Backspaces cause overstrike",
201 		"Print backspace as ^H"
202 	},
203 	{ 'V', NOVAR, 0, NULL, opt__V,
204 		NULL, NULL, NULL
205 	},
206 	{ 'w', BOOL|REPAINT, OPT_ON, &twiddle, NULL,
207 		"Display nothing for lines after end-of-file",
208 		"Display ~ for lines after end-of-file",
209 		NULL
210 	},
211 	{ 'x', NUMBER|REPAINT, 8, &tabstop, NULL,
212 		"Tab stops: ",
213 		"Tab stops every %d spaces",
214 		NULL
215 	},
216 	{ 'X', BOOL|NO_TOGGLE, OPT_OFF, &no_init, NULL,
217 		"Send init/deinit strings to terminal",
218 		"Don't use init/deinit strings",
219 		NULL
220 	},
221 	{ 'y', NUMBER, -1, &forw_scroll, NULL,
222 		"Forward scroll limit: ",
223 		"Forward scroll limit is %d lines",
224 		NULL
225 	},
226 	{ 'z', NUMBER, -1, &swindow, NULL,
227 		"Scroll window size: ",
228 		"Scroll window size is %d lines",
229 		NULL
230 	},
231 	{ '?', NOVAR, 0, NULL, opt_query,
232 		NULL, NULL, NULL
233 	},
234 	{ '\0' }
235 };
236 
237 
238 /*
239  * Initialize each option to its default value.
240  */
241 	public void
init_option()242 init_option()
243 {
244 	register struct option *o;
245 
246 	for (o = option;  o->oletter != '\0';  o++)
247 	{
248 		/*
249 		 * Set each variable to its default.
250 		 */
251 		if (o->ovar != NULL)
252 			*(o->ovar) = o->odefault;
253 	}
254 }
255 
256 /*
257  * Find an option in the option table.
258  */
259 	public struct option *
findopt(c)260 findopt(c)
261 	int c;
262 {
263 	register struct option *o;
264 
265 	for (o = option;  o->oletter != '\0';  o++)
266 	{
267 		if (o->oletter == c)
268 			return (o);
269 		if ((o->otype & TRIPLE) && toupper(o->oletter) == c)
270 			return (o);
271 	}
272 	return (NULL);
273 }
274