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