Home | History | Annotate | Line # | Download | only in sh
options.c revision 1.31
      1 /*	$NetBSD: options.c,v 1.31 2001/02/26 13:06:43 wiz Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Kenneth Almquist.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 #if 0
     42 static char sccsid[] = "@(#)options.c	8.2 (Berkeley) 5/4/95";
     43 #else
     44 __RCSID("$NetBSD: options.c,v 1.31 2001/02/26 13:06:43 wiz Exp $");
     45 #endif
     46 #endif /* not lint */
     47 
     48 #include <signal.h>
     49 #include <unistd.h>
     50 #include <stdlib.h>
     51 
     52 #include "shell.h"
     53 #define DEFINE_OPTIONS
     54 #include "options.h"
     55 #undef DEFINE_OPTIONS
     56 #include "nodes.h"	/* for other header files */
     57 #include "eval.h"
     58 #include "jobs.h"
     59 #include "input.h"
     60 #include "output.h"
     61 #include "trap.h"
     62 #include "var.h"
     63 #include "memalloc.h"
     64 #include "error.h"
     65 #include "mystring.h"
     66 #ifndef SMALL
     67 #include "myhistedit.h"
     68 #endif
     69 
     70 char *arg0;			/* value of $0 */
     71 struct shparam shellparam;	/* current positional parameters */
     72 char **argptr;			/* argument list for builtin commands */
     73 char *optionarg;		/* set by nextopt (like getopt) */
     74 char *optptr;			/* used by nextopt */
     75 
     76 char *minusc;			/* argument to -c option */
     77 
     78 
     79 STATIC void options __P((int));
     80 STATIC void minus_o __P((char *, int));
     81 STATIC void setoption __P((int, int));
     82 STATIC int getopts __P((char *, char *, char **, char ***, char **));
     83 
     84 
     85 /*
     86  * Process the shell command line arguments.
     87  */
     88 
     89 void
     90 procargs(argc, argv)
     91 	int argc;
     92 	char **argv;
     93 {
     94 	int i;
     95 
     96 	argptr = argv;
     97 	if (argc > 0)
     98 		argptr++;
     99 	for (i = 0; i < NOPTS; i++)
    100 		optlist[i].val = 2;
    101 	options(1);
    102 	if (*argptr == NULL && minusc == NULL)
    103 		sflag = 1;
    104 	if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
    105 		iflag = 1;
    106 	if (mflag == 2)
    107 		mflag = iflag;
    108 	for (i = 0; i < NOPTS; i++)
    109 		if (optlist[i].val == 2)
    110 			optlist[i].val = 0;
    111 	arg0 = argv[0];
    112 	if (sflag == 0 && minusc == NULL) {
    113 		commandname = argv[0];
    114 		arg0 = *argptr++;
    115 		setinputfile(arg0, 0);
    116 		commandname = arg0;
    117 	}
    118 	/* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
    119 	if (argptr && minusc && *argptr)
    120 	        arg0 = *argptr++;
    121 
    122 	shellparam.p = argptr;
    123 	shellparam.reset = 1;
    124 	/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
    125 	while (*argptr) {
    126 		shellparam.nparam++;
    127 		argptr++;
    128 	}
    129 	optschanged();
    130 }
    131 
    132 
    133 void
    134 optschanged()
    135 {
    136 	setinteractive(iflag);
    137 #ifndef SMALL
    138 	histedit();
    139 #endif
    140 	setjobctl(mflag);
    141 }
    142 
    143 /*
    144  * Process shell options.  The global variable argptr contains a pointer
    145  * to the argument list; we advance it past the options.
    146  */
    147 
    148 STATIC void
    149 options(cmdline)
    150 	int cmdline;
    151 {
    152 	char *p;
    153 	int val;
    154 	int c;
    155 
    156 	if (cmdline)
    157 		minusc = NULL;
    158 	while ((p = *argptr) != NULL) {
    159 		argptr++;
    160 		if ((c = *p++) == '-') {
    161 			val = 1;
    162                         if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
    163                                 if (!cmdline) {
    164                                         /* "-" means turn off -x and -v */
    165                                         if (p[0] == '\0')
    166                                                 xflag = vflag = 0;
    167                                         /* "--" means reset params */
    168                                         else if (*argptr == NULL)
    169 						setparam(argptr);
    170                                 }
    171 				break;	  /* "-" or  "--" terminates options */
    172 			}
    173 		} else if (c == '+') {
    174 			val = 0;
    175 		} else {
    176 			argptr--;
    177 			break;
    178 		}
    179 		while ((c = *p++) != '\0') {
    180 			if (c == 'c' && cmdline) {
    181 				char *q;
    182 #ifdef NOHACK	/* removing this code allows sh -ce 'foo' for compat */
    183 				if (*p == '\0')
    184 #endif
    185 					q = *argptr++;
    186 				if (q == NULL || minusc != NULL)
    187 					error("Bad -c option");
    188 				minusc = q;
    189 #ifdef NOHACK
    190 				break;
    191 #endif
    192 			} else if (c == 'o') {
    193 				minus_o(*argptr, val);
    194 				if (*argptr)
    195 					argptr++;
    196 			} else {
    197 				setoption(c, val);
    198 			}
    199 		}
    200 	}
    201 }
    202 
    203 STATIC void
    204 minus_o(name, val)
    205 	char *name;
    206 	int val;
    207 {
    208 	int i;
    209 
    210 	if (name == NULL) {
    211 		out1str("Current option settings\n");
    212 		for (i = 0; i < NOPTS; i++)
    213 			out1fmt("%-16s%s\n", optlist[i].name,
    214 				optlist[i].val ? "on" : "off");
    215 	} else {
    216 		for (i = 0; i < NOPTS; i++)
    217 			if (equal(name, optlist[i].name)) {
    218 				setoption(optlist[i].letter, val);
    219 				return;
    220 			}
    221 		error("Illegal option -o %s", name);
    222 	}
    223 }
    224 
    225 
    226 STATIC void
    227 setoption(flag, val)
    228 	char flag;
    229 	int val;
    230 	{
    231 	int i;
    232 
    233 	for (i = 0; i < NOPTS; i++)
    234 		if (optlist[i].letter == flag) {
    235 			optlist[i].val = val;
    236 			if (val) {
    237 				/* #%$ hack for ksh semantics */
    238 				if (flag == 'V')
    239 					Eflag = 0;
    240 				else if (flag == 'E')
    241 					Vflag = 0;
    242 			}
    243 			return;
    244 		}
    245 	error("Illegal option -%c", flag);
    246 	/* NOTREACHED */
    247 }
    248 
    249 
    250 
    251 #ifdef mkinit
    252 INCLUDE "options.h"
    253 
    254 SHELLPROC {
    255 	int i;
    256 
    257 	for (i = 0; i < NOPTS; i++)
    258 		optlist[i].val = 0;
    259 	optschanged();
    260 
    261 }
    262 #endif
    263 
    264 
    265 /*
    266  * Set the shell parameters.
    267  */
    268 
    269 void
    270 setparam(argv)
    271 	char **argv;
    272 	{
    273 	char **newparam;
    274 	char **ap;
    275 	int nparam;
    276 
    277 	for (nparam = 0 ; argv[nparam] ; nparam++);
    278 	ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
    279 	while (*argv) {
    280 		*ap++ = savestr(*argv++);
    281 	}
    282 	*ap = NULL;
    283 	freeparam(&shellparam);
    284 	shellparam.malloc = 1;
    285 	shellparam.nparam = nparam;
    286 	shellparam.p = newparam;
    287 	shellparam.optnext = NULL;
    288 }
    289 
    290 
    291 /*
    292  * Free the list of positional parameters.
    293  */
    294 
    295 void
    296 freeparam(param)
    297 	volatile struct shparam *param;
    298 	{
    299 	char **ap;
    300 
    301 	if (param->malloc) {
    302 		for (ap = param->p ; *ap ; ap++)
    303 			ckfree(*ap);
    304 		ckfree(param->p);
    305 	}
    306 }
    307 
    308 
    309 
    310 /*
    311  * The shift builtin command.
    312  */
    313 
    314 int
    315 shiftcmd(argc, argv)
    316 	int argc;
    317 	char **argv;
    318 {
    319 	int n;
    320 	char **ap1, **ap2;
    321 
    322 	n = 1;
    323 	if (argc > 1)
    324 		n = number(argv[1]);
    325 	if (n > shellparam.nparam)
    326 		error("can't shift that many");
    327 	INTOFF;
    328 	shellparam.nparam -= n;
    329 	for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
    330 		if (shellparam.malloc)
    331 			ckfree(*ap1);
    332 	}
    333 	ap2 = shellparam.p;
    334 	while ((*ap2++ = *ap1++) != NULL);
    335 	shellparam.optnext = NULL;
    336 	INTON;
    337 	return 0;
    338 }
    339 
    340 
    341 
    342 /*
    343  * The set command builtin.
    344  */
    345 
    346 int
    347 setcmd(argc, argv)
    348 	int argc;
    349 	char **argv;
    350 {
    351 	if (argc == 1)
    352 		return showvarscmd(argc, argv);
    353 	INTOFF;
    354 	options(0);
    355 	optschanged();
    356 	if (*argptr != NULL) {
    357 		setparam(argptr);
    358 	}
    359 	INTON;
    360 	return 0;
    361 }
    362 
    363 
    364 void
    365 getoptsreset(value)
    366 	const char *value;
    367 {
    368 	if (number(value) == 1) {
    369 		shellparam.optnext = NULL;
    370 		shellparam.reset = 1;
    371 	}
    372 }
    373 
    374 /*
    375  * The getopts builtin.  Shellparam.optnext points to the next argument
    376  * to be processed.  Shellparam.optptr points to the next character to
    377  * be processed in the current argument.  If shellparam.optnext is NULL,
    378  * then it's the first time getopts has been called.
    379  */
    380 
    381 int
    382 getoptscmd(argc, argv)
    383 	int argc;
    384 	char **argv;
    385 {
    386 	char **optbase;
    387 
    388 	if (argc < 3)
    389 		error("Usage: getopts optstring var [arg]");
    390 	else if (argc == 3)
    391 		optbase = shellparam.p;
    392 	else
    393 		optbase = &argv[3];
    394 
    395 	if (shellparam.reset == 1) {
    396 		shellparam.optnext = optbase;
    397 		shellparam.optptr = NULL;
    398 		shellparam.reset = 0;
    399 	}
    400 
    401 	return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
    402 		       &shellparam.optptr);
    403 }
    404 
    405 STATIC int
    406 getopts(optstr, optvar, optfirst, optnext, optpptr)
    407 	char *optstr;
    408 	char *optvar;
    409 	char **optfirst;
    410 	char ***optnext;
    411 	char **optpptr;
    412 {
    413 	char *p, *q;
    414 	char c = '?';
    415 	int done = 0;
    416 	int ind = 0;
    417 	int err = 0;
    418 	char s[10];
    419 
    420 	if ((p = *optpptr) == NULL || *p == '\0') {
    421 		/* Current word is done, advance */
    422 		if (*optnext == NULL)
    423 			return 1;
    424 		p = **optnext;
    425 		if (p == NULL || *p != '-' || *++p == '\0') {
    426 atend:
    427 			ind = *optnext - optfirst + 1;
    428 			*optnext = NULL;
    429 			p = NULL;
    430 			done = 1;
    431 			goto out;
    432 		}
    433 		(*optnext)++;
    434 		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
    435 			goto atend;
    436 	}
    437 
    438 	c = *p++;
    439 	for (q = optstr; *q != c; ) {
    440 		if (*q == '\0') {
    441 			if (optstr[0] == ':') {
    442 				s[0] = c;
    443 				s[1] = '\0';
    444 				err |= setvarsafe("OPTARG", s, 0);
    445 			}
    446 			else {
    447 				outfmt(&errout, "Illegal option -%c\n", c);
    448 				(void) unsetvar("OPTARG");
    449 			}
    450 			c = '?';
    451 			goto bad;
    452 		}
    453 		if (*++q == ':')
    454 			q++;
    455 	}
    456 
    457 	if (*++q == ':') {
    458 		if (*p == '\0' && (p = **optnext) == NULL) {
    459 			if (optstr[0] == ':') {
    460 				s[0] = c;
    461 				s[1] = '\0';
    462 				err |= setvarsafe("OPTARG", s, 0);
    463 				c = ':';
    464 			}
    465 			else {
    466 				outfmt(&errout, "No arg for -%c option\n", c);
    467 				(void) unsetvar("OPTARG");
    468 				c = '?';
    469 			}
    470 			goto bad;
    471 		}
    472 
    473 		if (p == **optnext)
    474 			(*optnext)++;
    475 		setvarsafe("OPTARG", p, 0);
    476 		p = NULL;
    477 	}
    478 	else
    479 		setvarsafe("OPTARG", "", 0);
    480 	ind = *optnext - optfirst + 1;
    481 	goto out;
    482 
    483 bad:
    484 	ind = 1;
    485 	*optnext = NULL;
    486 	p = NULL;
    487 out:
    488 	*optpptr = p;
    489 	fmtstr(s, sizeof(s), "%d", ind);
    490 	err |= setvarsafe("OPTIND", s, VNOFUNC);
    491 	s[0] = c;
    492 	s[1] = '\0';
    493 	err |= setvarsafe(optvar, s, 0);
    494 	if (err) {
    495 		*optnext = NULL;
    496 		*optpptr = NULL;
    497 		flushall();
    498 		exraise(EXERROR);
    499 	}
    500 	return done;
    501 }
    502 
    503 /*
    504  * XXX - should get rid of.  have all builtins use getopt(3).  the
    505  * library getopt must have the BSD extension static variable "optreset"
    506  * otherwise it can't be used within the shell safely.
    507  *
    508  * Standard option processing (a la getopt) for builtin routines.  The
    509  * only argument that is passed to nextopt is the option string; the
    510  * other arguments are unnecessary.  It return the character, or '\0' on
    511  * end of input.
    512  */
    513 
    514 int
    515 nextopt(optstring)
    516 	const char *optstring;
    517 	{
    518 	char *p;
    519 	const char *q;
    520 	char c;
    521 
    522 	if ((p = optptr) == NULL || *p == '\0') {
    523 		p = *argptr;
    524 		if (p == NULL || *p != '-' || *++p == '\0')
    525 			return '\0';
    526 		argptr++;
    527 		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
    528 			return '\0';
    529 	}
    530 	c = *p++;
    531 	for (q = optstring ; *q != c ; ) {
    532 		if (*q == '\0')
    533 			error("Illegal option -%c", c);
    534 		if (*++q == ':')
    535 			q++;
    536 	}
    537 	if (*++q == ':') {
    538 		if (*p == '\0' && (p = *argptr++) == NULL)
    539 			error("No arg for -%c option", c);
    540 		optionarg = p;
    541 		p = NULL;
    542 	}
    543 	optptr = p;
    544 	return c;
    545 }
    546