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