1 /* $NetBSD: options.c,v 1.6 2009/08/07 16:19:53 lukem Exp $ */
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1991, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
15 static const char sccsid
[] = "Id: options.c,v 10.65 2002/01/18 22:34:43 skimo Exp (Berkeley) Date: 2002/01/18 22:34:43";
18 #include <sys/types.h>
19 #include <sys/queue.h>
23 #include <bitstring.h>
34 #include "pathnames.h"
36 static int opts_abbcmp
__P((const void *, const void *));
37 static int opts_cmp
__P((const void *, const void *));
38 static int opts_print
__P((SCR
*, OPTLIST
const *));
43 #define OPT_WC (OPT_NOSAVE | OPT_NDISP)
47 * O'Reilly noted options and abbreviations are from "Learning the VI Editor",
48 * Fifth Edition, May 1992. There's no way of knowing what systems they are
51 * HPUX noted options and abbreviations are from "The Ultimate Guide to the
52 * VI and EX Text Editors", 1990.
54 * This list must be sorted...
56 OPTLIST
const optlist
[] = {
57 /* O_ALTWERASE 4.4BSD */
58 {L("altwerase"), f_altwerase
, OPT_0BOOL
, 0},
59 /* O_AUTOINDENT 4BSD */
60 {L("autoindent"), NULL
, OPT_0BOOL
, 0},
61 /* O_AUTOPRINT 4BSD */
62 {L("autoprint"), NULL
, OPT_1BOOL
, 0},
63 /* O_AUTOWRITE 4BSD */
64 {L("autowrite"), NULL
, OPT_0BOOL
, 0},
66 {L("backup"), NULL
, OPT_STR
, 0},
68 {L("beautify"), NULL
, OPT_0BOOL
, 0},
70 {L("cdpath"), NULL
, OPT_STR
, 0},
72 {L("cedit"), NULL
, OPT_STR
, 0},
73 /* O_COLUMNS 4.4BSD */
74 {L("columns"), f_columns
, OPT_NUM
, OPT_NOSAVE
},
76 {L("combined"), NULL
, OPT_0BOOL
, OPT_NOSET
|OPT_WC
},
77 /* O_COMMENT 4.4BSD */
78 {L("comment"), NULL
, OPT_0BOOL
, 0},
79 /* O_TMP_DIRECTORY 4BSD */
80 {L("directory"), NULL
, OPT_STR
, 0},
81 /* O_EDCOMPATIBLE 4BSD */
82 {L("edcompatible"),NULL
, OPT_0BOOL
, 0},
83 /* O_ERRORBELLS 4BSD */
84 {L("errorbells"), NULL
, OPT_0BOOL
, 0},
85 /* O_ESCAPETIME 4.4BSD */
86 {L("escapetime"), NULL
, OPT_NUM
, 0},
87 /* O_EXPANDTAB NetBSD 5.0 */
88 {L("expandtab"), NULL
, OPT_0BOOL
, 0},
89 /* O_EXRC System V (undocumented) */
90 {L("exrc"), NULL
, OPT_0BOOL
, 0},
91 /* O_EXTENDED 4.4BSD */
92 {L("extended"), f_recompile
, OPT_0BOOL
, 0},
94 {L("filec"), NULL
, OPT_STR
, 0},
96 {L("fileencoding"),f_encoding
, OPT_STR
, OPT_WC
},
98 {L("flash"), NULL
, OPT_1BOOL
, 0},
100 /* O_GTAGSMODE FreeBSD/NetBSD */
101 {L("gtagsmode"),NULL
, OPT_1BOOL
, 0},
103 /* O_HARDTABS 4BSD */
104 {L("hardtabs"), NULL
, OPT_NUM
, 0},
105 /* O_ICLOWER 4.4BSD */
106 {L("iclower"), f_recompile
, OPT_0BOOL
, 0},
107 /* O_IGNORECASE 4BSD */
108 {L("ignorecase"), f_recompile
, OPT_0BOOL
, 0},
109 /* O_INPUTENCODING */
110 {L("inputencoding"),f_encoding
, OPT_STR
, OPT_WC
},
111 /* O_KEYTIME 4.4BSD */
112 {L("keytime"), NULL
, OPT_NUM
, 0},
113 /* O_LEFTRIGHT 4.4BSD */
114 {L("leftright"), f_reformat
, OPT_0BOOL
, 0},
116 {L("lines"), f_lines
, OPT_NUM
, OPT_NOSAVE
},
119 * When the lisp option is implemented, delete the OPT_NOSAVE flag,
120 * so that :mkexrc dumps it.
122 {L("lisp"), f_lisp
, OPT_0BOOL
, OPT_NOSAVE
},
124 {L("list"), f_reformat
, OPT_0BOOL
, 0},
125 /* O_LOCKFILES 4.4BSD
127 * Locking isn't reliable enough over NFS to require it, in addition,
128 * it's a serious startup performance problem over some remote links.
130 {L("lock"), NULL
, OPT_1BOOL
, 0},
132 {L("magic"), NULL
, OPT_1BOOL
, 0},
133 /* O_MATCHCHARS netbsd 2.0 */
134 {L("matchchars"), NULL
, OPT_STR
, OPT_PAIRS
},
135 /* O_MATCHTIME 4.4BSD */
136 {L("matchtime"), NULL
, OPT_NUM
, 0},
138 {L("mesg"), NULL
, OPT_1BOOL
, 0},
141 * This has been documented in historical systems as both "modeline"
142 * and as "modelines". Regardless of the name, this option represents
143 * a security problem of mammoth proportions, not to mention a stunning
144 * example of what your intro CS professor referred to as the perils of
145 * mixing code and data. Don't add it, or I will kill you.
147 {L("modeline"), NULL
, OPT_0BOOL
, OPT_NOSET
},
148 /* O_MSGCAT 4.4BSD */
149 {L("msgcat"), f_msgcat
, OPT_STR
, 0},
150 /* O_NOPRINT 4.4BSD */
151 {L("noprint"), f_print
, OPT_STR
, 0},
153 {L("number"), f_reformat
, OPT_0BOOL
, 0},
155 {L("octal"), f_print
, OPT_0BOOL
, 0},
157 {L("open"), NULL
, OPT_1BOOL
, 0},
158 /* O_OPTIMIZE 4BSD */
159 {L("optimize"), NULL
, OPT_1BOOL
, 0},
160 /* O_PARAGRAPHS 4BSD */
161 {L("paragraphs"), NULL
, OPT_STR
, OPT_PAIRS
},
163 {L("path"), NULL
, OPT_STR
, 0},
165 {L("print"), f_print
, OPT_STR
, 0},
167 {L("prompt"), NULL
, OPT_1BOOL
, 0},
168 /* O_READONLY 4BSD (undocumented) */
169 {L("readonly"), f_readonly
, OPT_0BOOL
, OPT_ALWAYS
},
170 /* O_RECDIR 4.4BSD */
171 {L("recdir"), NULL
, OPT_STR
, 0},
173 {L("redraw"), NULL
, OPT_0BOOL
, 0},
175 {L("remap"), NULL
, OPT_1BOOL
, 0},
177 {L("report"), NULL
, OPT_NUM
, 0},
179 {L("ruler"), NULL
, OPT_0BOOL
, 0},
181 {L("scroll"), NULL
, OPT_NUM
, 0},
182 /* O_SEARCHINCR 4.4BSD */
183 {L("searchincr"), NULL
, OPT_0BOOL
, 0},
184 /* O_SECTIONS 4BSD */
185 {L("sections"), NULL
, OPT_STR
, OPT_PAIRS
},
186 /* O_SECURE 4.4BSD */
187 {L("secure"), NULL
, OPT_0BOOL
, OPT_NOUNSET
},
189 {L("shell"), NULL
, OPT_STR
, 0},
190 /* O_SHELLMETA 4.4BSD */
191 {L("shellmeta"), NULL
, OPT_STR
, 0},
192 /* O_SHIFTWIDTH 4BSD */
193 {L("shiftwidth"), NULL
, OPT_NUM
, OPT_NOZERO
},
194 /* O_SHOWMATCH 4BSD */
195 {L("showmatch"), NULL
, OPT_0BOOL
, 0},
196 /* O_SHOWMODE 4.4BSD */
197 {L("showmode"), NULL
, OPT_0BOOL
, 0},
198 /* O_SIDESCROLL 4.4BSD */
199 {L("sidescroll"), NULL
, OPT_NUM
, OPT_NOZERO
},
200 /* O_SLOWOPEN 4BSD */
201 {L("slowopen"), NULL
, OPT_0BOOL
, 0},
202 /* O_SOURCEANY 4BSD (undocumented)
204 * Historic vi, on startup, source'd $HOME/.exrc and ./.exrc, if they
205 * were owned by the user. The sourceany option was an undocumented
206 * feature of historic vi which permitted the startup source'ing of
207 * .exrc files the user didn't own. This is an obvious security problem,
208 * and we ignore the option.
210 {L("sourceany"), NULL
, OPT_0BOOL
, OPT_NOSET
},
212 {L("tabstop"), f_reformat
, OPT_NUM
, OPT_NOZERO
},
213 /* O_TAGLENGTH 4BSD */
214 {L("taglength"), NULL
, OPT_NUM
, 0},
216 {L("tags"), NULL
, OPT_STR
, 0},
219 * By default, the historic vi always displayed information about two
220 * options, redraw and term. Term seems sufficient.
222 {L("term"), NULL
, OPT_STR
, OPT_ADISP
|OPT_NOSAVE
},
224 {L("terse"), NULL
, OPT_0BOOL
, 0},
225 /* O_TILDEOP 4.4BSD */
226 {L("tildeop"), NULL
, OPT_0BOOL
, 0},
227 /* O_TIMEOUT 4BSD (undocumented) */
228 {L("timeout"), NULL
, OPT_1BOOL
, 0},
229 /* O_TTYWERASE 4.4BSD */
230 {L("ttywerase"), f_ttywerase
, OPT_0BOOL
, 0},
231 /* O_VERBOSE 4.4BSD */
232 {L("verbose"), NULL
, OPT_0BOOL
, 0},
234 {L("w1200"), f_w1200
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
236 {L("w300"), f_w300
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
238 {L("w9600"), f_w9600
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
240 {L("warn"), NULL
, OPT_1BOOL
, 0},
242 {L("window"), f_window
, OPT_NUM
, 0},
243 /* O_WINDOWNAME 4BSD */
244 {L("windowname"), NULL
, OPT_0BOOL
, 0},
245 /* O_WRAPLEN 4.4BSD */
246 {L("wraplen"), NULL
, OPT_NUM
, 0},
247 /* O_WRAPMARGIN 4BSD */
248 {L("wrapmargin"), NULL
, OPT_NUM
, 0},
249 /* O_WRAPSCAN 4BSD */
250 {L("wrapscan"), NULL
, OPT_1BOOL
, 0},
251 /* O_WRITEANY 4BSD */
252 {L("writeany"), NULL
, OPT_0BOOL
, 0},
253 {NULL
, NULL
, OPT_NUM
, 0},
256 typedef struct abbrev
{
261 static OABBREV
const abbrev
[] = {
262 {L("ai"), O_AUTOINDENT
}, /* 4BSD */
263 {L("ap"), O_AUTOPRINT
}, /* 4BSD */
264 {L("aw"), O_AUTOWRITE
}, /* 4BSD */
265 {L("bf"), O_BEAUTIFY
}, /* 4BSD */
266 {L("co"), O_COLUMNS
}, /* 4.4BSD */
267 {L("dir"), O_TMP_DIRECTORY
}, /* 4BSD */
268 {L("eb"), O_ERRORBELLS
}, /* 4BSD */
269 {L("ed"), O_EDCOMPATIBLE
}, /* 4BSD */
270 {L("et"), O_EXPANDTAB
}, /* NetBSD 5.0 */
271 {L("ex"), O_EXRC
}, /* System V (undocumented) */
273 {L("gt"), O_GTAGSMODE
}, /* FreeBSD, NetBSD */
275 {L("fe"), O_FILEENCODING
},
276 {L("ht"), O_HARDTABS
}, /* 4BSD */
277 {L("ic"), O_IGNORECASE
}, /* 4BSD */
278 {L("ie"), O_INPUTENCODING
},
279 {L("li"), O_LINES
}, /* 4.4BSD */
280 {L("modelines"), O_MODELINE
}, /* HPUX */
281 {L("nu"), O_NUMBER
}, /* 4BSD */
282 {L("opt"), O_OPTIMIZE
}, /* 4BSD */
283 {L("para"), O_PARAGRAPHS
}, /* 4BSD */
284 {L("re"), O_REDRAW
}, /* O'Reilly */
285 {L("ro"), O_READONLY
}, /* 4BSD (undocumented) */
286 {L("scr"), O_SCROLL
}, /* 4BSD (undocumented) */
287 {L("sect"), O_SECTIONS
}, /* O'Reilly */
288 {L("sh"), O_SHELL
}, /* 4BSD */
289 {L("slow"), O_SLOWOPEN
}, /* 4BSD */
290 {L("sm"), O_SHOWMATCH
}, /* 4BSD */
291 {L("smd"), O_SHOWMODE
}, /* 4BSD */
292 {L("sw"), O_SHIFTWIDTH
}, /* 4BSD */
293 {L("tag"), O_TAGS
}, /* 4BSD (undocumented) */
294 {L("tl"), O_TAGLENGTH
}, /* 4BSD */
295 {L("to"), O_TIMEOUT
}, /* 4BSD (undocumented) */
296 {L("ts"), O_TABSTOP
}, /* 4BSD */
297 {L("tty"), O_TERM
}, /* 4BSD (undocumented) */
298 {L("ttytype"), O_TERM
}, /* 4BSD (undocumented) */
299 {L("w"), O_WINDOW
}, /* O'Reilly */
300 {L("wa"), O_WRITEANY
}, /* 4BSD */
301 {L("wi"), O_WINDOW
}, /* 4BSD (undocumented) */
302 {L("wl"), O_WRAPLEN
}, /* 4.4BSD */
303 {L("wm"), O_WRAPMARGIN
}, /* 4BSD */
304 {L("ws"), O_WRAPSCAN
}, /* 4BSD */
310 * Initialize some of the options.
312 * PUBLIC: int opts_init __P((SCR *, int *));
315 opts_init(SCR
*sp
, int *oargs
)
320 int cnt
, optindx
= 0;
324 if (sizeof optlist
/ sizeof optlist
[0] - 1 != O_OPTIONCOUNT
) {
325 fprintf(stderr
, "vi: option table size error (%d != %d)\n",
326 (int)(sizeof optlist
/ sizeof optlist
[0] - 1),
337 /* Set numeric and string default values. */
338 #define OI(indx, str) { \
339 a.len = STRLEN(str); \
340 if ((const CHAR_T*)str != b2)/* GCC puts strings in text-space. */\
341 (void)MEMCPY(b2, str, a.len+1); \
342 if (opts_set(sp, argv, NULL)) { \
348 * Indirect global options to global space. Specifically, set up
349 * terminal, lines, columns first, they're used by other options.
350 * Note, don't set the flags until we've set up the indirection.
352 if (o_set(sp
, O_TERM
, 0, NULL
, GO_TERM
))
354 F_SET(&sp
->opts
[O_TERM
], OPT_GLOBAL
);
355 if (o_set(sp
, O_LINES
, 0, NULL
, GO_LINES
))
357 F_SET(&sp
->opts
[O_LINES
], OPT_GLOBAL
);
358 if (o_set(sp
, O_COLUMNS
, 0, NULL
, GO_COLUMNS
))
360 F_SET(&sp
->opts
[O_COLUMNS
], OPT_GLOBAL
);
361 if (o_set(sp
, O_SECURE
, 0, NULL
, GO_SECURE
))
363 F_SET(&sp
->opts
[O_SECURE
], OPT_GLOBAL
);
365 /* Initialize string values. */
366 (void)SPRINTF(b2
, SIZE(b2
),
367 L("cdpath=%s"), (s
= getenv("CDPATH")) == NULL
? ":" : s
);
372 * Vi historically stored temporary files in /var/tmp. We store them
373 * in /tmp by default, hoping it's a memory based file system. There
374 * are two ways to change this -- the user can set either the directory
375 * option or the TMPDIR environmental variable.
377 (void)SPRINTF(b2
, SIZE(b2
),
378 L("directory=%s"), (s
= getenv("TMPDIR")) == NULL
? _PATH_TMP
: s
);
379 OI(O_TMP_DIRECTORY
, b2
);
380 OI(O_ESCAPETIME
, L("escapetime=1"));
381 OI(O_KEYTIME
, L("keytime=6"));
382 OI(O_MATCHCHARS
, L("matchchars=()[]{}<>"));
383 OI(O_MATCHTIME
, L("matchtime=7"));
384 (void)SPRINTF(b2
, SIZE(b2
), L("msgcat=%s"), _PATH_MSGCAT
);
386 OI(O_REPORT
, L("report=5"));
387 OI(O_PARAGRAPHS
, L("paragraphs=IPLPPPQPP LIpplpipbp"));
388 (void)SPRINTF(b2
, SIZE(b2
), L("path=%s"), "");
390 (void)SPRINTF(b2
, SIZE(b2
), L("recdir=%s"), _PATH_PRESERVE
);
392 OI(O_SECTIONS
, L("sections=NHSHH HUnhsh"));
393 (void)SPRINTF(b2
, SIZE(b2
),
394 L("shell=%s"), (s
= getenv("SHELL")) == NULL
? _PATH_BSHELL
: s
);
396 OI(O_SHELLMETA
, L("shellmeta=~{[*?$`'\"\\"));
397 OI(O_SHIFTWIDTH
, L("shiftwidth=8"));
398 OI(O_SIDESCROLL
, L("sidescroll=16"));
399 OI(O_TABSTOP
, L("tabstop=8"));
400 (void)SPRINTF(b2
, SIZE(b2
), L("tags=%s"), _PATH_TAGS
);
405 * Initialize O_SCROLL here, after term; initializing term should
406 * have created a LINES/COLUMNS value.
408 if ((v
= (O_VAL(sp
, O_LINES
) - 1) / 2) == 0)
410 (void)SPRINTF(b2
, SIZE(b2
), L("scroll=%ld"), v
);
414 * The default window option values are:
415 * 8 if baud rate <= 600
416 * 16 if baud rate <= 1200
417 * LINES - 1 if baud rate > 1200
419 * Note, the windows option code will correct any too-large value
420 * or when the O_LINES value is 1.
422 if (sp
->gp
->scr_baud(sp
, &v
))
428 else if ((v
= O_VAL(sp
, O_LINES
) - 1) == 0)
431 (void)SPRINTF(b2
, SIZE(b2
), L("window=%lu"), v
);
435 * Set boolean default values, and copy all settings into the default
436 * information. OS_NOFREE is set, we're copying, not replacing.
438 for (op
= optlist
, cnt
= 0; op
->name
!= NULL
; ++op
, ++cnt
)
447 o_set(sp
, cnt
, OS_DEF
, NULL
, O_VAL(sp
, cnt
));
450 if (O_STR(sp
, cnt
) != NULL
&& o_set(sp
, cnt
,
451 OS_DEF
| OS_NOFREE
| OS_STRDUP
, O_STR(sp
, cnt
), 0))
460 * Some options can be initialized by the command name or the
461 * command-line arguments. They don't set the default values,
462 * it's historic practice.
464 for (; *oargs
!= -1; ++oargs
)
465 OI(*oargs
, optlist
[*oargs
].name
);
469 * Inform the underlying screen of the initial values of the
472 for (op
= optlist
, cnt
= 0; op
->name
!= NULL
; ++op
, ++cnt
) {
473 isset
= O_ISSET(sp
, cnt
);
474 (void)sp
->gp
->scr_optchange(sp
, cnt
, O_STR(sp
, cnt
), &isset
);
478 err
: msgq_wstr(sp
, M_ERR
, optlist
[optindx
].name
,
479 "031|Unable to set default %s option");
485 * Change the values of one or more options.
487 * PUBLIC: int opts_set __P((SCR *, ARGS *[], char *));
490 opts_set(SCR
*sp
, ARGS
**argv
, const char *usage
)
496 u_long isset
, turnoff
, value
;
497 int ch
, equals
, nf
, nf2
, offset
, qmark
, rval
;
498 CHAR_T
*endp
, *name
, *p
, *sep
;
504 for (rval
= 0; argv
[0]->len
!= 0; ++argv
) {
506 * The historic vi dumped the options for each occurrence of
507 * "all" in the set list. Puhleeze.
509 if (!STRCMP(argv
[0]->bp
, L("all"))) {
514 /* Find equals sign or question mark. */
515 for (sep
= NULL
, equals
= qmark
= 0,
516 p
= name
= argv
[0]->bp
; (ch
= *p
) != '\0'; ++p
)
517 if (ch
== '=' || ch
== '?') {
521 "032|Usage: %s", usage
);
537 /* Search for the name, then name without any leading "no". */
538 if ((op
= opts_search(name
)) == NULL
&&
539 name
[0] == L('n') && name
[1] == L('o')) {
542 op
= opts_search(name
);
545 opts_nomatch(sp
, name
);
550 /* Find current option values. */
551 offset
= op
- optlist
;
552 spo
= sp
->opts
+ offset
;
556 * Historically, the question mark could be a separate
559 if (!equals
&& !qmark
&&
560 argv
[1]->len
== 1 && argv
[1]->bp
[0] == '?') {
565 /* Set name, value. */
569 /* Some options may not be reset. */
570 if (F_ISSET(op
, OPT_NOUNSET
) && turnoff
) {
571 msgq_wstr(sp
, M_ERR
, name
,
572 "291|set: the %s option may not be turned off");
577 /* Some options may not be set. */
578 if (F_ISSET(op
, OPT_NOSET
) && !turnoff
) {
579 msgq_wstr(sp
, M_ERR
, name
,
580 "313|set: the %s option may never be turned on");
586 msgq_wstr(sp
, M_ERR
, name
,
587 "034|set: [no]%s option doesn't take a value");
593 disp
= SELECT_DISPLAY
;
594 F_SET(spo
, OPT_SELECTED
);
599 * Do nothing if the value is unchanged, the underlying
600 * functions can be expensive.
603 if (!F_ISSET(op
, OPT_ALWAYS
)) {
605 if (O_ISSET(sp
, offset
))
608 if (!O_ISSET(sp
, offset
))
612 /* Report to subsystems. */
613 if ((op
->func
!= NULL
&&
614 op
->func(sp
, spo
, NULL
, &isset
)) ||
615 ex_optchange(sp
, offset
, NULL
, &isset
) ||
616 v_optchange(sp
, offset
, NULL
, &isset
) ||
617 sp
->gp
->scr_optchange(sp
, offset
, NULL
, &isset
)) {
630 msgq_wstr(sp
, M_ERR
, name
,
631 "035|set: %s option isn't a boolean");
635 if (qmark
|| !equals
) {
637 disp
= SELECT_DISPLAY
;
638 F_SET(spo
, OPT_SELECTED
);
642 if (!ISDIGIT(sep
[0]))
645 nget_uslong(sp
, &value
, sep
, &endp
, 10)) != NUM_OK
) {
646 INT2CHAR(sp
, name
, STRLEN(name
) + 1,
648 p2
= msg_print(sp
, np
, &nf
);
649 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1,
651 t2
= msg_print(sp
, np
, &nf2
);
655 "036|set: %s option: %s", p2
, t2
);
659 "037|set: %s option: %s: value overflow", p2
, t2
);
666 FREE_SPACE(sp
, p2
, 0);
668 FREE_SPACE(sp
, t2
, 0);
672 if (*endp
&& !ISBLANK(*endp
)) {
673 badnum
: INT2CHAR(sp
, name
, STRLEN(name
) + 1,
675 p2
= msg_print(sp
, np
, &nf
);
676 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1,
678 t2
= msg_print(sp
, np
, &nf2
);
680 "038|set: %s option: %s is an illegal number", p2
, t2
);
682 FREE_SPACE(sp
, p2
, 0);
684 FREE_SPACE(sp
, t2
, 0);
689 /* Some options may never be set to zero. */
690 if (F_ISSET(op
, OPT_NOZERO
) && value
== 0) {
691 msgq_wstr(sp
, M_ERR
, name
,
692 "314|set: the %s option may never be set to 0");
698 * Do nothing if the value is unchanged, the underlying
699 * functions can be expensive.
701 if (!F_ISSET(op
, OPT_ALWAYS
) &&
702 O_VAL(sp
, offset
) == value
)
705 /* Report to subsystems. */
706 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1, np
, nlen
);
707 if ((op
->func
!= NULL
&&
708 op
->func(sp
, spo
, np
, &value
)) ||
709 ex_optchange(sp
, offset
, np
, &value
) ||
710 v_optchange(sp
, offset
, np
, &value
) ||
711 sp
->gp
->scr_optchange(sp
, offset
, np
, &value
)) {
717 if (o_set(sp
, offset
, 0, NULL
, value
))
722 msgq_wstr(sp
, M_ERR
, name
,
723 "039|set: %s option isn't a boolean");
727 if (qmark
|| !equals
) {
729 disp
= SELECT_DISPLAY
;
730 F_SET(spo
, OPT_SELECTED
);
734 /* Check for strings that must have even length */
735 if (F_ISSET(op
, OPT_PAIRS
) && STRLEN(sep
) & 1) {
736 msgq_wstr(sp
, M_ERR
, name
,
737 "047|set: the %s option must be in two character groups");
743 * Do nothing if the value is unchanged, the underlying
744 * functions can be expensive.
746 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1, np
, nlen
);
747 if (!F_ISSET(op
, OPT_ALWAYS
) &&
748 O_STR(sp
, offset
) != NULL
&&
749 !strcmp(O_STR(sp
, offset
), np
))
752 /* Report to subsystems. */
753 if ((op
->func
!= NULL
&&
754 op
->func(sp
, spo
, np
, NULL
)) ||
755 ex_optchange(sp
, offset
, np
, NULL
) ||
756 v_optchange(sp
, offset
, np
, NULL
) ||
757 sp
->gp
->scr_optchange(sp
, offset
, np
, NULL
)) {
763 if (o_set(sp
, offset
, OS_STRDUP
, np
, 0))
770 if (disp
!= NO_DISPLAY
)
777 * Set an option's value.
779 * PUBLIC: int o_set __P((SCR *, int, u_int, const char *, u_long));
782 o_set(SCR
*sp
, int opt
, u_int flags
, const char *str
, u_long val
)
786 /* Set a pointer to the options area. */
787 op
= F_ISSET(&sp
->opts
[opt
], OPT_GLOBAL
) ?
788 &sp
->gp
->opts
[sp
->opts
[opt
].o_cur
.val
] : &sp
->opts
[opt
];
790 /* Copy the string, if requested. */
791 if (LF_ISSET(OS_STRDUP
) && (str
= strdup(str
)) == NULL
) {
792 msgq(sp
, M_SYSERR
, NULL
);
796 /* Free the previous string, if requested, and set the value. */
798 if (LF_ISSET(OS_STR
| OS_STRDUP
)) {
799 if (!LF_ISSET(OS_NOFREE
) && op
->o_def
.str
!= NULL
)
800 free(__UNCONST(op
->o_def
.str
));
805 if (LF_ISSET(OS_STR
| OS_STRDUP
)) {
806 if (!LF_ISSET(OS_NOFREE
) && op
->o_cur
.str
!= NULL
)
807 free(__UNCONST(op
->o_cur
.str
));
816 * Return 1 if the string option is invalid, 0 if it's OK.
818 * PUBLIC: int opts_empty __P((SCR *, int, int));
821 opts_empty(SCR
*sp
, int off
, int silent
)
825 if ((p
= O_STR(sp
, off
)) == NULL
|| p
[0] == '\0') {
827 msgq_wstr(sp
, M_ERR
, optlist
[off
].name
,
828 "305|No %s edit option specified");
836 * List the current values of selected options.
838 * PUBLIC: void opts_dump __P((SCR *, enum optdisp));
841 opts_dump(SCR
*sp
, enum optdisp type
)
844 int base
, b_num
, cnt
, col
, colwidth
, curlen
, s_num
;
845 int numcols
, numrows
, row
;
846 int b_op
[O_OPTIONCOUNT
], s_op
[O_OPTIONCOUNT
];
850 * Options are output in two groups -- those that fit in a column and
851 * those that don't. Output is done on 6 character "tab" boundaries
852 * for no particular reason. (Since we don't output tab characters,
853 * we can ignore the terminal's tab settings.) Ignore the user's tab
854 * setting because we have no idea how reasonable it is.
856 * Find a column width we can live with, testing from 10 columns to 1.
858 for (numcols
= 10; numcols
> 1; --numcols
) {
859 colwidth
= sp
->cols
/ numcols
& ~(STANDARD_TAB
- 1);
860 if (colwidth
>= 10) {
862 (colwidth
+ STANDARD_TAB
) & ~(STANDARD_TAB
- 1);
863 numcols
= sp
->cols
/ colwidth
;
870 * Get the set of options to list, entering them into
871 * the column list or the overflow list.
873 for (b_num
= s_num
= 0, op
= optlist
; op
->name
!= NULL
; ++op
) {
876 /* If OPT_NDISP set, it's never displayed. */
877 if (F_ISSET(op
, OPT_NDISP
))
881 case ALL_DISPLAY
: /* Display all. */
883 case CHANGED_DISPLAY
: /* Display changed. */
884 /* If OPT_ADISP set, it's always "changed". */
885 if (F_ISSET(op
, OPT_ADISP
))
891 if (O_VAL(sp
, cnt
) == O_D_VAL(sp
, cnt
))
895 if (O_STR(sp
, cnt
) == O_D_STR(sp
, cnt
) ||
896 (O_D_STR(sp
, cnt
) != NULL
&&
897 !strcmp(O_STR(sp
, cnt
), O_D_STR(sp
, cnt
))))
902 case SELECT_DISPLAY
: /* Display selected. */
903 if (!F_ISSET(&sp
->opts
[cnt
], OPT_SELECTED
))
910 F_CLR(&sp
->opts
[cnt
], OPT_SELECTED
);
912 curlen
= STRLEN(op
->name
);
916 if (!O_ISSET(sp
, cnt
))
921 sizeof(nbuf
), "%ld", O_VAL(sp
, cnt
));
922 curlen
+= strlen(nbuf
);
925 if (O_STR(sp
, cnt
) != NULL
)
926 curlen
+= strlen(O_STR(sp
, cnt
));
930 /* Offset by 2 so there's a gap. */
931 if (curlen
<= colwidth
- 2)
938 /* Figure out the number of rows. */
939 if (s_num
> numcols
) {
940 numrows
= s_num
/ numcols
;
946 /* Display the options in sorted order. */
947 for (row
= 0; row
< numrows
;) {
948 for (base
= row
, col
= 0; col
< numcols
; ++col
) {
949 cnt
= opts_print(sp
, &optlist
[s_op
[base
]]);
950 if ((base
+= numrows
) >= s_num
)
952 (void)ex_printf(sp
, "%*s",
953 (int)(colwidth
- cnt
), "");
955 if (++row
< numrows
|| b_num
)
956 (void)ex_puts(sp
, "\n");
960 for (row
= 0; row
< b_num
;) {
961 (void)opts_print(sp
, &optlist
[b_op
[row
]]);
963 (void)ex_puts(sp
, "\n");
965 (void)ex_puts(sp
, "\n");
970 * Print out an option.
973 opts_print(SCR
*sp
, const OPTLIST
*op
)
978 offset
= op
- optlist
;
982 curlen
+= ex_printf(sp
,
983 "%s"WS
, O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
986 curlen
+= ex_printf(sp
, WS
"=%ld", op
->name
, O_VAL(sp
, offset
));
989 curlen
+= ex_printf(sp
, WS
"=\"%s\"", op
->name
,
990 O_STR(sp
, offset
) == NULL
? "" : O_STR(sp
, offset
));
998 * Write the current configuration to a file.
1000 * PUBLIC: int opts_save __P((SCR *, FILE *));
1003 opts_save(SCR
*sp
, FILE *fp
)
1012 for (op
= optlist
; op
->name
!= NULL
; ++op
) {
1013 if (F_ISSET(op
, OPT_NOSAVE
))
1019 if (O_ISSET(sp
, cnt
))
1020 (void)fprintf(fp
, "set "WS
"\n", op
->name
);
1022 (void)fprintf(fp
, "set no"WS
"\n", op
->name
);
1026 "set "WS
"=%-3ld\n", op
->name
, O_VAL(sp
, cnt
));
1029 if (O_STR(sp
, cnt
) == NULL
)
1031 (void)fprintf(fp
, "set ");
1032 for (p
= op
->name
; (ch
= *p
) != L('\0'); ++p
) {
1033 if (ISBLANK(ch
) || ch
== L('\\'))
1034 (void)putc('\\', fp
);
1035 fprintf(fp
, WC
, ch
);
1037 (void)putc('=', fp
);
1038 for (np
= O_STR(sp
, cnt
); (nch
= *np
) != '\0'; ++np
) {
1039 if (isblank(nch
) || nch
== '\\')
1040 (void)putc('\\', fp
);
1041 (void)putc(nch
, fp
);
1043 (void)putc('\n', fp
);
1047 msgq(sp
, M_SYSERR
, NULL
);
1056 * Search for an option.
1058 * PUBLIC: OPTLIST const *opts_search __P((CHAR_T *));
1061 opts_search(const CHAR_T
*name
)
1063 OPTLIST
const *op
, *found
;
1068 /* Check list of abbreviations. */
1070 if ((ap
= bsearch(&atmp
, abbrev
, sizeof(abbrev
) / sizeof(OABBREV
) - 1,
1071 sizeof(OABBREV
), opts_abbcmp
)) != NULL
)
1072 return (optlist
+ ap
->offset
);
1074 /* Check list of options. */
1076 if ((op
= bsearch(&otmp
, optlist
, sizeof(optlist
) / sizeof(OPTLIST
) - 1,
1077 sizeof(OPTLIST
), opts_cmp
)) != NULL
)
1081 * Check to see if the name is the prefix of one (and only one)
1082 * option. If so, return the option.
1085 for (found
= NULL
, op
= optlist
; op
->name
!= NULL
; ++op
) {
1086 if (op
->name
[0] < name
[0])
1088 if (op
->name
[0] > name
[0])
1090 if (!MEMCMP(op
->name
, name
, len
)) {
1101 * Standard nomatch error message for options.
1103 * PUBLIC: void opts_nomatch __P((SCR *, CHAR_T *));
1106 opts_nomatch(SCR
*sp
, const CHAR_T
*name
)
1108 msgq_wstr(sp
, M_ERR
, name
,
1109 "033|set: no %s option: 'set all' gives all option values");
1113 opts_abbcmp(const void *a
, const void *b
)
1115 return(STRCMP(((const OABBREV
*)a
)->name
, ((const OABBREV
*)b
)->name
));
1119 opts_cmp(const void *a
, const void *b
)
1121 return(STRCMP(((const OPTLIST
*)a
)->name
, ((const OPTLIST
*)b
)->name
));
1126 * Copy a screen's OPTION array.
1128 * PUBLIC: int opts_copy __P((SCR *, SCR *));
1131 opts_copy(SCR
*orig
, SCR
*sp
)
1135 /* Copy most everything without change. */
1136 memcpy(sp
->opts
, orig
->opts
, sizeof(orig
->opts
));
1138 /* Copy the string edit options. */
1139 for (cnt
= rval
= 0; cnt
< O_OPTIONCOUNT
; ++cnt
) {
1140 if (optlist
[cnt
].type
!= OPT_STR
||
1141 F_ISSET(&sp
->opts
[cnt
], OPT_GLOBAL
))
1144 * If never set, or already failed, NULL out the entries --
1145 * have to continue after failure, otherwise would have two
1146 * screens referencing the same memory.
1148 if (rval
|| O_STR(sp
, cnt
) == NULL
) {
1149 o_set(sp
, cnt
, OS_NOFREE
| OS_STR
, NULL
, 0);
1150 o_set(sp
, cnt
, OS_DEF
| OS_NOFREE
| OS_STR
, NULL
, 0);
1154 /* Copy the current string. */
1155 if (o_set(sp
, cnt
, OS_NOFREE
| OS_STRDUP
, O_STR(sp
, cnt
), 0)) {
1156 o_set(sp
, cnt
, OS_DEF
| OS_NOFREE
| OS_STR
, NULL
, 0);
1160 /* Copy the default string. */
1161 if (O_D_STR(sp
, cnt
) != NULL
&& o_set(sp
, cnt
,
1162 OS_DEF
| OS_NOFREE
| OS_STRDUP
, O_D_STR(sp
, cnt
), 0)) {
1163 nomem
: msgq(orig
, M_SYSERR
, NULL
);
1172 * Free all option strings
1174 * PUBLIC: void opts_free __P((SCR *));
1181 for (cnt
= 0; cnt
< O_OPTIONCOUNT
; ++cnt
) {
1182 if (optlist
[cnt
].type
!= OPT_STR
||
1183 F_ISSET(&sp
->opts
[cnt
], OPT_GLOBAL
))
1185 if (O_STR(sp
, cnt
) != NULL
)
1186 free(__UNCONST(O_STR(sp
, cnt
)));
1187 if (O_D_STR(sp
, cnt
) != NULL
)
1188 free(__UNCONST(O_D_STR(sp
, cnt
)));