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