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