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