1 /*-
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.
6 *
7 * See the LICENSE file for redistribution information.
8 */
9
10 #include "config.h"
11
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/stat.h>
15
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unctrl.h>
24 #include <unistd.h>
25
26 #include "common.h"
27 #include "../vi/vi.h"
28 #include "pathnames.h"
29
30 static int opts_abbcmp(const void *, const void *);
31 static int opts_cmp(const void *, const void *);
32 static int opts_print(SCR *, OPTLIST const *);
33
34 #ifdef USE_WIDECHAR
35 #define OPT_WC 0
36 #else
37 #define OPT_WC (OPT_NOSAVE | OPT_NDISP)
38 #endif
39
40 /*
41 * O'Reilly noted options and abbreviations are from "Learning the VI Editor",
42 * Fifth Edition, May 1992. There's no way of knowing what systems they are
43 * actually from.
44 *
45 * HPUX noted options and abbreviations are from "The Ultimate Guide to the
46 * VI and EX Text Editors", 1990.
47 */
48 OPTLIST const optlist[] = {
49 /* O_ALTNOTATION */
50 {L("altnotation"), f_print, OPT_0BOOL, 0},
51 /* O_ALTWERASE 4.4BSD */
52 {L("altwerase"), f_altwerase, OPT_0BOOL, 0},
53 /* O_AUTOINDENT 4BSD */
54 {L("autoindent"), NULL, OPT_0BOOL, 0},
55 /* O_AUTOPRINT 4BSD */
56 {L("autoprint"), NULL, OPT_1BOOL, 0},
57 /* O_AUTOWRITE 4BSD */
58 {L("autowrite"), NULL, OPT_0BOOL, 0},
59 /* O_BACKUP 4.4BSD */
60 {L("backup"), NULL, OPT_STR, 0},
61 /* O_BEAUTIFY 4BSD */
62 {L("beautify"), NULL, OPT_0BOOL, 0},
63 /* O_CDPATH 4.4BSD */
64 {L("cdpath"), NULL, OPT_STR, 0},
65 /* O_CEDIT 4.4BSD */
66 {L("cedit"), NULL, OPT_STR, 0},
67 /* O_COLUMNS 4.4BSD */
68 {L("columns"), f_columns, OPT_NUM, OPT_NOSAVE},
69 /* O_COMBINED */
70 {L("combined"), NULL, OPT_0BOOL, OPT_NOSET|OPT_WC},
71 /* O_COMMENT 4.4BSD */
72 {L("comment"), NULL, OPT_0BOOL, 0},
73 /* O_TMPDIR 4BSD */
74 {L("directory"), NULL, OPT_STR, 0},
75 /* O_EDCOMPATIBLE 4BSD */
76 {L("edcompatible"),NULL, OPT_0BOOL, 0},
77 /* O_ERRORBELLS 4BSD */
78 {L("errorbells"), NULL, OPT_0BOOL, 0},
79 /* O_ESCAPETIME 4.4BSD */
80 {L("escapetime"), NULL, OPT_NUM, 0},
81 /* O_EXPANDTAB NetBSD 5.0 */
82 {L("expandtab"), 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},
87 /* O_FILEC 4.4BSD */
88 {L("filec"), NULL, OPT_STR, 0},
89 /* O_FILEENCODING */
90 {L("fileencoding"),f_encoding, OPT_STR, OPT_WC},
91 /* O_FLASH HPUX */
92 {L("flash"), NULL, OPT_1BOOL, 0},
93 /* O_HARDTABS 4BSD */
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},
99 /* O_INPUTENCODING */
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},
105 /* O_LINES 4.4BSD */
106 {L("lines"), f_lines, OPT_NUM, OPT_NOSAVE},
107 /* O_LISP 4BSD
108 * XXX
109 * When the lisp option is implemented, delete the OPT_NOSAVE flag,
110 * so that :mkexrc dumps it.
111 */
112 {L("lisp"), f_lisp, OPT_0BOOL, OPT_NOSAVE},
113 /* O_LIST 4BSD */
114 {L("list"), f_reformat, OPT_0BOOL, 0},
115 /* O_LOCKFILES 4.4BSD
116 * XXX
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.
119 */
120 {L("lock"), NULL, OPT_1BOOL, 0},
121 /* O_MAGIC 4BSD */
122 {L("magic"), NULL, OPT_1BOOL, 0},
123 /* O_MATCHCHARS NetBSD 2.0 */
124 {L("matchchars"), NULL, OPT_STR, OPT_PAIRS},
125 /* O_MATCHTIME 4.4BSD */
126 {L("matchtime"), NULL, OPT_NUM, 0},
127 /* O_MESG 4BSD */
128 {L("mesg"), NULL, OPT_1BOOL, 0},
129 /* O_MODELINE 4BSD
130 * !!!
131 * This has been documented in historical systems as both "modeline"
132 * and as "modelines". Regardless of the name, this option represents
133 * a security problem of mammoth proportions, not to mention a stunning
134 * example of what your intro CS professor referred to as the perils of
135 * mixing code and data. Don't add it, or I will kill you.
136 */
137 {L("modeline"), NULL, OPT_0BOOL, OPT_NOSET},
138 /* O_MSGCAT 4.4BSD */
139 {L("msgcat"), f_msgcat, OPT_STR, 0},
140 /* O_NOPRINT 4.4BSD */
141 {L("noprint"), f_print, OPT_STR, 0},
142 /* O_NUMBER 4BSD */
143 {L("number"), f_reformat, OPT_0BOOL, 0},
144 /* O_OCTAL 4.4BSD */
145 {L("octal"), f_print, OPT_0BOOL, 0},
146 /* O_OPEN 4BSD */
147 {L("open"), NULL, OPT_1BOOL, 0},
148 /* O_OPTIMIZE 4BSD */
149 {L("optimize"), NULL, OPT_1BOOL, 0},
150 /* O_PARAGRAPHS 4BSD */
151 {L("paragraphs"), NULL, OPT_STR, OPT_PAIRS},
152 /* O_PATH 4.4BSD */
153 {L("path"), NULL, OPT_STR, 0},
154 /* O_PRINT 4.4BSD */
155 {L("print"), f_print, OPT_STR, 0},
156 /* O_PROMPT 4BSD */
157 {L("prompt"), NULL, OPT_1BOOL, 0},
158 /* O_READONLY 4BSD (undocumented) */
159 {L("readonly"), f_readonly, OPT_0BOOL, OPT_ALWAYS},
160 /* O_RECDIR 4.4BSD */
161 {L("recdir"), NULL, OPT_STR, 0},
162 /* O_REDRAW 4BSD */
163 {L("redraw"), NULL, OPT_0BOOL, 0},
164 /* O_REMAP 4BSD */
165 {L("remap"), NULL, OPT_1BOOL, 0},
166 /* O_REPORT 4BSD */
167 {L("report"), NULL, OPT_NUM, 0},
168 /* O_RULER 4.4BSD */
169 {L("ruler"), NULL, OPT_0BOOL, 0},
170 /* O_SCROLL 4BSD */
171 {L("scroll"), NULL, OPT_NUM, 0},
172 /* O_SEARCHINCR 4.4BSD */
173 {L("searchincr"), NULL, OPT_0BOOL, 0},
174 /* O_SECTIONS 4BSD */
175 {L("sections"), NULL, OPT_STR, OPT_PAIRS},
176 /* O_SECURE 4.4BSD */
177 {L("secure"), NULL, OPT_0BOOL, OPT_NOUNSET},
178 /* O_SHELL 4BSD */
179 {L("shell"), NULL, OPT_STR, 0},
180 /* O_SHELLMETA 4.4BSD */
181 {L("shellmeta"), NULL, OPT_STR, 0},
182 /* O_SHIFTWIDTH 4BSD */
183 {L("shiftwidth"), NULL, OPT_NUM, OPT_NOZERO},
184 /* O_SHOWFILENAME */
185 {L("showfilename"), NULL, OPT_0BOOL, 0},
186 /* O_SHOWMATCH 4BSD */
187 {L("showmatch"), NULL, OPT_0BOOL, 0},
188 /* O_SHOWMODE 4.4BSD */
189 {L("showmode"), NULL, OPT_0BOOL, 0},
190 /* O_SIDESCROLL 4.4BSD */
191 {L("sidescroll"), NULL, OPT_NUM, OPT_NOZERO},
192 /* O_SLOWOPEN 4BSD */
193 {L("slowopen"), NULL, OPT_0BOOL, 0},
194 /* O_SOURCEANY 4BSD (undocumented)
195 * !!!
196 * Historic vi, on startup, source'd $HOME/.exrc and ./.exrc, if they
197 * were owned by the user. The sourceany option was an undocumented
198 * feature of historic vi which permitted the startup source'ing of
199 * .exrc files the user didn't own. This is an obvious security problem,
200 * and we ignore the option.
201 */
202 {L("sourceany"), NULL, OPT_0BOOL, OPT_NOSET},
203 /* O_TABSTOP 4BSD */
204 {L("tabstop"), f_reformat, OPT_NUM, OPT_NOZERO},
205 /* O_TAGLENGTH 4BSD */
206 {L("taglength"), NULL, OPT_NUM, 0},
207 /* O_TAGS 4BSD */
208 {L("tags"), NULL, OPT_STR, 0},
209 /* O_TERM 4BSD
210 * !!!
211 * By default, the historic vi always displayed information about two
212 * options, redraw and term. Term seems sufficient.
213 */
214 {L("term"), NULL, OPT_STR, OPT_ADISP|OPT_NOSAVE},
215 /* O_TERSE 4BSD */
216 {L("terse"), NULL, OPT_0BOOL, 0},
217 /* O_TILDEOP 4.4BSD */
218 {L("tildeop"), NULL, OPT_0BOOL, 0},
219 /* O_TIMEOUT 4BSD (undocumented) */
220 {L("timeout"), NULL, OPT_1BOOL, 0},
221 /* O_TTYWERASE 4.4BSD */
222 {L("ttywerase"), f_ttywerase, OPT_0BOOL, 0},
223 /* O_VERBOSE 4.4BSD */
224 {L("verbose"), NULL, OPT_0BOOL, 0},
225 /* O_W1200 4BSD */
226 {L("w1200"), f_w1200, OPT_NUM, OPT_NDISP|OPT_NOSAVE},
227 /* O_W300 4BSD */
228 {L("w300"), f_w300, OPT_NUM, OPT_NDISP|OPT_NOSAVE},
229 /* O_W9600 4BSD */
230 {L("w9600"), f_w9600, OPT_NUM, OPT_NDISP|OPT_NOSAVE},
231 /* O_WARN 4BSD */
232 {L("warn"), NULL, OPT_1BOOL, 0},
233 /* O_WINDOW 4BSD */
234 {L("window"), f_window, OPT_NUM, 0},
235 /* O_WINDOWNAME 4BSD */
236 {L("windowname"), NULL, OPT_0BOOL, 0},
237 /* O_WRAPLEN 4.4BSD */
238 {L("wraplen"), NULL, OPT_NUM, 0},
239 /* O_WRAPMARGIN 4BSD */
240 {L("wrapmargin"), NULL, OPT_NUM, 0},
241 /* O_WRAPSCAN 4BSD */
242 {L("wrapscan"), NULL, OPT_1BOOL, 0},
243 /* O_WRITEANY 4BSD */
244 {L("writeany"), NULL, OPT_0BOOL, 0},
245 {NULL},
246 };
247
248 typedef struct abbrev {
249 CHAR_T *name;
250 int offset;
251 } OABBREV;
252
253 static OABBREV const abbrev[] = {
254 {L("ai"), O_AUTOINDENT}, /* 4BSD */
255 {L("ap"), O_AUTOPRINT}, /* 4BSD */
256 {L("aw"), O_AUTOWRITE}, /* 4BSD */
257 {L("bf"), O_BEAUTIFY}, /* 4BSD */
258 {L("co"), O_COLUMNS}, /* 4.4BSD */
259 {L("dir"), O_TMPDIR}, /* 4BSD */
260 {L("eb"), O_ERRORBELLS}, /* 4BSD */
261 {L("ed"), O_EDCOMPATIBLE}, /* 4BSD */
262 {L("et"), O_EXPANDTAB}, /* NetBSD 5.0 */
263 {L("ex"), O_EXRC}, /* System V (undocumented) */
264 {L("fe"), O_FILEENCODING},
265 {L("ht"), O_HARDTABS}, /* 4BSD */
266 {L("ic"), O_IGNORECASE}, /* 4BSD */
267 {L("ie"), O_INPUTENCODING},
268 {L("li"), O_LINES}, /* 4.4BSD */
269 {L("modelines"), O_MODELINE}, /* HPUX */
270 {L("nu"), O_NUMBER}, /* 4BSD */
271 {L("opt"), O_OPTIMIZE}, /* 4BSD */
272 {L("para"), O_PARAGRAPHS}, /* 4BSD */
273 {L("re"), O_REDRAW}, /* O'Reilly */
274 {L("ro"), O_READONLY}, /* 4BSD (undocumented) */
275 {L("scr"), O_SCROLL}, /* 4BSD (undocumented) */
276 {L("sect"), O_SECTIONS}, /* O'Reilly */
277 {L("sh"), O_SHELL}, /* 4BSD */
278 {L("slow"), O_SLOWOPEN}, /* 4BSD */
279 {L("sm"), O_SHOWMATCH}, /* 4BSD */
280 {L("smd"), O_SHOWMODE}, /* 4BSD */
281 {L("sw"), O_SHIFTWIDTH}, /* 4BSD */
282 {L("tag"), O_TAGS}, /* 4BSD (undocumented) */
283 {L("tl"), O_TAGLENGTH}, /* 4BSD */
284 {L("to"), O_TIMEOUT}, /* 4BSD (undocumented) */
285 {L("ts"), O_TABSTOP}, /* 4BSD */
286 {L("tty"), O_TERM}, /* 4BSD (undocumented) */
287 {L("ttytype"), O_TERM}, /* 4BSD (undocumented) */
288 {L("w"), O_WINDOW}, /* O'Reilly */
289 {L("wa"), O_WRITEANY}, /* 4BSD */
290 {L("wi"), O_WINDOW}, /* 4BSD (undocumented) */
291 {L("wl"), O_WRAPLEN}, /* 4.4BSD */
292 {L("wm"), O_WRAPMARGIN}, /* 4BSD */
293 {L("ws"), O_WRAPSCAN}, /* 4BSD */
294 {NULL},
295 };
296
297 /*
298 * opts_init --
299 * Initialize some of the options.
300 *
301 * PUBLIC: int opts_init(SCR *, int *);
302 */
303 int
opts_init(SCR * sp,int * oargs)304 opts_init(SCR *sp, int *oargs)
305 {
306 ARGS *argv[2], a, b;
307 OPTLIST const *op;
308 u_long v;
309 int cnt, optindx = 0;
310 char *s;
311 CHAR_T b2[1024];
312
313 a.bp = b2;
314 b.bp = NULL;
315 a.len = b.len = 0;
316 argv[0] = &a;
317 argv[1] = &b;
318
319 /* Set numeric and string default values. */
320 #define OI(indx, ...) do { \
321 size_t len = SPRINTF(b2, SIZE(b2), __VA_ARGS__); \
322 if (len < 0 || len >= SIZE(b2) || opts_set(sp, argv, NULL)) { \
323 optindx = indx; \
324 goto err; \
325 } \
326 a.len = len; \
327 } while (0)
328 /*
329 * Indirect global options to global space. Specifically, set up
330 * terminal, lines, columns first, they're used by other options.
331 * Note, don't set the flags until we've set up the indirection.
332 */
333 if (o_set(sp, O_TERM, 0, NULL, GO_TERM))
334 goto err;
335 F_SET(&sp->opts[O_TERM], OPT_GLOBAL);
336 if (o_set(sp, O_LINES, 0, NULL, GO_LINES))
337 goto err;
338 F_SET(&sp->opts[O_LINES], OPT_GLOBAL);
339 if (o_set(sp, O_COLUMNS, 0, NULL, GO_COLUMNS))
340 goto err;
341 F_SET(&sp->opts[O_COLUMNS], OPT_GLOBAL);
342 if (o_set(sp, O_SECURE, 0, NULL, GO_SECURE))
343 goto err;
344 F_SET(&sp->opts[O_SECURE], OPT_GLOBAL);
345
346 /* Initialize string values. */
347 OI(O_CDPATH, L("cdpath=%s"), (s = getenv("CDPATH")) == NULL ? ":" : s);
348 OI(O_CEDIT, L("cedit=\033"));
349
350 /*
351 * !!!
352 * Vi historically stored temporary files in /var/tmp. We store them
353 * in /tmp by default, hoping it's a memory based file system. There
354 * are two ways to change this -- the user can set either the directory
355 * option or the TMPDIR environmental variable.
356 */
357 OI(O_TMPDIR,
358 L("directory=%s"), (s = getenv("TMPDIR")) == NULL ? _PATH_TMP : s);
359 OI(O_ESCAPETIME, L("escapetime=6"));
360 OI(O_FILEC, L("filec=\t"));
361 OI(O_KEYTIME, L("keytime=6"));
362 OI(O_MATCHCHARS, L("matchchars=()[]{}"));
363 OI(O_MATCHTIME, L("matchtime=7"));
364 OI(O_MSGCAT, L("msgcat=%s"), _PATH_MSGCAT);
365 OI(O_REPORT, L("report=5"));
366 OI(O_PARAGRAPHS, L("paragraphs=IPLPPPQPP LIpplpipbp"));
367 OI(O_PATH, L("path=%s"), "");
368 OI(O_RECDIR, L("recdir=%s"), NVI_PATH_PRESERVE);
369 OI(O_SECTIONS, L("sections=NHSHH HUnhsh"));
370 OI(O_SHELL,
371 L("shell=%s"), (s = getenv("SHELL")) == NULL ? _PATH_BSHELL : s);
372 OI(O_SHELLMETA, L("shellmeta=~{[*?$`'\"\\"));
373 OI(O_SHIFTWIDTH, L("shiftwidth=8"));
374 OI(O_SIDESCROLL, L("sidescroll=16"));
375 OI(O_TABSTOP, L("tabstop=8"));
376 OI(O_TAGS, L("tags=%s"), _PATH_TAGS);
377
378 /*
379 * XXX
380 * Initialize O_SCROLL here, after term; initializing term should
381 * have created a LINES/COLUMNS value.
382 */
383 if ((v = (O_VAL(sp, O_LINES) - 1) / 2) == 0)
384 v = 1;
385 OI(O_SCROLL, L("scroll=%ld"), v);
386
387 /*
388 * The default window option values are:
389 * 8 if baud rate <= 600
390 * 16 if baud rate <= 1200
391 * LINES - 1 if baud rate > 1200
392 *
393 * Note, the windows option code will correct any too-large value
394 * or when the O_LINES value is 1.
395 */
396 if (sp->gp->scr_baud(sp, &v))
397 return (1);
398 if (v <= 600)
399 v = 8;
400 else if (v <= 1200)
401 v = 16;
402 else if ((v = O_VAL(sp, O_LINES) - 1) == 0)
403 v = 1;
404
405 OI(O_WINDOW, L("window=%lu"), v);
406
407 /*
408 * Set boolean default values, and copy all settings into the default
409 * information. OS_NOFREE is set, we're copying, not replacing.
410 */
411 for (op = optlist, cnt = 0; op->name != NULL; ++op, ++cnt) {
412 if (F_ISSET(op, OPT_GLOBAL))
413 continue;
414 switch (op->type) {
415 case OPT_0BOOL:
416 break;
417 case OPT_1BOOL:
418 O_SET(sp, cnt);
419 O_D_SET(sp, cnt);
420 break;
421 case OPT_NUM:
422 o_set(sp, cnt, OS_DEF, NULL, O_VAL(sp, cnt));
423 break;
424 case OPT_STR:
425 if (O_STR(sp, cnt) != NULL && o_set(sp, cnt,
426 OS_DEF | OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0))
427 goto err;
428 break;
429 default:
430 abort();
431 }
432 }
433
434 /*
435 * !!!
436 * Some options can be initialized by the command name or the
437 * command-line arguments. They don't set the default values,
438 * it's historic practice.
439 */
440 for (; *oargs != -1; ++oargs)
441 OI(*oargs, optlist[*oargs].name);
442 #undef OI
443 return (0);
444
445 err: msgq_wstr(sp, M_ERR, optlist[optindx].name,
446 "031|Unable to set default %s option");
447 return (1);
448 }
449
450 /*
451 * opts_set --
452 * Change the values of one or more options.
453 *
454 * PUBLIC: int opts_set(SCR *, ARGS *[], char *);
455 */
456 int
opts_set(SCR * sp,ARGS * argv[],char * usage)457 opts_set(SCR *sp, ARGS *argv[], char *usage)
458 {
459 enum optdisp disp;
460 enum nresult nret;
461 OPTLIST const *op;
462 OPTION *spo;
463 u_long isset, turnoff, value;
464 int ch, equals, nf, nf2, offset, qmark, rval;
465 CHAR_T *endp, *name, *p, *sep;
466 char *p2, *t2;
467 char *np;
468 size_t nlen;
469
470 disp = NO_DISPLAY;
471 for (rval = 0; argv[0]->len != 0; ++argv) {
472 /*
473 * The historic vi dumped the options for each occurrence of
474 * "all" in the set list. Puhleeze.
475 */
476 if (!STRCMP(argv[0]->bp, L("all"))) {
477 disp = ALL_DISPLAY;
478 continue;
479 }
480
481 /* Find equals sign or question mark. */
482 for (sep = NULL, equals = qmark = 0,
483 p = name = argv[0]->bp; (ch = *p) != '\0'; ++p)
484 if (ch == '=' || ch == '?') {
485 if (p == name) {
486 if (usage != NULL)
487 msgq(sp, M_ERR,
488 "032|Usage: %s", usage);
489 return (1);
490 }
491 sep = p;
492 if (ch == '=')
493 equals = 1;
494 else
495 qmark = 1;
496 break;
497 }
498
499 turnoff = 0;
500 op = NULL;
501 if (sep != NULL)
502 *sep++ = '\0';
503
504 /* Search for the name, then name without any leading "no". */
505 if ((op = opts_search(name)) == NULL &&
506 name[0] == 'n' && name[1] == 'o') {
507 turnoff = 1;
508 name += 2;
509 op = opts_search(name);
510 }
511 if (op == NULL) {
512 opts_nomatch(sp, name);
513 rval = 1;
514 continue;
515 }
516
517 /* Find current option values. */
518 offset = op - optlist;
519 spo = sp->opts + offset;
520
521 /*
522 * !!!
523 * Historically, the question mark could be a separate
524 * argument.
525 */
526 if (!equals && !qmark &&
527 argv[1]->len == 1 && argv[1]->bp[0] == '?') {
528 ++argv;
529 qmark = 1;
530 }
531
532 /* Set name, value. */
533 switch (op->type) {
534 case OPT_0BOOL:
535 case OPT_1BOOL:
536 /* Some options may not be reset. */
537 if (F_ISSET(op, OPT_NOUNSET) && turnoff) {
538 msgq_wstr(sp, M_ERR, name,
539 "291|set: the %s option may not be turned off");
540 rval = 1;
541 break;
542 }
543
544 /* Some options may not be set. */
545 if (F_ISSET(op, OPT_NOSET) && !turnoff) {
546 msgq_wstr(sp, M_ERR, name,
547 "313|set: the %s option may never be turned on");
548 rval = 1;
549 break;
550 }
551
552 if (equals) {
553 msgq_wstr(sp, M_ERR, name,
554 "034|set: [no]%s option doesn't take a value");
555 rval = 1;
556 break;
557 }
558 if (qmark) {
559 if (!disp)
560 disp = SELECT_DISPLAY;
561 F_SET(spo, OPT_SELECTED);
562 break;
563 }
564
565 /*
566 * Do nothing if the value is unchanged, the underlying
567 * functions can be expensive.
568 */
569 isset = !turnoff;
570 if (!F_ISSET(op, OPT_ALWAYS)) {
571 if (isset) {
572 if (O_ISSET(sp, offset))
573 break;
574 } else
575 if (!O_ISSET(sp, offset))
576 break;
577 }
578
579 /* Report to subsystems. */
580 if ((op->func != NULL &&
581 op->func(sp, spo, NULL, &isset)) ||
582 ex_optchange(sp, offset, NULL, &isset) ||
583 v_optchange(sp, offset, NULL, &isset) ||
584 sp->gp->scr_optchange(sp, offset, NULL, &isset)) {
585 rval = 1;
586 break;
587 }
588
589 /* Set the value. */
590 if (isset)
591 O_SET(sp, offset);
592 else
593 O_CLR(sp, offset);
594 break;
595 case OPT_NUM:
596 if (turnoff) {
597 msgq_wstr(sp, M_ERR, name,
598 "035|set: %s option isn't a boolean");
599 rval = 1;
600 break;
601 }
602 if (qmark || !equals) {
603 if (!disp)
604 disp = SELECT_DISPLAY;
605 F_SET(spo, OPT_SELECTED);
606 break;
607 }
608
609 if (!ISDIGIT(sep[0]))
610 goto badnum;
611 if ((nret =
612 nget_uslong(&value, sep, &endp, 10)) != NUM_OK) {
613 INT2CHAR(sp, name, STRLEN(name) + 1,
614 np, nlen);
615 p2 = msg_print(sp, np, &nf);
616 INT2CHAR(sp, sep, STRLEN(sep) + 1,
617 np, nlen);
618 t2 = msg_print(sp, np, &nf2);
619 switch (nret) {
620 case NUM_ERR:
621 msgq(sp, M_SYSERR,
622 "036|set: %s option: %s", p2, t2);
623 break;
624 case NUM_OVER:
625 msgq(sp, M_ERR,
626 "037|set: %s option: %s: value overflow", p2, t2);
627 break;
628 case NUM_OK:
629 case NUM_UNDER:
630 abort();
631 }
632 if (nf)
633 FREE_SPACE(sp, p2, 0);
634 if (nf2)
635 FREE_SPACE(sp, t2, 0);
636 rval = 1;
637 break;
638 }
639 if (*endp && !cmdskip(*endp)) {
640 badnum: INT2CHAR(sp, name, STRLEN(name) + 1,
641 np, nlen);
642 p2 = msg_print(sp, np, &nf);
643 INT2CHAR(sp, sep, STRLEN(sep) + 1,
644 np, nlen);
645 t2 = msg_print(sp, np, &nf2);
646 msgq(sp, M_ERR,
647 "038|set: %s option: %s is an illegal number", p2, t2);
648 if (nf)
649 FREE_SPACE(sp, p2, 0);
650 if (nf2)
651 FREE_SPACE(sp, t2, 0);
652 rval = 1;
653 break;
654 }
655
656 /* Some options may never be set to zero. */
657 if (F_ISSET(op, OPT_NOZERO) && value == 0) {
658 msgq_wstr(sp, M_ERR, name,
659 "314|set: the %s option may never be set to 0");
660 rval = 1;
661 break;
662 }
663
664 /*
665 * Do nothing if the value is unchanged, the underlying
666 * functions can be expensive.
667 */
668 if (!F_ISSET(op, OPT_ALWAYS) &&
669 O_VAL(sp, offset) == value)
670 break;
671
672 /* Report to subsystems. */
673 INT2CHAR(sp, sep, STRLEN(sep) + 1, np, nlen);
674 if ((op->func != NULL &&
675 op->func(sp, spo, np, &value)) ||
676 ex_optchange(sp, offset, np, &value) ||
677 v_optchange(sp, offset, np, &value) ||
678 sp->gp->scr_optchange(sp, offset, np, &value)) {
679 rval = 1;
680 break;
681 }
682
683 /* Set the value. */
684 if (o_set(sp, offset, 0, NULL, value))
685 rval = 1;
686 break;
687 case OPT_STR:
688 if (turnoff) {
689 msgq_wstr(sp, M_ERR, name,
690 "039|set: %s option isn't a boolean");
691 rval = 1;
692 break;
693 }
694 if (qmark || !equals) {
695 if (!disp)
696 disp = SELECT_DISPLAY;
697 F_SET(spo, OPT_SELECTED);
698 break;
699 }
700
701 /* Check for strings that must have even length. */
702 if (F_ISSET(op, OPT_PAIRS) && STRLEN(sep) & 1) {
703 msgq_wstr(sp, M_ERR, name,
704 "047|The %s option must be in two character groups");
705 rval = 1;
706 break;
707 }
708
709 /*
710 * Do nothing if the value is unchanged, the underlying
711 * functions can be expensive.
712 */
713 INT2CHAR(sp, sep, STRLEN(sep) + 1, np, nlen);
714 if (!F_ISSET(op, OPT_ALWAYS) &&
715 O_STR(sp, offset) != NULL &&
716 !strcmp(O_STR(sp, offset), np))
717 break;
718
719 /* Report to subsystems. */
720 if ((op->func != NULL &&
721 op->func(sp, spo, np, NULL)) ||
722 ex_optchange(sp, offset, np, NULL) ||
723 v_optchange(sp, offset, np, NULL) ||
724 sp->gp->scr_optchange(sp, offset, np, NULL)) {
725 rval = 1;
726 break;
727 }
728
729 /* Set the value. */
730 if (o_set(sp, offset, OS_STRDUP, np, 0))
731 rval = 1;
732 break;
733 default:
734 abort();
735 }
736 }
737 if (disp != NO_DISPLAY)
738 opts_dump(sp, disp);
739 return (rval);
740 }
741
742 /*
743 * o_set --
744 * Set an option's value.
745 *
746 * PUBLIC: int o_set(SCR *, int, u_int, char *, u_long);
747 */
748 int
o_set(SCR * sp,int opt,u_int flags,char * str,u_long val)749 o_set(SCR *sp, int opt, u_int flags, char *str, u_long val)
750 {
751 OPTION *op;
752
753 /* Set a pointer to the options area. */
754 op = F_ISSET(&sp->opts[opt], OPT_GLOBAL) ?
755 &sp->gp->opts[sp->opts[opt].o_cur.val] : &sp->opts[opt];
756
757 /* Copy the string, if requested. */
758 if (LF_ISSET(OS_STRDUP) && (str = strdup(str)) == NULL) {
759 msgq(sp, M_SYSERR, NULL);
760 return (1);
761 }
762
763 /* Free the previous string, if requested, and set the value. */
764 if LF_ISSET(OS_DEF)
765 if (LF_ISSET(OS_STR | OS_STRDUP)) {
766 if (!LF_ISSET(OS_NOFREE))
767 free(op->o_def.str);
768 op->o_def.str = str;
769 } else
770 op->o_def.val = val;
771 else
772 if (LF_ISSET(OS_STR | OS_STRDUP)) {
773 if (!LF_ISSET(OS_NOFREE))
774 free(op->o_cur.str);
775 op->o_cur.str = str;
776 } else
777 op->o_cur.val = val;
778 return (0);
779 }
780
781 /*
782 * opts_empty --
783 * Return 1 if the string option is invalid, 0 if it's OK.
784 *
785 * PUBLIC: int opts_empty(SCR *, int, int);
786 */
787 int
opts_empty(SCR * sp,int off,int silent)788 opts_empty(SCR *sp, int off, int silent)
789 {
790 char *p;
791
792 if ((p = O_STR(sp, off)) == NULL || p[0] == '\0') {
793 if (!silent)
794 msgq_wstr(sp, M_ERR, optlist[off].name,
795 "305|No %s edit option specified");
796 return (1);
797 }
798 return (0);
799 }
800
801 /*
802 * opts_dump --
803 * List the current values of selected options.
804 *
805 * PUBLIC: void opts_dump(SCR *, enum optdisp);
806 */
807 void
opts_dump(SCR * sp,enum optdisp type)808 opts_dump(SCR *sp, enum optdisp type)
809 {
810 OPTLIST const *op;
811 int base, b_num, cnt, col, colwidth, curlen, s_num;
812 int numcols, numrows, row;
813 int b_op[O_OPTIONCOUNT], s_op[O_OPTIONCOUNT];
814 char nbuf[20];
815
816 /*
817 * Options are output in two groups -- those that fit in a column and
818 * those that don't. Output is done on 6 character "tab" boundaries
819 * for no particular reason. (Since we don't output tab characters,
820 * we can ignore the terminal's tab settings.) Ignore the user's tab
821 * setting because we have no idea how reasonable it is.
822 *
823 * Find a column width we can live with, testing from 10 columns to 1.
824 */
825 for (numcols = 10; numcols > 1; --numcols) {
826 colwidth = sp->cols / numcols & ~(STANDARD_TAB - 1);
827 if (colwidth >= 10) {
828 colwidth =
829 (colwidth + STANDARD_TAB) & ~(STANDARD_TAB - 1);
830 numcols = sp->cols / colwidth;
831 break;
832 }
833 colwidth = 0;
834 }
835
836 /*
837 * Get the set of options to list, entering them into
838 * the column list or the overflow list.
839 */
840 for (b_num = s_num = 0, op = optlist; op->name != NULL; ++op) {
841 cnt = op - optlist;
842
843 /* If OPT_NDISP set, it's never displayed. */
844 if (F_ISSET(op, OPT_NDISP))
845 continue;
846
847 switch (type) {
848 case ALL_DISPLAY: /* Display all. */
849 break;
850 case CHANGED_DISPLAY: /* Display changed. */
851 /* If OPT_ADISP set, it's always "changed". */
852 if (F_ISSET(op, OPT_ADISP))
853 break;
854 switch (op->type) {
855 case OPT_0BOOL:
856 case OPT_1BOOL:
857 case OPT_NUM:
858 if (O_VAL(sp, cnt) == O_D_VAL(sp, cnt))
859 continue;
860 break;
861 case OPT_STR:
862 if (O_STR(sp, cnt) == O_D_STR(sp, cnt) ||
863 (O_D_STR(sp, cnt) != NULL &&
864 !strcmp(O_STR(sp, cnt), O_D_STR(sp, cnt))))
865 continue;
866 break;
867 }
868 break;
869 case SELECT_DISPLAY: /* Display selected. */
870 if (!F_ISSET(&sp->opts[cnt], OPT_SELECTED))
871 continue;
872 break;
873 default:
874 case NO_DISPLAY:
875 abort();
876 }
877 F_CLR(&sp->opts[cnt], OPT_SELECTED);
878
879 curlen = STRLEN(op->name);
880 switch (op->type) {
881 case OPT_0BOOL:
882 case OPT_1BOOL:
883 if (!O_ISSET(sp, cnt))
884 curlen += 2;
885 break;
886 case OPT_NUM:
887 (void)snprintf(nbuf,
888 sizeof(nbuf), "%ld", O_VAL(sp, cnt));
889 curlen += strlen(nbuf);
890 break;
891 case OPT_STR:
892 if (O_STR(sp, cnt) != NULL)
893 curlen += strlen(O_STR(sp, cnt));
894 curlen += 3;
895 break;
896 }
897 /* Offset by 2 so there's a gap. */
898 if (curlen <= colwidth - 2)
899 s_op[s_num++] = cnt;
900 else
901 b_op[b_num++] = cnt;
902 }
903
904 if (s_num > 0) {
905 /* Figure out the number of rows. */
906 if (s_num > numcols) {
907 numrows = s_num / numcols;
908 if (s_num % numcols)
909 ++numrows;
910 } else
911 numrows = 1;
912
913 /* Display the options in sorted order. */
914 for (row = 0; row < numrows;) {
915 for (base = row, col = 0; col < numcols; ++col) {
916 cnt = opts_print(sp, &optlist[s_op[base]]);
917 if ((base += numrows) >= s_num)
918 break;
919 (void)ex_printf(sp, "%*s",
920 (int)(colwidth - cnt), "");
921 }
922 if (++row < numrows || b_num)
923 (void)ex_puts(sp, "\n");
924 }
925 }
926
927 for (row = 0; row < b_num;) {
928 (void)opts_print(sp, &optlist[b_op[row]]);
929 if (++row < b_num)
930 (void)ex_puts(sp, "\n");
931 }
932 (void)ex_puts(sp, "\n");
933 }
934
935 /*
936 * opts_print --
937 * Print out an option.
938 */
939 static int
opts_print(SCR * sp,OPTLIST const * op)940 opts_print(SCR *sp, OPTLIST const *op)
941 {
942 int curlen, offset;
943 const char *p;
944
945 curlen = 0;
946 offset = op - optlist;
947 switch (op->type) {
948 case OPT_0BOOL:
949 case OPT_1BOOL:
950 curlen += ex_printf(sp,
951 "%s"WS, O_ISSET(sp, offset) ? "" : "no", op->name);
952 break;
953 case OPT_NUM:
954 curlen += ex_printf(sp, WS"=%ld", op->name, O_VAL(sp, offset));
955 break;
956 case OPT_STR:
957 curlen += ex_printf(sp, WS"=\"", op->name);
958 p = O_STR(sp, offset);
959 /* Keep correct count for unprintable character sequences */
960 if (p != NULL)
961 for (; *p != '\0'; ++p)
962 curlen += ex_puts(sp, unctrl(*p));
963 curlen += ex_puts(sp, "\"");
964 break;
965 }
966 return (curlen);
967 }
968
969 /*
970 * opts_save --
971 * Write the current configuration to a file.
972 *
973 * PUBLIC: int opts_save(SCR *, FILE *);
974 */
975 int
opts_save(SCR * sp,FILE * fp)976 opts_save(SCR *sp, FILE *fp)
977 {
978 OPTLIST const *op;
979 CHAR_T ch, *p;
980 char nch, *np;
981 int cnt;
982
983 for (op = optlist; op->name != NULL; ++op) {
984 if (F_ISSET(op, OPT_NOSAVE))
985 continue;
986 cnt = op - optlist;
987 switch (op->type) {
988 case OPT_0BOOL:
989 case OPT_1BOOL:
990 if (O_ISSET(sp, cnt))
991 (void)fprintf(fp, "set "WS"\n", op->name);
992 else
993 (void)fprintf(fp, "set no"WS"\n", op->name);
994 break;
995 case OPT_NUM:
996 (void)fprintf(fp,
997 "set "WS"=%-3ld\n", op->name, O_VAL(sp, cnt));
998 break;
999 case OPT_STR:
1000 if (O_STR(sp, cnt) == NULL)
1001 break;
1002 (void)fprintf(fp, "set ");
1003 for (p = op->name; (ch = *p) != '\0'; ++p) {
1004 if (cmdskip(ch) || ch == '\\')
1005 (void)putc('\\', fp);
1006 fprintf(fp, WC, ch);
1007 }
1008 (void)putc('=', fp);
1009 for (np = O_STR(sp, cnt); (nch = *np) != '\0'; ++np) {
1010 if (cmdskip(nch) || nch == '\\')
1011 (void)putc('\\', fp);
1012 (void)putc(nch, fp);
1013 }
1014 (void)putc('\n', fp);
1015 break;
1016 }
1017 if (ferror(fp)) {
1018 msgq(sp, M_SYSERR, NULL);
1019 return (1);
1020 }
1021 }
1022 return (0);
1023 }
1024
1025 /*
1026 * opts_search --
1027 * Search for an option.
1028 *
1029 * PUBLIC: OPTLIST const *opts_search(CHAR_T *);
1030 */
1031 OPTLIST const *
opts_search(CHAR_T * name)1032 opts_search(CHAR_T *name)
1033 {
1034 OPTLIST const *op, *found;
1035 OABBREV atmp, *ap;
1036 OPTLIST otmp;
1037 size_t len;
1038
1039 /* Check list of abbreviations. */
1040 atmp.name = name;
1041 if ((ap = bsearch(&atmp, abbrev, sizeof(abbrev) / sizeof(OABBREV) - 1,
1042 sizeof(OABBREV), opts_abbcmp)) != NULL)
1043 return (optlist + ap->offset);
1044
1045 /* Check list of options. */
1046 otmp.name = name;
1047 if ((op = bsearch(&otmp, optlist, sizeof(optlist) / sizeof(OPTLIST) - 1,
1048 sizeof(OPTLIST), opts_cmp)) != NULL)
1049 return (op);
1050
1051 /*
1052 * Check to see if the name is the prefix of one (and only one)
1053 * option. If so, return the option.
1054 */
1055 len = STRLEN(name);
1056 for (found = NULL, op = optlist; op->name != NULL; ++op) {
1057 if (op->name[0] < name[0])
1058 continue;
1059 if (op->name[0] > name[0])
1060 break;
1061 if (!MEMCMP(op->name, name, len)) {
1062 if (found != NULL)
1063 return (NULL);
1064 found = op;
1065 }
1066 }
1067 return (found);
1068 }
1069
1070 /*
1071 * opts_nomatch --
1072 * Standard nomatch error message for options.
1073 *
1074 * PUBLIC: void opts_nomatch(SCR *, CHAR_T *);
1075 */
1076 void
opts_nomatch(SCR * sp,CHAR_T * name)1077 opts_nomatch(SCR *sp, CHAR_T *name)
1078 {
1079 msgq_wstr(sp, M_ERR, name,
1080 "033|set: no %s option: 'set all' gives all option values");
1081 }
1082
1083 static int
opts_abbcmp(const void * a,const void * b)1084 opts_abbcmp(const void *a, const void *b)
1085 {
1086 return(STRCMP(((OABBREV *)a)->name, ((OABBREV *)b)->name));
1087 }
1088
1089 static int
opts_cmp(const void * a,const void * b)1090 opts_cmp(const void *a, const void *b)
1091 {
1092 return(STRCMP(((OPTLIST *)a)->name, ((OPTLIST *)b)->name));
1093 }
1094
1095 /*
1096 * opts_copy --
1097 * Copy a screen's OPTION array.
1098 *
1099 * PUBLIC: int opts_copy(SCR *, SCR *);
1100 */
1101 int
opts_copy(SCR * orig,SCR * sp)1102 opts_copy(SCR *orig, SCR *sp)
1103 {
1104 int cnt, rval;
1105
1106 /* Copy most everything without change. */
1107 memcpy(sp->opts, orig->opts, sizeof(orig->opts));
1108
1109 /* Copy the string edit options. */
1110 for (cnt = rval = 0; cnt < O_OPTIONCOUNT; ++cnt) {
1111 if (optlist[cnt].type != OPT_STR ||
1112 F_ISSET(&sp->opts[cnt], OPT_GLOBAL))
1113 continue;
1114 /*
1115 * If never set, or already failed, NULL out the entries --
1116 * have to continue after failure, otherwise would have two
1117 * screens referencing the same memory.
1118 */
1119 if (rval || O_STR(sp, cnt) == NULL) {
1120 o_set(sp, cnt, OS_NOFREE | OS_STR, NULL, 0);
1121 o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
1122 continue;
1123 }
1124
1125 /* Copy the current string. */
1126 if (o_set(sp, cnt, OS_NOFREE | OS_STRDUP, O_STR(sp, cnt), 0)) {
1127 o_set(sp, cnt, OS_DEF | OS_NOFREE | OS_STR, NULL, 0);
1128 goto nomem;
1129 }
1130
1131 /* Copy the default string. */
1132 if (O_D_STR(sp, cnt) != NULL && o_set(sp, cnt,
1133 OS_DEF | OS_NOFREE | OS_STRDUP, O_D_STR(sp, cnt), 0)) {
1134 nomem: msgq(orig, M_SYSERR, NULL);
1135 rval = 1;
1136 }
1137 }
1138 return (rval);
1139 }
1140
1141 /*
1142 * opts_free --
1143 * Free all option strings
1144 *
1145 * PUBLIC: void opts_free(SCR *);
1146 */
1147 void
opts_free(SCR * sp)1148 opts_free(SCR *sp)
1149 {
1150 int cnt;
1151
1152 for (cnt = 0; cnt < O_OPTIONCOUNT; ++cnt) {
1153 if (optlist[cnt].type != OPT_STR ||
1154 F_ISSET(&sp->opts[cnt], OPT_GLOBAL))
1155 continue;
1156 free(O_STR(sp, cnt));
1157 free(O_D_STR(sp, cnt));
1158 }
1159 }
1160