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