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