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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
28 #include "pathnames.h"
30 static int opts_abbcmp(const void *, const void *);
31 static int opts_cmp(const void *, const void *);
32 static int opts_print(SCR *, OPTLIST const *);
37 #define OPT_WC (OPT_NOSAVE | OPT_NDISP)
41 * O'Reilly noted options and abbreviations are from "Learning the VI Editor",
42 * Fifth Edition, May 1992. There's no way of knowing what systems they are
45 * HPUX noted options and abbreviations are from "The Ultimate Guide to the
46 * VI and EX Text Editors", 1990.
48 OPTLIST const optlist[] = {
49 /* O_ALTWERASE 4.4BSD */
50 {L("altwerase"), f_altwerase, OPT_0BOOL, 0},
51 /* O_AUTOINDENT 4BSD */
52 {L("autoindent"), NULL, OPT_0BOOL, 0},
53 /* O_AUTOPRINT 4BSD */
54 {L("autoprint"), NULL, OPT_1BOOL, 0},
55 /* O_AUTOWRITE 4BSD */
56 {L("autowrite"), NULL, OPT_0BOOL, 0},
58 {L("backup"), NULL, OPT_STR, 0},
60 {L("beautify"), NULL, OPT_0BOOL, 0},
62 {L("cdpath"), NULL, OPT_STR, 0},
64 {L("cedit"), NULL, OPT_STR, 0},
65 /* O_COLUMNS 4.4BSD */
66 {L("columns"), f_columns, OPT_NUM, OPT_NOSAVE},
68 {L("combined"), NULL, OPT_0BOOL, OPT_NOSET|OPT_WC},
69 /* O_COMMENT 4.4BSD */
70 {L("comment"), NULL, OPT_0BOOL, 0},
72 {L("directory"), NULL, OPT_STR, 0},
73 /* O_EDCOMPATIBLE 4BSD */
74 {L("edcompatible"),NULL, OPT_0BOOL, 0},
75 /* O_ERRORBELLS 4BSD */
76 {L("errorbells"), NULL, OPT_0BOOL, 0},
77 /* O_ESCAPETIME 4.4BSD */
78 {L("escapetime"), NULL, OPT_NUM, 0},
79 /* O_EXPANDTAB NetBSD 5.0 */
80 {L("expandtab"), NULL, OPT_0BOOL, 0},
81 /* O_EXRC System V (undocumented) */
82 {L("exrc"), NULL, OPT_0BOOL, 0},
83 /* O_EXTENDED 4.4BSD */
84 {L("extended"), f_recompile, OPT_0BOOL, 0},
86 {L("filec"), NULL, OPT_STR, 0},
88 {L("fileencoding"),f_encoding, OPT_STR, OPT_WC},
90 {L("flash"), NULL, OPT_1BOOL, 0},
92 {L("hardtabs"), NULL, OPT_NUM, 0},
93 /* O_ICLOWER 4.4BSD */
94 {L("iclower"), f_recompile, OPT_0BOOL, 0},
95 /* O_IGNORECASE 4BSD */
96 {L("ignorecase"), f_recompile, OPT_0BOOL, 0},
98 {L("inputencoding"),f_encoding, OPT_STR, OPT_WC},
99 /* O_KEYTIME 4.4BSD */
100 {L("keytime"), NULL, OPT_NUM, 0},
101 /* O_LEFTRIGHT 4.4BSD */
102 {L("leftright"), f_reformat, OPT_0BOOL, 0},
104 {L("lines"), f_lines, OPT_NUM, OPT_NOSAVE},
107 * When the lisp option is implemented, delete the OPT_NOSAVE flag,
108 * so that :mkexrc dumps it.
110 {L("lisp"), f_lisp, OPT_0BOOL, OPT_NOSAVE},
112 {L("list"), f_reformat, OPT_0BOOL, 0},
113 /* O_LOCKFILES 4.4BSD
115 * Locking isn't reliable enough over NFS to require it, in addition,
116 * it's a serious startup performance problem over some remote links.
118 {L("lock"), NULL, OPT_1BOOL, 0},
120 {L("magic"), NULL, OPT_1BOOL, 0},
121 /* O_MATCHCHARS NetBSD 2.0 */
122 {L("matchchars"), NULL, OPT_STR, OPT_PAIRS},
123 /* O_MATCHTIME 4.4BSD */
124 {L("matchtime"), NULL, OPT_NUM, 0},
126 {L("mesg"), NULL, OPT_1BOOL, 0},
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},
141 {L("number"), f_reformat, OPT_0BOOL, 0},
143 {L("octal"), f_print, OPT_0BOOL, 0},
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"), NULL, OPT_STR, OPT_PAIRS},
151 {L("path"), NULL, OPT_STR, 0},
153 {L("print"), f_print, OPT_STR, 0},
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},
161 {L("redraw"), NULL, OPT_0BOOL, 0},
163 {L("remap"), NULL, OPT_1BOOL, 0},
165 {L("report"), NULL, OPT_NUM, 0},
167 {L("ruler"), NULL, OPT_0BOOL, 0},
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"), NULL, OPT_STR, OPT_PAIRS},
174 /* O_SECURE 4.4BSD */
175 {L("secure"), NULL, OPT_0BOOL, OPT_NOUNSET},
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)
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},
200 {L("tabstop"), f_reformat, OPT_NUM, OPT_NOZERO},
201 /* O_TAGLENGTH 4BSD */
202 {L("taglength"), NULL, OPT_NUM, 0},
204 {L("tags"), NULL, OPT_STR, 0},
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},
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},
222 {L("w1200"), f_w1200, OPT_NUM, OPT_NDISP|OPT_NOSAVE},
224 {L("w300"), f_w300, OPT_NUM, OPT_NDISP|OPT_NOSAVE},
226 {L("w9600"), f_w9600, OPT_NUM, OPT_NDISP|OPT_NOSAVE},
228 {L("warn"), NULL, OPT_1BOOL, 0},
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},
244 typedef struct abbrev {
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_TMPDIR}, /* 4BSD */
256 {L("eb"), O_ERRORBELLS}, /* 4BSD */
257 {L("ed"), O_EDCOMPATIBLE}, /* 4BSD */
258 {L("et"), O_EXPANDTAB}, /* NetBSD 5.0 */
259 {L("ex"), O_EXRC}, /* System V (undocumented) */
260 {L("fe"), O_FILEENCODING},
261 {L("ht"), O_HARDTABS}, /* 4BSD */
262 {L("ic"), O_IGNORECASE}, /* 4BSD */
263 {L("ie"), O_INPUTENCODING},
264 {L("li"), O_LINES}, /* 4.4BSD */
265 {L("modelines"), O_MODELINE}, /* HPUX */
266 {L("nu"), O_NUMBER}, /* 4BSD */
267 {L("opt"), O_OPTIMIZE}, /* 4BSD */
268 {L("para"), O_PARAGRAPHS}, /* 4BSD */
269 {L("re"), O_REDRAW}, /* O'Reilly */
270 {L("ro"), O_READONLY}, /* 4BSD (undocumented) */
271 {L("scr"), O_SCROLL}, /* 4BSD (undocumented) */
272 {L("sect"), O_SECTIONS}, /* O'Reilly */
273 {L("sh"), O_SHELL}, /* 4BSD */
274 {L("slow"), O_SLOWOPEN}, /* 4BSD */
275 {L("sm"), O_SHOWMATCH}, /* 4BSD */
276 {L("smd"), O_SHOWMODE}, /* 4BSD */
277 {L("sw"), O_SHIFTWIDTH}, /* 4BSD */
278 {L("tag"), O_TAGS}, /* 4BSD (undocumented) */
279 {L("tl"), O_TAGLENGTH}, /* 4BSD */
280 {L("to"), O_TIMEOUT}, /* 4BSD (undocumented) */
281 {L("ts"), O_TABSTOP}, /* 4BSD */
282 {L("tty"), O_TERM}, /* 4BSD (undocumented) */
283 {L("ttytype"), O_TERM}, /* 4BSD (undocumented) */
284 {L("w"), O_WINDOW}, /* O'Reilly */
285 {L("wa"), O_WRITEANY}, /* 4BSD */
286 {L("wi"), O_WINDOW}, /* 4BSD (undocumented) */
287 {L("wl"), O_WRAPLEN}, /* 4.4BSD */
288 {L("wm"), O_WRAPMARGIN}, /* 4BSD */
289 {L("ws"), O_WRAPSCAN}, /* 4BSD */
295 * Initialize some of the options.
297 * PUBLIC: int opts_init(SCR *, int *);
300 opts_init(SCR *sp, int *oargs)
305 int cnt, optindx = 0;
315 /* Set numeric and string default values. */
316 #define OI(indx, str) do { \
317 a.len = STRLEN(str); \
318 if ((CHAR_T*)str != b2) /* GCC puts strings in text-space. */ \
319 (void)MEMCPY(b2, str, a.len+1); \
320 if (opts_set(sp, argv, NULL)) { \
326 * Indirect global options to global space. Specifically, set up
327 * terminal, lines, columns first, they're used by other options.
328 * Note, don't set the flags until we've set up the indirection.
330 if (o_set(sp, O_TERM, 0, NULL, GO_TERM))
332 F_SET(&sp->opts[O_TERM], OPT_GLOBAL);
333 if (o_set(sp, O_LINES, 0, NULL, GO_LINES))
335 F_SET(&sp->opts[O_LINES], OPT_GLOBAL);
336 if (o_set(sp, O_COLUMNS, 0, NULL, GO_COLUMNS))
338 F_SET(&sp->opts[O_COLUMNS], OPT_GLOBAL);
339 if (o_set(sp, O_SECURE, 0, NULL, GO_SECURE))
341 F_SET(&sp->opts[O_SECURE], OPT_GLOBAL);
343 /* Initialize string values. */
344 (void)SPRINTF(b2, SIZE(b2),
345 L("cdpath=%s"), (s = getenv("CDPATH")) == NULL ? ":" : s);
347 OI(O_CEDIT, L("cedit=\033"));
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);
359 OI(O_ESCAPETIME, L("escapetime=6"));
360 OI(O_FILEC, L("filec=\t"));
361 OI(O_KEYTIME, L("keytime=6"));
362 OI(O_MATCHCHARS, L("matchchars=()[]{}"));
363 OI(O_MATCHTIME, L("matchtime=7"));
364 (void)SPRINTF(b2, SIZE(b2), L("msgcat=%s"), _PATH_MSGCAT);
366 OI(O_REPORT, L("report=5"));
367 OI(O_PARAGRAPHS, L("paragraphs=IPLPPPQPP LIpplpipbp"));
368 (void)SPRINTF(b2, SIZE(b2), L("path=%s"), "");
370 (void)SPRINTF(b2, SIZE(b2), L("recdir=%s"), NVI_PATH_PRESERVE);
372 OI(O_SECTIONS, L("sections=NHSHH HUnhsh"));
373 (void)SPRINTF(b2, SIZE(b2),
374 L("shell=%s"), (s = getenv("SHELL")) == NULL ? _PATH_BSHELL : s);
376 OI(O_SHELLMETA, L("shellmeta=~{[*?$`'\"\\"));
377 OI(O_SHIFTWIDTH, L("shiftwidth=8"));
378 OI(O_SIDESCROLL, L("sidescroll=16"));
379 OI(O_TABSTOP, L("tabstop=8"));
380 (void)SPRINTF(b2, SIZE(b2), L("tags=%s"), _PATH_TAGS);
385 * Initialize O_SCROLL here, after term; initializing term should
386 * have created a LINES/COLUMNS value.
388 if ((v = (O_VAL(sp, O_LINES) - 1) / 2) == 0)
390 (void)SPRINTF(b2, SIZE(b2), L("scroll=%ld"), v);
394 * The default window option values are:
395 * 8 if baud rate <= 600
396 * 16 if baud rate <= 1200
397 * LINES - 1 if baud rate > 1200
399 * Note, the windows option code will correct any too-large value
400 * or when the O_LINES value is 1.
402 if (sp->gp->scr_baud(sp, &v))
408 else if ((v = O_VAL(sp, O_LINES) - 1) == 0)
411 (void)SPRINTF(b2, SIZE(b2), L("window=%lu"), v);
415 * Set boolean default values, and copy all settings into the default
416 * information. OS_NOFREE is set, we're copying, not replacing.
418 for (op = optlist, cnt = 0; op->name != NULL; ++op, ++cnt) {
419 if (F_ISSET(op, OPT_GLOBAL))
429 o_set(sp, cnt, OS_DEF, NULL, O_VAL(sp, cnt));
432 if (O_STR(sp, cnt) != NULL && o_set(sp, cnt,
433 OS_DEF | OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0))
443 * Some options can be initialized by the command name or the
444 * command-line arguments. They don't set the default values,
445 * it's historic practice.
447 for (; *oargs != -1; ++oargs)
448 OI(*oargs, optlist[*oargs].name);
452 err: msgq_wstr(sp, M_ERR, optlist[optindx].name,
453 "031|Unable to set default %s option");
459 * Change the values of one or more options.
461 * PUBLIC: int opts_set(SCR *, ARGS *[], char *);
464 opts_set(SCR *sp, ARGS *argv[], char *usage)
470 u_long isset, turnoff, value;
471 int ch, equals, nf, nf2, offset, qmark, rval;
472 CHAR_T *endp, *name, *p, *sep;
478 for (rval = 0; argv[0]->len != 0; ++argv) {
480 * The historic vi dumped the options for each occurrence of
481 * "all" in the set list. Puhleeze.
483 if (!STRCMP(argv[0]->bp, L("all"))) {
488 /* Find equals sign or question mark. */
489 for (sep = NULL, equals = qmark = 0,
490 p = name = argv[0]->bp; (ch = *p) != '\0'; ++p)
491 if (ch == '=' || ch == '?') {
495 "032|Usage: %s", usage);
511 /* Search for the name, then name without any leading "no". */
512 if ((op = opts_search(name)) == NULL &&
513 name[0] == 'n' && name[1] == 'o') {
516 op = opts_search(name);
519 opts_nomatch(sp, name);
524 /* Find current option values. */
525 offset = op - optlist;
526 spo = sp->opts + offset;
530 * Historically, the question mark could be a separate
533 if (!equals && !qmark &&
534 argv[1]->len == 1 && argv[1]->bp[0] == '?') {
539 /* Set name, value. */
543 /* Some options may not be reset. */
544 if (F_ISSET(op, OPT_NOUNSET) && turnoff) {
545 msgq_wstr(sp, M_ERR, name,
546 "291|set: the %s option may not be turned off");
551 /* Some options may not be set. */
552 if (F_ISSET(op, OPT_NOSET) && !turnoff) {
553 msgq_wstr(sp, M_ERR, name,
554 "313|set: the %s option may never be turned on");
560 msgq_wstr(sp, M_ERR, name,
561 "034|set: [no]%s option doesn't take a value");
567 disp = SELECT_DISPLAY;
568 F_SET(spo, OPT_SELECTED);
573 * Do nothing if the value is unchanged, the underlying
574 * functions can be expensive.
577 if (!F_ISSET(op, OPT_ALWAYS)) {
579 if (O_ISSET(sp, offset))
582 if (!O_ISSET(sp, offset))
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)) {
604 msgq_wstr(sp, M_ERR, name,
605 "035|set: %s option isn't a boolean");
609 if (qmark || !equals) {
611 disp = SELECT_DISPLAY;
612 F_SET(spo, OPT_SELECTED);
616 if (!ISDIGIT(sep[0]))
619 nget_uslong(&value, sep, &endp, 10)) != NUM_OK) {
620 INT2CHAR(sp, name, STRLEN(name) + 1,
622 p2 = msg_print(sp, np, &nf);
623 INT2CHAR(sp, sep, STRLEN(sep) + 1,
625 t2 = msg_print(sp, np, &nf2);
629 "036|set: %s option: %s", p2, t2);
633 "037|set: %s option: %s: value overflow", p2, t2);
640 FREE_SPACE(sp, p2, 0);
642 FREE_SPACE(sp, t2, 0);
646 if (*endp && !cmdskip(*endp)) {
647 badnum: INT2CHAR(sp, name, STRLEN(name) + 1,
649 p2 = msg_print(sp, np, &nf);
650 INT2CHAR(sp, sep, STRLEN(sep) + 1,
652 t2 = msg_print(sp, np, &nf2);
654 "038|set: %s option: %s is an illegal number", p2, t2);
656 FREE_SPACE(sp, p2, 0);
658 FREE_SPACE(sp, t2, 0);
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");
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)
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)) {
691 if (o_set(sp, offset, 0, NULL, value))
696 msgq_wstr(sp, M_ERR, name,
697 "039|set: %s option isn't a boolean");
701 if (qmark || !equals) {
703 disp = SELECT_DISPLAY;
704 F_SET(spo, OPT_SELECTED);
708 /* Check for strings that must have even length. */
709 if (F_ISSET(op, OPT_PAIRS) && STRLEN(sep) & 1) {
710 msgq_wstr(sp, M_ERR, name,
711 "047|The %s option must be in two character groups");
717 * Do nothing if the value is unchanged, the underlying
718 * functions can be expensive.
720 INT2CHAR(sp, sep, STRLEN(sep) + 1, np, nlen);
721 if (!F_ISSET(op, OPT_ALWAYS) &&
722 O_STR(sp, offset) != NULL &&
723 !strcmp(O_STR(sp, offset), np))
726 /* Report to subsystems. */
727 if ((op->func != NULL &&
728 op->func(sp, spo, np, NULL)) ||
729 ex_optchange(sp, offset, np, NULL) ||
730 v_optchange(sp, offset, np, NULL) ||
731 sp->gp->scr_optchange(sp, offset, np, NULL)) {
737 if (o_set(sp, offset, OS_STRDUP, np, 0))
744 if (disp != NO_DISPLAY)
751 * Set an option's value.
753 * PUBLIC: int o_set(SCR *, int, u_int, char *, u_long);
756 o_set(SCR *sp, int opt, u_int flags, char *str, u_long val)
760 /* Set a pointer to the options area. */
761 op = F_ISSET(&sp->opts[opt], OPT_GLOBAL) ?
762 &sp->gp->opts[sp->opts[opt].o_cur.val] : &sp->opts[opt];
764 /* Copy the string, if requested. */
765 if (LF_ISSET(OS_STRDUP) && (str = strdup(str)) == NULL) {
766 msgq(sp, M_SYSERR, NULL);
770 /* Free the previous string, if requested, and set the value. */
772 if (LF_ISSET(OS_STR | OS_STRDUP)) {
773 if (!LF_ISSET(OS_NOFREE))
779 if (LF_ISSET(OS_STR | OS_STRDUP)) {
780 if (!LF_ISSET(OS_NOFREE))
790 * Return 1 if the string option is invalid, 0 if it's OK.
792 * PUBLIC: int opts_empty(SCR *, int, int);
795 opts_empty(SCR *sp, int off, int silent)
799 if ((p = O_STR(sp, off)) == NULL || p[0] == '\0') {
801 msgq_wstr(sp, M_ERR, optlist[off].name,
802 "305|No %s edit option specified");
810 * List the current values of selected options.
812 * PUBLIC: void opts_dump(SCR *, enum optdisp);
815 opts_dump(SCR *sp, enum optdisp type)
818 int base, b_num, cnt, col, colwidth, curlen, s_num;
819 int numcols, numrows, row;
820 int b_op[O_OPTIONCOUNT], s_op[O_OPTIONCOUNT];
824 * Options are output in two groups -- those that fit in a column and
825 * those that don't. Output is done on 6 character "tab" boundaries
826 * for no particular reason. (Since we don't output tab characters,
827 * we can ignore the terminal's tab settings.) Ignore the user's tab
828 * setting because we have no idea how reasonable it is.
830 * Find a column width we can live with, testing from 10 columns to 1.
832 for (numcols = 10; numcols > 1; --numcols) {
833 colwidth = sp->cols / numcols & ~(STANDARD_TAB - 1);
834 if (colwidth >= 10) {
836 (colwidth + STANDARD_TAB) & ~(STANDARD_TAB - 1);
837 numcols = sp->cols / colwidth;
844 * Get the set of options to list, entering them into
845 * the column list or the overflow list.
847 for (b_num = s_num = 0, op = optlist; op->name != NULL; ++op) {
850 /* If OPT_NDISP set, it's never displayed. */
851 if (F_ISSET(op, OPT_NDISP))
855 case ALL_DISPLAY: /* Display all. */
857 case CHANGED_DISPLAY: /* Display changed. */
858 /* If OPT_ADISP set, it's always "changed". */
859 if (F_ISSET(op, OPT_ADISP))
865 if (O_VAL(sp, cnt) == O_D_VAL(sp, cnt))
869 if (O_STR(sp, cnt) == O_D_STR(sp, cnt) ||
870 (O_D_STR(sp, cnt) != NULL &&
871 !strcmp(O_STR(sp, cnt), O_D_STR(sp, cnt))))
876 case SELECT_DISPLAY: /* Display selected. */
877 if (!F_ISSET(&sp->opts[cnt], OPT_SELECTED))
884 F_CLR(&sp->opts[cnt], OPT_SELECTED);
886 curlen = STRLEN(op->name);
890 if (!O_ISSET(sp, cnt))
895 sizeof(nbuf), "%ld", O_VAL(sp, cnt));
896 curlen += strlen(nbuf);
899 if (O_STR(sp, cnt) != NULL)
900 curlen += strlen(O_STR(sp, cnt));
904 /* Offset by 2 so there's a gap. */
905 if (curlen <= colwidth - 2)
912 /* Figure out the number of rows. */
913 if (s_num > numcols) {
914 numrows = s_num / numcols;
920 /* Display the options in sorted order. */
921 for (row = 0; row < numrows;) {
922 for (base = row, col = 0; col < numcols; ++col) {
923 cnt = opts_print(sp, &optlist[s_op[base]]);
924 if ((base += numrows) >= s_num)
926 (void)ex_printf(sp, "%*s",
927 (int)(colwidth - cnt), "");
929 if (++row < numrows || b_num)
930 (void)ex_puts(sp, "\n");
934 for (row = 0; row < b_num;) {
935 (void)opts_print(sp, &optlist[b_op[row]]);
937 (void)ex_puts(sp, "\n");
939 (void)ex_puts(sp, "\n");
944 * Print out an option.
947 opts_print(SCR *sp, OPTLIST const *op)
953 offset = op - optlist;
957 curlen += ex_printf(sp,
958 "%s"WS, O_ISSET(sp, offset) ? "" : "no", op->name);
961 curlen += ex_printf(sp, WS"=%ld", op->name, O_VAL(sp, offset));
964 curlen += ex_printf(sp, WS"=\"", op->name);
965 p = O_STR(sp, offset);
966 /* Keep correct count for unprintable character sequences */
968 for (; *p != '\0'; ++p)
969 curlen += ex_puts(sp, unctrl(*p));
970 curlen += ex_puts(sp, "\"");
978 * Write the current configuration to a file.
980 * PUBLIC: int opts_save(SCR *, FILE *);
983 opts_save(SCR *sp, FILE *fp)
990 for (op = optlist; op->name != NULL; ++op) {
991 if (F_ISSET(op, OPT_NOSAVE))
997 if (O_ISSET(sp, cnt))
998 (void)fprintf(fp, "set "WS"\n", op->name);
1000 (void)fprintf(fp, "set no"WS"\n", op->name);
1004 "set "WS"=%-3ld\n", op->name, O_VAL(sp, cnt));
1007 if (O_STR(sp, cnt) == NULL)
1009 (void)fprintf(fp, "set ");
1010 for (p = op->name; (ch = *p) != '\0'; ++p) {
1011 if (cmdskip(ch) || ch == '\\')
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 (cmdskip(nch) || nch == '\\')
1018 (void)putc('\\', fp);
1019 (void)putc(nch, fp);
1021 (void)putc('\n', fp);
1025 msgq(sp, M_SYSERR, NULL);
1034 * Search for an option.
1036 * PUBLIC: OPTLIST const *opts_search(CHAR_T *);
1039 opts_search(CHAR_T *name)
1041 OPTLIST const *op, *found;
1046 /* Check list of abbreviations. */
1048 if ((ap = bsearch(&atmp, abbrev, sizeof(abbrev) / sizeof(OABBREV) - 1,
1049 sizeof(OABBREV), opts_abbcmp)) != NULL)
1050 return (optlist + ap->offset);
1052 /* Check list of options. */
1054 if ((op = bsearch(&otmp, optlist, sizeof(optlist) / sizeof(OPTLIST) - 1,
1055 sizeof(OPTLIST), opts_cmp)) != NULL)
1059 * Check to see if the name is the prefix of one (and only one)
1060 * option. If so, return the option.
1063 for (found = NULL, op = optlist; op->name != NULL; ++op) {
1064 if (op->name[0] < name[0])
1066 if (op->name[0] > name[0])
1068 if (!MEMCMP(op->name, name, len)) {
1079 * Standard nomatch error message for options.
1081 * PUBLIC: void opts_nomatch(SCR *, CHAR_T *);
1084 opts_nomatch(SCR *sp, CHAR_T *name)
1086 msgq_wstr(sp, M_ERR, name,
1087 "033|set: no %s option: 'set all' gives all option values");
1091 opts_abbcmp(const void *a, const void *b)
1093 return(STRCMP(((OABBREV *)a)->name, ((OABBREV *)b)->name));
1097 opts_cmp(const void *a, const void *b)
1099 return(STRCMP(((OPTLIST *)a)->name, ((OPTLIST *)b)->name));
1104 * Copy a screen's OPTION array.
1106 * PUBLIC: int opts_copy(SCR *, SCR *);
1109 opts_copy(SCR *orig, SCR *sp)
1113 /* Copy most everything without change. */
1114 memcpy(sp->opts, orig->opts, sizeof(orig->opts));
1116 /* Copy the string edit options. */
1117 for (cnt = rval = 0; cnt < O_OPTIONCOUNT; ++cnt) {
1118 if (optlist[cnt].type != OPT_STR ||
1119 F_ISSET(&sp->opts[cnt], OPT_GLOBAL))
1122 * If never set, or already failed, NULL out the entries --
1123 * have to continue after failure, otherwise would have two
1124 * screens referencing the same memory.
1126 if (rval || O_STR(sp, cnt) == NULL) {
1127 o_set(sp, cnt, OS_NOFREE | OS_STR, NULL, 0);
1128 o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
1132 /* Copy the current string. */
1133 if (o_set(sp, cnt, OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0)) {
1134 o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
1138 /* Copy the default string. */
1139 if (O_D_STR(sp, cnt) != NULL && o_set(sp, cnt,
1140 OS_DEF | OS_NOFREE | OS_STRDUP, O_D_STR(sp, cnt), 0)) {
1141 nomem: msgq(orig, M_SYSERR, NULL);
1150 * Free all option strings
1152 * PUBLIC: void opts_free(SCR *);
1159 for (cnt = 0; cnt < O_OPTIONCOUNT; ++cnt) {
1160 if (optlist[cnt].type != OPT_STR ||
1161 F_ISSET(&sp->opts[cnt], OPT_GLOBAL))
1163 free(O_STR(sp, cnt));
1164 free(O_D_STR(sp, cnt));