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