2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: src/bin/sh/options.c,v 1.15.2.2 2002/07/19 04:38:52 tjr Exp $");
50 #define DEFINE_OPTIONS
53 #include "nodes.h" /* for other header files */
64 #include "myhistedit.h"
67 char *arg0; /* value of $0 */
68 struct shparam shellparam; /* current positional parameters */
69 char **argptr; /* argument list for builtin commands */
70 char *shoptarg; /* set by nextopt (like getopt) */
71 char *optptr; /* used by nextopt */
73 char *minusc; /* argument to -c option */
76 STATIC void options(int);
77 STATIC void minus_o(char *, int);
78 STATIC void setoption(int, int);
79 STATIC int getopts(char *, char *, char **, char ***, char **);
83 * Process the shell command line arguments.
87 procargs(int argc, char **argv)
94 for (i = 0; i < NOPTS; i++)
96 privileged = (getuid() != geteuid() || getgid() != getegid());
98 if (*argptr == NULL && minusc == NULL)
100 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
104 for (i = 0; i < NOPTS; i++)
105 if (optlist[i].val == 2)
108 if (sflag == 0 && minusc == NULL) {
109 commandname = arg0 = *argptr++;
110 setinputfile(commandname, 0);
112 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
113 if (argptr && minusc && *argptr)
116 shellparam.p = argptr;
117 shellparam.reset = 1;
118 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
130 setinteractive(iflag);
138 * Process shell options. The global variable argptr contains a pointer
139 * to the argument list; we advance it past the options.
151 while ((p = *argptr) != NULL) {
153 if ((c = *p++) == '-') {
155 if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
157 /* "-" means turn off -x and -v */
160 /* "--" means reset params */
161 else if (*argptr == NULL)
164 break; /* "-" or "--" terminates options */
166 } else if (c == '+') {
172 while ((c = *p++) != '\0') {
173 if (c == 'c' && cmdline) {
175 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
179 if (q == NULL || minusc != NULL)
180 error("Bad -c option");
185 } else if (c == 'o') {
186 minus_o(*argptr, val);
190 if (c == 'p' && !val && privileged) {
191 (void) setuid(getuid());
192 (void) setgid(getgid());
201 minus_o(char *name, int val)
207 /* "Pretty" output. */
208 out1str("Current option settings\n");
209 for (i = 0; i < NOPTS; i++)
210 out1fmt("%-16s%s\n", optlist[i].name,
211 optlist[i].val ? "on" : "off");
213 /* Output suitable for re-input to shell. */
214 for (doneset = i = 0; i < NOPTS; i++)
215 if (optlist[i].val) {
220 out1fmt(" -o %s", optlist[i].name);
226 for (i = 0; i < NOPTS; i++)
227 if (equal(name, optlist[i].name)) {
228 if (!val && privileged && equal(name, "privileged")) {
229 (void) setuid(getuid());
230 (void) setgid(getgid());
232 setoption(optlist[i].letter, val);
235 error("Illegal option -o %s", name);
241 setoption(int flag, int val)
245 for (i = 0; i < NOPTS; i++)
246 if (optlist[i].letter == flag) {
247 optlist[i].val = val;
249 /* #%$ hack for ksh semantics */
252 else if (flag == 'E')
257 error("Illegal option -%c", flag);
268 for (i = 0; i < NOPTS; i++)
277 * Set the shell parameters.
281 setparam(char **argv)
287 for (nparam = 0 ; argv[nparam] ; nparam++);
288 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
290 *ap++ = savestr(*argv++);
293 freeparam(&shellparam);
294 shellparam.malloc = 1;
295 shellparam.nparam = nparam;
296 shellparam.p = newparam;
297 shellparam.optnext = NULL;
302 * Free the list of positional parameters.
306 freeparam(struct shparam *param)
311 for (ap = param->p ; *ap ; ap++)
320 * The shift builtin command.
324 shiftcmd(int argc, char **argv)
332 if (n > shellparam.nparam)
333 error("can't shift that many");
335 shellparam.nparam -= n;
336 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
337 if (shellparam.malloc)
341 while ((*ap2++ = *ap1++) != NULL);
342 shellparam.optnext = NULL;
350 * The set command builtin.
354 setcmd(int argc, char **argv)
357 return showvarscmd(argc, argv);
361 if (*argptr != NULL) {
370 getoptsreset(const char *value)
372 if (number(value) == 1) {
373 shellparam.optnext = NULL;
374 shellparam.reset = 1;
379 * The getopts builtin. Shellparam.optnext points to the next argument
380 * to be processed. Shellparam.optptr points to the next character to
381 * be processed in the current argument. If shellparam.optnext is NULL,
382 * then it's the first time getopts has been called.
386 getoptscmd(int argc, char **argv)
388 char **optbase = NULL;
391 error("usage: getopts optstring var [arg]");
393 optbase = shellparam.p;
397 if (shellparam.reset == 1) {
398 shellparam.optnext = optbase;
399 shellparam.optptr = NULL;
400 shellparam.reset = 0;
403 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
408 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
418 if ((p = *optptr) == NULL || *p == '\0') {
419 /* Current word is done, advance */
420 if (*optnext == NULL)
423 if (p == NULL || *p != '-' || *++p == '\0') {
425 ind = *optnext - optfirst + 1;
432 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
437 for (q = optstr; *q != c; ) {
439 if (optstr[0] == ':') {
442 err |= setvarsafe("OPTARG", s, 0);
445 out1fmt("Illegal option -%c\n", c);
446 (void) unsetvar("OPTARG");
456 if (*p == '\0' && (p = **optnext) == NULL) {
457 if (optstr[0] == ':') {
460 err |= setvarsafe("OPTARG", s, 0);
464 out1fmt("No arg for -%c option\n", c);
465 (void) unsetvar("OPTARG");
473 setvarsafe("OPTARG", p, 0);
477 setvarsafe("OPTARG", "", 0);
478 ind = *optnext - optfirst + 1;
487 fmtstr(s, sizeof(s), "%d", ind);
488 err |= setvarsafe("OPTIND", s, VNOFUNC);
491 err |= setvarsafe(optvar, s, 0);
502 * XXX - should get rid of. have all builtins use getopt(3). the
503 * library getopt must have the BSD extension static variable "optreset"
504 * otherwise it can't be used within the shell safely.
506 * Standard option processing (a la getopt) for builtin routines. The
507 * only argument that is passed to nextopt is the option string; the
508 * other arguments are unnecessary. It return the character, or '\0' on
513 nextopt(char *optstring)
518 if ((p = optptr) == NULL || *p == '\0') {
520 if (p == NULL || *p != '-' || *++p == '\0')
523 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
527 for (q = optstring ; *q != c ; ) {
529 error("Illegal option -%c", c);
534 if (*p == '\0' && (p = *argptr++) == NULL)
535 error("No arg for -%c option", c);