1 /* $NetBSD: options.c,v 1.43 2012/03/20 18:42:29 matt Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
40 __RCSID("$NetBSD: options.c,v 1.43 2012/03/20 18:42:29 matt Exp $");
49 #define DEFINE_OPTIONS
53 #include "nodes.h" /* for other header files */
64 #include "myhistedit.h"
68 char *arg0
; /* value of $0 */
69 struct shparam shellparam
; /* current positional parameters */
70 char **argptr
; /* argument list for builtin commands */
71 char *optionarg
; /* set by nextopt (like getopt) */
72 char *optptr
; /* used by nextopt */
74 char *minusc
; /* argument to -c option */
77 STATIC
void options(int);
78 STATIC
void minus_o(char *, int);
79 STATIC
void setoption(int, int);
80 STATIC
int getopts(char *, char *, char **, char ***, char **);
84 * Process the shell command line arguments.
88 procargs(int argc
, char **argv
)
95 for (i
= 0; i
< NOPTS
; i
++)
98 if (*argptr
== NULL
&& minusc
== NULL
)
100 if (iflag
== 2 && sflag
== 1 && isatty(0) && isatty(1))
102 if (iflag
== 1 && sflag
== 2)
106 for (i
= 0; i
< NOPTS
; i
++)
107 if (optlist
[i
].val
== 2)
113 if (sflag
== 0 && minusc
== NULL
) {
114 commandname
= argv
[0];
116 setinputfile(arg0
, 0);
119 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
120 if (minusc
!= NULL
) {
121 if (argptr
== NULL
|| *argptr
== NULL
)
122 error("Bad -c option");
128 shellparam
.p
= argptr
;
129 shellparam
.reset
= 1;
130 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
142 setinteractive(iflag
);
150 * Process shell options. The global variable argptr contains a pointer
151 * to the argument list; we advance it past the options.
157 static char empty
[] = "";
164 while ((p
= *argptr
) != NULL
) {
166 if ((c
= *p
++) == '-') {
168 if (p
[0] == '\0' || (p
[0] == '-' && p
[1] == '\0')) {
170 /* "-" means turn off -x and -v */
173 /* "--" means reset params */
174 else if (*argptr
== NULL
)
177 break; /* "-" or "--" terminates options */
179 } else if (c
== '+') {
185 while ((c
= *p
++) != '\0') {
186 if (c
== 'c' && cmdline
) {
187 /* command is after shell args*/
189 } else if (c
== 'o') {
190 minus_o(*argptr
, val
);
201 set_opt_val(size_t i
, int val
)
206 if (val
&& (flag
= optlist
[i
].opt_set
)) {
207 /* some options (eg vi/emacs) are mutually exclusive */
208 for (j
= 0; j
< NOPTS
; j
++)
209 if (optlist
[j
].opt_set
== flag
)
212 optlist
[i
].val
= val
;
214 if (&optlist
[i
].val
== &debug
)
220 minus_o(char *name
, int val
)
226 out1str("Current option settings\n");
227 for (i
= 0; i
< NOPTS
; i
++) {
228 out1fmt("%-16s%s\n", optlist
[i
].name
,
229 optlist
[i
].val
? "on" : "off");
233 for (i
= 0; i
< NOPTS
; i
++) {
235 "+-"[optlist
[i
].val
], optlist
[i
].name
);
240 for (i
= 0; i
< NOPTS
; i
++)
241 if (equal(name
, optlist
[i
].name
)) {
245 error("Illegal option -o %s", name
);
251 setoption(int flag
, int val
)
255 for (i
= 0; i
< NOPTS
; i
++)
256 if (optlist
[i
].letter
== flag
) {
257 set_opt_val( i
, val
);
260 error("Illegal option -%c", flag
);
272 for (i
= 0; optlist
[i
].name
; i
++)
281 * Set the shell parameters.
285 setparam(char **argv
)
291 for (nparam
= 0 ; argv
[nparam
] ; nparam
++)
293 ap
= newparam
= ckmalloc((nparam
+ 1) * sizeof *ap
);
295 *ap
++ = savestr(*argv
++);
298 freeparam(&shellparam
);
299 shellparam
.malloc
= 1;
300 shellparam
.nparam
= nparam
;
301 shellparam
.p
= newparam
;
302 shellparam
.optnext
= NULL
;
307 * Free the list of positional parameters.
311 freeparam(volatile struct shparam
*param
)
316 for (ap
= param
->p
; *ap
; ap
++)
325 * The shift builtin command.
329 shiftcmd(int argc
, char **argv
)
337 if (n
> shellparam
.nparam
)
338 error("can't shift that many");
340 shellparam
.nparam
-= n
;
341 for (ap1
= shellparam
.p
; --n
>= 0 ; ap1
++) {
342 if (shellparam
.malloc
)
346 while ((*ap2
++ = *ap1
++) != NULL
);
347 shellparam
.optnext
= NULL
;
355 * The set command builtin.
359 setcmd(int argc
, char **argv
)
362 return showvars(0, 0, 1);
366 if (*argptr
!= NULL
) {
375 getoptsreset(const char *value
)
377 if (number(value
) == 1) {
378 shellparam
.optnext
= NULL
;
379 shellparam
.reset
= 1;
384 * The getopts builtin. Shellparam.optnext points to the next argument
385 * to be processed. Shellparam.optptr points to the next character to
386 * be processed in the current argument. If shellparam.optnext is NULL,
387 * then it's the first time getopts has been called.
391 getoptscmd(int argc
, char **argv
)
396 error("usage: getopts optstring var [arg]");
398 optbase
= shellparam
.p
;
402 if (shellparam
.reset
== 1) {
403 shellparam
.optnext
= optbase
;
404 shellparam
.optptr
= NULL
;
405 shellparam
.reset
= 0;
408 return getopts(argv
[1], argv
[2], optbase
, &shellparam
.optnext
,
413 getopts(char *optstr
, char *optvar
, char **optfirst
, char ***optnext
, char **optpptr
)
422 if ((p
= *optpptr
) == NULL
|| *p
== '\0') {
423 /* Current word is done, advance */
424 if (*optnext
== NULL
)
427 if (p
== NULL
|| *p
!= '-' || *++p
== '\0') {
429 ind
= *optnext
- optfirst
+ 1;
436 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
441 for (q
= optstr
; *q
!= c
; ) {
443 if (optstr
[0] == ':') {
446 err
|= setvarsafe("OPTARG", s
, 0);
448 outfmt(&errout
, "Illegal option -%c\n", c
);
449 (void) unsetvar("OPTARG", 0);
459 if (*p
== '\0' && (p
= **optnext
) == NULL
) {
460 if (optstr
[0] == ':') {
463 err
|= setvarsafe("OPTARG", s
, 0);
466 outfmt(&errout
, "No arg for -%c option\n", c
);
467 (void) unsetvar("OPTARG", 0);
475 err
|= setvarsafe("OPTARG", p
, 0);
478 err
|= 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(const char *optstring
)
520 if ((p
= optptr
) == NULL
|| *p
== '\0') {
522 if (p
== NULL
|| *p
!= '-' || *++p
== '\0')
525 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
529 for (q
= optstring
; *q
!= c
; ) {
531 error("Illegal option -%c", c
);
536 if (*p
== '\0' && (p
= *argptr
++) == NULL
)
537 error("No arg for -%c option", c
);