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