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