add opendir alias
[minix.git] / commands / ash / options.c
blobc93ca2be15bb30bb03f491162c04c1a761c6c68d
1 /*-
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
6 * Kenneth Almquist.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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 * 4. 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
30 * SUCH DAMAGE.
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
36 #endif
37 #endif /* not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/bin/sh/options.c,v 1.21 2004/04/06 20:06:51 markm Exp $");
43 #include <signal.h>
44 #include <unistd.h>
45 #include <stdlib.h>
47 #include "shell.h"
48 #define DEFINE_OPTIONS
49 #include "options.h"
50 #undef DEFINE_OPTIONS
51 #include "nodes.h" /* for other header files */
52 #include "eval.h"
53 #include "jobs.h"
54 #include "input.h"
55 #include "output.h"
56 #include "trap.h"
57 #include "var.h"
58 #include "memalloc.h"
59 #include "error.h"
60 #include "mystring.h"
61 #include "builtins.h"
62 #if !defined(NO_HISTORY)
63 #include "myhistedit.h"
64 #endif
66 char *arg0; /* value of $0 */
67 struct shparam shellparam; /* current positional parameters */
68 char **argptr; /* argument list for builtin commands */
69 char *shoptarg; /* set by nextopt (like getopt) */
70 char *optptr; /* used by nextopt */
71 int editable; /* isatty(0) && isatty(1) */
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 int i;
91 argptr = argv;
92 if (argc > 0)
93 argptr++;
94 for (i = 0; i < NOPTS; i++)
95 optlist[i].val = 2;
96 privileged = (getuid() != geteuid() || getgid() != getegid());
97 options(1);
98 if (*argptr == NULL && minusc == NULL)
99 sflag = 1;
100 editable = (isatty(0) && isatty(1));
101 if (iflag == 2 && sflag == 1 && editable)
102 iflag = 1;
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 arg0 = argv[0];
109 if (sflag == 0 && minusc == NULL) {
110 commandname = arg0 = *argptr++;
111 setinputfile(commandname, 0);
113 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
114 if (argptr && minusc && *argptr)
115 arg0 = *argptr++;
117 shellparam.p = argptr;
118 shellparam.reset = 1;
119 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
120 while (*argptr) {
121 shellparam.nparam++;
122 argptr++;
124 optschanged();
128 void
129 optschanged(void)
131 setinteractive(iflag);
132 #if !defined(NO_HISTORY)
133 histedit();
134 #endif
135 setjobctl(mflag);
139 * Process shell options. The global variable argptr contains a pointer
140 * to the argument list; we advance it past the options.
143 STATIC void
144 options(int cmdline)
146 char *p;
147 int val;
148 int c;
150 if (cmdline)
151 minusc = NULL;
152 while ((p = *argptr) != NULL) {
153 argptr++;
154 if ((c = *p++) == '-') {
155 val = 1;
156 if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
157 if (!cmdline) {
158 /* "-" means turn off -x and -v */
159 if (p[0] == '\0')
160 xflag = vflag = 0;
161 /* "--" means reset params */
162 else if (*argptr == NULL)
163 setparam(argptr);
165 break; /* "-" or "--" terminates options */
167 } else if (c == '+') {
168 val = 0;
169 } else {
170 argptr--;
171 break;
173 while ((c = *p++) != '\0') {
174 if (c == 'c' && cmdline) {
175 char *q;
176 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
177 if (*p == '\0')
178 #endif
179 q = *argptr++;
180 if (q == NULL || minusc != NULL)
181 error("Bad -c option");
182 minusc = q;
183 #ifdef NOHACK
184 break;
185 #endif
186 } else if (c == 'o') {
187 minus_o(*argptr, val);
188 if (*argptr)
189 argptr++;
190 } else {
191 if (c == 'p' && !val && privileged) {
192 (void) setuid(getuid());
193 (void) setgid(getgid());
195 setoption(c, val);
201 STATIC void
202 minus_o(char *name, int val)
204 int doneset, i;
206 if (name == NULL) {
207 if (val) {
208 /* "Pretty" output. */
209 out1str("Current option settings\n");
210 for (i = 0; i < NOPTS; i++)
211 out1fmt("%-16s%s\n", optlist[i].name,
212 optlist[i].val ? "on" : "off");
213 } else {
214 /* Output suitable for re-input to shell. */
215 for (doneset = i = 0; i < NOPTS; i++)
216 if (optlist[i].val) {
217 if (!doneset) {
218 out1str("set");
219 doneset = 1;
221 out1fmt(" -o %s", optlist[i].name);
223 if (doneset)
224 out1c('\n');
226 } else {
227 for (i = 0; i < NOPTS; i++)
228 if (equal(name, optlist[i].name)) {
229 if (!val && privileged && equal(name, "privileged")) {
230 (void) setuid(getuid());
231 (void) setgid(getgid());
233 setoption(optlist[i].letter, val);
234 return;
236 error("Illegal option -o %s", name);
241 STATIC void
242 setoption(int flag, int val)
244 int i;
246 for (i = 0; i < NOPTS; i++)
247 if (optlist[i].letter == flag) {
248 optlist[i].val = val;
249 if (val) {
250 /* #%$ hack for ksh semantics */
251 if (flag == 'V')
252 Eflag = 0;
253 else if (flag == 'E')
254 Vflag = 0;
256 return;
258 error("Illegal option -%c", flag);
263 #ifdef mkinit
264 INCLUDE "options.h"
266 SHELLPROC {
267 int i;
269 for (i = 0; i < NOPTS; i++)
270 optlist[i].val = 0;
271 optschanged();
274 #endif
278 * Set the shell parameters.
281 void
282 setparam(char **argv)
284 char **newparam;
285 char **ap;
286 int nparam;
288 for (nparam = 0 ; argv[nparam] ; nparam++);
289 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
290 while (*argv) {
291 *ap++ = savestr(*argv++);
293 *ap = NULL;
294 freeparam(&shellparam);
295 shellparam.malloc = 1;
296 shellparam.nparam = nparam;
297 shellparam.p = newparam;
298 shellparam.optnext = NULL;
303 * Free the list of positional parameters.
306 void
307 freeparam(struct shparam *param)
309 char **ap;
311 if (param->malloc) {
312 for (ap = param->p ; *ap ; ap++)
313 ckfree(*ap);
314 ckfree(param->p);
321 * The shift builtin command.
325 shiftcmd(int argc, char **argv)
327 int n;
328 char **ap1, **ap2;
330 n = 1;
331 if (argc > 1)
332 n = number(argv[1]);
333 if (n > shellparam.nparam)
334 error("can't shift that many");
335 INTOFF;
336 shellparam.nparam -= n;
337 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
338 if (shellparam.malloc)
339 ckfree(*ap1);
341 ap2 = shellparam.p;
342 while ((*ap2++ = *ap1++) != NULL);
343 shellparam.optnext = NULL;
344 INTON;
345 return 0;
351 * The set command builtin.
355 setcmd(int argc, char **argv)
357 if (argc == 1)
358 return showvarscmd(argc, argv);
359 INTOFF;
360 options(0);
361 optschanged();
362 if (*argptr != NULL) {
363 setparam(argptr);
365 INTON;
366 return 0;
370 void
371 getoptsreset(const char *value)
373 if (number(value) == 1) {
374 shellparam.optnext = NULL;
375 shellparam.reset = 1;
380 * The getopts builtin. Shellparam.optnext points to the next argument
381 * to be processed. Shellparam.optptr points to the next character to
382 * be processed in the current argument. If shellparam.optnext is NULL,
383 * then it's the first time getopts has been called.
387 getoptscmd(int argc, char **argv)
389 char **optbase = NULL;
391 if (argc < 3)
392 error("usage: getopts optstring var [arg]");
393 else if (argc == 3)
394 optbase = shellparam.p;
395 else
396 optbase = &argv[3];
398 if (shellparam.reset == 1) {
399 shellparam.optnext = optbase;
400 shellparam.optptr = NULL;
401 shellparam.reset = 0;
404 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
405 &shellparam.optptr);
408 STATIC int
409 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext,
410 char **optptr)
412 char *p, *q;
413 char c = '?';
414 int done = 0;
415 int ind = 0;
416 int err = 0;
417 char s[10];
419 if ((p = *optptr) == NULL || *p == '\0') {
420 /* Current word is done, advance */
421 if (*optnext == NULL)
422 return 1;
423 p = **optnext;
424 if (p == NULL || *p != '-' || *++p == '\0') {
425 atend:
426 ind = *optnext - optfirst + 1;
427 *optnext = NULL;
428 p = NULL;
429 done = 1;
430 goto out;
432 (*optnext)++;
433 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
434 goto atend;
437 c = *p++;
438 for (q = optstr; *q != c; ) {
439 if (*q == '\0') {
440 if (optstr[0] == ':') {
441 s[0] = c;
442 s[1] = '\0';
443 err |= setvarsafe("OPTARG", s, 0);
445 else {
446 out1fmt("Illegal option -%c\n", c);
447 (void) unsetvar("OPTARG");
449 c = '?';
450 goto bad;
452 if (*++q == ':')
453 q++;
456 if (*++q == ':') {
457 if (*p == '\0' && (p = **optnext) == NULL) {
458 if (optstr[0] == ':') {
459 s[0] = c;
460 s[1] = '\0';
461 err |= setvarsafe("OPTARG", s, 0);
462 c = ':';
464 else {
465 out1fmt("No arg for -%c option\n", c);
466 (void) unsetvar("OPTARG");
467 c = '?';
469 goto bad;
472 if (p == **optnext)
473 (*optnext)++;
474 setvarsafe("OPTARG", p, 0);
475 p = NULL;
477 else
478 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 *optptr = 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 *optptr = 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(char *optstring)
516 char *p, *q;
517 char c;
519 if ((p = optptr) == NULL || *p == '\0') {
520 p = *argptr;
521 if (p == NULL || *p != '-' || *++p == '\0')
522 return '\0';
523 argptr++;
524 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
525 return '\0';
527 c = *p++;
528 for (q = optstring ; *q != c ; ) {
529 if (*q == '\0')
530 error("Illegal option -%c", c);
531 if (*++q == ':')
532 q++;
534 if (*++q == ':') {
535 if (*p == '\0' && (p = *argptr++) == NULL)
536 error("No arg for -%c option", c);
537 shoptarg = p;
538 p = NULL;
540 optptr = p;
541 return c;
545 * $PchId: options.c,v 1.6 2006/05/29 13:09:12 philip Exp $