options.c revision 1.32 1 /* $NetBSD: options.c,v 1.32 2002/11/24 22:35:42 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Kenneth Almquist.
9 *
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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
43 #else
44 __RCSID("$NetBSD: options.c,v 1.32 2002/11/24 22:35:42 christos Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <signal.h>
49 #include <unistd.h>
50 #include <stdlib.h>
51
52 #include "shell.h"
53 #define DEFINE_OPTIONS
54 #include "options.h"
55 #undef DEFINE_OPTIONS
56 #include "nodes.h" /* for other header files */
57 #include "eval.h"
58 #include "jobs.h"
59 #include "input.h"
60 #include "output.h"
61 #include "trap.h"
62 #include "var.h"
63 #include "memalloc.h"
64 #include "error.h"
65 #include "mystring.h"
66 #ifndef SMALL
67 #include "myhistedit.h"
68 #endif
69 #include "show.h"
70
71 char *arg0; /* value of $0 */
72 struct shparam shellparam; /* current positional parameters */
73 char **argptr; /* argument list for builtin commands */
74 char *optionarg; /* set by nextopt (like getopt) */
75 char *optptr; /* used by nextopt */
76
77 char *minusc; /* argument to -c option */
78
79
80 STATIC void options(int);
81 STATIC void minus_o(char *, int);
82 STATIC void setoption(int, int);
83 STATIC int getopts(char *, char *, char **, char ***, char **);
84
85
86 /*
87 * Process the shell command line arguments.
88 */
89
90 void
91 procargs(int argc, char **argv)
92 {
93 int i;
94
95 argptr = argv;
96 if (argc > 0)
97 argptr++;
98 for (i = 0; i < NOPTS; i++)
99 optlist[i].val = 2;
100 options(1);
101 if (*argptr == NULL && minusc == NULL)
102 sflag = 1;
103 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
104 iflag = 1;
105 if (mflag == 2)
106 mflag = iflag;
107 for (i = 0; i < NOPTS; i++)
108 if (optlist[i].val == 2)
109 optlist[i].val = 0;
110 #if DEBUG == 2
111 debug = 1;
112 #endif
113 arg0 = argv[0];
114 if (sflag == 0 && minusc == NULL) {
115 commandname = argv[0];
116 arg0 = *argptr++;
117 setinputfile(arg0, 0);
118 commandname = arg0;
119 }
120 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
121 if (argptr && minusc && *argptr)
122 arg0 = *argptr++;
123
124 shellparam.p = argptr;
125 shellparam.reset = 1;
126 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
127 while (*argptr) {
128 shellparam.nparam++;
129 argptr++;
130 }
131 optschanged();
132 }
133
134
135 void
136 optschanged(void)
137 {
138 setinteractive(iflag);
139 #ifndef SMALL
140 histedit();
141 #endif
142 setjobctl(mflag);
143 }
144
145 /*
146 * Process shell options. The global variable argptr contains a pointer
147 * to the argument list; we advance it past the options.
148 */
149
150 STATIC void
151 options(int cmdline)
152 {
153 char *p;
154 int val;
155 int c;
156
157 if (cmdline)
158 minusc = NULL;
159 while ((p = *argptr) != NULL) {
160 argptr++;
161 if ((c = *p++) == '-') {
162 val = 1;
163 if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
164 if (!cmdline) {
165 /* "-" means turn off -x and -v */
166 if (p[0] == '\0')
167 xflag = vflag = 0;
168 /* "--" means reset params */
169 else if (*argptr == NULL)
170 setparam(argptr);
171 }
172 break; /* "-" or "--" terminates options */
173 }
174 } else if (c == '+') {
175 val = 0;
176 } else {
177 argptr--;
178 break;
179 }
180 while ((c = *p++) != '\0') {
181 if (c == 'c' && cmdline) {
182 char *q;
183 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
184 if (*p == '\0')
185 #endif
186 q = *argptr++;
187 if (q == NULL || minusc != NULL)
188 error("Bad -c option");
189 minusc = q;
190 #ifdef NOHACK
191 break;
192 #endif
193 } else if (c == 'o') {
194 minus_o(*argptr, val);
195 if (*argptr)
196 argptr++;
197 } else {
198 setoption(c, val);
199 }
200 }
201 }
202 }
203
204 static void
205 set_opt_val(int i, int val)
206 {
207 int j;
208 int flag;
209
210 if (val && (flag = optlist[i].opt_set)) {
211 /* some options (eg vi/emacs) are mutually exclusive */
212 for (j = 0; j < NOPTS; j++)
213 if (optlist[j].opt_set == flag)
214 optlist[j].val = 0;
215 }
216 optlist[i].val = val;
217 #ifdef DEBUG
218 if (&optlist[i].val == &debug)
219 opentrace();
220 #endif
221 }
222
223 STATIC void
224 minus_o(char *name, int val)
225 {
226 int i;
227
228 if (name == NULL) {
229 out1str("Current option settings\n");
230 for (i = 0; i < NOPTS; i++)
231 out1fmt("%-16s%s\n", optlist[i].name,
232 optlist[i].val ? "on" : "off");
233 } else {
234 for (i = 0; i < NOPTS; i++)
235 if (equal(name, optlist[i].name)) {
236 set_opt_val(i, val);
237 return;
238 }
239 error("Illegal option -o %s", name);
240 }
241 }
242
243
244 STATIC void
245 setoption(int flag, int val)
246 {
247 int i;
248
249 for (i = 0; i < NOPTS; i++)
250 if (optlist[i].letter == flag) {
251 set_opt_val( i, val );
252 return;
253 }
254 error("Illegal option -%c", flag);
255 /* NOTREACHED */
256 }
257
258
259
260 #ifdef mkinit
261 INCLUDE "options.h"
262
263 SHELLPROC {
264 int i;
265
266 for (i = 0; optlist[i].name; i++)
267 optlist[i].val = 0;
268 optschanged();
269
270 }
271 #endif
272
273
274 /*
275 * Set the shell parameters.
276 */
277
278 void
279 setparam(char **argv)
280 {
281 char **newparam;
282 char **ap;
283 int nparam;
284
285 for (nparam = 0 ; argv[nparam] ; nparam++);
286 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
287 while (*argv) {
288 *ap++ = savestr(*argv++);
289 }
290 *ap = NULL;
291 freeparam(&shellparam);
292 shellparam.malloc = 1;
293 shellparam.nparam = nparam;
294 shellparam.p = newparam;
295 shellparam.optnext = NULL;
296 }
297
298
299 /*
300 * Free the list of positional parameters.
301 */
302
303 void
304 freeparam(volatile struct shparam *param)
305 {
306 char **ap;
307
308 if (param->malloc) {
309 for (ap = param->p ; *ap ; ap++)
310 ckfree(*ap);
311 ckfree(param->p);
312 }
313 }
314
315
316
317 /*
318 * The shift builtin command.
319 */
320
321 int
322 shiftcmd(int argc, char **argv)
323 {
324 int n;
325 char **ap1, **ap2;
326
327 n = 1;
328 if (argc > 1)
329 n = number(argv[1]);
330 if (n > shellparam.nparam)
331 error("can't shift that many");
332 INTOFF;
333 shellparam.nparam -= n;
334 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
335 if (shellparam.malloc)
336 ckfree(*ap1);
337 }
338 ap2 = shellparam.p;
339 while ((*ap2++ = *ap1++) != NULL);
340 shellparam.optnext = NULL;
341 INTON;
342 return 0;
343 }
344
345
346
347 /*
348 * The set command builtin.
349 */
350
351 int
352 setcmd(int argc, char **argv)
353 {
354 if (argc == 1)
355 return showvars(0, 0, 1);
356 INTOFF;
357 options(0);
358 optschanged();
359 if (*argptr != NULL) {
360 setparam(argptr);
361 }
362 INTON;
363 return 0;
364 }
365
366
367 void
368 getoptsreset(value)
369 const char *value;
370 {
371 if (number(value) == 1) {
372 shellparam.optnext = NULL;
373 shellparam.reset = 1;
374 }
375 }
376
377 /*
378 * The getopts builtin. Shellparam.optnext points to the next argument
379 * to be processed. Shellparam.optptr points to the next character to
380 * be processed in the current argument. If shellparam.optnext is NULL,
381 * then it's the first time getopts has been called.
382 */
383
384 int
385 getoptscmd(int argc, char **argv)
386 {
387 char **optbase;
388
389 if (argc < 3)
390 error("Usage: getopts optstring var [arg]");
391 else if (argc == 3)
392 optbase = shellparam.p;
393 else
394 optbase = &argv[3];
395
396 if (shellparam.reset == 1) {
397 shellparam.optnext = optbase;
398 shellparam.optptr = NULL;
399 shellparam.reset = 0;
400 }
401
402 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
403 &shellparam.optptr);
404 }
405
406 STATIC int
407 getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, char **optpptr)
408 {
409 char *p, *q;
410 char c = '?';
411 int done = 0;
412 int ind = 0;
413 int err = 0;
414 char s[10];
415
416 if ((p = *optpptr) == NULL || *p == '\0') {
417 /* Current word is done, advance */
418 if (*optnext == NULL)
419 return 1;
420 p = **optnext;
421 if (p == NULL || *p != '-' || *++p == '\0') {
422 atend:
423 ind = *optnext - optfirst + 1;
424 *optnext = NULL;
425 p = NULL;
426 done = 1;
427 goto out;
428 }
429 (*optnext)++;
430 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
431 goto atend;
432 }
433
434 c = *p++;
435 for (q = optstr; *q != c; ) {
436 if (*q == '\0') {
437 if (optstr[0] == ':') {
438 s[0] = c;
439 s[1] = '\0';
440 err |= setvarsafe("OPTARG", s, 0);
441 } else {
442 outfmt(&errout, "Illegal option -%c\n", c);
443 (void) unsetvar("OPTARG", 0);
444 }
445 c = '?';
446 goto bad;
447 }
448 if (*++q == ':')
449 q++;
450 }
451
452 if (*++q == ':') {
453 if (*p == '\0' && (p = **optnext) == NULL) {
454 if (optstr[0] == ':') {
455 s[0] = c;
456 s[1] = '\0';
457 err |= setvarsafe("OPTARG", s, 0);
458 c = ':';
459 } else {
460 outfmt(&errout, "No arg for -%c option\n", c);
461 (void) unsetvar("OPTARG", 0);
462 c = '?';
463 }
464 goto bad;
465 }
466
467 if (p == **optnext)
468 (*optnext)++;
469 err |= setvarsafe("OPTARG", p, 0);
470 p = NULL;
471 } else
472 err |= setvarsafe("OPTARG", "", 0);
473 ind = *optnext - optfirst + 1;
474 goto out;
475
476 bad:
477 ind = 1;
478 *optnext = NULL;
479 p = NULL;
480 out:
481 *optpptr = p;
482 fmtstr(s, sizeof(s), "%d", ind);
483 err |= setvarsafe("OPTIND", s, VNOFUNC);
484 s[0] = c;
485 s[1] = '\0';
486 err |= setvarsafe(optvar, s, 0);
487 if (err) {
488 *optnext = NULL;
489 *optpptr = NULL;
490 flushall();
491 exraise(EXERROR);
492 }
493 return done;
494 }
495
496 /*
497 * XXX - should get rid of. have all builtins use getopt(3). the
498 * library getopt must have the BSD extension static variable "optreset"
499 * otherwise it can't be used within the shell safely.
500 *
501 * Standard option processing (a la getopt) for builtin routines. The
502 * only argument that is passed to nextopt is the option string; the
503 * other arguments are unnecessary. It return the character, or '\0' on
504 * end of input.
505 */
506
507 int
508 nextopt(const char *optstring)
509 {
510 char *p;
511 const char *q;
512 char c;
513
514 if ((p = optptr) == NULL || *p == '\0') {
515 p = *argptr;
516 if (p == NULL || *p != '-' || *++p == '\0')
517 return '\0';
518 argptr++;
519 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
520 return '\0';
521 }
522 c = *p++;
523 for (q = optstring ; *q != c ; ) {
524 if (*q == '\0')
525 error("Illegal option -%c", c);
526 if (*++q == ':')
527 q++;
528 }
529 if (*++q == ':') {
530 if (*p == '\0' && (p = *argptr++) == NULL)
531 error("No arg for -%c option", c);
532 optionarg = p;
533 p = NULL;
534 }
535 optptr = p;
536 return c;
537 }
538