xref: /openbsd-src/usr.bin/vi/common/options_f.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*	$OpenBSD: options_f.c,v 1.7 2009/10/27 23:59:47 deraadt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1993, 1994, 1995, 1996
7  *	Keith Bostic.  All rights reserved.
8  *
9  * See the LICENSE file for redistribution information.
10  */
11 
12 #include "config.h"
13 
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #include <sys/stat.h>
17 
18 #include <bitstring.h>
19 #include <ctype.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #include "common.h"
28 
29 /*
30  * PUBLIC: int f_altwerase(SCR *, OPTION *, char *, u_long *);
31  */
32 int
33 f_altwerase(sp, op, str, valp)
34 	SCR *sp;
35 	OPTION *op;
36 	char *str;
37 	u_long *valp;
38 {
39 	if (!*valp)
40 		O_CLR(sp, O_TTYWERASE);
41 	return (0);
42 }
43 
44 /*
45  * PUBLIC: int f_columns(SCR *, OPTION *, char *, u_long *);
46  */
47 int
48 f_columns(sp, op, str, valp)
49 	SCR *sp;
50 	OPTION *op;
51 	char *str;
52 	u_long *valp;
53 {
54 	/* Validate the number. */
55 	if (*valp < MINIMUM_SCREEN_COLS) {
56 		msgq(sp, M_ERR, "040|Screen columns too small, less than %d",
57 		    MINIMUM_SCREEN_COLS);
58 		return (1);
59 	}
60 
61 	/*
62 	 * !!!
63 	 * It's not uncommon for allocation of huge chunks of memory to cause
64 	 * core dumps on various systems.  So, we prune out numbers that are
65 	 * "obviously" wrong.  Vi will not work correctly if it has the wrong
66 	 * number of lines/columns for the screen, but at least we don't drop
67 	 * core.
68 	 */
69 #define	MAXIMUM_SCREEN_COLS	500
70 	if (*valp > MAXIMUM_SCREEN_COLS) {
71 		msgq(sp, M_ERR, "041|Screen columns too large, greater than %d",
72 		    MAXIMUM_SCREEN_COLS);
73 		return (1);
74 	}
75 	return (0);
76 }
77 
78 /*
79  * PUBLIC: int f_lines(SCR *, OPTION *, char *, u_long *);
80  */
81 int
82 f_lines(sp, op, str, valp)
83 	SCR *sp;
84 	OPTION *op;
85 	char *str;
86 	u_long *valp;
87 {
88 	/* Validate the number. */
89 	if (*valp < MINIMUM_SCREEN_ROWS) {
90 		msgq(sp, M_ERR, "042|Screen lines too small, less than %d",
91 		    MINIMUM_SCREEN_ROWS);
92 		return (1);
93 	}
94 
95 	/*
96 	 * !!!
97 	 * It's not uncommon for allocation of huge chunks of memory to cause
98 	 * core dumps on various systems.  So, we prune out numbers that are
99 	 * "obviously" wrong.  Vi will not work correctly if it has the wrong
100 	 * number of lines/columns for the screen, but at least we don't drop
101 	 * core.
102 	 */
103 #define	MAXIMUM_SCREEN_ROWS	500
104 	if (*valp > MAXIMUM_SCREEN_ROWS) {
105 		msgq(sp, M_ERR, "043|Screen lines too large, greater than %d",
106 		    MAXIMUM_SCREEN_ROWS);
107 		return (1);
108 	}
109 
110 	/*
111 	 * Set the value, and the related scroll value.  If no window
112 	 * value set, set a new default window.
113 	 */
114 	o_set(sp, O_LINES, 0, NULL, *valp);
115 	if (*valp == 1) {
116 		sp->defscroll = 1;
117 
118 		if (O_VAL(sp, O_WINDOW) == O_D_VAL(sp, O_WINDOW) ||
119 		    O_VAL(sp, O_WINDOW) > *valp) {
120 			o_set(sp, O_WINDOW, 0, NULL, 1);
121 			o_set(sp, O_WINDOW, OS_DEF, NULL, 1);
122 		}
123 	} else {
124 		sp->defscroll = (*valp - 1) / 2;
125 
126 		if (O_VAL(sp, O_WINDOW) == O_D_VAL(sp, O_WINDOW) ||
127 		    O_VAL(sp, O_WINDOW) > *valp) {
128 			o_set(sp, O_WINDOW, 0, NULL, *valp - 1);
129 			o_set(sp, O_WINDOW, OS_DEF, NULL, *valp - 1);
130 		}
131 	}
132 	return (0);
133 }
134 
135 /*
136  * PUBLIC: int f_lisp(SCR *, OPTION *, char *, u_long *);
137  */
138 int
139 f_lisp(sp, op, str, valp)
140 	SCR *sp;
141 	OPTION *op;
142 	char *str;
143 	u_long *valp;
144 {
145 	msgq(sp, M_ERR, "044|The lisp option is not implemented");
146 	return (0);
147 }
148 
149 /*
150  * PUBLIC: int f_msgcat(SCR *, OPTION *, char *, u_long *);
151  */
152 int
153 f_msgcat(sp, op, str, valp)
154 	SCR *sp;
155 	OPTION *op;
156 	char *str;
157 	u_long *valp;
158 {
159 	(void)msg_open(sp, str);
160 	return (0);
161 }
162 
163 /*
164  * PUBLIC: int f_paragraph(SCR *, OPTION *, char *, u_long *);
165  */
166 int
167 f_paragraph(sp, op, str, valp)
168 	SCR *sp;
169 	OPTION *op;
170 	char *str;
171 	u_long *valp;
172 {
173 	if (strlen(str) & 1) {
174 		msgq(sp, M_ERR,
175 		    "048|The paragraph option must be in two character groups");
176 		return (1);
177 	}
178 	return (0);
179 }
180 
181 /*
182  * PUBLIC: int f_print(SCR *, OPTION *, char *, u_long *);
183  */
184 int
185 f_print(sp, op, str, valp)
186 	SCR *sp;
187 	OPTION *op;
188 	char *str;
189 	u_long *valp;
190 {
191 	/* Reinitialize the key fast lookup table. */
192 	v_key_ilookup(sp);
193 
194 	/* Reformat the screen. */
195 	F_SET(sp, SC_SCR_REFORMAT);
196 	return (0);
197 }
198 
199 /*
200  * PUBLIC: int f_readonly(SCR *, OPTION *, char *, u_long *);
201  */
202 int
203 f_readonly(sp, op, str, valp)
204 	SCR *sp;
205 	OPTION *op;
206 	char *str;
207 	u_long *valp;
208 {
209 	/*
210 	 * !!!
211 	 * See the comment in exf.c.
212 	 */
213 	if (*valp)
214 		F_CLR(sp, SC_READONLY);
215 	else
216 		F_SET(sp, SC_READONLY);
217 	return (0);
218 }
219 
220 /*
221  * PUBLIC: int f_recompile(SCR *, OPTION *, char *, u_long *);
222  */
223 int
224 f_recompile(sp, op, str, valp)
225 	SCR *sp;
226 	OPTION *op;
227 	char *str;
228 	u_long *valp;
229 {
230 	if (F_ISSET(sp, SC_RE_SEARCH)) {
231 		regfree(&sp->re_c);
232 		F_CLR(sp, SC_RE_SEARCH);
233 	}
234 	if (F_ISSET(sp, SC_RE_SUBST)) {
235 		regfree(&sp->subre_c);
236 		F_CLR(sp, SC_RE_SUBST);
237 	}
238 	return (0);
239 }
240 
241 /*
242  * PUBLIC: int f_reformat(SCR *, OPTION *, char *, u_long *);
243  */
244 int
245 f_reformat(sp, op, str, valp)
246 	SCR *sp;
247 	OPTION *op;
248 	char *str;
249 	u_long *valp;
250 {
251 	F_SET(sp, SC_SCR_REFORMAT);
252 	return (0);
253 }
254 
255 /*
256  * PUBLIC: int f_section(SCR *, OPTION *, char *, u_long *);
257  */
258 int
259 f_section(sp, op, str, valp)
260 	SCR *sp;
261 	OPTION *op;
262 	char *str;
263 	u_long *valp;
264 {
265 	if (strlen(str) & 1) {
266 		msgq(sp, M_ERR,
267 		    "049|The section option must be in two character groups");
268 		return (1);
269 	}
270 	return (0);
271 }
272 
273 /*
274  * PUBLIC: int f_ttywerase(SCR *, OPTION *, char *, u_long *);
275  */
276 int
277 f_ttywerase(sp, op, str, valp)
278 	SCR *sp;
279 	OPTION *op;
280 	char *str;
281 	u_long *valp;
282 {
283 	if (!*valp)
284 		O_CLR(sp, O_ALTWERASE);
285 	return (0);
286 }
287 
288 /*
289  * PUBLIC: int f_w300(SCR *, OPTION *, char *, u_long *);
290  */
291 int
292 f_w300(sp, op, str, valp)
293 	SCR *sp;
294 	OPTION *op;
295 	char *str;
296 	u_long *valp;
297 {
298 	u_long v;
299 
300 	/* Historical behavior for w300 was < 1200. */
301 	if (sp->gp->scr_baud(sp, &v))
302 		return (1);
303 	if (v >= 1200)
304 		return (0);
305 
306 	return (f_window(sp, op, str, valp));
307 }
308 
309 /*
310  * PUBLIC: int f_w1200(SCR *, OPTION *, char *, u_long *);
311  */
312 int
313 f_w1200(sp, op, str, valp)
314 	SCR *sp;
315 	OPTION *op;
316 	char *str;
317 	u_long *valp;
318 {
319 	u_long v;
320 
321 	/* Historical behavior for w1200 was == 1200. */
322 	if (sp->gp->scr_baud(sp, &v))
323 		return (1);
324 	if (v < 1200 || v > 4800)
325 		return (0);
326 
327 	return (f_window(sp, op, str, valp));
328 }
329 
330 /*
331  * PUBLIC: int f_w9600(SCR *, OPTION *, char *, u_long *);
332  */
333 int
334 f_w9600(sp, op, str, valp)
335 	SCR *sp;
336 	OPTION *op;
337 	char *str;
338 	u_long *valp;
339 {
340 	u_long v;
341 
342 	/* Historical behavior for w9600 was > 1200. */
343 	if (sp->gp->scr_baud(sp, &v))
344 		return (1);
345 	if (v <= 4800)
346 		return (0);
347 
348 	return (f_window(sp, op, str, valp));
349 }
350 
351 /*
352  * PUBLIC: int f_window(SCR *, OPTION *, char *, u_long *);
353  */
354 int
355 f_window(sp, op, str, valp)
356 	SCR *sp;
357 	OPTION *op;
358 	char *str;
359 	u_long *valp;
360 {
361 	if (*valp >= O_VAL(sp, O_LINES) - 1 &&
362 	    (*valp = O_VAL(sp, O_LINES) - 1) == 0)
363 		*valp = 1;
364 	return (0);
365 }
366