add VI_SEL_END, VI_SEL_START, SI_SELECT
[nvi.git] / common / options.c
blob538d3683c319fe5ad8038bd6a68e886e35e32ff9
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.53 1996/12/16 09:39:15 bostic Exp $ (Berkeley) $Date: 1996/12/16 09:39:15 $";
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
392 v = O_VAL(sp, O_LINES) - 1;
393 (void)snprintf(b1, sizeof(b1), "window=%lu", v);
394 OI(O_WINDOW, b1);
397 * Set boolean default values, and copy all settings into the default
398 * information. OS_NOFREE is set, we're copying, not replacing.
400 for (op = optlist, cnt = 0; op->name != NULL; ++op, ++cnt)
401 switch (op->type) {
402 case OPT_0BOOL:
403 break;
404 case OPT_1BOOL:
405 O_SET(sp, cnt);
406 O_D_SET(sp, cnt);
407 break;
408 case OPT_NUM:
409 o_set(sp, cnt, OS_DEF, NULL, O_VAL(sp, cnt));
410 break;
411 case OPT_STR:
412 if (O_STR(sp, cnt) != NULL && o_set(sp, cnt,
413 OS_DEF | OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0))
414 goto err;
415 break;
416 default:
417 abort();
421 * !!!
422 * Some options can be initialized by the command name or the
423 * command-line arguments. They don't set the default values,
424 * it's historic practice.
426 for (; *oargs != -1; ++oargs)
427 OI(*oargs, optlist[*oargs].name);
428 #undef OI
431 * Inform the underlying screen of the initial values of the
432 * edit options.
434 for (op = optlist, cnt = 0; op->name != NULL; ++op, ++cnt) {
435 isset = O_ISSET(sp, cnt);
436 (void)sp->gp->scr_optchange(sp, cnt, O_STR(sp, cnt), &isset);
438 return (0);
440 err: msgq(sp, M_ERR,
441 "031|Unable to set default %s option", optlist[optindx].name);
442 return (1);
446 * opts_set --
447 * Change the values of one or more options.
449 * PUBLIC: int opts_set __P((SCR *, ARGS *[], char *));
452 opts_set(sp, argv, usage)
453 SCR *sp;
454 ARGS *argv[];
455 char *usage;
457 enum optdisp disp;
458 enum nresult nret;
459 OPTLIST const *op;
460 OPTION *spo;
461 u_long isset, turnoff, value;
462 int ch, equals, nf, nf2, offset, qmark, rval;
463 char *endp, *name, *p, *sep, *t;
465 disp = NO_DISPLAY;
466 for (rval = 0; argv[0]->len != 0; ++argv) {
468 * The historic vi dumped the options for each occurrence of
469 * "all" in the set list. Puhleeze.
471 if (!strcmp(argv[0]->bp, "all")) {
472 disp = ALL_DISPLAY;
473 continue;
476 /* Find equals sign or question mark. */
477 for (sep = NULL, equals = qmark = 0,
478 p = name = argv[0]->bp; (ch = *p) != '\0'; ++p)
479 if (ch == '=' || ch == '?') {
480 if (p == name) {
481 if (usage != NULL)
482 msgq(sp, M_ERR,
483 "032|Usage: %s", usage);
484 return (1);
486 sep = p;
487 if (ch == '=')
488 equals = 1;
489 else
490 qmark = 1;
491 break;
494 turnoff = 0;
495 op = NULL;
496 if (sep != NULL)
497 *sep++ = '\0';
499 /* Search for the name, then name without any leading "no". */
500 if ((op = opts_search(name)) == NULL &&
501 name[0] == 'n' && name[1] == 'o') {
502 turnoff = 1;
503 name += 2;
504 op = opts_search(name);
506 if (op == NULL) {
507 opts_nomatch(sp, name);
508 rval = 1;
509 continue;
512 /* Find current option values. */
513 offset = op - optlist;
514 spo = sp->opts + offset;
517 * !!!
518 * Historically, the question mark could be a separate
519 * argument.
521 if (!equals && !qmark &&
522 argv[1]->len == 1 && argv[1]->bp[0] == '?') {
523 ++argv;
524 qmark = 1;
527 /* Set name, value. */
528 switch (op->type) {
529 case OPT_0BOOL:
530 case OPT_1BOOL:
531 /* Some options may not be reset. */
532 if (F_ISSET(op, OPT_NOUNSET) && turnoff) {
533 msgq_str(sp, M_ERR, name,
534 "291|set: the %s option may not be turned off");
535 rval = 1;
536 break;
539 /* Some options may not be set. */
540 if (F_ISSET(op, OPT_NOSET) && !turnoff) {
541 msgq_str(sp, M_ERR, name,
542 "313|set: the %s option may never be turned on");
543 rval = 1;
544 break;
547 if (equals) {
548 msgq_str(sp, M_ERR, name,
549 "034|set: [no]%s option doesn't take a value");
550 rval = 1;
551 break;
553 if (qmark) {
554 if (!disp)
555 disp = SELECT_DISPLAY;
556 F_SET(spo, OPT_SELECTED);
557 break;
561 * Do nothing if the value is unchanged, the underlying
562 * functions can be expensive.
564 isset = !turnoff;
565 if (!F_ISSET(op, OPT_ALWAYS))
566 if (isset) {
567 if (O_ISSET(sp, offset))
568 break;
569 } else
570 if (!O_ISSET(sp, offset))
571 break;
573 /* Report to subsystems. */
574 if (op->func != NULL &&
575 op->func(sp, spo, NULL, &isset) ||
576 ex_optchange(sp, offset, NULL, &isset) ||
577 v_optchange(sp, offset, NULL, &isset) ||
578 sp->gp->scr_optchange(sp, offset, NULL, &isset)) {
579 rval = 1;
580 break;
583 /* Set the value. */
584 if (isset)
585 O_SET(sp, offset);
586 else
587 O_CLR(sp, offset);
588 break;
589 case OPT_NUM:
590 if (turnoff) {
591 msgq_str(sp, M_ERR, name,
592 "035|set: %s option isn't a boolean");
593 rval = 1;
594 break;
596 if (qmark || !equals) {
597 if (!disp)
598 disp = SELECT_DISPLAY;
599 F_SET(spo, OPT_SELECTED);
600 break;
603 if (!isdigit(sep[0]))
604 goto badnum;
605 if ((nret =
606 nget_uslong(&value, sep, &endp, 10)) != NUM_OK) {
607 p = msg_print(sp, name, &nf);
608 t = msg_print(sp, sep, &nf2);
609 switch (nret) {
610 case NUM_ERR:
611 msgq(sp, M_SYSERR,
612 "036|set: %s option: %s", p, t);
613 break;
614 case NUM_OVER:
615 msgq(sp, M_ERR,
616 "037|set: %s option: %s: value overflow", p, t);
617 break;
618 case NUM_OK:
619 case NUM_UNDER:
620 abort();
622 if (nf)
623 FREE_SPACE(sp, p, 0);
624 if (nf2)
625 FREE_SPACE(sp, t, 0);
626 rval = 1;
627 break;
629 if (*endp && !isblank(*endp)) {
630 badnum: p = msg_print(sp, name, &nf);
631 t = msg_print(sp, sep, &nf2);
632 msgq(sp, M_ERR,
633 "038|set: %s option: %s is an illegal number", p, t);
634 if (nf)
635 FREE_SPACE(sp, p, 0);
636 if (nf2)
637 FREE_SPACE(sp, t, 0);
638 rval = 1;
639 break;
642 /* Some options may never be set to zero. */
643 if (F_ISSET(op, OPT_NOZERO) && value == 0) {
644 msgq_str(sp, M_ERR, name,
645 "314|set: the %s option may never be set to 0");
646 rval = 1;
647 break;
651 * Do nothing if the value is unchanged, the underlying
652 * functions can be expensive.
654 if (!F_ISSET(op, OPT_ALWAYS) &&
655 O_VAL(sp, offset) == value)
656 break;
658 /* Report to subsystems. */
659 if (op->func != NULL &&
660 op->func(sp, spo, sep, &value) ||
661 ex_optchange(sp, offset, sep, &value) ||
662 v_optchange(sp, offset, sep, &value) ||
663 sp->gp->scr_optchange(sp, offset, sep, &value)) {
664 rval = 1;
665 break;
668 /* Set the value. */
669 if (o_set(sp, offset, 0, NULL, value))
670 rval = 1;
671 break;
672 case OPT_STR:
673 if (turnoff) {
674 msgq_str(sp, M_ERR, name,
675 "039|set: %s option isn't a boolean");
676 rval = 1;
677 break;
679 if (qmark || !equals) {
680 if (!disp)
681 disp = SELECT_DISPLAY;
682 F_SET(spo, OPT_SELECTED);
683 break;
687 * Do nothing if the value is unchanged, the underlying
688 * functions can be expensive.
690 if (!F_ISSET(op, OPT_ALWAYS) &&
691 O_STR(sp, offset) != NULL &&
692 !strcmp(O_STR(sp, offset), sep))
693 break;
695 /* Report to subsystems. */
696 if (op->func != NULL &&
697 op->func(sp, spo, sep, NULL) ||
698 ex_optchange(sp, offset, sep, NULL) ||
699 v_optchange(sp, offset, sep, NULL) ||
700 sp->gp->scr_optchange(sp, offset, sep, NULL)) {
701 rval = 1;
702 break;
705 /* Set the value. */
706 if (o_set(sp, offset, OS_STRDUP, sep, 0))
707 rval = 1;
708 break;
709 default:
710 abort();
713 if (disp != NO_DISPLAY)
714 opts_dump(sp, disp);
715 return (rval);
719 * o_set --
720 * Set an option's value.
722 * PUBLIC: int o_set __P((SCR *, int, u_int, char *, u_long));
725 o_set(sp, opt, flags, str, val)
726 SCR *sp;
727 int opt;
728 u_int flags;
729 char *str;
730 u_long val;
732 OPTION *op;
734 /* Set a pointer to the options area. */
735 op = F_ISSET(&sp->opts[opt], OPT_GLOBAL) ?
736 &sp->gp->opts[sp->opts[opt].o_cur.val] : &sp->opts[opt];
738 /* Copy the string, if requested. */
739 if (LF_ISSET(OS_STRDUP) && (str = strdup(str)) == NULL) {
740 msgq(sp, M_SYSERR, NULL);
741 return (1);
744 /* Free the previous string, if requested, and set the value. */
745 if LF_ISSET(OS_DEF)
746 if (LF_ISSET(OS_STR | OS_STRDUP)) {
747 if (!LF_ISSET(OS_NOFREE) && op->o_def.str != NULL)
748 free(op->o_def.str);
749 op->o_def.str = str;
750 } else
751 op->o_def.val = val;
752 else
753 if (LF_ISSET(OS_STR | OS_STRDUP)) {
754 if (!LF_ISSET(OS_NOFREE) && op->o_cur.str != NULL)
755 free(op->o_cur.str);
756 op->o_cur.str = str;
757 } else
758 op->o_cur.val = val;
759 return (0);
763 * opts_empty --
764 * Return 1 if the string option is invalid, 0 if it's OK.
766 * PUBLIC: int opts_empty __P((SCR *, int, int));
769 opts_empty(sp, off, silent)
770 SCR *sp;
771 int off, silent;
773 char *p;
775 if ((p = O_STR(sp, off)) == NULL || p[0] == '\0') {
776 if (!silent)
777 msgq_str(sp, M_ERR, optlist[off].name,
778 "305|No %s edit option specified");
779 return (1);
781 return (0);
785 * opts_dump --
786 * List the current values of selected options.
788 * PUBLIC: void opts_dump __P((SCR *, enum optdisp));
790 void
791 opts_dump(sp, type)
792 SCR *sp;
793 enum optdisp type;
795 OPTLIST const *op;
796 int base, b_num, cnt, col, colwidth, curlen, s_num;
797 int numcols, numrows, row;
798 int b_op[O_OPTIONCOUNT], s_op[O_OPTIONCOUNT];
799 char nbuf[20];
802 * Options are output in two groups -- those that fit in a column and
803 * those that don't. Output is done on 6 character "tab" boundaries
804 * for no particular reason. (Since we don't output tab characters,
805 * we can ignore the terminal's tab settings.) Ignore the user's tab
806 * setting because we have no idea how reasonable it is.
808 * Find a column width we can live with, testing from 10 columns to 1.
810 for (numcols = 10; numcols > 1; --numcols) {
811 colwidth = sp->cols / numcols & ~(STANDARD_TAB - 1);
812 if (colwidth >= 10) {
813 colwidth =
814 (colwidth + STANDARD_TAB) & ~(STANDARD_TAB - 1);
815 numcols = sp->cols / colwidth;
816 break;
818 colwidth = 0;
822 * Get the set of options to list, entering them into
823 * the column list or the overflow list.
825 for (b_num = s_num = 0, op = optlist; op->name != NULL; ++op) {
826 cnt = op - optlist;
828 /* If OPT_NDISP set, it's never displayed. */
829 if (F_ISSET(op, OPT_NDISP))
830 continue;
832 switch (type) {
833 case ALL_DISPLAY: /* Display all. */
834 break;
835 case CHANGED_DISPLAY: /* Display changed. */
836 /* If OPT_ADISP set, it's always "changed". */
837 if (F_ISSET(op, OPT_ADISP))
838 break;
839 switch (op->type) {
840 case OPT_0BOOL:
841 case OPT_1BOOL:
842 case OPT_NUM:
843 if (O_VAL(sp, cnt) == O_D_VAL(sp, cnt))
844 continue;
845 break;
846 case OPT_STR:
847 if (O_STR(sp, cnt) == O_D_STR(sp, cnt) ||
848 O_D_STR(sp, cnt) != NULL &&
849 !strcmp(O_STR(sp, cnt), O_D_STR(sp, cnt)))
850 continue;
851 break;
853 break;
854 case SELECT_DISPLAY: /* Display selected. */
855 if (!F_ISSET(&sp->opts[cnt], OPT_SELECTED))
856 continue;
857 break;
858 default:
859 case NO_DISPLAY:
860 abort();
862 F_CLR(&sp->opts[cnt], OPT_SELECTED);
864 curlen = strlen(op->name);
865 switch (op->type) {
866 case OPT_0BOOL:
867 case OPT_1BOOL:
868 if (!O_ISSET(sp, cnt))
869 curlen += 2;
870 break;
871 case OPT_NUM:
872 (void)snprintf(nbuf,
873 sizeof(nbuf), "%ld", O_VAL(sp, cnt));
874 curlen += strlen(nbuf);
875 break;
876 case OPT_STR:
877 if (O_STR(sp, cnt) != NULL)
878 curlen += strlen(O_STR(sp, cnt));
879 curlen += 3;
880 break;
882 /* Offset by 2 so there's a gap. */
883 if (curlen <= colwidth - 2)
884 s_op[s_num++] = cnt;
885 else
886 b_op[b_num++] = cnt;
889 if (s_num > 0) {
890 /* Figure out the number of rows. */
891 if (s_num > numcols) {
892 numrows = s_num / numcols;
893 if (s_num % numcols)
894 ++numrows;
895 } else
896 numrows = 1;
898 /* Display the options in sorted order. */
899 for (row = 0; row < numrows;) {
900 for (base = row, col = 0; col < numcols; ++col) {
901 cnt = opts_print(sp, &optlist[s_op[base]]);
902 if ((base += numrows) >= s_num)
903 break;
904 (void)ex_printf(sp, "%*s",
905 (int)(colwidth - cnt), "");
907 if (++row < numrows || b_num)
908 (void)ex_puts(sp, "\n");
912 for (row = 0; row < b_num;) {
913 (void)opts_print(sp, &optlist[b_op[row]]);
914 if (++row < b_num)
915 (void)ex_puts(sp, "\n");
917 (void)ex_puts(sp, "\n");
921 * opts_print --
922 * Print out an option.
924 static int
925 opts_print(sp, op)
926 SCR *sp;
927 OPTLIST const *op;
929 int curlen, offset;
931 curlen = 0;
932 offset = op - optlist;
933 switch (op->type) {
934 case OPT_0BOOL:
935 case OPT_1BOOL:
936 curlen += ex_printf(sp,
937 "%s%s", O_ISSET(sp, offset) ? "" : "no", op->name);
938 break;
939 case OPT_NUM:
940 curlen += ex_printf(sp, "%s=%ld", op->name, O_VAL(sp, offset));
941 break;
942 case OPT_STR:
943 curlen += ex_printf(sp, "%s=\"%s\"", op->name,
944 O_STR(sp, offset) == NULL ? "" : O_STR(sp, offset));
945 break;
947 return (curlen);
951 * opts_save --
952 * Write the current configuration to a file.
954 * PUBLIC: int opts_save __P((SCR *, FILE *));
957 opts_save(sp, fp)
958 SCR *sp;
959 FILE *fp;
961 OPTLIST const *op;
962 int ch, cnt;
963 char *p;
965 for (op = optlist; op->name != NULL; ++op) {
966 if (F_ISSET(op, OPT_NOSAVE))
967 continue;
968 cnt = op - optlist;
969 switch (op->type) {
970 case OPT_0BOOL:
971 case OPT_1BOOL:
972 if (O_ISSET(sp, cnt))
973 (void)fprintf(fp, "set %s\n", op->name);
974 else
975 (void)fprintf(fp, "set no%s\n", op->name);
976 break;
977 case OPT_NUM:
978 (void)fprintf(fp,
979 "set %s=%-3ld\n", op->name, O_VAL(sp, cnt));
980 break;
981 case OPT_STR:
982 if (O_STR(sp, cnt) == NULL)
983 break;
984 (void)fprintf(fp, "set ");
985 for (p = op->name; (ch = *p) != '\0'; ++p) {
986 if (isblank(ch) || ch == '\\')
987 (void)putc('\\', fp);
988 (void)putc(ch, fp);
990 (void)putc('=', fp);
991 for (p = O_STR(sp, cnt); (ch = *p) != '\0'; ++p) {
992 if (isblank(ch) || ch == '\\')
993 (void)putc('\\', fp);
994 (void)putc(ch, fp);
996 (void)putc('\n', fp);
997 break;
999 if (ferror(fp)) {
1000 msgq(sp, M_SYSERR, NULL);
1001 return (1);
1004 return (0);
1008 * opts_search --
1009 * Search for an option.
1011 * PUBLIC: OPTLIST const *opts_search __P((char *));
1013 OPTLIST const *
1014 opts_search(name)
1015 char *name;
1017 OPTLIST const *op, *found;
1018 OABBREV atmp, *ap;
1019 OPTLIST otmp;
1020 size_t len;
1022 /* Check list of abbreviations. */
1023 atmp.name = name;
1024 if ((ap = bsearch(&atmp, abbrev, sizeof(abbrev) / sizeof(OABBREV) - 1,
1025 sizeof(OABBREV), opts_abbcmp)) != NULL)
1026 return (optlist + ap->offset);
1028 /* Check list of options. */
1029 otmp.name = name;
1030 if ((op = bsearch(&otmp, optlist, sizeof(optlist) / sizeof(OPTLIST) - 1,
1031 sizeof(OPTLIST), opts_cmp)) != NULL)
1032 return (op);
1035 * Check to see if the name is the prefix of one (and only one)
1036 * option. If so, return the option.
1038 len = strlen(name);
1039 for (found = NULL, op = optlist; op->name != NULL; ++op) {
1040 if (op->name[0] < name[0])
1041 continue;
1042 if (op->name[0] > name[0])
1043 break;
1044 if (!memcmp(op->name, name, len)) {
1045 if (found != NULL)
1046 return (NULL);
1047 found = op;
1050 return (found);
1054 * opts_nomatch --
1055 * Standard nomatch error message for options.
1057 * PUBLIC: void opts_nomatch __P((SCR *, char *));
1059 void
1060 opts_nomatch(sp, name)
1061 SCR *sp;
1062 char *name;
1064 msgq_str(sp, M_ERR, name,
1065 "033|set: no %s option: 'set all' gives all option values");
1068 static int
1069 opts_abbcmp(a, b)
1070 const void *a, *b;
1072 return(strcmp(((OABBREV *)a)->name, ((OABBREV *)b)->name));
1075 static int
1076 opts_cmp(a, b)
1077 const void *a, *b;
1079 return(strcmp(((OPTLIST *)a)->name, ((OPTLIST *)b)->name));
1083 * opts_copy --
1084 * Copy a screen's OPTION array.
1086 * PUBLIC: int opts_copy __P((SCR *, SCR *));
1089 opts_copy(orig, sp)
1090 SCR *orig, *sp;
1092 int cnt, rval;
1094 /* Copy most everything without change. */
1095 memcpy(sp->opts, orig->opts, sizeof(orig->opts));
1097 /* Copy the string edit options. */
1098 for (cnt = rval = 0; cnt < O_OPTIONCOUNT; ++cnt) {
1099 if (optlist[cnt].type != OPT_STR ||
1100 F_ISSET(&optlist[cnt], OPT_GLOBAL))
1101 continue;
1103 * If never set, or already failed, NULL out the entries --
1104 * have to continue after failure, otherwise would have two
1105 * screens referencing the same memory.
1107 if (rval || O_STR(sp, cnt) == NULL) {
1108 o_set(sp, cnt, OS_NOFREE | OS_STR, NULL, 0);
1109 o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
1110 continue;
1113 /* Copy the current string. */
1114 if (o_set(sp, cnt, OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0)) {
1115 o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
1116 goto nomem;
1119 /* Copy the default string. */
1120 if (O_D_STR(sp, cnt) != NULL && o_set(sp, cnt,
1121 OS_DEF | OS_NOFREE | OS_STRDUP, O_D_STR(sp, cnt), 0)) {
1122 nomem: msgq(orig, M_SYSERR, NULL);
1123 rval = 1;
1126 return (rval);
1130 * opts_free --
1131 * Free all option strings
1133 * PUBLIC: void opts_free __P((SCR *));
1135 void
1136 opts_free(sp)
1137 SCR *sp;
1139 int cnt;
1141 for (cnt = 0; cnt < O_OPTIONCOUNT; ++cnt) {
1142 if (optlist[cnt].type != OPT_STR ||
1143 F_ISSET(&optlist[cnt], OPT_GLOBAL))
1144 continue;
1145 if (O_STR(sp, cnt) != NULL)
1146 free(O_STR(sp, cnt));
1147 if (O_D_STR(sp, cnt) != NULL)
1148 free(O_D_STR(sp, cnt));