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 * 4. 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";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/options.c,v 1.21 2004/04/06 20:06:51 markm Exp $");
48 #define DEFINE_OPTIONS
51 #include "nodes.h" /* for other header files */
62 #if !defined(NO_HISTORY)
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 *optptr
; /* used by nextopt */
71 int editable
; /* isatty(0) && isatty(1) */
73 char *minusc
; /* argument to -c option */
76 STATIC
void options(int);
77 STATIC
void minus_o(char *, int);
78 STATIC
void setoption(int, int);
79 STATIC
int getopts(char *, char *, char **, char ***, char **);
83 * Process the shell command line arguments.
87 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 editable
= (isatty(0) && isatty(1));
101 if (iflag
== 2 && sflag
== 1 && editable
)
105 for (i
= 0; i
< NOPTS
; i
++)
106 if (optlist
[i
].val
== 2)
109 if (sflag
== 0 && minusc
== NULL
) {
110 commandname
= arg0
= *argptr
++;
111 setinputfile(commandname
, 0);
113 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
114 if (argptr
&& minusc
&& *argptr
)
117 shellparam
.p
= argptr
;
118 shellparam
.reset
= 1;
119 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
131 setinteractive(iflag
);
132 #if !defined(NO_HISTORY)
139 * Process shell options. The global variable argptr contains a pointer
140 * to the argument list; we advance it past the options.
152 while ((p
= *argptr
) != NULL
) {
154 if ((c
= *p
++) == '-') {
156 if (p
[0] == '\0' || (p
[0] == '-' && p
[1] == '\0')) {
158 /* "-" means turn off -x and -v */
161 /* "--" means reset params */
162 else if (*argptr
== NULL
)
165 break; /* "-" or "--" terminates options */
167 } else if (c
== '+') {
173 while ((c
= *p
++) != '\0') {
174 if (c
== 'c' && cmdline
) {
176 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
180 if (q
== NULL
|| minusc
!= NULL
)
181 error("Bad -c option");
186 } else if (c
== 'o') {
187 minus_o(*argptr
, val
);
191 if (c
== 'p' && !val
&& privileged
) {
192 (void) setuid(getuid());
193 (void) setgid(getgid());
202 minus_o(char *name
, int val
)
208 /* "Pretty" output. */
209 out1str("Current option settings\n");
210 for (i
= 0; i
< NOPTS
; i
++)
211 out1fmt("%-16s%s\n", optlist
[i
].name
,
212 optlist
[i
].val
? "on" : "off");
214 /* Output suitable for re-input to shell. */
215 for (doneset
= i
= 0; i
< NOPTS
; i
++)
216 if (optlist
[i
].val
) {
221 out1fmt(" -o %s", optlist
[i
].name
);
227 for (i
= 0; i
< NOPTS
; i
++)
228 if (equal(name
, optlist
[i
].name
)) {
229 if (!val
&& privileged
&& equal(name
, "privileged")) {
230 (void) setuid(getuid());
231 (void) setgid(getgid());
233 setoption(optlist
[i
].letter
, val
);
236 error("Illegal option -o %s", name
);
242 setoption(int flag
, int val
)
246 for (i
= 0; i
< NOPTS
; i
++)
247 if (optlist
[i
].letter
== flag
) {
248 optlist
[i
].val
= val
;
250 /* #%$ hack for ksh semantics */
253 else if (flag
== 'E')
258 error("Illegal option -%c", flag
);
269 for (i
= 0; i
< NOPTS
; i
++)
278 * Set the shell parameters.
282 setparam(char **argv
)
288 for (nparam
= 0 ; argv
[nparam
] ; nparam
++);
289 ap
= newparam
= ckmalloc((nparam
+ 1) * sizeof *ap
);
291 *ap
++ = savestr(*argv
++);
294 freeparam(&shellparam
);
295 shellparam
.malloc
= 1;
296 shellparam
.nparam
= nparam
;
297 shellparam
.p
= newparam
;
298 shellparam
.optnext
= NULL
;
303 * Free the list of positional parameters.
307 freeparam(struct shparam
*param
)
312 for (ap
= param
->p
; *ap
; ap
++)
321 * The shift builtin command.
325 shiftcmd(int argc
, char **argv
)
333 if (n
> shellparam
.nparam
)
334 error("can't shift that many");
336 shellparam
.nparam
-= n
;
337 for (ap1
= shellparam
.p
; --n
>= 0 ; ap1
++) {
338 if (shellparam
.malloc
)
342 while ((*ap2
++ = *ap1
++) != NULL
);
343 shellparam
.optnext
= NULL
;
351 * The set command builtin.
355 setcmd(int argc
, char **argv
)
358 return showvarscmd(argc
, argv
);
362 if (*argptr
!= NULL
) {
371 getoptsreset(const char *value
)
373 if (number(value
) == 1) {
374 shellparam
.optnext
= NULL
;
375 shellparam
.reset
= 1;
380 * The getopts builtin. Shellparam.optnext points to the next argument
381 * to be processed. Shellparam.optptr points to the next character to
382 * be processed in the current argument. If shellparam.optnext is NULL,
383 * then it's the first time getopts has been called.
387 getoptscmd(int argc
, char **argv
)
389 char **optbase
= NULL
;
392 error("usage: getopts optstring var [arg]");
394 optbase
= shellparam
.p
;
398 if (shellparam
.reset
== 1) {
399 shellparam
.optnext
= optbase
;
400 shellparam
.optptr
= NULL
;
401 shellparam
.reset
= 0;
404 return getopts(argv
[1], argv
[2], optbase
, &shellparam
.optnext
,
409 getopts(char *optstr
, char *optvar
, char **optfirst
, char ***optnext
,
419 if ((p
= *optptr
) == NULL
|| *p
== '\0') {
420 /* Current word is done, advance */
421 if (*optnext
== NULL
)
424 if (p
== NULL
|| *p
!= '-' || *++p
== '\0') {
426 ind
= *optnext
- optfirst
+ 1;
433 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
438 for (q
= optstr
; *q
!= c
; ) {
440 if (optstr
[0] == ':') {
443 err
|= setvarsafe("OPTARG", s
, 0);
446 out1fmt("Illegal option -%c\n", c
);
447 (void) unsetvar("OPTARG");
457 if (*p
== '\0' && (p
= **optnext
) == NULL
) {
458 if (optstr
[0] == ':') {
461 err
|= setvarsafe("OPTARG", s
, 0);
465 out1fmt("No arg for -%c option\n", c
);
466 (void) unsetvar("OPTARG");
474 setvarsafe("OPTARG", p
, 0);
478 setvarsafe("OPTARG", "", 0);
479 ind
= *optnext
- optfirst
+ 1;
488 fmtstr(s
, sizeof(s
), "%d", ind
);
489 err
|= setvarsafe("OPTIND", s
, VNOFUNC
);
492 err
|= setvarsafe(optvar
, s
, 0);
503 * XXX - should get rid of. have all builtins use getopt(3). the
504 * library getopt must have the BSD extension static variable "optreset"
505 * otherwise it can't be used within the shell safely.
507 * Standard option processing (a la getopt) for builtin routines. The
508 * only argument that is passed to nextopt is the option string; the
509 * other arguments are unnecessary. It return the character, or '\0' on
514 nextopt(char *optstring
)
519 if ((p
= optptr
) == NULL
|| *p
== '\0') {
521 if (p
== NULL
|| *p
!= '-' || *++p
== '\0')
524 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
528 for (q
= optstring
; *q
!= c
; ) {
530 error("Illegal option -%c", c
);
535 if (*p
== '\0' && (p
= *argptr
++) == NULL
)
536 error("No arg for -%c option", c
);
545 * $PchId: options.c,v 1.6 2006/05/29 13:09:12 philip Exp $