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