xref: /openbsd/usr.bin/vi/common/options_f.c (revision fc61954a)
1 /*	$OpenBSD: options_f.c,v 1.11 2016/01/06 22:28:52 millert 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(SCR *sp, OPTION *op, char *str, u_long *valp)
34 {
35 	if (!*valp)
36 		O_CLR(sp, O_TTYWERASE);
37 	return (0);
38 }
39 
40 /*
41  * PUBLIC: int f_columns(SCR *, OPTION *, char *, u_long *);
42  */
43 int
44 f_columns(SCR *sp, OPTION *op, char *str, u_long *valp)
45 {
46 	/* Validate the number. */
47 	if (*valp < MINIMUM_SCREEN_COLS) {
48 		msgq(sp, M_ERR, "Screen columns too small, less than %d",
49 		    MINIMUM_SCREEN_COLS);
50 		return (1);
51 	}
52 
53 	/*
54 	 * !!!
55 	 * It's not uncommon for allocation of huge chunks of memory to cause
56 	 * core dumps on various systems.  So, we prune out numbers that are
57 	 * "obviously" wrong.  Vi will not work correctly if it has the wrong
58 	 * number of lines/columns for the screen, but at least we don't drop
59 	 * core.
60 	 */
61 #define	MAXIMUM_SCREEN_COLS	768
62 	if (*valp > MAXIMUM_SCREEN_COLS) {
63 		msgq(sp, M_ERR, "Screen columns too large, greater than %d",
64 		    MAXIMUM_SCREEN_COLS);
65 		return (1);
66 	}
67 	return (0);
68 }
69 
70 /*
71  * PUBLIC: int f_lines(SCR *, OPTION *, char *, u_long *);
72  */
73 int
74 f_lines(SCR *sp, OPTION *op, char *str, u_long *valp)
75 {
76 	/* Validate the number. */
77 	if (*valp < MINIMUM_SCREEN_ROWS) {
78 		msgq(sp, M_ERR, "Screen lines too small, less than %d",
79 		    MINIMUM_SCREEN_ROWS);
80 		return (1);
81 	}
82 
83 	/*
84 	 * !!!
85 	 * It's not uncommon for allocation of huge chunks of memory to cause
86 	 * core dumps on various systems.  So, we prune out numbers that are
87 	 * "obviously" wrong.  Vi will not work correctly if it has the wrong
88 	 * number of lines/columns for the screen, but at least we don't drop
89 	 * core.
90 	 */
91 #define	MAXIMUM_SCREEN_ROWS	500
92 	if (*valp > MAXIMUM_SCREEN_ROWS) {
93 		msgq(sp, M_ERR, "Screen lines too large, greater than %d",
94 		    MAXIMUM_SCREEN_ROWS);
95 		return (1);
96 	}
97 
98 	/*
99 	 * Set the value, and the related scroll value.  If no window
100 	 * value set, set a new default window.
101 	 */
102 	o_set(sp, O_LINES, 0, NULL, *valp);
103 	if (*valp == 1) {
104 		sp->defscroll = 1;
105 
106 		if (O_VAL(sp, O_WINDOW) == O_D_VAL(sp, O_WINDOW) ||
107 		    O_VAL(sp, O_WINDOW) > *valp) {
108 			o_set(sp, O_WINDOW, 0, NULL, 1);
109 			o_set(sp, O_WINDOW, OS_DEF, NULL, 1);
110 		}
111 	} else {
112 		sp->defscroll = (*valp - 1) / 2;
113 
114 		if (O_VAL(sp, O_WINDOW) == O_D_VAL(sp, O_WINDOW) ||
115 		    O_VAL(sp, O_WINDOW) > *valp) {
116 			o_set(sp, O_WINDOW, 0, NULL, *valp - 1);
117 			o_set(sp, O_WINDOW, OS_DEF, NULL, *valp - 1);
118 		}
119 	}
120 	return (0);
121 }
122 
123 /*
124  * PUBLIC: int f_lisp(SCR *, OPTION *, char *, u_long *);
125  */
126 int
127 f_lisp(SCR *sp, OPTION *op, char *str, u_long *valp)
128 {
129 	msgq(sp, M_ERR, "The lisp option is not implemented");
130 	return (0);
131 }
132 
133 /*
134  * PUBLIC: int f_paragraph(SCR *, OPTION *, char *, u_long *);
135  */
136 int
137 f_paragraph(SCR *sp, OPTION *op, char *str, u_long *valp)
138 {
139 	if (strlen(str) & 1) {
140 		msgq(sp, M_ERR,
141 		    "The paragraph option must be in two character groups");
142 		return (1);
143 	}
144 	return (0);
145 }
146 
147 /*
148  * PUBLIC: int f_print(SCR *, OPTION *, char *, u_long *);
149  */
150 int
151 f_print(SCR *sp, OPTION *op, char *str, u_long *valp)
152 {
153 	/* Reinitialize the key fast lookup table. */
154 	v_key_ilookup(sp);
155 
156 	/* Reformat the screen. */
157 	F_SET(sp, SC_SCR_REFORMAT);
158 	return (0);
159 }
160 
161 /*
162  * PUBLIC: int f_readonly(SCR *, OPTION *, char *, u_long *);
163  */
164 int
165 f_readonly(SCR *sp, OPTION *op, char *str, u_long *valp)
166 {
167 	/*
168 	 * !!!
169 	 * See the comment in exf.c.
170 	 */
171 	if (*valp)
172 		F_CLR(sp, SC_READONLY);
173 	else
174 		F_SET(sp, SC_READONLY);
175 	return (0);
176 }
177 
178 /*
179  * PUBLIC: int f_recompile(SCR *, OPTION *, char *, u_long *);
180  */
181 int
182 f_recompile(SCR *sp, OPTION *op, char *str, u_long *valp)
183 {
184 	if (F_ISSET(sp, SC_RE_SEARCH)) {
185 		regfree(&sp->re_c);
186 		F_CLR(sp, SC_RE_SEARCH);
187 	}
188 	if (F_ISSET(sp, SC_RE_SUBST)) {
189 		regfree(&sp->subre_c);
190 		F_CLR(sp, SC_RE_SUBST);
191 	}
192 	return (0);
193 }
194 
195 /*
196  * PUBLIC: int f_reformat(SCR *, OPTION *, char *, u_long *);
197  */
198 int
199 f_reformat(SCR *sp, OPTION *op, char *str, u_long *valp)
200 {
201 	F_SET(sp, SC_SCR_REFORMAT);
202 	return (0);
203 }
204 
205 /*
206  * PUBLIC: int f_section(SCR *, OPTION *, char *, u_long *);
207  */
208 int
209 f_section(SCR *sp, OPTION *op, char *str, u_long *valp)
210 {
211 	if (strlen(str) & 1) {
212 		msgq(sp, M_ERR,
213 		    "The section option must be in two character groups");
214 		return (1);
215 	}
216 	return (0);
217 }
218 
219 /*
220  * PUBLIC: int f_ttywerase(SCR *, OPTION *, char *, u_long *);
221  */
222 int
223 f_ttywerase(SCR *sp, OPTION *op, char *str, u_long *valp)
224 {
225 	if (!*valp)
226 		O_CLR(sp, O_ALTWERASE);
227 	return (0);
228 }
229 
230 /*
231  * PUBLIC: int f_w300(SCR *, OPTION *, char *, u_long *);
232  */
233 int
234 f_w300(SCR *sp, OPTION *op, char *str, u_long *valp)
235 {
236 	u_long v;
237 
238 	/* Historical behavior for w300 was < 1200. */
239 	if (sp->gp->scr_baud(sp, &v))
240 		return (1);
241 	if (v >= 1200)
242 		return (0);
243 
244 	return (f_window(sp, op, str, valp));
245 }
246 
247 /*
248  * PUBLIC: int f_w1200(SCR *, OPTION *, char *, u_long *);
249  */
250 int
251 f_w1200(SCR *sp, OPTION *op, char *str, u_long *valp)
252 {
253 	u_long v;
254 
255 	/* Historical behavior for w1200 was == 1200. */
256 	if (sp->gp->scr_baud(sp, &v))
257 		return (1);
258 	if (v < 1200 || v > 4800)
259 		return (0);
260 
261 	return (f_window(sp, op, str, valp));
262 }
263 
264 /*
265  * PUBLIC: int f_w9600(SCR *, OPTION *, char *, u_long *);
266  */
267 int
268 f_w9600(SCR *sp, OPTION *op, char *str, u_long *valp)
269 {
270 	u_long v;
271 
272 	/* Historical behavior for w9600 was > 1200. */
273 	if (sp->gp->scr_baud(sp, &v))
274 		return (1);
275 	if (v <= 4800)
276 		return (0);
277 
278 	return (f_window(sp, op, str, valp));
279 }
280 
281 /*
282  * PUBLIC: int f_window(SCR *, OPTION *, char *, u_long *);
283  */
284 int
285 f_window(SCR *sp, OPTION *op, char *str, u_long *valp)
286 {
287 	if (*valp >= O_VAL(sp, O_LINES) - 1 &&
288 	    (*valp = O_VAL(sp, O_LINES) - 1) == 0)
289 		*valp = 1;
290 	return (0);
291 }
292