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 /* turn on tabcomplete in an interactive shell by default */
102 for (i
= 0; i
< NOPTS
; i
++)
103 if (optlist
[i
].val
== 2)
106 if (sflag
== 0 && minusc
== NULL
) {
107 commandname
= arg0
= *argptr
++;
108 setinputfile(commandname
, 0);
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); */
128 setinteractive(iflag
);
136 * Process shell options. The global variable argptr contains a pointer
137 * to the argument list; we advance it past the options.
149 while ((p
= *argptr
) != NULL
) {
151 if ((c
= *p
++) == '-') {
153 /* A "-" or "--" terminates options */
156 if (p
[0] == '-' && p
[1] == '\0')
159 * For the benefit of `#!' lines in shell scripts,
160 * treat a string of '-- *#.*' the same as '--'.
161 * This is needed so that a script starting with:
162 * #!/bin/sh -- # -*- perl -*-
163 * will continue to work after a change is made to
164 * kern/imgact_shell.c to NOT token-ize the options
165 * specified on a '#!' line. A bit of a kludge,
166 * but that trick is recommended in documentation
167 * for some scripting languages, and we might as
168 * well continue to support it.
172 while (*kp
== ' ' || *kp
== '\t')
174 if (*kp
== '#' || *kp
== '\0')
177 } else if (c
== '+') {
183 while ((c
= *p
++) != '\0') {
184 if (c
== 'c' && cmdline
) {
186 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
190 if (q
== NULL
|| minusc
!= NULL
)
191 error("Bad -c option");
196 } else if (c
== 'o') {
197 minus_o(*argptr
, val
);
201 if (c
== 'p' && !val
&& privileged
) {
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
)
256 /* "Pretty" output. */
257 out1str("Current option settings\n");
258 for (i
= 0; i
< NOPTS
; i
++)
259 out1fmt("%-16s%s\n", optlist
[i
].name
,
260 optlist
[i
].val
? "on" : "off");
262 /* Output suitable for re-input to shell. */
263 for (i
= 0; i
< NOPTS
; i
++) {
265 out1str(i
== 0 ? "set" : "\nset");
266 out1fmt(" %co %s", optlist
[i
].val
? '-' : '+',
272 for (i
= 0; i
< NOPTS
; i
++)
273 if (equal(name
, optlist
[i
].name
)) {
274 if (!val
&& privileged
&& equal(name
, "privileged")) {
278 setoption(optlist
[i
].letter
, val
);
281 error("Illegal option -o %s", name
);
287 setoption(int flag
, int val
)
291 for (i
= 0; i
< NOPTS
; i
++)
292 if (optlist
[i
].letter
== flag
) {
293 optlist
[i
].val
= val
;
295 /* #%$ hack for ksh semantics */
298 else if (flag
== 'E')
303 error("Illegal option -%c", flag
);
314 for (i
= 0; i
< NOPTS
; i
++)
323 * Set the shell parameters.
327 setparam(char **argv
)
333 for (nparam
= 0 ; argv
[nparam
] ; nparam
++);
334 ap
= newparam
= ckmalloc((nparam
+ 1) * sizeof *ap
);
336 *ap
++ = savestr(*argv
++);
339 freeparam(&shellparam
);
340 shellparam
.malloc
= 1;
341 shellparam
.nparam
= nparam
;
342 shellparam
.p
= newparam
;
343 shellparam
.optnext
= NULL
;
348 * Free the list of positional parameters.
352 freeparam(volatile struct shparam
*param
)
357 for (ap
= param
->p
; *ap
; ap
++)
366 * The shift builtin command.
370 shiftcmd(int argc
, char **argv
)
378 if (n
> shellparam
.nparam
)
381 shellparam
.nparam
-= n
;
382 for (ap1
= shellparam
.p
; --n
>= 0 ; ap1
++) {
383 if (shellparam
.malloc
)
387 while ((*ap2
++ = *ap1
++) != NULL
);
388 shellparam
.optnext
= NULL
;
396 * The set command builtin.
400 setcmd(int argc
, char **argv
)
403 return showvarscmd(argc
, argv
);
407 if (*argptr
!= NULL
) {
416 getoptsreset(const char *value
)
418 if (number(value
) == 1) {
419 shellparam
.optnext
= NULL
;
420 shellparam
.reset
= 1;
425 * The getopts builtin. Shellparam.optnext points to the next argument
426 * to be processed. Shellparam.optptr points to the next character to
427 * be processed in the current argument. If shellparam.optnext is NULL,
428 * then it's the first time getopts has been called.
432 getoptscmd(int argc
, char **argv
)
434 char **optbase
= NULL
;
437 error("usage: getopts optstring var [arg]");
439 optbase
= shellparam
.p
;
443 if (shellparam
.reset
== 1) {
444 shellparam
.optnext
= optbase
;
445 shellparam
.optptr
= NULL
;
446 shellparam
.reset
= 0;
449 return getopts(argv
[1], argv
[2], optbase
, &shellparam
.optnext
,
454 getopts(char *optstr
, char *optvar
, char **optfirst
, char ***optnext
,
464 if ((p
= *optp
) == NULL
|| *p
== '\0') {
465 /* Current word is done, advance */
466 if (*optnext
== NULL
)
469 if (p
== NULL
|| *p
!= '-' || *++p
== '\0') {
471 ind
= *optnext
- optfirst
+ 1;
478 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
483 for (q
= optstr
; *q
!= c
; ) {
485 if (optstr
[0] == ':') {
488 err
|= setvarsafe("OPTARG", s
, 0);
491 out1fmt("Illegal option -%c\n", c
);
502 if (*p
== '\0' && (p
= **optnext
) == NULL
) {
503 if (optstr
[0] == ':') {
506 err
|= setvarsafe("OPTARG", s
, 0);
510 out1fmt("No arg for -%c option\n", c
);
519 setvarsafe("OPTARG", p
, 0);
523 setvarsafe("OPTARG", "", 0);
524 ind
= *optnext
- optfirst
+ 1;
533 fmtstr(s
, sizeof(s
), "%d", ind
);
534 err
|= setvarsafe("OPTIND", s
, VNOFUNC
);
537 err
|= setvarsafe(optvar
, s
, 0);
548 * XXX - should get rid of. have all builtins use getopt(3). the
549 * library getopt must have the BSD extension static variable "optreset"
550 * otherwise it can't be used within the shell safely.
552 * Standard option processing (a la getopt) for builtin routines. The
553 * only argument that is passed to nextopt is the option string; the
554 * other arguments are unnecessary. It return the character, or '\0' on
559 nextopt(const char *optstring
)
564 if ((p
= optptr
) == NULL
|| *p
== '\0') {
566 if (p
== NULL
|| *p
!= '-' || *++p
== '\0')
569 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
573 for (q
= optstring
; *q
!= c
; ) {
575 error("Illegal option -%c", c
);
580 if (*p
== '\0' && (p
= *argptr
++) == NULL
)
581 error("No arg for -%c option", c
);