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