1 /* $NetBSD: options.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1991, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1991, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: options.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
27 #include <bitstring.h>
38 #include "pathnames.h"
40 static int opts_abbcmp
__P((const void *, const void *));
41 static int opts_cmp
__P((const void *, const void *));
42 static int opts_print
__P((SCR
*, OPTLIST
const *));
47 #define OPT_WC (OPT_NOSAVE | OPT_NDISP)
51 * O'Reilly noted options and abbreviations are from "Learning the VI Editor",
52 * Fifth Edition, May 1992. There's no way of knowing what systems they are
55 * HPUX noted options and abbreviations are from "The Ultimate Guide to the
56 * VI and EX Text Editors", 1990.
58 * This list must be sorted...
60 OPTLIST
const optlist
[] = {
61 /* O_ALTWERASE 4.4BSD */
62 {L("altwerase"), f_altwerase
, OPT_0BOOL
, 0},
63 /* O_AUTOINDENT 4BSD */
64 {L("autoindent"), NULL
, OPT_0BOOL
, 0},
65 /* O_AUTOPRINT 4BSD */
66 {L("autoprint"), NULL
, OPT_1BOOL
, 0},
67 /* O_AUTOWRITE 4BSD */
68 {L("autowrite"), NULL
, OPT_0BOOL
, 0},
70 {L("backup"), NULL
, OPT_STR
, 0},
72 {L("beautify"), NULL
, OPT_0BOOL
, 0},
74 {L("cdpath"), NULL
, OPT_STR
, 0},
76 {L("cedit"), NULL
, OPT_STR
, 0},
77 /* O_COLUMNS 4.4BSD */
78 {L("columns"), f_columns
, OPT_NUM
, OPT_NOSAVE
},
80 {L("combined"), NULL
, OPT_0BOOL
, OPT_NOSET
|OPT_WC
},
81 /* O_COMMENT 4.4BSD */
82 {L("comment"), NULL
, OPT_0BOOL
, 0},
83 /* O_TMP_DIRECTORY 4BSD */
84 {L("directory"), NULL
, OPT_STR
, 0},
85 /* O_EDCOMPATIBLE 4BSD */
86 {L("edcompatible"),NULL
, OPT_0BOOL
, 0},
87 /* O_ERRORBELLS 4BSD */
88 {L("errorbells"), NULL
, OPT_0BOOL
, 0},
89 /* O_ESCAPETIME 4.4BSD */
90 {L("escapetime"), NULL
, OPT_NUM
, 0},
91 /* O_EXPANDTAB NetBSD 5.0 */
92 {L("expandtab"), NULL
, OPT_0BOOL
, 0},
93 /* O_EXRC System V (undocumented) */
94 {L("exrc"), NULL
, OPT_0BOOL
, 0},
95 /* O_EXTENDED 4.4BSD */
96 {L("extended"), f_recompile
, OPT_0BOOL
, 0},
98 {L("filec"), NULL
, OPT_STR
, 0},
100 {L("fileencoding"),f_encoding
, OPT_STR
, OPT_WC
},
102 {L("flash"), NULL
, OPT_1BOOL
, 0},
104 /* O_GTAGSMODE FreeBSD/NetBSD */
105 {L("gtagsmode"),NULL
, OPT_0BOOL
, 0},
107 /* O_HARDTABS 4BSD */
108 {L("hardtabs"), NULL
, OPT_NUM
, 0},
109 /* O_ICLOWER 4.4BSD */
110 {L("iclower"), f_recompile
, OPT_0BOOL
, 0},
111 /* O_IGNORECASE 4BSD */
112 {L("ignorecase"), f_recompile
, OPT_0BOOL
, 0},
113 /* O_INPUTENCODING */
114 {L("inputencoding"),f_encoding
, OPT_STR
, OPT_WC
},
115 /* O_KEYTIME 4.4BSD */
116 {L("keytime"), NULL
, OPT_NUM
, 0},
117 /* O_LEFTRIGHT 4.4BSD */
118 {L("leftright"), f_reformat
, OPT_0BOOL
, 0},
120 {L("lines"), f_lines
, OPT_NUM
, OPT_NOSAVE
},
123 * When the lisp option is implemented, delete the OPT_NOSAVE flag,
124 * so that :mkexrc dumps it.
126 {L("lisp"), f_lisp
, OPT_0BOOL
, OPT_NOSAVE
},
128 {L("list"), f_reformat
, OPT_0BOOL
, 0},
129 /* O_LOCKFILES 4.4BSD
131 * Locking isn't reliable enough over NFS to require it, in addition,
132 * it's a serious startup performance problem over some remote links.
134 {L("lock"), NULL
, OPT_1BOOL
, 0},
136 {L("magic"), NULL
, OPT_1BOOL
, 0},
137 /* O_MATCHCHARS netbsd 2.0 */
138 {L("matchchars"), NULL
, OPT_STR
, OPT_PAIRS
},
139 /* O_MATCHTIME 4.4BSD */
140 {L("matchtime"), NULL
, OPT_NUM
, 0},
142 {L("mesg"), NULL
, OPT_1BOOL
, 0},
145 * This has been documented in historical systems as both "modeline"
146 * and as "modelines". Regardless of the name, this option represents
147 * a security problem of mammoth proportions, not to mention a stunning
148 * example of what your intro CS professor referred to as the perils of
149 * mixing code and data. Don't add it, or I will kill you.
151 {L("modeline"), NULL
, OPT_0BOOL
, OPT_NOSET
},
152 /* O_MSGCAT 4.4BSD */
153 {L("msgcat"), f_msgcat
, OPT_STR
, 0},
154 /* O_NOPRINT 4.4BSD */
155 {L("noprint"), f_print
, OPT_STR
, 0},
157 {L("number"), f_reformat
, OPT_0BOOL
, 0},
159 {L("octal"), f_print
, OPT_0BOOL
, 0},
161 {L("open"), NULL
, OPT_1BOOL
, 0},
162 /* O_OPTIMIZE 4BSD */
163 {L("optimize"), NULL
, OPT_1BOOL
, 0},
164 /* O_PARAGRAPHS 4BSD */
165 {L("paragraphs"), NULL
, OPT_STR
, OPT_PAIRS
},
167 {L("path"), NULL
, OPT_STR
, 0},
169 {L("print"), f_print
, OPT_STR
, 0},
171 {L("prompt"), NULL
, OPT_1BOOL
, 0},
172 /* O_READONLY 4BSD (undocumented) */
173 {L("readonly"), f_readonly
, OPT_0BOOL
, OPT_ALWAYS
},
174 /* O_RECDIR 4.4BSD */
175 {L("recdir"), NULL
, OPT_STR
, 0},
177 {L("redraw"), NULL
, OPT_0BOOL
, 0},
179 {L("remap"), NULL
, OPT_1BOOL
, 0},
181 {L("report"), NULL
, OPT_NUM
, 0},
183 {L("ruler"), NULL
, OPT_0BOOL
, 0},
185 {L("scroll"), NULL
, OPT_NUM
, 0},
186 /* O_SEARCHINCR 4.4BSD */
187 {L("searchincr"), NULL
, OPT_0BOOL
, 0},
188 /* O_SECTIONS 4BSD */
189 {L("sections"), NULL
, OPT_STR
, OPT_PAIRS
},
190 /* O_SECURE 4.4BSD */
191 {L("secure"), NULL
, OPT_0BOOL
, OPT_NOUNSET
},
193 {L("shell"), NULL
, OPT_STR
, 0},
194 /* O_SHELLMETA 4.4BSD */
195 {L("shellmeta"), NULL
, OPT_STR
, 0},
196 /* O_SHIFTWIDTH 4BSD */
197 {L("shiftwidth"), NULL
, OPT_NUM
, OPT_NOZERO
},
198 /* O_SHOWMATCH 4BSD */
199 {L("showmatch"), NULL
, OPT_0BOOL
, 0},
200 /* O_SHOWMODE 4.4BSD */
201 {L("showmode"), NULL
, OPT_0BOOL
, 0},
202 /* O_SIDESCROLL 4.4BSD */
203 {L("sidescroll"), NULL
, OPT_NUM
, OPT_NOZERO
},
204 /* O_SLOWOPEN 4BSD */
205 {L("slowopen"), NULL
, OPT_0BOOL
, 0},
206 /* O_SOURCEANY 4BSD (undocumented)
208 * Historic vi, on startup, source'd $HOME/.exrc and ./.exrc, if they
209 * were owned by the user. The sourceany option was an undocumented
210 * feature of historic vi which permitted the startup source'ing of
211 * .exrc files the user didn't own. This is an obvious security problem,
212 * and we ignore the option.
214 {L("sourceany"), NULL
, OPT_0BOOL
, OPT_NOSET
},
216 {L("tabstop"), f_reformat
, OPT_NUM
, OPT_NOZERO
},
217 /* O_TAGLENGTH 4BSD */
218 {L("taglength"), NULL
, OPT_NUM
, 0},
220 {L("tags"), NULL
, OPT_STR
, 0},
223 * By default, the historic vi always displayed information about two
224 * options, redraw and term. Term seems sufficient.
226 {L("term"), NULL
, OPT_STR
, OPT_ADISP
|OPT_NOSAVE
},
228 {L("terse"), NULL
, OPT_0BOOL
, 0},
229 /* O_TILDEOP 4.4BSD */
230 {L("tildeop"), NULL
, OPT_0BOOL
, 0},
231 /* O_TIMEOUT 4BSD (undocumented) */
232 {L("timeout"), NULL
, OPT_1BOOL
, 0},
233 /* O_TTYWERASE 4.4BSD */
234 {L("ttywerase"), f_ttywerase
, OPT_0BOOL
, 0},
235 /* O_VERBOSE 4.4BSD */
236 {L("verbose"), NULL
, OPT_0BOOL
, 0},
238 {L("w1200"), f_w1200
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
240 {L("w300"), f_w300
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
242 {L("w9600"), f_w9600
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
244 {L("warn"), NULL
, OPT_1BOOL
, 0},
246 {L("window"), f_window
, OPT_NUM
, 0},
247 /* O_WINDOWNAME 4BSD */
248 {L("windowname"), NULL
, OPT_0BOOL
, 0},
249 /* O_WRAPLEN 4.4BSD */
250 {L("wraplen"), NULL
, OPT_NUM
, 0},
251 /* O_WRAPMARGIN 4BSD */
252 {L("wrapmargin"), NULL
, OPT_NUM
, 0},
253 /* O_WRAPSCAN 4BSD */
254 {L("wrapscan"), NULL
, OPT_1BOOL
, 0},
255 /* O_WRITEANY 4BSD */
256 {L("writeany"), NULL
, OPT_0BOOL
, 0},
257 {NULL
, NULL
, OPT_NUM
, 0},
260 typedef struct abbrev
{
265 static OABBREV
const abbrev
[] = {
266 {L("ai"), O_AUTOINDENT
}, /* 4BSD */
267 {L("ap"), O_AUTOPRINT
}, /* 4BSD */
268 {L("aw"), O_AUTOWRITE
}, /* 4BSD */
269 {L("bf"), O_BEAUTIFY
}, /* 4BSD */
270 {L("co"), O_COLUMNS
}, /* 4.4BSD */
271 {L("dir"), O_TMP_DIRECTORY
}, /* 4BSD */
272 {L("eb"), O_ERRORBELLS
}, /* 4BSD */
273 {L("ed"), O_EDCOMPATIBLE
}, /* 4BSD */
274 {L("et"), O_EXPANDTAB
}, /* NetBSD 5.0 */
275 {L("ex"), O_EXRC
}, /* System V (undocumented) */
277 {L("gt"), O_GTAGSMODE
}, /* FreeBSD, NetBSD */
279 {L("fe"), O_FILEENCODING
},
280 {L("ht"), O_HARDTABS
}, /* 4BSD */
281 {L("ic"), O_IGNORECASE
}, /* 4BSD */
282 {L("ie"), O_INPUTENCODING
},
283 {L("li"), O_LINES
}, /* 4.4BSD */
284 {L("modelines"), O_MODELINE
}, /* HPUX */
285 {L("nu"), O_NUMBER
}, /* 4BSD */
286 {L("opt"), O_OPTIMIZE
}, /* 4BSD */
287 {L("para"), O_PARAGRAPHS
}, /* 4BSD */
288 {L("re"), O_REDRAW
}, /* O'Reilly */
289 {L("ro"), O_READONLY
}, /* 4BSD (undocumented) */
290 {L("scr"), O_SCROLL
}, /* 4BSD (undocumented) */
291 {L("sect"), O_SECTIONS
}, /* O'Reilly */
292 {L("sh"), O_SHELL
}, /* 4BSD */
293 {L("slow"), O_SLOWOPEN
}, /* 4BSD */
294 {L("sm"), O_SHOWMATCH
}, /* 4BSD */
295 {L("smd"), O_SHOWMODE
}, /* 4BSD */
296 {L("sw"), O_SHIFTWIDTH
}, /* 4BSD */
297 {L("tag"), O_TAGS
}, /* 4BSD (undocumented) */
298 {L("tl"), O_TAGLENGTH
}, /* 4BSD */
299 {L("to"), O_TIMEOUT
}, /* 4BSD (undocumented) */
300 {L("ts"), O_TABSTOP
}, /* 4BSD */
301 {L("tty"), O_TERM
}, /* 4BSD (undocumented) */
302 {L("ttytype"), O_TERM
}, /* 4BSD (undocumented) */
303 {L("w"), O_WINDOW
}, /* O'Reilly */
304 {L("wa"), O_WRITEANY
}, /* 4BSD */
305 {L("wi"), O_WINDOW
}, /* 4BSD (undocumented) */
306 {L("wl"), O_WRAPLEN
}, /* 4.4BSD */
307 {L("wm"), O_WRAPMARGIN
}, /* 4BSD */
308 {L("ws"), O_WRAPSCAN
}, /* 4BSD */
314 * Initialize some of the options.
316 * PUBLIC: int opts_init __P((SCR *, int *));
319 opts_init(SCR
*sp
, int *oargs
)
324 int cnt
, optindx
= 0;
328 if (sizeof optlist
/ sizeof optlist
[0] - 1 != O_OPTIONCOUNT
) {
329 fprintf(stderr
, "vi: option table size error (%d != %d)\n",
330 (int)(sizeof optlist
/ sizeof optlist
[0] - 1),
341 /* Set numeric and string default values. */
342 #define OI(indx, str) { \
343 a.len = STRLEN(str); \
344 if ((const CHAR_T*)str != b2)/* GCC puts strings in text-space. */\
345 (void)MEMCPY(b2, str, a.len+1); \
346 if (opts_set(sp, argv, NULL)) { \
352 * Indirect global options to global space. Specifically, set up
353 * terminal, lines, columns first, they're used by other options.
354 * Note, don't set the flags until we've set up the indirection.
356 if (o_set(sp
, O_TERM
, 0, NULL
, GO_TERM
))
358 F_SET(&sp
->opts
[O_TERM
], OPT_GLOBAL
);
359 if (o_set(sp
, O_LINES
, 0, NULL
, GO_LINES
))
361 F_SET(&sp
->opts
[O_LINES
], OPT_GLOBAL
);
362 if (o_set(sp
, O_COLUMNS
, 0, NULL
, GO_COLUMNS
))
364 F_SET(&sp
->opts
[O_COLUMNS
], OPT_GLOBAL
);
365 if (o_set(sp
, O_SECURE
, 0, NULL
, GO_SECURE
))
367 F_SET(&sp
->opts
[O_SECURE
], OPT_GLOBAL
);
369 /* Initialize string values. */
370 (void)SPRINTF(b2
, SIZE(b2
),
371 L("cdpath=%s"), (s
= getenv("CDPATH")) == NULL
? ":" : s
);
376 * Vi historically stored temporary files in /var/tmp. We store them
377 * in /tmp by default, hoping it's a memory based file system. There
378 * are two ways to change this -- the user can set either the directory
379 * option or the TMPDIR environmental variable.
381 (void)SPRINTF(b2
, SIZE(b2
),
382 L("directory=%s"), (s
= getenv("TMPDIR")) == NULL
? _PATH_TMP
: s
);
383 OI(O_TMP_DIRECTORY
, b2
);
384 OI(O_ESCAPETIME
, L("escapetime=1"));
385 OI(O_KEYTIME
, L("keytime=6"));
386 OI(O_MATCHCHARS
, L("matchchars=()[]{}<>"));
387 OI(O_MATCHTIME
, L("matchtime=7"));
388 (void)SPRINTF(b2
, SIZE(b2
), L("msgcat=%s"), _PATH_MSGCAT
);
390 OI(O_REPORT
, L("report=5"));
391 OI(O_PARAGRAPHS
, L("paragraphs=IPLPPPQPP LIpplpipbp"));
392 (void)SPRINTF(b2
, SIZE(b2
), L("path=%s"), "");
394 (void)SPRINTF(b2
, SIZE(b2
), L("recdir=%s"), _PATH_PRESERVE
);
396 OI(O_SECTIONS
, L("sections=NHSHH HUnhsh"));
397 (void)SPRINTF(b2
, SIZE(b2
),
398 L("shell=%s"), (s
= getenv("SHELL")) == NULL
? _PATH_BSHELL
: s
);
400 OI(O_SHELLMETA
, L("shellmeta=~{[*?$`'\"\\"));
401 OI(O_SHIFTWIDTH
, L("shiftwidth=8"));
402 OI(O_SIDESCROLL
, L("sidescroll=16"));
403 OI(O_TABSTOP
, L("tabstop=8"));
404 (void)SPRINTF(b2
, SIZE(b2
), L("tags=%s"), _PATH_TAGS
);
409 * Initialize O_SCROLL here, after term; initializing term should
410 * have created a LINES/COLUMNS value.
412 if ((v
= (O_VAL(sp
, O_LINES
) - 1) / 2) == 0)
414 (void)SPRINTF(b2
, SIZE(b2
), L("scroll=%ld"), v
);
418 * The default window option values are:
419 * 8 if baud rate <= 600
420 * 16 if baud rate <= 1200
421 * LINES - 1 if baud rate > 1200
423 * Note, the windows option code will correct any too-large value
424 * or when the O_LINES value is 1.
426 if (sp
->gp
->scr_baud(sp
, &v
))
432 else if ((v
= O_VAL(sp
, O_LINES
) - 1) == 0)
435 (void)SPRINTF(b2
, SIZE(b2
), L("window=%lu"), v
);
439 * Set boolean default values, and copy all settings into the default
440 * information. OS_NOFREE is set, we're copying, not replacing.
442 for (op
= optlist
, cnt
= 0; op
->name
!= NULL
; ++op
, ++cnt
)
451 o_set(sp
, cnt
, OS_DEF
, NULL
, O_VAL(sp
, cnt
));
454 if (O_STR(sp
, cnt
) != NULL
&& o_set(sp
, cnt
,
455 OS_DEF
| OS_NOFREE
| OS_STRDUP
, O_STR(sp
, cnt
), 0))
464 * Some options can be initialized by the command name or the
465 * command-line arguments. They don't set the default values,
466 * it's historic practice.
468 for (; *oargs
!= -1; ++oargs
)
469 OI(*oargs
, optlist
[*oargs
].name
);
473 * Inform the underlying screen of the initial values of the
476 for (op
= optlist
, cnt
= 0; op
->name
!= NULL
; ++op
, ++cnt
) {
477 isset
= O_ISSET(sp
, cnt
);
478 (void)sp
->gp
->scr_optchange(sp
, cnt
, O_STR(sp
, cnt
), &isset
);
482 err
: msgq_wstr(sp
, M_ERR
, optlist
[optindx
].name
,
483 "031|Unable to set default %s option");
489 * Change the values of one or more options.
491 * PUBLIC: int opts_set __P((SCR *, ARGS *[], const char *));
494 opts_set(SCR
*sp
, ARGS
**argv
, const char *usage
)
500 u_long isset
, turnoff
, value
;
501 int ch
, equals
, nf
, nf2
, offset
, qmark
, rval
;
502 CHAR_T
*endp
, *name
, *p
, *sep
;
508 for (rval
= 0; argv
[0]->len
!= 0; ++argv
) {
510 * The historic vi dumped the options for each occurrence of
511 * "all" in the set list. Puhleeze.
513 if (!STRCMP(argv
[0]->bp
, L("all"))) {
518 /* Find equals sign or question mark. */
519 for (sep
= NULL
, equals
= qmark
= 0,
520 p
= name
= argv
[0]->bp
; (ch
= *p
) != '\0'; ++p
)
521 if (ch
== '=' || ch
== '?') {
525 "032|Usage: %s", usage
);
541 /* Search for the name, then name without any leading "no". */
542 if ((op
= opts_search(name
)) == NULL
&&
543 name
[0] == L('n') && name
[1] == L('o')) {
546 op
= opts_search(name
);
549 opts_nomatch(sp
, name
);
554 /* Find current option values. */
555 offset
= op
- optlist
;
556 spo
= sp
->opts
+ offset
;
560 * Historically, the question mark could be a separate
563 if (!equals
&& !qmark
&&
564 argv
[1]->len
== 1 && argv
[1]->bp
[0] == '?') {
569 /* Set name, value. */
573 /* Some options may not be reset. */
574 if (F_ISSET(op
, OPT_NOUNSET
) && turnoff
) {
575 msgq_wstr(sp
, M_ERR
, name
,
576 "291|set: the %s option may not be turned off");
581 /* Some options may not be set. */
582 if (F_ISSET(op
, OPT_NOSET
) && !turnoff
) {
583 msgq_wstr(sp
, M_ERR
, name
,
584 "313|set: the %s option may never be turned on");
590 msgq_wstr(sp
, M_ERR
, name
,
591 "034|set: [no]%s option doesn't take a value");
597 disp
= SELECT_DISPLAY
;
598 F_SET(spo
, OPT_SELECTED
);
603 * Do nothing if the value is unchanged, the underlying
604 * functions can be expensive.
607 if (!F_ISSET(op
, OPT_ALWAYS
)) {
609 if (O_ISSET(sp
, offset
))
612 if (!O_ISSET(sp
, offset
))
616 /* Report to subsystems. */
617 if ((op
->func
!= NULL
&&
618 op
->func(sp
, spo
, NULL
, &isset
)) ||
619 ex_optchange(sp
, offset
, NULL
, &isset
) ||
620 v_optchange(sp
, offset
, NULL
, &isset
) ||
621 sp
->gp
->scr_optchange(sp
, offset
, NULL
, &isset
)) {
634 msgq_wstr(sp
, M_ERR
, name
,
635 "035|set: %s option isn't a boolean");
639 if (qmark
|| !equals
) {
641 disp
= SELECT_DISPLAY
;
642 F_SET(spo
, OPT_SELECTED
);
646 if (!ISDIGIT((UCHAR_T
)sep
[0]))
649 nget_uslong(sp
, &value
, sep
, &endp
, 10)) != NUM_OK
) {
650 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
);
659 "036|set: %s option: %s", p2
, t2
);
663 "037|set: %s option: %s: value overflow", p2
, t2
);
670 FREE_SPACE(sp
, p2
, 0);
672 FREE_SPACE(sp
, t2
, 0);
676 if (*endp
&& !ISBLANK(*endp
)) {
677 badnum
: INT2CHAR(sp
, name
, STRLEN(name
) + 1,
679 p2
= msg_print(sp
, np
, &nf
);
680 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1,
682 t2
= msg_print(sp
, np
, &nf2
);
684 "038|set: %s option: %s is an illegal number", p2
, t2
);
686 FREE_SPACE(sp
, p2
, 0);
688 FREE_SPACE(sp
, t2
, 0);
693 /* Some options may never be set to zero. */
694 if (F_ISSET(op
, OPT_NOZERO
) && value
== 0) {
695 msgq_wstr(sp
, M_ERR
, name
,
696 "314|set: the %s option may never be set to 0");
702 * Do nothing if the value is unchanged, the underlying
703 * functions can be expensive.
705 if (!F_ISSET(op
, OPT_ALWAYS
) &&
706 O_VAL(sp
, offset
) == value
)
709 /* Report to subsystems. */
710 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1, np
, nlen
);
711 if ((op
->func
!= NULL
&&
712 op
->func(sp
, spo
, np
, &value
)) ||
713 ex_optchange(sp
, offset
, np
, &value
) ||
714 v_optchange(sp
, offset
, np
, &value
) ||
715 sp
->gp
->scr_optchange(sp
, offset
, np
, &value
)) {
721 if (o_set(sp
, offset
, 0, NULL
, value
))
726 msgq_wstr(sp
, M_ERR
, name
,
727 "039|set: %s option isn't a boolean");
731 if (qmark
|| !equals
) {
733 disp
= SELECT_DISPLAY
;
734 F_SET(spo
, OPT_SELECTED
);
738 /* Check for strings that must have even length */
739 if (F_ISSET(op
, OPT_PAIRS
) && STRLEN(sep
) & 1) {
740 msgq_wstr(sp
, M_ERR
, name
,
741 "047|set: the %s option must be in two character groups");
747 * Do nothing if the value is unchanged, the underlying
748 * functions can be expensive.
750 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1, np
, nlen
);
751 if (!F_ISSET(op
, OPT_ALWAYS
) &&
752 O_STR(sp
, offset
) != NULL
&&
753 !strcmp(O_STR(sp
, offset
), np
))
756 /* Report to subsystems. */
757 if ((op
->func
!= NULL
&&
758 op
->func(sp
, spo
, np
, NULL
)) ||
759 ex_optchange(sp
, offset
, np
, NULL
) ||
760 v_optchange(sp
, offset
, np
, NULL
) ||
761 sp
->gp
->scr_optchange(sp
, offset
, np
, NULL
)) {
767 if (o_set(sp
, offset
, OS_STRDUP
, np
, 0))
774 if (disp
!= NO_DISPLAY
)
781 * Set an option's value.
783 * PUBLIC: int o_set __P((SCR *, int, u_int, const char *, u_long));
786 o_set(SCR
*sp
, int opt
, u_int flags
, const char *str
, u_long val
)
790 /* Set a pointer to the options area. */
791 op
= F_ISSET(&sp
->opts
[opt
], OPT_GLOBAL
) ?
792 &sp
->gp
->opts
[sp
->opts
[opt
].o_cur
.val
] : &sp
->opts
[opt
];
794 /* Copy the string, if requested. */
795 if (LF_ISSET(OS_STRDUP
) && (str
= strdup(str
)) == NULL
) {
796 msgq(sp
, M_SYSERR
, NULL
);
800 /* Free the previous string, if requested, and set the value. */
802 if (LF_ISSET(OS_STR
| OS_STRDUP
)) {
803 if (!LF_ISSET(OS_NOFREE
) && op
->o_def
.str
!= NULL
)
804 free(__UNCONST(op
->o_def
.str
));
809 if (LF_ISSET(OS_STR
| OS_STRDUP
)) {
810 if (!LF_ISSET(OS_NOFREE
) && op
->o_cur
.str
!= NULL
)
811 free(__UNCONST(op
->o_cur
.str
));
820 * Return 1 if the string option is invalid, 0 if it's OK.
822 * PUBLIC: int opts_empty __P((SCR *, int, int));
825 opts_empty(SCR
*sp
, int off
, int silent
)
829 if ((p
= O_STR(sp
, off
)) == NULL
|| p
[0] == '\0') {
831 msgq_wstr(sp
, M_ERR
, optlist
[off
].name
,
832 "305|No %s edit option specified");
840 * List the current values of selected options.
842 * PUBLIC: void opts_dump __P((SCR *, enum optdisp));
845 opts_dump(SCR
*sp
, enum optdisp type
)
848 int base
, b_num
, cnt
, col
, colwidth
, curlen
, s_num
;
849 int numcols
, numrows
, row
;
850 int b_op
[O_OPTIONCOUNT
], s_op
[O_OPTIONCOUNT
];
854 * Options are output in two groups -- those that fit in a column and
855 * those that don't. Output is done on 6 character "tab" boundaries
856 * for no particular reason. (Since we don't output tab characters,
857 * we can ignore the terminal's tab settings.) Ignore the user's tab
858 * setting because we have no idea how reasonable it is.
860 * Find a column width we can live with, testing from 10 columns to 1.
862 for (numcols
= 10; numcols
> 1; --numcols
) {
863 colwidth
= sp
->cols
/ numcols
& ~(STANDARD_TAB
- 1);
864 if (colwidth
>= 10) {
866 (colwidth
+ STANDARD_TAB
) & ~(STANDARD_TAB
- 1);
867 numcols
= sp
->cols
/ colwidth
;
874 * Get the set of options to list, entering them into
875 * the column list or the overflow list.
877 for (b_num
= s_num
= 0, op
= optlist
; op
->name
!= NULL
; ++op
) {
880 /* If OPT_NDISP set, it's never displayed. */
881 if (F_ISSET(op
, OPT_NDISP
))
885 case ALL_DISPLAY
: /* Display all. */
887 case CHANGED_DISPLAY
: /* Display changed. */
888 /* If OPT_ADISP set, it's always "changed". */
889 if (F_ISSET(op
, OPT_ADISP
))
895 if (O_VAL(sp
, cnt
) == O_D_VAL(sp
, cnt
))
899 if (O_STR(sp
, cnt
) == O_D_STR(sp
, cnt
) ||
900 (O_D_STR(sp
, cnt
) != NULL
&&
901 !strcmp(O_STR(sp
, cnt
), O_D_STR(sp
, cnt
))))
906 case SELECT_DISPLAY
: /* Display selected. */
907 if (!F_ISSET(&sp
->opts
[cnt
], OPT_SELECTED
))
914 F_CLR(&sp
->opts
[cnt
], OPT_SELECTED
);
916 curlen
= STRLEN(op
->name
);
920 if (!O_ISSET(sp
, cnt
))
925 sizeof(nbuf
), "%ld", O_VAL(sp
, cnt
));
926 curlen
+= strlen(nbuf
);
929 if (O_STR(sp
, cnt
) != NULL
)
930 curlen
+= strlen(O_STR(sp
, cnt
));
934 /* Offset by 2 so there's a gap. */
935 if (curlen
<= colwidth
- 2)
942 /* Figure out the number of rows. */
943 if (s_num
> numcols
) {
944 numrows
= s_num
/ numcols
;
950 /* Display the options in sorted order. */
951 for (row
= 0; row
< numrows
;) {
952 for (base
= row
, col
= 0; col
< numcols
; ++col
) {
953 cnt
= opts_print(sp
, &optlist
[s_op
[base
]]);
954 if ((base
+= numrows
) >= s_num
)
956 (void)ex_printf(sp
, "%*s",
957 (int)(colwidth
- cnt
), "");
959 if (++row
< numrows
|| b_num
)
960 (void)ex_puts(sp
, "\n");
964 for (row
= 0; row
< b_num
;) {
965 (void)opts_print(sp
, &optlist
[b_op
[row
]]);
967 (void)ex_puts(sp
, "\n");
969 (void)ex_puts(sp
, "\n");
974 * Print out an option.
977 opts_print(SCR
*sp
, const OPTLIST
*op
)
982 offset
= op
- optlist
;
986 curlen
+= ex_printf(sp
,
987 "%s"WS
, O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
990 curlen
+= ex_printf(sp
, WS
"=%ld", op
->name
, O_VAL(sp
, offset
));
993 curlen
+= ex_printf(sp
, WS
"=\"%s\"", op
->name
,
994 O_STR(sp
, offset
) == NULL
? "" : O_STR(sp
, offset
));
1002 * Write the current configuration to a file.
1004 * PUBLIC: int opts_save __P((SCR *, FILE *));
1007 opts_save(SCR
*sp
, FILE *fp
)
1016 for (op
= optlist
; op
->name
!= NULL
; ++op
) {
1017 if (F_ISSET(op
, OPT_NOSAVE
))
1023 if (O_ISSET(sp
, cnt
))
1024 (void)fprintf(fp
, "set "WS
"\n", op
->name
);
1026 (void)fprintf(fp
, "set no"WS
"\n", op
->name
);
1030 "set "WS
"=%-3ld\n", op
->name
, O_VAL(sp
, cnt
));
1033 if (O_STR(sp
, cnt
) == NULL
)
1035 (void)fprintf(fp
, "set ");
1036 for (p
= op
->name
; (ch
= *p
) != L('\0'); ++p
) {
1037 if (ISBLANK(ch
) || ch
== L('\\'))
1038 (void)putc('\\', fp
);
1039 fprintf(fp
, WC
, ch
);
1041 (void)putc('=', fp
);
1042 for (np
= O_STR(sp
, cnt
); (nch
= *np
) != '\0'; ++np
) {
1043 if (isblank((unsigned char)nch
) || nch
== '\\')
1044 (void)putc('\\', fp
);
1045 (void)putc(nch
, fp
);
1047 (void)putc('\n', fp
);
1051 msgq(sp
, M_SYSERR
, NULL
);
1060 * Search for an option.
1062 * PUBLIC: OPTLIST const *opts_search __P((const CHAR_T *));
1065 opts_search(const CHAR_T
*name
)
1067 OPTLIST
const *op
, *found
;
1072 /* Check list of abbreviations. */
1074 if ((ap
= bsearch(&atmp
, abbrev
, sizeof(abbrev
) / sizeof(OABBREV
) - 1,
1075 sizeof(OABBREV
), opts_abbcmp
)) != NULL
)
1076 return (optlist
+ ap
->offset
);
1078 /* Check list of options. */
1080 if ((op
= bsearch(&otmp
, optlist
, sizeof(optlist
) / sizeof(OPTLIST
) - 1,
1081 sizeof(OPTLIST
), opts_cmp
)) != NULL
)
1085 * Check to see if the name is the prefix of one (and only one)
1086 * option. If so, return the option.
1089 for (found
= NULL
, op
= optlist
; op
->name
!= NULL
; ++op
) {
1090 if (op
->name
[0] < name
[0])
1092 if (op
->name
[0] > name
[0])
1094 if (!MEMCMP(op
->name
, name
, len
)) {
1105 * Standard nomatch error message for options.
1107 * PUBLIC: void opts_nomatch __P((SCR *, const CHAR_T *));
1110 opts_nomatch(SCR
*sp
, const CHAR_T
*name
)
1112 msgq_wstr(sp
, M_ERR
, name
,
1113 "033|set: no %s option: 'set all' gives all option values");
1117 opts_abbcmp(const void *a
, const void *b
)
1119 return(STRCMP(((const OABBREV
*)a
)->name
, ((const OABBREV
*)b
)->name
));
1123 opts_cmp(const void *a
, const void *b
)
1125 return(STRCMP(((const OPTLIST
*)a
)->name
, ((const OPTLIST
*)b
)->name
));
1130 * Copy a screen's OPTION array.
1132 * PUBLIC: int opts_copy __P((SCR *, SCR *));
1135 opts_copy(SCR
*orig
, SCR
*sp
)
1139 /* Copy most everything without change. */
1140 memcpy(sp
->opts
, orig
->opts
, sizeof(orig
->opts
));
1142 /* Copy the string edit options. */
1143 for (cnt
= rval
= 0; cnt
< O_OPTIONCOUNT
; ++cnt
) {
1144 if (optlist
[cnt
].type
!= OPT_STR
||
1145 F_ISSET(&sp
->opts
[cnt
], OPT_GLOBAL
))
1148 * If never set, or already failed, NULL out the entries --
1149 * have to continue after failure, otherwise would have two
1150 * screens referencing the same memory.
1152 if (rval
|| O_STR(sp
, cnt
) == NULL
) {
1153 o_set(sp
, cnt
, OS_NOFREE
| OS_STR
, NULL
, 0);
1154 o_set(sp
, cnt
, OS_DEF
| OS_NOFREE
| OS_STR
, NULL
, 0);
1158 /* Copy the current string. */
1159 if (o_set(sp
, cnt
, OS_NOFREE
| OS_STRDUP
, O_STR(sp
, cnt
), 0)) {
1160 o_set(sp
, cnt
, OS_DEF
| OS_NOFREE
| OS_STR
, NULL
, 0);
1164 /* Copy the default string. */
1165 if (O_D_STR(sp
, cnt
) != NULL
&& o_set(sp
, cnt
,
1166 OS_DEF
| OS_NOFREE
| OS_STRDUP
, O_D_STR(sp
, cnt
), 0)) {
1167 nomem
: msgq(orig
, M_SYSERR
, NULL
);
1176 * Free all option strings
1178 * PUBLIC: void opts_free __P((SCR *));
1185 for (cnt
= 0; cnt
< O_OPTIONCOUNT
; ++cnt
) {
1186 if (optlist
[cnt
].type
!= OPT_STR
||
1187 F_ISSET(&sp
->opts
[cnt
], OPT_GLOBAL
))
1189 if (O_STR(sp
, cnt
) != NULL
)
1190 free(__UNCONST(O_STR(sp
, cnt
)));
1191 if (O_D_STR(sp
, cnt
) != NULL
)
1192 free(__UNCONST(O_D_STR(sp
, cnt
)));