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. 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 */
61 #include "myhistedit.h"
64 char *arg0; /* value of $0 */
65 struct shparam shellparam; /* current positional parameters */
66 char **argptr; /* argument list for builtin commands */
67 char *shoptarg; /* set by nextopt (like getopt) */
68 char *nextopt_optptr; /* used by nextopt */
70 char *minusc; /* argument to -c option */
73 static void options(int);
74 static void minus_o(char *, int);
75 static void setoption(int, int);
76 static void setoptionbyindex(int, int);
77 static void setparam(int, char **);
78 static int getopts(char *, char *, char **, char ***, char **);
82 * Process the shell command line arguments.
86 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 != 0 && sflag == 1 && isatty(0) && isatty(1)) {
107 for (i = 0; i < NOPTS; i++)
111 if (sflag == 0 && minusc == NULL) {
112 scriptname = *argptr++;
113 setinputfile(scriptname, 0);
114 commandname = arg0 = scriptname;
116 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
117 if (argptr && minusc && *argptr)
120 shellparam.p = argptr;
121 shellparam.reset = 1;
122 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
142 * Process shell options. The global variable argptr contains a pointer
143 * to the argument list; we advance it past the options.
144 * If cmdline is true, process the shell's argv; otherwise, process arguments
145 * to the set special builtin.
157 while ((p = *argptr) != NULL) {
159 if ((c = *p++) == '-') {
161 /* A "-" or "--" terminates options */
164 if (p[0] == '-' && p[1] == '\0')
167 * For the benefit of `#!' lines in shell scripts,
168 * treat a string of '-- *#.*' the same as '--'.
169 * This is needed so that a script starting with:
170 * #!/bin/sh -- # -*- perl -*-
171 * will continue to work after a change is made to
172 * kern/imgact_shell.c to NOT token-ize the options
173 * specified on a '#!' line. A bit of a kludge,
174 * but that trick is recommended in documentation
175 * for some scripting languages, and we might as
176 * well continue to support it.
180 while (*kp == ' ' || *kp == '\t')
182 if (*kp == '#' || *kp == '\0')
185 } else if (c == '+') {
191 while ((c = *p++) != '\0') {
192 if (c == 'c' && cmdline) {
196 if (q == NULL || minusc != NULL)
197 error("Bad -c option");
199 } else if (c == 'o') {
200 minus_o(*argptr, val);
209 /* When processing `set', a single "-" means turn off -x and -v */
217 * When processing `set', a "--" means the remaining arguments
218 * replace the positional parameters in the active shell. If
219 * there are no remaining options, then all the positional
220 * parameters are cleared (equivalent to doing ``shift $#'').
230 * At this point we are processing options given to 'sh' on a command
231 * line. If an end-of-options marker ("-" or "--") is followed by an
232 * arg of "#", then skip over all remaining arguments. Some scripting
233 * languages (e.g.: perl) document that /bin/sh will implement this
234 * behavior, and they recommend that users take advantage of it to
235 * solve certain issues that can come up when writing a perl script.
236 * Yes, this feature is in /bin/sh to help users write perl scripts.
239 if (p != NULL && p[0] == '#' && p[1] == '\0') {
240 while (*argptr != NULL)
242 /* We need to keep the final argument */
248 minus_o(char *name, int val)
251 const unsigned char *on;
256 /* "Pretty" output. */
257 out1str("Current option settings\n");
258 for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
259 out1fmt("%-16.*s%s\n", *on, on + 1,
260 optval[i] ? "on" : "off");
262 /* Output suitable for re-input to shell. */
263 for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
264 out1fmt("%s %co %.*s%s",
265 i % 6 == 0 ? "set" : "",
266 optval[i] ? '-' : '+',
268 i % 6 == 5 || i == NOPTS - 1 ? "\n" : "");
272 for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
273 if (*on == len && memcmp(on + 1, name, len) == 0) {
274 setoptionbyindex(i, val);
277 error("Illegal option -o %s", name);
283 setoptionbyindex(int idx, int val)
285 if (&optval[idx] == &privileged && !val && privileged) {
286 if (setgid(getgid()) == -1)
288 if (setuid(getuid()) == -1)
293 /* #%$ hack for ksh semantics */
294 if (&optval[idx] == &Vflag)
296 else if (&optval[idx] == &Eflag)
302 setoption(int flag, int val)
306 for (i = 0; i < NSHORTOPTS; i++)
307 if (optletter[i] == flag) {
308 setoptionbyindex(i, val);
311 error("Illegal option -%c", flag);
316 * Set the shell parameters.
320 setparam(int argc, char **argv)
325 ap = newparam = ckmalloc((argc + 1) * sizeof *ap);
327 *ap++ = savestr(*argv++);
330 freeparam(&shellparam);
331 shellparam.malloc = 1;
332 shellparam.nparam = argc;
333 shellparam.p = newparam;
334 shellparam.optp = NULL;
335 shellparam.reset = 1;
336 shellparam.optnext = NULL;
341 * Free the list of positional parameters.
345 freeparam(struct shparam *param)
350 for (ap = param->p ; *ap ; ap++)
355 for (ap = param->optp ; *ap ; ap++)
364 * The shift builtin command.
368 shiftcmd(int argc, char **argv)
375 if (n > shellparam.nparam)
378 shellparam.nparam -= n;
379 if (shellparam.malloc)
380 for (i = 0; i < n; i++)
381 ckfree(shellparam.p[i]);
382 memmove(shellparam.p, shellparam.p + n,
383 (shellparam.nparam + 1) * sizeof(shellparam.p[0]));
384 shellparam.reset = 1;
392 * The set builtin command.
396 setcmd(int argc, char **argv)
399 return showvarscmd(argc, argv);
403 if (*argptr != NULL) {
404 setparam(argc - (argptr - argv), argptr);
412 getoptsreset(const char *value)
414 while (*value == '0')
416 if (strcmp(value, "1") == 0)
417 shellparam.reset = 1;
421 * The getopts builtin. Shellparam.optnext points to the next argument
422 * to be processed. Shellparam.optptr points to the next character to
423 * be processed in the current argument. If shellparam.optnext is NULL,
424 * then it's the first time getopts has been called.
428 getoptscmd(int argc, char **argv)
430 char **optbase = NULL, **ap;
434 error("usage: getopts optstring var [arg]");
436 if (shellparam.reset == 1) {
438 if (shellparam.optp) {
439 for (ap = shellparam.optp ; *ap ; ap++)
441 ckfree(shellparam.optp);
442 shellparam.optp = NULL;
445 shellparam.optp = ckmalloc((argc - 2) * sizeof *ap);
446 memset(shellparam.optp, '\0', (argc - 2) * sizeof *ap);
447 for (i = 0; i < argc - 3; i++)
448 shellparam.optp[i] = savestr(argv[i + 3]);
451 optbase = argc == 3 ? shellparam.p : shellparam.optp;
452 shellparam.optnext = optbase;
453 shellparam.optptr = NULL;
454 shellparam.reset = 0;
456 optbase = shellparam.optp ? shellparam.optp : shellparam.p;
458 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
463 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
472 const char *newoptarg = NULL;
474 if ((p = *optptr) == NULL || *p == '\0') {
475 /* Current word is done, advance */
476 if (*optnext == NULL)
479 if (p == NULL || *p != '-' || *++p == '\0') {
481 ind = *optnext - optfirst + 1;
488 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
493 for (q = optstr; *q != c; ) {
495 if (optstr[0] == ':') {
501 out2fmt_flush("Illegal option -%c\n", c);
510 if (*p == '\0' && (p = **optnext) == NULL) {
511 if (optstr[0] == ':') {
518 out2fmt_flush("No arg for -%c option\n", c);
531 if (*optnext != NULL)
532 ind = *optnext - optfirst + 1;
534 if (newoptarg != NULL)
535 err |= setvarsafe("OPTARG", newoptarg, 0);
538 err |= unsetvar("OPTARG");
541 fmtstr(s, sizeof(s), "%d", ind);
542 err |= setvarsafe("OPTIND", s, VNOFUNC);
545 err |= setvarsafe(optvar, s, 0);
556 * Standard option processing (a la getopt) for builtin routines. The
557 * only argument that is passed to nextopt is the option string; the
558 * other arguments are unnecessary. It returns the option, or '\0' on
563 nextopt(const char *optstring)
569 if ((p = nextopt_optptr) == NULL || *p == '\0') {
571 if (p == NULL || *p != '-' || *++p == '\0')
574 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
578 for (q = optstring ; *q != c ; ) {
580 error("Illegal option -%c", c);
585 if (*p == '\0' && (p = *argptr++) == NULL)
586 error("No arg for -%c option", c);