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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)options.c 8.2 (Berkeley) 5/4/95
33 * $FreeBSD: head/bin/sh/options.c 240247 2012-09-08 19:24:03Z jilles $
41 #define DEFINE_OPTIONS
44 #include "nodes.h" /* for other header files */
56 #include "myhistedit.h"
59 char *arg0; /* value of $0 */
60 struct shparam shellparam; /* current positional parameters */
61 char **argptr; /* argument list for builtin commands */
62 const char *shoptarg; /* set by nextopt (like getopt) */
63 char *nextopt_optptr; /* used by nextopt */
65 char *minusc; /* argument to -c option */
68 static void options(int);
69 static void minus_o(char *, int);
70 static void setoption(int, int);
71 static int getopts(char *, char *, char **, char ***, char **);
75 * Process the shell command line arguments.
79 procargs(int argc, char **argv)
87 for (i = 0; i < NOPTS; i++)
89 privileged = (getuid() != geteuid() || getgid() != getegid());
91 if (*argptr == NULL && minusc == NULL)
93 if (iflag != 0 && sflag == 1 && isatty(0) && isatty(1)) {
100 /* turn on tabcomplete in an interactive shell by default */
102 for (i = 0; i < NOPTS; i++)
103 if (optlist[i].val == 2)
106 if (sflag == 0 && minusc == NULL) {
107 scriptname = *argptr++;
108 setinputfile(scriptname, 0);
109 commandname = arg0 = scriptname;
111 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
112 if (argptr && minusc && *argptr)
115 shellparam.p = argptr;
116 shellparam.reset = 1;
117 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
129 setinteractive(iflag);
137 * Process shell options. The global variable argptr contains a pointer
138 * to the argument list; we advance it past the options.
150 while ((p = *argptr) != NULL) {
152 if ((c = *p++) == '-') {
154 /* A "-" or "--" terminates options */
157 if (p[0] == '-' && p[1] == '\0')
160 * For the benefit of `#!' lines in shell scripts,
161 * treat a string of '-- *#.*' the same as '--'.
162 * This is needed so that a script starting with:
163 * #!/bin/sh -- # -*- perl -*-
164 * will continue to work after a change is made to
165 * kern/imgact_shell.c to NOT token-ize the options
166 * specified on a '#!' line. A bit of a kludge,
167 * but that trick is recommended in documentation
168 * for some scripting languages, and we might as
169 * well continue to support it.
173 while (*kp == ' ' || *kp == '\t')
175 if (*kp == '#' || *kp == '\0')
178 } else if (c == '+') {
184 while ((c = *p++) != '\0') {
185 if (c == 'c' && cmdline) {
187 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
191 if (q == NULL || minusc != NULL)
192 error("Bad -c option");
197 } else if (c == 'o') {
198 minus_o(*argptr, val);
207 /* When processing `set', a single "-" means turn off -x and -v */
215 * When processing `set', a "--" means the remaining arguments
216 * replace the positional parameters in the active shell. If
217 * there are no remaining options, then all the positional
218 * parameters are cleared (equivalent to doing ``shift $#'').
228 * At this point we are processing options given to 'sh' on a command
229 * line. If an end-of-options marker ("-" or "--") is followed by an
230 * arg of "#", then skip over all remaining arguments. Some scripting
231 * languages (e.g.: perl) document that /bin/sh will implement this
232 * behavior, and they recommend that users take advantage of it to
233 * solve certain issues that can come up when writing a perl script.
234 * Yes, this feature is in /bin/sh to help users write perl scripts.
237 if (p != NULL && p[0] == '#' && p[1] == '\0') {
238 while (*argptr != NULL)
240 /* We need to keep the final argument */
246 minus_o(char *name, int val)
252 /* "Pretty" output. */
253 out1str("Current option settings\n");
254 for (i = 0; i < NOPTS; i++)
255 out1fmt("%-16s%s\n", optlist[i].name,
256 optlist[i].val ? "on" : "off");
258 /* Output suitable for re-input to shell. */
259 for (i = 0; i < NOPTS; i++)
260 out1fmt("%s %co %s%s",
261 i % 6 == 0 ? "set" : "",
262 optlist[i].val ? '-' : '+',
264 i % 6 == 5 || i == NOPTS - 1 ? "\n" : "");
267 for (i = 0; i < NOPTS; i++)
268 if (equal(name, optlist[i].name)) {
269 setoption(optlist[i].letter, val);
272 error("Illegal option -o %s", name);
278 setoption(int flag, int val)
282 if (flag == 'p' && !val && privileged) {
283 if (setgid(getgid()) == -1)
285 if (setuid(getuid()) == -1)
288 for (i = 0; i < NOPTS; i++)
289 if (optlist[i].letter == flag) {
290 optlist[i].val = val;
292 /* #%$ hack for ksh semantics */
295 else if (flag == 'E')
300 error("Illegal option -%c", flag);
305 * Set the shell parameters.
309 setparam(char **argv)
315 for (nparam = 0 ; argv[nparam] ; nparam++);
316 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
318 *ap++ = savestr(*argv++);
321 freeparam(&shellparam);
322 shellparam.malloc = 1;
323 shellparam.nparam = nparam;
324 shellparam.p = newparam;
325 shellparam.reset = 1;
326 shellparam.optnext = NULL;
331 * Free the list of positional parameters.
335 freeparam(volatile struct shparam *param)
340 for (ap = param->p ; *ap ; ap++)
349 * The shift builtin command.
353 shiftcmd(int argc, char **argv)
361 if (n > shellparam.nparam)
364 shellparam.nparam -= n;
365 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
366 if (shellparam.malloc)
370 while ((*ap2++ = *ap1++) != NULL);
371 shellparam.reset = 1;
379 * The set command builtin.
383 setcmd(int argc, char **argv)
386 return showvarscmd(argc, argv);
390 if (*argptr != NULL) {
399 getoptsreset(const char *value)
401 while (*value == '0')
403 if (strcmp(value, "1") == 0)
404 shellparam.reset = 1;
408 * The getopts builtin. Shellparam.optnext points to the next argument
409 * to be processed. Shellparam.optptr points to the next character to
410 * be processed in the current argument. If shellparam.optnext is NULL,
411 * then it's the first time getopts has been called.
415 getoptscmd(int argc, char **argv)
417 char **optbase = NULL;
420 error("usage: getopts optstring var [arg]");
422 optbase = shellparam.p;
426 if (shellparam.reset == 1) {
427 shellparam.optnext = optbase;
428 shellparam.optptr = NULL;
429 shellparam.reset = 0;
432 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
437 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
447 if ((p = *optp) == NULL || *p == '\0') {
448 /* Current word is done, advance */
449 if (*optnext == NULL)
452 if (p == NULL || *p != '-' || *++p == '\0') {
454 ind = *optnext - optfirst + 1;
461 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
466 for (q = optstr; *q != c; ) {
468 if (optstr[0] == ':') {
471 err |= setvarsafe("OPTARG", s, 0);
474 out1fmt("Illegal option -%c\n", c);
485 if (*p == '\0' && (p = **optnext) == NULL) {
486 if (optstr[0] == ':') {
489 err |= setvarsafe("OPTARG", s, 0);
493 out1fmt("No arg for -%c option\n", c);
502 setvarsafe("OPTARG", p, 0);
506 setvarsafe("OPTARG", "", 0);
507 ind = *optnext - optfirst + 1;
516 fmtstr(s, sizeof(s), "%d", ind);
517 err |= setvarsafe("OPTIND", s, VNOFUNC);
520 err |= setvarsafe(optvar, s, 0);
531 * Standard option processing (a la getopt) for builtin routines. The
532 * only argument that is passed to nextopt is the option string; the
533 * other arguments are unnecessary. It return the character, or '\0' on
538 nextopt(const char *optstring)
544 if ((p = nextopt_optptr) == NULL || *p == '\0') {
546 if (p == NULL || *p != '-' || *++p == '\0')
549 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
553 for (q = optstring ; *q != c ; ) {
555 error("Illegal option -%c", c);
560 if (*p == '\0' && (p = *argptr++) == NULL)
561 error("No arg for -%c option", c);