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