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.64 2001/07/08 13:26:49 skimo Exp $ (Berkeley) $Date: 2001/07/08 13:26:49 $";
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 *));
41 #define OPT_WC (OPT_NOSAVE | OPT_NDISP)
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
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},
62 {L("backup"), NULL
, OPT_STR
, 0},
64 {L("beautify"), NULL
, OPT_0BOOL
, 0},
66 {L("cdpath"), NULL
, OPT_STR
, 0},
68 {L("cedit"), NULL
, OPT_STR
, 0},
69 /* O_COLUMNS 4.4BSD */
70 {L("columns"), f_columns
, OPT_NUM
, OPT_NOSAVE
},
72 {L("combined"), NULL
, OPT_0BOOL
, OPT_NOSET
|OPT_WC
},
73 /* O_COMMENT 4.4BSD */
74 {L("comment"), NULL
, OPT_0BOOL
, 0},
75 /* O_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},
88 {L("filec"), NULL
, OPT_STR
, 0},
90 {L("fileencoding"),f_encoding
, OPT_STR
, OPT_WC
},
92 {L("flash"), NULL
, OPT_1BOOL
, 0},
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},
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},
106 {L("lines"), f_lines
, OPT_NUM
, OPT_NOSAVE
},
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
},
114 {L("list"), f_reformat
, OPT_0BOOL
, 0},
115 /* O_LOCKFILES 4.4BSD
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},
122 {L("magic"), NULL
, OPT_1BOOL
, 0},
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"), f_paragraph
, OPT_STR
, 0},
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"), f_section
, OPT_STR
, 0},
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_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 */
294 * Initialize some of the options.
296 * PUBLIC: int opts_init __P((SCR *, int *));
299 opts_init(SCR
*sp
, int *oargs
)
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)) { \
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
))
333 F_SET(&sp
->opts
[O_TERM
], OPT_GLOBAL
);
334 if (o_set(sp
, O_LINES
, 0, NULL
, GO_LINES
))
336 F_SET(&sp
->opts
[O_LINES
], OPT_GLOBAL
);
337 if (o_set(sp
, O_COLUMNS
, 0, NULL
, GO_COLUMNS
))
339 F_SET(&sp
->opts
[O_COLUMNS
], OPT_GLOBAL
);
340 if (o_set(sp
, O_SECURE
, 0, NULL
, GO_SECURE
))
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
);
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=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
);
364 OI(O_REPORT
, L("report=5"));
365 OI(O_PARAGRAPHS
, L("paragraphs=IPLPPPQPP LIpplpipbp"));
366 (void)SPRINTF(b2
, SIZE(b2
), L("path=%s"), "");
368 (void)SPRINTF(b2
, SIZE(b2
), L("recdir=%s"), _PATH_PRESERVE
);
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
);
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
);
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)
388 (void)SPRINTF(b2
, SIZE(b2
), L("scroll=%ld"), v
);
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
))
406 else if ((v
= O_VAL(sp
, O_LINES
) - 1) == 0)
409 (void)SPRINTF(b2
, SIZE(b2
), L("window=%lu"), v
);
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
)
425 o_set(sp
, cnt
, OS_DEF
, NULL
, O_VAL(sp
, cnt
));
428 if (O_STR(sp
, cnt
) != NULL
&& o_set(sp
, cnt
,
429 OS_DEF
| OS_NOFREE
| OS_STRDUP
, O_STR(sp
, cnt
), 0))
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
);
447 * Inform the underlying screen of the initial values of the
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
);
457 "031|Unable to set default %s option", optlist
[optindx
].name
);
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
)
474 u_long isset
, turnoff
, value
;
475 int ch
, equals
, nf
, nf2
, offset
, qmark
, rval
;
476 CHAR_T
*endp
, *name
, *p
, *sep
, *t
;
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"))) {
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
== '?') {
499 "032|Usage: %s", usage
);
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')) {
520 op
= opts_search(name
);
523 opts_nomatch(sp
, name
);
528 /* Find current option values. */
529 offset
= op
- optlist
;
530 spo
= sp
->opts
+ offset
;
534 * Historically, the question mark could be a separate
537 if (!equals
&& !qmark
&&
538 argv
[1]->len
== 1 && argv
[1]->bp
[0] == '?') {
543 /* Set name, value. */
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");
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");
564 msgq_wstr(sp
, M_ERR
, name
,
565 "034|set: [no]%s option doesn't take a value");
571 disp
= SELECT_DISPLAY
;
572 F_SET(spo
, OPT_SELECTED
);
577 * Do nothing if the value is unchanged, the underlying
578 * functions can be expensive.
581 if (!F_ISSET(op
, OPT_ALWAYS
))
583 if (O_ISSET(sp
, offset
))
586 if (!O_ISSET(sp
, offset
))
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
)) {
607 msgq_wstr(sp
, M_ERR
, name
,
608 "035|set: %s option isn't a boolean");
612 if (qmark
|| !equals
) {
614 disp
= SELECT_DISPLAY
;
615 F_SET(spo
, OPT_SELECTED
);
619 if (!ISDIGIT(sep
[0]))
622 nget_uslong(sp
, &value
, sep
, &endp
, 10)) != NUM_OK
) {
623 INT2CHAR(sp
, name
, STRLEN(name
) + 1,
625 p2
= msg_print(sp
, np
, &nf
);
626 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1,
628 t2
= msg_print(sp
, np
, &nf2
);
632 "036|set: %s option: %s", p2
, t2
);
636 "037|set: %s option: %s: value overflow", p2
, t2
);
643 FREE_SPACE(sp
, p2
, 0);
645 FREE_SPACE(sp
, t2
, 0);
649 if (*endp
&& !ISBLANK(*endp
)) {
650 badnum
: INT2CHAR(sp
, name
, STRLEN(name
) + 1,
652 p2
= msg_print(sp
, np
, &nf
);
653 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1,
655 t2
= msg_print(sp
, np
, &nf2
);
657 "038|set: %s option: %s is an illegal number", p2
, t2
);
659 FREE_SPACE(sp
, p2
, 0);
661 FREE_SPACE(sp
, t2
, 0);
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");
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
)
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
)) {
694 if (o_set(sp
, offset
, 0, NULL
, value
))
699 msgq_wstr(sp
, M_ERR
, name
,
700 "039|set: %s option isn't a boolean");
704 if (qmark
|| !equals
) {
706 disp
= SELECT_DISPLAY
;
707 F_SET(spo
, OPT_SELECTED
);
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
))
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
)) {
732 if (o_set(sp
, offset
, OS_STRDUP
, np
, 0))
739 if (disp
!= NO_DISPLAY
)
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
)
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
);
765 /* Free the previous string, if requested, and set the value. */
767 if (LF_ISSET(OS_STR
| OS_STRDUP
)) {
768 if (!LF_ISSET(OS_NOFREE
) && op
->o_def
.str
!= NULL
)
774 if (LF_ISSET(OS_STR
| OS_STRDUP
)) {
775 if (!LF_ISSET(OS_NOFREE
) && op
->o_cur
.str
!= NULL
)
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
)
794 if ((p
= O_STR(sp
, off
)) == NULL
|| p
[0] == '\0') {
796 msgq_wstr(sp
, M_ERR
, optlist
[off
].name
,
797 "305|No %s edit option specified");
805 * List the current values of selected options.
807 * PUBLIC: void opts_dump __P((SCR *, enum optdisp));
810 opts_dump(SCR
*sp
, enum optdisp type
)
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
];
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) {
832 (colwidth
+ STANDARD_TAB
) & ~(STANDARD_TAB
- 1);
833 numcols
= sp
->cols
/ colwidth
;
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
) {
846 /* If OPT_NDISP set, it's never displayed. */
847 if (F_ISSET(op
, OPT_NDISP
))
851 case ALL_DISPLAY
: /* Display all. */
853 case CHANGED_DISPLAY
: /* Display changed. */
854 /* If OPT_ADISP set, it's always "changed". */
855 if (F_ISSET(op
, OPT_ADISP
))
861 if (O_VAL(sp
, cnt
) == O_D_VAL(sp
, cnt
))
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
)))
872 case SELECT_DISPLAY
: /* Display selected. */
873 if (!F_ISSET(&sp
->opts
[cnt
], OPT_SELECTED
))
880 F_CLR(&sp
->opts
[cnt
], OPT_SELECTED
);
882 curlen
= STRLEN(op
->name
);
886 if (!O_ISSET(sp
, cnt
))
891 sizeof(nbuf
), "%ld", O_VAL(sp
, cnt
));
892 curlen
+= strlen(nbuf
);
895 if (O_STR(sp
, cnt
) != NULL
)
896 curlen
+= strlen(O_STR(sp
, cnt
));
900 /* Offset by 2 so there's a gap. */
901 if (curlen
<= colwidth
- 2)
908 /* Figure out the number of rows. */
909 if (s_num
> numcols
) {
910 numrows
= s_num
/ numcols
;
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
)
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
]]);
933 (void)ex_puts(sp
, "\n");
935 (void)ex_puts(sp
, "\n");
940 * Print out an option.
943 opts_print(SCR
*sp
, const OPTLIST
*op
)
948 offset
= op
- optlist
;
952 curlen
+= ex_printf(sp
,
953 "%s"WS
, O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
956 curlen
+= ex_printf(sp
, WS
"=%ld", op
->name
, O_VAL(sp
, offset
));
959 curlen
+= ex_printf(sp
, WS
"=\"%s\"", op
->name
,
960 O_STR(sp
, offset
) == NULL
? "" : O_STR(sp
, offset
));
968 * Write the current configuration to a file.
970 * PUBLIC: int opts_save __P((SCR *, FILE *));
973 opts_save(SCR
*sp
, FILE *fp
)
980 for (op
= optlist
; op
->name
!= NULL
; ++op
) {
981 if (F_ISSET(op
, OPT_NOSAVE
))
987 if (O_ISSET(sp
, cnt
))
988 (void)fprintf(fp
, "set "WS
"\n", op
->name
);
990 (void)fprintf(fp
, "set no"WS
"\n", op
->name
);
994 "set "WS
"=%-3ld\n", op
->name
, O_VAL(sp
, cnt
));
997 if (O_STR(sp
, cnt
) == NULL
)
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
);
1015 msgq(sp
, M_SYSERR
, NULL
);
1024 * Search for an option.
1026 * PUBLIC: OPTLIST const *opts_search __P((CHAR_T *));
1029 opts_search(CHAR_T
*name
)
1031 OPTLIST
const *op
, *found
;
1036 /* Check list of abbreviations. */
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. */
1044 if ((op
= bsearch(&otmp
, optlist
, sizeof(optlist
) / sizeof(OPTLIST
) - 1,
1045 sizeof(OPTLIST
), opts_cmp
)) != NULL
)
1049 * Check to see if the name is the prefix of one (and only one)
1050 * option. If so, return the option.
1053 for (found
= NULL
, op
= optlist
; op
->name
!= NULL
; ++op
) {
1054 if (op
->name
[0] < name
[0])
1056 if (op
->name
[0] > name
[0])
1058 if (!memcmp(op
->name
, name
, len
)) {
1069 * Standard nomatch error message for options.
1071 * PUBLIC: void opts_nomatch __P((SCR *, CHAR_T *));
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");
1081 opts_abbcmp(const void *a
, const void *b
)
1083 return(STRCMP(((OABBREV
*)a
)->name
, ((OABBREV
*)b
)->name
));
1087 opts_cmp(const void *a
, const void *b
)
1089 return(STRCMP(((OPTLIST
*)a
)->name
, ((OPTLIST
*)b
)->name
));
1094 * Copy a screen's OPTION array.
1096 * PUBLIC: int opts_copy __P((SCR *, SCR *));
1099 opts_copy(SCR
*orig
, SCR
*sp
)
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
))
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);
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);
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
);
1140 * Free all option strings
1142 * PUBLIC: void opts_free __P((SCR *));
1149 for (cnt
= 0; cnt
< O_OPTIONCOUNT
; ++cnt
) {
1150 if (optlist
[cnt
].type
!= OPT_STR
||
1151 F_ISSET(&sp
->opts
[cnt
], OPT_GLOBAL
))
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
));