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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #if defined (HAVE_UNISTD_H)
27 #include "../bashansi.h"
28 #include <chartypes.h>
34 #define ERR(S, C) builtin_error("%s%c", (S), (C))
41 static WORD_LIST *lhead = (WORD_LIST *)NULL;
42 WORD_LIST *lcurrent = (WORD_LIST *)NULL;
43 WORD_LIST *loptend; /* Points to the first non-option argument in the list */
46 internal_getopt(list, opts)
52 int plus; /* nonzero means to handle +option */
61 list_optarg = (char *)NULL;
62 loptend = (WORD_LIST *)NULL; /* No non-option arguments */
66 if (list != lhead || lhead == 0) {
67 /* Hmmm.... called with a different word list. Reset. */
69 lcurrent = lhead = list;
70 loptend = (WORD_LIST *)NULL;
75 (lcurrent->word->word[0] != '-' || lcurrent->word->word[1] == '\0')) {
76 lhead = (WORD_LIST *)NULL;
79 } else if (lcurrent->word->word[0] == '-' &&
80 lcurrent->word->word[1] == '-' &&
81 lcurrent->word->word[2] == 0) {
82 lhead = (WORD_LIST *)NULL;
83 loptend = lcurrent->next;
88 list_optopt = c = lcurrent->word->word[sp];
90 if (c == ':' || (cp = strchr(opts, c)) == NULL) {
91 ERR("illegal option: -", c);
92 if (lcurrent->word->word[++sp] == '\0') {
93 lcurrent = lcurrent->next;
98 loptend = lcurrent->next;
102 if (*++cp == ':' || *cp == ';') {
103 /* `:': Option requires an argument. */
104 /* `;': option argument may be missing */
105 /* We allow -l2 as equivalent to -l 2 */
106 if (lcurrent->word->word[sp+1]) {
107 list_optarg = lcurrent->word->word + sp + 1;
108 lcurrent = lcurrent->next;
109 /* If the specifier is `;', don't set optarg if the next
110 argument looks like another option. */
111 } else if (lcurrent->next && (*cp == ':' || lcurrent->next->word->word[0] != '-')) {
112 lcurrent = lcurrent->next;
113 list_optarg = lcurrent->word->word;
114 lcurrent = lcurrent->next;
115 } else if (*cp == ';') {
116 list_optarg = (char *)NULL;
117 lcurrent = lcurrent->next;
118 } else { /* lcurrent->next == NULL */
119 ERR("option requires an argument: -", c);
121 list_optarg = (char *)NULL;
125 } else if (*cp == '#') {
126 /* optional numeric argument */
127 if (lcurrent->word->word[sp+1]) {
128 if (DIGIT(lcurrent->word->word[sp+1])) {
129 list_optarg = lcurrent->word->word + sp + 1;
130 lcurrent = lcurrent->next;
132 list_optarg = (char *)NULL;
134 if (lcurrent->next && legal_number(lcurrent->next->word->word, (long *)0)) {
135 lcurrent = lcurrent->next;
136 list_optarg = lcurrent->word->word;
137 lcurrent = lcurrent->next;
139 list_optarg = (char *)NULL;
143 /* No argument, just return the option. */
144 if (lcurrent->word->word[++sp] == '\0') {
146 lcurrent = lcurrent->next;
148 list_optarg = (char *)NULL;
155 * reset_internal_getopt -- force the in[ft]ernal getopt to reset
159 reset_internal_getopt ()
161 lhead = lcurrent = loptend = (WORD_LIST *)NULL;
165 #ifdef INCLUDE_UNUSED