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
35 static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
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 char *shoptarg; /* set by nextopt (like getopt) */
67 char *nextopt_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 /* A "-" or "--" terminates options */
154 if (p[0] == '-' && p[1] == '\0')
157 * For the benefit of `#!' lines in shell scripts,
158 * treat a string of '-- *#.*' the same as '--'.
159 * This is needed so that a script starting with:
160 * #!/bin/sh -- # -*- perl -*-
161 * will continue to work after a change is made to
162 * kern/imgact_shell.c to NOT token-ize the options
163 * specified on a '#!' line. A bit of a kludge,
164 * but that trick is recommended in documentation
165 * for some scripting languages, and we might as
166 * well continue to support it.
170 while (*kp == ' ' || *kp == '\t')
172 if (*kp == '#' || *kp == '\0')
175 } else if (c == '+') {
181 while ((c = *p++) != '\0') {
182 if (c == 'c' && cmdline) {
184 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
188 if (q == NULL || minusc != NULL)
189 error("Bad -c option");
194 } else if (c == 'o') {
195 minus_o(*argptr, val);
204 /* When processing `set', a single "-" means turn off -x and -v */
212 * When processing `set', a "--" means the remaining arguments
213 * replace the positional parameters in the active shell. If
214 * there are no remaining options, then all the positional
215 * parameters are cleared (equivalent to doing ``shift $#'').
225 * At this point we are processing options given to 'sh' on a command
226 * line. If an end-of-options marker ("-" or "--") is followed by an
227 * arg of "#", then skip over all remaining arguments. Some scripting
228 * languages (e.g.: perl) document that /bin/sh will implement this
229 * behavior, and they recommend that users take advantage of it to
230 * solve certain issues that can come up when writing a perl script.
231 * Yes, this feature is in /bin/sh to help users write perl scripts.
234 if (p != NULL && p[0] == '#' && p[1] == '\0') {
235 while (*argptr != NULL)
237 /* We need to keep the final argument */
243 minus_o(char *name, int val)
249 /* "Pretty" output. */
250 out1str("Current option settings\n");
251 for (i = 0; i < NOPTS; i++)
252 out1fmt("%-16s%s\n", optlist[i].name,
253 optlist[i].val ? "on" : "off");
255 /* Output suitable for re-input to shell. */
256 for (i = 0; i < NOPTS; i++) {
258 out1str(i == 0 ? "set" : "\nset");
259 out1fmt(" %co %s", optlist[i].val ? '-' : '+',
265 for (i = 0; i < NOPTS; i++)
266 if (equal(name, optlist[i].name)) {
267 setoption(optlist[i].letter, val);
270 error("Illegal option -o %s", name);
276 setoption(int flag, int val)
280 if (flag == 'p' && !val && privileged) {
281 if (setgid(getgid()) == -1)
283 if (setuid(getuid()) == -1)
286 for (i = 0; i < NOPTS; i++)
287 if (optlist[i].letter == flag) {
288 optlist[i].val = val;
290 /* #%$ hack for ksh semantics */
293 else if (flag == 'E')
298 error("Illegal option -%c", flag);
309 for (i = 0; i < NOPTS; i++)
318 * Set the shell parameters.
322 setparam(char **argv)
328 for (nparam = 0 ; argv[nparam] ; nparam++);
329 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
331 *ap++ = savestr(*argv++);
334 freeparam(&shellparam);
335 shellparam.malloc = 1;
336 shellparam.nparam = nparam;
337 shellparam.p = newparam;
338 shellparam.reset = 1;
339 shellparam.optnext = NULL;
344 * Free the list of positional parameters.
348 freeparam(struct shparam *param)
353 for (ap = param->p ; *ap ; ap++)
362 * The shift builtin command.
366 shiftcmd(int argc, char **argv)
374 if (n > shellparam.nparam)
377 shellparam.nparam -= n;
378 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
379 if (shellparam.malloc)
383 while ((*ap2++ = *ap1++) != NULL);
384 shellparam.reset = 1;
392 * The set command builtin.
396 setcmd(int argc, char **argv)
399 return showvarscmd(argc, argv);
403 if (*argptr != NULL) {
412 getoptsreset(const char *value)
414 if (number(value) == 1) {
415 shellparam.reset = 1;
420 * The getopts builtin. Shellparam.optnext points to the next argument
421 * to be processed. Shellparam.optptr points to the next character to
422 * be processed in the current argument. If shellparam.optnext is NULL,
423 * then it's the first time getopts has been called.
427 getoptscmd(int argc, char **argv)
429 char **optbase = NULL;
432 error("usage: getopts optstring var [arg]");
434 optbase = shellparam.p;
438 if (shellparam.reset == 1) {
439 shellparam.optnext = optbase;
440 shellparam.optptr = NULL;
441 shellparam.reset = 0;
444 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
449 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
459 if ((p = *optptr) == NULL || *p == '\0') {
460 /* Current word is done, advance */
461 if (*optnext == NULL)
464 if (p == NULL || *p != '-' || *++p == '\0') {
466 ind = *optnext - optfirst + 1;
473 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
478 for (q = optstr; *q != c; ) {
480 if (optstr[0] == ':') {
483 err |= setvarsafe("OPTARG", s, 0);
486 out1fmt("Illegal option -%c\n", c);
487 (void) unsetvar("OPTARG");
497 if (*p == '\0' && (p = **optnext) == NULL) {
498 if (optstr[0] == ':') {
501 err |= setvarsafe("OPTARG", s, 0);
505 out1fmt("No arg for -%c option\n", c);
506 (void) unsetvar("OPTARG");
514 setvarsafe("OPTARG", p, 0);
518 setvarsafe("OPTARG", "", 0);
519 ind = *optnext - optfirst + 1;
528 fmtstr(s, sizeof(s), "%d", ind);
529 err |= setvarsafe("OPTIND", s, VNOFUNC);
532 err |= setvarsafe(optvar, s, 0);
543 * XXX - should get rid of. have all builtins use getopt(3). the
544 * library getopt must have the BSD extension static variable "optreset"
545 * otherwise it can't be used within the shell safely.
547 * Standard option processing (a la getopt) for builtin routines. The
548 * only argument that is passed to nextopt is the option string; the
549 * other arguments are unnecessary. It return the character, or '\0' on
554 nextopt(const char *optstring)
560 if ((p = nextopt_optptr) == NULL || *p == '\0') {
562 if (p == NULL || *p != '-' || *++p == '\0')
565 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
569 for (q = optstring ; *q != c ; ) {
571 error("Illegal option -%c", c);
576 if (*p == '\0' && (p = *argptr++) == NULL)
577 error("No arg for -%c option", c);