xref: /freebsd/bin/sh/options.c (revision 5cc540d471f885e245faf92b690fc50d61d16d17)
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  * 4. Neither the name of the University nor the names of its contributors
174b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
184b88c807SRodney W. Grimes  *    without specific prior written permission.
194b88c807SRodney W. Grimes  *
204b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
214b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304b88c807SRodney W. Grimes  * SUCH DAMAGE.
314b88c807SRodney W. Grimes  */
324b88c807SRodney W. Grimes 
334b88c807SRodney W. Grimes #ifndef lint
343d7b5b93SPhilippe Charnier #if 0
353d7b5b93SPhilippe Charnier static char sccsid[] = "@(#)options.c	8.2 (Berkeley) 5/4/95";
363d7b5b93SPhilippe Charnier #endif
374b88c807SRodney W. Grimes #endif /* not lint */
382749b141SDavid E. O'Brien #include <sys/cdefs.h>
392749b141SDavid E. O'Brien __FBSDID("$FreeBSD$");
404b88c807SRodney W. Grimes 
41aa9caaf6SPeter Wemm #include <signal.h>
42aa9caaf6SPeter Wemm #include <unistd.h>
43aa9caaf6SPeter Wemm #include <stdlib.h>
44aa9caaf6SPeter Wemm 
454b88c807SRodney W. Grimes #include "shell.h"
464b88c807SRodney W. Grimes #define DEFINE_OPTIONS
474b88c807SRodney W. Grimes #include "options.h"
484b88c807SRodney W. Grimes #undef DEFINE_OPTIONS
494b88c807SRodney W. Grimes #include "nodes.h"	/* for other header files */
504b88c807SRodney W. Grimes #include "eval.h"
514b88c807SRodney W. Grimes #include "jobs.h"
524b88c807SRodney W. Grimes #include "input.h"
534b88c807SRodney W. Grimes #include "output.h"
544b88c807SRodney W. Grimes #include "trap.h"
554b88c807SRodney W. Grimes #include "var.h"
564b88c807SRodney W. Grimes #include "memalloc.h"
574b88c807SRodney W. Grimes #include "error.h"
584b88c807SRodney W. Grimes #include "mystring.h"
59aa9caaf6SPeter Wemm #ifndef NO_HISTORY
60aa9caaf6SPeter Wemm #include "myhistedit.h"
61aa9caaf6SPeter Wemm #endif
624b88c807SRodney W. Grimes 
634b88c807SRodney W. Grimes char *arg0;			/* value of $0 */
644b88c807SRodney W. Grimes struct shparam shellparam;	/* current positional parameters */
654b88c807SRodney W. Grimes char **argptr;			/* argument list for builtin commands */
66f01e3d0cSMartin Cracauer char *shoptarg;			/* set by nextopt (like getopt) */
674b88c807SRodney W. Grimes char *optptr;			/* used by nextopt */
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes char *minusc;			/* argument to -c option */
704b88c807SRodney W. Grimes 
714b88c807SRodney W. Grimes 
725134c3f7SWarner Losh STATIC void options(int);
735134c3f7SWarner Losh STATIC void minus_o(char *, int);
745134c3f7SWarner Losh STATIC void setoption(int, int);
755134c3f7SWarner Losh STATIC int getopts(char *, char *, char **, char ***, char **);
764b88c807SRodney W. Grimes 
774b88c807SRodney W. Grimes 
784b88c807SRodney W. Grimes /*
794b88c807SRodney W. Grimes  * Process the shell command line arguments.
804b88c807SRodney W. Grimes  */
814b88c807SRodney W. Grimes 
824b88c807SRodney W. Grimes void
835134c3f7SWarner Losh procargs(int argc, char **argv)
844b88c807SRodney W. Grimes {
854b88c807SRodney W. Grimes 	int i;
864b88c807SRodney W. Grimes 
874b88c807SRodney W. Grimes 	argptr = argv;
884b88c807SRodney W. Grimes 	if (argc > 0)
894b88c807SRodney W. Grimes 		argptr++;
904b88c807SRodney W. Grimes 	for (i = 0; i < NOPTS; i++)
914b88c807SRodney W. Grimes 		optlist[i].val = 2;
92621a31c6SSteve Price 	privileged = (getuid() != geteuid() || getgid() != getegid());
934b88c807SRodney W. Grimes 	options(1);
944b88c807SRodney W. Grimes 	if (*argptr == NULL && minusc == NULL)
954b88c807SRodney W. Grimes 		sflag = 1;
964b88c807SRodney W. Grimes 	if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
974b88c807SRodney W. Grimes 		iflag = 1;
984b88c807SRodney W. Grimes 	if (mflag == 2)
994b88c807SRodney W. Grimes 		mflag = iflag;
1004b88c807SRodney W. Grimes 	for (i = 0; i < NOPTS; i++)
1014b88c807SRodney W. Grimes 		if (optlist[i].val == 2)
1024b88c807SRodney W. Grimes 			optlist[i].val = 0;
1034b88c807SRodney W. Grimes 	arg0 = argv[0];
1044b88c807SRodney W. Grimes 	if (sflag == 0 && minusc == NULL) {
1054b88c807SRodney W. Grimes 		commandname = arg0 = *argptr++;
1064b88c807SRodney W. Grimes 		setinputfile(commandname, 0);
1074b88c807SRodney W. Grimes 	}
108ab0a2172SSteve Price 	/* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
109904a3dc8SSteve Price 	if (argptr && minusc && *argptr)
1106d753bddSJoerg Wunsch 		arg0 = *argptr++;
11193d0e5efSSteve Price 
1124b88c807SRodney W. Grimes 	shellparam.p = argptr;
11393d0e5efSSteve Price 	shellparam.reset = 1;
1144b88c807SRodney W. Grimes 	/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
1154b88c807SRodney W. Grimes 	while (*argptr) {
1164b88c807SRodney W. Grimes 		shellparam.nparam++;
1174b88c807SRodney W. Grimes 		argptr++;
1184b88c807SRodney W. Grimes 	}
1194b88c807SRodney W. Grimes 	optschanged();
1204b88c807SRodney W. Grimes }
1214b88c807SRodney W. Grimes 
1224b88c807SRodney W. Grimes 
123aa9caaf6SPeter Wemm void
1245134c3f7SWarner Losh optschanged(void)
125aa9caaf6SPeter Wemm {
1264b88c807SRodney W. Grimes 	setinteractive(iflag);
127aa9caaf6SPeter Wemm #ifndef NO_HISTORY
1284b88c807SRodney W. Grimes 	histedit();
129aa9caaf6SPeter Wemm #endif
1304b88c807SRodney W. Grimes 	setjobctl(mflag);
1314b88c807SRodney W. Grimes }
1324b88c807SRodney W. Grimes 
1334b88c807SRodney W. Grimes /*
1344b88c807SRodney W. Grimes  * Process shell options.  The global variable argptr contains a pointer
1354b88c807SRodney W. Grimes  * to the argument list; we advance it past the options.
1364b88c807SRodney W. Grimes  */
1374b88c807SRodney W. Grimes 
1384b88c807SRodney W. Grimes STATIC void
1395134c3f7SWarner Losh options(int cmdline)
140aa9caaf6SPeter Wemm {
141904a3dc8SSteve Price 	char *p;
1424b88c807SRodney W. Grimes 	int val;
1434b88c807SRodney W. Grimes 	int c;
1444b88c807SRodney W. Grimes 
1454b88c807SRodney W. Grimes 	if (cmdline)
1464b88c807SRodney W. Grimes 		minusc = NULL;
1474b88c807SRodney W. Grimes 	while ((p = *argptr) != NULL) {
1484b88c807SRodney W. Grimes 		argptr++;
1494b88c807SRodney W. Grimes 		if ((c = *p++) == '-') {
1504b88c807SRodney W. Grimes 			val = 1;
1515cc540d4SGarance A Drosehn 			/* A "-" or  "--" terminates options */
1524b88c807SRodney W. Grimes 			if (p[0] == '\0')
1535cc540d4SGarance A Drosehn 				goto end_options1;
1545cc540d4SGarance A Drosehn 			if (p[0] == '-' && p[1] == '\0')
1555cc540d4SGarance A Drosehn 				goto end_options2;
1564b88c807SRodney W. Grimes 		} else if (c == '+') {
1574b88c807SRodney W. Grimes 			val = 0;
1584b88c807SRodney W. Grimes 		} else {
1594b88c807SRodney W. Grimes 			argptr--;
1604b88c807SRodney W. Grimes 			break;
1614b88c807SRodney W. Grimes 		}
1624b88c807SRodney W. Grimes 		while ((c = *p++) != '\0') {
1634b88c807SRodney W. Grimes 			if (c == 'c' && cmdline) {
1644b88c807SRodney W. Grimes 				char *q;
1654b88c807SRodney W. Grimes #ifdef NOHACK	/* removing this code allows sh -ce 'foo' for compat */
1664b88c807SRodney W. Grimes 				if (*p == '\0')
1674b88c807SRodney W. Grimes #endif
1684b88c807SRodney W. Grimes 					q = *argptr++;
1694b88c807SRodney W. Grimes 				if (q == NULL || minusc != NULL)
1704b88c807SRodney W. Grimes 					error("Bad -c option");
1714b88c807SRodney W. Grimes 				minusc = q;
1724b88c807SRodney W. Grimes #ifdef NOHACK
1734b88c807SRodney W. Grimes 				break;
1744b88c807SRodney W. Grimes #endif
1754b88c807SRodney W. Grimes 			} else if (c == 'o') {
1764b88c807SRodney W. Grimes 				minus_o(*argptr, val);
1774b88c807SRodney W. Grimes 				if (*argptr)
1784b88c807SRodney W. Grimes 					argptr++;
1794b88c807SRodney W. Grimes 			} else {
180621a31c6SSteve Price 				if (c == 'p' && !val && privileged) {
181621a31c6SSteve Price 					(void) setuid(getuid());
182621a31c6SSteve Price 					(void) setgid(getgid());
183621a31c6SSteve Price 				}
1844b88c807SRodney W. Grimes 				setoption(c, val);
1854b88c807SRodney W. Grimes 			}
1864b88c807SRodney W. Grimes 		}
1874b88c807SRodney W. Grimes 	}
1885cc540d4SGarance A Drosehn 	return;
1895cc540d4SGarance A Drosehn 
1905cc540d4SGarance A Drosehn 	/* When processing `set', a single "-" means turn off -x and -v */
1915cc540d4SGarance A Drosehn end_options1:
1925cc540d4SGarance A Drosehn 	if (!cmdline) {
1935cc540d4SGarance A Drosehn 		xflag = vflag = 0;
1945cc540d4SGarance A Drosehn 		return;
1955cc540d4SGarance A Drosehn 	}
1965cc540d4SGarance A Drosehn 
1975cc540d4SGarance A Drosehn 	/*
1985cc540d4SGarance A Drosehn 	 * When processing `set', a "--" means the remaining arguments
1995cc540d4SGarance A Drosehn 	 * replace the positional parameters in the active shell.  If
2005cc540d4SGarance A Drosehn 	 * there are no remaining options, then all the positional
2015cc540d4SGarance A Drosehn 	 * parameters are cleared (equivalent to doing ``shift $#'').
2025cc540d4SGarance A Drosehn 	 */
2035cc540d4SGarance A Drosehn end_options2:
2045cc540d4SGarance A Drosehn 	if (!cmdline) {
2055cc540d4SGarance A Drosehn 		if (*argptr == NULL)
2065cc540d4SGarance A Drosehn 			setparam(argptr);
2075cc540d4SGarance A Drosehn 		return;
2085cc540d4SGarance A Drosehn 	}
2095cc540d4SGarance A Drosehn 
2105cc540d4SGarance A Drosehn 	/*
2115cc540d4SGarance A Drosehn 	 * At this point we are processing options given to 'sh' on a command
2125cc540d4SGarance A Drosehn 	 * line.  If an end-of-options marker ("-" or "--") is followed by an
2135cc540d4SGarance A Drosehn 	 * arg of "#", then skip over all remaining arguments.  Some scripting
2145cc540d4SGarance A Drosehn 	 * languages (e.g.: perl) document that /bin/sh will implement this
2155cc540d4SGarance A Drosehn 	 * behavior, and they recommend that users take advantage of it to
2165cc540d4SGarance A Drosehn 	 * solve certain issues that can come up when writing a perl script.
2175cc540d4SGarance A Drosehn 	 * Yes, this feature is in /bin/sh to help users write perl scripts.
2185cc540d4SGarance A Drosehn 	 */
2195cc540d4SGarance A Drosehn 	p = *argptr;
2205cc540d4SGarance A Drosehn 	if (p != NULL && p[0] == '#' && p[1] == '\0') {
2215cc540d4SGarance A Drosehn 		while (*argptr != NULL)
2225cc540d4SGarance A Drosehn 			argptr++;
2235cc540d4SGarance A Drosehn 		/* We need to keep the final argument */
2245cc540d4SGarance A Drosehn 		argptr--;
2255cc540d4SGarance A Drosehn 	}
2264b88c807SRodney W. Grimes }
2274b88c807SRodney W. Grimes 
2284b88c807SRodney W. Grimes STATIC void
2295134c3f7SWarner Losh minus_o(char *name, int val)
2304b88c807SRodney W. Grimes {
231d513af6aSTim J. Robbins 	int doneset, i;
2324b88c807SRodney W. Grimes 
2334b88c807SRodney W. Grimes 	if (name == NULL) {
234d513af6aSTim J. Robbins 		if (val) {
235d513af6aSTim J. Robbins 			/* "Pretty" output. */
2364b88c807SRodney W. Grimes 			out1str("Current option settings\n");
2374b88c807SRodney W. Grimes 			for (i = 0; i < NOPTS; i++)
2384b88c807SRodney W. Grimes 				out1fmt("%-16s%s\n", optlist[i].name,
2394b88c807SRodney W. Grimes 					optlist[i].val ? "on" : "off");
2404b88c807SRodney W. Grimes 		} else {
241d513af6aSTim J. Robbins 			/* Output suitable for re-input to shell. */
242d513af6aSTim J. Robbins 			for (doneset = i = 0; i < NOPTS; i++)
243d513af6aSTim J. Robbins 				if (optlist[i].val) {
244d513af6aSTim J. Robbins 					if (!doneset) {
245d513af6aSTim J. Robbins 						out1str("set");
246d513af6aSTim J. Robbins 						doneset = 1;
247d513af6aSTim J. Robbins 					}
248d513af6aSTim J. Robbins 					out1fmt(" -o %s", optlist[i].name);
249d513af6aSTim J. Robbins 				}
250d513af6aSTim J. Robbins 			if (doneset)
251d513af6aSTim J. Robbins 				out1c('\n');
252d513af6aSTim J. Robbins 		}
253d513af6aSTim J. Robbins 	} else {
2544b88c807SRodney W. Grimes 		for (i = 0; i < NOPTS; i++)
2554b88c807SRodney W. Grimes 			if (equal(name, optlist[i].name)) {
256621a31c6SSteve Price 				if (!val && privileged && equal(name, "privileged")) {
257621a31c6SSteve Price 					(void) setuid(getuid());
258621a31c6SSteve Price 					(void) setgid(getgid());
259621a31c6SSteve Price 				}
2604b88c807SRodney W. Grimes 				setoption(optlist[i].letter, val);
2614b88c807SRodney W. Grimes 				return;
2624b88c807SRodney W. Grimes 			}
2634b88c807SRodney W. Grimes 		error("Illegal option -o %s", name);
2644b88c807SRodney W. Grimes 	}
2654b88c807SRodney W. Grimes }
2664b88c807SRodney W. Grimes 
2674b88c807SRodney W. Grimes 
2684b88c807SRodney W. Grimes STATIC void
2695134c3f7SWarner Losh setoption(int flag, int val)
2704b88c807SRodney W. Grimes {
2714b88c807SRodney W. Grimes 	int i;
2724b88c807SRodney W. Grimes 
2734b88c807SRodney W. Grimes 	for (i = 0; i < NOPTS; i++)
2744b88c807SRodney W. Grimes 		if (optlist[i].letter == flag) {
2754b88c807SRodney W. Grimes 			optlist[i].val = val;
2764b88c807SRodney W. Grimes 			if (val) {
2774b88c807SRodney W. Grimes 				/* #%$ hack for ksh semantics */
2784b88c807SRodney W. Grimes 				if (flag == 'V')
2794b88c807SRodney W. Grimes 					Eflag = 0;
2804b88c807SRodney W. Grimes 				else if (flag == 'E')
2814b88c807SRodney W. Grimes 					Vflag = 0;
2824b88c807SRodney W. Grimes 			}
2834b88c807SRodney W. Grimes 			return;
2844b88c807SRodney W. Grimes 		}
2854b88c807SRodney W. Grimes 	error("Illegal option -%c", flag);
2864b88c807SRodney W. Grimes }
2874b88c807SRodney W. Grimes 
2884b88c807SRodney W. Grimes 
2894b88c807SRodney W. Grimes 
2904b88c807SRodney W. Grimes #ifdef mkinit
2914b88c807SRodney W. Grimes INCLUDE "options.h"
2924b88c807SRodney W. Grimes 
2934b88c807SRodney W. Grimes SHELLPROC {
2944b88c807SRodney W. Grimes 	int i;
2954b88c807SRodney W. Grimes 
2964b88c807SRodney W. Grimes 	for (i = 0; i < NOPTS; i++)
2974b88c807SRodney W. Grimes 		optlist[i].val = 0;
2984b88c807SRodney W. Grimes 	optschanged();
2994b88c807SRodney W. Grimes 
3004b88c807SRodney W. Grimes }
3014b88c807SRodney W. Grimes #endif
3024b88c807SRodney W. Grimes 
3034b88c807SRodney W. Grimes 
3044b88c807SRodney W. Grimes /*
3054b88c807SRodney W. Grimes  * Set the shell parameters.
3064b88c807SRodney W. Grimes  */
3074b88c807SRodney W. Grimes 
3084b88c807SRodney W. Grimes void
3095134c3f7SWarner Losh setparam(char **argv)
3104b88c807SRodney W. Grimes {
3114b88c807SRodney W. Grimes 	char **newparam;
3124b88c807SRodney W. Grimes 	char **ap;
3134b88c807SRodney W. Grimes 	int nparam;
3144b88c807SRodney W. Grimes 
3154b88c807SRodney W. Grimes 	for (nparam = 0 ; argv[nparam] ; nparam++);
3164b88c807SRodney W. Grimes 	ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
3174b88c807SRodney W. Grimes 	while (*argv) {
3184b88c807SRodney W. Grimes 		*ap++ = savestr(*argv++);
3194b88c807SRodney W. Grimes 	}
3204b88c807SRodney W. Grimes 	*ap = NULL;
3214b88c807SRodney W. Grimes 	freeparam(&shellparam);
3224b88c807SRodney W. Grimes 	shellparam.malloc = 1;
3234b88c807SRodney W. Grimes 	shellparam.nparam = nparam;
3244b88c807SRodney W. Grimes 	shellparam.p = newparam;
3254b88c807SRodney W. Grimes 	shellparam.optnext = NULL;
3264b88c807SRodney W. Grimes }
3274b88c807SRodney W. Grimes 
3284b88c807SRodney W. Grimes 
3294b88c807SRodney W. Grimes /*
3304b88c807SRodney W. Grimes  * Free the list of positional parameters.
3314b88c807SRodney W. Grimes  */
3324b88c807SRodney W. Grimes 
3334b88c807SRodney W. Grimes void
3345134c3f7SWarner Losh freeparam(struct shparam *param)
3354b88c807SRodney W. Grimes {
3364b88c807SRodney W. Grimes 	char **ap;
3374b88c807SRodney W. Grimes 
3384b88c807SRodney W. Grimes 	if (param->malloc) {
3394b88c807SRodney W. Grimes 		for (ap = param->p ; *ap ; ap++)
3404b88c807SRodney W. Grimes 			ckfree(*ap);
3414b88c807SRodney W. Grimes 		ckfree(param->p);
3424b88c807SRodney W. Grimes 	}
3434b88c807SRodney W. Grimes }
3444b88c807SRodney W. Grimes 
3454b88c807SRodney W. Grimes 
3464b88c807SRodney W. Grimes 
3474b88c807SRodney W. Grimes /*
3484b88c807SRodney W. Grimes  * The shift builtin command.
3494b88c807SRodney W. Grimes  */
3504b88c807SRodney W. Grimes 
351aa9caaf6SPeter Wemm int
3525134c3f7SWarner Losh shiftcmd(int argc, char **argv)
353aa9caaf6SPeter Wemm {
3544b88c807SRodney W. Grimes 	int n;
3554b88c807SRodney W. Grimes 	char **ap1, **ap2;
3564b88c807SRodney W. Grimes 
3574b88c807SRodney W. Grimes 	n = 1;
3584b88c807SRodney W. Grimes 	if (argc > 1)
3594b88c807SRodney W. Grimes 		n = number(argv[1]);
3604b88c807SRodney W. Grimes 	if (n > shellparam.nparam)
3614b88c807SRodney W. Grimes 		error("can't shift that many");
3624b88c807SRodney W. Grimes 	INTOFF;
3634b88c807SRodney W. Grimes 	shellparam.nparam -= n;
3644b88c807SRodney W. Grimes 	for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
3654b88c807SRodney W. Grimes 		if (shellparam.malloc)
3664b88c807SRodney W. Grimes 			ckfree(*ap1);
3674b88c807SRodney W. Grimes 	}
3684b88c807SRodney W. Grimes 	ap2 = shellparam.p;
3694b88c807SRodney W. Grimes 	while ((*ap2++ = *ap1++) != NULL);
3704b88c807SRodney W. Grimes 	shellparam.optnext = NULL;
3714b88c807SRodney W. Grimes 	INTON;
3724b88c807SRodney W. Grimes 	return 0;
3734b88c807SRodney W. Grimes }
3744b88c807SRodney W. Grimes 
3754b88c807SRodney W. Grimes 
3764b88c807SRodney W. Grimes 
3774b88c807SRodney W. Grimes /*
3784b88c807SRodney W. Grimes  * The set command builtin.
3794b88c807SRodney W. Grimes  */
3804b88c807SRodney W. Grimes 
381aa9caaf6SPeter Wemm int
3825134c3f7SWarner Losh setcmd(int argc, char **argv)
383aa9caaf6SPeter Wemm {
3844b88c807SRodney W. Grimes 	if (argc == 1)
3854b88c807SRodney W. Grimes 		return showvarscmd(argc, argv);
3864b88c807SRodney W. Grimes 	INTOFF;
3874b88c807SRodney W. Grimes 	options(0);
3884b88c807SRodney W. Grimes 	optschanged();
3894b88c807SRodney W. Grimes 	if (*argptr != NULL) {
3904b88c807SRodney W. Grimes 		setparam(argptr);
3914b88c807SRodney W. Grimes 	}
3924b88c807SRodney W. Grimes 	INTON;
3934b88c807SRodney W. Grimes 	return 0;
3944b88c807SRodney W. Grimes }
3954b88c807SRodney W. Grimes 
3964b88c807SRodney W. Grimes 
397ab0a2172SSteve Price void
3985134c3f7SWarner Losh getoptsreset(const char *value)
399ab0a2172SSteve Price {
400ab0a2172SSteve Price 	if (number(value) == 1) {
401ab0a2172SSteve Price 		shellparam.optnext = NULL;
402ab0a2172SSteve Price 		shellparam.reset = 1;
403ab0a2172SSteve Price 	}
404ab0a2172SSteve Price }
405ab0a2172SSteve Price 
4064b88c807SRodney W. Grimes /*
4074b88c807SRodney W. Grimes  * The getopts builtin.  Shellparam.optnext points to the next argument
4084b88c807SRodney W. Grimes  * to be processed.  Shellparam.optptr points to the next character to
4094b88c807SRodney W. Grimes  * be processed in the current argument.  If shellparam.optnext is NULL,
4104b88c807SRodney W. Grimes  * then it's the first time getopts has been called.
4114b88c807SRodney W. Grimes  */
4124b88c807SRodney W. Grimes 
413aa9caaf6SPeter Wemm int
4145134c3f7SWarner Losh getoptscmd(int argc, char **argv)
415aa9caaf6SPeter Wemm {
416ab0a2172SSteve Price 	char **optbase = NULL;
417ab0a2172SSteve Price 
418ab0a2172SSteve Price 	if (argc < 3)
419d3974088SDag-Erling Smørgrav 		error("usage: getopts optstring var [arg]");
420ab0a2172SSteve Price 	else if (argc == 3)
421ab0a2172SSteve Price 		optbase = shellparam.p;
422ab0a2172SSteve Price 	else
423ab0a2172SSteve Price 		optbase = &argv[3];
424ab0a2172SSteve Price 
425ab0a2172SSteve Price 	if (shellparam.reset == 1) {
426ab0a2172SSteve Price 		shellparam.optnext = optbase;
427ab0a2172SSteve Price 		shellparam.optptr = NULL;
428ab0a2172SSteve Price 		shellparam.reset = 0;
429ab0a2172SSteve Price 	}
430ab0a2172SSteve Price 
431ab0a2172SSteve Price 	return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
432ab0a2172SSteve Price 		       &shellparam.optptr);
433ab0a2172SSteve Price }
434ab0a2172SSteve Price 
435ab0a2172SSteve Price STATIC int
4365134c3f7SWarner Losh getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
4375134c3f7SWarner Losh     char **optptr)
438ab0a2172SSteve Price {
439904a3dc8SSteve Price 	char *p, *q;
440ab0a2172SSteve Price 	char c = '?';
441ab0a2172SSteve Price 	int done = 0;
442ab0a2172SSteve Price 	int ind = 0;
443ab0a2172SSteve Price 	int err = 0;
4444b88c807SRodney W. Grimes 	char s[10];
4454b88c807SRodney W. Grimes 
446ab0a2172SSteve Price 	if ((p = *optptr) == NULL || *p == '\0') {
447ab0a2172SSteve Price 		/* Current word is done, advance */
448ab0a2172SSteve Price 		if (*optnext == NULL)
449ab0a2172SSteve Price 			return 1;
450ab0a2172SSteve Price 		p = **optnext;
4514b88c807SRodney W. Grimes 		if (p == NULL || *p != '-' || *++p == '\0') {
4524b88c807SRodney W. Grimes atend:
453ab0a2172SSteve Price 			ind = *optnext - optfirst + 1;
45493d0e5efSSteve Price 			*optnext = NULL;
45593d0e5efSSteve Price 			p = NULL;
456ab0a2172SSteve Price 			done = 1;
457ab0a2172SSteve Price 			goto out;
4584b88c807SRodney W. Grimes 		}
459ab0a2172SSteve Price 		(*optnext)++;
4604b88c807SRodney W. Grimes 		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
4614b88c807SRodney W. Grimes 			goto atend;
4624b88c807SRodney W. Grimes 	}
463ab0a2172SSteve Price 
4644b88c807SRodney W. Grimes 	c = *p++;
465ab0a2172SSteve Price 	for (q = optstr; *q != c; ) {
4664b88c807SRodney W. Grimes 		if (*q == '\0') {
467ab0a2172SSteve Price 			if (optstr[0] == ':') {
468ab0a2172SSteve Price 				s[0] = c;
469ab0a2172SSteve Price 				s[1] = '\0';
470ab0a2172SSteve Price 				err |= setvarsafe("OPTARG", s, 0);
471ab0a2172SSteve Price 			}
472ab0a2172SSteve Price 			else {
4734b88c807SRodney W. Grimes 				out1fmt("Illegal option -%c\n", c);
474ab0a2172SSteve Price 				(void) unsetvar("OPTARG");
475ab0a2172SSteve Price 			}
4764b88c807SRodney W. Grimes 			c = '?';
477ab0a2172SSteve Price 			goto bad;
4784b88c807SRodney W. Grimes 		}
4794b88c807SRodney W. Grimes 		if (*++q == ':')
4804b88c807SRodney W. Grimes 			q++;
4814b88c807SRodney W. Grimes 	}
482ab0a2172SSteve Price 
4834b88c807SRodney W. Grimes 	if (*++q == ':') {
484ab0a2172SSteve Price 		if (*p == '\0' && (p = **optnext) == NULL) {
485ab0a2172SSteve Price 			if (optstr[0] == ':') {
4864b88c807SRodney W. Grimes 				s[0] = c;
4874b88c807SRodney W. Grimes 				s[1] = '\0';
488ab0a2172SSteve Price 				err |= setvarsafe("OPTARG", s, 0);
489ab0a2172SSteve Price 				c = ':';
490ab0a2172SSteve Price 			}
491ab0a2172SSteve Price 			else {
492ab0a2172SSteve Price 				out1fmt("No arg for -%c option\n", c);
493ab0a2172SSteve Price 				(void) unsetvar("OPTARG");
494ab0a2172SSteve Price 				c = '?';
495ab0a2172SSteve Price 			}
496ab0a2172SSteve Price 			goto bad;
497ab0a2172SSteve Price 		}
498ab0a2172SSteve Price 
499ab0a2172SSteve Price 		if (p == **optnext)
500ab0a2172SSteve Price 			(*optnext)++;
501ab0a2172SSteve Price 		setvarsafe("OPTARG", p, 0);
502ab0a2172SSteve Price 		p = NULL;
503ab0a2172SSteve Price 	}
504ab0a2172SSteve Price 	else
505ab0a2172SSteve Price 		setvarsafe("OPTARG", "", 0);
506ab0a2172SSteve Price 	ind = *optnext - optfirst + 1;
507ab0a2172SSteve Price 	goto out;
508ab0a2172SSteve Price 
509ab0a2172SSteve Price bad:
510ab0a2172SSteve Price 	ind = 1;
511ab0a2172SSteve Price 	*optnext = NULL;
512ab0a2172SSteve Price 	p = NULL;
513ab0a2172SSteve Price out:
514ab0a2172SSteve Price 	*optptr = p;
515ab0a2172SSteve Price 	fmtstr(s, sizeof(s), "%d", ind);
516ab0a2172SSteve Price 	err |= setvarsafe("OPTIND", s, VNOFUNC);
517ab0a2172SSteve Price 	s[0] = c;
518ab0a2172SSteve Price 	s[1] = '\0';
519ab0a2172SSteve Price 	err |= setvarsafe(optvar, s, 0);
520ab0a2172SSteve Price 	if (err) {
521ab0a2172SSteve Price 		*optnext = NULL;
522ab0a2172SSteve Price 		*optptr = NULL;
523ab0a2172SSteve Price 		flushall();
524ab0a2172SSteve Price 		exraise(EXERROR);
525ab0a2172SSteve Price 	}
526ab0a2172SSteve Price 	return done;
5274b88c807SRodney W. Grimes }
5284b88c807SRodney W. Grimes 
5294b88c807SRodney W. Grimes /*
5304b88c807SRodney W. Grimes  * XXX - should get rid of.  have all builtins use getopt(3).  the
5314b88c807SRodney W. Grimes  * library getopt must have the BSD extension static variable "optreset"
5324b88c807SRodney W. Grimes  * otherwise it can't be used within the shell safely.
5334b88c807SRodney W. Grimes  *
5344b88c807SRodney W. Grimes  * Standard option processing (a la getopt) for builtin routines.  The
5354b88c807SRodney W. Grimes  * only argument that is passed to nextopt is the option string; the
5364b88c807SRodney W. Grimes  * other arguments are unnecessary.  It return the character, or '\0' on
5374b88c807SRodney W. Grimes  * end of input.
5384b88c807SRodney W. Grimes  */
5394b88c807SRodney W. Grimes 
5404b88c807SRodney W. Grimes int
5415134c3f7SWarner Losh nextopt(char *optstring)
5424b88c807SRodney W. Grimes {
543904a3dc8SSteve Price 	char *p, *q;
5444b88c807SRodney W. Grimes 	char c;
5454b88c807SRodney W. Grimes 
5464b88c807SRodney W. Grimes 	if ((p = optptr) == NULL || *p == '\0') {
5474b88c807SRodney W. Grimes 		p = *argptr;
5484b88c807SRodney W. Grimes 		if (p == NULL || *p != '-' || *++p == '\0')
5494b88c807SRodney W. Grimes 			return '\0';
5504b88c807SRodney W. Grimes 		argptr++;
5514b88c807SRodney W. Grimes 		if (p[0] == '-' && p[1] == '\0')	/* check for "--" */
5524b88c807SRodney W. Grimes 			return '\0';
5534b88c807SRodney W. Grimes 	}
5544b88c807SRodney W. Grimes 	c = *p++;
5554b88c807SRodney W. Grimes 	for (q = optstring ; *q != c ; ) {
5564b88c807SRodney W. Grimes 		if (*q == '\0')
5574b88c807SRodney W. Grimes 			error("Illegal option -%c", c);
5584b88c807SRodney W. Grimes 		if (*++q == ':')
5594b88c807SRodney W. Grimes 			q++;
5604b88c807SRodney W. Grimes 	}
5614b88c807SRodney W. Grimes 	if (*++q == ':') {
5624b88c807SRodney W. Grimes 		if (*p == '\0' && (p = *argptr++) == NULL)
5634b88c807SRodney W. Grimes 			error("No arg for -%c option", c);
564f01e3d0cSMartin Cracauer 		shoptarg = p;
5654b88c807SRodney W. Grimes 		p = NULL;
5664b88c807SRodney W. Grimes 	}
5674b88c807SRodney W. Grimes 	optptr = p;
5684b88c807SRodney W. Grimes 	return c;
5694b88c807SRodney W. Grimes }
570