options.c revision 267654
1270096Strasz/*- 2270096Strasz * Copyright (c) 1991, 1993 3270096Strasz * The Regents of the University of California. All rights reserved. 4270096Strasz * 5270096Strasz * This code is derived from software contributed to Berkeley by 6270096Strasz * Kenneth Almquist. 7270096Strasz * 8270096Strasz * Redistribution and use in source and binary forms, with or without 9270096Strasz * modification, are permitted provided that the following conditions 10270096Strasz * are met: 11270096Strasz * 1. Redistributions of source code must retain the above copyright 12270096Strasz * notice, this list of conditions and the following disclaimer. 13270096Strasz * 2. Redistributions in binary form must reproduce the above copyright 14270096Strasz * notice, this list of conditions and the following disclaimer in the 15270096Strasz * documentation and/or other materials provided with the distribution. 16270096Strasz * 4. Neither the name of the University nor the names of its contributors 17270096Strasz * may be used to endorse or promote products derived from this software 18270096Strasz * without specific prior written permission. 19270096Strasz * 20270096Strasz * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21270096Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22270096Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23270096Strasz * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24270096Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25270096Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26270096Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27270096Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28270096Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29270096Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30270096Strasz * SUCH DAMAGE. 31270096Strasz */ 32270096Strasz 33270096Strasz#ifndef lint 34270096Strasz#if 0 35270096Straszstatic char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95"; 36270096Strasz#endif 37270096Strasz#endif /* not lint */ 38270096Strasz#include <sys/cdefs.h> 39270096Strasz__FBSDID("$FreeBSD: releng/9.3/bin/sh/options.c 223060 2011-06-13 21:03:27Z jilles $"); 40270096Strasz 41270096Strasz#include <signal.h> 42270096Strasz#include <unistd.h> 43270096Strasz#include <stdlib.h> 44270096Strasz 45270096Strasz#include "shell.h" 46270096Strasz#define DEFINE_OPTIONS 47270096Strasz#include "options.h" 48270096Strasz#undef DEFINE_OPTIONS 49270281Strasz#include "nodes.h" /* for other header files */ 50270096Strasz#include "eval.h" 51270096Strasz#include "jobs.h" 52270096Strasz#include "input.h" 53270096Strasz#include "output.h" 54270402Strasz#include "trap.h" 55270402Strasz#include "var.h" 56270096Strasz#include "memalloc.h" 57270096Strasz#include "error.h" 58270096Strasz#include "mystring.h" 59270096Strasz#include "builtins.h" 60270096Strasz#ifndef NO_HISTORY 61270096Strasz#include "myhistedit.h" 62270096Strasz#endif 63270096Strasz 64270096Straszchar *arg0; /* value of $0 */ 65270096Straszstruct shparam shellparam; /* current positional parameters */ 66270096Straszchar **argptr; /* argument list for builtin commands */ 67270096Straszchar *shoptarg; /* set by nextopt (like getopt) */ 68270096Straszchar *nextopt_optptr; /* used by nextopt */ 69270096Strasz 70270096Straszchar *minusc; /* argument to -c option */ 71270096Strasz 72270096Strasz 73270096Straszstatic void options(int); 74270096Straszstatic void minus_o(char *, int); 75270096Straszstatic void setoption(int, int); 76270096Straszstatic int getopts(char *, char *, char **, char ***, char **); 77270096Strasz 78270096Strasz 79270096Strasz/* 80270096Strasz * Process the shell command line arguments. 81270096Strasz */ 82270096Strasz 83270096Straszvoid 84270096Straszprocargs(int argc, char **argv) 85270096Strasz{ 86270096Strasz int i; 87270096Strasz char *scriptname; 88270096Strasz 89270096Strasz argptr = argv; 90270096Strasz if (argc > 0) 91270096Strasz argptr++; 92270096Strasz for (i = 0; i < NOPTS; i++) 93270096Strasz optlist[i].val = 2; 94270096Strasz privileged = (getuid() != geteuid() || getgid() != getegid()); 95270096Strasz options(1); 96270096Strasz if (*argptr == NULL && minusc == NULL) 97270096Strasz sflag = 1; 98270096Strasz if (iflag != 0 && sflag == 1 && isatty(0) && isatty(1)) { 99270096Strasz iflag = 1; 100270096Strasz if (Eflag == 2) 101270096Strasz Eflag = 1; 102270096Strasz } 103270096Strasz if (mflag == 2) 104270096Strasz mflag = iflag; 105270096Strasz for (i = 0; i < NOPTS; i++) 106270096Strasz if (optlist[i].val == 2) 107270096Strasz optlist[i].val = 0; 108270096Strasz arg0 = argv[0]; 109270096Strasz if (sflag == 0 && minusc == NULL) { 110270096Strasz scriptname = *argptr++; 111270096Strasz setinputfile(scriptname, 0); 112270096Strasz commandname = arg0 = scriptname; 113270096Strasz } 114270096Strasz /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ 115270096Strasz if (argptr && minusc && *argptr) 116270096Strasz arg0 = *argptr++; 117270096Strasz 118270096Strasz shellparam.p = argptr; 119270096Strasz shellparam.reset = 1; 120270096Strasz /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ 121270096Strasz while (*argptr) { 122270096Strasz shellparam.nparam++; 123270096Strasz argptr++; 124270096Strasz } 125270096Strasz optschanged(); 126270096Strasz} 127270096Strasz 128270096Strasz 129270096Straszvoid 130270096Straszoptschanged(void) 131270096Strasz{ 132270096Strasz setinteractive(iflag); 133270096Strasz#ifndef NO_HISTORY 134270096Strasz histedit(); 135270096Strasz#endif 136270096Strasz setjobctl(mflag); 137270096Strasz} 138270096Strasz 139270096Strasz/* 140270096Strasz * Process shell options. The global variable argptr contains a pointer 141270096Strasz * to the argument list; we advance it past the options. 142270096Strasz */ 143270096Strasz 144270096Straszstatic void 145270096Straszoptions(int cmdline) 146270096Strasz{ 147270096Strasz char *kp, *p; 148270096Strasz int val; 149270096Strasz int c; 150270096Strasz 151270096Strasz if (cmdline) 152270096Strasz minusc = NULL; 153270096Strasz while ((p = *argptr) != NULL) { 154270402Strasz argptr++; 155270096Strasz if ((c = *p++) == '-') { 156270096Strasz val = 1; 157270096Strasz /* A "-" or "--" terminates options */ 158270096Strasz if (p[0] == '\0') 159270096Strasz goto end_options1; 160270096Strasz if (p[0] == '-' && p[1] == '\0') 161270096Strasz goto end_options2; 162270096Strasz /** 163270096Strasz * For the benefit of `#!' lines in shell scripts, 164270096Strasz * treat a string of '-- *#.*' the same as '--'. 165270096Strasz * This is needed so that a script starting with: 166270402Strasz * #!/bin/sh -- # -*- perl -*- 167270096Strasz * will continue to work after a change is made to 168270096Strasz * kern/imgact_shell.c to NOT token-ize the options 169270096Strasz * specified on a '#!' line. A bit of a kludge, 170270096Strasz * but that trick is recommended in documentation 171270096Strasz * for some scripting languages, and we might as 172270096Strasz * well continue to support it. 173270096Strasz */ 174270096Strasz if (p[0] == '-') { 175270096Strasz kp = p + 1; 176270096Strasz while (*kp == ' ' || *kp == '\t') 177270096Strasz kp++; 178270096Strasz if (*kp == '#' || *kp == '\0') 179270096Strasz goto end_options2; 180270096Strasz } 181270096Strasz } else if (c == '+') { 182270096Strasz val = 0; 183270096Strasz } else { 184270096Strasz argptr--; 185270096Strasz break; 186270096Strasz } 187270096Strasz while ((c = *p++) != '\0') { 188270096Strasz if (c == 'c' && cmdline) { 189270096Strasz char *q; 190270096Strasz#ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */ 191270096Strasz if (*p == '\0') 192270096Strasz#endif 193270096Strasz q = *argptr++; 194270096Strasz if (q == NULL || minusc != NULL) 195270096Strasz error("Bad -c option"); 196270096Strasz minusc = q; 197270096Strasz#ifdef NOHACK 198270096Strasz break; 199270096Strasz#endif 200270207Strasz } else if (c == 'o') { 201270207Strasz minus_o(*argptr, val); 202270207Strasz if (*argptr) 203270207Strasz argptr++; 204270207Strasz } else 205270207Strasz setoption(c, val); 206270207Strasz } 207270207Strasz } 208270207Strasz return; 209270096Strasz 210270096Strasz /* When processing `set', a single "-" means turn off -x and -v */ 211270096Straszend_options1: 212270096Strasz if (!cmdline) { 213270096Strasz xflag = vflag = 0; 214270096Strasz return; 215270096Strasz } 216270096Strasz 217270096Strasz /* 218270096Strasz * When processing `set', a "--" means the remaining arguments 219270096Strasz * replace the positional parameters in the active shell. If 220270096Strasz * there are no remaining options, then all the positional 221270096Strasz * parameters are cleared (equivalent to doing ``shift $#''). 222270096Strasz */ 223270096Straszend_options2: 224270096Strasz if (!cmdline) { 225270096Strasz if (*argptr == NULL) 226270096Strasz setparam(argptr); 227270096Strasz return; 228270207Strasz } 229270207Strasz 230270207Strasz /* 231270207Strasz * At this point we are processing options given to 'sh' on a command 232270207Strasz * line. If an end-of-options marker ("-" or "--") is followed by an 233270096Strasz * arg of "#", then skip over all remaining arguments. Some scripting 234270207Strasz * languages (e.g.: perl) document that /bin/sh will implement this 235270207Strasz * behavior, and they recommend that users take advantage of it to 236270096Strasz * solve certain issues that can come up when writing a perl script. 237270207Strasz * Yes, this feature is in /bin/sh to help users write perl scripts. 238270096Strasz */ 239270207Strasz p = *argptr; 240270096Strasz if (p != NULL && p[0] == '#' && p[1] == '\0') { 241270096Strasz while (*argptr != NULL) 242270096Strasz argptr++; 243270096Strasz /* We need to keep the final argument */ 244270096Strasz argptr--; 245270096Strasz } 246270096Strasz} 247270096Strasz 248270096Straszstatic void 249270096Straszminus_o(char *name, int val) 250270096Strasz{ 251270096Strasz int i; 252270096Strasz 253270096Strasz if (name == NULL) { 254270096Strasz if (val) { 255270096Strasz /* "Pretty" output. */ 256270096Strasz out1str("Current option settings\n"); 257270096Strasz for (i = 0; i < NOPTS; i++) 258270096Strasz out1fmt("%-16s%s\n", optlist[i].name, 259270096Strasz optlist[i].val ? "on" : "off"); 260270096Strasz } else { 261270096Strasz /* Output suitable for re-input to shell. */ 262270096Strasz for (i = 0; i < NOPTS; i++) 263270096Strasz out1fmt("%s %co %s%s", 264270096Strasz i % 6 == 0 ? "set" : "", 265270096Strasz optlist[i].val ? '-' : '+', 266270096Strasz optlist[i].name, 267270096Strasz i % 6 == 5 || i == NOPTS - 1 ? "\n" : ""); 268270096Strasz } 269270096Strasz } else { 270270096Strasz for (i = 0; i < NOPTS; i++) 271270096Strasz if (equal(name, optlist[i].name)) { 272270096Strasz setoption(optlist[i].letter, val); 273270096Strasz return; 274270096Strasz } 275270096Strasz error("Illegal option -o %s", name); 276270096Strasz } 277270096Strasz} 278270096Strasz 279270096Strasz 280270096Straszstatic void 281270096Straszsetoption(int flag, int val) 282270096Strasz{ 283270096Strasz int i; 284270096Strasz 285270096Strasz if (flag == 'p' && !val && privileged) { 286270096Strasz if (setgid(getgid()) == -1) 287270096Strasz error("setgid"); 288270096Strasz if (setuid(getuid()) == -1) 289270096Strasz error("setuid"); 290270096Strasz } 291270096Strasz for (i = 0; i < NOPTS; i++) 292270096Strasz if (optlist[i].letter == flag) { 293270096Strasz optlist[i].val = val; 294270096Strasz if (val) { 295270096Strasz /* #%$ hack for ksh semantics */ 296270096Strasz if (flag == 'V') 297270096Strasz Eflag = 0; 298270096Strasz else if (flag == 'E') 299270096Strasz Vflag = 0; 300270096Strasz } 301270096Strasz return; 302270096Strasz } 303270096Strasz error("Illegal option -%c", flag); 304270096Strasz} 305270096Strasz 306270096Strasz 307270096Strasz/* 308270096Strasz * Set the shell parameters. 309270096Strasz */ 310270096Strasz 311270096Straszvoid 312270096Straszsetparam(char **argv) 313270096Strasz{ 314270096Strasz char **newparam; 315270096Strasz char **ap; 316270096Strasz int nparam; 317270096Strasz 318270096Strasz for (nparam = 0 ; argv[nparam] ; nparam++); 319270096Strasz ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); 320270096Strasz while (*argv) { 321270096Strasz *ap++ = savestr(*argv++); 322270096Strasz } 323270096Strasz *ap = NULL; 324270096Strasz freeparam(&shellparam); 325270096Strasz shellparam.malloc = 1; 326270096Strasz shellparam.nparam = nparam; 327270096Strasz shellparam.p = newparam; 328270096Strasz shellparam.reset = 1; 329270096Strasz shellparam.optnext = NULL; 330270096Strasz} 331270096Strasz 332270096Strasz 333270096Strasz/* 334270096Strasz * Free the list of positional parameters. 335270096Strasz */ 336270096Strasz 337270096Straszvoid 338270096Straszfreeparam(struct shparam *param) 339270096Strasz{ 340270096Strasz char **ap; 341270096Strasz 342270096Strasz if (param->malloc) { 343270096Strasz for (ap = param->p ; *ap ; ap++) 344270096Strasz ckfree(*ap); 345270096Strasz ckfree(param->p); 346270096Strasz } 347270096Strasz} 348270096Strasz 349270096Strasz 350270096Strasz 351270096Strasz/* 352270096Strasz * The shift builtin command. 353270096Strasz */ 354270096Strasz 355270096Straszint 356270096Straszshiftcmd(int argc, char **argv) 357270096Strasz{ 358270096Strasz int n; 359270096Strasz char **ap1, **ap2; 360270096Strasz 361270096Strasz n = 1; 362270096Strasz if (argc > 1) 363270096Strasz n = number(argv[1]); 364270096Strasz if (n > shellparam.nparam) 365270096Strasz return 1; 366270096Strasz INTOFF; 367270096Strasz shellparam.nparam -= n; 368270096Strasz for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { 369270096Strasz if (shellparam.malloc) 370270096Strasz ckfree(*ap1); 371270096Strasz } 372270096Strasz ap2 = shellparam.p; 373270096Strasz while ((*ap2++ = *ap1++) != NULL); 374270096Strasz shellparam.reset = 1; 375270096Strasz INTON; 376270096Strasz return 0; 377270096Strasz} 378270096Strasz 379270096Strasz 380270096Strasz 381270096Strasz/* 382270096Strasz * The set command builtin. 383270096Strasz */ 384270096Strasz 385270096Straszint 386270096Straszsetcmd(int argc, char **argv) 387270096Strasz{ 388270096Strasz if (argc == 1) 389270096Strasz return showvarscmd(argc, argv); 390270096Strasz INTOFF; 391270096Strasz options(0); 392270096Strasz optschanged(); 393270096Strasz if (*argptr != NULL) { 394270096Strasz setparam(argptr); 395270096Strasz } 396270096Strasz INTON; 397270096Strasz return 0; 398270096Strasz} 399270096Strasz 400270096Strasz 401270096Straszvoid 402270096Straszgetoptsreset(const char *value) 403270096Strasz{ 404270096Strasz if (number(value) == 1) { 405270096Strasz shellparam.reset = 1; 406270096Strasz } 407270096Strasz} 408270096Strasz 409270096Strasz/* 410270096Strasz * The getopts builtin. Shellparam.optnext points to the next argument 411270096Strasz * to be processed. Shellparam.optptr points to the next character to 412270096Strasz * be processed in the current argument. If shellparam.optnext is NULL, 413270096Strasz * then it's the first time getopts has been called. 414270096Strasz */ 415270096Strasz 416270096Straszint 417270096Straszgetoptscmd(int argc, char **argv) 418270096Strasz{ 419270096Strasz char **optbase = NULL; 420270096Strasz 421270096Strasz if (argc < 3) 422270096Strasz error("usage: getopts optstring var [arg]"); 423270096Strasz else if (argc == 3) 424270096Strasz optbase = shellparam.p; 425270096Strasz else 426270096Strasz optbase = &argv[3]; 427270096Strasz 428270096Strasz if (shellparam.reset == 1) { 429270096Strasz shellparam.optnext = optbase; 430270096Strasz shellparam.optptr = NULL; 431270096Strasz shellparam.reset = 0; 432270096Strasz } 433270096Strasz 434270096Strasz return getopts(argv[1], argv[2], optbase, &shellparam.optnext, 435270096Strasz &shellparam.optptr); 436270096Strasz} 437270096Strasz 438270096Straszstatic int 439270096Straszgetopts(char *optstr, char *optvar, char **optfirst, char ***optnext, 440270096Strasz char **optptr) 441270096Strasz{ 442270096Strasz char *p, *q; 443270096Strasz char c = '?'; 444270096Strasz int done = 0; 445270096Strasz int ind = 0; 446270096Strasz int err = 0; 447270096Strasz char s[10]; 448270096Strasz 449270096Strasz if ((p = *optptr) == NULL || *p == '\0') { 450270096Strasz /* Current word is done, advance */ 451270096Strasz if (*optnext == NULL) 452270096Strasz return 1; 453270096Strasz p = **optnext; 454270096Strasz if (p == NULL || *p != '-' || *++p == '\0') { 455270096Straszatend: 456270096Strasz ind = *optnext - optfirst + 1; 457270096Strasz *optnext = NULL; 458270096Strasz p = NULL; 459270096Strasz done = 1; 460270096Strasz goto out; 461270096Strasz } 462270096Strasz (*optnext)++; 463270096Strasz if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 464270096Strasz goto atend; 465270096Strasz } 466270096Strasz 467270096Strasz c = *p++; 468270096Strasz for (q = optstr; *q != c; ) { 469270096Strasz if (*q == '\0') { 470270096Strasz if (optstr[0] == ':') { 471270096Strasz s[0] = c; 472270096Strasz s[1] = '\0'; 473270096Strasz err |= setvarsafe("OPTARG", s, 0); 474270096Strasz } 475270096Strasz else { 476270096Strasz out1fmt("Illegal option -%c\n", c); 477270096Strasz (void) unsetvar("OPTARG"); 478270096Strasz } 479270096Strasz c = '?'; 480270096Strasz goto bad; 481270096Strasz } 482270096Strasz if (*++q == ':') 483270096Strasz q++; 484270096Strasz } 485270096Strasz 486270096Strasz if (*++q == ':') { 487270096Strasz if (*p == '\0' && (p = **optnext) == NULL) { 488270096Strasz if (optstr[0] == ':') { 489270096Strasz s[0] = c; 490270096Strasz s[1] = '\0'; 491270096Strasz err |= setvarsafe("OPTARG", s, 0); 492270096Strasz c = ':'; 493270096Strasz } 494270096Strasz else { 495270096Strasz out1fmt("No arg for -%c option\n", c); 496270096Strasz (void) unsetvar("OPTARG"); 497270096Strasz c = '?'; 498270096Strasz } 499270096Strasz goto bad; 500270096Strasz } 501270096Strasz 502270096Strasz if (p == **optnext) 503270096Strasz (*optnext)++; 504270096Strasz setvarsafe("OPTARG", p, 0); 505270096Strasz p = NULL; 506270096Strasz } 507270096Strasz else 508270096Strasz setvarsafe("OPTARG", "", 0); 509270096Strasz ind = *optnext - optfirst + 1; 510270096Strasz goto out; 511270096Strasz 512270096Straszbad: 513270096Strasz ind = 1; 514270096Strasz *optnext = NULL; 515270096Strasz p = NULL; 516270096Straszout: 517270096Strasz *optptr = p; 518270096Strasz fmtstr(s, sizeof(s), "%d", ind); 519270096Strasz err |= setvarsafe("OPTIND", s, VNOFUNC); 520270096Strasz s[0] = c; 521270096Strasz s[1] = '\0'; 522270096Strasz err |= setvarsafe(optvar, s, 0); 523270096Strasz if (err) { 524270096Strasz *optnext = NULL; 525270096Strasz *optptr = NULL; 526270096Strasz flushall(); 527270096Strasz exraise(EXERROR); 528270096Strasz } 529270096Strasz return done; 530270096Strasz} 531270096Strasz 532270096Strasz/* 533270096Strasz * XXX - should get rid of. have all builtins use getopt(3). the 534270096Strasz * library getopt must have the BSD extension static variable "optreset" 535270096Strasz * otherwise it can't be used within the shell safely. 536270096Strasz * 537270096Strasz * Standard option processing (a la getopt) for builtin routines. The 538270096Strasz * only argument that is passed to nextopt is the option string; the 539270096Strasz * other arguments are unnecessary. It return the character, or '\0' on 540270096Strasz * end of input. 541270096Strasz */ 542270096Strasz 543270096Straszint 544270096Strasznextopt(const char *optstring) 545270096Strasz{ 546270096Strasz char *p; 547270096Strasz const char *q; 548270096Strasz char c; 549270096Strasz 550270096Strasz if ((p = nextopt_optptr) == NULL || *p == '\0') { 551270096Strasz p = *argptr; 552270096Strasz if (p == NULL || *p != '-' || *++p == '\0') 553270096Strasz return '\0'; 554270096Strasz argptr++; 555270096Strasz if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 556270096Strasz return '\0'; 557270096Strasz } 558270096Strasz c = *p++; 559270096Strasz for (q = optstring ; *q != c ; ) { 560270096Strasz if (*q == '\0') 561270096Strasz error("Illegal option -%c", c); 562270096Strasz if (*++q == ':') 563270096Strasz q++; 564270096Strasz } 565270096Strasz if (*++q == ':') { 566270096Strasz if (*p == '\0' && (p = *argptr++) == NULL) 567270096Strasz error("No arg for -%c option", c); 568270096Strasz shoptarg = p; 569270096Strasz p = NULL; 570270096Strasz } 571270096Strasz nextopt_optptr = p; 572270096Strasz return c; 573270096Strasz} 574270096Strasz