VirtualBox

source: kBuild/trunk/src/kash/options.c@ 1196

Last change on this file since 1196 was 809, checked in by bird, 18 years ago

Solaris + cleanup.

  • Property svn:eol-style set to native
File size: 10.9 KB
Line 
1/* $NetBSD: options.c,v 1.38 2005/03/20 21:38:17 dsl 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#ifdef HAVE_SYS_CDEFS_H
36#include <sys/cdefs.h>
37#endif
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95";
41#else
42__RCSID("$NetBSD: options.c,v 1.38 2005/03/20 21:38:17 dsl Exp $");
43#endif
44#endif /* not lint */
45
46#include <signal.h>
47#include <unistd.h>
48#include <stdlib.h>
49
50#include "shell.h"
51#define DEFINE_OPTIONS
52#include "options.h"
53#undef DEFINE_OPTIONS
54#include "nodes.h" /* for other header files */
55#include "eval.h"
56#include "jobs.h"
57#include "input.h"
58#include "output.h"
59#include "trap.h"
60#include "var.h"
61#include "memalloc.h"
62#include "error.h"
63#include "mystring.h"
64#ifndef SMALL
65#include "myhistedit.h"
66#endif
67#include "show.h"
68
69char *arg0; /* value of $0 */
70struct shparam shellparam; /* current positional parameters */
71char **argptr; /* argument list for builtin commands */
72char *optionarg; /* set by nextopt (like getopt) */
73char *optptr; /* used by nextopt */
74
75char *minusc; /* argument to -c option */
76
77
78STATIC void options(int);
79STATIC void minus_o(char *, int);
80STATIC void setoption(int, int);
81STATIC int getopts(char *, char *, char **, char ***, char **);
82
83
84/*
85 * Process the shell command line arguments.
86 */
87
88void
89procargs(int argc, char **argv)
90{
91 int i;
92
93 argptr = argv;
94 if (argc > 0)
95 argptr++;
96 for (i = 0; i < NOPTS; i++)
97 optlist[i].val = 2;
98 options(1);
99 if (*argptr == NULL && minusc == NULL)
100 sflag = 1;
101 if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1))
102 iflag = 1;
103 if (mflag == 2)
104 mflag = iflag;
105 for (i = 0; i < NOPTS; i++)
106 if (optlist[i].val == 2)
107 optlist[i].val = 0;
108#if DEBUG == 2
109 debug = 1;
110#endif
111 arg0 = argv[0];
112 if (sflag == 0 && minusc == NULL) {
113 commandname = argv[0];
114 arg0 = *argptr++;
115 setinputfile(arg0, 0);
116 commandname = arg0;
117 }
118 /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */
119 if (minusc != NULL) {
120 if (argptr == NULL || *argptr == NULL)
121 error("Bad -c option");
122 minusc = *argptr++;
123 if (*argptr != 0)
124 arg0 = *argptr++;
125 }
126
127 shellparam.p = argptr;
128 shellparam.reset = 1;
129 /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */
130 while (*argptr) {
131 shellparam.nparam++;
132 argptr++;
133 }
134 optschanged();
135}
136
137
138void
139optschanged(void)
140{
141 setinteractive(iflag);
142#ifndef SMALL
143 histedit();
144#endif
145 setjobctl(mflag);
146}
147
148/*
149 * Process shell options. The global variable argptr contains a pointer
150 * to the argument list; we advance it past the options.
151 */
152
153STATIC void
154options(int cmdline)
155{
156 static char empty[] = "";
157 char *p;
158 int val;
159 int c;
160
161 if (cmdline)
162 minusc = NULL;
163 while ((p = *argptr) != NULL) {
164 argptr++;
165 if ((c = *p++) == '-') {
166 val = 1;
167 if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) {
168 if (!cmdline) {
169 /* "-" means turn off -x and -v */
170 if (p[0] == '\0')
171 xflag = vflag = 0;
172 /* "--" means reset params */
173 else if (*argptr == NULL)
174 setparam(argptr);
175 }
176 break; /* "-" or "--" terminates options */
177 }
178 } else if (c == '+') {
179 val = 0;
180 } else {
181 argptr--;
182 break;
183 }
184 while ((c = *p++) != '\0') {
185 if (c == 'c' && cmdline) {
186 /* command is after shell args*/
187 minusc = empty;
188 } else if (c == 'o') {
189 minus_o(*argptr, val);
190 if (*argptr)
191 argptr++;
192 } else {
193 setoption(c, val);
194 }
195 }
196 }
197}
198
199static void
200set_opt_val(int i, int val)
201{
202 int j;
203 int flag;
204
205 if (val && (flag = optlist[i].opt_set)) {
206 /* some options (eg vi/emacs) are mutually exclusive */
207 for (j = 0; j < NOPTS; j++)
208 if (optlist[j].opt_set == flag)
209 optlist[j].val = 0;
210 }
211 optlist[i].val = val;
212#ifdef DEBUG
213 if (&optlist[i].val == &debug)
214 opentrace();
215#endif
216}
217
218STATIC void
219minus_o(char *name, int val)
220{
221 int i;
222
223 if (name == NULL) {
224 out1str("Current option settings\n");
225 for (i = 0; i < NOPTS; i++)
226 out1fmt("%-16s%s\n", optlist[i].name,
227 optlist[i].val ? "on" : "off");
228 } else {
229 for (i = 0; i < NOPTS; i++)
230 if (equal(name, optlist[i].name)) {
231 set_opt_val(i, val);
232 return;
233 }
234 error("Illegal option -o %s", name);
235 }
236}
237
238
239STATIC void
240setoption(int flag, int val)
241{
242 int i;
243
244 for (i = 0; i < NOPTS; i++)
245 if (optlist[i].letter == flag) {
246 set_opt_val( i, val );
247 return;
248 }
249 error("Illegal option -%c", flag);
250 /* NOTREACHED */
251}
252
253
254
255#ifdef mkinit
256INCLUDE "options.h"
257
258SHELLPROC {
259 int i;
260
261 for (i = 0; optlist[i].name; i++)
262 optlist[i].val = 0;
263 optschanged();
264
265}
266#endif
267
268
269/*
270 * Set the shell parameters.
271 */
272
273void
274setparam(char **argv)
275{
276 char **newparam;
277 char **ap;
278 int nparam;
279
280 for (nparam = 0 ; argv[nparam] ; nparam++)
281 continue;
282 ap = newparam = ckmalloc((nparam + 1) * sizeof *ap);
283 while (*argv) {
284 *ap++ = savestr(*argv++);
285 }
286 *ap = NULL;
287 freeparam(&shellparam);
288 shellparam.malloc = 1;
289 shellparam.nparam = nparam;
290 shellparam.p = newparam;
291 shellparam.optnext = NULL;
292}
293
294
295/*
296 * Free the list of positional parameters.
297 */
298
299void
300freeparam(volatile struct shparam *param)
301{
302 char **ap;
303
304 if (param->malloc) {
305 for (ap = param->p ; *ap ; ap++)
306 ckfree(*ap);
307 ckfree(param->p);
308 }
309}
310
311
312
313/*
314 * The shift builtin command.
315 */
316
317int
318shiftcmd(int argc, char **argv)
319{
320 int n;
321 char **ap1, **ap2;
322
323 n = 1;
324 if (argc > 1)
325 n = number(argv[1]);
326 if (n > shellparam.nparam)
327 error("can't shift that many");
328 INTOFF;
329 shellparam.nparam -= n;
330 for (ap1 = shellparam.p ; --n >= 0 ; ap1++) {
331 if (shellparam.malloc)
332 ckfree(*ap1);
333 }
334 ap2 = shellparam.p;
335 while ((*ap2++ = *ap1++) != NULL);
336 shellparam.optnext = NULL;
337 INTON;
338 return 0;
339}
340
341
342
343/*
344 * The set command builtin.
345 */
346
347int
348setcmd(int argc, char **argv)
349{
350 if (argc == 1)
351 return showvars(0, 0, 1);
352 INTOFF;
353 options(0);
354 optschanged();
355 if (*argptr != NULL) {
356 setparam(argptr);
357 }
358 INTON;
359 return 0;
360}
361
362
363void
364getoptsreset(value)
365 const char *value;
366{
367 if (number(value) == 1) {
368 shellparam.optnext = NULL;
369 shellparam.reset = 1;
370 }
371}
372
373/*
374 * The getopts builtin. Shellparam.optnext points to the next argument
375 * to be processed. Shellparam.optptr points to the next character to
376 * be processed in the current argument. If shellparam.optnext is NULL,
377 * then it's the first time getopts has been called.
378 */
379
380int
381getoptscmd(int argc, char **argv)
382{
383 char **optbase;
384
385 if (argc < 3)
386 error("usage: getopts optstring var [arg]");
387 else if (argc == 3)
388 optbase = shellparam.p;
389 else
390 optbase = &argv[3];
391
392 if (shellparam.reset == 1) {
393 shellparam.optnext = optbase;
394 shellparam.optptr = NULL;
395 shellparam.reset = 0;
396 }
397
398 return getopts(argv[1], argv[2], optbase, &shellparam.optnext,
399 &shellparam.optptr);
400}
401
402STATIC int
403getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, char **optpptr)
404{
405 char *p, *q;
406 char c = '?';
407 int done = 0;
408 int ind = 0;
409 int err = 0;
410 char s[12];
411
412 if ((p = *optpptr) == NULL || *p == '\0') {
413 /* Current word is done, advance */
414 if (*optnext == NULL)
415 return 1;
416 p = **optnext;
417 if (p == NULL || *p != '-' || *++p == '\0') {
418atend:
419 ind = *optnext - optfirst + 1;
420 *optnext = NULL;
421 p = NULL;
422 done = 1;
423 goto out;
424 }
425 (*optnext)++;
426 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
427 goto atend;
428 }
429
430 c = *p++;
431 for (q = optstr; *q != c; ) {
432 if (*q == '\0') {
433 if (optstr[0] == ':') {
434 s[0] = c;
435 s[1] = '\0';
436 err |= setvarsafe("OPTARG", s, 0);
437 } else {
438 outfmt(&errout, "Illegal option -%c\n", c);
439 (void) unsetvar("OPTARG", 0);
440 }
441 c = '?';
442 goto bad;
443 }
444 if (*++q == ':')
445 q++;
446 }
447
448 if (*++q == ':') {
449 if (*p == '\0' && (p = **optnext) == NULL) {
450 if (optstr[0] == ':') {
451 s[0] = c;
452 s[1] = '\0';
453 err |= setvarsafe("OPTARG", s, 0);
454 c = ':';
455 } else {
456 outfmt(&errout, "No arg for -%c option\n", c);
457 (void) unsetvar("OPTARG", 0);
458 c = '?';
459 }
460 goto bad;
461 }
462
463 if (p == **optnext)
464 (*optnext)++;
465 err |= setvarsafe("OPTARG", p, 0);
466 p = NULL;
467 } else
468 err |= setvarsafe("OPTARG", "", 0);
469 ind = *optnext - optfirst + 1;
470 goto out;
471
472bad:
473 ind = 1;
474 *optnext = NULL;
475 p = NULL;
476out:
477 *optpptr = p;
478 fmtstr(s, sizeof(s), "%d", ind);
479 err |= setvarsafe("OPTIND", s, VNOFUNC);
480 s[0] = c;
481 s[1] = '\0';
482 err |= setvarsafe(optvar, s, 0);
483 if (err) {
484 *optnext = NULL;
485 *optpptr = NULL;
486 output_flushall();
487 exraise(EXERROR);
488 }
489 return done;
490}
491
492/*
493 * XXX - should get rid of. have all builtins use getopt(3). the
494 * library getopt must have the BSD extension static variable "optreset"
495 * otherwise it can't be used within the shell safely.
496 *
497 * Standard option processing (a la getopt) for builtin routines. The
498 * only argument that is passed to nextopt is the option string; the
499 * other arguments are unnecessary. It return the character, or '\0' on
500 * end of input.
501 */
502
503int
504nextopt(const char *optstring)
505{
506 char *p;
507 const char *q;
508 char c;
509
510 if ((p = optptr) == NULL || *p == '\0') {
511 p = *argptr;
512 if (p == NULL || *p != '-' || *++p == '\0')
513 return '\0';
514 argptr++;
515 if (p[0] == '-' && p[1] == '\0') /* check for "--" */
516 return '\0';
517 }
518 c = *p++;
519 for (q = optstring ; *q != c ; ) {
520 if (*q == '\0')
521 error("Illegal option -%c", c);
522 if (*++q == ':')
523 q++;
524 }
525 if (*++q == ':') {
526 if (*p == '\0' && (p = *argptr++) == NULL)
527 error("No arg for -%c option", c);
528 optionarg = p;
529 p = NULL;
530 }
531 optptr = p;
532 return c;
533}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette