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.
13 static const char sccsid[] = "@(#)options.c 10.51 (Berkeley) 10/14/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
21 #include <bitstring.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
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},
56 {"backup", NULL, OPT_STR, 0},
58 {"beautify", NULL, OPT_0BOOL, 0},
60 {"cdpath", NULL, OPT_STR, 0},
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},
80 {"filec", NULL, OPT_STR, 0},
82 {"flash", NULL, OPT_1BOOL, 0},
84 /* O_GTAGSMODE FreeBSD2.2 */
85 {"gtagsmode", NULL, OPT_0BOOL, 0},
88 {"hardtabs", NULL, OPT_NUM, 0},
89 /* O_ICLOWER 4.4BSD */
90 {"iclower", f_recompile, OPT_0BOOL, 0},
91 /* O_IGNORECASE 4BSD */
92 {"ignorecase", f_recompile, OPT_0BOOL, 0},
93 /* O_KEYTIME 4.4BSD */
94 {"keytime", NULL, OPT_NUM, 0},
95 /* O_LEFTRIGHT 4.4BSD */
96 {"leftright", f_reformat, OPT_0BOOL, 0},
98 {"lines", f_lines, OPT_NUM, OPT_NOSAVE},
101 * When the lisp option is implemented, delete the OPT_NOSAVE flag,
102 * so that :mkexrc dumps it.
104 {"lisp", f_lisp, OPT_0BOOL, OPT_NOSAVE},
106 {"list", f_reformat, OPT_0BOOL, 0},
107 /* O_LOCKFILES 4.4BSD
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 {"lock", NULL, OPT_1BOOL, 0},
114 {"magic", NULL, OPT_1BOOL, 0},
115 /* O_MATCHTIME 4.4BSD */
116 {"matchtime", NULL, OPT_NUM, 0},
118 {"mesg", NULL, OPT_1BOOL, 0},
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 {"modeline", NULL, OPT_0BOOL, OPT_NOSET},
128 /* O_MSGCAT 4.4BSD */
129 {"msgcat", f_msgcat, OPT_STR, 0},
130 /* O_NOPRINT 4.4BSD */
131 {"noprint", f_print, OPT_STR, 0},
133 {"number", f_reformat, OPT_0BOOL, 0},
135 {"octal", f_print, OPT_0BOOL, 0},
137 {"open", NULL, OPT_1BOOL, 0},
138 /* O_OPTIMIZE 4BSD */
139 {"optimize", NULL, OPT_1BOOL, 0},
140 /* O_PARAGRAPHS 4BSD */
141 {"paragraphs", f_paragraph, OPT_STR, 0},
143 {"path", NULL, OPT_STR, 0},
145 {"print", f_print, OPT_STR, 0},
147 {"prompt", NULL, OPT_1BOOL, 0},
148 /* O_READONLY 4BSD (undocumented) */
149 {"readonly", f_readonly, OPT_0BOOL, OPT_ALWAYS},
150 /* O_RECDIR 4.4BSD */
151 {"recdir", NULL, OPT_STR, 0},
153 {"redraw", NULL, OPT_0BOOL, 0},
155 {"remap", NULL, OPT_1BOOL, 0},
157 {"report", NULL, OPT_NUM, 0},
159 {"ruler", NULL, OPT_0BOOL, 0},
161 {"scroll", NULL, OPT_NUM, 0},
162 /* O_SEARCHINCR 4.4BSD */
163 {"searchincr", NULL, OPT_0BOOL, 0},
164 /* O_SECTIONS 4BSD */
165 {"sections", f_section, OPT_STR, 0},
166 /* O_SECURE 4.4BSD */
167 {"secure", NULL, OPT_0BOOL, OPT_NOUNSET},
169 {"shell", NULL, OPT_STR, 0},
170 /* O_SHELLMETA 4.4BSD */
171 {"shellmeta", NULL, OPT_STR, 0},
172 /* O_SHIFTWIDTH 4BSD */
173 {"shiftwidth", NULL, OPT_NUM, OPT_NOZERO},
174 /* O_SHOWMATCH 4BSD */
175 {"showmatch", NULL, OPT_0BOOL, 0},
176 /* O_SHOWMODE 4.4BSD */
177 {"showmode", NULL, OPT_0BOOL, 0},
178 /* O_SIDESCROLL 4.4BSD */
179 {"sidescroll", NULL, OPT_NUM, OPT_NOZERO},
180 /* O_SLOWOPEN 4BSD */
181 {"slowopen", NULL, OPT_0BOOL, 0},
182 /* O_SOURCEANY 4BSD (undocumented)
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 {"sourceany", NULL, OPT_0BOOL, OPT_NOSET},
192 {"tabstop", f_reformat, OPT_NUM, OPT_NOZERO},
193 /* O_TAGLENGTH 4BSD */
194 {"taglength", NULL, OPT_NUM, 0},
196 {"tags", NULL, OPT_STR, 0},
199 * By default, the historic vi always displayed information about two
200 * options, redraw and term. Term seems sufficient.
202 {"term", NULL, OPT_STR, OPT_ADISP|OPT_NOSAVE},
204 {"terse", NULL, OPT_0BOOL, 0},
205 /* O_TILDEOP 4.4BSD */
206 {"tildeop", NULL, OPT_0BOOL, 0},
207 /* O_TIMEOUT 4BSD (undocumented) */
208 {"timeout", NULL, OPT_1BOOL, 0},
209 /* O_TTYWERASE 4.4BSD */
210 {"ttywerase", f_ttywerase, OPT_0BOOL, 0},
211 /* O_VERBOSE 4.4BSD */
212 {"verbose", NULL, OPT_0BOOL, 0},
214 {"w1200", f_w1200, OPT_NUM, OPT_NDISP|OPT_NOSAVE},
216 {"w300", f_w300, OPT_NUM, OPT_NDISP|OPT_NOSAVE},
218 {"w9600", f_w9600, OPT_NUM, OPT_NDISP|OPT_NOSAVE},
220 {"warn", NULL, OPT_1BOOL, 0},
222 {"window", f_window, OPT_NUM, 0},
223 /* O_WINDOWNAME 4BSD */
224 {"windowname", NULL, OPT_0BOOL, 0},
225 /* O_WRAPLEN 4.4BSD */
226 {"wraplen", NULL, OPT_NUM, 0},
227 /* O_WRAPMARGIN 4BSD */
228 {"wrapmargin", NULL, OPT_NUM, 0},
229 /* O_WRAPSCAN 4BSD */
230 {"wrapscan", NULL, OPT_1BOOL, 0},
231 /* O_WRITEANY 4BSD */
232 {"writeany", NULL, OPT_0BOOL, 0},
236 typedef struct abbrev {
241 static OABBREV const abbrev[] = {
242 {"ai", O_AUTOINDENT}, /* 4BSD */
243 {"ap", O_AUTOPRINT}, /* 4BSD */
244 {"aw", O_AUTOWRITE}, /* 4BSD */
245 {"bf", O_BEAUTIFY}, /* 4BSD */
246 {"co", O_COLUMNS}, /* 4.4BSD */
247 {"dir", O_DIRECTORY}, /* 4BSD */
248 {"eb", O_ERRORBELLS}, /* 4BSD */
249 {"ed", O_EDCOMPATIBLE}, /* 4BSD */
250 {"ex", O_EXRC}, /* System V (undocumented) */
252 {"gt", O_GTAGSMODE}, /* FreeBSD2.2 */
254 {"ht", O_HARDTABS}, /* 4BSD */
255 {"ic", O_IGNORECASE}, /* 4BSD */
256 {"li", O_LINES}, /* 4.4BSD */
257 {"modelines", O_MODELINE}, /* HPUX */
258 {"nu", O_NUMBER}, /* 4BSD */
259 {"opt", O_OPTIMIZE}, /* 4BSD */
260 {"para", O_PARAGRAPHS}, /* 4BSD */
261 {"re", O_REDRAW}, /* O'Reilly */
262 {"ro", O_READONLY}, /* 4BSD (undocumented) */
263 {"scr", O_SCROLL}, /* 4BSD (undocumented) */
264 {"sect", O_SECTIONS}, /* O'Reilly */
265 {"sh", O_SHELL}, /* 4BSD */
266 {"slow", O_SLOWOPEN}, /* 4BSD */
267 {"sm", O_SHOWMATCH}, /* 4BSD */
268 {"smd", O_SHOWMODE}, /* 4BSD */
269 {"sw", O_SHIFTWIDTH}, /* 4BSD */
270 {"tag", O_TAGS}, /* 4BSD (undocumented) */
271 {"tl", O_TAGLENGTH}, /* 4BSD */
272 {"to", O_TIMEOUT}, /* 4BSD (undocumented) */
273 {"ts", O_TABSTOP}, /* 4BSD */
274 {"tty", O_TERM}, /* 4BSD (undocumented) */
275 {"ttytype", O_TERM}, /* 4BSD (undocumented) */
276 {"w", O_WINDOW}, /* O'Reilly */
277 {"wa", O_WRITEANY}, /* 4BSD */
278 {"wi", O_WINDOW}, /* 4BSD (undocumented) */
279 {"wl", O_WRAPLEN}, /* 4.4BSD */
280 {"wm", O_WRAPMARGIN}, /* 4BSD */
281 {"ws", O_WRAPSCAN}, /* 4BSD */
287 * Initialize some of the options.
289 * PUBLIC: int opts_init __P((SCR *, int *));
308 /* Set numeric and string default values. */
309 #define OI(indx, str) { \
310 if (str != b1) /* GCC puts strings in text-space. */ \
311 (void)strcpy(b1, str); \
312 a.len = strlen(b1); \
313 if (opts_set(sp, argv, NULL)) { \
319 * Indirect global options to global space. Specifically, set up
320 * terminal, lines, columns first, they're used by other options.
321 * Note, don't set the flags until we've set up the indirection.
323 if (o_set(sp, O_TERM, 0, NULL, GO_TERM))
325 F_SET(&sp->opts[O_TERM], OPT_GLOBAL);
326 if (o_set(sp, O_LINES, 0, NULL, GO_LINES))
328 F_SET(&sp->opts[O_LINES], OPT_GLOBAL);
329 if (o_set(sp, O_COLUMNS, 0, NULL, GO_COLUMNS))
331 F_SET(&sp->opts[O_COLUMNS], OPT_GLOBAL);
332 if (o_set(sp, O_SECURE, 0, NULL, GO_SECURE))
334 F_SET(&sp->opts[O_SECURE], OPT_GLOBAL);
336 /* Initialize string values. */
337 (void)snprintf(b1, sizeof(b1),
338 "cdpath=%s", (s = getenv("CDPATH")) == NULL ? ":" : s);
343 * Vi historically stored temporary files in /var/tmp. We store them
344 * in /tmp by default, hoping it's a memory based file system. There
345 * are two ways to change this -- the user can set either the directory
346 * option or the TMPDIR environmental variable.
348 (void)snprintf(b1, sizeof(b1),
349 "directory=%s", (s = getenv("TMPDIR")) == NULL ? _PATH_TMP : s);
351 OI(O_ESCAPETIME, "escapetime=6");
352 OI(O_KEYTIME, "keytime=6");
353 OI(O_MATCHTIME, "matchtime=7");
354 (void)snprintf(b1, sizeof(b1), "msgcat=%s", _PATH_MSGCAT);
356 OI(O_REPORT, "report=5");
357 OI(O_PARAGRAPHS, "paragraphs=IPLPPPQPP LIpplpipbp");
358 (void)snprintf(b1, sizeof(b1), "path=%s", "");
360 (void)snprintf(b1, sizeof(b1), "recdir=%s", _PATH_PRESERVE);
362 OI(O_SECTIONS, "sections=NHSHH HUnhsh");
363 (void)snprintf(b1, sizeof(b1),
364 "shell=%s", (s = getenv("SHELL")) == NULL ? _PATH_BSHELL : s);
366 OI(O_SHELLMETA, "shellmeta=~{[*?$`'\"\\");
367 OI(O_SHIFTWIDTH, "shiftwidth=8");
368 OI(O_SIDESCROLL, "sidescroll=16");
369 OI(O_TABSTOP, "tabstop=8");
370 (void)snprintf(b1, sizeof(b1), "tags=%s", _PATH_TAGS);
375 * Initialize O_SCROLL here, after term; initializing term should
376 * have created a LINES/COLUMNS value.
378 if ((v = (O_VAL(sp, O_LINES) - 1) / 2) == 0)
380 (void)snprintf(b1, sizeof(b1), "scroll=%ld", v);
384 * The default window option values are:
385 * 8 if baud rate <= 600
386 * 16 if baud rate <= 1200
387 * LINES - 1 if baud rate > 1200
389 * Note, the windows option code will correct any too-large value
390 * or when the O_LINES value is 1.
392 if (sp->gp->scr_baud(sp, &v))
399 v = O_VAL(sp, O_LINES) - 1;
400 (void)snprintf(b1, sizeof(b1), "window=%lu", v);
404 * Set boolean default values, and copy all settings into the default
405 * information. OS_NOFREE is set, we're copying, not replacing.
407 for (op = optlist, cnt = 0; op->name != NULL; ++op, ++cnt)
416 o_set(sp, cnt, OS_DEF, NULL, O_VAL(sp, cnt));
419 if (O_STR(sp, cnt) != NULL && o_set(sp, cnt,
420 OS_DEF | OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0))
429 * Some options can be initialized by the command name or the
430 * command-line arguments. They don't set the default values,
431 * it's historic practice.
433 for (; *oargs != -1; ++oargs)
434 OI(*oargs, optlist[*oargs].name);
439 "031|Unable to set default %s option", optlist[optindx].name);
445 * Change the values of one or more options.
447 * PUBLIC: int opts_set __P((SCR *, ARGS *[], char *));
450 opts_set(sp, argv, usage)
459 u_long value, turnoff;
460 int ch, equals, nf, nf2, offset, qmark, rval;
461 char *endp, *name, *p, *sep, *t;
464 for (rval = 0; argv[0]->len != 0; ++argv) {
466 * The historic vi dumped the options for each occurrence of
467 * "all" in the set list. Puhleeze.
469 if (!strcmp(argv[0]->bp, "all")) {
474 /* Find equals sign or question mark. */
475 for (sep = NULL, equals = qmark = 0,
476 p = name = argv[0]->bp; (ch = *p) != '\0'; ++p)
477 if (ch == '=' || ch == '?') {
481 "032|Usage: %s", usage);
497 /* Search for the name, then name without any leading "no". */
498 if ((op = opts_search(name)) == NULL &&
499 name[0] == 'n' && name[1] == 'o') {
502 op = opts_search(name);
505 opts_nomatch(sp, name);
510 /* Find current option values. */
511 offset = op - optlist;
512 spo = sp->opts + offset;
516 * Historically, the question mark could be a separate
519 if (!equals && !qmark &&
520 argv[1]->len == 1 && argv[1]->bp[0] == '?') {
525 /* Set name, value. */
529 /* Some options may not be reset. */
530 if (F_ISSET(op, OPT_NOUNSET) && turnoff) {
531 msgq_str(sp, M_ERR, name,
532 "291|set: the %s option may not be turned off");
537 /* Some options may not be set. */
538 if (F_ISSET(op, OPT_NOSET) && !turnoff) {
539 msgq_str(sp, M_ERR, name,
540 "313|set: the %s option may never be turned on");
546 msgq_str(sp, M_ERR, name,
547 "034|set: [no]%s option doesn't take a value");
553 disp = SELECT_DISPLAY;
554 F_SET(spo, OPT_SELECTED);
559 * Do nothing if the value is unchanged, the underlying
560 * functions can be expensive.
562 if (!F_ISSET(op, OPT_ALWAYS))
564 if (!O_ISSET(sp, offset))
567 if (O_ISSET(sp, offset))
571 /* Report to subsystems. */
572 if (op->func != NULL &&
573 op->func(sp, spo, NULL, &turnoff) ||
574 ex_optchange(sp, offset, NULL, &turnoff) ||
575 v_optchange(sp, offset, NULL, &turnoff) ||
576 sp->gp->scr_optchange(sp, offset, NULL, &turnoff)) {
589 msgq_str(sp, M_ERR, name,
590 "035|set: %s option isn't a boolean");
594 if (qmark || !equals) {
596 disp = SELECT_DISPLAY;
597 F_SET(spo, OPT_SELECTED);
601 if (!isdigit(sep[0]))
604 nget_uslong(&value, sep, &endp, 10)) != NUM_OK) {
605 p = msg_print(sp, name, &nf);
606 t = msg_print(sp, sep, &nf2);
610 "036|set: %s option: %s", p, t);
614 "037|set: %s option: %s: value overflow", p, t);
621 FREE_SPACE(sp, p, 0);
623 FREE_SPACE(sp, t, 0);
627 if (*endp && !isblank(*endp)) {
628 badnum: p = msg_print(sp, name, &nf);
629 t = msg_print(sp, sep, &nf2);
631 "038|set: %s option: %s is an illegal number", p, t);
633 FREE_SPACE(sp, p, 0);
635 FREE_SPACE(sp, t, 0);
640 /* Some options may never be set to zero. */
641 if (F_ISSET(op, OPT_NOZERO) && value == 0) {
642 msgq_str(sp, M_ERR, name,
643 "314|set: the %s option may never be set to 0");
649 * Do nothing if the value is unchanged, the underlying
650 * functions can be expensive.
652 if (!F_ISSET(op, OPT_ALWAYS) &&
653 O_VAL(sp, offset) == value)
656 /* Report to subsystems. */
657 if (op->func != NULL &&
658 op->func(sp, spo, sep, &value) ||
659 ex_optchange(sp, offset, sep, &value) ||
660 v_optchange(sp, offset, sep, &value) ||
661 sp->gp->scr_optchange(sp, offset, sep, &value)) {
667 if (o_set(sp, offset, 0, NULL, value))
672 msgq_str(sp, M_ERR, name,
673 "039|set: %s option isn't a boolean");
677 if (qmark || !equals) {
679 disp = SELECT_DISPLAY;
680 F_SET(spo, OPT_SELECTED);
685 * Do nothing if the value is unchanged, the underlying
686 * functions can be expensive.
688 if (!F_ISSET(op, OPT_ALWAYS) &&
689 O_STR(sp, offset) != NULL &&
690 !strcmp(O_STR(sp, offset), sep))
693 /* Report to subsystems. */
694 if (op->func != NULL &&
695 op->func(sp, spo, sep, NULL) ||
696 ex_optchange(sp, offset, sep, NULL) ||
697 v_optchange(sp, offset, sep, NULL) ||
698 sp->gp->scr_optchange(sp, offset, sep, NULL)) {
704 if (o_set(sp, offset, OS_STRDUP, sep, 0))
711 if (disp != NO_DISPLAY)
718 * Set an option's value.
720 * PUBLIC: int o_set __P((SCR *, int, u_int, char *, u_long));
723 o_set(sp, opt, flags, str, val)
732 /* Set a pointer to the options area. */
733 op = F_ISSET(&sp->opts[opt], OPT_GLOBAL) ?
734 &sp->gp->opts[sp->opts[opt].o_cur.val] : &sp->opts[opt];
736 /* Copy the string, if requested. */
737 if (LF_ISSET(OS_STRDUP) && (str = strdup(str)) == NULL) {
738 msgq(sp, M_SYSERR, NULL);
742 /* Free the previous string, if requested, and set the value. */
744 if (LF_ISSET(OS_STR | OS_STRDUP)) {
745 if (!LF_ISSET(OS_NOFREE) && op->o_def.str != NULL)
751 if (LF_ISSET(OS_STR | OS_STRDUP)) {
752 if (!LF_ISSET(OS_NOFREE) && op->o_cur.str != NULL)
762 * Return 1 if the string option is invalid, 0 if it's OK.
764 * PUBLIC: int opts_empty __P((SCR *, int, int));
767 opts_empty(sp, off, silent)
773 if ((p = O_STR(sp, off)) == NULL || p[0] == '\0') {
775 msgq_str(sp, M_ERR, optlist[off].name,
776 "305|No %s edit option specified");
784 * List the current values of selected options.
786 * PUBLIC: void opts_dump __P((SCR *, enum optdisp));
794 int base, b_num, cnt, col, colwidth, curlen, s_num;
795 int numcols, numrows, row;
796 int b_op[O_OPTIONCOUNT], s_op[O_OPTIONCOUNT];
800 * Options are output in two groups -- those that fit in a column and
801 * those that don't. Output is done on 6 character "tab" boundaries
802 * for no particular reason. (Since we don't output tab characters,
803 * we can ignore the terminal's tab settings.) Ignore the user's tab
804 * setting because we have no idea how reasonable it is.
806 * Find a column width we can live with, testing from 10 columns to 1.
808 for (numcols = 10; numcols > 1; --numcols) {
809 colwidth = sp->cols / numcols & ~(STANDARD_TAB - 1);
810 if (colwidth >= 10) {
812 (colwidth + STANDARD_TAB) & ~(STANDARD_TAB - 1);
813 numcols = sp->cols / colwidth;
820 * Get the set of options to list, entering them into
821 * the column list or the overflow list.
823 for (b_num = s_num = 0, op = optlist; op->name != NULL; ++op) {
826 /* If OPT_NDISP set, it's never displayed. */
827 if (F_ISSET(op, OPT_NDISP))
831 case ALL_DISPLAY: /* Display all. */
833 case CHANGED_DISPLAY: /* Display changed. */
834 /* If OPT_ADISP set, it's always "changed". */
835 if (F_ISSET(op, OPT_ADISP))
841 if (O_VAL(sp, cnt) == O_D_VAL(sp, cnt))
845 if (O_STR(sp, cnt) == O_D_STR(sp, cnt) ||
846 O_D_STR(sp, cnt) != NULL &&
847 !strcmp(O_STR(sp, cnt), O_D_STR(sp, cnt)))
852 case SELECT_DISPLAY: /* Display selected. */
853 if (!F_ISSET(&sp->opts[cnt], OPT_SELECTED))
860 F_CLR(&sp->opts[cnt], OPT_SELECTED);
862 curlen = strlen(op->name);
866 if (!O_ISSET(sp, cnt))
871 sizeof(nbuf), "%ld", O_VAL(sp, cnt));
872 curlen += strlen(nbuf);
875 if (O_STR(sp, cnt) != NULL)
876 curlen += strlen(O_STR(sp, cnt));
880 /* Offset by 2 so there's a gap. */
881 if (curlen <= colwidth - 2)
888 /* Figure out the number of rows. */
889 if (s_num > numcols) {
890 numrows = s_num / numcols;
896 /* Display the options in sorted order. */
897 for (row = 0; row < numrows;) {
898 for (base = row, col = 0; col < numcols; ++col) {
899 cnt = opts_print(sp, &optlist[s_op[base]]);
900 if ((base += numrows) >= s_num)
902 (void)ex_printf(sp, "%*s",
903 (int)(colwidth - cnt), "");
905 if (++row < numrows || b_num)
906 (void)ex_puts(sp, "\n");
910 for (row = 0; row < b_num;) {
911 (void)opts_print(sp, &optlist[b_op[row]]);
913 (void)ex_puts(sp, "\n");
915 (void)ex_puts(sp, "\n");
920 * Print out an option.
930 offset = op - optlist;
934 curlen += ex_printf(sp,
935 "%s%s", O_ISSET(sp, offset) ? "" : "no", op->name);
938 curlen += ex_printf(sp, "%s=%ld", op->name, O_VAL(sp, offset));
941 curlen += ex_printf(sp, "%s=\"%s\"", op->name,
942 O_STR(sp, offset) == NULL ? "" : O_STR(sp, offset));
950 * Write the current configuration to a file.
952 * PUBLIC: int opts_save __P((SCR *, FILE *));
963 for (op = optlist; op->name != NULL; ++op) {
964 if (F_ISSET(op, OPT_NOSAVE))
970 if (O_ISSET(sp, cnt))
971 (void)fprintf(fp, "set %s\n", op->name);
973 (void)fprintf(fp, "set no%s\n", op->name);
977 "set %s=%-3ld\n", op->name, O_VAL(sp, cnt));
980 if (O_STR(sp, cnt) == NULL)
982 (void)fprintf(fp, "set ");
983 for (p = op->name; (ch = *p) != '\0'; ++p) {
984 if (isblank(ch) || ch == '\\')
985 (void)putc('\\', fp);
989 for (p = O_STR(sp, cnt); (ch = *p) != '\0'; ++p) {
990 if (isblank(ch) || ch == '\\')
991 (void)putc('\\', fp);
994 (void)putc('\n', fp);
998 msgq(sp, M_SYSERR, NULL);
1007 * Search for an option.
1009 * PUBLIC: OPTLIST const *opts_search __P((char *));
1015 OPTLIST const *op, *found;
1020 /* Check list of abbreviations. */
1022 if ((ap = bsearch(&atmp, abbrev, sizeof(abbrev) / sizeof(OABBREV) - 1,
1023 sizeof(OABBREV), opts_abbcmp)) != NULL)
1024 return (optlist + ap->offset);
1026 /* Check list of options. */
1028 if ((op = bsearch(&otmp, optlist, sizeof(optlist) / sizeof(OPTLIST) - 1,
1029 sizeof(OPTLIST), opts_cmp)) != NULL)
1033 * Check to see if the name is the prefix of one (and only one)
1034 * option. If so, return the option.
1037 for (found = NULL, op = optlist; op->name != NULL; ++op) {
1038 if (op->name[0] < name[0])
1040 if (op->name[0] > name[0])
1042 if (!memcmp(op->name, name, len)) {
1053 * Standard nomatch error message for options.
1055 * PUBLIC: void opts_nomatch __P((SCR *, char *));
1058 opts_nomatch(sp, name)
1062 msgq_str(sp, M_ERR, name,
1063 "033|set: no %s option: 'set all' gives all option values");
1070 return(strcmp(((OABBREV *)a)->name, ((OABBREV *)b)->name));
1077 return(strcmp(((OPTLIST *)a)->name, ((OPTLIST *)b)->name));
1082 * Copy a screen's OPTION array.
1084 * PUBLIC: int opts_copy __P((SCR *, SCR *));
1092 /* Copy most everything without change. */
1093 memcpy(sp->opts, orig->opts, sizeof(orig->opts));
1095 /* Copy the string edit options. */
1096 for (cnt = rval = 0; cnt < O_OPTIONCOUNT; ++cnt) {
1097 if (optlist[cnt].type != OPT_STR ||
1098 F_ISSET(&optlist[cnt], OPT_GLOBAL))
1101 * If never set, or already failed, NULL out the entries --
1102 * have to continue after failure, otherwise would have two
1103 * screens referencing the same memory.
1105 if (rval || O_STR(sp, cnt) == NULL) {
1106 o_set(sp, cnt, OS_NOFREE | OS_STR, NULL, 0);
1107 o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
1111 /* Copy the current string. */
1112 if (o_set(sp, cnt, OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0)) {
1113 o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
1117 /* Copy the default string. */
1118 if (O_D_STR(sp, cnt) != NULL && o_set(sp, cnt,
1119 OS_DEF | OS_NOFREE | OS_STRDUP, O_D_STR(sp, cnt), 0)) {
1120 nomem: msgq(orig, M_SYSERR, NULL);
1129 * Free all option strings
1131 * PUBLIC: void opts_free __P((SCR *));
1139 for (cnt = 0; cnt < O_OPTIONCOUNT; ++cnt) {
1140 if (optlist[cnt].type != OPT_STR ||
1141 F_ISSET(&optlist[cnt], OPT_GLOBAL))
1143 if (O_STR(sp, cnt) != NULL)
1144 free(O_STR(sp, cnt));
1145 if (O_D_STR(sp, cnt) != NULL)
1146 free(O_D_STR(sp, cnt));