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