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
36 * @(#)options.c 8.2 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/options.c,v 1.15.2.2 2002/07/19 04:38:52 tjr Exp $
38 * $DragonFly: src/bin/sh/options.c,v 1.3 2004/03/19 18:39:41 cpressey Exp $
46 #define DEFINE_OPTIONS
49 #include "nodes.h" /* for other header files */
60 #include "myhistedit.h"
63 char *arg0; /* value of $0 */
64 struct shparam shellparam; /* current positional parameters */
65 char **argptr; /* argument list for builtin commands */
66 const char *shoptarg; /* set by nextopt (like getopt) */
67 const char *optptr; /* used by nextopt */
69 char *minusc; /* argument to -c option */
72 STATIC void options(int);
73 STATIC void minus_o(char *, int);
74 STATIC void setoption(int, int);
75 STATIC int getopts(char *, char *, char **, char ***, char **);
79 * Process the shell command line arguments.
83 procargs(int argc, char **argv)
90 for (i = 0; i < NOPTS; i++)
92 privileged = (getuid() != geteuid() || getgid() != getegid());
94 if (*argptr == NULL && minusc == NULL)
96 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
100 for (i = 0; i < NOPTS; i++)
101 if (optlist[i].val == 2)
104 if (sflag == 0 && minusc == NULL) {
105 commandname = arg0 = *argptr++;
106 setinputfile(commandname, 0);
108 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
109 if (argptr && minusc && *argptr)
112 shellparam.p = argptr;
113 shellparam.reset = 1;
114 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
126 setinteractive(iflag);
134 * Process shell options. The global variable argptr contains a pointer
135 * to the argument list; we advance it past the options.
147 while ((p = *argptr) != NULL) {
149 if ((c = *p++) == '-') {
151 if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
153 /* "-" means turn off -x and -v */
156 /* "--" means reset params */
157 else if (*argptr == NULL)
160 break; /* "-" or "--" terminates options */
162 } else if (c == '+') {
168 while ((c = *p++) != '\0') {
169 if (c == 'c' && cmdline) {
171 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
175 if (q == NULL || minusc != NULL)
176 error("Bad -c option");
181 } else if (c == 'o') {
182 minus_o(*argptr, val);
186 if (c == 'p' && !val && privileged) {
187 (void) setuid(getuid());
188 (void) setgid(getgid());
197 minus_o(char *name, int val)
203 /* "Pretty" output. */
204 out1str("Current option settings\n");
205 for (i = 0; i < NOPTS; i++)
206 out1fmt("%-16s%s\n", optlist[i].name,
207 optlist[i].val ? "on" : "off");
209 /* Output suitable for re-input to shell. */
210 for (doneset = i = 0; i < NOPTS; i++)
211 if (optlist[i].val) {
216 out1fmt(" -o %s", optlist[i].name);
222 for (i = 0; i < NOPTS; i++)
223 if (equal(name, optlist[i].name)) {
224 if (!val && privileged && equal(name, "privileged")) {
225 (void) setuid(getuid());
226 (void) setgid(getgid());
228 setoption(optlist[i].letter, val);
231 error("Illegal option -o %s", name);
237 setoption(int flag, int val)
241 for (i = 0; i < NOPTS; i++)
242 if (optlist[i].letter == flag) {
243 optlist[i].val = val;
245 /* #%$ hack for ksh semantics */
248 else if (flag == 'E')
253 error("Illegal option -%c", flag);
264 for (i = 0; i < NOPTS; i++)
273 * Set the shell parameters.
277 setparam(char **argv)
283 for (nparam = 0 ; argv[nparam] ; nparam++);
284 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
286 *ap++ = savestr(*argv++);
289 freeparam(&shellparam);
290 shellparam.malloc = 1;
291 shellparam.nparam = nparam;
292 shellparam.p = newparam;
293 shellparam.optnext = NULL;
298 * Free the list of positional parameters.
302 freeparam(struct shparam *param)
307 for (ap = param->p ; *ap ; ap++)
316 * The shift builtin command.
320 shiftcmd(int argc, char **argv)
328 if (n > shellparam.nparam)
329 error("can't shift that many");
331 shellparam.nparam -= n;
332 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
333 if (shellparam.malloc)
337 while ((*ap2++ = *ap1++) != NULL);
338 shellparam.optnext = NULL;
346 * The set command builtin.
350 setcmd(int argc, char **argv)
353 return showvarscmd(argc, argv);
357 if (*argptr != NULL) {
366 getoptsreset(const char *value)
368 if (number(value) == 1) {
369 shellparam.optnext = NULL;
370 shellparam.reset = 1;
375 * The getopts builtin. Shellparam.optnext points to the next argument
376 * to be processed. Shellparam.optptr points to the next character to
377 * be processed in the current argument. If shellparam.optnext is NULL,
378 * then it's the first time getopts has been called.
382 getoptscmd(int argc, char **argv)
384 char **optbase = NULL;
387 error("usage: getopts optstring var [arg]");
389 optbase = shellparam.p;
393 if (shellparam.reset == 1) {
394 shellparam.optnext = optbase;
395 shellparam.optptr = NULL;
396 shellparam.reset = 0;
399 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
404 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
414 if ((p = *optptr) == NULL || *p == '\0') {
415 /* Current word is done, advance */
416 if (*optnext == NULL)
419 if (p == NULL || *p != '-' || *++p == '\0') {
421 ind = *optnext - optfirst + 1;
428 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
433 for (q = optstr; *q != c; ) {
435 if (optstr[0] == ':') {
438 err |= setvarsafe("OPTARG", s, 0);
441 out1fmt("Illegal option -%c\n", c);
442 (void) unsetvar("OPTARG");
452 if (*p == '\0' && (p = **optnext) == NULL) {
453 if (optstr[0] == ':') {
456 err |= setvarsafe("OPTARG", s, 0);
460 out1fmt("No arg for -%c option\n", c);
461 (void) unsetvar("OPTARG");
469 setvarsafe("OPTARG", p, 0);
473 setvarsafe("OPTARG", "", 0);
474 ind = *optnext - optfirst + 1;
483 fmtstr(s, sizeof(s), "%d", ind);
484 err |= setvarsafe("OPTIND", s, VNOFUNC);
487 err |= setvarsafe(optvar, s, 0);
498 * XXX - should get rid of. have all builtins use getopt(3). the
499 * library getopt must have the BSD extension static variable "optreset"
500 * otherwise it can't be used within the shell safely.
502 * Standard option processing (a la getopt) for builtin routines. The
503 * only argument that is passed to nextopt is the option string; the
504 * other arguments are unnecessary. It return the character, or '\0' on
509 nextopt(const char *optstring)
514 if ((p = optptr) == NULL || *p == '\0') {
516 if (p == NULL || *p != '-' || *++p == '\0')
519 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
523 for (q = optstring ; *q != c ; ) {
525 error("Illegal option -%c", c);
530 if (*p == '\0' && (p = *argptr++) == NULL)
531 error("No arg for -%c option", c);