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