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