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. 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";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
46 #define DEFINE_OPTIONS
49 #include "nodes.h" /* for other header files */
61 #include "myhistedit.h"
64 char *arg0
; /* value of $0 */
65 struct shparam shellparam
; /* current positional parameters */
66 char **argptr
; /* argument list for builtin commands */
67 char *shoptarg
; /* set by nextopt (like getopt) */
68 char *nextopt_optptr
; /* used by nextopt */
70 char *minusc
; /* argument to -c option */
73 static void options(int);
74 static void minus_o(char *, int);
75 static void setoption(int, int);
76 static int getopts(char *, char *, char **, char ***, char **);
80 * Process the shell command line arguments.
84 procargs(int argc
, char **argv
)
92 for (i
= 0; i
< NOPTS
; i
++)
94 privileged
= (getuid() != geteuid() || getgid() != getegid());
96 if (*argptr
== NULL
&& minusc
== NULL
)
98 if (iflag
!= 0 && sflag
== 1 && isatty(0) && isatty(1)) {
105 for (i
= 0; i
< NOPTS
; i
++)
106 if (optlist
[i
].val
== 2)
109 if (sflag
== 0 && minusc
== NULL
) {
110 scriptname
= *argptr
++;
111 setinputfile(scriptname
, 0);
112 commandname
= arg0
= scriptname
;
114 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
115 if (argptr
&& minusc
&& *argptr
)
118 shellparam
.p
= argptr
;
119 shellparam
.reset
= 1;
120 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
132 setinteractive(iflag
);
140 * Process shell options. The global variable argptr contains a pointer
141 * to the argument list; we advance it past the options.
153 while ((p
= *argptr
) != NULL
) {
155 if ((c
= *p
++) == '-') {
157 /* A "-" or "--" terminates options */
160 if (p
[0] == '-' && p
[1] == '\0')
163 * For the benefit of `#!' lines in shell scripts,
164 * treat a string of '-- *#.*' the same as '--'.
165 * This is needed so that a script starting with:
166 * #!/bin/sh -- # -*- perl -*-
167 * will continue to work after a change is made to
168 * kern/imgact_shell.c to NOT token-ize the options
169 * specified on a '#!' line. A bit of a kludge,
170 * but that trick is recommended in documentation
171 * for some scripting languages, and we might as
172 * well continue to support it.
176 while (*kp
== ' ' || *kp
== '\t')
178 if (*kp
== '#' || *kp
== '\0')
181 } else if (c
== '+') {
187 while ((c
= *p
++) != '\0') {
188 if (c
== 'c' && cmdline
) {
190 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
194 if (q
== NULL
|| minusc
!= NULL
)
195 error("Bad -c option");
200 } else if (c
== 'o') {
201 minus_o(*argptr
, val
);
210 /* When processing `set', a single "-" means turn off -x and -v */
218 * When processing `set', a "--" means the remaining arguments
219 * replace the positional parameters in the active shell. If
220 * there are no remaining options, then all the positional
221 * parameters are cleared (equivalent to doing ``shift $#'').
231 * At this point we are processing options given to 'sh' on a command
232 * line. If an end-of-options marker ("-" or "--") is followed by an
233 * arg of "#", then skip over all remaining arguments. Some scripting
234 * languages (e.g.: perl) document that /bin/sh will implement this
235 * behavior, and they recommend that users take advantage of it to
236 * solve certain issues that can come up when writing a perl script.
237 * Yes, this feature is in /bin/sh to help users write perl scripts.
240 if (p
!= NULL
&& p
[0] == '#' && p
[1] == '\0') {
241 while (*argptr
!= NULL
)
243 /* We need to keep the final argument */
249 minus_o(char *name
, int val
)
255 /* "Pretty" output. */
256 out1str("Current option settings\n");
257 for (i
= 0; i
< NOPTS
; i
++)
258 out1fmt("%-16s%s\n", optlist
[i
].name
,
259 optlist
[i
].val
? "on" : "off");
261 /* Output suitable for re-input to shell. */
262 for (i
= 0; i
< NOPTS
; i
++)
263 out1fmt("%s %co %s%s",
264 i
% 6 == 0 ? "set" : "",
265 optlist
[i
].val
? '-' : '+',
267 i
% 6 == 5 || i
== NOPTS
- 1 ? "\n" : "");
270 for (i
= 0; i
< NOPTS
; i
++)
271 if (equal(name
, optlist
[i
].name
)) {
272 setoption(optlist
[i
].letter
, val
);
275 error("Illegal option -o %s", name
);
281 setoption(int flag
, int val
)
285 if (flag
== 'p' && !val
&& privileged
) {
286 if (setgid(getgid()) == -1)
288 if (setuid(getuid()) == -1)
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
);
308 * Set the shell parameters.
312 setparam(char **argv
)
318 for (nparam
= 0 ; argv
[nparam
] ; nparam
++);
319 ap
= newparam
= ckmalloc((nparam
+ 1) * sizeof *ap
);
321 *ap
++ = savestr(*argv
++);
324 freeparam(&shellparam
);
325 shellparam
.malloc
= 1;
326 shellparam
.nparam
= nparam
;
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
)
370 if (n
> shellparam
.nparam
)
373 shellparam
.nparam
-= n
;
374 for (ap1
= shellparam
.p
; --n
>= 0 ; ap1
++) {
375 if (shellparam
.malloc
)
379 while ((*ap2
++ = *ap1
++) != NULL
);
380 shellparam
.reset
= 1;
388 * The set command builtin.
392 setcmd(int argc
, char **argv
)
395 return showvarscmd(argc
, argv
);
399 if (*argptr
!= NULL
) {
408 getoptsreset(const char *value
)
410 while (*value
== '0')
412 if (strcmp(value
, "1") == 0)
413 shellparam
.reset
= 1;
417 * The getopts builtin. Shellparam.optnext points to the next argument
418 * to be processed. Shellparam.optptr points to the next character to
419 * be processed in the current argument. If shellparam.optnext is NULL,
420 * then it's the first time getopts has been called.
424 getoptscmd(int argc
, char **argv
)
426 char **optbase
= NULL
, **ap
;
430 error("usage: getopts optstring var [arg]");
432 if (shellparam
.reset
== 1) {
434 if (shellparam
.optp
) {
435 for (ap
= shellparam
.optp
; *ap
; ap
++)
437 ckfree(shellparam
.optp
);
438 shellparam
.optp
= NULL
;
441 shellparam
.optp
= ckmalloc((argc
- 2) * sizeof *ap
);
442 memset(shellparam
.optp
, '\0', (argc
- 2) * sizeof *ap
);
443 for (i
= 0; i
< argc
- 3; i
++)
444 shellparam
.optp
[i
] = savestr(argv
[i
+ 3]);
447 optbase
= argc
== 3 ? shellparam
.p
: shellparam
.optp
;
448 shellparam
.optnext
= optbase
;
449 shellparam
.optptr
= NULL
;
450 shellparam
.reset
= 0;
452 optbase
= shellparam
.optp
? shellparam
.optp
: shellparam
.p
;
454 return getopts(argv
[1], argv
[2], optbase
, &shellparam
.optnext
,
459 getopts(char *optstr
, char *optvar
, char **optfirst
, char ***optnext
,
468 const char *newoptarg
= NULL
;
470 if ((p
= *optptr
) == NULL
|| *p
== '\0') {
471 /* Current word is done, advance */
472 if (*optnext
== NULL
)
475 if (p
== NULL
|| *p
!= '-' || *++p
== '\0') {
477 ind
= *optnext
- optfirst
+ 1;
484 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
489 for (q
= optstr
; *q
!= c
; ) {
491 if (optstr
[0] == ':') {
497 out2fmt_flush("Illegal option -%c\n", c
);
506 if (*p
== '\0' && (p
= **optnext
) == NULL
) {
507 if (optstr
[0] == ':') {
514 out2fmt_flush("No arg for -%c option\n", c
);
527 if (*optnext
!= NULL
)
528 ind
= *optnext
- optfirst
+ 1;
530 if (newoptarg
!= NULL
)
531 err
|= setvarsafe("OPTARG", newoptarg
, 0);
534 err
|= unsetvar("OPTARG");
537 fmtstr(s
, sizeof(s
), "%d", ind
);
538 err
|= setvarsafe("OPTIND", s
, VNOFUNC
);
541 err
|= setvarsafe(optvar
, s
, 0);
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
)
565 if ((p
= nextopt_optptr
) == NULL
|| *p
== '\0') {
567 if (p
== NULL
|| *p
!= '-' || *++p
== '\0')
570 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
574 for (q
= optstring
; *q
!= c
; ) {
576 error("Illegal option -%c", c
);
581 if (*p
== '\0' && (p
= *argptr
++) == NULL
)
582 error("No arg for -%c option", c
);