]> git.ipfire.org Git - thirdparty/bash.git/blob - builtins/bashgetopt.c
bash-5.2 distribution sources and documentation
[thirdparty/bash.git] / builtins / bashgetopt.c
1 /* bashgetopt.c -- `getopt' for use by the builtins. */
2
3 /* Copyright (C) 1992-2021 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
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.
11
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.
16
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/>.
19 */
20
21 #include <config.h>
22
23 #if defined (HAVE_UNISTD_H)
24 # include <unistd.h>
25 #endif
26
27 #include "../bashansi.h"
28 #include <chartypes.h>
29 #include <errno.h>
30
31 #include "../shell.h"
32 #include "common.h"
33
34 #include "bashgetopt.h"
35
36 #define ISOPT(s) (((*(s) == '-') || (plus && *(s) == '+')) && (s)[1])
37 #define NOTOPT(s) (((*(s) != '-') && (!plus || *(s) != '+')) || (s)[1] == '\0')
38
39 static int sp;
40
41 char *list_optarg;
42 int list_optflags;
43 int list_optopt;
44 int list_opttype;
45
46 static WORD_LIST *lhead = (WORD_LIST *)NULL;
47 WORD_LIST *lcurrent = (WORD_LIST *)NULL;
48 WORD_LIST *loptend; /* Points to the first non-option argument in the list */
49
50 int
51 internal_getopt(list, opts)
52 WORD_LIST *list;
53 char *opts;
54 {
55 register int c;
56 register char *cp;
57 int plus; /* nonzero means to handle +option */
58 static char errstr[3] = { '-', '\0', '\0' };
59
60 plus = *opts == '+';
61 if (plus)
62 opts++;
63
64 if (list == 0) {
65 list_optarg = (char *)NULL;
66 list_optflags = 0;
67 loptend = (WORD_LIST *)NULL; /* No non-option arguments */
68 return -1;
69 }
70
71 if (list != lhead || lhead == 0) {
72 /* Hmmm.... called with a different word list. Reset. */
73 sp = 1;
74 lcurrent = lhead = list;
75 loptend = (WORD_LIST *)NULL;
76 }
77
78 if (sp == 1) {
79 if (lcurrent == 0 || NOTOPT(lcurrent->word->word)) {
80 lhead = (WORD_LIST *)NULL;
81 loptend = lcurrent;
82 return(-1);
83 } else if (ISHELP (lcurrent->word->word)) {
84 lhead = (WORD_LIST *)NULL;
85 loptend = lcurrent;
86 return (GETOPT_HELP);
87 } else if (lcurrent->word->word[0] == '-' &&
88 lcurrent->word->word[1] == '-' &&
89 lcurrent->word->word[2] == 0) {
90 lhead = (WORD_LIST *)NULL;
91 loptend = lcurrent->next;
92 return(-1);
93 }
94 errstr[0] = list_opttype = lcurrent->word->word[0];
95 }
96
97 list_optopt = c = lcurrent->word->word[sp];
98
99 if (c == ':' || (cp = strchr(opts, c)) == NULL) {
100 errstr[1] = c;
101 sh_invalidopt (errstr);
102 if (lcurrent->word->word[++sp] == '\0') {
103 lcurrent = lcurrent->next;
104 sp = 1;
105 }
106 list_optarg = NULL;
107 list_optflags = 0;
108 if (lcurrent)
109 loptend = lcurrent->next;
110 return('?');
111 }
112
113 if (*++cp == ':' || *cp == ';') {
114 /* `:': Option requires an argument. */
115 /* `;': option argument may be missing */
116 /* We allow -l2 as equivalent to -l 2 */
117 if (lcurrent->word->word[sp+1]) {
118 list_optarg = lcurrent->word->word + sp + 1;
119 list_optflags = 0;
120 lcurrent = lcurrent->next;
121 /* If the specifier is `;', don't set optarg if the next
122 argument looks like another option. */
123 #if 0
124 } else if (lcurrent->next && (*cp == ':' || lcurrent->next->word->word[0] != '-')) {
125 #else
126 } else if (lcurrent->next && (*cp == ':' || NOTOPT(lcurrent->next->word->word))) {
127 #endif
128 lcurrent = lcurrent->next;
129 list_optarg = lcurrent->word->word;
130 list_optflags = lcurrent->word->flags;
131 lcurrent = lcurrent->next;
132 } else if (*cp == ';') {
133 list_optarg = (char *)NULL;
134 list_optflags = 0;
135 lcurrent = lcurrent->next;
136 } else { /* lcurrent->next == NULL */
137 errstr[1] = c;
138 sh_needarg (errstr);
139 sp = 1;
140 list_optarg = (char *)NULL;
141 list_optflags = 0;
142 return('?');
143 }
144 sp = 1;
145 } else if (*cp == '#') {
146 /* option requires a numeric argument */
147 if (lcurrent->word->word[sp+1]) {
148 if (DIGIT(lcurrent->word->word[sp+1])) {
149 list_optarg = lcurrent->word->word + sp + 1;
150 list_optflags = 0;
151 lcurrent = lcurrent->next;
152 } else {
153 list_optarg = (char *)NULL;
154 list_optflags = 0;
155 }
156 } else {
157 if (lcurrent->next && legal_number(lcurrent->next->word->word, (intmax_t *)0)) {
158 lcurrent = lcurrent->next;
159 list_optarg = lcurrent->word->word;
160 list_optflags = lcurrent->word->flags;
161 lcurrent = lcurrent->next;
162 } else {
163 errstr[1] = c;
164 sh_neednumarg (errstr);
165 sp = 1;
166 list_optarg = (char *)NULL;
167 list_optflags = 0;
168 return ('?');
169 }
170 }
171
172 } else {
173 /* No argument, just return the option. */
174 if (lcurrent->word->word[++sp] == '\0') {
175 sp = 1;
176 lcurrent = lcurrent->next;
177 }
178 list_optarg = (char *)NULL;
179 list_optflags = 0;
180 }
181
182 return(c);
183 }
184
185 /*
186 * reset_internal_getopt -- force the in[ft]ernal getopt to reset
187 */
188
189 void
190 reset_internal_getopt ()
191 {
192 lhead = lcurrent = loptend = (WORD_LIST *)NULL;
193 sp = 1;
194 }