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