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