xref: /openbsd/usr.bin/vi/common/options.c (revision 8932bfb7)
1 /*	$OpenBSD: options.c,v 1.16 2011/07/10 13:20:25 millert Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1991, 1993, 1994, 1995, 1996
7  *	Keith Bostic.  All rights reserved.
8  *
9  * See the LICENSE file for redistribution information.
10  */
11 
12 #include "config.h"
13 
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 
19 #include <bitstring.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "common.h"
29 #include "../vi/vi.h"
30 #include "pathnames.h"
31 
32 static int	 	 opts_abbcmp(const void *, const void *);
33 static int	 	 opts_cmp(const void *, const void *);
34 static int	 	 opts_print(SCR *, OPTLIST const *);
35 
36 /*
37  * O'Reilly noted options and abbreviations are from "Learning the VI Editor",
38  * Fifth Edition, May 1992.  There's no way of knowing what systems they are
39  * actually from.
40  *
41  * HPUX noted options and abbreviations are from "The Ultimate Guide to the
42  * VI and EX Text Editors", 1990.
43  */
44 OPTLIST const optlist[] = {
45 /* O_ALTWERASE	  4.4BSD */
46 	{"altwerase",	f_altwerase,	OPT_0BOOL,	0},
47 /* O_AUTOINDENT	    4BSD */
48 	{"autoindent",	NULL,		OPT_0BOOL,	0},
49 /* O_AUTOPRINT	    4BSD */
50 	{"autoprint",	NULL,		OPT_1BOOL,	0},
51 /* O_AUTOWRITE	    4BSD */
52 	{"autowrite",	NULL,		OPT_0BOOL,	0},
53 /* O_BACKUP	  4.4BSD */
54 	{"backup",	NULL,		OPT_STR,	0},
55 /* O_BEAUTIFY	    4BSD */
56 	{"beautify",	NULL,		OPT_0BOOL,	0},
57 /* O_CDPATH	  4.4BSD */
58 	{"cdpath",	NULL,		OPT_STR,	0},
59 /* O_CEDIT	  4.4BSD */
60 	{"cedit",	NULL,		OPT_STR,	0},
61 /* O_COLUMNS	  4.4BSD */
62 	{"columns",	f_columns,	OPT_NUM,	OPT_NOSAVE},
63 /* O_COMMENT	  4.4BSD */
64 	{"comment",	NULL,		OPT_0BOOL,	0},
65 /* O_TMP_DIRECTORY	    4BSD */
66 	{"directory",	NULL,		OPT_STR,	0},
67 /* O_EDCOMPATIBLE   4BSD */
68 	{"edcompatible",NULL,		OPT_0BOOL,	0},
69 /* O_ESCAPETIME	  4.4BSD */
70 	{"escapetime",	NULL,		OPT_NUM,	0},
71 /* O_ERRORBELLS	    4BSD */
72 	{"errorbells",	NULL,		OPT_0BOOL,	0},
73 /* O_EXRC	System V (undocumented) */
74 	{"exrc",	NULL,		OPT_0BOOL,	0},
75 /* O_EXTENDED	  4.4BSD */
76 	{"extended",	f_recompile,	OPT_0BOOL,	0},
77 /* O_FILEC	  4.4BSD */
78 	{"filec",	NULL,		OPT_STR,	0},
79 /* O_FLASH	    HPUX */
80 	{"flash",	NULL,		OPT_0BOOL,	0},
81 /* O_HARDTABS	    4BSD */
82 	{"hardtabs",	NULL,		OPT_NUM,	0},
83 /* O_ICLOWER	  4.4BSD */
84 	{"iclower",	f_recompile,	OPT_0BOOL,	0},
85 /* O_IGNORECASE	    4BSD */
86 	{"ignorecase",	f_recompile,	OPT_0BOOL,	0},
87 /* O_KEYTIME	  4.4BSD */
88 	{"keytime",	NULL,		OPT_NUM,	0},
89 /* O_LEFTRIGHT	  4.4BSD */
90 	{"leftright",	f_reformat,	OPT_0BOOL,	0},
91 /* O_LINES	  4.4BSD */
92 	{"lines",	f_lines,	OPT_NUM,	OPT_NOSAVE},
93 /* O_LISP	    4BSD
94  *	XXX
95  *	When the lisp option is implemented, delete the OPT_NOSAVE flag,
96  *	so that :mkexrc dumps it.
97  */
98 	{"lisp",	f_lisp,		OPT_0BOOL,	OPT_NOSAVE},
99 /* O_LIST	    4BSD */
100 	{"list",	f_reformat,	OPT_0BOOL,	0},
101 /* O_LOCKFILES	  4.4BSD
102  *	XXX
103  *	Locking isn't reliable enough over NFS to require it, in addition,
104  *	it's a serious startup performance problem over some remote links.
105  */
106 	{"lock",	NULL,		OPT_1BOOL,	0},
107 /* O_MAGIC	    4BSD */
108 	{"magic",	NULL,		OPT_1BOOL,	0},
109 /* O_MATCHTIME	  4.4BSD */
110 	{"matchtime",	NULL,		OPT_NUM,	0},
111 /* O_MESG	    4BSD */
112 	{"mesg",	NULL,		OPT_1BOOL,	0},
113 /* O_MODELINE	    4BSD
114  *	!!!
115  *	This has been documented in historical systems as both "modeline"
116  *	and as "modelines".  Regardless of the name, this option represents
117  *	a security problem of mammoth proportions, not to mention a stunning
118  *	example of what your intro CS professor referred to as the perils of
119  *	mixing code and data.  Don't add it, or I will kill you.
120  */
121 	{"modeline",	NULL,		OPT_0BOOL,	OPT_NOSET},
122 /* O_MSGCAT	  4.4BSD */
123 	{"msgcat",	f_msgcat,	OPT_STR,	0},
124 /* O_NOPRINT	  4.4BSD */
125 	{"noprint",	f_print,	OPT_STR,	OPT_EARLYSET},
126 /* O_NUMBER	    4BSD */
127 	{"number",	f_reformat,	OPT_0BOOL,	0},
128 /* O_OCTAL	  4.4BSD */
129 	{"octal",	f_print,	OPT_0BOOL,	OPT_EARLYSET},
130 /* O_OPEN	    4BSD */
131 	{"open",	NULL,		OPT_1BOOL,	0},
132 /* O_OPTIMIZE	    4BSD */
133 	{"optimize",	NULL,		OPT_1BOOL,	0},
134 /* O_PARAGRAPHS	    4BSD */
135 	{"paragraphs",	f_paragraph,	OPT_STR,	0},
136 /* O_PATH	  4.4BSD */
137 	{"path",	NULL,		OPT_STR,	0},
138 /* O_PRINT	  4.4BSD */
139 	{"print",	f_print,	OPT_STR,	OPT_EARLYSET},
140 /* O_PROMPT	    4BSD */
141 	{"prompt",	NULL,		OPT_1BOOL,	0},
142 /* O_READONLY	    4BSD (undocumented) */
143 	{"readonly",	f_readonly,	OPT_0BOOL,	OPT_ALWAYS},
144 /* O_RECDIR	  4.4BSD */
145 	{"recdir",	NULL,		OPT_STR,	0},
146 /* O_REDRAW	    4BSD */
147 	{"redraw",	NULL,		OPT_0BOOL,	0},
148 /* O_REMAP	    4BSD */
149 	{"remap",	NULL,		OPT_1BOOL,	0},
150 /* O_REPORT	    4BSD */
151 	{"report",	NULL,		OPT_NUM,	0},
152 /* O_RULER	  4.4BSD */
153 	{"ruler",	NULL,		OPT_0BOOL,	0},
154 /* O_SCROLL	    4BSD */
155 	{"scroll",	NULL,		OPT_NUM,	0},
156 /* O_SEARCHINCR	  4.4BSD */
157 	{"searchincr",	NULL,		OPT_0BOOL,	0},
158 /* O_SECTIONS	    4BSD */
159 	{"sections",	f_section,	OPT_STR,	0},
160 /* O_SECURE	  4.4BSD */
161 	{"secure",	NULL,		OPT_0BOOL,	OPT_NOUNSET},
162 /* O_SHELL	    4BSD */
163 	{"shell",	NULL,		OPT_STR,	0},
164 /* O_SHELLMETA	  4.4BSD */
165 	{"shellmeta",	NULL,		OPT_STR,	0},
166 /* O_SHIFTWIDTH	    4BSD */
167 	{"shiftwidth",	NULL,		OPT_NUM,	OPT_NOZERO},
168 /* O_SHOWMATCH	    4BSD */
169 	{"showmatch",	NULL,		OPT_0BOOL,	0},
170 /* O_SHOWMODE	  4.4BSD */
171 	{"showmode",	NULL,		OPT_0BOOL,	0},
172 /* O_SIDESCROLL	  4.4BSD */
173 	{"sidescroll",	NULL,		OPT_NUM,	OPT_NOZERO},
174 /* O_SLOWOPEN	    4BSD  */
175 	{"slowopen",	NULL,		OPT_0BOOL,	0},
176 /* O_SOURCEANY	    4BSD (undocumented)
177  *	!!!
178  *	Historic vi, on startup, source'd $HOME/.exrc and ./.exrc, if they
179  *	were owned by the user.  The sourceany option was an undocumented
180  *	feature of historic vi which permitted the startup source'ing of
181  *	.exrc files the user didn't own.  This is an obvious security problem,
182  *	and we ignore the option.
183  */
184 	{"sourceany",	NULL,		OPT_0BOOL,	OPT_NOSET},
185 /* O_TABSTOP	    4BSD */
186 	{"tabstop",	f_reformat,	OPT_NUM,	OPT_NOZERO},
187 /* O_TAGLENGTH	    4BSD */
188 	{"taglength",	NULL,		OPT_NUM,	0},
189 /* O_TAGS	    4BSD */
190 	{"tags",	NULL,		OPT_STR,	0},
191 /* O_TERM	    4BSD
192  *	!!!
193  *	By default, the historic vi always displayed information about two
194  *	options, redraw and term.  Term seems sufficient.
195  */
196 	{"term",	NULL,		OPT_STR,	OPT_ADISP|OPT_NOSAVE},
197 /* O_TERSE	    4BSD */
198 	{"terse",	NULL,		OPT_0BOOL,	0},
199 /* O_TILDEOP      4.4BSD */
200 	{"tildeop",	NULL,		OPT_0BOOL,	0},
201 /* O_TIMEOUT	    4BSD (undocumented) */
202 	{"timeout",	NULL,		OPT_1BOOL,	0},
203 /* O_TTYWERASE	  4.4BSD */
204 	{"ttywerase",	f_ttywerase,	OPT_0BOOL,	0},
205 /* O_VERBOSE	  4.4BSD */
206 	{"verbose",	NULL,		OPT_0BOOL,	0},
207 /* O_W1200	    4BSD */
208 	{"w1200",	f_w1200,	OPT_NUM,	OPT_NDISP|OPT_NOSAVE},
209 /* O_W300	    4BSD */
210 	{"w300",	f_w300,		OPT_NUM,	OPT_NDISP|OPT_NOSAVE},
211 /* O_W9600	    4BSD */
212 	{"w9600",	f_w9600,	OPT_NUM,	OPT_NDISP|OPT_NOSAVE},
213 /* O_WARN	    4BSD */
214 	{"warn",	NULL,		OPT_1BOOL,	0},
215 /* O_WINDOW	    4BSD */
216 	{"window",	f_window,	OPT_NUM,	0},
217 /* O_WINDOWNAME	    4BSD */
218 	{"windowname",	NULL,		OPT_0BOOL,	0},
219 /* O_WRAPLEN	  4.4BSD */
220 	{"wraplen",	NULL,		OPT_NUM,	0},
221 /* O_WRAPMARGIN	    4BSD */
222 	{"wrapmargin",	NULL,		OPT_NUM,	0},
223 /* O_WRAPSCAN	    4BSD */
224 	{"wrapscan",	NULL,		OPT_1BOOL,	0},
225 /* O_WRITEANY	    4BSD */
226 	{"writeany",	NULL,		OPT_0BOOL,	0},
227 	{NULL},
228 };
229 
230 typedef struct abbrev {
231         char *name;
232         int offset;
233 } OABBREV;
234 
235 static OABBREV const abbrev[] = {
236 	{"ai",		O_AUTOINDENT},		/*     4BSD */
237 	{"ap",		O_AUTOPRINT},		/*     4BSD */
238 	{"aw",		O_AUTOWRITE},		/*     4BSD */
239 	{"bf",		O_BEAUTIFY},		/*     4BSD */
240 	{"co",		O_COLUMNS},		/*   4.4BSD */
241 	{"dir",		O_TMP_DIRECTORY},	/*     4BSD */
242 	{"eb",		O_ERRORBELLS},		/*     4BSD */
243 	{"ed",		O_EDCOMPATIBLE},	/*     4BSD */
244 	{"ex",		O_EXRC},		/* System V (undocumented) */
245 	{"ht",		O_HARDTABS},		/*     4BSD */
246 	{"ic",		O_IGNORECASE},		/*     4BSD */
247 	{"li",		O_LINES},		/*   4.4BSD */
248 	{"modelines",	O_MODELINE},		/*     HPUX */
249 	{"nu",		O_NUMBER},		/*     4BSD */
250 	{"opt",		O_OPTIMIZE},		/*     4BSD */
251 	{"para",	O_PARAGRAPHS},		/*     4BSD */
252 	{"re",		O_REDRAW},		/* O'Reilly */
253 	{"ro",		O_READONLY},		/*     4BSD (undocumented) */
254 	{"scr",		O_SCROLL},		/*     4BSD (undocumented) */
255 	{"sect",	O_SECTIONS},		/* O'Reilly */
256 	{"sh",		O_SHELL},		/*     4BSD */
257 	{"slow",	O_SLOWOPEN},		/*     4BSD */
258 	{"sm",		O_SHOWMATCH},		/*     4BSD */
259 	{"smd",		O_SHOWMODE},		/*     4BSD */
260 	{"sw",		O_SHIFTWIDTH},		/*     4BSD */
261 	{"tag",		O_TAGS},		/*     4BSD (undocumented) */
262 	{"tl",		O_TAGLENGTH},		/*     4BSD */
263 	{"to",		O_TIMEOUT},		/*     4BSD (undocumented) */
264 	{"ts",		O_TABSTOP},		/*     4BSD */
265 	{"tty",		O_TERM},		/*     4BSD (undocumented) */
266 	{"ttytype",	O_TERM},		/*     4BSD (undocumented) */
267 	{"w",		O_WINDOW},		/* O'Reilly */
268 	{"wa",		O_WRITEANY},		/*     4BSD */
269 	{"wi",		O_WINDOW},		/*     4BSD (undocumented) */
270 	{"wl",		O_WRAPLEN},		/*   4.4BSD */
271 	{"wm",		O_WRAPMARGIN},		/*     4BSD */
272 	{"ws",		O_WRAPSCAN},		/*     4BSD */
273 	{NULL},
274 };
275 
276 /*
277  * opts_init --
278  *	Initialize some of the options.
279  *
280  * PUBLIC: int opts_init(SCR *, int *);
281  */
282 int
283 opts_init(sp, oargs)
284 	SCR *sp;
285 	int *oargs;
286 {
287 	ARGS *argv[2], a, b;
288 	OPTLIST const *op;
289 	u_long v;
290 	int optindx;
291 	char *s, b1[1024];
292 
293 	a.bp = b1;
294 	b.bp = NULL;
295 	a.len = b.len = 0;
296 	argv[0] = &a;
297 	argv[1] = &b;
298 
299 	/* Set numeric and string default values. */
300 #define	OI(indx, str) {							\
301 	if ((str) != b1)	/* GCC puts strings in text-space. */	\
302 		(void)strlcpy(b1, (str), sizeof(b1));			\
303 	a.len = strlen(b1);						\
304 	if (opts_set(sp, argv, NULL)) {					\
305 		optindx = indx;						\
306 		goto err;						\
307 	}								\
308 }
309 	/*
310 	 * Indirect global options to global space.  Specifically, set up
311 	 * terminal, lines, columns first, they're used by other options.
312 	 * Note, don't set the flags until we've set up the indirection.
313 	 */
314 	if (o_set(sp, O_TERM, 0, NULL, GO_TERM)) {
315 		optindx = O_TERM;
316 		goto err;
317 	}
318 	F_SET(&sp->opts[O_TERM], OPT_GLOBAL);
319 	if (o_set(sp, O_LINES, 0, NULL, GO_LINES)) {
320 		optindx = O_LINES;
321 		goto err;
322 	}
323 	F_SET(&sp->opts[O_LINES], OPT_GLOBAL);
324 	if (o_set(sp, O_COLUMNS, 0, NULL, GO_COLUMNS)) {
325 		optindx = O_COLUMNS;
326 		goto err;
327 	}
328 	F_SET(&sp->opts[O_COLUMNS], OPT_GLOBAL);
329 	if (o_set(sp, O_SECURE, 0, NULL, GO_SECURE)) {
330 		optindx = O_SECURE;
331 		goto err;
332 	}
333 	F_SET(&sp->opts[O_SECURE], OPT_GLOBAL);
334 
335 	/* Initialize string values. */
336 	(void)snprintf(b1, sizeof(b1),
337 	    "cdpath=%s", (s = getenv("CDPATH")) == NULL ? ":" : s);
338 	OI(O_CDPATH, b1);
339 
340 	/*
341 	 * !!!
342 	 * Vi historically stored temporary files in /var/tmp.  We store them
343 	 * in /tmp by default, hoping it's a memory based file system.  There
344 	 * are two ways to change this -- the user can set either the directory
345 	 * option or the TMPDIR environmental variable.
346 	 */
347 	(void)snprintf(b1, sizeof(b1),
348 	    "directory=%s", (s = getenv("TMPDIR")) == NULL ? _PATH_TMP : s);
349 	OI(O_TMP_DIRECTORY, b1);
350 	OI(O_ESCAPETIME, "escapetime=1");
351 	OI(O_KEYTIME, "keytime=6");
352 	OI(O_MATCHTIME, "matchtime=7");
353 	(void)snprintf(b1, sizeof(b1), "msgcat=%s", _PATH_MSGCAT);
354 	OI(O_MSGCAT, b1);
355 	OI(O_REPORT, "report=5");
356 	OI(O_PARAGRAPHS, "paragraphs=IPLPPPQPP LIpplpipbp");
357 	(void)snprintf(b1, sizeof(b1), "path=%s", "");
358 	OI(O_PATH, b1);
359 	(void)snprintf(b1, sizeof(b1), "recdir=%s", _PATH_PRESERVE);
360 	OI(O_RECDIR, b1);
361 	OI(O_SECTIONS, "sections=NHSHH HUnhsh");
362 	(void)snprintf(b1, sizeof(b1),
363 	    "shell=%s", (s = getenv("SHELL")) == NULL ? _PATH_BSHELL : s);
364 	OI(O_SHELL, b1);
365 	OI(O_SHELLMETA, "shellmeta=~{[*?$`'\"\\");
366 	OI(O_SHIFTWIDTH, "shiftwidth=8");
367 	OI(O_SIDESCROLL, "sidescroll=16");
368 	OI(O_TABSTOP, "tabstop=8");
369 	(void)snprintf(b1, sizeof(b1), "tags=%s", _PATH_TAGS);
370 	OI(O_TAGS, b1);
371 
372 	/*
373 	 * XXX
374 	 * Initialize O_SCROLL here, after term; initializing term should
375 	 * have created a LINES/COLUMNS value.
376 	 */
377 	if ((v = (O_VAL(sp, O_LINES) - 1) / 2) == 0)
378 		v = 1;
379 	(void)snprintf(b1, sizeof(b1), "scroll=%ld", v);
380 	OI(O_SCROLL, b1);
381 
382 	/*
383 	 * The default window option values are:
384 	 *		8 if baud rate <=  600
385 	 *	       16 if baud rate <= 1200
386 	 *	LINES - 1 if baud rate  > 1200
387 	 *
388 	 * Note, the windows option code will correct any too-large value
389 	 * or when the O_LINES value is 1.
390 	 */
391 	if (sp->gp->scr_baud(sp, &v))
392 		return (1);
393 	if (v <= 600)
394 		v = 8;
395 	else if (v <= 1200)
396 		v = 16;
397 	else
398 		v = O_VAL(sp, O_LINES) - 1;
399 	(void)snprintf(b1, sizeof(b1), "window=%lu", v);
400 	OI(O_WINDOW, b1);
401 
402 	/*
403 	 * Set boolean default values, and copy all settings into the default
404 	 * information.  OS_NOFREE is set, we're copying, not replacing.
405 	 */
406 	for (op = optlist, optindx = 0; op->name != NULL; ++op, ++optindx)
407 		switch (op->type) {
408 		case OPT_0BOOL:
409 			break;
410 		case OPT_1BOOL:
411 			O_SET(sp, optindx);
412 			O_D_SET(sp, optindx);
413 			break;
414 		case OPT_NUM:
415 			o_set(sp, optindx, OS_DEF, NULL, O_VAL(sp, optindx));
416 			break;
417 		case OPT_STR:
418 			if (O_STR(sp, optindx) != NULL && o_set(sp, optindx,
419 			    OS_DEF | OS_NOFREE | OS_STRDUP, O_STR(sp, optindx), 0))
420 				goto err;
421 			break;
422 		default:
423 			abort();
424 		}
425 
426 	/*
427 	 * !!!
428 	 * Some options can be initialized by the command name or the
429 	 * command-line arguments.  They don't set the default values,
430 	 * it's historic practice.
431 	 */
432 	for (; *oargs != -1; ++oargs)
433 		OI(*oargs, optlist[*oargs].name);
434 	return (0);
435 #undef OI
436 
437 err:	msgq(sp, M_ERR,
438 	    "031|Unable to set default %s option", optlist[optindx].name);
439 	return (1);
440 }
441 
442 /*
443  * opts_set --
444  *	Change the values of one or more options.
445  *
446  * PUBLIC: int opts_set(SCR *, ARGS *[], char *);
447  */
448 int
449 opts_set(sp, argv, usage)
450 	SCR *sp;
451 	ARGS *argv[];
452 	char *usage;
453 {
454 	enum optdisp disp;
455 	enum nresult nret;
456 	OPTLIST const *op;
457 	OPTION *spo;
458 	u_long value, turnoff;
459 	int ch, equals, nf, nf2, offset, qmark, rval;
460 	char *endp, *name, *p, *sep, *t;
461 
462 	disp = NO_DISPLAY;
463 	for (rval = 0; argv[0]->len != 0; ++argv) {
464 		/*
465 		 * The historic vi dumped the options for each occurrence of
466 		 * "all" in the set list.  Puhleeze.
467 		 */
468 		if (!strcmp(argv[0]->bp, "all")) {
469 			disp = ALL_DISPLAY;
470 			continue;
471 		}
472 
473 		/* Find equals sign or question mark. */
474 		for (sep = NULL, equals = qmark = 0,
475 		    p = name = argv[0]->bp; (ch = *p) != '\0'; ++p)
476 			if (ch == '=' || ch == '?') {
477 				if (p == name) {
478 					if (usage != NULL)
479 						msgq(sp, M_ERR,
480 						    "032|Usage: %s", usage);
481 					return (1);
482 				}
483 				sep = p;
484 				if (ch == '=')
485 					equals = 1;
486 				else
487 					qmark = 1;
488 				break;
489 			}
490 
491 		turnoff = 0;
492 		op = NULL;
493 		if (sep != NULL)
494 			*sep++ = '\0';
495 
496 		/* Search for the name, then name without any leading "no". */
497 		if ((op = opts_search(name)) == NULL &&
498 		    name[0] == 'n' && name[1] == 'o') {
499 			turnoff = 1;
500 			name += 2;
501 			op = opts_search(name);
502 		}
503 		if (op == NULL) {
504 			opts_nomatch(sp, name);
505 			rval = 1;
506 			continue;
507 		}
508 
509 		/* Find current option values. */
510 		offset = op - optlist;
511 		spo = sp->opts + offset;
512 
513 		/*
514 		 * !!!
515 		 * Historically, the question mark could be a separate
516 		 * argument.
517 		 */
518 		if (!equals && !qmark &&
519 		    argv[1]->len == 1 && argv[1]->bp[0] == '?') {
520 			++argv;
521 			qmark = 1;
522 		}
523 
524 		/* Set name, value. */
525 		switch (op->type) {
526 		case OPT_0BOOL:
527 		case OPT_1BOOL:
528 			/* Some options may not be reset. */
529 			if (F_ISSET(op, OPT_NOUNSET) && turnoff) {
530 				msgq_str(sp, M_ERR, name,
531 			    "291|set: the %s option may not be turned off");
532 				rval = 1;
533 				break;
534 			}
535 
536 			/* Some options may not be set. */
537 			if (F_ISSET(op, OPT_NOSET) && !turnoff) {
538 				msgq_str(sp, M_ERR, name,
539 			    "313|set: the %s option may never be turned on");
540 				rval = 1;
541 				break;
542 			}
543 
544 			if (equals) {
545 				msgq_str(sp, M_ERR, name,
546 			    "034|set: [no]%s option doesn't take a value");
547 				rval = 1;
548 				break;
549 			}
550 			if (qmark) {
551 				if (!disp)
552 					disp = SELECT_DISPLAY;
553 				F_SET(spo, OPT_SELECTED);
554 				break;
555 			}
556 
557 			/*
558 			 * Do nothing if the value is unchanged, the underlying
559 			 * functions can be expensive.
560 			 */
561 			if (!F_ISSET(op, OPT_ALWAYS)) {
562 				if (turnoff) {
563 					if (!O_ISSET(sp, offset))
564 						break;
565 				} else {
566 					if (O_ISSET(sp, offset))
567 						break;
568 				}
569 			}
570 
571 			if (F_ISSET(op, OPT_EARLYSET)) {
572 			    /* Set the value. */
573 			    if (turnoff)
574 				O_CLR(sp, offset);
575 			    else
576 				O_SET(sp, offset);
577 			}
578 
579 			/* Report to subsystems. */
580 			if ((op->func != NULL &&
581 			    op->func(sp, spo, NULL, &turnoff)) ||
582 			    ex_optchange(sp, offset, NULL, &turnoff) ||
583 			    v_optchange(sp, offset, NULL, &turnoff) ||
584 			    sp->gp->scr_optchange(sp, offset, NULL, &turnoff)) {
585 				rval = 1;
586 				break;
587 			}
588 
589 			if (!F_ISSET(op, OPT_EARLYSET)) {
590 			    /* Set the value. */
591 			    if (turnoff)
592 				O_CLR(sp, offset);
593 			    else
594 				O_SET(sp, offset);
595 			}
596 			break;
597 		case OPT_NUM:
598 			if (turnoff) {
599 				msgq_str(sp, M_ERR, name,
600 				    "035|set: %s option isn't a boolean");
601 				rval = 1;
602 				break;
603 			}
604 			if (qmark || !equals) {
605 				if (!disp)
606 					disp = SELECT_DISPLAY;
607 				F_SET(spo, OPT_SELECTED);
608 				break;
609 			}
610 
611 			if (!isdigit(sep[0]))
612 				goto badnum;
613 			if ((nret =
614 			    nget_uslong(&value, sep, &endp, 10)) != NUM_OK) {
615 				p = msg_print(sp, name, &nf);
616 				t = msg_print(sp, sep, &nf2);
617 				switch (nret) {
618 				case NUM_ERR:
619 					msgq(sp, M_SYSERR,
620 					    "036|set: %s option: %s", p, t);
621 					break;
622 				case NUM_OVER:
623 					msgq(sp, M_ERR,
624 			    "037|set: %s option: %s: value overflow", p, t);
625 					break;
626 				case NUM_OK:
627 				case NUM_UNDER:
628 					abort();
629 				}
630 				if (nf)
631 					FREE_SPACE(sp, p, 0);
632 				if (nf2)
633 					FREE_SPACE(sp, t, 0);
634 				rval = 1;
635 				break;
636 			}
637 			if (*endp && !isblank(*endp)) {
638 badnum:				p = msg_print(sp, name, &nf);
639 				t = msg_print(sp, sep, &nf2);
640 				msgq(sp, M_ERR,
641 		    "038|set: %s option: %s is an illegal number", p, t);
642 				if (nf)
643 					FREE_SPACE(sp, p, 0);
644 				if (nf2)
645 					FREE_SPACE(sp, t, 0);
646 				rval = 1;
647 				break;
648 			}
649 
650 			/* Some options may never be set to zero. */
651 			if (F_ISSET(op, OPT_NOZERO) && value == 0) {
652 				msgq_str(sp, M_ERR, name,
653 			    "314|set: the %s option may never be set to 0");
654 				rval = 1;
655 				break;
656 			}
657 
658 			/*
659 			 * Do nothing if the value is unchanged, the underlying
660 			 * functions can be expensive.
661 			 */
662 			if (!F_ISSET(op, OPT_ALWAYS) &&
663 			    O_VAL(sp, offset) == value)
664 				break;
665 
666 			if (F_ISSET(op, OPT_EARLYSET)) {
667 			    /* Set the value. */
668 			    if (o_set(sp, offset, 0, NULL, value)) {
669 				rval = 1;
670 				break;
671 			    }
672 			}
673 
674 			/* Report to subsystems. */
675 			if ((op->func != NULL &&
676 			    op->func(sp, spo, sep, &value)) ||
677 			    ex_optchange(sp, offset, sep, &value) ||
678 			    v_optchange(sp, offset, sep, &value) ||
679 			    sp->gp->scr_optchange(sp, offset, sep, &value)) {
680 				rval = 1;
681 				break;
682 			}
683 
684 			if (!F_ISSET(op, OPT_EARLYSET)) {
685 			    /* Set the value. */
686 			    if (o_set(sp, offset, 0, NULL, value))
687 				rval = 1;
688 			}
689 			break;
690 		case OPT_STR:
691 			if (turnoff) {
692 				msgq_str(sp, M_ERR, name,
693 				    "039|set: %s option isn't a boolean");
694 				rval = 1;
695 				break;
696 			}
697 			if (qmark || !equals) {
698 				if (!disp)
699 					disp = SELECT_DISPLAY;
700 				F_SET(spo, OPT_SELECTED);
701 				break;
702 			}
703 
704 			/*
705 			 * Do nothing if the value is unchanged, the underlying
706 			 * functions can be expensive.
707 			 */
708 			if (!F_ISSET(op, OPT_ALWAYS) &&
709 			    O_STR(sp, offset) != NULL &&
710 			    !strcmp(O_STR(sp, offset), sep))
711 				break;
712 
713 			if (F_ISSET(op, OPT_EARLYSET)) {
714 			    /* Set the value. */
715 			    if (o_set(sp, offset, OS_STRDUP, sep, 0)) {
716 				rval = 1;
717 				break;
718 			    }
719 			}
720 
721 			/* Report to subsystems. */
722 			if ((op->func != NULL &&
723 			    op->func(sp, spo, sep, NULL)) ||
724 			    ex_optchange(sp, offset, sep, NULL) ||
725 			    v_optchange(sp, offset, sep, NULL) ||
726 			    sp->gp->scr_optchange(sp, offset, sep, NULL)) {
727 				rval = 1;
728 				break;
729 			}
730 
731 			if (!F_ISSET(op, OPT_EARLYSET)) {
732 			    /* Set the value. */
733 			    if (o_set(sp, offset, OS_STRDUP, sep, 0))
734 				rval = 1;
735 			}
736 			break;
737 		default:
738 			abort();
739 		}
740 	}
741 	if (disp != NO_DISPLAY)
742 		opts_dump(sp, disp);
743 	return (rval);
744 }
745 
746 /*
747  * o_set --
748  *	Set an option's value.
749  *
750  * PUBLIC: int o_set(SCR *, int, u_int, char *, u_long);
751  */
752 int
753 o_set(sp, opt, flags, str, val)
754 	SCR *sp;
755 	int opt;
756 	u_int flags;
757 	char *str;
758 	u_long val;
759 {
760 	OPTION *op;
761 
762 	/* Set a pointer to the options area. */
763 	op = F_ISSET(&sp->opts[opt], OPT_GLOBAL) ?
764 	    &sp->gp->opts[sp->opts[opt].o_cur.val] : &sp->opts[opt];
765 
766 	/* Copy the string, if requested. */
767 	if (LF_ISSET(OS_STRDUP) && (str = strdup(str)) == NULL) {
768 		msgq(sp, M_SYSERR, NULL);
769 		return (1);
770 	}
771 
772 	/* Free the previous string, if requested, and set the value. */
773 	if (LF_ISSET(OS_DEF))
774 		if (LF_ISSET(OS_STR | OS_STRDUP)) {
775 			if (!LF_ISSET(OS_NOFREE) && op->o_def.str != NULL)
776 				free(op->o_def.str);
777 			op->o_def.str = str;
778 		} else
779 			op->o_def.val = val;
780 	else
781 		if (LF_ISSET(OS_STR | OS_STRDUP)) {
782 			if (!LF_ISSET(OS_NOFREE) && op->o_cur.str != NULL)
783 				free(op->o_cur.str);
784 			op->o_cur.str = str;
785 		} else
786 			op->o_cur.val = val;
787 	return (0);
788 }
789 
790 /*
791  * opts_empty --
792  *	Return 1 if the string option is invalid, 0 if it's OK.
793  *
794  * PUBLIC: int opts_empty(SCR *, int, int);
795  */
796 int
797 opts_empty(sp, off, silent)
798 	SCR *sp;
799 	int off, silent;
800 {
801 	char *p;
802 
803 	if ((p = O_STR(sp, off)) == NULL || p[0] == '\0') {
804 		if (!silent)
805 			msgq_str(sp, M_ERR, optlist[off].name,
806 			    "305|No %s edit option specified");
807 		return (1);
808 	}
809 	return (0);
810 }
811 
812 /*
813  * opts_dump --
814  *	List the current values of selected options.
815  *
816  * PUBLIC: void opts_dump(SCR *, enum optdisp);
817  */
818 void
819 opts_dump(sp, type)
820 	SCR *sp;
821 	enum optdisp type;
822 {
823 	OPTLIST const *op;
824 	int base, b_num, cnt, col, colwidth, curlen, s_num;
825 	int numcols, numrows, row;
826 	int b_op[O_OPTIONCOUNT], s_op[O_OPTIONCOUNT];
827 	char nbuf[20];
828 
829 	/*
830 	 * Options are output in two groups -- those that fit in a column and
831 	 * those that don't.  Output is done on 6 character "tab" boundaries
832 	 * for no particular reason.  (Since we don't output tab characters,
833 	 * we can ignore the terminal's tab settings.)  Ignore the user's tab
834 	 * setting because we have no idea how reasonable it is.
835 	 *
836 	 * Find a column width we can live with, testing from 10 columns to 1.
837 	 */
838 	for (numcols = 10; numcols > 1; --numcols) {
839 		colwidth = sp->cols / numcols & ~(STANDARD_TAB - 1);
840 		if (colwidth >= 10) {
841 			colwidth =
842 			    (colwidth + STANDARD_TAB) & ~(STANDARD_TAB - 1);
843 			numcols = sp->cols / colwidth;
844 			break;
845 		}
846 		colwidth = 0;
847 	}
848 
849 	/*
850 	 * Get the set of options to list, entering them into
851 	 * the column list or the overflow list.
852 	 */
853 	for (b_num = s_num = 0, op = optlist; op->name != NULL; ++op) {
854 		cnt = op - optlist;
855 
856 		/* If OPT_NDISP set, it's never displayed. */
857 		if (F_ISSET(op, OPT_NDISP))
858 			continue;
859 
860 		switch (type) {
861 		case ALL_DISPLAY:		/* Display all. */
862 			break;
863 		case CHANGED_DISPLAY:		/* Display changed. */
864 			/* If OPT_ADISP set, it's always "changed". */
865 			if (F_ISSET(op, OPT_ADISP))
866 				break;
867 			switch (op->type) {
868 			case OPT_0BOOL:
869 			case OPT_1BOOL:
870 			case OPT_NUM:
871 				if (O_VAL(sp, cnt) == O_D_VAL(sp, cnt))
872 					continue;
873 				break;
874 			case OPT_STR:
875 				if (O_STR(sp, cnt) == O_D_STR(sp, cnt) ||
876 				    (O_D_STR(sp, cnt) != NULL &&
877 				    !strcmp(O_STR(sp, cnt), O_D_STR(sp, cnt))))
878 					continue;
879 				break;
880 			}
881 			break;
882 		case SELECT_DISPLAY:		/* Display selected. */
883 			if (!F_ISSET(&sp->opts[cnt], OPT_SELECTED))
884 				continue;
885 			break;
886 		default:
887 		case NO_DISPLAY:
888 			abort();
889 		}
890 		F_CLR(&sp->opts[cnt], OPT_SELECTED);
891 
892 		curlen = strlen(op->name);
893 		switch (op->type) {
894 		case OPT_0BOOL:
895 		case OPT_1BOOL:
896 			if (!O_ISSET(sp, cnt))
897 				curlen += 2;
898 			break;
899 		case OPT_NUM:
900 			(void)snprintf(nbuf,
901 			    sizeof(nbuf), "%ld", O_VAL(sp, cnt));
902 			curlen += strlen(nbuf);
903 			break;
904 		case OPT_STR:
905 			if (O_STR(sp, cnt) != NULL)
906 				curlen += strlen(O_STR(sp, cnt));
907 			curlen += 3;
908 			break;
909 		}
910 		/* Offset by 2 so there's a gap. */
911 		if (curlen <= colwidth - 2)
912 			s_op[s_num++] = cnt;
913 		else
914 			b_op[b_num++] = cnt;
915 	}
916 
917 	if (s_num > 0) {
918 		/* Figure out the number of rows. */
919 		if (s_num > numcols) {
920 			numrows = s_num / numcols;
921 			if (s_num % numcols)
922 				++numrows;
923 		} else
924 			numrows = 1;
925 
926 		/* Display the options in sorted order. */
927 		for (row = 0; row < numrows;) {
928 			for (base = row, col = 0; col < numcols; ++col) {
929 				cnt = opts_print(sp, &optlist[s_op[base]]);
930 				if ((base += numrows) >= s_num)
931 					break;
932 				(void)ex_printf(sp, "%*s",
933 				    (int)(colwidth - cnt), "");
934 			}
935 			if (++row < numrows || b_num)
936 				(void)ex_puts(sp, "\n");
937 		}
938 	}
939 
940 	for (row = 0; row < b_num;) {
941 		(void)opts_print(sp, &optlist[b_op[row]]);
942 		if (++row < b_num)
943 			(void)ex_puts(sp, "\n");
944 	}
945 	(void)ex_puts(sp, "\n");
946 }
947 
948 /*
949  * opts_print --
950  *	Print out an option.
951  */
952 static int
953 opts_print(sp, op)
954 	SCR *sp;
955 	OPTLIST const *op;
956 {
957 	int curlen, offset;
958 
959 	curlen = 0;
960 	offset = op - optlist;
961 	switch (op->type) {
962 	case OPT_0BOOL:
963 	case OPT_1BOOL:
964 		curlen += ex_printf(sp,
965 		    "%s%s", O_ISSET(sp, offset) ? "" : "no", op->name);
966 		break;
967 	case OPT_NUM:
968 		curlen += ex_printf(sp, "%s=%ld", op->name, O_VAL(sp, offset));
969 		break;
970 	case OPT_STR:
971 		curlen += ex_printf(sp, "%s=\"%s\"", op->name,
972 		    O_STR(sp, offset) == NULL ? "" : O_STR(sp, offset));
973 		break;
974 	}
975 	return (curlen);
976 }
977 
978 /*
979  * opts_save --
980  *	Write the current configuration to a file.
981  *
982  * PUBLIC: int opts_save(SCR *, FILE *);
983  */
984 int
985 opts_save(sp, fp)
986 	SCR *sp;
987 	FILE *fp;
988 {
989 	OPTLIST const *op;
990 	int ch, cnt;
991 	char *p;
992 
993 	for (op = optlist; op->name != NULL; ++op) {
994 		if (F_ISSET(op, OPT_NOSAVE))
995 			continue;
996 		cnt = op - optlist;
997 		switch (op->type) {
998 		case OPT_0BOOL:
999 		case OPT_1BOOL:
1000 			if (O_ISSET(sp, cnt))
1001 				(void)fprintf(fp, "set %s\n", op->name);
1002 			else
1003 				(void)fprintf(fp, "set no%s\n", op->name);
1004 			break;
1005 		case OPT_NUM:
1006 			(void)fprintf(fp,
1007 			    "set %s=%-3ld\n", op->name, O_VAL(sp, cnt));
1008 			break;
1009 		case OPT_STR:
1010 			if (O_STR(sp, cnt) == NULL)
1011 				break;
1012 			(void)fprintf(fp, "set ");
1013 			for (p = op->name; (ch = *p) != '\0'; ++p) {
1014 				if (isblank(ch) || ch == '\\')
1015 					(void)putc('\\', fp);
1016 				(void)putc(ch, fp);
1017 			}
1018 			(void)putc('=', fp);
1019 			for (p = O_STR(sp, cnt); (ch = *p) != '\0'; ++p) {
1020 				if (isblank(ch) || ch == '\\')
1021 					(void)putc('\\', fp);
1022 				(void)putc(ch, fp);
1023 			}
1024 			(void)putc('\n', fp);
1025 			break;
1026 		}
1027 		if (ferror(fp)) {
1028 			msgq(sp, M_SYSERR, NULL);
1029 			return (1);
1030 		}
1031 	}
1032 	return (0);
1033 }
1034 
1035 /*
1036  * opts_search --
1037  *	Search for an option.
1038  *
1039  * PUBLIC: OPTLIST const *opts_search(char *);
1040  */
1041 OPTLIST const *
1042 opts_search(name)
1043 	char *name;
1044 {
1045 	OPTLIST const *op, *found;
1046 	OABBREV atmp, *ap;
1047 	OPTLIST otmp;
1048 	size_t len;
1049 
1050 	/* Check list of abbreviations. */
1051 	atmp.name = name;
1052 	if ((ap = bsearch(&atmp, abbrev, sizeof(abbrev) / sizeof(OABBREV) - 1,
1053 	    sizeof(OABBREV), opts_abbcmp)) != NULL)
1054 		return (optlist + ap->offset);
1055 
1056 	/* Check list of options. */
1057 	otmp.name = name;
1058 	if ((op = bsearch(&otmp, optlist, sizeof(optlist) / sizeof(OPTLIST) - 1,
1059 	    sizeof(OPTLIST), opts_cmp)) != NULL)
1060 		return (op);
1061 
1062 	/*
1063 	 * Check to see if the name is the prefix of one (and only one)
1064 	 * option.  If so, return the option.
1065 	 */
1066 	len = strlen(name);
1067 	for (found = NULL, op = optlist; op->name != NULL; ++op) {
1068 		if (op->name[0] < name[0])
1069 			continue;
1070 		if (op->name[0] > name[0])
1071 			break;
1072 		if (!memcmp(op->name, name, len)) {
1073 			if (found != NULL)
1074 				return (NULL);
1075 			found = op;
1076 		}
1077 	}
1078 	return (found);
1079 }
1080 
1081 /*
1082  * opts_nomatch --
1083  *	Standard nomatch error message for options.
1084  *
1085  * PUBLIC: void opts_nomatch(SCR *, char *);
1086  */
1087 void
1088 opts_nomatch(sp, name)
1089 	SCR *sp;
1090 	char *name;
1091 {
1092 	msgq_str(sp, M_ERR, name,
1093 	    "033|set: no %s option: 'set all' gives all option values");
1094 }
1095 
1096 static int
1097 opts_abbcmp(a, b)
1098         const void *a, *b;
1099 {
1100         return(strcmp(((OABBREV *)a)->name, ((OABBREV *)b)->name));
1101 }
1102 
1103 static int
1104 opts_cmp(a, b)
1105         const void *a, *b;
1106 {
1107         return(strcmp(((OPTLIST *)a)->name, ((OPTLIST *)b)->name));
1108 }
1109 
1110 /*
1111  * opts_copy --
1112  *	Copy a screen's OPTION array.
1113  *
1114  * PUBLIC: int opts_copy(SCR *, SCR *);
1115  */
1116 int
1117 opts_copy(orig, sp)
1118 	SCR *orig, *sp;
1119 {
1120 	int cnt, rval;
1121 
1122 	/* Copy most everything without change. */
1123 	memcpy(sp->opts, orig->opts, sizeof(orig->opts));
1124 
1125 	/* Copy the string edit options. */
1126 	for (cnt = rval = 0; cnt < O_OPTIONCOUNT; ++cnt) {
1127 		if (optlist[cnt].type != OPT_STR ||
1128 		    F_ISSET(&optlist[cnt], OPT_GLOBAL))
1129 			continue;
1130 		/*
1131 		 * If never set, or already failed, NULL out the entries --
1132 		 * have to continue after failure, otherwise would have two
1133 		 * screens referencing the same memory.
1134 		 */
1135 		if (rval || O_STR(sp, cnt) == NULL) {
1136 			o_set(sp, cnt, OS_NOFREE | OS_STR, NULL, 0);
1137 			o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
1138 			continue;
1139 		}
1140 
1141 		/* Copy the current string. */
1142 		if (o_set(sp, cnt, OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0)) {
1143 			o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
1144 			goto nomem;
1145 		}
1146 
1147 		/* Copy the default string. */
1148 		if (O_D_STR(sp, cnt) != NULL && o_set(sp, cnt,
1149 		    OS_DEF | OS_NOFREE | OS_STRDUP, O_D_STR(sp, cnt), 0)) {
1150 nomem:			msgq(orig, M_SYSERR, NULL);
1151 			rval = 1;
1152 		}
1153 	}
1154 	return (rval);
1155 }
1156 
1157 /*
1158  * opts_free --
1159  *	Free all option strings
1160  *
1161  * PUBLIC: void opts_free(SCR *);
1162  */
1163 void
1164 opts_free(sp)
1165 	SCR *sp;
1166 {
1167 	int cnt;
1168 
1169 	for (cnt = 0; cnt < O_OPTIONCOUNT; ++cnt) {
1170 		if (optlist[cnt].type != OPT_STR ||
1171 		    F_ISSET(&optlist[cnt], OPT_GLOBAL))
1172 			continue;
1173 		if (O_STR(sp, cnt) != NULL)
1174 			free(O_STR(sp, cnt));
1175 		if (O_D_STR(sp, cnt) != NULL)
1176 			free(O_D_STR(sp, cnt));
1177 	}
1178 }
1179