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
[] = "$Id: options.c,v 10.51 1996/10/14 14:56:28 bostic Exp $ (Berkeley) $Date: 1996/10/14 14:56:28 $";
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 {"hardtabs", NULL
, OPT_NUM
, 0},
85 /* O_ICLOWER 4.4BSD */
86 {"iclower", f_recompile
, OPT_0BOOL
, 0},
87 /* O_IGNORECASE 4BSD */
88 {"ignorecase", f_recompile
, OPT_0BOOL
, 0},
89 /* O_KEYTIME 4.4BSD */
90 {"keytime", NULL
, OPT_NUM
, 0},
91 /* O_LEFTRIGHT 4.4BSD */
92 {"leftright", f_reformat
, OPT_0BOOL
, 0},
94 {"lines", f_lines
, OPT_NUM
, OPT_NOSAVE
},
97 * When the lisp option is implemented, delete the OPT_NOSAVE flag,
98 * so that :mkexrc dumps it.
100 {"lisp", f_lisp
, OPT_0BOOL
, OPT_NOSAVE
},
102 {"list", f_reformat
, OPT_0BOOL
, 0},
103 /* O_LOCKFILES 4.4BSD
105 * Locking isn't reliable enough over NFS to require it, in addition,
106 * it's a serious startup performance problem over some remote links.
108 {"lock", NULL
, OPT_1BOOL
, 0},
110 {"magic", NULL
, OPT_1BOOL
, 0},
111 /* O_MATCHTIME 4.4BSD */
112 {"matchtime", NULL
, OPT_NUM
, 0},
114 {"mesg", NULL
, OPT_1BOOL
, 0},
117 * This has been documented in historical systems as both "modeline"
118 * and as "modelines". Regardless of the name, this option represents
119 * a security problem of mammoth proportions, not to mention a stunning
120 * example of what your intro CS professor referred to as the perils of
121 * mixing code and data. Don't add it, or I will kill you.
123 {"modeline", NULL
, OPT_0BOOL
, OPT_NOSET
},
124 /* O_MSGCAT 4.4BSD */
125 {"msgcat", f_msgcat
, OPT_STR
, 0},
126 /* O_NOPRINT 4.4BSD */
127 {"noprint", f_print
, OPT_STR
, 0},
129 {"number", f_reformat
, OPT_0BOOL
, 0},
131 {"octal", f_print
, OPT_0BOOL
, 0},
133 {"open", NULL
, OPT_1BOOL
, 0},
134 /* O_OPTIMIZE 4BSD */
135 {"optimize", NULL
, OPT_1BOOL
, 0},
136 /* O_PARAGRAPHS 4BSD */
137 {"paragraphs", f_paragraph
, OPT_STR
, 0},
139 {"path", NULL
, OPT_STR
, 0},
141 {"print", f_print
, OPT_STR
, 0},
143 {"prompt", NULL
, OPT_1BOOL
, 0},
144 /* O_READONLY 4BSD (undocumented) */
145 {"readonly", f_readonly
, OPT_0BOOL
, OPT_ALWAYS
},
146 /* O_RECDIR 4.4BSD */
147 {"recdir", NULL
, OPT_STR
, 0},
149 {"redraw", NULL
, OPT_0BOOL
, 0},
151 {"remap", NULL
, OPT_1BOOL
, 0},
153 {"report", NULL
, OPT_NUM
, 0},
155 {"ruler", NULL
, OPT_0BOOL
, 0},
157 {"scroll", NULL
, OPT_NUM
, 0},
158 /* O_SEARCHINCR 4.4BSD */
159 {"searchincr", NULL
, OPT_0BOOL
, 0},
160 /* O_SECTIONS 4BSD */
161 {"sections", f_section
, OPT_STR
, 0},
162 /* O_SECURE 4.4BSD */
163 {"secure", NULL
, OPT_0BOOL
, OPT_NOUNSET
},
165 {"shell", NULL
, OPT_STR
, 0},
166 /* O_SHELLMETA 4.4BSD */
167 {"shellmeta", NULL
, OPT_STR
, 0},
168 /* O_SHIFTWIDTH 4BSD */
169 {"shiftwidth", NULL
, OPT_NUM
, OPT_NOZERO
},
170 /* O_SHOWMATCH 4BSD */
171 {"showmatch", NULL
, OPT_0BOOL
, 0},
172 /* O_SHOWMODE 4.4BSD */
173 {"showmode", NULL
, OPT_0BOOL
, 0},
174 /* O_SIDESCROLL 4.4BSD */
175 {"sidescroll", NULL
, OPT_NUM
, OPT_NOZERO
},
176 /* O_SLOWOPEN 4BSD */
177 {"slowopen", NULL
, OPT_0BOOL
, 0},
178 /* O_SOURCEANY 4BSD (undocumented)
180 * Historic vi, on startup, source'd $HOME/.exrc and ./.exrc, if they
181 * were owned by the user. The sourceany option was an undocumented
182 * feature of historic vi which permitted the startup source'ing of
183 * .exrc files the user didn't own. This is an obvious security problem,
184 * and we ignore the option.
186 {"sourceany", NULL
, OPT_0BOOL
, OPT_NOSET
},
188 {"tabstop", f_reformat
, OPT_NUM
, OPT_NOZERO
},
189 /* O_TAGLENGTH 4BSD */
190 {"taglength", NULL
, OPT_NUM
, 0},
192 {"tags", NULL
, OPT_STR
, 0},
195 * By default, the historic vi always displayed information about two
196 * options, redraw and term. Term seems sufficient.
198 {"term", NULL
, OPT_STR
, OPT_ADISP
|OPT_NOSAVE
},
200 {"terse", NULL
, OPT_0BOOL
, 0},
201 /* O_TILDEOP 4.4BSD */
202 {"tildeop", NULL
, OPT_0BOOL
, 0},
203 /* O_TIMEOUT 4BSD (undocumented) */
204 {"timeout", NULL
, OPT_1BOOL
, 0},
205 /* O_TTYWERASE 4.4BSD */
206 {"ttywerase", f_ttywerase
, OPT_0BOOL
, 0},
207 /* O_VERBOSE 4.4BSD */
208 {"verbose", NULL
, OPT_0BOOL
, 0},
210 {"w1200", f_w1200
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
212 {"w300", f_w300
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
214 {"w9600", f_w9600
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
216 {"warn", NULL
, OPT_1BOOL
, 0},
218 {"window", f_window
, OPT_NUM
, 0},
219 /* O_WINDOWNAME 4BSD */
220 {"windowname", NULL
, OPT_0BOOL
, 0},
221 /* O_WRAPLEN 4.4BSD */
222 {"wraplen", NULL
, OPT_NUM
, 0},
223 /* O_WRAPMARGIN 4BSD */
224 {"wrapmargin", NULL
, OPT_NUM
, 0},
225 /* O_WRAPSCAN 4BSD */
226 {"wrapscan", NULL
, OPT_1BOOL
, 0},
227 /* O_WRITEANY 4BSD */
228 {"writeany", NULL
, OPT_0BOOL
, 0},
232 typedef struct abbrev
{
237 static OABBREV
const abbrev
[] = {
238 {"ai", O_AUTOINDENT
}, /* 4BSD */
239 {"ap", O_AUTOPRINT
}, /* 4BSD */
240 {"aw", O_AUTOWRITE
}, /* 4BSD */
241 {"bf", O_BEAUTIFY
}, /* 4BSD */
242 {"co", O_COLUMNS
}, /* 4.4BSD */
243 {"dir", O_DIRECTORY
}, /* 4BSD */
244 {"eb", O_ERRORBELLS
}, /* 4BSD */
245 {"ed", O_EDCOMPATIBLE
}, /* 4BSD */
246 {"ex", O_EXRC
}, /* System V (undocumented) */
247 {"ht", O_HARDTABS
}, /* 4BSD */
248 {"ic", O_IGNORECASE
}, /* 4BSD */
249 {"li", O_LINES
}, /* 4.4BSD */
250 {"modelines", O_MODELINE
}, /* HPUX */
251 {"nu", O_NUMBER
}, /* 4BSD */
252 {"opt", O_OPTIMIZE
}, /* 4BSD */
253 {"para", O_PARAGRAPHS
}, /* 4BSD */
254 {"re", O_REDRAW
}, /* O'Reilly */
255 {"ro", O_READONLY
}, /* 4BSD (undocumented) */
256 {"scr", O_SCROLL
}, /* 4BSD (undocumented) */
257 {"sect", O_SECTIONS
}, /* O'Reilly */
258 {"sh", O_SHELL
}, /* 4BSD */
259 {"slow", O_SLOWOPEN
}, /* 4BSD */
260 {"sm", O_SHOWMATCH
}, /* 4BSD */
261 {"smd", O_SHOWMODE
}, /* 4BSD */
262 {"sw", O_SHIFTWIDTH
}, /* 4BSD */
263 {"tag", O_TAGS
}, /* 4BSD (undocumented) */
264 {"tl", O_TAGLENGTH
}, /* 4BSD */
265 {"to", O_TIMEOUT
}, /* 4BSD (undocumented) */
266 {"ts", O_TABSTOP
}, /* 4BSD */
267 {"tty", O_TERM
}, /* 4BSD (undocumented) */
268 {"ttytype", O_TERM
}, /* 4BSD (undocumented) */
269 {"w", O_WINDOW
}, /* O'Reilly */
270 {"wa", O_WRITEANY
}, /* 4BSD */
271 {"wi", O_WINDOW
}, /* 4BSD (undocumented) */
272 {"wl", O_WRAPLEN
}, /* 4.4BSD */
273 {"wm", O_WRAPMARGIN
}, /* 4BSD */
274 {"ws", O_WRAPSCAN
}, /* 4BSD */
280 * Initialize some of the options.
282 * PUBLIC: int opts_init __P((SCR *, int *));
301 /* Set numeric and string default values. */
302 #define OI(indx, str) { \
303 if (str != b1) /* GCC puts strings in text-space. */ \
304 (void)strcpy(b1, str); \
305 a.len = strlen(b1); \
306 if (opts_set(sp, argv, NULL)) { \
312 * Indirect global options to global space. Specifically, set up
313 * terminal, lines, columns first, they're used by other options.
314 * Note, don't set the flags until we've set up the indirection.
316 if (o_set(sp
, O_TERM
, 0, NULL
, GO_TERM
))
318 F_SET(&sp
->opts
[O_TERM
], OPT_GLOBAL
);
319 if (o_set(sp
, O_LINES
, 0, NULL
, GO_LINES
))
321 F_SET(&sp
->opts
[O_LINES
], OPT_GLOBAL
);
322 if (o_set(sp
, O_COLUMNS
, 0, NULL
, GO_COLUMNS
))
324 F_SET(&sp
->opts
[O_COLUMNS
], OPT_GLOBAL
);
325 if (o_set(sp
, O_SECURE
, 0, NULL
, GO_SECURE
))
327 F_SET(&sp
->opts
[O_SECURE
], OPT_GLOBAL
);
329 /* Initialize string values. */
330 (void)snprintf(b1
, sizeof(b1
),
331 "cdpath=%s", (s
= getenv("CDPATH")) == NULL
? ":" : s
);
336 * Vi historically stored temporary files in /var/tmp. We store them
337 * in /tmp by default, hoping it's a memory based file system. There
338 * are two ways to change this -- the user can set either the directory
339 * option or the TMPDIR environmental variable.
341 (void)snprintf(b1
, sizeof(b1
),
342 "directory=%s", (s
= getenv("TMPDIR")) == NULL
? _PATH_TMP
: s
);
344 OI(O_ESCAPETIME
, "escapetime=1");
345 OI(O_KEYTIME
, "keytime=6");
346 OI(O_MATCHTIME
, "matchtime=7");
347 (void)snprintf(b1
, sizeof(b1
), "msgcat=%s", _PATH_MSGCAT
);
349 OI(O_REPORT
, "report=5");
350 OI(O_PARAGRAPHS
, "paragraphs=IPLPPPQPP LIpplpipbp");
351 (void)snprintf(b1
, sizeof(b1
), "path=%s", "");
353 (void)snprintf(b1
, sizeof(b1
), "recdir=%s", _PATH_PRESERVE
);
355 OI(O_SECTIONS
, "sections=NHSHH HUnhsh");
356 (void)snprintf(b1
, sizeof(b1
),
357 "shell=%s", (s
= getenv("SHELL")) == NULL
? _PATH_BSHELL
: s
);
359 OI(O_SHELLMETA
, "shellmeta=~{[*?$`'\"\\");
360 OI(O_SHIFTWIDTH
, "shiftwidth=8");
361 OI(O_SIDESCROLL
, "sidescroll=16");
362 OI(O_TABSTOP
, "tabstop=8");
363 (void)snprintf(b1
, sizeof(b1
), "tags=%s", _PATH_TAGS
);
368 * Initialize O_SCROLL here, after term; initializing term should
369 * have created a LINES/COLUMNS value.
371 if ((v
= (O_VAL(sp
, O_LINES
) - 1) / 2) == 0)
373 (void)snprintf(b1
, sizeof(b1
), "scroll=%ld", v
);
377 * The default window option values are:
378 * 8 if baud rate <= 600
379 * 16 if baud rate <= 1200
380 * LINES - 1 if baud rate > 1200
382 * Note, the windows option code will correct any too-large value
383 * or when the O_LINES value is 1.
385 if (sp
->gp
->scr_baud(sp
, &v
))
392 v
= O_VAL(sp
, O_LINES
) - 1;
393 (void)snprintf(b1
, sizeof(b1
), "window=%lu", v
);
397 * Set boolean default values, and copy all settings into the default
398 * information. OS_NOFREE is set, we're copying, not replacing.
400 for (op
= optlist
, cnt
= 0; op
->name
!= NULL
; ++op
, ++cnt
)
409 o_set(sp
, cnt
, OS_DEF
, NULL
, O_VAL(sp
, cnt
));
412 if (O_STR(sp
, cnt
) != NULL
&& o_set(sp
, cnt
,
413 OS_DEF
| OS_NOFREE
| OS_STRDUP
, O_STR(sp
, cnt
), 0))
422 * Some options can be initialized by the command name or the
423 * command-line arguments. They don't set the default values,
424 * it's historic practice.
426 for (; *oargs
!= -1; ++oargs
)
427 OI(*oargs
, optlist
[*oargs
].name
);
432 "031|Unable to set default %s option", optlist
[optindx
].name
);
438 * Change the values of one or more options.
440 * PUBLIC: int opts_set __P((SCR *, ARGS *[], char *));
443 opts_set(sp
, argv
, usage
)
452 u_long value
, turnoff
;
453 int ch
, equals
, nf
, nf2
, offset
, qmark
, rval
;
454 char *endp
, *name
, *p
, *sep
, *t
;
457 for (rval
= 0; argv
[0]->len
!= 0; ++argv
) {
459 * The historic vi dumped the options for each occurrence of
460 * "all" in the set list. Puhleeze.
462 if (!strcmp(argv
[0]->bp
, "all")) {
467 /* Find equals sign or question mark. */
468 for (sep
= NULL
, equals
= qmark
= 0,
469 p
= name
= argv
[0]->bp
; (ch
= *p
) != '\0'; ++p
)
470 if (ch
== '=' || ch
== '?') {
474 "032|Usage: %s", usage
);
490 /* Search for the name, then name without any leading "no". */
491 if ((op
= opts_search(name
)) == NULL
&&
492 name
[0] == 'n' && name
[1] == 'o') {
495 op
= opts_search(name
);
498 opts_nomatch(sp
, name
);
503 /* Find current option values. */
504 offset
= op
- optlist
;
505 spo
= sp
->opts
+ offset
;
509 * Historically, the question mark could be a separate
512 if (!equals
&& !qmark
&&
513 argv
[1]->len
== 1 && argv
[1]->bp
[0] == '?') {
518 /* Set name, value. */
522 /* Some options may not be reset. */
523 if (F_ISSET(op
, OPT_NOUNSET
) && turnoff
) {
524 msgq_str(sp
, M_ERR
, name
,
525 "291|set: the %s option may not be turned off");
530 /* Some options may not be set. */
531 if (F_ISSET(op
, OPT_NOSET
) && !turnoff
) {
532 msgq_str(sp
, M_ERR
, name
,
533 "313|set: the %s option may never be turned on");
539 msgq_str(sp
, M_ERR
, name
,
540 "034|set: [no]%s option doesn't take a value");
546 disp
= SELECT_DISPLAY
;
547 F_SET(spo
, OPT_SELECTED
);
552 * Do nothing if the value is unchanged, the underlying
553 * functions can be expensive.
555 if (!F_ISSET(op
, OPT_ALWAYS
))
557 if (!O_ISSET(sp
, offset
))
560 if (O_ISSET(sp
, offset
))
564 /* Report to subsystems. */
565 if (op
->func
!= NULL
&&
566 op
->func(sp
, spo
, NULL
, &turnoff
) ||
567 ex_optchange(sp
, offset
, NULL
, &turnoff
) ||
568 v_optchange(sp
, offset
, NULL
, &turnoff
) ||
569 sp
->gp
->scr_optchange(sp
, offset
, NULL
, &turnoff
)) {
582 msgq_str(sp
, M_ERR
, name
,
583 "035|set: %s option isn't a boolean");
587 if (qmark
|| !equals
) {
589 disp
= SELECT_DISPLAY
;
590 F_SET(spo
, OPT_SELECTED
);
594 if (!isdigit(sep
[0]))
597 nget_uslong(&value
, sep
, &endp
, 10)) != NUM_OK
) {
598 p
= msg_print(sp
, name
, &nf
);
599 t
= msg_print(sp
, sep
, &nf2
);
603 "036|set: %s option: %s", p
, t
);
607 "037|set: %s option: %s: value overflow", p
, t
);
614 FREE_SPACE(sp
, p
, 0);
616 FREE_SPACE(sp
, t
, 0);
620 if (*endp
&& !isblank(*endp
)) {
621 badnum
: p
= msg_print(sp
, name
, &nf
);
622 t
= msg_print(sp
, sep
, &nf2
);
624 "038|set: %s option: %s is an illegal number", p
, t
);
626 FREE_SPACE(sp
, p
, 0);
628 FREE_SPACE(sp
, t
, 0);
633 /* Some options may never be set to zero. */
634 if (F_ISSET(op
, OPT_NOZERO
) && value
== 0) {
635 msgq_str(sp
, M_ERR
, name
,
636 "314|set: the %s option may never be set to 0");
642 * Do nothing if the value is unchanged, the underlying
643 * functions can be expensive.
645 if (!F_ISSET(op
, OPT_ALWAYS
) &&
646 O_VAL(sp
, offset
) == value
)
649 /* Report to subsystems. */
650 if (op
->func
!= NULL
&&
651 op
->func(sp
, spo
, sep
, &value
) ||
652 ex_optchange(sp
, offset
, sep
, &value
) ||
653 v_optchange(sp
, offset
, sep
, &value
) ||
654 sp
->gp
->scr_optchange(sp
, offset
, sep
, &value
)) {
660 if (o_set(sp
, offset
, 0, NULL
, value
))
665 msgq_str(sp
, M_ERR
, name
,
666 "039|set: %s option isn't a boolean");
670 if (qmark
|| !equals
) {
672 disp
= SELECT_DISPLAY
;
673 F_SET(spo
, OPT_SELECTED
);
678 * Do nothing if the value is unchanged, the underlying
679 * functions can be expensive.
681 if (!F_ISSET(op
, OPT_ALWAYS
) &&
682 O_STR(sp
, offset
) != NULL
&&
683 !strcmp(O_STR(sp
, offset
), sep
))
686 /* Report to subsystems. */
687 if (op
->func
!= NULL
&&
688 op
->func(sp
, spo
, sep
, NULL
) ||
689 ex_optchange(sp
, offset
, sep
, NULL
) ||
690 v_optchange(sp
, offset
, sep
, NULL
) ||
691 sp
->gp
->scr_optchange(sp
, offset
, sep
, NULL
)) {
697 if (o_set(sp
, offset
, OS_STRDUP
, sep
, 0))
704 if (disp
!= NO_DISPLAY
)
711 * Set an option's value.
713 * PUBLIC: int o_set __P((SCR *, int, u_int, char *, u_long));
716 o_set(sp
, opt
, flags
, str
, val
)
725 /* Set a pointer to the options area. */
726 op
= F_ISSET(&sp
->opts
[opt
], OPT_GLOBAL
) ?
727 &sp
->gp
->opts
[sp
->opts
[opt
].o_cur
.val
] : &sp
->opts
[opt
];
729 /* Copy the string, if requested. */
730 if (LF_ISSET(OS_STRDUP
) && (str
= strdup(str
)) == NULL
) {
731 msgq(sp
, M_SYSERR
, NULL
);
735 /* Free the previous string, if requested, and set the value. */
737 if (LF_ISSET(OS_STR
| OS_STRDUP
)) {
738 if (!LF_ISSET(OS_NOFREE
) && op
->o_def
.str
!= NULL
)
744 if (LF_ISSET(OS_STR
| OS_STRDUP
)) {
745 if (!LF_ISSET(OS_NOFREE
) && op
->o_cur
.str
!= NULL
)
755 * Return 1 if the string option is invalid, 0 if it's OK.
757 * PUBLIC: int opts_empty __P((SCR *, int, int));
760 opts_empty(sp
, off
, silent
)
766 if ((p
= O_STR(sp
, off
)) == NULL
|| p
[0] == '\0') {
768 msgq_str(sp
, M_ERR
, optlist
[off
].name
,
769 "305|No %s edit option specified");
777 * List the current values of selected options.
779 * PUBLIC: void opts_dump __P((SCR *, enum optdisp));
787 int base
, b_num
, cnt
, col
, colwidth
, curlen
, s_num
;
788 int numcols
, numrows
, row
;
789 int b_op
[O_OPTIONCOUNT
], s_op
[O_OPTIONCOUNT
];
793 * Options are output in two groups -- those that fit in a column and
794 * those that don't. Output is done on 6 character "tab" boundaries
795 * for no particular reason. (Since we don't output tab characters,
796 * we can ignore the terminal's tab settings.) Ignore the user's tab
797 * setting because we have no idea how reasonable it is.
799 * Find a column width we can live with, testing from 10 columns to 1.
801 for (numcols
= 10; numcols
> 1; --numcols
) {
802 colwidth
= sp
->cols
/ numcols
& ~(STANDARD_TAB
- 1);
803 if (colwidth
>= 10) {
805 (colwidth
+ STANDARD_TAB
) & ~(STANDARD_TAB
- 1);
806 numcols
= sp
->cols
/ colwidth
;
813 * Get the set of options to list, entering them into
814 * the column list or the overflow list.
816 for (b_num
= s_num
= 0, op
= optlist
; op
->name
!= NULL
; ++op
) {
819 /* If OPT_NDISP set, it's never displayed. */
820 if (F_ISSET(op
, OPT_NDISP
))
824 case ALL_DISPLAY
: /* Display all. */
826 case CHANGED_DISPLAY
: /* Display changed. */
827 /* If OPT_ADISP set, it's always "changed". */
828 if (F_ISSET(op
, OPT_ADISP
))
834 if (O_VAL(sp
, cnt
) == O_D_VAL(sp
, cnt
))
838 if (O_STR(sp
, cnt
) == O_D_STR(sp
, cnt
) ||
839 O_D_STR(sp
, cnt
) != NULL
&&
840 !strcmp(O_STR(sp
, cnt
), O_D_STR(sp
, cnt
)))
845 case SELECT_DISPLAY
: /* Display selected. */
846 if (!F_ISSET(&sp
->opts
[cnt
], OPT_SELECTED
))
853 F_CLR(&sp
->opts
[cnt
], OPT_SELECTED
);
855 curlen
= strlen(op
->name
);
859 if (!O_ISSET(sp
, cnt
))
864 sizeof(nbuf
), "%ld", O_VAL(sp
, cnt
));
865 curlen
+= strlen(nbuf
);
868 if (O_STR(sp
, cnt
) != NULL
)
869 curlen
+= strlen(O_STR(sp
, cnt
));
873 /* Offset by 2 so there's a gap. */
874 if (curlen
<= colwidth
- 2)
881 /* Figure out the number of rows. */
882 if (s_num
> numcols
) {
883 numrows
= s_num
/ numcols
;
889 /* Display the options in sorted order. */
890 for (row
= 0; row
< numrows
;) {
891 for (base
= row
, col
= 0; col
< numcols
; ++col
) {
892 cnt
= opts_print(sp
, &optlist
[s_op
[base
]]);
893 if ((base
+= numrows
) >= s_num
)
895 (void)ex_printf(sp
, "%*s",
896 (int)(colwidth
- cnt
), "");
898 if (++row
< numrows
|| b_num
)
899 (void)ex_puts(sp
, "\n");
903 for (row
= 0; row
< b_num
;) {
904 (void)opts_print(sp
, &optlist
[b_op
[row
]]);
906 (void)ex_puts(sp
, "\n");
908 (void)ex_puts(sp
, "\n");
913 * Print out an option.
923 offset
= op
- optlist
;
927 curlen
+= ex_printf(sp
,
928 "%s%s", O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
931 curlen
+= ex_printf(sp
, "%s=%ld", op
->name
, O_VAL(sp
, offset
));
934 curlen
+= ex_printf(sp
, "%s=\"%s\"", op
->name
,
935 O_STR(sp
, offset
) == NULL
? "" : O_STR(sp
, offset
));
943 * Write the current configuration to a file.
945 * PUBLIC: int opts_save __P((SCR *, FILE *));
956 for (op
= optlist
; op
->name
!= NULL
; ++op
) {
957 if (F_ISSET(op
, OPT_NOSAVE
))
963 if (O_ISSET(sp
, cnt
))
964 (void)fprintf(fp
, "set %s\n", op
->name
);
966 (void)fprintf(fp
, "set no%s\n", op
->name
);
970 "set %s=%-3ld\n", op
->name
, O_VAL(sp
, cnt
));
973 if (O_STR(sp
, cnt
) == NULL
)
975 (void)fprintf(fp
, "set ");
976 for (p
= op
->name
; (ch
= *p
) != '\0'; ++p
) {
977 if (isblank(ch
) || ch
== '\\')
978 (void)putc('\\', fp
);
982 for (p
= O_STR(sp
, cnt
); (ch
= *p
) != '\0'; ++p
) {
983 if (isblank(ch
) || ch
== '\\')
984 (void)putc('\\', fp
);
987 (void)putc('\n', fp
);
991 msgq(sp
, M_SYSERR
, NULL
);
1000 * Search for an option.
1002 * PUBLIC: OPTLIST const *opts_search __P((char *));
1008 OPTLIST
const *op
, *found
;
1013 /* Check list of abbreviations. */
1015 if ((ap
= bsearch(&atmp
, abbrev
, sizeof(abbrev
) / sizeof(OABBREV
) - 1,
1016 sizeof(OABBREV
), opts_abbcmp
)) != NULL
)
1017 return (optlist
+ ap
->offset
);
1019 /* Check list of options. */
1021 if ((op
= bsearch(&otmp
, optlist
, sizeof(optlist
) / sizeof(OPTLIST
) - 1,
1022 sizeof(OPTLIST
), opts_cmp
)) != NULL
)
1026 * Check to see if the name is the prefix of one (and only one)
1027 * option. If so, return the option.
1030 for (found
= NULL
, op
= optlist
; op
->name
!= NULL
; ++op
) {
1031 if (op
->name
[0] < name
[0])
1033 if (op
->name
[0] > name
[0])
1035 if (!memcmp(op
->name
, name
, len
)) {
1046 * Standard nomatch error message for options.
1048 * PUBLIC: void opts_nomatch __P((SCR *, char *));
1051 opts_nomatch(sp
, name
)
1055 msgq_str(sp
, M_ERR
, name
,
1056 "033|set: no %s option: 'set all' gives all option values");
1063 return(strcmp(((OABBREV
*)a
)->name
, ((OABBREV
*)b
)->name
));
1070 return(strcmp(((OPTLIST
*)a
)->name
, ((OPTLIST
*)b
)->name
));
1075 * Copy a screen's OPTION array.
1077 * PUBLIC: int opts_copy __P((SCR *, SCR *));
1085 /* Copy most everything without change. */
1086 memcpy(sp
->opts
, orig
->opts
, sizeof(orig
->opts
));
1088 /* Copy the string edit options. */
1089 for (cnt
= rval
= 0; cnt
< O_OPTIONCOUNT
; ++cnt
) {
1090 if (optlist
[cnt
].type
!= OPT_STR
||
1091 F_ISSET(&optlist
[cnt
], OPT_GLOBAL
))
1094 * If never set, or already failed, NULL out the entries --
1095 * have to continue after failure, otherwise would have two
1096 * screens referencing the same memory.
1098 if (rval
|| O_STR(sp
, cnt
) == NULL
) {
1099 o_set(sp
, cnt
, OS_NOFREE
| OS_STR
, NULL
, 0);
1100 o_set(sp
, cnt
, OS_DEF
| OS_NOFREE
| OS_STR
, NULL
, 0);
1104 /* Copy the current string. */
1105 if (o_set(sp
, cnt
, OS_NOFREE
| OS_STRDUP
, O_STR(sp
, cnt
), 0)) {
1106 o_set(sp
, cnt
, OS_DEF
| OS_NOFREE
| OS_STR
, NULL
, 0);
1110 /* Copy the default string. */
1111 if (O_D_STR(sp
, cnt
) != NULL
&& o_set(sp
, cnt
,
1112 OS_DEF
| OS_NOFREE
| OS_STRDUP
, O_D_STR(sp
, cnt
), 0)) {
1113 nomem
: msgq(orig
, M_SYSERR
, NULL
);
1122 * Free all option strings
1124 * PUBLIC: void opts_free __P((SCR *));
1132 for (cnt
= 0; cnt
< O_OPTIONCOUNT
; ++cnt
) {
1133 if (optlist
[cnt
].type
!= OPT_STR
||
1134 F_ISSET(&optlist
[cnt
], OPT_GLOBAL
))
1136 if (O_STR(sp
, cnt
) != NULL
)
1137 free(O_STR(sp
, cnt
));
1138 if (O_D_STR(sp
, cnt
) != NULL
)
1139 free(O_D_STR(sp
, cnt
));