1 /* $NetBSD: options.c,v 1.38 2005/03/20 21:38:17 dsl 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 #ifdef HAVE_SYS_CDEFS_H
36 #include <sys/cdefs.h>
40 static char sccsid
[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
42 __RCSID("$NetBSD: options.c,v 1.38 2005/03/20 21:38:17 dsl Exp $");
51 #define DEFINE_OPTIONS
54 #include "nodes.h" /* for other header files */
65 #include "myhistedit.h"
69 char *arg0
; /* value of $0 */
70 struct shparam shellparam
; /* current positional parameters */
71 char **argptr
; /* argument list for builtin commands */
72 char *optionarg
; /* set by nextopt (like getopt) */
73 char *optptr
; /* used by nextopt */
75 char *minusc
; /* argument to -c option */
78 STATIC
void options(int);
79 STATIC
void minus_o(char *, int);
80 STATIC
void setoption(int, int);
81 STATIC
int getopts(char *, char *, char **, char ***, char **);
85 * Process the shell command line arguments.
89 procargs(int argc
, char **argv
)
96 for (i
= 0; i
< NOPTS
; i
++)
99 if (*argptr
== NULL
&& minusc
== NULL
)
101 if (iflag
== 2 && sflag
== 1 && isatty(0) && isatty(1))
105 for (i
= 0; i
< NOPTS
; i
++)
106 if (optlist
[i
].val
== 2)
112 if (sflag
== 0 && minusc
== NULL
) {
113 commandname
= argv
[0];
115 setinputfile(arg0
, 0);
118 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
119 if (minusc
!= NULL
) {
120 if (argptr
== NULL
|| *argptr
== NULL
)
121 error("Bad -c option");
127 shellparam
.p
= argptr
;
128 shellparam
.reset
= 1;
129 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
141 setinteractive(iflag
);
149 * Process shell options. The global variable argptr contains a pointer
150 * to the argument list; we advance it past the options.
156 static char empty
[] = "";
163 while ((p
= *argptr
) != NULL
) {
165 if ((c
= *p
++) == '-') {
167 if (p
[0] == '\0' || (p
[0] == '-' && p
[1] == '\0')) {
169 /* "-" means turn off -x and -v */
172 /* "--" means reset params */
173 else if (*argptr
== NULL
)
176 break; /* "-" or "--" terminates options */
178 } else if (c
== '+') {
184 while ((c
= *p
++) != '\0') {
185 if (c
== 'c' && cmdline
) {
186 /* command is after shell args*/
188 } else if (c
== 'o') {
189 minus_o(*argptr
, val
);
200 set_opt_val(int i
, int val
)
205 if (val
&& (flag
= optlist
[i
].opt_set
)) {
206 /* some options (eg vi/emacs) are mutually exclusive */
207 for (j
= 0; j
< NOPTS
; j
++)
208 if (optlist
[j
].opt_set
== flag
)
211 optlist
[i
].val
= val
;
213 if (&optlist
[i
].val
== &debug
)
219 minus_o(char *name
, int val
)
224 out1str("Current option settings\n");
225 for (i
= 0; i
< NOPTS
; i
++)
226 out1fmt("%-16s%s\n", optlist
[i
].name
,
227 optlist
[i
].val
? "on" : "off");
229 for (i
= 0; i
< NOPTS
; i
++)
230 if (equal(name
, optlist
[i
].name
)) {
234 error("Illegal option -o %s", name
);
240 setoption(int flag
, int val
)
244 for (i
= 0; i
< NOPTS
; i
++)
245 if (optlist
[i
].letter
== flag
) {
246 set_opt_val( i
, val
);
249 error("Illegal option -%c", flag
);
261 for (i
= 0; optlist
[i
].name
; i
++)
270 * Set the shell parameters.
274 setparam(char **argv
)
280 for (nparam
= 0 ; argv
[nparam
] ; nparam
++)
282 ap
= newparam
= ckmalloc((nparam
+ 1) * sizeof *ap
);
284 *ap
++ = savestr(*argv
++);
287 freeparam(&shellparam
);
288 shellparam
.malloc
= 1;
289 shellparam
.nparam
= nparam
;
290 shellparam
.p
= newparam
;
291 shellparam
.optnext
= NULL
;
296 * Free the list of positional parameters.
300 freeparam(volatile struct shparam
*param
)
305 for (ap
= param
->p
; *ap
; ap
++)
314 * The shift builtin command.
318 shiftcmd(int argc
, char **argv
)
326 if (n
> shellparam
.nparam
)
327 error("can't shift that many");
329 shellparam
.nparam
-= n
;
330 for (ap1
= shellparam
.p
; --n
>= 0 ; ap1
++) {
331 if (shellparam
.malloc
)
335 while ((*ap2
++ = *ap1
++) != NULL
);
336 shellparam
.optnext
= NULL
;
344 * The set command builtin.
348 setcmd(int argc
, char **argv
)
351 return showvars(0, 0, 1);
355 if (*argptr
!= NULL
) {
367 if (number(value
) == 1) {
368 shellparam
.optnext
= NULL
;
369 shellparam
.reset
= 1;
374 * The getopts builtin. Shellparam.optnext points to the next argument
375 * to be processed. Shellparam.optptr points to the next character to
376 * be processed in the current argument. If shellparam.optnext is NULL,
377 * then it's the first time getopts has been called.
381 getoptscmd(int argc
, char **argv
)
386 error("usage: getopts optstring var [arg]");
388 optbase
= shellparam
.p
;
392 if (shellparam
.reset
== 1) {
393 shellparam
.optnext
= optbase
;
394 shellparam
.optptr
= NULL
;
395 shellparam
.reset
= 0;
398 return getopts(argv
[1], argv
[2], optbase
, &shellparam
.optnext
,
403 getopts(char *optstr
, char *optvar
, char **optfirst
, char ***optnext
, char **optpptr
)
412 if ((p
= *optpptr
) == NULL
|| *p
== '\0') {
413 /* Current word is done, advance */
414 if (*optnext
== NULL
)
417 if (p
== NULL
|| *p
!= '-' || *++p
== '\0') {
419 ind
= *optnext
- optfirst
+ 1;
426 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
431 for (q
= optstr
; *q
!= c
; ) {
433 if (optstr
[0] == ':') {
436 err
|= setvarsafe("OPTARG", s
, 0);
438 outfmt(&errout
, "Illegal option -%c\n", c
);
439 (void) unsetvar("OPTARG", 0);
449 if (*p
== '\0' && (p
= **optnext
) == NULL
) {
450 if (optstr
[0] == ':') {
453 err
|= setvarsafe("OPTARG", s
, 0);
456 outfmt(&errout
, "No arg for -%c option\n", c
);
457 (void) unsetvar("OPTARG", 0);
465 err
|= setvarsafe("OPTARG", p
, 0);
468 err
|= setvarsafe("OPTARG", "", 0);
469 ind
= *optnext
- optfirst
+ 1;
478 fmtstr(s
, sizeof(s
), "%d", ind
);
479 err
|= setvarsafe("OPTIND", s
, VNOFUNC
);
482 err
|= setvarsafe(optvar
, s
, 0);
493 * XXX - should get rid of. have all builtins use getopt(3). the
494 * library getopt must have the BSD extension static variable "optreset"
495 * otherwise it can't be used within the shell safely.
497 * Standard option processing (a la getopt) for builtin routines. The
498 * only argument that is passed to nextopt is the option string; the
499 * other arguments are unnecessary. It return the character, or '\0' on
504 nextopt(const char *optstring
)
510 if ((p
= optptr
) == NULL
|| *p
== '\0') {
512 if (p
== NULL
|| *p
!= '-' || *++p
== '\0')
515 if (p
[0] == '-' && p
[1] == '\0') /* check for "--" */
519 for (q
= optstring
; *q
!= c
; ) {
521 error("Illegal option -%c", c
);
526 if (*p
== '\0' && (p
= *argptr
++) == NULL
)
527 error("No arg for -%c option", c
);