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