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.62 2001/06/25 15:19:11 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:11 $";
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 {L("altwerase"), f_altwerase
, OPT_0BOOL
, 0},
49 /* O_AUTOINDENT 4BSD */
50 {L("autoindent"), NULL
, OPT_0BOOL
, 0},
51 /* O_AUTOPRINT 4BSD */
52 {L("autoprint"), NULL
, OPT_1BOOL
, 0},
53 /* O_AUTOWRITE 4BSD */
54 {L("autowrite"), NULL
, OPT_0BOOL
, 0},
56 {L("backup"), NULL
, OPT_STR
, 0},
58 {L("beautify"), NULL
, OPT_0BOOL
, 0},
60 {L("cdpath"), NULL
, OPT_STR
, 0},
62 {L("cedit"), NULL
, OPT_STR
, 0},
63 /* O_COLUMNS 4.4BSD */
64 {L("columns"), f_columns
, OPT_NUM
, OPT_NOSAVE
},
65 /* O_COMMENT 4.4BSD */
66 {L("comment"), NULL
, OPT_0BOOL
, 0},
67 /* O_DIRECTORY 4BSD */
68 {L("directory"), NULL
, OPT_STR
, 0},
69 /* O_EDCOMPATIBLE 4BSD */
70 {L("edcompatible"),NULL
, OPT_0BOOL
, 0},
71 /* O_ESCAPETIME 4.4BSD */
72 {L("escapetime"), NULL
, OPT_NUM
, 0},
73 /* O_ERRORBELLS 4BSD */
74 {L("errorbells"), NULL
, OPT_0BOOL
, 0},
75 /* O_EXRC System V (undocumented) */
76 {L("exrc"), NULL
, OPT_0BOOL
, 0},
77 /* O_EXTENDED 4.4BSD */
78 {L("extended"), f_recompile
, OPT_0BOOL
, 0},
80 {L("filec"), NULL
, OPT_STR
, 0},
82 {L("fileencoding"),f_encoding
, OPT_STR
, 0},
84 {L("flash"), NULL
, OPT_1BOOL
, 0},
86 {L("hardtabs"), NULL
, OPT_NUM
, 0},
87 /* O_ICLOWER 4.4BSD */
88 {L("iclower"), f_recompile
, OPT_0BOOL
, 0},
89 /* O_IGNORECASE 4BSD */
90 {L("ignorecase"), f_recompile
, OPT_0BOOL
, 0},
92 {L("inputencoding"),f_encoding
, OPT_STR
, 0},
93 /* O_KEYTIME 4.4BSD */
94 {L("keytime"), NULL
, OPT_NUM
, 0},
95 /* O_LEFTRIGHT 4.4BSD */
96 {L("leftright"), f_reformat
, OPT_0BOOL
, 0},
98 {L("lines"), f_lines
, OPT_NUM
, OPT_NOSAVE
},
101 * When the lisp option is implemented, delete the OPT_NOSAVE flag,
102 * so that :mkexrc dumps it.
104 {L("lisp"), f_lisp
, OPT_0BOOL
, OPT_NOSAVE
},
106 {L("list"), f_reformat
, OPT_0BOOL
, 0},
107 /* O_LOCKFILES 4.4BSD
109 * Locking isn't reliable enough over NFS to require it, in addition,
110 * it's a serious startup performance problem over some remote links.
112 {L("lock"), NULL
, OPT_1BOOL
, 0},
114 {L("magic"), NULL
, OPT_1BOOL
, 0},
115 /* O_MATCHTIME 4.4BSD */
116 {L("matchtime"), NULL
, OPT_NUM
, 0},
118 {L("mesg"), NULL
, OPT_1BOOL
, 0},
121 * This has been documented in historical systems as both "modeline"
122 * and as "modelines". Regardless of the name, this option represents
123 * a security problem of mammoth proportions, not to mention a stunning
124 * example of what your intro CS professor referred to as the perils of
125 * mixing code and data. Don't add it, or I will kill you.
127 {L("modeline"), NULL
, OPT_0BOOL
, OPT_NOSET
},
128 /* O_MSGCAT 4.4BSD */
129 {L("msgcat"), f_msgcat
, OPT_STR
, 0},
130 /* O_NOPRINT 4.4BSD */
131 {L("noprint"), f_print
, OPT_STR
, 0},
133 {L("number"), f_reformat
, OPT_0BOOL
, 0},
135 {L("octal"), f_print
, OPT_0BOOL
, 0},
137 {L("open"), NULL
, OPT_1BOOL
, 0},
138 /* O_OPTIMIZE 4BSD */
139 {L("optimize"), NULL
, OPT_1BOOL
, 0},
140 /* O_PARAGRAPHS 4BSD */
141 {L("paragraphs"), f_paragraph
, OPT_STR
, 0},
143 {L("path"), NULL
, OPT_STR
, 0},
145 {L("print"), f_print
, OPT_STR
, 0},
147 {L("prompt"), NULL
, OPT_1BOOL
, 0},
148 /* O_READONLY 4BSD (undocumented) */
149 {L("readonly"), f_readonly
, OPT_0BOOL
, OPT_ALWAYS
},
150 /* O_RECDIR 4.4BSD */
151 {L("recdir"), NULL
, OPT_STR
, 0},
153 {L("redraw"), NULL
, OPT_0BOOL
, 0},
155 {L("remap"), NULL
, OPT_1BOOL
, 0},
157 {L("report"), NULL
, OPT_NUM
, 0},
159 {L("ruler"), NULL
, OPT_0BOOL
, 0},
161 {L("scroll"), NULL
, OPT_NUM
, 0},
162 /* O_SEARCHINCR 4.4BSD */
163 {L("searchincr"), NULL
, OPT_0BOOL
, 0},
164 /* O_SECTIONS 4BSD */
165 {L("sections"), f_section
, OPT_STR
, 0},
166 /* O_SECURE 4.4BSD */
167 {L("secure"), NULL
, OPT_0BOOL
, OPT_NOUNSET
},
169 {L("shell"), NULL
, OPT_STR
, 0},
170 /* O_SHELLMETA 4.4BSD */
171 {L("shellmeta"), NULL
, OPT_STR
, 0},
172 /* O_SHIFTWIDTH 4BSD */
173 {L("shiftwidth"), NULL
, OPT_NUM
, OPT_NOZERO
},
174 /* O_SHOWMATCH 4BSD */
175 {L("showmatch"), NULL
, OPT_0BOOL
, 0},
176 /* O_SHOWMODE 4.4BSD */
177 {L("showmode"), NULL
, OPT_0BOOL
, 0},
178 /* O_SIDESCROLL 4.4BSD */
179 {L("sidescroll"), NULL
, OPT_NUM
, OPT_NOZERO
},
180 /* O_SLOWOPEN 4BSD */
181 {L("slowopen"), NULL
, OPT_0BOOL
, 0},
182 /* O_SOURCEANY 4BSD (undocumented)
184 * Historic vi, on startup, source'd $HOME/.exrc and ./.exrc, if they
185 * were owned by the user. The sourceany option was an undocumented
186 * feature of historic vi which permitted the startup source'ing of
187 * .exrc files the user didn't own. This is an obvious security problem,
188 * and we ignore the option.
190 {L("sourceany"), NULL
, OPT_0BOOL
, OPT_NOSET
},
192 {L("tabstop"), f_reformat
, OPT_NUM
, OPT_NOZERO
},
193 /* O_TAGLENGTH 4BSD */
194 {L("taglength"), NULL
, OPT_NUM
, 0},
196 {L("tags"), NULL
, OPT_STR
, 0},
199 * By default, the historic vi always displayed information about two
200 * options, redraw and term. Term seems sufficient.
202 {L("term"), NULL
, OPT_STR
, OPT_ADISP
|OPT_NOSAVE
},
204 {L("terse"), NULL
, OPT_0BOOL
, 0},
205 /* O_TILDEOP 4.4BSD */
206 {L("tildeop"), NULL
, OPT_0BOOL
, 0},
207 /* O_TIMEOUT 4BSD (undocumented) */
208 {L("timeout"), NULL
, OPT_1BOOL
, 0},
209 /* O_TTYWERASE 4.4BSD */
210 {L("ttywerase"), f_ttywerase
, OPT_0BOOL
, 0},
211 /* O_VERBOSE 4.4BSD */
212 {L("verbose"), NULL
, OPT_0BOOL
, 0},
214 {L("w1200"), f_w1200
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
216 {L("w300"), f_w300
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
218 {L("w9600"), f_w9600
, OPT_NUM
, OPT_NDISP
|OPT_NOSAVE
},
220 {L("warn"), NULL
, OPT_1BOOL
, 0},
222 {L("window"), f_window
, OPT_NUM
, 0},
223 /* O_WINDOWNAME 4BSD */
224 {L("windowname"), NULL
, OPT_0BOOL
, 0},
225 /* O_WRAPLEN 4.4BSD */
226 {L("wraplen"), NULL
, OPT_NUM
, 0},
227 /* O_WRAPMARGIN 4BSD */
228 {L("wrapmargin"), NULL
, OPT_NUM
, 0},
229 /* O_WRAPSCAN 4BSD */
230 {L("wrapscan"), NULL
, OPT_1BOOL
, 0},
231 /* O_WRITEANY 4BSD */
232 {L("writeany"), NULL
, OPT_0BOOL
, 0},
236 typedef struct abbrev
{
241 static OABBREV
const abbrev
[] = {
242 {L("ai"), O_AUTOINDENT
}, /* 4BSD */
243 {L("ap"), O_AUTOPRINT
}, /* 4BSD */
244 {L("aw"), O_AUTOWRITE
}, /* 4BSD */
245 {L("bf"), O_BEAUTIFY
}, /* 4BSD */
246 {L("co"), O_COLUMNS
}, /* 4.4BSD */
247 {L("dir"), O_DIRECTORY
}, /* 4BSD */
248 {L("eb"), O_ERRORBELLS
}, /* 4BSD */
249 {L("ed"), O_EDCOMPATIBLE
}, /* 4BSD */
250 {L("ex"), O_EXRC
}, /* System V (undocumented) */
251 {L("fe"), O_FILEENCODING
},
252 {L("ht"), O_HARDTABS
}, /* 4BSD */
253 {L("ic"), O_IGNORECASE
}, /* 4BSD */
254 {L("ie"), O_INPUTENCODING
},
255 {L("li"), O_LINES
}, /* 4.4BSD */
256 {L("modelines"), O_MODELINE
}, /* HPUX */
257 {L("nu"), O_NUMBER
}, /* 4BSD */
258 {L("opt"), O_OPTIMIZE
}, /* 4BSD */
259 {L("para"), O_PARAGRAPHS
}, /* 4BSD */
260 {L("re"), O_REDRAW
}, /* O'Reilly */
261 {L("ro"), O_READONLY
}, /* 4BSD (undocumented) */
262 {L("scr"), O_SCROLL
}, /* 4BSD (undocumented) */
263 {L("sect"), O_SECTIONS
}, /* O'Reilly */
264 {L("sh"), O_SHELL
}, /* 4BSD */
265 {L("slow"), O_SLOWOPEN
}, /* 4BSD */
266 {L("sm"), O_SHOWMATCH
}, /* 4BSD */
267 {L("smd"), O_SHOWMODE
}, /* 4BSD */
268 {L("sw"), O_SHIFTWIDTH
}, /* 4BSD */
269 {L("tag"), O_TAGS
}, /* 4BSD (undocumented) */
270 {L("tl"), O_TAGLENGTH
}, /* 4BSD */
271 {L("to"), O_TIMEOUT
}, /* 4BSD (undocumented) */
272 {L("ts"), O_TABSTOP
}, /* 4BSD */
273 {L("tty"), O_TERM
}, /* 4BSD (undocumented) */
274 {L("ttytype"), O_TERM
}, /* 4BSD (undocumented) */
275 {L("w"), O_WINDOW
}, /* O'Reilly */
276 {L("wa"), O_WRITEANY
}, /* 4BSD */
277 {L("wi"), O_WINDOW
}, /* 4BSD (undocumented) */
278 {L("wl"), O_WRAPLEN
}, /* 4.4BSD */
279 {L("wm"), O_WRAPMARGIN
}, /* 4BSD */
280 {L("ws"), O_WRAPSCAN
}, /* 4BSD */
286 * Initialize some of the options.
288 * PUBLIC: int opts_init __P((SCR *, int *));
291 opts_init(SCR
*sp
, int *oargs
)
308 /* Set numeric and string default values. */
309 #define OI(indx, str) { \
310 a.len = STRLEN(str); \
311 if ((CHAR_T*)str != b2) /* GCC puts strings in text-space. */ \
312 (void)MEMCPY(b2, str, a.len+1); \
313 if (opts_set(sp, argv, NULL)) { \
319 * Indirect global options to global space. Specifically, set up
320 * terminal, lines, columns first, they're used by other options.
321 * Note, don't set the flags until we've set up the indirection.
323 if (o_set(sp
, O_TERM
, 0, NULL
, GO_TERM
))
325 F_SET(&sp
->opts
[O_TERM
], OPT_GLOBAL
);
326 if (o_set(sp
, O_LINES
, 0, NULL
, GO_LINES
))
328 F_SET(&sp
->opts
[O_LINES
], OPT_GLOBAL
);
329 if (o_set(sp
, O_COLUMNS
, 0, NULL
, GO_COLUMNS
))
331 F_SET(&sp
->opts
[O_COLUMNS
], OPT_GLOBAL
);
332 if (o_set(sp
, O_SECURE
, 0, NULL
, GO_SECURE
))
334 F_SET(&sp
->opts
[O_SECURE
], OPT_GLOBAL
);
336 /* Initialize string values. */
337 (void)SPRINTF(b2
, SIZE(b2
),
338 L("cdpath=%s"), (s
= getenv("CDPATH")) == NULL
? ":" : s
);
343 * Vi historically stored temporary files in /var/tmp. We store them
344 * in /tmp by default, hoping it's a memory based file system. There
345 * are two ways to change this -- the user can set either the directory
346 * option or the TMPDIR environmental variable.
348 (void)SPRINTF(b2
, SIZE(b2
),
349 L("directory=%s"), (s
= getenv("TMPDIR")) == NULL
? _PATH_TMP
: s
);
351 OI(O_ESCAPETIME
, L("escapetime=1"));
352 OI(O_KEYTIME
, L("keytime=6"));
353 OI(O_MATCHTIME
, L("matchtime=7"));
354 (void)SPRINTF(b2
, SIZE(b2
), L("msgcat=%s"), _PATH_MSGCAT
);
356 OI(O_REPORT
, L("report=5"));
357 OI(O_PARAGRAPHS
, L("paragraphs=IPLPPPQPP LIpplpipbp"));
358 (void)SPRINTF(b2
, SIZE(b2
), L("path=%s"), "");
360 (void)SPRINTF(b2
, SIZE(b2
), L("recdir=%s"), _PATH_PRESERVE
);
362 OI(O_SECTIONS
, L("sections=NHSHH HUnhsh"));
363 (void)SPRINTF(b2
, SIZE(b2
),
364 L("shell=%s"), (s
= getenv("SHELL")) == NULL
? _PATH_BSHELL
: s
);
366 OI(O_SHELLMETA
, L("shellmeta=~{[*?$`'\"\\"));
367 OI(O_SHIFTWIDTH
, L("shiftwidth=8"));
368 OI(O_SIDESCROLL
, L("sidescroll=16"));
369 OI(O_TABSTOP
, L("tabstop=8"));
370 (void)SPRINTF(b2
, SIZE(b2
), L("tags=%s"), _PATH_TAGS
);
375 * Initialize O_SCROLL here, after term; initializing term should
376 * have created a LINES/COLUMNS value.
378 if ((v
= (O_VAL(sp
, O_LINES
) - 1) / 2) == 0)
380 (void)SPRINTF(b2
, SIZE(b2
), L("scroll=%ld"), v
);
384 * The default window option values are:
385 * 8 if baud rate <= 600
386 * 16 if baud rate <= 1200
387 * LINES - 1 if baud rate > 1200
389 * Note, the windows option code will correct any too-large value
390 * or when the O_LINES value is 1.
392 if (sp
->gp
->scr_baud(sp
, &v
))
398 else if ((v
= O_VAL(sp
, O_LINES
) - 1) == 0)
401 (void)SPRINTF(b2
, SIZE(b2
), L("window=%lu"), v
);
405 * Set boolean default values, and copy all settings into the default
406 * information. OS_NOFREE is set, we're copying, not replacing.
408 for (op
= optlist
, cnt
= 0; op
->name
!= NULL
; ++op
, ++cnt
)
417 o_set(sp
, cnt
, OS_DEF
, NULL
, O_VAL(sp
, cnt
));
420 if (O_STR(sp
, cnt
) != NULL
&& o_set(sp
, cnt
,
421 OS_DEF
| OS_NOFREE
| OS_STRDUP
, O_STR(sp
, cnt
), 0))
430 * Some options can be initialized by the command name or the
431 * command-line arguments. They don't set the default values,
432 * it's historic practice.
434 for (; *oargs
!= -1; ++oargs
)
435 OI(*oargs
, optlist
[*oargs
].name
);
439 * Inform the underlying screen of the initial values of the
442 for (op
= optlist
, cnt
= 0; op
->name
!= NULL
; ++op
, ++cnt
) {
443 isset
= O_ISSET(sp
, cnt
);
444 (void)sp
->gp
->scr_optchange(sp
, cnt
, O_STR(sp
, cnt
), &isset
);
449 "031|Unable to set default %s option", optlist
[optindx
].name
);
455 * Change the values of one or more options.
457 * PUBLIC: int opts_set __P((SCR *, ARGS *[], char *));
460 opts_set(SCR
*sp
, ARGS
**argv
, char *usage
)
466 u_long isset
, turnoff
, value
;
467 int ch
, equals
, nf
, nf2
, offset
, qmark
, rval
;
468 CHAR_T
*endp
, *name
, *p
, *sep
, *t
;
474 for (rval
= 0; argv
[0]->len
!= 0; ++argv
) {
476 * The historic vi dumped the options for each occurrence of
477 * "all" in the set list. Puhleeze.
479 if (!STRCMP(argv
[0]->bp
, L("all"))) {
484 /* Find equals sign or question mark. */
485 for (sep
= NULL
, equals
= qmark
= 0,
486 p
= name
= argv
[0]->bp
; (ch
= *p
) != '\0'; ++p
)
487 if (ch
== '=' || ch
== '?') {
491 "032|Usage: %s", usage
);
507 /* Search for the name, then name without any leading "no". */
508 if ((op
= opts_search(name
)) == NULL
&&
509 name
[0] == L('n') && name
[1] == L('o')) {
512 op
= opts_search(name
);
515 opts_nomatch(sp
, name
);
520 /* Find current option values. */
521 offset
= op
- optlist
;
522 spo
= sp
->opts
+ offset
;
526 * Historically, the question mark could be a separate
529 if (!equals
&& !qmark
&&
530 argv
[1]->len
== 1 && argv
[1]->bp
[0] == '?') {
535 /* Set name, value. */
539 /* Some options may not be reset. */
540 if (F_ISSET(op
, OPT_NOUNSET
) && turnoff
) {
541 msgq_wstr(sp
, M_ERR
, name
,
542 "291|set: the %s option may not be turned off");
547 /* Some options may not be set. */
548 if (F_ISSET(op
, OPT_NOSET
) && !turnoff
) {
549 msgq_wstr(sp
, M_ERR
, name
,
550 "313|set: the %s option may never be turned on");
556 msgq_wstr(sp
, M_ERR
, name
,
557 "034|set: [no]%s option doesn't take a value");
563 disp
= SELECT_DISPLAY
;
564 F_SET(spo
, OPT_SELECTED
);
569 * Do nothing if the value is unchanged, the underlying
570 * functions can be expensive.
573 if (!F_ISSET(op
, OPT_ALWAYS
))
575 if (O_ISSET(sp
, offset
))
578 if (!O_ISSET(sp
, offset
))
581 /* Report to subsystems. */
582 if (op
->func
!= NULL
&&
583 op
->func(sp
, spo
, NULL
, &isset
) ||
584 ex_optchange(sp
, offset
, NULL
, &isset
) ||
585 v_optchange(sp
, offset
, NULL
, &isset
) ||
586 sp
->gp
->scr_optchange(sp
, offset
, NULL
, &isset
)) {
599 msgq_wstr(sp
, M_ERR
, name
,
600 "035|set: %s option isn't a boolean");
604 if (qmark
|| !equals
) {
606 disp
= SELECT_DISPLAY
;
607 F_SET(spo
, OPT_SELECTED
);
611 if (!ISDIGIT(sep
[0]))
614 nget_uslong(sp
, &value
, sep
, &endp
, 10)) != NUM_OK
) {
615 INT2CHAR(sp
, name
, STRLEN(name
) + 1,
617 p2
= msg_print(sp
, np
, &nf
);
618 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1,
620 t2
= msg_print(sp
, np
, &nf2
);
624 "036|set: %s option: %s", p2
, t2
);
628 "037|set: %s option: %s: value overflow", p2
, t2
);
635 FREE_SPACE(sp
, p2
, 0);
637 FREE_SPACE(sp
, t2
, 0);
641 if (*endp
&& !ISBLANK(*endp
)) {
642 badnum
: INT2CHAR(sp
, name
, STRLEN(name
) + 1,
644 p2
= msg_print(sp
, np
, &nf
);
645 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1,
647 t2
= msg_print(sp
, np
, &nf2
);
649 "038|set: %s option: %s is an illegal number", p2
, t2
);
651 FREE_SPACE(sp
, p2
, 0);
653 FREE_SPACE(sp
, t2
, 0);
658 /* Some options may never be set to zero. */
659 if (F_ISSET(op
, OPT_NOZERO
) && value
== 0) {
660 msgq_wstr(sp
, M_ERR
, name
,
661 "314|set: the %s option may never be set to 0");
667 * Do nothing if the value is unchanged, the underlying
668 * functions can be expensive.
670 if (!F_ISSET(op
, OPT_ALWAYS
) &&
671 O_VAL(sp
, offset
) == value
)
674 /* Report to subsystems. */
675 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1, np
, nlen
);
676 if (op
->func
!= NULL
&&
677 op
->func(sp
, spo
, np
, &value
) ||
678 ex_optchange(sp
, offset
, np
, &value
) ||
679 v_optchange(sp
, offset
, np
, &value
) ||
680 sp
->gp
->scr_optchange(sp
, offset
, np
, &value
)) {
686 if (o_set(sp
, offset
, 0, NULL
, value
))
691 msgq_wstr(sp
, M_ERR
, name
,
692 "039|set: %s option isn't a boolean");
696 if (qmark
|| !equals
) {
698 disp
= SELECT_DISPLAY
;
699 F_SET(spo
, OPT_SELECTED
);
704 * Do nothing if the value is unchanged, the underlying
705 * functions can be expensive.
707 INT2CHAR(sp
, sep
, STRLEN(sep
) + 1, np
, nlen
);
708 if (!F_ISSET(op
, OPT_ALWAYS
) &&
709 O_STR(sp
, offset
) != NULL
&&
710 !strcmp(O_STR(sp
, offset
), np
))
713 /* Report to subsystems. */
714 if (op
->func
!= NULL
&&
715 op
->func(sp
, spo
, np
, NULL
) ||
716 ex_optchange(sp
, offset
, np
, NULL
) ||
717 v_optchange(sp
, offset
, np
, NULL
) ||
718 sp
->gp
->scr_optchange(sp
, offset
, np
, NULL
)) {
724 if (o_set(sp
, offset
, OS_STRDUP
, np
, 0))
731 if (disp
!= NO_DISPLAY
)
738 * Set an option's value.
740 * PUBLIC: int o_set __P((SCR *, int, u_int, char *, u_long));
743 o_set(SCR
*sp
, int opt
, u_int flags
, char *str
, u_long val
)
747 /* Set a pointer to the options area. */
748 op
= F_ISSET(&sp
->opts
[opt
], OPT_GLOBAL
) ?
749 &sp
->gp
->opts
[sp
->opts
[opt
].o_cur
.val
] : &sp
->opts
[opt
];
751 /* Copy the string, if requested. */
752 if (LF_ISSET(OS_STRDUP
) && (str
= strdup(str
)) == NULL
) {
753 msgq(sp
, M_SYSERR
, NULL
);
757 /* Free the previous string, if requested, and set the value. */
759 if (LF_ISSET(OS_STR
| OS_STRDUP
)) {
760 if (!LF_ISSET(OS_NOFREE
) && op
->o_def
.str
!= NULL
)
766 if (LF_ISSET(OS_STR
| OS_STRDUP
)) {
767 if (!LF_ISSET(OS_NOFREE
) && op
->o_cur
.str
!= NULL
)
777 * Return 1 if the string option is invalid, 0 if it's OK.
779 * PUBLIC: int opts_empty __P((SCR *, int, int));
782 opts_empty(SCR
*sp
, int off
, int silent
)
786 if ((p
= O_STR(sp
, off
)) == NULL
|| p
[0] == '\0') {
788 msgq_wstr(sp
, M_ERR
, optlist
[off
].name
,
789 "305|No %s edit option specified");
797 * List the current values of selected options.
799 * PUBLIC: void opts_dump __P((SCR *, enum optdisp));
802 opts_dump(SCR
*sp
, enum optdisp type
)
805 int base
, b_num
, cnt
, col
, colwidth
, curlen
, s_num
;
806 int numcols
, numrows
, row
;
807 int b_op
[O_OPTIONCOUNT
], s_op
[O_OPTIONCOUNT
];
812 * Options are output in two groups -- those that fit in a column and
813 * those that don't. Output is done on 6 character "tab" boundaries
814 * for no particular reason. (Since we don't output tab characters,
815 * we can ignore the terminal's tab settings.) Ignore the user's tab
816 * setting because we have no idea how reasonable it is.
818 * Find a column width we can live with, testing from 10 columns to 1.
820 for (numcols
= 10; numcols
> 1; --numcols
) {
821 colwidth
= sp
->cols
/ numcols
& ~(STANDARD_TAB
- 1);
822 if (colwidth
>= 10) {
824 (colwidth
+ STANDARD_TAB
) & ~(STANDARD_TAB
- 1);
825 numcols
= sp
->cols
/ colwidth
;
832 * Get the set of options to list, entering them into
833 * the column list or the overflow list.
835 for (b_num
= s_num
= 0, op
= optlist
; op
->name
!= NULL
; ++op
) {
838 /* If OPT_NDISP set, it's never displayed. */
839 if (F_ISSET(op
, OPT_NDISP
))
843 case ALL_DISPLAY
: /* Display all. */
845 case CHANGED_DISPLAY
: /* Display changed. */
846 /* If OPT_ADISP set, it's always "changed". */
847 if (F_ISSET(op
, OPT_ADISP
))
853 if (O_VAL(sp
, cnt
) == O_D_VAL(sp
, cnt
))
857 if (O_STR(sp
, cnt
) == O_D_STR(sp
, cnt
) ||
858 O_D_STR(sp
, cnt
) != NULL
&&
859 !strcmp(O_STR(sp
, cnt
), O_D_STR(sp
, cnt
)))
864 case SELECT_DISPLAY
: /* Display selected. */
865 if (!F_ISSET(&sp
->opts
[cnt
], OPT_SELECTED
))
872 F_CLR(&sp
->opts
[cnt
], OPT_SELECTED
);
874 curlen
= STRLEN(op
->name
);
878 if (!O_ISSET(sp
, cnt
))
883 sizeof(nbuf
), "%ld", O_VAL(sp
, cnt
));
884 curlen
+= strlen(nbuf
);
887 if (O_STR(sp
, cnt
) != NULL
)
888 curlen
+= strlen(O_STR(sp
, cnt
));
892 /* Offset by 2 so there's a gap. */
893 if (curlen
<= colwidth
- 2)
900 /* Figure out the number of rows. */
901 if (s_num
> numcols
) {
902 numrows
= s_num
/ numcols
;
908 /* Display the options in sorted order. */
909 for (row
= 0; row
< numrows
;) {
910 for (base
= row
, col
= 0; col
< numcols
; ++col
) {
911 cnt
= opts_print(sp
, &optlist
[s_op
[base
]]);
912 if ((base
+= numrows
) >= s_num
)
914 (void)ex_printf(sp
, "%*s",
915 (int)(colwidth
- cnt
), "");
917 if (++row
< numrows
|| b_num
)
918 (void)ex_puts(sp
, "\n");
922 for (row
= 0; row
< b_num
;) {
923 (void)opts_print(sp
, &optlist
[b_op
[row
]]);
925 (void)ex_puts(sp
, "\n");
927 (void)ex_puts(sp
, "\n");
932 * Print out an option.
935 opts_print(SCR
*sp
, const OPTLIST
*op
)
940 offset
= op
- optlist
;
944 curlen
+= ex_printf(sp
,
945 "%s"WS
, O_ISSET(sp
, offset
) ? "" : "no", op
->name
);
948 curlen
+= ex_printf(sp
, WS
"=%ld", op
->name
, O_VAL(sp
, offset
));
951 curlen
+= ex_printf(sp
, WS
"=\"%s\"", op
->name
,
952 O_STR(sp
, offset
) == NULL
? "" : O_STR(sp
, offset
));
960 * Write the current configuration to a file.
962 * PUBLIC: int opts_save __P((SCR *, FILE *));
965 opts_save(SCR
*sp
, FILE *fp
)
972 for (op
= optlist
; op
->name
!= NULL
; ++op
) {
973 if (F_ISSET(op
, OPT_NOSAVE
))
979 if (O_ISSET(sp
, cnt
))
980 (void)fprintf(fp
, "set "WS
"\n", op
->name
);
982 (void)fprintf(fp
, "set no"WS
"\n", op
->name
);
986 "set "WS
"=%-3ld\n", op
->name
, O_VAL(sp
, cnt
));
989 if (O_STR(sp
, cnt
) == NULL
)
991 (void)fprintf(fp
, "set ");
992 for (p
= op
->name
; (ch
= *p
) != L('\0'); ++p
) {
993 if (ISBLANK(ch
) || ch
== L('\\'))
994 (void)putc('\\', fp
);
998 for (np
= O_STR(sp
, cnt
); (nch
= *np
) != '\0'; ++np
) {
999 if (isblank(nch
) || nch
== '\\')
1000 (void)putc('\\', fp
);
1001 (void)putc(nch
, fp
);
1003 (void)putc('\n', fp
);
1007 msgq(sp
, M_SYSERR
, NULL
);
1016 * Search for an option.
1018 * PUBLIC: OPTLIST const *opts_search __P((CHAR_T *));
1021 opts_search(CHAR_T
*name
)
1023 OPTLIST
const *op
, *found
;
1028 /* Check list of abbreviations. */
1030 if ((ap
= bsearch(&atmp
, abbrev
, sizeof(abbrev
) / sizeof(OABBREV
) - 1,
1031 sizeof(OABBREV
), opts_abbcmp
)) != NULL
)
1032 return (optlist
+ ap
->offset
);
1034 /* Check list of options. */
1036 if ((op
= bsearch(&otmp
, optlist
, sizeof(optlist
) / sizeof(OPTLIST
) - 1,
1037 sizeof(OPTLIST
), opts_cmp
)) != NULL
)
1041 * Check to see if the name is the prefix of one (and only one)
1042 * option. If so, return the option.
1045 for (found
= NULL
, op
= optlist
; op
->name
!= NULL
; ++op
) {
1046 if (op
->name
[0] < name
[0])
1048 if (op
->name
[0] > name
[0])
1050 if (!memcmp(op
->name
, name
, len
)) {
1061 * Standard nomatch error message for options.
1063 * PUBLIC: void opts_nomatch __P((SCR *, CHAR_T *));
1066 opts_nomatch(SCR
*sp
, CHAR_T
*name
)
1068 msgq_wstr(sp
, M_ERR
, name
,
1069 "033|set: no %s option: 'set all' gives all option values");
1073 opts_abbcmp(const void *a
, const void *b
)
1075 return(STRCMP(((OABBREV
*)a
)->name
, ((OABBREV
*)b
)->name
));
1079 opts_cmp(const void *a
, const void *b
)
1081 return(STRCMP(((OPTLIST
*)a
)->name
, ((OPTLIST
*)b
)->name
));
1086 * Copy a screen's OPTION array.
1088 * PUBLIC: int opts_copy __P((SCR *, SCR *));
1091 opts_copy(SCR
*orig
, SCR
*sp
)
1095 /* Copy most everything without change. */
1096 memcpy(sp
->opts
, orig
->opts
, sizeof(orig
->opts
));
1098 /* Copy the string edit options. */
1099 for (cnt
= rval
= 0; cnt
< O_OPTIONCOUNT
; ++cnt
) {
1100 if (optlist
[cnt
].type
!= OPT_STR
||
1101 F_ISSET(&optlist
[cnt
], OPT_GLOBAL
))
1104 * If never set, or already failed, NULL out the entries --
1105 * have to continue after failure, otherwise would have two
1106 * screens referencing the same memory.
1108 if (rval
|| O_STR(sp
, cnt
) == NULL
) {
1109 o_set(sp
, cnt
, OS_NOFREE
| OS_STR
, NULL
, 0);
1110 o_set(sp
, cnt
, OS_DEF
| OS_NOFREE
| OS_STR
, NULL
, 0);
1114 /* Copy the current string. */
1115 if (o_set(sp
, cnt
, OS_NOFREE
| OS_STRDUP
, O_STR(sp
, cnt
), 0)) {
1116 o_set(sp
, cnt
, OS_DEF
| OS_NOFREE
| OS_STR
, NULL
, 0);
1120 /* Copy the default string. */
1121 if (O_D_STR(sp
, cnt
) != NULL
&& o_set(sp
, cnt
,
1122 OS_DEF
| OS_NOFREE
| OS_STRDUP
, O_D_STR(sp
, cnt
), 0)) {
1123 nomem
: msgq(orig
, M_SYSERR
, NULL
);
1132 * Free all option strings
1134 * PUBLIC: void opts_free __P((SCR *));
1141 for (cnt
= 0; cnt
< O_OPTIONCOUNT
; ++cnt
) {
1142 if (optlist
[cnt
].type
!= OPT_STR
||
1143 F_ISSET(&optlist
[cnt
], OPT_GLOBAL
))
1145 if (O_STR(sp
, cnt
) != NULL
)
1146 free(O_STR(sp
, cnt
));
1147 if (O_D_STR(sp
, cnt
) != NULL
)
1148 free(O_D_STR(sp
, cnt
));