source: trunk/minix/commands/ash/options.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 8.7 KB
Line 
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * 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
38static char sccsid[] = "@(#)options.c 5.2 (Berkeley) 3/13/91";
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
56char *arg0; /* value of $0 */
57struct shparam shellparam; /* current positional parameters */
58char **argptr; /* argument list for builtin commands */
59char *optarg; /* set by nextopt (like getopt) */
60char *optptr; /* used by nextopt */
61int editable; /* isatty(0) && isatty(1) */
62
63char *minusc; /* argument to -c option */
64
65
66#ifdef __STDC__
67STATIC void options(int);
68STATIC void setoption(int, int);
69#else
70STATIC void options();
71STATIC void setoption();
72#endif
73
74
75
76/*
77 * Process the shell command line arguments.
78 */
79
80void
81procargs(argc, argv)
82 char **argv;
83 {
84 char *p;
85
86 argptr = argv;
87 if (argc > 0)
88 argptr++;
89 for (p = optval ; p < optval + sizeof optval - 1 ; p++)
90 *p = 2;
91 options(1);
92 if (*argptr == NULL && minusc == NULL)
93 sflag = 1;
94 editable = (isatty(0) && isatty(1));
95 if (iflag == 2 && sflag == 1 && editable)
96 iflag = 1;
97 if (jflag == 2)
98 jflag = iflag;
99 for (p = optval ; p < optval + sizeof optval - 1 ; p++)
100 if (*p == 2)
101 *p = 0;
102 arg0 = argv[0];
103 if (sflag == 0) {
104 arg0 = *argptr++;
105 if (minusc == NULL) {
106 commandname = arg0;
107 setinputfile(commandname, 0);
108 }
109 }
110 shellparam.p = argptr;
111 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
112 while (*argptr) {
113 shellparam.nparam++;
114 argptr++;
115 }
116 setinteractive(iflag);
117 setjobctl(jflag);
118}
119
120
121
122/*
123 * Process shell options. The global variable argptr contains a pointer
124 * to the argument list; we advance it past the options.
125 */
126
127STATIC void
128options(cmdline) {
129 register char *p;
130 int val;
131 int c;
132
133 if (cmdline)
134 minusc = NULL;
135 while ((p = *argptr) != NULL) {
136 argptr++;
137 if ((c = *p++) == '-') {
138 val = 1;
139 if (p[0] == '\0' || p[0] == '-' && p[1] == '\0') {
140 if (!cmdline) {
141 /* "-" means turn off -x and -v */
142 if (p[0] == '\0')
143 xflag = vflag = 0;
144 /* "--" means reset params */
145 else if (*argptr == NULL)
146 setparam(argptr);
147 }
148 break; /* "-" or "--" terminates options */
149 }
150 } else if (c == '+') {
151 val = 0;
152 } else {
153 argptr--;
154 break;
155 }
156 while ((c = *p++) != '\0') {
157 if (c == 'c' && cmdline) {
158 char *q;
159#ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */
160 if (*p == '\0')
161#endif
162 q = *argptr++;
163 if (q == NULL || minusc != NULL)
164 error("Bad -c option");
165 minusc = q;
166#ifdef NOHACK
167 break;
168#endif
169 } else {
170 setoption(c, val);
171 }
172 }
173 if (! cmdline)
174 break;
175 }
176}
177
178
179STATIC void
180setoption(flag, val)
181 char flag;
182 int val;
183 {
184 register char *p;
185
186 if ((p = strchr(optchar, flag)) == NULL)
187 error("Illegal option -%c", flag);
188 optval[p - optchar] = val;
189}
190
191
192
193#ifdef mkinit
194INCLUDE "options.h"
195
196SHELLPROC {
197 char *p;
198
199 for (p = optval ; p < optval + sizeof optval ; p++)
200 *p = 0;
201}
202#endif
203
204
205/*
206 * Set the shell parameters.
207 */
208
209void
210setparam(argv)
211 char **argv;
212 {
213 char **newparam;
214 char **ap;
215 int nparam;
216
217 for (nparam = 0 ; argv[nparam] ; nparam++);
218 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
219 while (*argv) {
220 *ap++ = savestr(*argv++);
221 }
222 *ap = NULL;
223 freeparam(&shellparam);
224 shellparam.malloc = 1;
225 shellparam.nparam = nparam;
226 shellparam.p = newparam;
227 shellparam.optnext = NULL;
228}
229
230
231/*
232 * Free the list of positional parameters.
233 */
234
235void
236freeparam(param)
237 struct shparam *param;
238 {
239 char **ap;
240
241 if (param->malloc) {
242 for (ap = param->p ; *ap ; ap++)
243 ckfree(*ap);
244 ckfree(param->p);
245 }
246}
247
248
249
250/*
251 * The shift builtin command.
252 */
253
254shiftcmd(argc, argv) char **argv; {
255 int n;
256 char **ap1, **ap2;
257
258 n = 1;
259 if (argc > 1)
260 n = number(argv[1]);
261 if (n > shellparam.nparam)
262 n = shellparam.nparam;
263 INTOFF;
264 shellparam.nparam -= n;
265 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
266 if (shellparam.malloc)
267 ckfree(*ap1);
268 }
269 ap2 = shellparam.p;
270 while ((*ap2++ = *ap1++) != NULL);
271 shellparam.optnext = NULL;
272 INTON;
273 return 0;
274}
275
276
277
278/*
279 * The set command builtin.
280 */
281
282setcmd(argc, argv) char **argv; {
283 if (argc == 1)
284 return showvarscmd(argc, argv);
285 INTOFF;
286 options(0);
287 setinteractive(iflag);
288 setjobctl(jflag);
289 if (*argptr != NULL) {
290 setparam(argptr);
291 }
292 INTON;
293 return 0;
294}
295
296
297/*
298 * The getopts builtin. Shellparam.optnext points to the next argument
299 * to be processed. Shellparam.optptr points to the next character to
300 * be processed in the current argument. If shellparam.optnext is NULL,
301 * then it's the first time getopts has been called.
302 */
303
304getoptscmd(argc, argv) char **argv; {
305 register char *p, *q;
306 char c;
307 char s[10];
308
309 if (argc != 3)
310 error("Usage: getopts optstring var");
311 if (shellparam.optnext == NULL) {
312 shellparam.optnext = shellparam.p;
313 shellparam.optptr = NULL;
314 }
315 if ((p = shellparam.optptr) == NULL || *p == '\0') {
316 p = *shellparam.optnext;
317 if (p == NULL || *p != '-' || *++p == '\0') {
318atend:
319 fmtstr(s, 10, "%d", shellparam.optnext - shellparam.p + 1);
320 setvar("OPTIND", s, 0);
321 shellparam.optnext = NULL;
322 return 1;
323 }
324 shellparam.optnext++;
325 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
326 goto atend;
327 }
328 c = *p++;
329 for (q = argv[1] ; *q != c ; ) {
330 if (*q == '\0') {
331 out1fmt("Illegal option -%c\n", c);
332 c = '?';
333 goto out;
334 }
335 if (*++q == ':')
336 q++;
337 }
338 if (*++q == ':') {
339 if (*p == '\0') {
340 if ((p = *shellparam.optnext) == NULL) {
341 out1fmt("No arg for -%c option\n", c);
342 c = '?';
343 goto out;
344 }
345 shellparam.optnext++;
346 }
347 setvar("OPTARG", p, 0);
348 p = "";
349 }
350out:
351 shellparam.optptr = p;
352 s[0] = c;
353 s[1] = '\0';
354 setvar(argv[2], s, 0);
355 fmtstr(s, 10, "%d", shellparam.optnext - shellparam.p + 1);
356 setvar("OPTIND", s, 0);
357 return 0;
358}
359
360/*
361 * Standard option processing (a la getopt) for builtin routines. The
362 * only argument that is passed to nextopt is the option string; the
363 * other arguments are unnecessary. It return the character, or '\0' on
364 * end of input.
365 */
366
367int
368nextopt(optstring)
369 char *optstring;
370 {
371 register char *p, *q;
372 char c;
373
374 if ((p = optptr) == NULL || *p == '\0') {
375 p = *argptr;
376 if (p == NULL || *p != '-' || *++p == '\0')
377 return '\0';
378 argptr++;
379 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
380 return '\0';
381 }
382 c = *p++;
383 for (q = optstring ; *q != c ; ) {
384 if (*q == '\0')
385 error("Illegal option -%c", c);
386 if (*++q == ':')
387 q++;
388 }
389 if (*++q == ':') {
390 if (*p == '\0' && (p = *argptr++) == NULL)
391 error("No arg for -%c option", c);
392 optarg = p;
393 p = NULL;
394 }
395 optptr = p;
396 return c;
397}
Note: See TracBrowser for help on using the repository browser.