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