options.c revision 151866
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Kenneth Almquist.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 4. Neither the name of the University nor the names of its contributors
171556Srgrimes *    may be used to endorse or promote products derived from this software
181556Srgrimes *    without specific prior written permission.
191556Srgrimes *
201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301556Srgrimes * SUCH DAMAGE.
311556Srgrimes */
321556Srgrimes
331556Srgrimes#ifndef lint
3436150Scharnier#if 0
3536150Scharnierstatic char sccsid[] = "@(#)options.c	8.2 (Berkeley) 5/4/95";
3636150Scharnier#endif
371556Srgrimes#endif /* not lint */
3899110Sobrien#include <sys/cdefs.h>
3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/options.c 151866 2005-10-29 18:41:35Z stefanf $");
401556Srgrimes
4117987Speter#include <signal.h>
4217987Speter#include <unistd.h>
4317987Speter#include <stdlib.h>
4417987Speter
451556Srgrimes#include "shell.h"
461556Srgrimes#define DEFINE_OPTIONS
471556Srgrimes#include "options.h"
481556Srgrimes#undef DEFINE_OPTIONS
491556Srgrimes#include "nodes.h"	/* for other header files */
501556Srgrimes#include "eval.h"
511556Srgrimes#include "jobs.h"
521556Srgrimes#include "input.h"
531556Srgrimes#include "output.h"
541556Srgrimes#include "trap.h"
551556Srgrimes#include "var.h"
561556Srgrimes#include "memalloc.h"
571556Srgrimes#include "error.h"
581556Srgrimes#include "mystring.h"
5917987Speter#ifndef NO_HISTORY
6017987Speter#include "myhistedit.h"
6117987Speter#endif
621556Srgrimes
631556Srgrimeschar *arg0;			/* value of $0 */
641556Srgrimesstruct shparam shellparam;	/* current positional parameters */
651556Srgrimeschar **argptr;			/* argument list for builtin commands */
6659436Scracauerchar *shoptarg;			/* set by nextopt (like getopt) */
671556Srgrimeschar *optptr;			/* used by nextopt */
681556Srgrimes
691556Srgrimeschar *minusc;			/* argument to -c option */
701556Srgrimes
711556Srgrimes
7290111SimpSTATIC void options(int);
7390111SimpSTATIC void minus_o(char *, int);
7490111SimpSTATIC void setoption(int, int);
7590111SimpSTATIC int getopts(char *, char *, char **, char ***, char **);
761556Srgrimes
771556Srgrimes
781556Srgrimes/*
791556Srgrimes * Process the shell command line arguments.
801556Srgrimes */
811556Srgrimes
821556Srgrimesvoid
8390111Simpprocargs(int argc, char **argv)
8417987Speter{
851556Srgrimes	int i;
861556Srgrimes
871556Srgrimes	argptr = argv;
881556Srgrimes	if (argc > 0)
891556Srgrimes		argptr++;
901556Srgrimes	for (i = 0; i < NOPTS; i++)
911556Srgrimes		optlist[i].val = 2;
9219240Ssteve	privileged = (getuid() != geteuid() || getgid() != getegid());
931556Srgrimes	options(1);
941556Srgrimes	if (*argptr == NULL && minusc == NULL)
951556Srgrimes		sflag = 1;
961556Srgrimes	if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
971556Srgrimes		iflag = 1;
981556Srgrimes	if (mflag == 2)
991556Srgrimes		mflag = iflag;
1001556Srgrimes	for (i = 0; i < NOPTS; i++)
1011556Srgrimes		if (optlist[i].val == 2)
1021556Srgrimes			optlist[i].val = 0;
1031556Srgrimes	arg0 = argv[0];
1041556Srgrimes	if (sflag == 0 && minusc == NULL) {
1051556Srgrimes		commandname = arg0 = *argptr++;
1061556Srgrimes		setinputfile(commandname, 0);
1071556Srgrimes	}
10820425Ssteve	/* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
10925227Ssteve	if (argptr && minusc && *argptr)
11011111Sjoerg		arg0 = *argptr++;
11120742Ssteve
1121556Srgrimes	shellparam.p = argptr;
11320742Ssteve	shellparam.reset = 1;
1141556Srgrimes	/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
1151556Srgrimes	while (*argptr) {
1161556Srgrimes		shellparam.nparam++;
1171556Srgrimes		argptr++;
1181556Srgrimes	}
1191556Srgrimes	optschanged();
1201556Srgrimes}
1211556Srgrimes
1221556Srgrimes
12317987Spetervoid
12490111Simpoptschanged(void)
12517987Speter{
1261556Srgrimes	setinteractive(iflag);
12717987Speter#ifndef NO_HISTORY
1281556Srgrimes	histedit();
12917987Speter#endif
1301556Srgrimes	setjobctl(mflag);
1311556Srgrimes}
1321556Srgrimes
1331556Srgrimes/*
1341556Srgrimes * Process shell options.  The global variable argptr contains a pointer
1351556Srgrimes * to the argument list; we advance it past the options.
1361556Srgrimes */
1371556Srgrimes
1381556SrgrimesSTATIC void
13990111Simpoptions(int cmdline)
14017987Speter{
141146255Sgad	char *kp, *p;
1421556Srgrimes	int val;
1431556Srgrimes	int c;
1441556Srgrimes
1451556Srgrimes	if (cmdline)
1461556Srgrimes		minusc = NULL;
1471556Srgrimes	while ((p = *argptr) != NULL) {
1481556Srgrimes		argptr++;
1491556Srgrimes		if ((c = *p++) == '-') {
1501556Srgrimes			val = 1;
151141962Sgad			/* A "-" or  "--" terminates options */
152141962Sgad			if (p[0] == '\0')
153141962Sgad				goto end_options1;
154141962Sgad			if (p[0] == '-' && p[1] == '\0')
155141962Sgad				goto end_options2;
156146255Sgad			/**
157146255Sgad			 * For the benefit of `#!' lines in shell scripts,
158146255Sgad			 * treat a string of '-- *#.*' the same as '--'.
159146255Sgad			 * This is needed so that a script starting with:
160146255Sgad			 *	#!/bin/sh -- # -*- perl -*-
161146255Sgad			 * will continue to work after a change is made to
162146255Sgad			 * kern/imgact_shell.c to NOT token-ize the options
163146255Sgad			 * specified on a '#!' line.  A bit of a kludge,
164146255Sgad			 * but that trick is recommended in documentation
165146255Sgad			 * for some scripting languages, and we might as
166146255Sgad			 * well continue to support it.
167146255Sgad			 */
168146255Sgad			if (p[0] == '-') {
169146255Sgad				kp = p + 1;
170146255Sgad				while (*kp == ' ' || *kp == '\t')
171146255Sgad					kp++;
172146255Sgad				if (*kp == '#' || *kp == '\0')
173146255Sgad					goto end_options2;
174146255Sgad			}
1751556Srgrimes		} else if (c == '+') {
1761556Srgrimes			val = 0;
1771556Srgrimes		} else {
1781556Srgrimes			argptr--;
1791556Srgrimes			break;
1801556Srgrimes		}
1811556Srgrimes		while ((c = *p++) != '\0') {
1821556Srgrimes			if (c == 'c' && cmdline) {
1831556Srgrimes				char *q;
1841556Srgrimes#ifdef NOHACK	/* removing this code allows sh -ce 'foo' for compat */
1851556Srgrimes				if (*p == '\0')
1861556Srgrimes#endif
1871556Srgrimes					q = *argptr++;
1881556Srgrimes				if (q == NULL || minusc != NULL)
1891556Srgrimes					error("Bad -c option");
1901556Srgrimes				minusc = q;
1911556Srgrimes#ifdef NOHACK
1921556Srgrimes				break;
1931556Srgrimes#endif
1941556Srgrimes			} else if (c == 'o') {
1951556Srgrimes				minus_o(*argptr, val);
1961556Srgrimes				if (*argptr)
1971556Srgrimes					argptr++;
1981556Srgrimes			} else {
19919240Ssteve				if (c == 'p' && !val && privileged) {
20019240Ssteve					(void) setuid(getuid());
20119240Ssteve					(void) setgid(getgid());
20219240Ssteve				}
2031556Srgrimes				setoption(c, val);
2041556Srgrimes			}
2051556Srgrimes		}
2061556Srgrimes	}
207141962Sgad	return;
208141962Sgad
209141962Sgad	/* When processing `set', a single "-" means turn off -x and -v */
210141962Sgadend_options1:
211141962Sgad	if (!cmdline) {
212141962Sgad		xflag = vflag = 0;
213141962Sgad		return;
214141962Sgad	}
215141962Sgad
216141962Sgad	/*
217141962Sgad	 * When processing `set', a "--" means the remaining arguments
218141962Sgad	 * replace the positional parameters in the active shell.  If
219141962Sgad	 * there are no remaining options, then all the positional
220141962Sgad	 * parameters are cleared (equivalent to doing ``shift $#'').
221141962Sgad	 */
222141962Sgadend_options2:
223141962Sgad	if (!cmdline) {
224141962Sgad		if (*argptr == NULL)
225141962Sgad			setparam(argptr);
226141962Sgad		return;
227141962Sgad	}
228141962Sgad
229141962Sgad	/*
230141962Sgad	 * At this point we are processing options given to 'sh' on a command
231141962Sgad	 * line.  If an end-of-options marker ("-" or "--") is followed by an
232141962Sgad	 * arg of "#", then skip over all remaining arguments.  Some scripting
233141962Sgad	 * languages (e.g.: perl) document that /bin/sh will implement this
234141962Sgad	 * behavior, and they recommend that users take advantage of it to
235141962Sgad	 * solve certain issues that can come up when writing a perl script.
236141962Sgad	 * Yes, this feature is in /bin/sh to help users write perl scripts.
237141962Sgad	 */
238141962Sgad	p = *argptr;
239141962Sgad	if (p != NULL && p[0] == '#' && p[1] == '\0') {
240141962Sgad		while (*argptr != NULL)
241141962Sgad			argptr++;
242141962Sgad		/* We need to keep the final argument */
243141962Sgad		argptr--;
244141962Sgad	}
2451556Srgrimes}
2461556Srgrimes
2471556SrgrimesSTATIC void
24890111Simpminus_o(char *name, int val)
2491556Srgrimes{
250151866Sstefanf	int i;
2511556Srgrimes
2521556Srgrimes	if (name == NULL) {
25397276Stjr		if (val) {
25497276Stjr			/* "Pretty" output. */
25597276Stjr			out1str("Current option settings\n");
25697276Stjr			for (i = 0; i < NOPTS; i++)
25797276Stjr				out1fmt("%-16s%s\n", optlist[i].name,
25897276Stjr					optlist[i].val ? "on" : "off");
25997276Stjr		} else {
26097276Stjr			/* Output suitable for re-input to shell. */
261151866Sstefanf			for (i = 0; i < NOPTS; i++) {
262151866Sstefanf				if (i % 6 == 0)
263151866Sstefanf					out1str(i == 0 ? "set" : "\nset");
264151866Sstefanf				out1fmt(" %co %s", optlist[i].val ? '-' : '+',
265151866Sstefanf					optlist[i].name);
266151866Sstefanf			}
267151866Sstefanf			out1c('\n');
26897276Stjr		}
2691556Srgrimes	} else {
2701556Srgrimes		for (i = 0; i < NOPTS; i++)
2711556Srgrimes			if (equal(name, optlist[i].name)) {
27219240Ssteve				if (!val && privileged && equal(name, "privileged")) {
27319240Ssteve					(void) setuid(getuid());
27419240Ssteve					(void) setgid(getgid());
27519240Ssteve				}
2761556Srgrimes				setoption(optlist[i].letter, val);
2771556Srgrimes				return;
2781556Srgrimes			}
2791556Srgrimes		error("Illegal option -o %s", name);
2801556Srgrimes	}
2811556Srgrimes}
2821556Srgrimes
2838855Srgrimes
2841556SrgrimesSTATIC void
28590111Simpsetoption(int flag, int val)
28690111Simp{
2871556Srgrimes	int i;
2881556Srgrimes
2891556Srgrimes	for (i = 0; i < NOPTS; i++)
2901556Srgrimes		if (optlist[i].letter == flag) {
2911556Srgrimes			optlist[i].val = val;
2921556Srgrimes			if (val) {
2931556Srgrimes				/* #%$ hack for ksh semantics */
2941556Srgrimes				if (flag == 'V')
2951556Srgrimes					Eflag = 0;
2961556Srgrimes				else if (flag == 'E')
2971556Srgrimes					Vflag = 0;
2981556Srgrimes			}
2991556Srgrimes			return;
3001556Srgrimes		}
3011556Srgrimes	error("Illegal option -%c", flag);
3021556Srgrimes}
3031556Srgrimes
3041556Srgrimes
3051556Srgrimes
3061556Srgrimes#ifdef mkinit
3071556SrgrimesINCLUDE "options.h"
3081556Srgrimes
3091556SrgrimesSHELLPROC {
3101556Srgrimes	int i;
3111556Srgrimes
3121556Srgrimes	for (i = 0; i < NOPTS; i++)
3131556Srgrimes		optlist[i].val = 0;
3141556Srgrimes	optschanged();
3151556Srgrimes
3161556Srgrimes}
3171556Srgrimes#endif
3181556Srgrimes
3191556Srgrimes
3201556Srgrimes/*
3211556Srgrimes * Set the shell parameters.
3221556Srgrimes */
3231556Srgrimes
3241556Srgrimesvoid
32590111Simpsetparam(char **argv)
32690111Simp{
3271556Srgrimes	char **newparam;
3281556Srgrimes	char **ap;
3291556Srgrimes	int nparam;
3301556Srgrimes
3311556Srgrimes	for (nparam = 0 ; argv[nparam] ; nparam++);
3321556Srgrimes	ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
3331556Srgrimes	while (*argv) {
3341556Srgrimes		*ap++ = savestr(*argv++);
3351556Srgrimes	}
3361556Srgrimes	*ap = NULL;
3371556Srgrimes	freeparam(&shellparam);
3381556Srgrimes	shellparam.malloc = 1;
3391556Srgrimes	shellparam.nparam = nparam;
3401556Srgrimes	shellparam.p = newparam;
3411556Srgrimes	shellparam.optnext = NULL;
3421556Srgrimes}
3431556Srgrimes
3441556Srgrimes
3451556Srgrimes/*
3461556Srgrimes * Free the list of positional parameters.
3471556Srgrimes */
3481556Srgrimes
3491556Srgrimesvoid
35090111Simpfreeparam(struct shparam *param)
35190111Simp{
3521556Srgrimes	char **ap;
3531556Srgrimes
3541556Srgrimes	if (param->malloc) {
3551556Srgrimes		for (ap = param->p ; *ap ; ap++)
3561556Srgrimes			ckfree(*ap);
3571556Srgrimes		ckfree(param->p);
3581556Srgrimes	}
3591556Srgrimes}
3601556Srgrimes
3611556Srgrimes
3621556Srgrimes
3631556Srgrimes/*
3641556Srgrimes * The shift builtin command.
3651556Srgrimes */
3661556Srgrimes
36717987Speterint
36890111Simpshiftcmd(int argc, char **argv)
36917987Speter{
3701556Srgrimes	int n;
3711556Srgrimes	char **ap1, **ap2;
3721556Srgrimes
3731556Srgrimes	n = 1;
3741556Srgrimes	if (argc > 1)
3751556Srgrimes		n = number(argv[1]);
3761556Srgrimes	if (n > shellparam.nparam)
3771556Srgrimes		error("can't shift that many");
3781556Srgrimes	INTOFF;
3791556Srgrimes	shellparam.nparam -= n;
3801556Srgrimes	for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
3811556Srgrimes		if (shellparam.malloc)
3821556Srgrimes			ckfree(*ap1);
3831556Srgrimes	}
3841556Srgrimes	ap2 = shellparam.p;
3851556Srgrimes	while ((*ap2++ = *ap1++) != NULL);
3861556Srgrimes	shellparam.optnext = NULL;
3871556Srgrimes	INTON;
3881556Srgrimes	return 0;
3891556Srgrimes}
3901556Srgrimes
3911556Srgrimes
3921556Srgrimes
3931556Srgrimes/*
3941556Srgrimes * The set command builtin.
3951556Srgrimes */
3961556Srgrimes
39717987Speterint
39890111Simpsetcmd(int argc, char **argv)
39917987Speter{
4001556Srgrimes	if (argc == 1)
4011556Srgrimes		return showvarscmd(argc, argv);
4021556Srgrimes	INTOFF;
4031556Srgrimes	options(0);
4041556Srgrimes	optschanged();
4051556Srgrimes	if (*argptr != NULL) {
4061556Srgrimes		setparam(argptr);
4071556Srgrimes	}
4081556Srgrimes	INTON;
4091556Srgrimes	return 0;
4101556Srgrimes}
4111556Srgrimes
4121556Srgrimes
41320425Sstevevoid
41490111Simpgetoptsreset(const char *value)
41520425Ssteve{
41620425Ssteve	if (number(value) == 1) {
41720425Ssteve		shellparam.optnext = NULL;
41820425Ssteve		shellparam.reset = 1;
41920425Ssteve	}
42020425Ssteve}
42120425Ssteve
4221556Srgrimes/*
4231556Srgrimes * The getopts builtin.  Shellparam.optnext points to the next argument
4241556Srgrimes * to be processed.  Shellparam.optptr points to the next character to
4251556Srgrimes * be processed in the current argument.  If shellparam.optnext is NULL,
4261556Srgrimes * then it's the first time getopts has been called.
4271556Srgrimes */
4281556Srgrimes
42917987Speterint
43090111Simpgetoptscmd(int argc, char **argv)
43117987Speter{
43220425Ssteve	char **optbase = NULL;
43320425Ssteve
43420425Ssteve	if (argc < 3)
43595258Sdes		error("usage: getopts optstring var [arg]");
43620425Ssteve	else if (argc == 3)
43720425Ssteve		optbase = shellparam.p;
43820425Ssteve	else
43920425Ssteve		optbase = &argv[3];
44020425Ssteve
44120425Ssteve	if (shellparam.reset == 1) {
44220425Ssteve		shellparam.optnext = optbase;
44320425Ssteve		shellparam.optptr = NULL;
44420425Ssteve		shellparam.reset = 0;
44520425Ssteve	}
44620425Ssteve
44720425Ssteve	return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
44820425Ssteve		       &shellparam.optptr);
44920425Ssteve}
45020425Ssteve
45120425SsteveSTATIC int
45290111Simpgetopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
45390111Simp    char **optptr)
45420425Ssteve{
45525227Ssteve	char *p, *q;
45620425Ssteve	char c = '?';
45720425Ssteve	int done = 0;
45820425Ssteve	int ind = 0;
45920425Ssteve	int err = 0;
4601556Srgrimes	char s[10];
4611556Srgrimes
46220425Ssteve	if ((p = *optptr) == NULL || *p == '\0') {
46320425Ssteve		/* Current word is done, advance */
46420425Ssteve		if (*optnext == NULL)
46520425Ssteve			return 1;
46620425Ssteve		p = **optnext;
4671556Srgrimes		if (p == NULL || *p != '-' || *++p == '\0') {
4681556Srgrimesatend:
46920742Ssteve			ind = *optnext - optfirst + 1;
47020425Ssteve			*optnext = NULL;
47120742Ssteve			p = NULL;
47220425Ssteve			done = 1;
47320425Ssteve			goto out;
4741556Srgrimes		}
47520425Ssteve		(*optnext)++;
4761556Srgrimes		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
4771556Srgrimes			goto atend;
4781556Srgrimes	}
47920425Ssteve
4801556Srgrimes	c = *p++;
48120425Ssteve	for (q = optstr; *q != c; ) {
4821556Srgrimes		if (*q == '\0') {
48320425Ssteve			if (optstr[0] == ':') {
48420425Ssteve				s[0] = c;
48520425Ssteve				s[1] = '\0';
48620425Ssteve				err |= setvarsafe("OPTARG", s, 0);
48720425Ssteve			}
48820425Ssteve			else {
48920425Ssteve				out1fmt("Illegal option -%c\n", c);
49020425Ssteve				(void) unsetvar("OPTARG");
49120425Ssteve			}
4921556Srgrimes			c = '?';
49320425Ssteve			goto bad;
4941556Srgrimes		}
4951556Srgrimes		if (*++q == ':')
4961556Srgrimes			q++;
4971556Srgrimes	}
49820425Ssteve
4991556Srgrimes	if (*++q == ':') {
50020425Ssteve		if (*p == '\0' && (p = **optnext) == NULL) {
50120425Ssteve			if (optstr[0] == ':') {
50220425Ssteve				s[0] = c;
50320425Ssteve				s[1] = '\0';
50420425Ssteve				err |= setvarsafe("OPTARG", s, 0);
50520425Ssteve				c = ':';
50620425Ssteve			}
50720425Ssteve			else {
50820425Ssteve				out1fmt("No arg for -%c option\n", c);
50920425Ssteve				(void) unsetvar("OPTARG");
51020425Ssteve				c = '?';
51120425Ssteve			}
51220425Ssteve			goto bad;
5131556Srgrimes		}
51420425Ssteve
51520425Ssteve		if (p == **optnext)
51620425Ssteve			(*optnext)++;
51720425Ssteve		setvarsafe("OPTARG", p, 0);
5181556Srgrimes		p = NULL;
5191556Srgrimes	}
52020425Ssteve	else
52120425Ssteve		setvarsafe("OPTARG", "", 0);
52220425Ssteve	ind = *optnext - optfirst + 1;
52320425Ssteve	goto out;
52420425Ssteve
52520425Sstevebad:
52620425Ssteve	ind = 1;
52720425Ssteve	*optnext = NULL;
52820425Ssteve	p = NULL;
5291556Srgrimesout:
53020425Ssteve	*optptr = p;
53120425Ssteve	fmtstr(s, sizeof(s), "%d", ind);
53220425Ssteve	err |= setvarsafe("OPTIND", s, VNOFUNC);
5331556Srgrimes	s[0] = c;
5341556Srgrimes	s[1] = '\0';
53520425Ssteve	err |= setvarsafe(optvar, s, 0);
53620425Ssteve	if (err) {
53720425Ssteve		*optnext = NULL;
53820425Ssteve		*optptr = NULL;
53920425Ssteve		flushall();
54020425Ssteve		exraise(EXERROR);
54120425Ssteve	}
54220425Ssteve	return done;
5431556Srgrimes}
5441556Srgrimes
5451556Srgrimes/*
5461556Srgrimes * XXX - should get rid of.  have all builtins use getopt(3).  the
5471556Srgrimes * library getopt must have the BSD extension static variable "optreset"
5481556Srgrimes * otherwise it can't be used within the shell safely.
5491556Srgrimes *
5501556Srgrimes * Standard option processing (a la getopt) for builtin routines.  The
5511556Srgrimes * only argument that is passed to nextopt is the option string; the
5521556Srgrimes * other arguments are unnecessary.  It return the character, or '\0' on
5531556Srgrimes * end of input.
5541556Srgrimes */
5551556Srgrimes
5561556Srgrimesint
55790111Simpnextopt(char *optstring)
55890111Simp{
55925227Ssteve	char *p, *q;
5601556Srgrimes	char c;
5611556Srgrimes
5621556Srgrimes	if ((p = optptr) == NULL || *p == '\0') {
5631556Srgrimes		p = *argptr;
5641556Srgrimes		if (p == NULL || *p != '-' || *++p == '\0')
5651556Srgrimes			return '\0';
5661556Srgrimes		argptr++;
5671556Srgrimes		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
5681556Srgrimes			return '\0';
5691556Srgrimes	}
5701556Srgrimes	c = *p++;
5711556Srgrimes	for (q = optstring ; *q != c ; ) {
5721556Srgrimes		if (*q == '\0')
5731556Srgrimes			error("Illegal option -%c", c);
5741556Srgrimes		if (*++q == ':')
5751556Srgrimes			q++;
5761556Srgrimes	}
5771556Srgrimes	if (*++q == ':') {
5781556Srgrimes		if (*p == '\0' && (p = *argptr++) == NULL)
5791556Srgrimes			error("No arg for -%c option", c);
58059436Scracauer		shoptarg = p;
5811556Srgrimes		p = NULL;
5821556Srgrimes	}
5831556Srgrimes	optptr = p;
5841556Srgrimes	return c;
5851556Srgrimes}
586