options.c revision 1.29 1 /* $NetBSD: options.c,v 1.29 1999/07/09 03:05:50 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.29 1999/07/09 03:05:50 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
70 char *arg0; /* value of $0 */
71 struct shparam shellparam; /* current positional parameters */
72 char **argptr; /* argument list for builtin commands */
73 char *optarg; /* set by nextopt (like getopt) */
74 char *optptr; /* used by nextopt */
75
76 char *minusc; /* argument to -c option */
77
78
79 STATIC void options __P((int));
80 STATIC void minus_o __P((char *, int));
81 STATIC void setoption __P((int, int));
82 STATIC int getopts __P((char *, char *, char **, char ***, char **));
83
84
85 /*
86 * Process the shell command line arguments.
87 */
88
89 void
90 procargs(argc, argv)
91 int argc;
92 char **argv;
93 {
94 int i;
95
96 argptr = argv;
97 if (argc > 0)
98 argptr++;
99 for (i = 0; i < NOPTS; i++)
100 optlist[i].val = 2;
101 options(1);
102 if (*argptr == NULL && minusc == NULL)
103 sflag = 1;
104 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
105 iflag = 1;
106 if (mflag == 2)
107 mflag = iflag;
108 for (i = 0; i < NOPTS; i++)
109 if (optlist[i].val == 2)
110 optlist[i].val = 0;
111 arg0 = argv[0];
112 if (sflag == 0 && minusc == NULL) {
113 commandname = arg0 = *argptr++;
114 setinputfile(commandname, 0);
115 }
116 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
117 if (argptr && minusc && *argptr)
118 arg0 = *argptr++;
119
120 shellparam.p = argptr;
121 shellparam.reset = 1;
122 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
123 while (*argptr) {
124 shellparam.nparam++;
125 argptr++;
126 }
127 optschanged();
128 }
129
130
131 void
132 optschanged()
133 {
134 setinteractive(iflag);
135 #ifndef SMALL
136 histedit();
137 #endif
138 setjobctl(mflag);
139 }
140
141 /*
142 * Process shell options. The global variable argptr contains a pointer
143 * to the argument list; we advance it past the options.
144 */
145
146 STATIC void
147 options(cmdline)
148 int cmdline;
149 {
150 char *p;
151 int val;
152 int c;
153
154 if (cmdline)
155 minusc = NULL;
156 while ((p = *argptr) != NULL) {
157 argptr++;
158 if ((c = *p++) == '-') {
159 val = 1;
160 if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
161 if (!cmdline) {
162 /* "-" means turn off -x and -v */
163 if (p[0] == '\0')
164 xflag = vflag = 0;
165 /* "--" means reset params */
166 else if (*argptr == NULL)
167 setparam(argptr);
168 }
169 break; /* "-" or "--" terminates options */
170 }
171 } else if (c == '+') {
172 val = 0;
173 } else {
174 argptr--;
175 break;
176 }
177 while ((c = *p++) != '\0') {
178 if (c == 'c' && cmdline) {
179 char *q;
180 #ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
181 if (*p == '\0')
182 #endif
183 q = *argptr++;
184 if (q == NULL || minusc != NULL)
185 error("Bad -c option");
186 minusc = q;
187 #ifdef NOHACK
188 break;
189 #endif
190 } else if (c == 'o') {
191 minus_o(*argptr, val);
192 if (*argptr)
193 argptr++;
194 } else {
195 setoption(c, val);
196 }
197 }
198 }
199 }
200
201 STATIC void
202 minus_o(name, val)
203 char *name;
204 int val;
205 {
206 int i;
207
208 if (name == NULL) {
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 for (i = 0; i < NOPTS; i++)
215 if (equal(name, optlist[i].name)) {
216 setoption(optlist[i].letter, val);
217 return;
218 }
219 error("Illegal option -o %s", name);
220 }
221 }
222
223
224 STATIC void
225 setoption(flag, val)
226 char flag;
227 int val;
228 {
229 int i;
230
231 for (i = 0; i < NOPTS; i++)
232 if (optlist[i].letter == flag) {
233 optlist[i].val = val;
234 if (val) {
235 /* #%$ hack for ksh semantics */
236 if (flag == 'V')
237 Eflag = 0;
238 else if (flag == 'E')
239 Vflag = 0;
240 }
241 return;
242 }
243 error("Illegal option -%c", flag);
244 /* NOTREACHED */
245 }
246
247
248
249 #ifdef mkinit
250 INCLUDE "options.h"
251
252 SHELLPROC {
253 int i;
254
255 for (i = 0; i < NOPTS; i++)
256 optlist[i].val = 0;
257 optschanged();
258
259 }
260 #endif
261
262
263 /*
264 * Set the shell parameters.
265 */
266
267 void
268 setparam(argv)
269 char **argv;
270 {
271 char **newparam;
272 char **ap;
273 int nparam;
274
275 for (nparam = 0 ; argv[nparam] ; nparam++);
276 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
277 while (*argv) {
278 *ap++ = savestr(*argv++);
279 }
280 *ap = NULL;
281 freeparam(&shellparam);
282 shellparam.malloc = 1;
283 shellparam.nparam = nparam;
284 shellparam.p = newparam;
285 shellparam.optnext = NULL;
286 }
287
288
289 /*
290 * Free the list of positional parameters.
291 */
292
293 void
294 freeparam(param)
295 volatile struct shparam *param;
296 {
297 char **ap;
298
299 if (param->malloc) {
300 for (ap = param->p ; *ap ; ap++)
301 ckfree(*ap);
302 ckfree(param->p);
303 }
304 }
305
306
307
308 /*
309 * The shift builtin command.
310 */
311
312 int
313 shiftcmd(argc, argv)
314 int argc;
315 char **argv;
316 {
317 int n;
318 char **ap1, **ap2;
319
320 n = 1;
321 if (argc > 1)
322 n = number(argv[1]);
323 if (n > shellparam.nparam)
324 error("can't shift that many");
325 INTOFF;
326 shellparam.nparam -= n;
327 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
328 if (shellparam.malloc)
329 ckfree(*ap1);
330 }
331 ap2 = shellparam.p;
332 while ((*ap2++ = *ap1++) != NULL);
333 shellparam.optnext = NULL;
334 INTON;
335 return 0;
336 }
337
338
339
340 /*
341 * The set command builtin.
342 */
343
344 int
345 setcmd(argc, argv)
346 int argc;
347 char **argv;
348 {
349 if (argc == 1)
350 return showvarscmd(argc, argv);
351 INTOFF;
352 options(0);
353 optschanged();
354 if (*argptr != NULL) {
355 setparam(argptr);
356 }
357 INTON;
358 return 0;
359 }
360
361
362 void
363 getoptsreset(value)
364 const char *value;
365 {
366 if (number(value) == 1) {
367 shellparam.optnext = NULL;
368 shellparam.reset = 1;
369 }
370 }
371
372 /*
373 * The getopts builtin. Shellparam.optnext points to the next argument
374 * to be processed. Shellparam.optptr points to the next character to
375 * be processed in the current argument. If shellparam.optnext is NULL,
376 * then it's the first time getopts has been called.
377 */
378
379 int
380 getoptscmd(argc, argv)
381 int argc;
382 char **argv;
383 {
384 char **optbase;
385
386 if (argc < 3)
387 error("Usage: getopts optstring var [arg]");
388 else if (argc == 3)
389 optbase = shellparam.p;
390 else
391 optbase = &argv[3];
392
393 if (shellparam.reset == 1) {
394 shellparam.optnext = optbase;
395 shellparam.optptr = NULL;
396 shellparam.reset = 0;
397 }
398
399 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
400 &shellparam.optptr);
401 }
402
403 STATIC int
404 getopts(optstr, optvar, optfirst, optnext, optpptr)
405 char *optstr;
406 char *optvar;
407 char **optfirst;
408 char ***optnext;
409 char **optpptr;
410 {
411 char *p, *q;
412 char c = '?';
413 int done = 0;
414 int ind = 0;
415 int err = 0;
416 char s[10];
417
418 if ((p = *optpptr) == NULL || *p == '\0') {
419 /* Current word is done, advance */
420 if (*optnext == NULL)
421 return 1;
422 p = **optnext;
423 if (p == NULL || *p != '-' || *++p == '\0') {
424 atend:
425 ind = *optnext - optfirst + 1;
426 *optnext = NULL;
427 p = NULL;
428 done = 1;
429 goto out;
430 }
431 (*optnext)++;
432 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
433 goto atend;
434 }
435
436 c = *p++;
437 for (q = optstr; *q != c; ) {
438 if (*q == '\0') {
439 if (optstr[0] == ':') {
440 s[0] = c;
441 s[1] = '\0';
442 err |= setvarsafe("OPTARG", s, 0);
443 }
444 else {
445 outfmt(&errout, "Illegal option -%c\n", c);
446 (void) unsetvar("OPTARG");
447 }
448 c = '?';
449 goto bad;
450 }
451 if (*++q == ':')
452 q++;
453 }
454
455 if (*++q == ':') {
456 if (*p == '\0' && (p = **optnext) == NULL) {
457 if (optstr[0] == ':') {
458 s[0] = c;
459 s[1] = '\0';
460 err |= setvarsafe("OPTARG", s, 0);
461 c = ':';
462 }
463 else {
464 outfmt(&errout, "No arg for -%c option\n", c);
465 (void) unsetvar("OPTARG");
466 c = '?';
467 }
468 goto bad;
469 }
470
471 if (p == **optnext)
472 (*optnext)++;
473 setvarsafe("OPTARG", p, 0);
474 p = NULL;
475 }
476 else
477 setvarsafe("OPTARG", "", 0);
478 ind = *optnext - optfirst + 1;
479 goto out;
480
481 bad:
482 ind = 1;
483 *optnext = NULL;
484 p = NULL;
485 out:
486 *optpptr = p;
487 fmtstr(s, sizeof(s), "%d", ind);
488 err |= setvarsafe("OPTIND", s, VNOFUNC);
489 s[0] = c;
490 s[1] = '\0';
491 err |= setvarsafe(optvar, s, 0);
492 if (err) {
493 *optnext = NULL;
494 *optpptr = NULL;
495 flushall();
496 exraise(EXERROR);
497 }
498 return done;
499 }
500
501 /*
502 * XXX - should get rid of. have all builtins use getopt(3). the
503 * library getopt must have the BSD extension static variable "optreset"
504 * otherwise it can't be used within the shell safely.
505 *
506 * Standard option processing (a la getopt) for builtin routines. The
507 * only argument that is passed to nextopt is the option string; the
508 * other arguments are unnecessary. It return the character, or '\0' on
509 * end of input.
510 */
511
512 int
513 nextopt(optstring)
514 const char *optstring;
515 {
516 char *p;
517 const char *q;
518 char c;
519
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';
527 }
528 c = *p++;
529 for (q = optstring ; *q != c ; ) {
530 if (*q == '\0')
531 error("Illegal option -%c", c);
532 if (*++q == ':')
533 q++;
534 }
535 if (*++q == ':') {
536 if (*p == '\0' && (p = *argptr++) == NULL)
537 error("No arg for -%c option", c);
538 optarg = p;
539 p = NULL;
540 }
541 optptr = p;
542 return c;
543 }
544