1 /* bashgetopt.c -- `getopt' for use by the builtins. */
3 /* Copyright (C) 1992-2002 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
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #if defined (HAVE_UNISTD_H)
27 #include "../bashansi.h"
28 #include <chartypes.h>
34 #define ISOPT(s) (((*(s) == '-') || (plus && *(s) == '+')) && (s)[1])
35 #define NOTOPT(s) (((*(s) != '-') && (!plus || *(s) != '+')) || (s)[1] == '\0')
43 static WORD_LIST
*lhead
= (WORD_LIST
*)NULL
;
44 WORD_LIST
*lcurrent
= (WORD_LIST
*)NULL
;
45 WORD_LIST
*loptend
; /* Points to the first non-option argument in the list */
48 internal_getopt(list
, opts
)
54 int plus
; /* nonzero means to handle +option */
55 static char errstr
[3] = { '-', '\0', '\0' };
62 list_optarg
= (char *)NULL
;
63 loptend
= (WORD_LIST
*)NULL
; /* No non-option arguments */
67 if (list
!= lhead
|| lhead
== 0) {
68 /* Hmmm.... called with a different word list. Reset. */
70 lcurrent
= lhead
= list
;
71 loptend
= (WORD_LIST
*)NULL
;
75 if (lcurrent
== 0 || NOTOPT(lcurrent
->word
->word
)) {
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
;
86 errstr
[0] = list_opttype
= lcurrent
->word
->word
[0];
89 list_optopt
= c
= lcurrent
->word
->word
[sp
];
91 if (c
== ':' || (cp
= strchr(opts
, c
)) == NULL
) {
93 sh_invalidopt (errstr
);
94 if (lcurrent
->word
->word
[++sp
] == '\0') {
95 lcurrent
= lcurrent
->next
;
100 loptend
= lcurrent
->next
;
104 if (*++cp
== ':' || *cp
== ';') {
105 /* `:': Option requires an argument. */
106 /* `;': option argument may be missing */
107 /* We allow -l2 as equivalent to -l 2 */
108 if (lcurrent
->word
->word
[sp
+1]) {
109 list_optarg
= lcurrent
->word
->word
+ sp
+ 1;
110 lcurrent
= lcurrent
->next
;
111 /* If the specifier is `;', don't set optarg if the next
112 argument looks like another option. */
114 } else if (lcurrent
->next
&& (*cp
== ':' || lcurrent
->next
->word
->word
[0] != '-')) {
116 } else if (lcurrent
->next
&& (*cp
== ':' || NOTOPT(lcurrent
->next
->word
->word
))) {
118 lcurrent
= lcurrent
->next
;
119 list_optarg
= lcurrent
->word
->word
;
120 lcurrent
= lcurrent
->next
;
121 } else if (*cp
== ';') {
122 list_optarg
= (char *)NULL
;
123 lcurrent
= lcurrent
->next
;
124 } else { /* lcurrent->next == NULL */
128 list_optarg
= (char *)NULL
;
132 } else if (*cp
== '#') {
133 /* option requires a numeric argument */
134 if (lcurrent
->word
->word
[sp
+1]) {
135 if (DIGIT(lcurrent
->word
->word
[sp
+1])) {
136 list_optarg
= lcurrent
->word
->word
+ sp
+ 1;
137 lcurrent
= lcurrent
->next
;
139 list_optarg
= (char *)NULL
;
141 if (lcurrent
->next
&& legal_number(lcurrent
->next
->word
->word
, (intmax_t *)0)) {
142 lcurrent
= lcurrent
->next
;
143 list_optarg
= lcurrent
->word
->word
;
144 lcurrent
= lcurrent
->next
;
147 sh_neednumarg (errstr
);
149 list_optarg
= (char *)NULL
;
155 /* No argument, just return the option. */
156 if (lcurrent
->word
->word
[++sp
] == '\0') {
158 lcurrent
= lcurrent
->next
;
160 list_optarg
= (char *)NULL
;
167 * reset_internal_getopt -- force the in[ft]ernal getopt to reset
171 reset_internal_getopt ()
173 lhead
= lcurrent
= loptend
= (WORD_LIST
*)NULL
;