No empty .Rs/.Re
[netbsd-mini2440.git] / bin / sh / options.c
blob10abf801421d9e5e302764874706c59fdda7d350
1 /* $NetBSD: options.c,v 1.40 2005/12/13 17:44:18 dsl Exp $ */
3 /*-
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
8 * Kenneth Almquist.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
32 * SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
39 #else
40 __RCSID("$NetBSD: options.c,v 1.40 2005/12/13 17:44:18 dsl Exp $");
41 #endif
42 #endif /* not lint */
44 #include <signal.h>
45 #include <unistd.h>
46 #include <stdlib.h>
48 #include "shell.h"
49 #define DEFINE_OPTIONS
50 #include "options.h"
51 #undef DEFINE_OPTIONS
52 #include "nodes.h" /* for other header files */
53 #include "eval.h"
54 #include "jobs.h"
55 #include "input.h"
56 #include "output.h"
57 #include "trap.h"
58 #include "var.h"
59 #include "memalloc.h"
60 #include "error.h"
61 #include "mystring.h"
62 #ifndef SMALL
63 #include "myhistedit.h"
64 #endif
65 #include "show.h"
67 char *arg0; /* value of $0 */
68 struct shparam shellparam; /* current positional parameters */
69 char **argptr; /* argument list for builtin commands */
70 char *optionarg; /* set by nextopt (like getopt) */
71 char *optptr; /* used by nextopt */
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.
86 void
87 procargs(int argc, char **argv)
89 size_t i;
91 argptr = argv;
92 if (argc > 0)
93 argptr++;
94 for (i = 0; i < NOPTS; i++)
95 optlist[i].val = 2;
96 options(1);
97 if (*argptr == NULL && minusc == NULL)
98 sflag = 1;
99 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
100 iflag = 1;
101 if (iflag == 1 && sflag == 2)
102 iflag = 2;
103 if (mflag == 2)
104 mflag = iflag;
105 for (i = 0; i < NOPTS; i++)
106 if (optlist[i].val == 2)
107 optlist[i].val = 0;
108 #if DEBUG == 2
109 debug = 1;
110 #endif
111 arg0 = argv[0];
112 if (sflag == 0 && minusc == NULL) {
113 commandname = argv[0];
114 arg0 = *argptr++;
115 setinputfile(arg0, 0);
116 commandname = arg0;
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");
122 minusc = *argptr++;
123 if (*argptr != 0)
124 arg0 = *argptr++;
127 shellparam.p = argptr;
128 shellparam.reset = 1;
129 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
130 while (*argptr) {
131 shellparam.nparam++;
132 argptr++;
134 optschanged();
138 void
139 optschanged(void)
141 setinteractive(iflag);
142 #ifndef SMALL
143 histedit();
144 #endif
145 setjobctl(mflag);
149 * Process shell options. The global variable argptr contains a pointer
150 * to the argument list; we advance it past the options.
153 STATIC void
154 options(int cmdline)
156 static char empty[] = "";
157 char *p;
158 int val;
159 int c;
161 if (cmdline)
162 minusc = NULL;
163 while ((p = *argptr) != NULL) {
164 argptr++;
165 if ((c = *p++) == '-') {
166 val = 1;
167 if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
168 if (!cmdline) {
169 /* "-" means turn off -x and -v */
170 if (p[0] == '\0')
171 xflag = vflag = 0;
172 /* "--" means reset params */
173 else if (*argptr == NULL)
174 setparam(argptr);
176 break; /* "-" or "--" terminates options */
178 } else if (c == '+') {
179 val = 0;
180 } else {
181 argptr--;
182 break;
184 while ((c = *p++) != '\0') {
185 if (c == 'c' && cmdline) {
186 /* command is after shell args*/
187 minusc = empty;
188 } else if (c == 'o') {
189 minus_o(*argptr, val);
190 if (*argptr)
191 argptr++;
192 } else {
193 setoption(c, val);
199 static void
200 set_opt_val(size_t i, int val)
202 size_t j;
203 int flag;
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)
209 optlist[j].val = 0;
211 optlist[i].val = val;
212 #ifdef DEBUG
213 if (&optlist[i].val == &debug)
214 opentrace();
215 #endif
218 STATIC void
219 minus_o(char *name, int val)
221 size_t i;
223 if (name == NULL) {
224 if (val) {
225 out1str("Current option settings\n");
226 for (i = 0; i < NOPTS; i++) {
227 out1fmt("%-16s%s\n", optlist[i].name,
228 optlist[i].val ? "on" : "off");
230 } else {
231 out1str("set");
232 for (i = 0; i < NOPTS; i++) {
233 out1fmt(" %co %s",
234 "+-"[optlist[i].val], optlist[i].name);
236 out1str("\n");
238 } else {
239 for (i = 0; i < NOPTS; i++)
240 if (equal(name, optlist[i].name)) {
241 set_opt_val(i, val);
242 return;
244 error("Illegal option -o %s", name);
249 STATIC void
250 setoption(int flag, int val)
252 size_t i;
254 for (i = 0; i < NOPTS; i++)
255 if (optlist[i].letter == flag) {
256 set_opt_val( i, val );
257 return;
259 error("Illegal option -%c", flag);
260 /* NOTREACHED */
265 #ifdef mkinit
266 INCLUDE "options.h"
268 SHELLPROC {
269 int i;
271 for (i = 0; optlist[i].name; i++)
272 optlist[i].val = 0;
273 optschanged();
276 #endif
280 * Set the shell parameters.
283 void
284 setparam(char **argv)
286 char **newparam;
287 char **ap;
288 int nparam;
290 for (nparam = 0 ; argv[nparam] ; nparam++)
291 continue;
292 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
293 while (*argv) {
294 *ap++ = savestr(*argv++);
296 *ap = NULL;
297 freeparam(&shellparam);
298 shellparam.malloc = 1;
299 shellparam.nparam = nparam;
300 shellparam.p = newparam;
301 shellparam.optnext = NULL;
306 * Free the list of positional parameters.
309 void
310 freeparam(volatile struct shparam *param)
312 char **ap;
314 if (param->malloc) {
315 for (ap = param->p ; *ap ; ap++)
316 ckfree(*ap);
317 ckfree(param->p);
324 * The shift builtin command.
328 shiftcmd(int argc, char **argv)
330 int n;
331 char **ap1, **ap2;
333 n = 1;
334 if (argc > 1)
335 n = number(argv[1]);
336 if (n > shellparam.nparam)
337 error("can't shift that many");
338 INTOFF;
339 shellparam.nparam -= n;
340 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
341 if (shellparam.malloc)
342 ckfree(*ap1);
344 ap2 = shellparam.p;
345 while ((*ap2++ = *ap1++) != NULL);
346 shellparam.optnext = NULL;
347 INTON;
348 return 0;
354 * The set command builtin.
358 setcmd(int argc, char **argv)
360 if (argc == 1)
361 return showvars(0, 0, 1);
362 INTOFF;
363 options(0);
364 optschanged();
365 if (*argptr != NULL) {
366 setparam(argptr);
368 INTON;
369 return 0;
373 void
374 getoptsreset(value)
375 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)
393 char **optbase;
395 if (argc < 3)
396 error("usage: getopts optstring var [arg]");
397 else if (argc == 3)
398 optbase = shellparam.p;
399 else
400 optbase = &argv[3];
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,
409 &shellparam.optptr);
412 STATIC int
413 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, char **optpptr)
415 char *p, *q;
416 char c = '?';
417 int done = 0;
418 int ind = 0;
419 int err = 0;
420 char s[12];
422 if ((p = *optpptr) == NULL || *p == '\0') {
423 /* Current word is done, advance */
424 if (*optnext == NULL)
425 return 1;
426 p = **optnext;
427 if (p == NULL || *p != '-' || *++p == '\0') {
428 atend:
429 ind = *optnext - optfirst + 1;
430 *optnext = NULL;
431 p = NULL;
432 done = 1;
433 goto out;
435 (*optnext)++;
436 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
437 goto atend;
440 c = *p++;
441 for (q = optstr; *q != c; ) {
442 if (*q == '\0') {
443 if (optstr[0] == ':') {
444 s[0] = c;
445 s[1] = '\0';
446 err |= setvarsafe("OPTARG", s, 0);
447 } else {
448 outfmt(&errout, "Illegal option -%c\n", c);
449 (void) unsetvar("OPTARG", 0);
451 c = '?';
452 goto bad;
454 if (*++q == ':')
455 q++;
458 if (*++q == ':') {
459 if (*p == '\0' && (p = **optnext) == NULL) {
460 if (optstr[0] == ':') {
461 s[0] = c;
462 s[1] = '\0';
463 err |= setvarsafe("OPTARG", s, 0);
464 c = ':';
465 } else {
466 outfmt(&errout, "No arg for -%c option\n", c);
467 (void) unsetvar("OPTARG", 0);
468 c = '?';
470 goto bad;
473 if (p == **optnext)
474 (*optnext)++;
475 err |= setvarsafe("OPTARG", p, 0);
476 p = NULL;
477 } else
478 err |= setvarsafe("OPTARG", "", 0);
479 ind = *optnext - optfirst + 1;
480 goto out;
482 bad:
483 ind = 1;
484 *optnext = NULL;
485 p = NULL;
486 out:
487 *optpptr = p;
488 fmtstr(s, sizeof(s), "%d", ind);
489 err |= setvarsafe("OPTIND", s, VNOFUNC);
490 s[0] = c;
491 s[1] = '\0';
492 err |= setvarsafe(optvar, s, 0);
493 if (err) {
494 *optnext = NULL;
495 *optpptr = NULL;
496 flushall();
497 exraise(EXERROR);
499 return done;
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
510 * end of input.
514 nextopt(const char *optstring)
516 char *p;
517 const char *q;
518 char c;
520 if ((p = optptr) == NULL || *p == '\0') {
521 p = *argptr;
522 if (p == NULL || *p != '-' || *++p == '\0')
523 return '\0';
524 argptr++;
525 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
526 return '\0';
528 c = *p++;
529 for (q = optstring ; *q != c ; ) {
530 if (*q == '\0')
531 error("Illegal option -%c", c);
532 if (*++q == ':')
533 q++;
535 if (*++q == ':') {
536 if (*p == '\0' && (p = *argptr++) == NULL)
537 error("No arg for -%c option", c);
538 optionarg = p;
539 p = NULL;
541 optptr = p;
542 return c;