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