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.33 2011/02/04 22:47:55 jilles Exp $
45 #define DEFINE_OPTIONS
48 #include "nodes.h" /* for other header files */
59 #include "myhistedit.h"
62 char *arg0; /* value of $0 */
63 struct shparam shellparam; /* current positional parameters */
64 char **argptr; /* argument list for builtin commands */
65 const char *shoptarg; /* set by nextopt (like getopt) */
66 char *nextopt_optptr; /* used by nextopt */
68 char *minusc; /* argument to -c option */
71 static void options(int);
72 static void minus_o(char *, int);
73 static void setoption(int, int);
74 static int getopts(char *, char *, char **, char ***, char **);
78 * Process the shell command line arguments.
82 procargs(int argc, char **argv)
89 for (i = 0; i < NOPTS; i++)
91 privileged = (getuid() != geteuid() || getgid() != getegid());
93 if (*argptr == NULL && minusc == NULL)
95 if (iflag != 0 && sflag == 1 && isatty(0) && isatty(1)) {
102 /* turn on tabcomplete in an interactive shell by default */
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 /* A "-" or "--" terminates options */
158 if (p[0] == '-' && p[1] == '\0')
161 * For the benefit of `#!' lines in shell scripts,
162 * treat a string of '-- *#.*' the same as '--'.
163 * This is needed so that a script starting with:
164 * #!/bin/sh -- # -*- perl -*-
165 * will continue to work after a change is made to
166 * kern/imgact_shell.c to NOT token-ize the options
167 * specified on a '#!' line. A bit of a kludge,
168 * but that trick is recommended in documentation
169 * for some scripting languages, and we might as
170 * well continue to support it.
174 while (*kp == ' ' || *kp == '\t')
176 if (*kp == '#' || *kp == '\0')
179 } else if (c == '+') {
185 while ((c = *p++) != '\0') {
186 if (c == 'c' && cmdline) {
188 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
192 if (q == NULL || minusc != NULL)
193 error("Bad -c option");
198 } else if (c == 'o') {
199 minus_o(*argptr, val);
203 if (c == 'p' && !val && privileged) {
213 /* When processing `set', a single "-" means turn off -x and -v */
221 * When processing `set', a "--" means the remaining arguments
222 * replace the positional parameters in the active shell. If
223 * there are no remaining options, then all the positional
224 * parameters are cleared (equivalent to doing ``shift $#'').
234 * At this point we are processing options given to 'sh' on a command
235 * line. If an end-of-options marker ("-" or "--") is followed by an
236 * arg of "#", then skip over all remaining arguments. Some scripting
237 * languages (e.g.: perl) document that /bin/sh will implement this
238 * behavior, and they recommend that users take advantage of it to
239 * solve certain issues that can come up when writing a perl script.
240 * Yes, this feature is in /bin/sh to help users write perl scripts.
243 if (p != NULL && p[0] == '#' && p[1] == '\0') {
244 while (*argptr != NULL)
246 /* We need to keep the final argument */
252 minus_o(char *name, int val)
258 /* "Pretty" output. */
259 out1str("Current option settings\n");
260 for (i = 0; i < NOPTS; i++)
261 out1fmt("%-16s%s\n", optlist[i].name,
262 optlist[i].val ? "on" : "off");
264 /* Output suitable for re-input to shell. */
265 for (i = 0; i < NOPTS; i++)
266 out1fmt("%s %co %s%s",
267 i % 6 == 0 ? "set" : "",
268 optlist[i].val ? '-' : '+',
270 i % 6 == 5 || i == NOPTS - 1 ? "\n" : "");
273 for (i = 0; i < NOPTS; i++)
274 if (equal(name, optlist[i].name)) {
275 if (!val && privileged && equal(name, "privileged")) {
279 setoption(optlist[i].letter, val);
282 error("Illegal option -o %s", name);
288 setoption(int flag, int val)
292 for (i = 0; i < NOPTS; i++)
293 if (optlist[i].letter == flag) {
294 optlist[i].val = val;
296 /* #%$ hack for ksh semantics */
299 else if (flag == 'E')
304 error("Illegal option -%c", flag);
309 * Set the shell parameters.
313 setparam(char **argv)
319 for (nparam = 0 ; argv[nparam] ; nparam++);
320 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
322 *ap++ = savestr(*argv++);
325 freeparam(&shellparam);
326 shellparam.malloc = 1;
327 shellparam.nparam = nparam;
328 shellparam.p = newparam;
329 shellparam.reset = 1;
330 shellparam.optnext = NULL;
335 * Free the list of positional parameters.
339 freeparam(volatile struct shparam *param)
344 for (ap = param->p ; *ap ; ap++)
353 * The shift builtin command.
357 shiftcmd(int argc, char **argv)
365 if (n > shellparam.nparam)
368 shellparam.nparam -= n;
369 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
370 if (shellparam.malloc)
374 while ((*ap2++ = *ap1++) != NULL);
375 shellparam.reset = 1;
383 * The set command builtin.
387 setcmd(int argc, char **argv)
390 return showvarscmd(argc, argv);
394 if (*argptr != NULL) {
403 getoptsreset(const char *value)
405 if (number(value) == 1) {
406 shellparam.reset = 1;
411 * The getopts builtin. Shellparam.optnext points to the next argument
412 * to be processed. Shellparam.optptr points to the next character to
413 * be processed in the current argument. If shellparam.optnext is NULL,
414 * then it's the first time getopts has been called.
418 getoptscmd(int argc, char **argv)
420 char **optbase = NULL;
423 error("usage: getopts optstring var [arg]");
425 optbase = shellparam.p;
429 if (shellparam.reset == 1) {
430 shellparam.optnext = optbase;
431 shellparam.optptr = NULL;
432 shellparam.reset = 0;
435 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
440 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
450 if ((p = *optp) == NULL || *p == '\0') {
451 /* Current word is done, advance */
452 if (*optnext == NULL)
455 if (p == NULL || *p != '-' || *++p == '\0') {
457 ind = *optnext - optfirst + 1;
464 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
469 for (q = optstr; *q != c; ) {
471 if (optstr[0] == ':') {
474 err |= setvarsafe("OPTARG", s, 0);
477 out1fmt("Illegal option -%c\n", c);
488 if (*p == '\0' && (p = **optnext) == NULL) {
489 if (optstr[0] == ':') {
492 err |= setvarsafe("OPTARG", s, 0);
496 out1fmt("No arg for -%c option\n", c);
505 setvarsafe("OPTARG", p, 0);
509 setvarsafe("OPTARG", "", 0);
510 ind = *optnext - optfirst + 1;
519 fmtstr(s, sizeof(s), "%d", ind);
520 err |= setvarsafe("OPTIND", s, VNOFUNC);
523 err |= setvarsafe(optvar, s, 0);
534 * XXX - should get rid of. have all builtins use getopt(3). the
535 * library getopt must have the BSD extension static variable "optreset"
536 * otherwise it can't be used within the shell safely.
538 * Standard option processing (a la getopt) for builtin routines. The
539 * only argument that is passed to nextopt is the option string; the
540 * other arguments are unnecessary. It return the character, or '\0' on
545 nextopt(const char *optstring)
551 if ((p = nextopt_optptr) == NULL || *p == '\0') {
553 if (p == NULL || *p != '-' || *++p == '\0')
556 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
560 for (q = optstring ; *q != c ; ) {
562 error("Illegal option -%c", c);
567 if (*p == '\0' && (p = *argptr++) == NULL)
568 error("No arg for -%c option", c);