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