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