options.c revision 215567
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 215567 2010-11-20 14:14:52Z jilles $");
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) */
67201053Sjilleschar *nextopt_optptr;		/* used by nextopt */
681556Srgrimes
691556Srgrimeschar *minusc;			/* argument to -c option */
701556Srgrimes
711556Srgrimes
72213811Sobrienstatic void options(int);
73213811Sobrienstatic void minus_o(char *, int);
74213811Sobrienstatic void setoption(int, int);
75213811Sobrienstatic 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;
96206182Sjilles	if (iflag != 0 && sflag == 1 && isatty(0) && isatty(1)) {
971556Srgrimes		iflag = 1;
98206182Sjilles		if (Eflag == 2)
99206182Sjilles			Eflag = 1;
100206182Sjilles	}
1011556Srgrimes	if (mflag == 2)
1021556Srgrimes		mflag = iflag;
1031556Srgrimes	for (i = 0; i < NOPTS; i++)
1041556Srgrimes		if (optlist[i].val == 2)
1051556Srgrimes			optlist[i].val = 0;
1061556Srgrimes	arg0 = argv[0];
1071556Srgrimes	if (sflag == 0 && minusc == NULL) {
1081556Srgrimes		commandname = arg0 = *argptr++;
1091556Srgrimes		setinputfile(commandname, 0);
1101556Srgrimes	}
11120425Ssteve	/* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
11225227Ssteve	if (argptr && minusc && *argptr)
11311111Sjoerg		arg0 = *argptr++;
11420742Ssteve
1151556Srgrimes	shellparam.p = argptr;
11620742Ssteve	shellparam.reset = 1;
1171556Srgrimes	/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
1181556Srgrimes	while (*argptr) {
1191556Srgrimes		shellparam.nparam++;
1201556Srgrimes		argptr++;
1211556Srgrimes	}
1221556Srgrimes	optschanged();
1231556Srgrimes}
1241556Srgrimes
1251556Srgrimes
12617987Spetervoid
12790111Simpoptschanged(void)
12817987Speter{
1291556Srgrimes	setinteractive(iflag);
13017987Speter#ifndef NO_HISTORY
1311556Srgrimes	histedit();
13217987Speter#endif
1331556Srgrimes	setjobctl(mflag);
1341556Srgrimes}
1351556Srgrimes
1361556Srgrimes/*
1371556Srgrimes * Process shell options.  The global variable argptr contains a pointer
1381556Srgrimes * to the argument list; we advance it past the options.
1391556Srgrimes */
1401556Srgrimes
141213811Sobrienstatic void
14290111Simpoptions(int cmdline)
14317987Speter{
144146255Sgad	char *kp, *p;
1451556Srgrimes	int val;
1461556Srgrimes	int c;
1471556Srgrimes
1481556Srgrimes	if (cmdline)
1491556Srgrimes		minusc = NULL;
1501556Srgrimes	while ((p = *argptr) != NULL) {
1511556Srgrimes		argptr++;
1521556Srgrimes		if ((c = *p++) == '-') {
1531556Srgrimes			val = 1;
154141962Sgad			/* A "-" or  "--" terminates options */
155141962Sgad			if (p[0] == '\0')
156141962Sgad				goto end_options1;
157141962Sgad			if (p[0] == '-' && p[1] == '\0')
158141962Sgad				goto end_options2;
159146255Sgad			/**
160146255Sgad			 * For the benefit of `#!' lines in shell scripts,
161146255Sgad			 * treat a string of '-- *#.*' the same as '--'.
162146255Sgad			 * This is needed so that a script starting with:
163146255Sgad			 *	#!/bin/sh -- # -*- perl -*-
164146255Sgad			 * will continue to work after a change is made to
165146255Sgad			 * kern/imgact_shell.c to NOT token-ize the options
166146255Sgad			 * specified on a '#!' line.  A bit of a kludge,
167146255Sgad			 * but that trick is recommended in documentation
168146255Sgad			 * for some scripting languages, and we might as
169146255Sgad			 * well continue to support it.
170146255Sgad			 */
171146255Sgad			if (p[0] == '-') {
172146255Sgad				kp = p + 1;
173146255Sgad				while (*kp == ' ' || *kp == '\t')
174146255Sgad					kp++;
175146255Sgad				if (*kp == '#' || *kp == '\0')
176146255Sgad					goto end_options2;
177146255Sgad			}
1781556Srgrimes		} else if (c == '+') {
1791556Srgrimes			val = 0;
1801556Srgrimes		} else {
1811556Srgrimes			argptr--;
1821556Srgrimes			break;
1831556Srgrimes		}
1841556Srgrimes		while ((c = *p++) != '\0') {
1851556Srgrimes			if (c == 'c' && cmdline) {
1861556Srgrimes				char *q;
1871556Srgrimes#ifdef NOHACK	/* removing this code allows sh -ce 'foo' for compat */
1881556Srgrimes				if (*p == '\0')
1891556Srgrimes#endif
1901556Srgrimes					q = *argptr++;
1911556Srgrimes				if (q == NULL || minusc != NULL)
1921556Srgrimes					error("Bad -c option");
1931556Srgrimes				minusc = q;
1941556Srgrimes#ifdef NOHACK
1951556Srgrimes				break;
1961556Srgrimes#endif
1971556Srgrimes			} else if (c == 'o') {
1981556Srgrimes				minus_o(*argptr, val);
1991556Srgrimes				if (*argptr)
2001556Srgrimes					argptr++;
2011556Srgrimes			} else {
20219240Ssteve				if (c == 'p' && !val && privileged) {
20319240Ssteve					(void) setuid(getuid());
20419240Ssteve					(void) setgid(getgid());
20519240Ssteve				}
2061556Srgrimes				setoption(c, val);
2071556Srgrimes			}
2081556Srgrimes		}
2091556Srgrimes	}
210141962Sgad	return;
211141962Sgad
212141962Sgad	/* When processing `set', a single "-" means turn off -x and -v */
213141962Sgadend_options1:
214141962Sgad	if (!cmdline) {
215141962Sgad		xflag = vflag = 0;
216141962Sgad		return;
217141962Sgad	}
218141962Sgad
219141962Sgad	/*
220141962Sgad	 * When processing `set', a "--" means the remaining arguments
221141962Sgad	 * replace the positional parameters in the active shell.  If
222141962Sgad	 * there are no remaining options, then all the positional
223141962Sgad	 * parameters are cleared (equivalent to doing ``shift $#'').
224141962Sgad	 */
225141962Sgadend_options2:
226141962Sgad	if (!cmdline) {
227141962Sgad		if (*argptr == NULL)
228141962Sgad			setparam(argptr);
229141962Sgad		return;
230141962Sgad	}
231141962Sgad
232141962Sgad	/*
233141962Sgad	 * At this point we are processing options given to 'sh' on a command
234141962Sgad	 * line.  If an end-of-options marker ("-" or "--") is followed by an
235141962Sgad	 * arg of "#", then skip over all remaining arguments.  Some scripting
236141962Sgad	 * languages (e.g.: perl) document that /bin/sh will implement this
237141962Sgad	 * behavior, and they recommend that users take advantage of it to
238141962Sgad	 * solve certain issues that can come up when writing a perl script.
239141962Sgad	 * Yes, this feature is in /bin/sh to help users write perl scripts.
240141962Sgad	 */
241141962Sgad	p = *argptr;
242141962Sgad	if (p != NULL && p[0] == '#' && p[1] == '\0') {
243141962Sgad		while (*argptr != NULL)
244141962Sgad			argptr++;
245141962Sgad		/* We need to keep the final argument */
246141962Sgad		argptr--;
247141962Sgad	}
2481556Srgrimes}
2491556Srgrimes
250213811Sobrienstatic void
25190111Simpminus_o(char *name, int val)
2521556Srgrimes{
253151866Sstefanf	int i;
2541556Srgrimes
2551556Srgrimes	if (name == NULL) {
25697276Stjr		if (val) {
25797276Stjr			/* "Pretty" output. */
25897276Stjr			out1str("Current option settings\n");
25997276Stjr			for (i = 0; i < NOPTS; i++)
26097276Stjr				out1fmt("%-16s%s\n", optlist[i].name,
26197276Stjr					optlist[i].val ? "on" : "off");
26297276Stjr		} else {
26397276Stjr			/* Output suitable for re-input to shell. */
264215567Sjilles			for (i = 0; i < NOPTS; i++)
265215567Sjilles				out1fmt("%s %co %s%s",
266215567Sjilles				    i % 6 == 0 ? "set" : "",
267215567Sjilles				    optlist[i].val ? '-' : '+',
268215567Sjilles				    optlist[i].name,
269215567Sjilles				    i % 6 == 5 || i == NOPTS - 1 ? "\n" : "");
27097276Stjr		}
2711556Srgrimes	} else {
2721556Srgrimes		for (i = 0; i < NOPTS; i++)
2731556Srgrimes			if (equal(name, optlist[i].name)) {
27419240Ssteve				if (!val && privileged && equal(name, "privileged")) {
27519240Ssteve					(void) setuid(getuid());
27619240Ssteve					(void) setgid(getgid());
27719240Ssteve				}
2781556Srgrimes				setoption(optlist[i].letter, val);
2791556Srgrimes				return;
2801556Srgrimes			}
2811556Srgrimes		error("Illegal option -o %s", name);
2821556Srgrimes	}
2831556Srgrimes}
2841556Srgrimes
2858855Srgrimes
286213811Sobrienstatic void
28790111Simpsetoption(int flag, int val)
28890111Simp{
2891556Srgrimes	int i;
2901556Srgrimes
2911556Srgrimes	for (i = 0; i < NOPTS; i++)
2921556Srgrimes		if (optlist[i].letter == flag) {
2931556Srgrimes			optlist[i].val = val;
2941556Srgrimes			if (val) {
2951556Srgrimes				/* #%$ hack for ksh semantics */
2961556Srgrimes				if (flag == 'V')
2971556Srgrimes					Eflag = 0;
2981556Srgrimes				else if (flag == 'E')
2991556Srgrimes					Vflag = 0;
3001556Srgrimes			}
3011556Srgrimes			return;
3021556Srgrimes		}
3031556Srgrimes	error("Illegal option -%c", flag);
3041556Srgrimes}
3051556Srgrimes
3061556Srgrimes
3071556Srgrimes
3081556Srgrimes#ifdef mkinit
3091556SrgrimesINCLUDE "options.h"
3101556Srgrimes
3111556SrgrimesSHELLPROC {
3121556Srgrimes	int i;
3131556Srgrimes
3141556Srgrimes	for (i = 0; i < NOPTS; i++)
3151556Srgrimes		optlist[i].val = 0;
3161556Srgrimes	optschanged();
3171556Srgrimes
3181556Srgrimes}
3191556Srgrimes#endif
3201556Srgrimes
3211556Srgrimes
3221556Srgrimes/*
3231556Srgrimes * Set the shell parameters.
3241556Srgrimes */
3251556Srgrimes
3261556Srgrimesvoid
32790111Simpsetparam(char **argv)
32890111Simp{
3291556Srgrimes	char **newparam;
3301556Srgrimes	char **ap;
3311556Srgrimes	int nparam;
3321556Srgrimes
3331556Srgrimes	for (nparam = 0 ; argv[nparam] ; nparam++);
3341556Srgrimes	ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
3351556Srgrimes	while (*argv) {
3361556Srgrimes		*ap++ = savestr(*argv++);
3371556Srgrimes	}
3381556Srgrimes	*ap = NULL;
3391556Srgrimes	freeparam(&shellparam);
3401556Srgrimes	shellparam.malloc = 1;
3411556Srgrimes	shellparam.nparam = nparam;
3421556Srgrimes	shellparam.p = newparam;
343182300Sstefanf	shellparam.reset = 1;
3441556Srgrimes	shellparam.optnext = NULL;
3451556Srgrimes}
3461556Srgrimes
3471556Srgrimes
3481556Srgrimes/*
3491556Srgrimes * Free the list of positional parameters.
3501556Srgrimes */
3511556Srgrimes
3521556Srgrimesvoid
35390111Simpfreeparam(struct shparam *param)
35490111Simp{
3551556Srgrimes	char **ap;
3561556Srgrimes
3571556Srgrimes	if (param->malloc) {
3581556Srgrimes		for (ap = param->p ; *ap ; ap++)
3591556Srgrimes			ckfree(*ap);
3601556Srgrimes		ckfree(param->p);
3611556Srgrimes	}
3621556Srgrimes}
3631556Srgrimes
3641556Srgrimes
3651556Srgrimes
3661556Srgrimes/*
3671556Srgrimes * The shift builtin command.
3681556Srgrimes */
3691556Srgrimes
37017987Speterint
37190111Simpshiftcmd(int argc, char **argv)
37217987Speter{
3731556Srgrimes	int n;
3741556Srgrimes	char **ap1, **ap2;
3751556Srgrimes
3761556Srgrimes	n = 1;
3771556Srgrimes	if (argc > 1)
3781556Srgrimes		n = number(argv[1]);
3791556Srgrimes	if (n > shellparam.nparam)
380157601Sstefanf		return 1;
3811556Srgrimes	INTOFF;
3821556Srgrimes	shellparam.nparam -= n;
3831556Srgrimes	for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
3841556Srgrimes		if (shellparam.malloc)
3851556Srgrimes			ckfree(*ap1);
3861556Srgrimes	}
3871556Srgrimes	ap2 = shellparam.p;
3881556Srgrimes	while ((*ap2++ = *ap1++) != NULL);
389177497Sstefanf	shellparam.reset = 1;
3901556Srgrimes	INTON;
3911556Srgrimes	return 0;
3921556Srgrimes}
3931556Srgrimes
3941556Srgrimes
3951556Srgrimes
3961556Srgrimes/*
3971556Srgrimes * The set command builtin.
3981556Srgrimes */
3991556Srgrimes
40017987Speterint
40190111Simpsetcmd(int argc, char **argv)
40217987Speter{
4031556Srgrimes	if (argc == 1)
4041556Srgrimes		return showvarscmd(argc, argv);
4051556Srgrimes	INTOFF;
4061556Srgrimes	options(0);
4071556Srgrimes	optschanged();
4081556Srgrimes	if (*argptr != NULL) {
4091556Srgrimes		setparam(argptr);
4101556Srgrimes	}
4111556Srgrimes	INTON;
4121556Srgrimes	return 0;
4131556Srgrimes}
4141556Srgrimes
4151556Srgrimes
41620425Sstevevoid
41790111Simpgetoptsreset(const char *value)
41820425Ssteve{
41920425Ssteve	if (number(value) == 1) {
42020425Ssteve		shellparam.reset = 1;
42120425Ssteve	}
42220425Ssteve}
42320425Ssteve
4241556Srgrimes/*
4251556Srgrimes * The getopts builtin.  Shellparam.optnext points to the next argument
4261556Srgrimes * to be processed.  Shellparam.optptr points to the next character to
4271556Srgrimes * be processed in the current argument.  If shellparam.optnext is NULL,
4281556Srgrimes * then it's the first time getopts has been called.
4291556Srgrimes */
4301556Srgrimes
43117987Speterint
43290111Simpgetoptscmd(int argc, char **argv)
43317987Speter{
43420425Ssteve	char **optbase = NULL;
43520425Ssteve
43620425Ssteve	if (argc < 3)
43795258Sdes		error("usage: getopts optstring var [arg]");
43820425Ssteve	else if (argc == 3)
43920425Ssteve		optbase = shellparam.p;
44020425Ssteve	else
44120425Ssteve		optbase = &argv[3];
44220425Ssteve
44320425Ssteve	if (shellparam.reset == 1) {
44420425Ssteve		shellparam.optnext = optbase;
44520425Ssteve		shellparam.optptr = NULL;
44620425Ssteve		shellparam.reset = 0;
44720425Ssteve	}
44820425Ssteve
44920425Ssteve	return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
45020425Ssteve		       &shellparam.optptr);
45120425Ssteve}
45220425Ssteve
453213811Sobrienstatic int
45490111Simpgetopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
45590111Simp    char **optptr)
45620425Ssteve{
45725227Ssteve	char *p, *q;
45820425Ssteve	char c = '?';
45920425Ssteve	int done = 0;
46020425Ssteve	int ind = 0;
46120425Ssteve	int err = 0;
4621556Srgrimes	char s[10];
4631556Srgrimes
46420425Ssteve	if ((p = *optptr) == NULL || *p == '\0') {
46520425Ssteve		/* Current word is done, advance */
46620425Ssteve		if (*optnext == NULL)
46720425Ssteve			return 1;
46820425Ssteve		p = **optnext;
4691556Srgrimes		if (p == NULL || *p != '-' || *++p == '\0') {
4701556Srgrimesatend:
47120742Ssteve			ind = *optnext - optfirst + 1;
47220425Ssteve			*optnext = NULL;
47320742Ssteve			p = NULL;
47420425Ssteve			done = 1;
47520425Ssteve			goto out;
4761556Srgrimes		}
47720425Ssteve		(*optnext)++;
4781556Srgrimes		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
4791556Srgrimes			goto atend;
4801556Srgrimes	}
48120425Ssteve
4821556Srgrimes	c = *p++;
48320425Ssteve	for (q = optstr; *q != c; ) {
4841556Srgrimes		if (*q == '\0') {
48520425Ssteve			if (optstr[0] == ':') {
48620425Ssteve				s[0] = c;
48720425Ssteve				s[1] = '\0';
48820425Ssteve				err |= setvarsafe("OPTARG", s, 0);
48920425Ssteve			}
49020425Ssteve			else {
49120425Ssteve				out1fmt("Illegal option -%c\n", c);
49220425Ssteve				(void) unsetvar("OPTARG");
49320425Ssteve			}
4941556Srgrimes			c = '?';
49520425Ssteve			goto bad;
4961556Srgrimes		}
4971556Srgrimes		if (*++q == ':')
4981556Srgrimes			q++;
4991556Srgrimes	}
50020425Ssteve
5011556Srgrimes	if (*++q == ':') {
50220425Ssteve		if (*p == '\0' && (p = **optnext) == NULL) {
50320425Ssteve			if (optstr[0] == ':') {
50420425Ssteve				s[0] = c;
50520425Ssteve				s[1] = '\0';
50620425Ssteve				err |= setvarsafe("OPTARG", s, 0);
50720425Ssteve				c = ':';
50820425Ssteve			}
50920425Ssteve			else {
51020425Ssteve				out1fmt("No arg for -%c option\n", c);
51120425Ssteve				(void) unsetvar("OPTARG");
51220425Ssteve				c = '?';
51320425Ssteve			}
51420425Ssteve			goto bad;
5151556Srgrimes		}
51620425Ssteve
51720425Ssteve		if (p == **optnext)
51820425Ssteve			(*optnext)++;
51920425Ssteve		setvarsafe("OPTARG", p, 0);
5201556Srgrimes		p = NULL;
5211556Srgrimes	}
52220425Ssteve	else
52320425Ssteve		setvarsafe("OPTARG", "", 0);
52420425Ssteve	ind = *optnext - optfirst + 1;
52520425Ssteve	goto out;
52620425Ssteve
52720425Sstevebad:
52820425Ssteve	ind = 1;
52920425Ssteve	*optnext = NULL;
53020425Ssteve	p = NULL;
5311556Srgrimesout:
53220425Ssteve	*optptr = p;
53320425Ssteve	fmtstr(s, sizeof(s), "%d", ind);
53420425Ssteve	err |= setvarsafe("OPTIND", s, VNOFUNC);
5351556Srgrimes	s[0] = c;
5361556Srgrimes	s[1] = '\0';
53720425Ssteve	err |= setvarsafe(optvar, s, 0);
53820425Ssteve	if (err) {
53920425Ssteve		*optnext = NULL;
54020425Ssteve		*optptr = NULL;
54120425Ssteve		flushall();
54220425Ssteve		exraise(EXERROR);
54320425Ssteve	}
54420425Ssteve	return done;
5451556Srgrimes}
5461556Srgrimes
5471556Srgrimes/*
5481556Srgrimes * XXX - should get rid of.  have all builtins use getopt(3).  the
5491556Srgrimes * library getopt must have the BSD extension static variable "optreset"
5501556Srgrimes * otherwise it can't be used within the shell safely.
5511556Srgrimes *
5521556Srgrimes * Standard option processing (a la getopt) for builtin routines.  The
5531556Srgrimes * only argument that is passed to nextopt is the option string; the
5541556Srgrimes * other arguments are unnecessary.  It return the character, or '\0' on
5551556Srgrimes * end of input.
5561556Srgrimes */
5571556Srgrimes
5581556Srgrimesint
559200956Sjillesnextopt(const char *optstring)
56090111Simp{
561200956Sjilles	char *p;
562200956Sjilles	const char *q;
5631556Srgrimes	char c;
5641556Srgrimes
565201053Sjilles	if ((p = nextopt_optptr) == NULL || *p == '\0') {
5661556Srgrimes		p = *argptr;
5671556Srgrimes		if (p == NULL || *p != '-' || *++p == '\0')
5681556Srgrimes			return '\0';
5691556Srgrimes		argptr++;
5701556Srgrimes		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
5711556Srgrimes			return '\0';
5721556Srgrimes	}
5731556Srgrimes	c = *p++;
5741556Srgrimes	for (q = optstring ; *q != c ; ) {
5751556Srgrimes		if (*q == '\0')
5761556Srgrimes			error("Illegal option -%c", c);
5771556Srgrimes		if (*++q == ':')
5781556Srgrimes			q++;
5791556Srgrimes	}
5801556Srgrimes	if (*++q == ':') {
5811556Srgrimes		if (*p == '\0' && (p = *argptr++) == NULL)
5821556Srgrimes			error("No arg for -%c option", c);
58359436Scracauer		shoptarg = p;
5841556Srgrimes		p = NULL;
5851556Srgrimes	}
586201053Sjilles	nextopt_optptr = p;
5871556Srgrimes	return c;
5881556Srgrimes}
589