Home | History | Annotate | Line # | Download | only in sh
options.c revision 1.26
      1 /*	$NetBSD: options.c,v 1.26 1998/05/02 18:04:09 christos 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.26 1998/05/02 18:04:09 christos 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 *optarg;			/* 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 = arg0 = *argptr++;
    114 		setinputfile(commandname, 0);
    115 	}
    116 	/* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
    117 	if (argptr && minusc && *argptr)
    118 	        arg0 = *argptr++;
    119 
    120 	shellparam.p = argptr;
    121 	shellparam.reset = 1;
    122 	/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
    123 	while (*argptr) {
    124 		shellparam.nparam++;
    125 		argptr++;
    126 	}
    127 	optschanged();
    128 }
    129 
    130 
    131 void
    132 optschanged()
    133 {
    134 	setinteractive(iflag);
    135 #ifndef SMALL
    136 	histedit();
    137 #endif
    138 	setjobctl(mflag);
    139 }
    140 
    141 /*
    142  * Process shell options.  The global variable argptr contains a pointer
    143  * to the argument list; we advance it past the options.
    144  */
    145 
    146 STATIC void
    147 options(cmdline)
    148 	int cmdline;
    149 {
    150 	char *p;
    151 	int val;
    152 	int c;
    153 
    154 	if (cmdline)
    155 		minusc = NULL;
    156 	while ((p = *argptr) != NULL) {
    157 		argptr++;
    158 		if ((c = *p++) == '-') {
    159 			val = 1;
    160                         if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
    161                                 if (!cmdline) {
    162                                         /* "-" means turn off -x and -v */
    163                                         if (p[0] == '\0')
    164                                                 xflag = vflag = 0;
    165                                         /* "--" means reset params */
    166                                         else if (*argptr == NULL)
    167 						setparam(argptr);
    168                                 }
    169 				break;	  /* "-" or  "--" terminates options */
    170 			}
    171 		} else if (c == '+') {
    172 			val = 0;
    173 		} else {
    174 			argptr--;
    175 			break;
    176 		}
    177 		while ((c = *p++) != '\0') {
    178 			if (c == 'c' && cmdline) {
    179 				char *q;
    180 #ifdef NOHACK	/* removing this code allows sh -ce 'foo' for compat */
    181 				if (*p == '\0')
    182 #endif
    183 					q = *argptr++;
    184 				if (q == NULL || minusc != NULL)
    185 					error("Bad -c option");
    186 				minusc = q;
    187 #ifdef NOHACK
    188 				break;
    189 #endif
    190 			} else if (c == 'o') {
    191 				minus_o(*argptr, val);
    192 				if (*argptr)
    193 					argptr++;
    194 			} else {
    195 				setoption(c, val);
    196 			}
    197 		}
    198 	}
    199 }
    200 
    201 STATIC void
    202 minus_o(name, val)
    203 	char *name;
    204 	int val;
    205 {
    206 	int i;
    207 
    208 	if (name == NULL) {
    209 		out1str("Current option settings\n");
    210 		for (i = 0; i < NOPTS; i++)
    211 			out1fmt("%-16s%s\n", optlist[i].name,
    212 				optlist[i].val ? "on" : "off");
    213 	} else {
    214 		for (i = 0; i < NOPTS; i++)
    215 			if (equal(name, optlist[i].name)) {
    216 				setoption(optlist[i].letter, val);
    217 				return;
    218 			}
    219 		error("Illegal option -o %s", name);
    220 	}
    221 }
    222 
    223 
    224 STATIC void
    225 setoption(flag, val)
    226 	char flag;
    227 	int val;
    228 	{
    229 	int i;
    230 
    231 	for (i = 0; i < NOPTS; i++)
    232 		if (optlist[i].letter == flag) {
    233 			optlist[i].val = val;
    234 			if (val) {
    235 				/* #%$ hack for ksh semantics */
    236 				if (flag == 'V')
    237 					Eflag = 0;
    238 				else if (flag == 'E')
    239 					Vflag = 0;
    240 			}
    241 			return;
    242 		}
    243 	error("Illegal option -%c", flag);
    244 }
    245 
    246 
    247 
    248 #ifdef mkinit
    249 INCLUDE "options.h"
    250 
    251 SHELLPROC {
    252 	int i;
    253 
    254 	for (i = 0; i < NOPTS; i++)
    255 		optlist[i].val = 0;
    256 	optschanged();
    257 
    258 }
    259 #endif
    260 
    261 
    262 /*
    263  * Set the shell parameters.
    264  */
    265 
    266 void
    267 setparam(argv)
    268 	char **argv;
    269 	{
    270 	char **newparam;
    271 	char **ap;
    272 	int nparam;
    273 
    274 	for (nparam = 0 ; argv[nparam] ; nparam++);
    275 	ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
    276 	while (*argv) {
    277 		*ap++ = savestr(*argv++);
    278 	}
    279 	*ap = NULL;
    280 	freeparam(&shellparam);
    281 	shellparam.malloc = 1;
    282 	shellparam.nparam = nparam;
    283 	shellparam.p = newparam;
    284 	shellparam.optnext = NULL;
    285 }
    286 
    287 
    288 /*
    289  * Free the list of positional parameters.
    290  */
    291 
    292 void
    293 freeparam(param)
    294 	struct shparam *param;
    295 	{
    296 	char **ap;
    297 
    298 	if (param->malloc) {
    299 		for (ap = param->p ; *ap ; ap++)
    300 			ckfree(*ap);
    301 		ckfree(param->p);
    302 	}
    303 }
    304 
    305 
    306 
    307 /*
    308  * The shift builtin command.
    309  */
    310 
    311 int
    312 shiftcmd(argc, argv)
    313 	int argc;
    314 	char **argv;
    315 {
    316 	int n;
    317 	char **ap1, **ap2;
    318 
    319 	n = 1;
    320 	if (argc > 1)
    321 		n = number(argv[1]);
    322 	if (n > shellparam.nparam)
    323 		error("can't shift that many");
    324 	INTOFF;
    325 	shellparam.nparam -= n;
    326 	for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
    327 		if (shellparam.malloc)
    328 			ckfree(*ap1);
    329 	}
    330 	ap2 = shellparam.p;
    331 	while ((*ap2++ = *ap1++) != NULL);
    332 	shellparam.optnext = NULL;
    333 	INTON;
    334 	return 0;
    335 }
    336 
    337 
    338 
    339 /*
    340  * The set command builtin.
    341  */
    342 
    343 int
    344 setcmd(argc, argv)
    345 	int argc;
    346 	char **argv;
    347 {
    348 	if (argc == 1)
    349 		return showvarscmd(argc, argv);
    350 	INTOFF;
    351 	options(0);
    352 	optschanged();
    353 	if (*argptr != NULL) {
    354 		setparam(argptr);
    355 	}
    356 	INTON;
    357 	return 0;
    358 }
    359 
    360 
    361 void
    362 getoptsreset(value)
    363 	const char *value;
    364 {
    365 	if (number(value) == 1) {
    366 		shellparam.optnext = NULL;
    367 		shellparam.reset = 1;
    368 	}
    369 }
    370 
    371 /*
    372  * The getopts builtin.  Shellparam.optnext points to the next argument
    373  * to be processed.  Shellparam.optptr points to the next character to
    374  * be processed in the current argument.  If shellparam.optnext is NULL,
    375  * then it's the first time getopts has been called.
    376  */
    377 
    378 int
    379 getoptscmd(argc, argv)
    380 	int argc;
    381 	char **argv;
    382 {
    383 	char **optbase;
    384 
    385 	if (argc < 3)
    386 		error("Usage: getopts optstring var [arg]");
    387 	else if (argc == 3)
    388 		optbase = shellparam.p;
    389 	else
    390 		optbase = &argv[3];
    391 
    392 	if (shellparam.reset == 1) {
    393 		shellparam.optnext = optbase;
    394 		shellparam.optptr = NULL;
    395 		shellparam.reset = 0;
    396 	}
    397 
    398 	return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
    399 		       &shellparam.optptr);
    400 }
    401 
    402 STATIC int
    403 getopts(optstr, optvar, optfirst, optnext, optptr)
    404 	char *optstr;
    405 	char *optvar;
    406 	char **optfirst;
    407 	char ***optnext;
    408 	char **optptr;
    409 {
    410 	char *p, *q;
    411 	char c = '?';
    412 	int done = 0;
    413 	int ind = 0;
    414 	int err = 0;
    415 	char s[10];
    416 
    417 	if ((p = *optptr) == NULL || *p == '\0') {
    418 		/* Current word is done, advance */
    419 		if (*optnext == NULL)
    420 			return 1;
    421 		p = **optnext;
    422 		if (p == NULL || *p != '-' || *++p == '\0') {
    423 atend:
    424 			ind = *optnext - optfirst + 1;
    425 			*optnext = NULL;
    426 			p = NULL;
    427 			done = 1;
    428 			goto out;
    429 		}
    430 		(*optnext)++;
    431 		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
    432 			goto atend;
    433 	}
    434 
    435 	c = *p++;
    436 	for (q = optstr; *q != c; ) {
    437 		if (*q == '\0') {
    438 			if (optstr[0] == ':') {
    439 				s[0] = c;
    440 				s[1] = '\0';
    441 				err |= setvarsafe("OPTARG", s, 0);
    442 			}
    443 			else {
    444 				outfmt(&errout, "Illegal option -%c\n", c);
    445 				(void) unsetvar("OPTARG");
    446 			}
    447 			c = '?';
    448 			goto bad;
    449 		}
    450 		if (*++q == ':')
    451 			q++;
    452 	}
    453 
    454 	if (*++q == ':') {
    455 		if (*p == '\0' && (p = **optnext) == NULL) {
    456 			if (optstr[0] == ':') {
    457 				s[0] = c;
    458 				s[1] = '\0';
    459 				err |= setvarsafe("OPTARG", s, 0);
    460 				c = ':';
    461 			}
    462 			else {
    463 				outfmt(&errout, "No arg for -%c option\n", c);
    464 				(void) unsetvar("OPTARG");
    465 				c = '?';
    466 			}
    467 			goto bad;
    468 		}
    469 
    470 		if (p == **optnext)
    471 			(*optnext)++;
    472 		setvarsafe("OPTARG", p, 0);
    473 		p = NULL;
    474 	}
    475 	else
    476 		setvarsafe("OPTARG", "", 0);
    477 	ind = *optnext - optfirst + 1;
    478 	goto out;
    479 
    480 bad:
    481 	ind = 1;
    482 	*optnext = NULL;
    483 	p = NULL;
    484 out:
    485 	*optptr = p;
    486 	fmtstr(s, sizeof(s), "%d", ind);
    487 	err |= setvarsafe("OPTIND", s, VNOFUNC);
    488 	s[0] = c;
    489 	s[1] = '\0';
    490 	err |= setvarsafe(optvar, s, 0);
    491 	if (err) {
    492 		*optnext = NULL;
    493 		*optptr = NULL;
    494 		flushall();
    495 		exraise(EXERROR);
    496 	}
    497 	return done;
    498 }
    499 
    500 /*
    501  * XXX - should get rid of.  have all builtins use getopt(3).  the
    502  * library getopt must have the BSD extension static variable "optreset"
    503  * otherwise it can't be used within the shell safely.
    504  *
    505  * Standard option processing (a la getopt) for builtin routines.  The
    506  * only argument that is passed to nextopt is the option string; the
    507  * other arguments are unnecessary.  It return the character, or '\0' on
    508  * end of input.
    509  */
    510 
    511 int
    512 nextopt(optstring)
    513 	char *optstring;
    514 	{
    515 	char *p, *q;
    516 	char c;
    517 
    518 	if ((p = optptr) == NULL || *p == '\0') {
    519 		p = *argptr;
    520 		if (p == NULL || *p != '-' || *++p == '\0')
    521 			return '\0';
    522 		argptr++;
    523 		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
    524 			return '\0';
    525 	}
    526 	c = *p++;
    527 	for (q = optstring ; *q != c ; ) {
    528 		if (*q == '\0')
    529 			error("Illegal option -%c", c);
    530 		if (*++q == ':')
    531 			q++;
    532 	}
    533 	if (*++q == ':') {
    534 		if (*p == '\0' && (p = *argptr++) == NULL)
    535 			error("No arg for -%c option", c);
    536 		optarg = p;
    537 		p = NULL;
    538 	}
    539 	optptr = p;
    540 	return c;
    541 }
    542