summaryrefslogtreecommitdiff
path: root/usr.bin/vi/common/options_f.c
blob: ea3c61160cf5ed158ac1da73fbc20095420184e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*-
 * Copyright (c) 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#ifndef lint
static const char sccsid[] = "@(#)options_f.c	10.25 (Berkeley) 7/12/96";
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/stat.h>

#include <bitstring.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "common.h"

/*
 * PUBLIC: int f_altwerase __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_altwerase(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	if (!*valp)
		O_CLR(sp, O_TTYWERASE);
	return (0);
}

/*
 * PUBLIC: int f_columns __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_columns(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	/* Validate the number. */
	if (*valp < MINIMUM_SCREEN_COLS) {
		msgq(sp, M_ERR, "040|Screen columns too small, less than %d",
		    MINIMUM_SCREEN_COLS);
		return (1);
	}

	/*
	 * !!!
	 * It's not uncommon for allocation of huge chunks of memory to cause
	 * core dumps on various systems.  So, we prune out numbers that are
	 * "obviously" wrong.  Vi will not work correctly if it has the wrong
	 * number of lines/columns for the screen, but at least we don't drop
	 * core.
	 */
#define	MAXIMUM_SCREEN_COLS	500
	if (*valp > MAXIMUM_SCREEN_COLS) {
		msgq(sp, M_ERR, "041|Screen columns too large, greater than %d",
		    MAXIMUM_SCREEN_COLS);
		return (1);
	}
	return (0);
}

/*
 * PUBLIC: int f_lines __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_lines(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	/* Validate the number. */
	if (*valp < MINIMUM_SCREEN_ROWS) {
		msgq(sp, M_ERR, "042|Screen lines too small, less than %d",
		    MINIMUM_SCREEN_ROWS);
		return (1);
	}

	/*
	 * !!!
	 * It's not uncommon for allocation of huge chunks of memory to cause
	 * core dumps on various systems.  So, we prune out numbers that are
	 * "obviously" wrong.  Vi will not work correctly if it has the wrong
	 * number of lines/columns for the screen, but at least we don't drop
	 * core.
	 */
#define	MAXIMUM_SCREEN_ROWS	500
	if (*valp > MAXIMUM_SCREEN_ROWS) {
		msgq(sp, M_ERR, "043|Screen lines too large, greater than %d",
		    MAXIMUM_SCREEN_ROWS);
		return (1);
	}

	/*
	 * Set the value, and the related scroll value.  If no window
	 * value set, set a new default window.
	 */
	o_set(sp, O_LINES, 0, NULL, *valp);
	if (*valp == 1) {
		sp->defscroll = 1;

		if (O_VAL(sp, O_WINDOW) == O_D_VAL(sp, O_WINDOW) ||
		    O_VAL(sp, O_WINDOW) > *valp) {
			o_set(sp, O_WINDOW, 0, NULL, 1);
			o_set(sp, O_WINDOW, OS_DEF, NULL, 1);
		}
	} else {
		sp->defscroll = (*valp - 1) / 2;

		if (O_VAL(sp, O_WINDOW) == O_D_VAL(sp, O_WINDOW) ||
		    O_VAL(sp, O_WINDOW) > *valp) {
			o_set(sp, O_WINDOW, 0, NULL, *valp - 1);
			o_set(sp, O_WINDOW, OS_DEF, NULL, *valp - 1);
		}
	}
	return (0);
}

/*
 * PUBLIC: int f_lisp __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_lisp(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	msgq(sp, M_ERR, "044|The lisp option is not implemented");
	return (0);
}

/*
 * PUBLIC: int f_msgcat __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_msgcat(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	(void)msg_open(sp, str);
	return (0);
}

/*
 * PUBLIC: int f_paragraph __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_paragraph(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	if (strlen(str) & 1) {
		msgq(sp, M_ERR,
		    "048|The paragraph option must be in two character groups");
		return (1);
	}
	return (0);
}

/*
 * PUBLIC: int f_print __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_print(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	/* Reinitialize the key fast lookup table. */
	v_key_ilookup(sp);

	/* Reformat the screen. */
	F_SET(sp, SC_SCR_REFORMAT);
	return (0);
}

/*
 * PUBLIC: int f_readonly __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_readonly(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	/*
	 * !!!
	 * See the comment in exf.c.
	 */
	if (*valp)
		F_CLR(sp, SC_READONLY);
	else
		F_SET(sp, SC_READONLY);
	return (0);
}

/*
 * PUBLIC: int f_recompile __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_recompile(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	if (F_ISSET(sp, SC_RE_SEARCH)) {
		regfree(&sp->re_c);
		F_CLR(sp, SC_RE_SEARCH);
	}
	if (F_ISSET(sp, SC_RE_SUBST)) {
		regfree(&sp->subre_c);
		F_CLR(sp, SC_RE_SUBST);
	}
	return (0);
}

/*
 * PUBLIC: int f_reformat __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_reformat(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	F_SET(sp, SC_SCR_REFORMAT);
	return (0);
}

/*
 * PUBLIC: int f_section __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_section(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	if (strlen(str) & 1) {
		msgq(sp, M_ERR,
		    "049|The section option must be in two character groups");
		return (1);
	}
	return (0);
}

/*
 * PUBLIC: int f_ttywerase __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_ttywerase(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	if (!*valp)
		O_CLR(sp, O_ALTWERASE);
	return (0);
}

/*
 * PUBLIC: int f_w300 __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_w300(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	u_long v;

	/* Historical behavior for w300 was < 1200. */
	if (sp->gp->scr_baud(sp, &v))
		return (1);
	if (v >= 1200)
		return (0);

	return (f_window(sp, op, str, valp));
}

/*
 * PUBLIC: int f_w1200 __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_w1200(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	u_long v;

	/* Historical behavior for w1200 was == 1200. */
	if (sp->gp->scr_baud(sp, &v))
		return (1);
	if (v < 1200 || v > 4800)
		return (0);

	return (f_window(sp, op, str, valp));
}

/*
 * PUBLIC: int f_w9600 __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_w9600(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	u_long v;

	/* Historical behavior for w9600 was > 1200. */
	if (sp->gp->scr_baud(sp, &v))
		return (1);
	if (v <= 4800)
		return (0);

	return (f_window(sp, op, str, valp));
}

/*
 * PUBLIC: int f_window __P((SCR *, OPTION *, char *, u_long *));
 */
int
f_window(sp, op, str, valp)
	SCR *sp;
	OPTION *op;
	char *str;
	u_long *valp;
{
	if (*valp >= O_VAL(sp, O_LINES) - 1 &&
	    (*valp = O_VAL(sp, O_LINES) - 1) == 0)
		*valp = 1;
	return (0);
}