Home | History | Annotate | Line # | Download | only in sh
options.c revision 1.12
      1 /*	$NetBSD: options.c,v 1.12 1995/03/25 23:45:24 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 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)options.c	8.1 (Berkeley) 5/31/93";
     42 #else
     43 static char rcsid[] = "$NetBSD: options.c,v 1.12 1995/03/25 23:45:24 christos Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 #include "shell.h"
     48 #define DEFINE_OPTIONS
     49 #include "options.h"
     50 #undef DEFINE_OPTIONS
     51 #include "nodes.h"	/* for other header files */
     52 #include "eval.h"
     53 #include "jobs.h"
     54 #include "input.h"
     55 #include "output.h"
     56 #include "trap.h"
     57 #include "var.h"
     58 #include "memalloc.h"
     59 #include "error.h"
     60 #include "mystring.h"
     61 #include "extern.h"
     62 #include <unistd.h>
     63 
     64 char *arg0;			/* value of $0 */
     65 struct shparam shellparam;	/* current positional parameters */
     66 char **argptr;			/* argument list for builtin commands */
     67 char *optarg;			/* set by nextopt (like getopt) */
     68 char *optptr;			/* used by nextopt */
     69 
     70 char *minusc;			/* argument to -c option */
     71 
     72 
     73 #ifdef __STDC__
     74 STATIC void options(int);
     75 STATIC void setoption(int, int);
     76 STATIC void minus_o(char *, int);
     77 #else
     78 STATIC void options();
     79 STATIC void setoption();
     80 STATIC void minus_o();
     81 #endif
     82 
     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 	shellparam.p = argptr;
    117 	/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
    118 	while (*argptr) {
    119 		shellparam.nparam++;
    120 		argptr++;
    121 	}
    122 	optschanged();
    123 }
    124 
    125 
    126 void
    127 optschanged()
    128 {
    129 	setinteractive(iflag);
    130 #ifndef NO_HISTORY
    131 	histedit();
    132 #endif
    133 	setjobctl(mflag);
    134 }
    135 
    136 /*
    137  * Process shell options.  The global variable argptr contains a pointer
    138  * to the argument list; we advance it past the options.
    139  */
    140 
    141 STATIC void
    142 options(cmdline)
    143 	int cmdline;
    144 {
    145 	register char *p;
    146 	int val;
    147 	int c;
    148 
    149 	if (cmdline)
    150 		minusc = NULL;
    151 	while ((p = *argptr) != NULL) {
    152 		argptr++;
    153 		if ((c = *p++) == '-') {
    154 			val = 1;
    155                         if (p[0] == '\0' || p[0] == '-' && p[1] == '\0') {
    156                                 if (!cmdline) {
    157 #if 0	/* No other bourne shell does this! */
    158                                         /* "-" means turn off -x and -v */
    159                                         if (p[0] == '\0')
    160                                                 xflag = vflag = 0;
    161                                         /* "--" means reset params */
    162                                         else
    163 #endif
    164 						if (*argptr == NULL)
    165 							setparam(argptr);
    166                                 }
    167 				break;	  /* "-" or  "--" terminates options */
    168 			}
    169 		} else if (c == '+') {
    170 			val = 0;
    171 		} else {
    172 			argptr--;
    173 			break;
    174 		}
    175 		while ((c = *p++) != '\0') {
    176 			if (c == 'c' && cmdline) {
    177 				char *q;
    178 #ifdef NOHACK	/* removing this code allows sh -ce 'foo' for compat */
    179 				if (*p == '\0')
    180 #endif
    181 					q = *argptr++;
    182 				if (q == NULL || minusc != NULL)
    183 					error("Bad -c option");
    184 				minusc = q;
    185 #ifdef NOHACK
    186 				break;
    187 #endif
    188 			} else if (c == 'o') {
    189 				minus_o(*argptr, val);
    190 				if (*argptr)
    191 					argptr++;
    192 			} else {
    193 				setoption(c, val);
    194 			}
    195 		}
    196 	}
    197 }
    198 
    199 STATIC void
    200 minus_o(name, val)
    201 	char *name;
    202 	int val;
    203 {
    204 	int i;
    205 
    206 	if (name == NULL) {
    207 		out1str("Current option settings\n");
    208 		for (i = 0; i < NOPTS; i++)
    209 			out1fmt("%-16s%s\n", optlist[i].name,
    210 				optlist[i].val ? "on" : "off");
    211 	} else {
    212 		for (i = 0; i < NOPTS; i++)
    213 			if (equal(name, optlist[i].name)) {
    214 				setoption(optlist[i].letter, val);
    215 				return;
    216 			}
    217 		error("Illegal option -o %s", name);
    218 	}
    219 }
    220 
    221 
    222 STATIC void
    223 setoption(flag, val)
    224 	char flag;
    225 	int val;
    226 	{
    227 	int i;
    228 
    229 	for (i = 0; i < NOPTS; i++)
    230 		if (optlist[i].letter == flag) {
    231 			optlist[i].val = val;
    232 			if (val) {
    233 				/* #%$ hack for ksh semantics */
    234 				if (flag == 'V')
    235 					Eflag = 0;
    236 				else if (flag == 'E')
    237 					Vflag = 0;
    238 			}
    239 			return;
    240 		}
    241 	error("Illegal option -%c", flag);
    242 }
    243 
    244 
    245 
    246 #ifdef mkinit
    247 INCLUDE "options.h"
    248 INCLUDE "extern.h"
    249 
    250 SHELLPROC {
    251 	int i;
    252 
    253 	for (i = 0; i < NOPTS; i++)
    254 		optlist[i].val = 0;
    255 	optschanged();
    256 
    257 }
    258 #endif
    259 
    260 
    261 /*
    262  * Set the shell parameters.
    263  */
    264 
    265 void
    266 setparam(argv)
    267 	char **argv;
    268 	{
    269 	char **newparam;
    270 	char **ap;
    271 	int nparam;
    272 
    273 	for (nparam = 0 ; argv[nparam] ; nparam++);
    274 	ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
    275 	while (*argv) {
    276 		*ap++ = savestr(*argv++);
    277 	}
    278 	*ap = NULL;
    279 	freeparam(&shellparam);
    280 	shellparam.malloc = 1;
    281 	shellparam.nparam = nparam;
    282 	shellparam.p = newparam;
    283 	shellparam.optnext = NULL;
    284 }
    285 
    286 
    287 /*
    288  * Free the list of positional parameters.
    289  */
    290 
    291 void
    292 freeparam(param)
    293 	struct shparam *param;
    294 	{
    295 	char **ap;
    296 
    297 	if (param->malloc) {
    298 		for (ap = param->p ; *ap ; ap++)
    299 			ckfree(*ap);
    300 		ckfree(param->p);
    301 	}
    302 }
    303 
    304 
    305 
    306 /*
    307  * The shift builtin command.
    308  */
    309 
    310 int
    311 shiftcmd(argc, argv)
    312 	int argc;
    313 	char **argv;
    314 {
    315 	int n;
    316 	char **ap1, **ap2;
    317 
    318 	n = 1;
    319 	if (argc > 1)
    320 		n = number(argv[1]);
    321 	if (n > shellparam.nparam)
    322 		error("can't shift that many");
    323 	INTOFF;
    324 	shellparam.nparam -= n;
    325 	for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
    326 		if (shellparam.malloc)
    327 			ckfree(*ap1);
    328 	}
    329 	ap2 = shellparam.p;
    330 	while ((*ap2++ = *ap1++) != NULL);
    331 	shellparam.optnext = NULL;
    332 	INTON;
    333 	return 0;
    334 }
    335 
    336 
    337 
    338 /*
    339  * The set command builtin.
    340  */
    341 
    342 int
    343 setcmd(argc, argv)
    344 	int argc;
    345 	char **argv;
    346 {
    347 	if (argc == 1)
    348 		return showvarscmd(argc, argv);
    349 	INTOFF;
    350 	options(0);
    351 	optschanged();
    352 	if (*argptr != NULL) {
    353 		setparam(argptr);
    354 	}
    355 	INTON;
    356 	return 0;
    357 }
    358 
    359 
    360 /*
    361  * The getopts builtin.  Shellparam.optnext points to the next argument
    362  * to be processed.  Shellparam.optptr points to the next character to
    363  * be processed in the current argument.  If shellparam.optnext is NULL,
    364  * then it's the first time getopts has been called.
    365  */
    366 
    367 int
    368 getoptscmd(argc, argv)
    369 	int argc;
    370 	char **argv;
    371 {
    372 	register char *p, *q;
    373 	char c;
    374 	char s[10];
    375 
    376 	if (argc != 3)
    377 		error("Usage: getopts optstring var");
    378 	if (shellparam.optnext == NULL) {
    379 		shellparam.optnext = shellparam.p;
    380 		shellparam.optptr = NULL;
    381 	}
    382 	if ((p = shellparam.optptr) == NULL || *p == '\0') {
    383 		p = *shellparam.optnext;
    384 		if (p == NULL || *p != '-' || *++p == '\0') {
    385 atend:
    386 			fmtstr(s, 10, "%d", shellparam.optnext - shellparam.p + 1);
    387 			setvar("OPTIND", s, 0);
    388 			shellparam.optnext = NULL;
    389 			return 1;
    390 		}
    391 		shellparam.optnext++;
    392 		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
    393 			goto atend;
    394 	}
    395 	c = *p++;
    396 	for (q = argv[1] ; *q != c ; ) {
    397 		if (*q == '\0') {
    398 			out1fmt("Illegal option -%c\n", c);
    399 			c = '?';
    400 			goto out;
    401 		}
    402 		if (*++q == ':')
    403 			q++;
    404 	}
    405 	if (*++q == ':') {
    406 		if (*p == '\0' && (p = *shellparam.optnext) == NULL) {
    407 			out1fmt("No arg for -%c option\n", c);
    408 			c = '?';
    409 			goto out;
    410 		}
    411 		shellparam.optnext++;
    412 		setvar("OPTARG", p, 0);
    413 		p = NULL;
    414 	}
    415 out:
    416 	shellparam.optptr = p;
    417 	s[0] = c;
    418 	s[1] = '\0';
    419 	setvar(argv[2], s, 0);
    420 	return 0;
    421 }
    422 
    423 /*
    424  * XXX - should get rid of.  have all builtins use getopt(3).  the
    425  * library getopt must have the BSD extension static variable "optreset"
    426  * otherwise it can't be used within the shell safely.
    427  *
    428  * Standard option processing (a la getopt) for builtin routines.  The
    429  * only argument that is passed to nextopt is the option string; the
    430  * other arguments are unnecessary.  It return the character, or '\0' on
    431  * end of input.
    432  */
    433 
    434 int
    435 nextopt(optstring)
    436 	char *optstring;
    437 	{
    438 	register char *p, *q;
    439 	char c;
    440 
    441 	if ((p = optptr) == NULL || *p == '\0') {
    442 		p = *argptr;
    443 		if (p == NULL || *p != '-' || *++p == '\0')
    444 			return '\0';
    445 		argptr++;
    446 		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
    447 			return '\0';
    448 	}
    449 	c = *p++;
    450 	for (q = optstring ; *q != c ; ) {
    451 		if (*q == '\0')
    452 			error("Illegal option -%c", c);
    453 		if (*++q == ':')
    454 			q++;
    455 	}
    456 	if (*++q == ':') {
    457 		if (*p == '\0' && (p = *argptr++) == NULL)
    458 			error("No arg for -%c option", c);
    459 		optarg = p;
    460 		p = NULL;
    461 	}
    462 	optptr = p;
    463 	return c;
    464 }
    465