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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)options.c 8.2 (Berkeley) 5/4/95
37 * $FreeBSD: src/bin/sh/options.c,v 1.25 2006/04/09 12:20:42 stefanf Exp $
38 * $DragonFly: src/bin/sh/options.c,v 1.7 2007/01/14 04:41:32 pavalos Exp $
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 const char *shoptarg
; /* set by nextopt (like getopt) */
67 const char *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
);
199 if (c
== 'p' && !val
&& privileged
) {
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
)
254 /* "Pretty" output. */
255 out1str("Current option settings\n");
256 for (i
= 0; i
< NOPTS
; i
++)
257 out1fmt("%-16s%s\n", optlist
[i
].name
,
258 optlist
[i
].val
? "on" : "off");
260 /* Output suitable for re-input to shell. */
261 for (i
= 0; i
< NOPTS
; i
++) {
263 out1str(i
== 0 ? "set" : "\nset");
264 out1fmt(" %co %s", optlist
[i
].val
? '-' : '+',
270 for (i
= 0; i
< NOPTS
; i
++)
271 if (equal(name
, optlist
[i
].name
)) {
272 if (!val
&& privileged
&& equal(name
, "privileged")) {
276 setoption(optlist
[i
].letter
, val
);
279 error("Illegal option -o %s", name
);
285 setoption(int flag
, int val
)
289 for (i
= 0; i
< NOPTS
; i
++)
290 if (optlist
[i
].letter
== flag
) {
291 optlist
[i
].val
= val
;
293 /* #%$ hack for ksh semantics */
296 else if (flag
== 'E')
301 error("Illegal option -%c", flag
);
312 for (i
= 0; i
< NOPTS
; i
++)
321 * Set the shell parameters.
325 setparam(char **argv
)
331 for (nparam
= 0 ; argv
[nparam
] ; nparam
++);
332 ap
= newparam
= ckmalloc((nparam
+ 1) * sizeof *ap
);
334 *ap
++ = savestr(*argv
++);
337 freeparam(&shellparam
);
338 shellparam
.malloc
= 1;
339 shellparam
.nparam
= nparam
;
340 shellparam
.p
= newparam
;
341 shellparam
.optnext
= NULL
;
346 * Free the list of positional parameters.
350 freeparam(volatile struct shparam
*param
)
355 for (ap
= param
->p
; *ap
; ap
++)
364 * The shift builtin command.
368 shiftcmd(int argc
, char **argv
)
376 if (n
> shellparam
.nparam
)
379 shellparam
.nparam
-= n
;
380 for (ap1
= shellparam
.p
; --n
>= 0 ; ap1
++) {
381 if (shellparam
.malloc
)
385 while ((*ap2
++ = *ap1
++) != NULL
);
386 shellparam
.optnext
= NULL
;
394 * The set command builtin.
398 setcmd(int argc
, char **argv
)
401 return showvarscmd(argc
, argv
);
405 if (*argptr
!= NULL
) {
414 getoptsreset(const char *value
)
416 if (number(value
) == 1) {
417 shellparam
.optnext
= NULL
;
418 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
;
435 error("usage: getopts optstring var [arg]");
437 optbase
= shellparam
.p
;
441 if (shellparam
.reset
== 1) {
442 shellparam
.optnext
= optbase
;
443 shellparam
.optptr
= NULL
;
444 shellparam
.reset
= 0;
447 return getopts(argv
[1], argv
[2], optbase
, &shellparam
.optnext
,
452 getopts(char *optstr
, char *optvar
, char **optfirst
, char ***optnext
,
462 if ((p
= *optp
) == NULL
|| *p
== '\0') {
463 /* Current word is done, advance */
464 if (*optnext
== NULL
)
467 if (p
== NULL
|| *p
!= '-' || *++p
== '\0') {
469 ind
= *optnext
- optfirst
+ 1;
476 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
481 for (q
= optstr
; *q
!= c
; ) {
483 if (optstr
[0] == ':') {
486 err
|= setvarsafe("OPTARG", s
, 0);
489 out1fmt("Illegal option -%c\n", c
);
500 if (*p
== '\0' && (p
= **optnext
) == NULL
) {
501 if (optstr
[0] == ':') {
504 err
|= setvarsafe("OPTARG", s
, 0);
508 out1fmt("No arg for -%c option\n", c
);
517 setvarsafe("OPTARG", p
, 0);
521 setvarsafe("OPTARG", "", 0);
522 ind
= *optnext
- optfirst
+ 1;
531 fmtstr(s
, sizeof(s
), "%d", ind
);
532 err
|= setvarsafe("OPTIND", s
, VNOFUNC
);
535 err
|= setvarsafe(optvar
, s
, 0);
546 * XXX - should get rid of. have all builtins use getopt(3). the
547 * library getopt must have the BSD extension static variable "optreset"
548 * otherwise it can't be used within the shell safely.
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 return the character, or '\0' on
557 nextopt(const char *optstring
)
562 if ((p
= optptr
) == NULL
|| *p
== '\0') {
564 if (p
== NULL
|| *p
!= '-' || *++p
== '\0')
567 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
571 for (q
= optstring
; *q
!= c
; ) {
573 error("Illegal option -%c", c
);
578 if (*p
== '\0' && (p
= *argptr
++) == NULL
)
579 error("No arg for -%c option", c
);