* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* %sccs.include.redist.c%
static char sccsid
[] = "@(#)options.c 8.2 (Berkeley) %G%";
#include "nodes.h" /* for other header files */
char *arg0
; /* value of $0 */
struct shparam shellparam
; /* current positional parameters */
char **argptr
; /* argument list for builtin commands */
char *optarg
; /* set by nextopt (like getopt) */
char *optptr
; /* used by nextopt */
char *minusc
; /* argument to -c option */
STATIC
void options
__P((int));
STATIC
void minus_o
__P((char *, int));
STATIC
void setoption
__P((int, int));
* Process the shell command line arguments.
for (i
= 0; i
< NOPTS
; i
++)
if (*argptr
== NULL
&& minusc
== NULL
)
if (iflag
== 2 && sflag
== 1 && isatty(0) && isatty(1))
for (i
= 0; i
< NOPTS
; i
++)
if (sflag
== 0 && minusc
== NULL
) {
commandname
= arg0
= *argptr
++;
setinputfile(commandname
, 0);
/* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
* Process shell options. The global variable argptr contains a pointer
* to the argument list; we advance it past the options.
while ((p
= *argptr
) != NULL
) {
if (p
[0] == '\0' || (p
[0] == '-' && p
[1] == '\0')) {
/* "-" means turn off -x and -v */
/* "--" means reset params */
else if (*argptr
== NULL
)
break; /* "-" or "--" terminates options */
while ((c
= *p
++) != '\0') {
if (c
== 'c' && cmdline
) {
#ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
if (q
== NULL
|| minusc
!= NULL
)
out1str("Current option settings\n");
for (i
= 0; i
< NOPTS
; i
++)
out1fmt("%-16s%s\n", optlist
[i
].name
,
optlist
[i
].val
? "on" : "off");
for (i
= 0; i
< NOPTS
; i
++)
if (equal(name
, optlist
[i
].name
)) {
setoption(optlist
[i
].letter
, val
);
error("Illegal option -o %s", name
);
for (i
= 0; i
< NOPTS
; i
++)
if (optlist
[i
].letter
== flag
) {
/* #%$ hack for ksh semantics */
error("Illegal option -%c", flag
);
for (i
= 0; i
< NOPTS
; i
++)
* Set the shell parameters.
for (nparam
= 0 ; argv
[nparam
] ; nparam
++);
ap
= newparam
= ckmalloc((nparam
+ 1) * sizeof *ap
);
*ap
++ = savestr(*argv
++);
shellparam
.nparam
= nparam
;
shellparam
.optnext
= NULL
;
* Free the list of positional parameters.
for (ap
= param
->p
; *ap
; ap
++)
* The shift builtin command.
if (n
> shellparam
.nparam
)
error("can't shift that many");
for (ap1
= shellparam
.p
; --n
>= 0 ; ap1
++) {
while ((*ap2
++ = *ap1
++) != NULL
);
shellparam
.optnext
= NULL
;
* The set command builtin.
return showvarscmd(argc
, argv
);
* The getopts builtin. Shellparam.optnext points to the next argument
* to be processed. Shellparam.optptr points to the next character to
* be processed in the current argument. If shellparam.optnext is NULL,
* then it's the first time getopts has been called.
error("Usage: getopts optstring var");
if (shellparam
.optnext
== NULL
) {
shellparam
.optnext
= shellparam
.p
;
shellparam
.optptr
= NULL
;
if ((p
= shellparam
.optptr
) == NULL
|| *p
== '\0') {
if (p
== NULL
|| *p
!= '-' || *++p
== '\0') {
fmtstr(s
, 10, "%d", shellparam
.optnext
- shellparam
.p
+ 1);
shellparam
.optnext
= NULL
;
if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
for (q
= argv
[1] ; *q
!= c
; ) {
out1fmt("Illegal option -%c\n", c
);
if (*p
== '\0' && (p
= *shellparam
.optnext
) == NULL
) {
out1fmt("No arg for -%c option\n", c
);
* XXX - should get rid of. have all builtins use getopt(3). the
* library getopt must have the BSD extension static variable "optreset"
* otherwise it can't be used within the shell safely.
* Standard option processing (a la getopt) for builtin routines. The
* only argument that is passed to nextopt is the option string; the
* other arguments are unnecessary. It return the character, or '\0' on
if ((p
= optptr
) == NULL
|| *p
== '\0') {
if (p
== NULL
|| *p
!= '-' || *++p
== '\0')
if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
for (q
= optstring
; *q
!= c
; ) {
error("Illegal option -%c", c
);
if (*p
== '\0' && (p
= *argptr
++) == NULL
)
error("No arg for -%c option", c
);