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