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
37 static char sccsid
[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: head/bin/sh/options.c 326025 2017-11-20 19:49:47Z pfg $");
48 #define DEFINE_OPTIONS
51 #include "nodes.h" /* for other header files */
63 #include "myhistedit.h"
66 char *arg0
; /* value of $0 */
67 struct shparam shellparam
; /* current positional parameters */
68 char **argptr
; /* argument list for builtin commands */
69 char *shoptarg
; /* set by nextopt (like getopt) */
70 char *nextopt_optptr
; /* used by nextopt */
72 char *minusc
; /* argument to -c option */
75 static void options(int);
76 static void minus_o(char *, int);
77 static void setoption(int, int);
78 static void setoptionbyindex(int, int);
79 static void setparam(int, char **);
80 static int getopts(char *, char *, char **, char ***, char **);
84 * Process the shell command line arguments.
88 procargs(int argc
, char **argv
)
96 for (i
= 0; i
< NOPTS
; i
++)
98 privileged
= (getuid() != geteuid() || getgid() != getegid());
100 if (*argptr
== NULL
&& minusc
== NULL
)
102 if (iflag
!= 0 && sflag
== 1 && isatty(0) && isatty(1)) {
109 for (i
= 0; i
< NOPTS
; i
++)
113 if (sflag
== 0 && minusc
== NULL
) {
114 scriptname
= *argptr
++;
115 setinputfile(scriptname
, 0);
116 commandname
= arg0
= scriptname
;
118 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
119 if (argptr
&& minusc
&& *argptr
)
122 shellparam
.p
= argptr
;
123 shellparam
.reset
= 1;
124 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
144 * Process shell options. The global variable argptr contains a pointer
145 * to the argument list; we advance it past the options.
146 * If cmdline is true, process the shell's argv; otherwise, process arguments
147 * to the set special builtin.
159 while ((p
= *argptr
) != NULL
) {
161 if ((c
= *p
++) == '-') {
163 /* A "-" or "--" terminates options */
166 if (p
[0] == '-' && p
[1] == '\0')
169 * For the benefit of `#!' lines in shell scripts,
170 * treat a string of '-- *#.*' the same as '--'.
171 * This is needed so that a script starting with:
172 * #!/bin/sh -- # -*- perl -*-
173 * will continue to work after a change is made to
174 * kern/imgact_shell.c to NOT token-ize the options
175 * specified on a '#!' line. A bit of a kludge,
176 * but that trick is recommended in documentation
177 * for some scripting languages, and we might as
178 * well continue to support it.
182 while (*kp
== ' ' || *kp
== '\t')
184 if (*kp
== '#' || *kp
== '\0')
187 } else if (c
== '+') {
193 while ((c
= *p
++) != '\0') {
194 if (c
== 'c' && cmdline
) {
198 if (q
== NULL
|| minusc
!= NULL
)
199 error("Bad -c option");
201 } else if (c
== 'o') {
202 minus_o(*argptr
, val
);
211 /* When processing `set', a single "-" means turn off -x and -v */
219 * When processing `set', a "--" means the remaining arguments
220 * replace the positional parameters in the active shell. If
221 * there are no remaining options, then all the positional
222 * parameters are cleared (equivalent to doing ``shift $#'').
232 * At this point we are processing options given to 'sh' on a command
233 * line. If an end-of-options marker ("-" or "--") is followed by an
234 * arg of "#", then skip over all remaining arguments. Some scripting
235 * languages (e.g.: perl) document that /bin/sh will implement this
236 * behavior, and they recommend that users take advantage of it to
237 * solve certain issues that can come up when writing a perl script.
238 * Yes, this feature is in /bin/sh to help users write perl scripts.
241 if (p
!= NULL
&& p
[0] == '#' && p
[1] == '\0') {
242 while (*argptr
!= NULL
)
244 /* We need to keep the final argument */
250 minus_o(char *name
, int val
)
253 const unsigned char *on
;
258 /* "Pretty" output. */
259 out1str("Current option settings\n");
260 for (i
= 0, on
= optname
; i
< NOPTS
; i
++, on
+= *on
+ 1)
261 out1fmt("%-16.*s%s\n", *on
, on
+ 1,
262 optval
[i
] ? "on" : "off");
264 /* Output suitable for re-input to shell. */
265 for (i
= 0, on
= optname
; i
< NOPTS
; i
++, on
+= *on
+ 1)
266 out1fmt("%s %co %.*s%s",
267 i
% 6 == 0 ? "set" : "",
268 optval
[i
] ? '-' : '+',
270 i
% 6 == 5 || i
== NOPTS
- 1 ? "\n" : "");
274 for (i
= 0, on
= optname
; i
< NOPTS
; i
++, on
+= *on
+ 1)
275 if (*on
== len
&& memcmp(on
+ 1, name
, len
) == 0) {
276 setoptionbyindex(i
, val
);
279 error("Illegal option -o %s", name
);
285 setoptionbyindex(int idx
, int val
)
287 if (&optval
[idx
] == &privileged
&& !val
&& privileged
) {
288 if (setgid(getgid()) == -1)
290 if (setuid(getuid()) == -1)
295 /* #%$ hack for ksh semantics */
296 if (&optval
[idx
] == &Vflag
)
298 else if (&optval
[idx
] == &Eflag
)
304 setoption(int flag
, int val
)
308 for (i
= 0; i
< NSHORTOPTS
; i
++)
309 if (optletter
[i
] == flag
) {
310 setoptionbyindex(i
, val
);
313 error("Illegal option -%c", flag
);
318 * Set the shell parameters.
322 setparam(int argc
, char **argv
)
327 ap
= newparam
= ckmalloc((argc
+ 1) * sizeof *ap
);
329 *ap
++ = savestr(*argv
++);
332 freeparam(&shellparam
);
333 shellparam
.malloc
= 1;
334 shellparam
.nparam
= argc
;
335 shellparam
.p
= newparam
;
336 shellparam
.optp
= NULL
;
337 shellparam
.reset
= 1;
338 shellparam
.optnext
= NULL
;
343 * Free the list of positional parameters.
347 freeparam(struct shparam
*param
)
352 for (ap
= param
->p
; *ap
; ap
++)
357 for (ap
= param
->optp
; *ap
; ap
++)
366 * The shift builtin command.
370 shiftcmd(int argc
, char **argv
)
377 if (n
> shellparam
.nparam
)
380 shellparam
.nparam
-= n
;
381 if (shellparam
.malloc
)
382 for (i
= 0; i
< n
; i
++)
383 ckfree(shellparam
.p
[i
]);
384 memmove(shellparam
.p
, shellparam
.p
+ n
,
385 (shellparam
.nparam
+ 1) * sizeof(shellparam
.p
[0]));
386 shellparam
.reset
= 1;
394 * The set builtin command.
398 setcmd(int argc
, char **argv
)
401 return showvarscmd(argc
, argv
);
405 if (*argptr
!= NULL
) {
406 setparam(argc
- (argptr
- argv
), argptr
);
414 getoptsreset(const char *value
)
416 while (*value
== '0')
418 if (strcmp(value
, "1") == 0)
419 shellparam
.reset
= 1;
423 * The getopts builtin. Shellparam.optnext points to the next argument
424 * to be processed. Shellparam.optptr points to the next character to
425 * be processed in the current argument. If shellparam.optnext is NULL,
426 * then it's the first time getopts has been called.
430 getoptscmd(int argc
, char **argv
)
432 char **optbase
= NULL
, **ap
;
436 error("usage: getopts optstring var [arg]");
438 if (shellparam
.reset
== 1) {
440 if (shellparam
.optp
) {
441 for (ap
= shellparam
.optp
; *ap
; ap
++)
443 ckfree(shellparam
.optp
);
444 shellparam
.optp
= NULL
;
447 shellparam
.optp
= ckmalloc((argc
- 2) * sizeof *ap
);
448 memset(shellparam
.optp
, '\0', (argc
- 2) * sizeof *ap
);
449 for (i
= 0; i
< argc
- 3; i
++)
450 shellparam
.optp
[i
] = savestr(argv
[i
+ 3]);
453 optbase
= argc
== 3 ? shellparam
.p
: shellparam
.optp
;
454 shellparam
.optnext
= optbase
;
455 shellparam
.optptr
= NULL
;
456 shellparam
.reset
= 0;
458 optbase
= shellparam
.optp
? shellparam
.optp
: shellparam
.p
;
460 return getopts(argv
[1], argv
[2], optbase
, &shellparam
.optnext
,
465 getopts(char *optstr
, char *optvar
, char **optfirst
, char ***optnext
,
474 const char *newoptarg
= NULL
;
476 if ((p
= *optptr
) == NULL
|| *p
== '\0') {
477 /* Current word is done, advance */
478 if (*optnext
== NULL
)
481 if (p
== NULL
|| *p
!= '-' || *++p
== '\0') {
483 ind
= *optnext
- optfirst
+ 1;
490 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
495 for (q
= optstr
; *q
!= c
; ) {
497 if (optstr
[0] == ':') {
503 out2fmt_flush("Illegal option -%c\n", c
);
512 if (*p
== '\0' && (p
= **optnext
) == NULL
) {
513 if (optstr
[0] == ':') {
520 out2fmt_flush("No arg for -%c option\n", c
);
533 if (*optnext
!= NULL
)
534 ind
= *optnext
- optfirst
+ 1;
536 if (newoptarg
!= NULL
)
537 err
|= setvarsafe("OPTARG", newoptarg
, 0);
540 err
|= unsetvar("OPTARG");
543 fmtstr(s
, sizeof(s
), "%d", ind
);
544 err
|= setvarsafe("OPTIND", s
, VNOFUNC
);
547 err
|= setvarsafe(optvar
, s
, 0);
558 * Standard option processing (a la getopt) for builtin routines. The
559 * only argument that is passed to nextopt is the option string; the
560 * other arguments are unnecessary. It returns the option, or '\0' on
565 nextopt(const char *optstring
)
571 if ((p
= nextopt_optptr
) == NULL
|| *p
== '\0') {
573 if (p
== NULL
|| *p
!= '-' || *++p
== '\0')
576 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
580 for (q
= optstring
; *q
!= c
; ) {
582 error("Illegal option -%c", c
);
587 if (*p
== '\0' && (p
= *argptr
++) == NULL
)
588 error("No arg for -%c option", c
);