1 /* bashgetopt.c -- `getopt' for use by the builtins. */
3 /* Copyright (C) 1992 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 1, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #define ERR(S, C) builtin_error("%s%c", (S), (C))
33 static WORD_LIST *lhead = (WORD_LIST *)NULL;
34 WORD_LIST *lcurrent = (WORD_LIST *)NULL;
35 WORD_LIST *loptend; /* Points to the first non-option argument in the list */
38 internal_getopt(list, opts)
46 list_optarg = (char *)NULL;
47 loptend = (WORD_LIST *)NULL; /* No non-option arguments */
51 if (list != lhead || !lhead) {
52 /* Hmmm.... called with a different word list. Reset. */
54 lcurrent = lhead = list;
55 loptend = (WORD_LIST *)NULL;
60 (lcurrent->word->word[0] != '-' || lcurrent->word->word[1] == '\0')) {
61 lhead = (WORD_LIST *)NULL;
64 } else if (lcurrent->word->word[0] == '-' &&
65 lcurrent->word->word[1] == '-' &&
66 lcurrent->word->word[2] == 0) {
67 lhead = (WORD_LIST *)NULL;
68 loptend = lcurrent->next;
73 list_optopt = c = lcurrent->word->word[sp];
75 if (c == ':' || (cp = strchr(opts, c)) == NULL) {
76 ERR("illegal option: -", c);
77 if (lcurrent->word->word[++sp] == '\0') {
78 lcurrent = lcurrent->next;
83 loptend = lcurrent->next;
88 /* Option requires an argument. */
89 /* We allow -l2 as equivalent to -l 2 */
90 if (lcurrent->word->word[sp+1] != '\0') {
91 list_optarg = &(lcurrent->word->word[sp+1]);
92 lcurrent = lcurrent->next;
93 } else if (lcurrent->next == NULL) {
94 ERR("option requires an argument: -", c);
96 list_optarg = (char *)NULL;
99 lcurrent = lcurrent->next;
100 list_optarg = lcurrent->word->word;
101 lcurrent = lcurrent->next;
105 /* No argument, just return the option. */
106 if (lcurrent->word->word[++sp] == '\0') {
108 lcurrent = lcurrent->next;
110 list_optarg = (char *)NULL;
117 * reset_internal_getopt -- force the in[ft]ernal getopt to reset
121 reset_internal_getopt ()
123 lhead = lcurrent = loptend = (WORD_LIST *)NULL;