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