2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #define DEFINE_OPTIONS
43 #include "nodes.h" /* for other header files */
55 #include "myhistedit.h"
58 char *arg0; /* value of $0 */
59 struct shparam shellparam; /* current positional parameters */
60 char **argptr; /* argument list for builtin commands */
61 char *shoptarg; /* set by nextopt (like getopt) */
62 char *nextopt_optptr; /* used by nextopt */
64 char *minusc; /* argument to -c option */
67 static void options(int);
68 static void minus_o(char *, int);
69 static void setoption(int, int);
70 static void setoptionbyindex(int, int);
71 static void setparam(int, char **);
72 static int getopts(char *, char *, char **, char ***, char **);
76 * Process the shell command line arguments.
80 procargs(int argc, char **argv)
88 for (i = 0; i < NOPTS; i++)
90 privileged = (getuid() != geteuid() || getgid() != getegid());
92 if (*argptr == NULL && minusc == NULL)
94 if (iflag != 0 && sflag == 1 && isatty(0) && isatty(1)) {
101 for (i = 0; i < NOPTS; i++)
105 if (sflag == 0 && minusc == NULL) {
106 scriptname = *argptr++;
107 setinputfile(scriptname, 0, -1 /* verify */);
108 commandname = arg0 = scriptname;
110 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
111 if (argptr && minusc && *argptr)
114 shellparam.p = argptr;
115 shellparam.reset = 1;
116 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
136 * Process shell options. The global variable argptr contains a pointer
137 * to the argument list; we advance it past the options.
138 * If cmdline is true, process the shell's argv; otherwise, process arguments
139 * to the set special builtin.
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) {
190 if (q == NULL || minusc != NULL)
191 error("Bad -c option");
193 } else if (c == 'o') {
194 minus_o(*argptr, val);
203 /* When processing `set', a single "-" means turn off -x and -v */
211 * When processing `set', a "--" means the remaining arguments
212 * replace the positional parameters in the active shell. If
213 * there are no remaining options, then all the positional
214 * parameters are cleared (equivalent to doing ``shift $#'').
224 * At this point we are processing options given to 'sh' on a command
225 * line. If an end-of-options marker ("-" or "--") is followed by an
226 * arg of "#", then skip over all remaining arguments. Some scripting
227 * languages (e.g.: perl) document that /bin/sh will implement this
228 * behavior, and they recommend that users take advantage of it to
229 * solve certain issues that can come up when writing a perl script.
230 * Yes, this feature is in /bin/sh to help users write perl scripts.
233 if (p != NULL && p[0] == '#' && p[1] == '\0') {
234 while (*argptr != NULL)
236 /* We need to keep the final argument */
242 minus_o(char *name, int val)
245 const unsigned char *on;
250 /* "Pretty" output. */
251 out1str("Current option settings\n");
252 for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
253 out1fmt("%-16.*s%s\n", *on, on + 1,
254 optval[i] ? "on" : "off");
256 /* Output suitable for re-input to shell. */
257 for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
258 out1fmt("%s %co %.*s%s",
259 i % 6 == 0 ? "set" : "",
260 optval[i] ? '-' : '+',
262 i % 6 == 5 || i == NOPTS - 1 ? "\n" : "");
266 for (i = 0, on = optname; i < NOPTS; i++, on += *on + 1)
267 if (*on == len && memcmp(on + 1, name, len) == 0) {
268 setoptionbyindex(i, val);
271 error("Illegal option -o %s", name);
277 setoptionbyindex(int idx, int val)
279 if (&optval[idx] == &privileged && !val && privileged) {
280 if (setgid(getgid()) == -1)
282 if (setuid(getuid()) == -1)
287 /* #%$ hack for ksh semantics */
288 if (&optval[idx] == &Vflag)
290 else if (&optval[idx] == &Eflag)
296 setoption(int flag, int val)
300 for (i = 0; i < NSHORTOPTS; i++)
301 if (optletter[i] == flag) {
302 setoptionbyindex(i, val);
305 error("Illegal option -%c", flag);
310 * Set the shell parameters.
314 setparam(int argc, char **argv)
319 ap = newparam = ckmalloc((argc + 1) * sizeof *ap);
321 *ap++ = savestr(*argv++);
324 freeparam(&shellparam);
325 shellparam.malloc = 1;
326 shellparam.nparam = argc;
327 shellparam.p = newparam;
328 shellparam.optp = NULL;
329 shellparam.reset = 1;
330 shellparam.optnext = NULL;
335 * Free the list of positional parameters.
339 freeparam(struct shparam *param)
344 for (ap = param->p ; *ap ; ap++)
349 for (ap = param->optp ; *ap ; ap++)
358 * The shift builtin command.
362 shiftcmd(int argc, char **argv)
369 if (n > shellparam.nparam)
372 shellparam.nparam -= n;
373 if (shellparam.malloc)
374 for (i = 0; i < n; i++)
375 ckfree(shellparam.p[i]);
376 memmove(shellparam.p, shellparam.p + n,
377 (shellparam.nparam + 1) * sizeof(shellparam.p[0]));
378 shellparam.reset = 1;
386 * The set builtin command.
390 setcmd(int argc, char **argv)
393 return showvarscmd(argc, argv);
397 if (*argptr != NULL) {
398 setparam(argc - (argptr - argv), argptr);
406 getoptsreset(const char *value)
408 while (*value == '0')
410 if (strcmp(value, "1") == 0)
411 shellparam.reset = 1;
415 * The getopts builtin. Shellparam.optnext points to the next argument
416 * to be processed. Shellparam.optptr points to the next character to
417 * be processed in the current argument. If shellparam.optnext is NULL,
418 * then it's the first time getopts has been called.
422 getoptscmd(int argc, char **argv)
424 char **optbase = NULL, **ap;
428 error("usage: getopts optstring var [arg]");
430 if (shellparam.reset == 1) {
432 if (shellparam.optp) {
433 for (ap = shellparam.optp ; *ap ; ap++)
435 ckfree(shellparam.optp);
436 shellparam.optp = NULL;
439 shellparam.optp = ckmalloc((argc - 2) * sizeof *ap);
440 memset(shellparam.optp, '\0', (argc - 2) * sizeof *ap);
441 for (i = 0; i < argc - 3; i++)
442 shellparam.optp[i] = savestr(argv[i + 3]);
445 optbase = argc == 3 ? shellparam.p : shellparam.optp;
446 shellparam.optnext = optbase;
447 shellparam.optptr = NULL;
448 shellparam.reset = 0;
450 optbase = shellparam.optp ? shellparam.optp : shellparam.p;
452 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
457 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
466 const char *newoptarg = NULL;
468 if ((p = *optptr) == NULL || *p == '\0') {
469 /* Current word is done, advance */
470 if (*optnext == NULL)
473 if (p == NULL || *p != '-' || *++p == '\0') {
475 ind = *optnext - optfirst + 1;
482 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
487 for (q = optstr; *q != c; ) {
489 if (optstr[0] == ':') {
495 out2fmt_flush("Illegal option -%c\n", c);
504 if (*p == '\0' && (p = **optnext) == NULL) {
505 if (optstr[0] == ':') {
512 out2fmt_flush("No arg for -%c option\n", c);
525 if (*optnext != NULL)
526 ind = *optnext - optfirst + 1;
528 if (newoptarg != NULL)
529 err |= setvarsafe("OPTARG", newoptarg, 0);
532 err |= unsetvar("OPTARG");
535 fmtstr(s, sizeof(s), "%d", ind);
536 err |= setvarsafe("OPTIND", s, VNOFUNC);
539 err |= setvarsafe(optvar, s, 0);
550 * Standard option processing (a la getopt) for builtin routines. The
551 * only argument that is passed to nextopt is the option string; the
552 * other arguments are unnecessary. It returns the option, or '\0' on
557 nextopt(const char *optstring)
563 if ((p = nextopt_optptr) == NULL || *p == '\0') {
565 if (p == NULL || *p != '-' || *++p == '\0')
568 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
572 for (q = optstring ; *q != c ; ) {
574 error("Illegal option -%c", c);
579 if (*p == '\0' && (p = *argptr++) == NULL)
580 error("No arg for -%c option", c);
584 if (p != NULL && *p != '\0')
587 nextopt_optptr = NULL;