xref: /freebsd/bin/sh/options.c (revision d513af6a6628a7d9664fcfb983e5c5f9a6c99a8a)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Kenneth Almquist.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
94b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
104b88c807SRodney W. Grimes  * are met:
114b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
124b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
134b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
144b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
154b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
164b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
174b88c807SRodney W. Grimes  *    must display the following acknowledgement:
184b88c807SRodney W. Grimes  *	This product includes software developed by the University of
194b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
204b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
214b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
224b88c807SRodney W. Grimes  *    without specific prior written permission.
234b88c807SRodney W. Grimes  *
244b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
254b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
264b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
274b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
284b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
294b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
304b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
314b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
324b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
334b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
344b88c807SRodney W. Grimes  * SUCH DAMAGE.
354b88c807SRodney W. Grimes  */
364b88c807SRodney W. Grimes 
374b88c807SRodney W. Grimes #ifndef lint
383d7b5b93SPhilippe Charnier #if 0
393d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)options.c	8.2 (Berkeley) 5/4/95";
403d7b5b93SPhilippe Charnier #endif
413d7b5b93SPhilippe Charnier static const char rcsid[] =
422a456239SPeter Wemm   "$FreeBSD$";
434b88c807SRodney W. Grimes #endif /* not lint */
444b88c807SRodney W. Grimes 
45aa9caaf6SPeter Wemm #include <signal.h>
46aa9caaf6SPeter Wemm #include <unistd.h>
47aa9caaf6SPeter Wemm #include <stdlib.h>
48aa9caaf6SPeter Wemm 
494b88c807SRodney W. Grimes #include "shell.h"
504b88c807SRodney W. Grimes #define DEFINE_OPTIONS
514b88c807SRodney W. Grimes #include "options.h"
524b88c807SRodney W. Grimes #undef DEFINE_OPTIONS
534b88c807SRodney W. Grimes #include "nodes.h"	/* for other header files */
544b88c807SRodney W. Grimes #include "eval.h"
554b88c807SRodney W. Grimes #include "jobs.h"
564b88c807SRodney W. Grimes #include "input.h"
574b88c807SRodney W. Grimes #include "output.h"
584b88c807SRodney W. Grimes #include "trap.h"
594b88c807SRodney W. Grimes #include "var.h"
604b88c807SRodney W. Grimes #include "memalloc.h"
614b88c807SRodney W. Grimes #include "error.h"
624b88c807SRodney W. Grimes #include "mystring.h"
63aa9caaf6SPeter Wemm #ifndef NO_HISTORY
64aa9caaf6SPeter Wemm #include "myhistedit.h"
65aa9caaf6SPeter Wemm #endif
664b88c807SRodney W. Grimes 
674b88c807SRodney W. Grimes char *arg0;			/* value of $0 */
684b88c807SRodney W. Grimes struct shparam shellparam;	/* current positional parameters */
694b88c807SRodney W. Grimes char **argptr;			/* argument list for builtin commands */
70f01e3d0cSMartin Cracauer char *shoptarg;			/* set by nextopt (like getopt) */
714b88c807SRodney W. Grimes char *optptr;			/* used by nextopt */
724b88c807SRodney W. Grimes 
734b88c807SRodney W. Grimes char *minusc;			/* argument to -c option */
744b88c807SRodney W. Grimes 
754b88c807SRodney W. Grimes 
765134c3f7SWarner Losh STATIC void options(int);
775134c3f7SWarner Losh STATIC void minus_o(char *, int);
785134c3f7SWarner Losh STATIC void setoption(int, int);
795134c3f7SWarner Losh STATIC int getopts(char *, char *, char **, char ***, char **);
804b88c807SRodney W. Grimes 
814b88c807SRodney W. Grimes 
824b88c807SRodney W. Grimes /*
834b88c807SRodney W. Grimes  * Process the shell command line arguments.
844b88c807SRodney W. Grimes  */
854b88c807SRodney W. Grimes 
864b88c807SRodney W. Grimes void
875134c3f7SWarner Losh procargs(int argc, char **argv)
884b88c807SRodney W. Grimes {
894b88c807SRodney W. Grimes 	int i;
904b88c807SRodney W. Grimes 
914b88c807SRodney W. Grimes 	argptr = argv;
924b88c807SRodney W. Grimes 	if (argc > 0)
934b88c807SRodney W. Grimes 		argptr++;
944b88c807SRodney W. Grimes 	for (i = 0; i < NOPTS; i++)
954b88c807SRodney W. Grimes 		optlist[i].val = 2;
96621a31c6SSteve Price 	privileged = (getuid() != geteuid() || getgid() != getegid());
974b88c807SRodney W. Grimes 	options(1);
984b88c807SRodney W. Grimes 	if (*argptr == NULL && minusc == NULL)
994b88c807SRodney W. Grimes 		sflag = 1;
1004b88c807SRodney W. Grimes 	if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
1014b88c807SRodney W. Grimes 		iflag = 1;
1024b88c807SRodney W. Grimes 	if (mflag == 2)
1034b88c807SRodney W. Grimes 		mflag = iflag;
1044b88c807SRodney W. Grimes 	for (i = 0; i < NOPTS; i++)
1054b88c807SRodney W. Grimes 		if (optlist[i].val == 2)
1064b88c807SRodney W. Grimes 			optlist[i].val = 0;
1074b88c807SRodney W. Grimes 	arg0 = argv[0];
1084b88c807SRodney W. Grimes 	if (sflag == 0 && minusc == NULL) {
1094b88c807SRodney W. Grimes 		commandname = arg0 = *argptr++;
1104b88c807SRodney W. Grimes 		setinputfile(commandname, 0);
1114b88c807SRodney W. Grimes 	}
112ab0a2172SSteve Price 	/* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
113904a3dc8SSteve Price 	if (argptr && minusc && *argptr)
1146d753bddSJoerg Wunsch 		arg0 = *argptr++;
11593d0e5efSSteve Price 
1164b88c807SRodney W. Grimes 	shellparam.p = argptr;
11793d0e5efSSteve Price 	shellparam.reset = 1;
1184b88c807SRodney W. Grimes 	/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
1194b88c807SRodney W. Grimes 	while (*argptr) {
1204b88c807SRodney W. Grimes 		shellparam.nparam++;
1214b88c807SRodney W. Grimes 		argptr++;
1224b88c807SRodney W. Grimes 	}
1234b88c807SRodney W. Grimes 	optschanged();
1244b88c807SRodney W. Grimes }
1254b88c807SRodney W. Grimes 
1264b88c807SRodney W. Grimes 
127aa9caaf6SPeter Wemm void
1285134c3f7SWarner Losh optschanged(void)
129aa9caaf6SPeter Wemm {
1304b88c807SRodney W. Grimes 	setinteractive(iflag);
131aa9caaf6SPeter Wemm #ifndef NO_HISTORY
1324b88c807SRodney W. Grimes 	histedit();
133aa9caaf6SPeter Wemm #endif
1344b88c807SRodney W. Grimes 	setjobctl(mflag);
1354b88c807SRodney W. Grimes }
1364b88c807SRodney W. Grimes 
1374b88c807SRodney W. Grimes /*
1384b88c807SRodney W. Grimes  * Process shell options.  The global variable argptr contains a pointer
1394b88c807SRodney W. Grimes  * to the argument list; we advance it past the options.
1404b88c807SRodney W. Grimes  */
1414b88c807SRodney W. Grimes 
1424b88c807SRodney W. Grimes STATIC void
1435134c3f7SWarner Losh options(int cmdline)
144aa9caaf6SPeter Wemm {
145904a3dc8SSteve Price 	char *p;
1464b88c807SRodney W. Grimes 	int val;
1474b88c807SRodney W. Grimes 	int c;
1484b88c807SRodney W. Grimes 
1494b88c807SRodney W. Grimes 	if (cmdline)
1504b88c807SRodney W. Grimes 		minusc = NULL;
1514b88c807SRodney W. Grimes 	while ((p = *argptr) != NULL) {
1524b88c807SRodney W. Grimes 		argptr++;
1534b88c807SRodney W. Grimes 		if ((c = *p++) == '-') {
1544b88c807SRodney W. Grimes 			val = 1;
155aa9caaf6SPeter Wemm                         if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
1564b88c807SRodney W. Grimes                                 if (!cmdline) {
1574b88c807SRodney W. Grimes                                         /* "-" means turn off -x and -v */
1584b88c807SRodney W. Grimes                                         if (p[0] == '\0')
1594b88c807SRodney W. Grimes                                                 xflag = vflag = 0;
1604b88c807SRodney W. Grimes                                         /* "--" means reset params */
1614b88c807SRodney W. Grimes                                         else if (*argptr == NULL)
1624b88c807SRodney W. Grimes 						setparam(argptr);
1634b88c807SRodney W. Grimes                                 }
1644b88c807SRodney W. Grimes 				break;	  /* "-" or  "--" terminates options */
1654b88c807SRodney W. Grimes 			}
1664b88c807SRodney W. Grimes 		} else if (c == '+') {
1674b88c807SRodney W. Grimes 			val = 0;
1684b88c807SRodney W. Grimes 		} else {
1694b88c807SRodney W. Grimes 			argptr--;
1704b88c807SRodney W. Grimes 			break;
1714b88c807SRodney W. Grimes 		}
1724b88c807SRodney W. Grimes 		while ((c = *p++) != '\0') {
1734b88c807SRodney W. Grimes 			if (c == 'c' && cmdline) {
1744b88c807SRodney W. Grimes 				char *q;
1754b88c807SRodney W. Grimes #ifdef NOHACK	/* removing this code allows sh -ce 'foo' for compat */
1764b88c807SRodney W. Grimes 				if (*p == '\0')
1774b88c807SRodney W. Grimes #endif
1784b88c807SRodney W. Grimes 					q = *argptr++;
1794b88c807SRodney W. Grimes 				if (q == NULL || minusc != NULL)
1804b88c807SRodney W. Grimes 					error("Bad -c option");
1814b88c807SRodney W. Grimes 				minusc = q;
1824b88c807SRodney W. Grimes #ifdef NOHACK
1834b88c807SRodney W. Grimes 				break;
1844b88c807SRodney W. Grimes #endif
1854b88c807SRodney W. Grimes 			} else if (c == 'o') {
1864b88c807SRodney W. Grimes 				minus_o(*argptr, val);
1874b88c807SRodney W. Grimes 				if (*argptr)
1884b88c807SRodney W. Grimes 					argptr++;
1894b88c807SRodney W. Grimes 			} else {
190621a31c6SSteve Price 				if (c == 'p' && !val && privileged) {
191621a31c6SSteve Price 					(void) setuid(getuid());
192621a31c6SSteve Price 					(void) setgid(getgid());
193621a31c6SSteve Price 				}
1944b88c807SRodney W. Grimes 				setoption(c, val);
1954b88c807SRodney W. Grimes 			}
1964b88c807SRodney W. Grimes 		}
1974b88c807SRodney W. Grimes 	}
1984b88c807SRodney W. Grimes }
1994b88c807SRodney W. Grimes 
2004b88c807SRodney W. Grimes STATIC void
2015134c3f7SWarner Losh minus_o(char *name, int val)
2024b88c807SRodney W. Grimes {
203d513af6aSTim J. Robbins 	int doneset, i;
2044b88c807SRodney W. Grimes 
2054b88c807SRodney W. Grimes 	if (name == NULL) {
206d513af6aSTim J. Robbins 		if (val) {
207d513af6aSTim J. Robbins 			/* "Pretty" output. */
2084b88c807SRodney W. Grimes 			out1str("Current option settings\n");
2094b88c807SRodney W. Grimes 			for (i = 0; i < NOPTS; i++)
2104b88c807SRodney W. Grimes 				out1fmt("%-16s%s\n", optlist[i].name,
2114b88c807SRodney W. Grimes 					optlist[i].val ? "on" : "off");
2124b88c807SRodney W. Grimes 		} else {
213d513af6aSTim J. Robbins 			/* Output suitable for re-input to shell. */
214d513af6aSTim J. Robbins 			for (doneset = i = 0; i < NOPTS; i++)
215d513af6aSTim J. Robbins 				if (optlist[i].val) {
216d513af6aSTim J. Robbins 					if (!doneset) {
217d513af6aSTim J. Robbins 						out1str("set");
218d513af6aSTim J. Robbins 						doneset = 1;
219d513af6aSTim J. Robbins 					}
220d513af6aSTim J. Robbins 					out1fmt(" -o %s", optlist[i].name);
221d513af6aSTim J. Robbins 				}
222d513af6aSTim J. Robbins 			if (doneset)
223d513af6aSTim J. Robbins 				out1c('\n');
224d513af6aSTim J. Robbins 		}
225d513af6aSTim J. Robbins 	} else {
2264b88c807SRodney W. Grimes 		for (i = 0; i < NOPTS; i++)
2274b88c807SRodney W. Grimes 			if (equal(name, optlist[i].name)) {
228621a31c6SSteve Price 				if (!val && privileged && equal(name, "privileged")) {
229621a31c6SSteve Price 					(void) setuid(getuid());
230621a31c6SSteve Price 					(void) setgid(getgid());
231621a31c6SSteve Price 				}
2324b88c807SRodney W. Grimes 				setoption(optlist[i].letter, val);
2334b88c807SRodney W. Grimes 				return;
2344b88c807SRodney W. Grimes 			}
2354b88c807SRodney W. Grimes 		error("Illegal option -o %s", name);
2364b88c807SRodney W. Grimes 	}
2374b88c807SRodney W. Grimes }
2384b88c807SRodney W. Grimes 
2394b88c807SRodney W. Grimes 
2404b88c807SRodney W. Grimes STATIC void
2415134c3f7SWarner Losh setoption(int flag, int val)
2424b88c807SRodney W. Grimes {
2434b88c807SRodney W. Grimes 	int i;
2444b88c807SRodney W. Grimes 
2454b88c807SRodney W. Grimes 	for (i = 0; i < NOPTS; i++)
2464b88c807SRodney W. Grimes 		if (optlist[i].letter == flag) {
2474b88c807SRodney W. Grimes 			optlist[i].val = val;
2484b88c807SRodney W. Grimes 			if (val) {
2494b88c807SRodney W. Grimes 				/* #%$ hack for ksh semantics */
2504b88c807SRodney W. Grimes 				if (flag == 'V')
2514b88c807SRodney W. Grimes 					Eflag = 0;
2524b88c807SRodney W. Grimes 				else if (flag == 'E')
2534b88c807SRodney W. Grimes 					Vflag = 0;
2544b88c807SRodney W. Grimes 			}
2554b88c807SRodney W. Grimes 			return;
2564b88c807SRodney W. Grimes 		}
2574b88c807SRodney W. Grimes 	error("Illegal option -%c", flag);
2584b88c807SRodney W. Grimes }
2594b88c807SRodney W. Grimes 
2604b88c807SRodney W. Grimes 
2614b88c807SRodney W. Grimes 
2624b88c807SRodney W. Grimes #ifdef mkinit
2634b88c807SRodney W. Grimes INCLUDE "options.h"
2644b88c807SRodney W. Grimes 
2654b88c807SRodney W. Grimes SHELLPROC {
2664b88c807SRodney W. Grimes 	int i;
2674b88c807SRodney W. Grimes 
2684b88c807SRodney W. Grimes 	for (i = 0; i < NOPTS; i++)
2694b88c807SRodney W. Grimes 		optlist[i].val = 0;
2704b88c807SRodney W. Grimes 	optschanged();
2714b88c807SRodney W. Grimes 
2724b88c807SRodney W. Grimes }
2734b88c807SRodney W. Grimes #endif
2744b88c807SRodney W. Grimes 
2754b88c807SRodney W. Grimes 
2764b88c807SRodney W. Grimes /*
2774b88c807SRodney W. Grimes  * Set the shell parameters.
2784b88c807SRodney W. Grimes  */
2794b88c807SRodney W. Grimes 
2804b88c807SRodney W. Grimes void
2815134c3f7SWarner Losh setparam(char **argv)
2824b88c807SRodney W. Grimes {
2834b88c807SRodney W. Grimes 	char **newparam;
2844b88c807SRodney W. Grimes 	char **ap;
2854b88c807SRodney W. Grimes 	int nparam;
2864b88c807SRodney W. Grimes 
2874b88c807SRodney W. Grimes 	for (nparam = 0 ; argv[nparam] ; nparam++);
2884b88c807SRodney W. Grimes 	ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
2894b88c807SRodney W. Grimes 	while (*argv) {
2904b88c807SRodney W. Grimes 		*ap++ = savestr(*argv++);
2914b88c807SRodney W. Grimes 	}
2924b88c807SRodney W. Grimes 	*ap = NULL;
2934b88c807SRodney W. Grimes 	freeparam(&shellparam);
2944b88c807SRodney W. Grimes 	shellparam.malloc = 1;
2954b88c807SRodney W. Grimes 	shellparam.nparam = nparam;
2964b88c807SRodney W. Grimes 	shellparam.p = newparam;
2974b88c807SRodney W. Grimes 	shellparam.optnext = NULL;
2984b88c807SRodney W. Grimes }
2994b88c807SRodney W. Grimes 
3004b88c807SRodney W. Grimes 
3014b88c807SRodney W. Grimes /*
3024b88c807SRodney W. Grimes  * Free the list of positional parameters.
3034b88c807SRodney W. Grimes  */
3044b88c807SRodney W. Grimes 
3054b88c807SRodney W. Grimes void
3065134c3f7SWarner Losh freeparam(struct shparam *param)
3074b88c807SRodney W. Grimes {
3084b88c807SRodney W. Grimes 	char **ap;
3094b88c807SRodney W. Grimes 
3104b88c807SRodney W. Grimes 	if (param->malloc) {
3114b88c807SRodney W. Grimes 		for (ap = param->p ; *ap ; ap++)
3124b88c807SRodney W. Grimes 			ckfree(*ap);
3134b88c807SRodney W. Grimes 		ckfree(param->p);
3144b88c807SRodney W. Grimes 	}
3154b88c807SRodney W. Grimes }
3164b88c807SRodney W. Grimes 
3174b88c807SRodney W. Grimes 
3184b88c807SRodney W. Grimes 
3194b88c807SRodney W. Grimes /*
3204b88c807SRodney W. Grimes  * The shift builtin command.
3214b88c807SRodney W. Grimes  */
3224b88c807SRodney W. Grimes 
323aa9caaf6SPeter Wemm int
3245134c3f7SWarner Losh shiftcmd(int argc, char **argv)
325aa9caaf6SPeter Wemm {
3264b88c807SRodney W. Grimes 	int n;
3274b88c807SRodney W. Grimes 	char **ap1, **ap2;
3284b88c807SRodney W. Grimes 
3294b88c807SRodney W. Grimes 	n = 1;
3304b88c807SRodney W. Grimes 	if (argc > 1)
3314b88c807SRodney W. Grimes 		n = number(argv[1]);
3324b88c807SRodney W. Grimes 	if (n > shellparam.nparam)
3334b88c807SRodney W. Grimes 		error("can't shift that many");
3344b88c807SRodney W. Grimes 	INTOFF;
3354b88c807SRodney W. Grimes 	shellparam.nparam -= n;
3364b88c807SRodney W. Grimes 	for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
3374b88c807SRodney W. Grimes 		if (shellparam.malloc)
3384b88c807SRodney W. Grimes 			ckfree(*ap1);
3394b88c807SRodney W. Grimes 	}
3404b88c807SRodney W. Grimes 	ap2 = shellparam.p;
3414b88c807SRodney W. Grimes 	while ((*ap2++ = *ap1++) != NULL);
3424b88c807SRodney W. Grimes 	shellparam.optnext = NULL;
3434b88c807SRodney W. Grimes 	INTON;
3444b88c807SRodney W. Grimes 	return 0;
3454b88c807SRodney W. Grimes }
3464b88c807SRodney W. Grimes 
3474b88c807SRodney W. Grimes 
3484b88c807SRodney W. Grimes 
3494b88c807SRodney W. Grimes /*
3504b88c807SRodney W. Grimes  * The set command builtin.
3514b88c807SRodney W. Grimes  */
3524b88c807SRodney W. Grimes 
353aa9caaf6SPeter Wemm int
3545134c3f7SWarner Losh setcmd(int argc, char **argv)
355aa9caaf6SPeter Wemm {
3564b88c807SRodney W. Grimes 	if (argc == 1)
3574b88c807SRodney W. Grimes 		return showvarscmd(argc, argv);
3584b88c807SRodney W. Grimes 	INTOFF;
3594b88c807SRodney W. Grimes 	options(0);
3604b88c807SRodney W. Grimes 	optschanged();
3614b88c807SRodney W. Grimes 	if (*argptr != NULL) {
3624b88c807SRodney W. Grimes 		setparam(argptr);
3634b88c807SRodney W. Grimes 	}
3644b88c807SRodney W. Grimes 	INTON;
3654b88c807SRodney W. Grimes 	return 0;
3664b88c807SRodney W. Grimes }
3674b88c807SRodney W. Grimes 
3684b88c807SRodney W. Grimes 
369ab0a2172SSteve Price void
3705134c3f7SWarner Losh getoptsreset(const char *value)
371ab0a2172SSteve Price {
372ab0a2172SSteve Price 	if (number(value) == 1) {
373ab0a2172SSteve Price 		shellparam.optnext = NULL;
374ab0a2172SSteve Price 		shellparam.reset = 1;
375ab0a2172SSteve Price 	}
376ab0a2172SSteve Price }
377ab0a2172SSteve Price 
3784b88c807SRodney W. Grimes /*
3794b88c807SRodney W. Grimes  * The getopts builtin.  Shellparam.optnext points to the next argument
3804b88c807SRodney W. Grimes  * to be processed.  Shellparam.optptr points to the next character to
3814b88c807SRodney W. Grimes  * be processed in the current argument.  If shellparam.optnext is NULL,
3824b88c807SRodney W. Grimes  * then it's the first time getopts has been called.
3834b88c807SRodney W. Grimes  */
3844b88c807SRodney W. Grimes 
385aa9caaf6SPeter Wemm int
3865134c3f7SWarner Losh getoptscmd(int argc, char **argv)
387aa9caaf6SPeter Wemm {
388ab0a2172SSteve Price 	char **optbase = NULL;
389ab0a2172SSteve Price 
390ab0a2172SSteve Price 	if (argc < 3)
391d3974088SDag-Erling Smørgrav 		error("usage: getopts optstring var [arg]");
392ab0a2172SSteve Price 	else if (argc == 3)
393ab0a2172SSteve Price 		optbase = shellparam.p;
394ab0a2172SSteve Price 	else
395ab0a2172SSteve Price 		optbase = &argv[3];
396ab0a2172SSteve Price 
397ab0a2172SSteve Price 	if (shellparam.reset == 1) {
398ab0a2172SSteve Price 		shellparam.optnext = optbase;
399ab0a2172SSteve Price 		shellparam.optptr = NULL;
400ab0a2172SSteve Price 		shellparam.reset = 0;
401ab0a2172SSteve Price 	}
402ab0a2172SSteve Price 
403ab0a2172SSteve Price 	return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
404ab0a2172SSteve Price 		       &shellparam.optptr);
405ab0a2172SSteve Price }
406ab0a2172SSteve Price 
407ab0a2172SSteve Price STATIC int
4085134c3f7SWarner Losh getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
4095134c3f7SWarner Losh     char **optptr)
410ab0a2172SSteve Price {
411904a3dc8SSteve Price 	char *p, *q;
412ab0a2172SSteve Price 	char c = '?';
413ab0a2172SSteve Price 	int done = 0;
414ab0a2172SSteve Price 	int ind = 0;
415ab0a2172SSteve Price 	int err = 0;
4164b88c807SRodney W. Grimes 	char s[10];
4174b88c807SRodney W. Grimes 
418ab0a2172SSteve Price 	if ((p = *optptr) == NULL || *p == '\0') {
419ab0a2172SSteve Price 		/* Current word is done, advance */
420ab0a2172SSteve Price 		if (*optnext == NULL)
421ab0a2172SSteve Price 			return 1;
422ab0a2172SSteve Price 		p = **optnext;
4234b88c807SRodney W. Grimes 		if (p == NULL || *p != '-' || *++p == '\0') {
4244b88c807SRodney W. Grimes atend:
425ab0a2172SSteve Price 			ind = *optnext - optfirst + 1;
42693d0e5efSSteve Price 			*optnext = NULL;
42793d0e5efSSteve Price 			p = NULL;
428ab0a2172SSteve Price 			done = 1;
429ab0a2172SSteve Price 			goto out;
4304b88c807SRodney W. Grimes 		}
431ab0a2172SSteve Price 		(*optnext)++;
4324b88c807SRodney W. Grimes 		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
4334b88c807SRodney W. Grimes 			goto atend;
4344b88c807SRodney W. Grimes 	}
435ab0a2172SSteve Price 
4364b88c807SRodney W. Grimes 	c = *p++;
437ab0a2172SSteve Price 	for (q = optstr; *q != c; ) {
4384b88c807SRodney W. Grimes 		if (*q == '\0') {
439ab0a2172SSteve Price 			if (optstr[0] == ':') {
440ab0a2172SSteve Price 				s[0] = c;
441ab0a2172SSteve Price 				s[1] = '\0';
442ab0a2172SSteve Price 				err |= setvarsafe("OPTARG", s, 0);
443ab0a2172SSteve Price 			}
444ab0a2172SSteve Price 			else {
4454b88c807SRodney W. Grimes 				out1fmt("Illegal option -%c\n", c);
446ab0a2172SSteve Price 				(void) unsetvar("OPTARG");
447ab0a2172SSteve Price 			}
4484b88c807SRodney W. Grimes 			c = '?';
449ab0a2172SSteve Price 			goto bad;
4504b88c807SRodney W. Grimes 		}
4514b88c807SRodney W. Grimes 		if (*++q == ':')
4524b88c807SRodney W. Grimes 			q++;
4534b88c807SRodney W. Grimes 	}
454ab0a2172SSteve Price 
4554b88c807SRodney W. Grimes 	if (*++q == ':') {
456ab0a2172SSteve Price 		if (*p == '\0' && (p = **optnext) == NULL) {
457ab0a2172SSteve Price 			if (optstr[0] == ':') {
4584b88c807SRodney W. Grimes 				s[0] = c;
4594b88c807SRodney W. Grimes 				s[1] = '\0';
460ab0a2172SSteve Price 				err |= setvarsafe("OPTARG", s, 0);
461ab0a2172SSteve Price 				c = ':';
462ab0a2172SSteve Price 			}
463ab0a2172SSteve Price 			else {
464ab0a2172SSteve Price 				out1fmt("No arg for -%c option\n", c);
465ab0a2172SSteve Price 				(void) unsetvar("OPTARG");
466ab0a2172SSteve Price 				c = '?';
467ab0a2172SSteve Price 			}
468ab0a2172SSteve Price 			goto bad;
469ab0a2172SSteve Price 		}
470ab0a2172SSteve Price 
471ab0a2172SSteve Price 		if (p == **optnext)
472ab0a2172SSteve Price 			(*optnext)++;
473ab0a2172SSteve Price 		setvarsafe("OPTARG", p, 0);
474ab0a2172SSteve Price 		p = NULL;
475ab0a2172SSteve Price 	}
476ab0a2172SSteve Price 	else
477ab0a2172SSteve Price 		setvarsafe("OPTARG", "", 0);
478ab0a2172SSteve Price 	ind = *optnext - optfirst + 1;
479ab0a2172SSteve Price 	goto out;
480ab0a2172SSteve Price 
481ab0a2172SSteve Price bad:
482ab0a2172SSteve Price 	ind = 1;
483ab0a2172SSteve Price 	*optnext = NULL;
484ab0a2172SSteve Price 	p = NULL;
485ab0a2172SSteve Price out:
486ab0a2172SSteve Price 	*optptr = p;
487ab0a2172SSteve Price 	fmtstr(s, sizeof(s), "%d", ind);
488ab0a2172SSteve Price 	err |= setvarsafe("OPTIND", s, VNOFUNC);
489ab0a2172SSteve Price 	s[0] = c;
490ab0a2172SSteve Price 	s[1] = '\0';
491ab0a2172SSteve Price 	err |= setvarsafe(optvar, s, 0);
492ab0a2172SSteve Price 	if (err) {
493ab0a2172SSteve Price 		*optnext = NULL;
494ab0a2172SSteve Price 		*optptr = NULL;
495ab0a2172SSteve Price 		flushall();
496ab0a2172SSteve Price 		exraise(EXERROR);
497ab0a2172SSteve Price 	}
498ab0a2172SSteve Price 	return done;
4994b88c807SRodney W. Grimes }
5004b88c807SRodney W. Grimes 
5014b88c807SRodney W. Grimes /*
5024b88c807SRodney W. Grimes  * XXX - should get rid of.  have all builtins use getopt(3).  the
5034b88c807SRodney W. Grimes  * library getopt must have the BSD extension static variable "optreset"
5044b88c807SRodney W. Grimes  * otherwise it can't be used within the shell safely.
5054b88c807SRodney W. Grimes  *
5064b88c807SRodney W. Grimes  * Standard option processing (a la getopt) for builtin routines.  The
5074b88c807SRodney W. Grimes  * only argument that is passed to nextopt is the option string; the
5084b88c807SRodney W. Grimes  * other arguments are unnecessary.  It return the character, or '\0' on
5094b88c807SRodney W. Grimes  * end of input.
5104b88c807SRodney W. Grimes  */
5114b88c807SRodney W. Grimes 
5124b88c807SRodney W. Grimes int
5135134c3f7SWarner Losh nextopt(char *optstring)
5144b88c807SRodney W. Grimes {
515904a3dc8SSteve Price 	char *p, *q;
5164b88c807SRodney W. Grimes 	char c;
5174b88c807SRodney W. Grimes 
5184b88c807SRodney W. Grimes 	if ((p = optptr) == NULL || *p == '\0') {
5194b88c807SRodney W. Grimes 		p = *argptr;
5204b88c807SRodney W. Grimes 		if (p == NULL || *p != '-' || *++p == '\0')
5214b88c807SRodney W. Grimes 			return '\0';
5224b88c807SRodney W. Grimes 		argptr++;
5234b88c807SRodney W. Grimes 		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
5244b88c807SRodney W. Grimes 			return '\0';
5254b88c807SRodney W. Grimes 	}
5264b88c807SRodney W. Grimes 	c = *p++;
5274b88c807SRodney W. Grimes 	for (q = optstring ; *q != c ; ) {
5284b88c807SRodney W. Grimes 		if (*q == '\0')
5294b88c807SRodney W. Grimes 			error("Illegal option -%c", c);
5304b88c807SRodney W. Grimes 		if (*++q == ':')
5314b88c807SRodney W. Grimes 			q++;
5324b88c807SRodney W. Grimes 	}
5334b88c807SRodney W. Grimes 	if (*++q == ':') {
5344b88c807SRodney W. Grimes 		if (*p == '\0' && (p = *argptr++) == NULL)
5354b88c807SRodney W. Grimes 			error("No arg for -%c option", c);
536f01e3d0cSMartin Cracauer 		shoptarg = p;
5374b88c807SRodney W. Grimes 		p = NULL;
5384b88c807SRodney W. Grimes 	}
5394b88c807SRodney W. Grimes 	optptr = p;
5404b88c807SRodney W. Grimes 	return c;
5414b88c807SRodney W. Grimes }
542