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