annotate lib/getopt.c @ 276:23034db7dd8a 2.5

fixed expression 'slots' to not store null on a pass 1 store
author lost
date Mon, 31 Aug 2009 08:39:36 +0000
parents d5392bb5da3c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
272
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1 /* Getopt for GNU.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
2 NOTE: getopt is now part of the C library, so if you don't know what
d5392bb5da3c Added generated files
lost
parents:
diff changeset
3 "Keep this file name-space clean" means, talk to drepper@gnu.org
d5392bb5da3c Added generated files
lost
parents:
diff changeset
4 before changing it!
d5392bb5da3c Added generated files
lost
parents:
diff changeset
5 Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004,2006,2008
d5392bb5da3c Added generated files
lost
parents:
diff changeset
6 Free Software Foundation, Inc.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
7 This file is part of the GNU C Library.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
8
d5392bb5da3c Added generated files
lost
parents:
diff changeset
9 This program is free software: you can redistribute it and/or modify
d5392bb5da3c Added generated files
lost
parents:
diff changeset
10 it under the terms of the GNU General Public License as published by
d5392bb5da3c Added generated files
lost
parents:
diff changeset
11 the Free Software Foundation; either version 3 of the License, or
d5392bb5da3c Added generated files
lost
parents:
diff changeset
12 (at your option) any later version.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
13
d5392bb5da3c Added generated files
lost
parents:
diff changeset
14 This program is distributed in the hope that it will be useful,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
d5392bb5da3c Added generated files
lost
parents:
diff changeset
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
17 GNU General Public License for more details.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
18
d5392bb5da3c Added generated files
lost
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
d5392bb5da3c Added generated files
lost
parents:
diff changeset
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
21
d5392bb5da3c Added generated files
lost
parents:
diff changeset
22 #ifndef _LIBC
d5392bb5da3c Added generated files
lost
parents:
diff changeset
23 # include <config.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
24 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
25
d5392bb5da3c Added generated files
lost
parents:
diff changeset
26 #include "getopt.h"
d5392bb5da3c Added generated files
lost
parents:
diff changeset
27
d5392bb5da3c Added generated files
lost
parents:
diff changeset
28 #include <stdio.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
29 #include <stdlib.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
30 #include <string.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
31 #include <unistd.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
32
d5392bb5da3c Added generated files
lost
parents:
diff changeset
33 #ifdef _LIBC
d5392bb5da3c Added generated files
lost
parents:
diff changeset
34 # include <libintl.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
35 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
36 # include "gettext.h"
d5392bb5da3c Added generated files
lost
parents:
diff changeset
37 # define _(msgid) gettext (msgid)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
38 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
39
d5392bb5da3c Added generated files
lost
parents:
diff changeset
40 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
41 # include <wchar.h>
d5392bb5da3c Added generated files
lost
parents:
diff changeset
42 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
43
d5392bb5da3c Added generated files
lost
parents:
diff changeset
44 #ifndef attribute_hidden
d5392bb5da3c Added generated files
lost
parents:
diff changeset
45 # define attribute_hidden
d5392bb5da3c Added generated files
lost
parents:
diff changeset
46 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
47
d5392bb5da3c Added generated files
lost
parents:
diff changeset
48 /* Unlike standard Unix `getopt', functions like `getopt_long'
d5392bb5da3c Added generated files
lost
parents:
diff changeset
49 let the user intersperse the options with the other arguments.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
50
d5392bb5da3c Added generated files
lost
parents:
diff changeset
51 As `getopt_long' works, it permutes the elements of ARGV so that,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
52 when it is done, all the options precede everything else. Thus
d5392bb5da3c Added generated files
lost
parents:
diff changeset
53 all application programs are extended to handle flexible argument order.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
54
d5392bb5da3c Added generated files
lost
parents:
diff changeset
55 Using `getopt' or setting the environment variable POSIXLY_CORRECT
d5392bb5da3c Added generated files
lost
parents:
diff changeset
56 disables permutation.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
57 Then the application's behavior is completely standard.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
58
d5392bb5da3c Added generated files
lost
parents:
diff changeset
59 GNU application programs can use a third alternative mode in which
d5392bb5da3c Added generated files
lost
parents:
diff changeset
60 they can distinguish the relative order of options and other arguments. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
61
d5392bb5da3c Added generated files
lost
parents:
diff changeset
62 #include "getopt_int.h"
d5392bb5da3c Added generated files
lost
parents:
diff changeset
63
d5392bb5da3c Added generated files
lost
parents:
diff changeset
64 /* For communication from `getopt' to the caller.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
65 When `getopt' finds an option that takes an argument,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
66 the argument value is returned here.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
67 Also, when `ordering' is RETURN_IN_ORDER,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
68 each non-option ARGV-element is returned here. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
69
d5392bb5da3c Added generated files
lost
parents:
diff changeset
70 char *optarg;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
71
d5392bb5da3c Added generated files
lost
parents:
diff changeset
72 /* Index in ARGV of the next element to be scanned.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
73 This is used for communication to and from the caller
d5392bb5da3c Added generated files
lost
parents:
diff changeset
74 and for communication between successive calls to `getopt'.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
75
d5392bb5da3c Added generated files
lost
parents:
diff changeset
76 On entry to `getopt', zero means this is the first call; initialize.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
77
d5392bb5da3c Added generated files
lost
parents:
diff changeset
78 When `getopt' returns -1, this is the index of the first of the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
79 non-option elements that the caller should itself scan.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
80
d5392bb5da3c Added generated files
lost
parents:
diff changeset
81 Otherwise, `optind' communicates from one call to the next
d5392bb5da3c Added generated files
lost
parents:
diff changeset
82 how much of ARGV has been scanned so far. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
83
d5392bb5da3c Added generated files
lost
parents:
diff changeset
84 /* 1003.2 says this must be 1 before any call. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
85 int optind = 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
86
d5392bb5da3c Added generated files
lost
parents:
diff changeset
87 /* Callers store zero here to inhibit the error message
d5392bb5da3c Added generated files
lost
parents:
diff changeset
88 for unrecognized options. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
89
d5392bb5da3c Added generated files
lost
parents:
diff changeset
90 int opterr = 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
91
d5392bb5da3c Added generated files
lost
parents:
diff changeset
92 /* Set to an option character which was unrecognized.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
93 This must be initialized on some systems to avoid linking in the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
94 system's own getopt implementation. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
95
d5392bb5da3c Added generated files
lost
parents:
diff changeset
96 int optopt = '?';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
97
d5392bb5da3c Added generated files
lost
parents:
diff changeset
98 /* Keep a global copy of all internal members of getopt_data. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
99
d5392bb5da3c Added generated files
lost
parents:
diff changeset
100 static struct _getopt_data getopt_data;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
101
d5392bb5da3c Added generated files
lost
parents:
diff changeset
102
d5392bb5da3c Added generated files
lost
parents:
diff changeset
103 #if defined HAVE_DECL_GETENV && !HAVE_DECL_GETENV
d5392bb5da3c Added generated files
lost
parents:
diff changeset
104 extern char *getenv ();
d5392bb5da3c Added generated files
lost
parents:
diff changeset
105 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
106
d5392bb5da3c Added generated files
lost
parents:
diff changeset
107 #ifdef _LIBC
d5392bb5da3c Added generated files
lost
parents:
diff changeset
108 /* Stored original parameters.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
109 XXX This is no good solution. We should rather copy the args so
d5392bb5da3c Added generated files
lost
parents:
diff changeset
110 that we can compare them later. But we must not use malloc(3). */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
111 extern int __libc_argc;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
112 extern char **__libc_argv;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
113
d5392bb5da3c Added generated files
lost
parents:
diff changeset
114 /* Bash 2.0 gives us an environment variable containing flags
d5392bb5da3c Added generated files
lost
parents:
diff changeset
115 indicating ARGV elements that should not be considered arguments. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
116
d5392bb5da3c Added generated files
lost
parents:
diff changeset
117 # ifdef USE_NONOPTION_FLAGS
d5392bb5da3c Added generated files
lost
parents:
diff changeset
118 /* Defined in getopt_init.c */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
119 extern char *__getopt_nonoption_flags;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
120 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
121
d5392bb5da3c Added generated files
lost
parents:
diff changeset
122 # ifdef USE_NONOPTION_FLAGS
d5392bb5da3c Added generated files
lost
parents:
diff changeset
123 # define SWAP_FLAGS(ch1, ch2) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
124 if (d->__nonoption_flags_len > 0) \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
125 { \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
126 char __tmp = __getopt_nonoption_flags[ch1]; \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
127 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
128 __getopt_nonoption_flags[ch2] = __tmp; \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
129 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
130 # else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
131 # define SWAP_FLAGS(ch1, ch2)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
132 # endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
133 #else /* !_LIBC */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
134 # define SWAP_FLAGS(ch1, ch2)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
135 #endif /* _LIBC */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
136
d5392bb5da3c Added generated files
lost
parents:
diff changeset
137 /* Exchange two adjacent subsequences of ARGV.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
138 One subsequence is elements [first_nonopt,last_nonopt)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
139 which contains all the non-options that have been skipped so far.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
140 The other is elements [last_nonopt,optind), which contains all
d5392bb5da3c Added generated files
lost
parents:
diff changeset
141 the options processed since those non-options were skipped.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
142
d5392bb5da3c Added generated files
lost
parents:
diff changeset
143 `first_nonopt' and `last_nonopt' are relocated so that they describe
d5392bb5da3c Added generated files
lost
parents:
diff changeset
144 the new indices of the non-options in ARGV after they are moved. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
145
d5392bb5da3c Added generated files
lost
parents:
diff changeset
146 static void
d5392bb5da3c Added generated files
lost
parents:
diff changeset
147 exchange (char **argv, struct _getopt_data *d)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
148 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
149 int bottom = d->__first_nonopt;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
150 int middle = d->__last_nonopt;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
151 int top = d->optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
152 char *tem;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
153
d5392bb5da3c Added generated files
lost
parents:
diff changeset
154 /* Exchange the shorter segment with the far end of the longer segment.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
155 That puts the shorter segment into the right place.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
156 It leaves the longer segment in the right place overall,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
157 but it consists of two parts that need to be swapped next. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
158
d5392bb5da3c Added generated files
lost
parents:
diff changeset
159 #if defined _LIBC && defined USE_NONOPTION_FLAGS
d5392bb5da3c Added generated files
lost
parents:
diff changeset
160 /* First make sure the handling of the `__getopt_nonoption_flags'
d5392bb5da3c Added generated files
lost
parents:
diff changeset
161 string can work normally. Our top argument must be in the range
d5392bb5da3c Added generated files
lost
parents:
diff changeset
162 of the string. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
163 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
164 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
165 /* We must extend the array. The user plays games with us and
d5392bb5da3c Added generated files
lost
parents:
diff changeset
166 presents new arguments. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
167 char *new_str = malloc (top + 1);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
168 if (new_str == NULL)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
169 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
170 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
171 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
172 memset (__mempcpy (new_str, __getopt_nonoption_flags,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
173 d->__nonoption_flags_max_len),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
174 '\0', top + 1 - d->__nonoption_flags_max_len);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
175 d->__nonoption_flags_max_len = top + 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
176 __getopt_nonoption_flags = new_str;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
177 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
178 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
179 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
180
d5392bb5da3c Added generated files
lost
parents:
diff changeset
181 while (top > middle && middle > bottom)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
182 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
183 if (top - middle > middle - bottom)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
184 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
185 /* Bottom segment is the short one. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
186 int len = middle - bottom;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
187 register int i;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
188
d5392bb5da3c Added generated files
lost
parents:
diff changeset
189 /* Swap it with the top part of the top segment. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
190 for (i = 0; i < len; i++)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
191 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
192 tem = argv[bottom + i];
d5392bb5da3c Added generated files
lost
parents:
diff changeset
193 argv[bottom + i] = argv[top - (middle - bottom) + i];
d5392bb5da3c Added generated files
lost
parents:
diff changeset
194 argv[top - (middle - bottom) + i] = tem;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
195 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
196 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
197 /* Exclude the moved bottom segment from further swapping. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
198 top -= len;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
199 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
200 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
201 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
202 /* Top segment is the short one. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
203 int len = top - middle;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
204 register int i;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
205
d5392bb5da3c Added generated files
lost
parents:
diff changeset
206 /* Swap it with the bottom part of the bottom segment. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
207 for (i = 0; i < len; i++)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
208 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
209 tem = argv[bottom + i];
d5392bb5da3c Added generated files
lost
parents:
diff changeset
210 argv[bottom + i] = argv[middle + i];
d5392bb5da3c Added generated files
lost
parents:
diff changeset
211 argv[middle + i] = tem;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
212 SWAP_FLAGS (bottom + i, middle + i);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
213 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
214 /* Exclude the moved top segment from further swapping. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
215 bottom += len;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
216 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
217 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
218
d5392bb5da3c Added generated files
lost
parents:
diff changeset
219 /* Update records for the slots the non-options now occupy. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
220
d5392bb5da3c Added generated files
lost
parents:
diff changeset
221 d->__first_nonopt += (d->optind - d->__last_nonopt);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
222 d->__last_nonopt = d->optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
223 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
224
d5392bb5da3c Added generated files
lost
parents:
diff changeset
225 /* Initialize the internal data when the first call is made. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
226
d5392bb5da3c Added generated files
lost
parents:
diff changeset
227 static const char *
d5392bb5da3c Added generated files
lost
parents:
diff changeset
228 _getopt_initialize (int argc, char **argv, const char *optstring,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
229 int posixly_correct, struct _getopt_data *d)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
230 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
231 /* Start processing options with ARGV-element 1 (since ARGV-element 0
d5392bb5da3c Added generated files
lost
parents:
diff changeset
232 is the program name); the sequence of previously skipped
d5392bb5da3c Added generated files
lost
parents:
diff changeset
233 non-option ARGV-elements is empty. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
234
d5392bb5da3c Added generated files
lost
parents:
diff changeset
235 d->__first_nonopt = d->__last_nonopt = d->optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
236
d5392bb5da3c Added generated files
lost
parents:
diff changeset
237 d->__nextchar = NULL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
238
d5392bb5da3c Added generated files
lost
parents:
diff changeset
239 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
d5392bb5da3c Added generated files
lost
parents:
diff changeset
240
d5392bb5da3c Added generated files
lost
parents:
diff changeset
241 /* Determine how to handle the ordering of options and nonoptions. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
242
d5392bb5da3c Added generated files
lost
parents:
diff changeset
243 if (optstring[0] == '-')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
244 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
245 d->__ordering = RETURN_IN_ORDER;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
246 ++optstring;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
247 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
248 else if (optstring[0] == '+')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
249 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
250 d->__ordering = REQUIRE_ORDER;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
251 ++optstring;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
252 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
253 else if (d->__posixly_correct)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
254 d->__ordering = REQUIRE_ORDER;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
255 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
256 d->__ordering = PERMUTE;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
257
d5392bb5da3c Added generated files
lost
parents:
diff changeset
258 #if defined _LIBC && defined USE_NONOPTION_FLAGS
d5392bb5da3c Added generated files
lost
parents:
diff changeset
259 if (!d->__posixly_correct
d5392bb5da3c Added generated files
lost
parents:
diff changeset
260 && argc == __libc_argc && argv == __libc_argv)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
261 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
262 if (d->__nonoption_flags_max_len == 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
263 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
264 if (__getopt_nonoption_flags == NULL
d5392bb5da3c Added generated files
lost
parents:
diff changeset
265 || __getopt_nonoption_flags[0] == '\0')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
266 d->__nonoption_flags_max_len = -1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
267 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
268 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
269 const char *orig_str = __getopt_nonoption_flags;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
270 int len = d->__nonoption_flags_max_len = strlen (orig_str);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
271 if (d->__nonoption_flags_max_len < argc)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
272 d->__nonoption_flags_max_len = argc;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
273 __getopt_nonoption_flags =
d5392bb5da3c Added generated files
lost
parents:
diff changeset
274 (char *) malloc (d->__nonoption_flags_max_len);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
275 if (__getopt_nonoption_flags == NULL)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
276 d->__nonoption_flags_max_len = -1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
277 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
278 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
279 '\0', d->__nonoption_flags_max_len - len);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
280 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
281 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
282 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
283 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
284 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
285 d->__nonoption_flags_len = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
286 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
287
d5392bb5da3c Added generated files
lost
parents:
diff changeset
288 return optstring;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
289 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
290
d5392bb5da3c Added generated files
lost
parents:
diff changeset
291 /* Scan elements of ARGV (whose length is ARGC) for option characters
d5392bb5da3c Added generated files
lost
parents:
diff changeset
292 given in OPTSTRING.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
293
d5392bb5da3c Added generated files
lost
parents:
diff changeset
294 If an element of ARGV starts with '-', and is not exactly "-" or "--",
d5392bb5da3c Added generated files
lost
parents:
diff changeset
295 then it is an option element. The characters of this element
d5392bb5da3c Added generated files
lost
parents:
diff changeset
296 (aside from the initial '-') are option characters. If `getopt'
d5392bb5da3c Added generated files
lost
parents:
diff changeset
297 is called repeatedly, it returns successively each of the option characters
d5392bb5da3c Added generated files
lost
parents:
diff changeset
298 from each of the option elements.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
299
d5392bb5da3c Added generated files
lost
parents:
diff changeset
300 If `getopt' finds another option character, it returns that character,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
301 updating `optind' and `nextchar' so that the next call to `getopt' can
d5392bb5da3c Added generated files
lost
parents:
diff changeset
302 resume the scan with the following option character or ARGV-element.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
303
d5392bb5da3c Added generated files
lost
parents:
diff changeset
304 If there are no more option characters, `getopt' returns -1.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
305 Then `optind' is the index in ARGV of the first ARGV-element
d5392bb5da3c Added generated files
lost
parents:
diff changeset
306 that is not an option. (The ARGV-elements have been permuted
d5392bb5da3c Added generated files
lost
parents:
diff changeset
307 so that those that are not options now come last.)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
308
d5392bb5da3c Added generated files
lost
parents:
diff changeset
309 OPTSTRING is a string containing the legitimate option characters.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
310 If an option character is seen that is not listed in OPTSTRING,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
311 return '?' after printing an error message. If you set `opterr' to
d5392bb5da3c Added generated files
lost
parents:
diff changeset
312 zero, the error message is suppressed but we still return '?'.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
313
d5392bb5da3c Added generated files
lost
parents:
diff changeset
314 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
315 so the following text in the same ARGV-element, or the text of the following
d5392bb5da3c Added generated files
lost
parents:
diff changeset
316 ARGV-element, is returned in `optarg'. Two colons mean an option that
d5392bb5da3c Added generated files
lost
parents:
diff changeset
317 wants an optional arg; if there is text in the current ARGV-element,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
318 it is returned in `optarg', otherwise `optarg' is set to zero.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
319
d5392bb5da3c Added generated files
lost
parents:
diff changeset
320 If OPTSTRING starts with `-' or `+', it requests different methods of
d5392bb5da3c Added generated files
lost
parents:
diff changeset
321 handling the non-option ARGV-elements.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
322 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
323
d5392bb5da3c Added generated files
lost
parents:
diff changeset
324 Long-named options begin with `--' instead of `-'.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
325 Their names may be abbreviated as long as the abbreviation is unique
d5392bb5da3c Added generated files
lost
parents:
diff changeset
326 or is an exact match for some defined option. If they have an
d5392bb5da3c Added generated files
lost
parents:
diff changeset
327 argument, it follows the option name in the same ARGV-element, separated
d5392bb5da3c Added generated files
lost
parents:
diff changeset
328 from the option name by a `=', or else the in next ARGV-element.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
329 When `getopt' finds a long-named option, it returns 0 if that option's
d5392bb5da3c Added generated files
lost
parents:
diff changeset
330 `flag' field is nonzero, the value of the option's `val' field
d5392bb5da3c Added generated files
lost
parents:
diff changeset
331 if the `flag' field is zero.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
332
d5392bb5da3c Added generated files
lost
parents:
diff changeset
333 LONGOPTS is a vector of `struct option' terminated by an
d5392bb5da3c Added generated files
lost
parents:
diff changeset
334 element containing a name which is zero.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
335
d5392bb5da3c Added generated files
lost
parents:
diff changeset
336 LONGIND returns the index in LONGOPT of the long-named option found.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
337 It is only valid when a long-named option has been found by the most
d5392bb5da3c Added generated files
lost
parents:
diff changeset
338 recent call.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
339
d5392bb5da3c Added generated files
lost
parents:
diff changeset
340 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
d5392bb5da3c Added generated files
lost
parents:
diff changeset
341 long-named options.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
342
d5392bb5da3c Added generated files
lost
parents:
diff changeset
343 If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
d5392bb5da3c Added generated files
lost
parents:
diff changeset
344 environment variable were set. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
345
d5392bb5da3c Added generated files
lost
parents:
diff changeset
346 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
347 _getopt_internal_r (int argc, char **argv, const char *optstring,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
348 const struct option *longopts, int *longind,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
349 int long_only, int posixly_correct, struct _getopt_data *d)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
350 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
351 int print_errors = d->opterr;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
352 if (optstring[0] == ':')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
353 print_errors = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
354
d5392bb5da3c Added generated files
lost
parents:
diff changeset
355 if (argc < 1)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
356 return -1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
357
d5392bb5da3c Added generated files
lost
parents:
diff changeset
358 d->optarg = NULL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
359
d5392bb5da3c Added generated files
lost
parents:
diff changeset
360 if (d->optind == 0 || !d->__initialized)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
361 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
362 if (d->optind == 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
363 d->optind = 1; /* Don't scan ARGV[0], the program name. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
364 optstring = _getopt_initialize (argc, argv, optstring,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
365 posixly_correct, d);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
366 d->__initialized = 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
367 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
368
d5392bb5da3c Added generated files
lost
parents:
diff changeset
369 /* Test whether ARGV[optind] points to a non-option argument.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
370 Either it does not have option syntax, or there is an environment flag
d5392bb5da3c Added generated files
lost
parents:
diff changeset
371 from the shell indicating it is not an option. The later information
d5392bb5da3c Added generated files
lost
parents:
diff changeset
372 is only used when the used in the GNU libc. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
373 #if defined _LIBC && defined USE_NONOPTION_FLAGS
d5392bb5da3c Added generated files
lost
parents:
diff changeset
374 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
375 || (d->optind < d->__nonoption_flags_len \
d5392bb5da3c Added generated files
lost
parents:
diff changeset
376 && __getopt_nonoption_flags[d->optind] == '1'))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
377 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
378 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
379 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
380
d5392bb5da3c Added generated files
lost
parents:
diff changeset
381 if (d->__nextchar == NULL || *d->__nextchar == '\0')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
382 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
383 /* Advance to the next ARGV-element. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
384
d5392bb5da3c Added generated files
lost
parents:
diff changeset
385 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
d5392bb5da3c Added generated files
lost
parents:
diff changeset
386 moved back by the user (who may also have changed the arguments). */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
387 if (d->__last_nonopt > d->optind)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
388 d->__last_nonopt = d->optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
389 if (d->__first_nonopt > d->optind)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
390 d->__first_nonopt = d->optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
391
d5392bb5da3c Added generated files
lost
parents:
diff changeset
392 if (d->__ordering == PERMUTE)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
393 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
394 /* If we have just processed some options following some non-options,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
395 exchange them so that the options come first. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
396
d5392bb5da3c Added generated files
lost
parents:
diff changeset
397 if (d->__first_nonopt != d->__last_nonopt
d5392bb5da3c Added generated files
lost
parents:
diff changeset
398 && d->__last_nonopt != d->optind)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
399 exchange ((char **) argv, d);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
400 else if (d->__last_nonopt != d->optind)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
401 d->__first_nonopt = d->optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
402
d5392bb5da3c Added generated files
lost
parents:
diff changeset
403 /* Skip any additional non-options
d5392bb5da3c Added generated files
lost
parents:
diff changeset
404 and extend the range of non-options previously skipped. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
405
d5392bb5da3c Added generated files
lost
parents:
diff changeset
406 while (d->optind < argc && NONOPTION_P)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
407 d->optind++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
408 d->__last_nonopt = d->optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
409 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
410
d5392bb5da3c Added generated files
lost
parents:
diff changeset
411 /* The special ARGV-element `--' means premature end of options.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
412 Skip it like a null option,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
413 then exchange with previous non-options as if it were an option,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
414 then skip everything else like a non-option. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
415
d5392bb5da3c Added generated files
lost
parents:
diff changeset
416 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
417 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
418 d->optind++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
419
d5392bb5da3c Added generated files
lost
parents:
diff changeset
420 if (d->__first_nonopt != d->__last_nonopt
d5392bb5da3c Added generated files
lost
parents:
diff changeset
421 && d->__last_nonopt != d->optind)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
422 exchange ((char **) argv, d);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
423 else if (d->__first_nonopt == d->__last_nonopt)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
424 d->__first_nonopt = d->optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
425 d->__last_nonopt = argc;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
426
d5392bb5da3c Added generated files
lost
parents:
diff changeset
427 d->optind = argc;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
428 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
429
d5392bb5da3c Added generated files
lost
parents:
diff changeset
430 /* If we have done all the ARGV-elements, stop the scan
d5392bb5da3c Added generated files
lost
parents:
diff changeset
431 and back over any non-options that we skipped and permuted. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
432
d5392bb5da3c Added generated files
lost
parents:
diff changeset
433 if (d->optind == argc)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
434 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
435 /* Set the next-arg-index to point at the non-options
d5392bb5da3c Added generated files
lost
parents:
diff changeset
436 that we previously skipped, so the caller will digest them. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
437 if (d->__first_nonopt != d->__last_nonopt)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
438 d->optind = d->__first_nonopt;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
439 return -1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
440 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
441
d5392bb5da3c Added generated files
lost
parents:
diff changeset
442 /* If we have come to a non-option and did not permute it,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
443 either stop the scan or describe it to the caller and pass it by. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
444
d5392bb5da3c Added generated files
lost
parents:
diff changeset
445 if (NONOPTION_P)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
446 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
447 if (d->__ordering == REQUIRE_ORDER)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
448 return -1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
449 d->optarg = argv[d->optind++];
d5392bb5da3c Added generated files
lost
parents:
diff changeset
450 return 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
451 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
452
d5392bb5da3c Added generated files
lost
parents:
diff changeset
453 /* We have found another option-ARGV-element.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
454 Skip the initial punctuation. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
455
d5392bb5da3c Added generated files
lost
parents:
diff changeset
456 d->__nextchar = (argv[d->optind] + 1
d5392bb5da3c Added generated files
lost
parents:
diff changeset
457 + (longopts != NULL && argv[d->optind][1] == '-'));
d5392bb5da3c Added generated files
lost
parents:
diff changeset
458 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
459
d5392bb5da3c Added generated files
lost
parents:
diff changeset
460 /* Decode the current option-ARGV-element. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
461
d5392bb5da3c Added generated files
lost
parents:
diff changeset
462 /* Check whether the ARGV-element is a long option.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
463
d5392bb5da3c Added generated files
lost
parents:
diff changeset
464 If long_only and the ARGV-element has the form "-f", where f is
d5392bb5da3c Added generated files
lost
parents:
diff changeset
465 a valid short option, don't consider it an abbreviated form of
d5392bb5da3c Added generated files
lost
parents:
diff changeset
466 a long option that starts with f. Otherwise there would be no
d5392bb5da3c Added generated files
lost
parents:
diff changeset
467 way to give the -f short option.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
468
d5392bb5da3c Added generated files
lost
parents:
diff changeset
469 On the other hand, if there's a long option "fubar" and
d5392bb5da3c Added generated files
lost
parents:
diff changeset
470 the ARGV-element is "-fu", do consider that an abbreviation of
d5392bb5da3c Added generated files
lost
parents:
diff changeset
471 the long option, just like "--fu", and not "-f" with arg "u".
d5392bb5da3c Added generated files
lost
parents:
diff changeset
472
d5392bb5da3c Added generated files
lost
parents:
diff changeset
473 This distinction seems to be the most useful approach. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
474
d5392bb5da3c Added generated files
lost
parents:
diff changeset
475 if (longopts != NULL
d5392bb5da3c Added generated files
lost
parents:
diff changeset
476 && (argv[d->optind][1] == '-'
d5392bb5da3c Added generated files
lost
parents:
diff changeset
477 || (long_only && (argv[d->optind][2]
d5392bb5da3c Added generated files
lost
parents:
diff changeset
478 || !strchr (optstring, argv[d->optind][1])))))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
479 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
480 char *nameend;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
481 const struct option *p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
482 const struct option *pfound = NULL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
483 int exact = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
484 int ambig = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
485 int indfound = -1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
486 int option_index;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
487
d5392bb5da3c Added generated files
lost
parents:
diff changeset
488 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
489 /* Do nothing. */ ;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
490
d5392bb5da3c Added generated files
lost
parents:
diff changeset
491 /* Test all long options for either exact match
d5392bb5da3c Added generated files
lost
parents:
diff changeset
492 or abbreviated matches. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
493 for (p = longopts, option_index = 0; p->name; p++, option_index++)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
494 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
495 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
496 if ((unsigned int) (nameend - d->__nextchar)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
497 == (unsigned int) strlen (p->name))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
498 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
499 /* Exact match found. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
500 pfound = p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
501 indfound = option_index;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
502 exact = 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
503 break;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
504 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
505 else if (pfound == NULL)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
506 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
507 /* First nonexact match found. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
508 pfound = p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
509 indfound = option_index;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
510 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
511 else if (long_only
d5392bb5da3c Added generated files
lost
parents:
diff changeset
512 || pfound->has_arg != p->has_arg
d5392bb5da3c Added generated files
lost
parents:
diff changeset
513 || pfound->flag != p->flag
d5392bb5da3c Added generated files
lost
parents:
diff changeset
514 || pfound->val != p->val)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
515 /* Second or later nonexact match found. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
516 ambig = 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
517 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
518
d5392bb5da3c Added generated files
lost
parents:
diff changeset
519 if (ambig && !exact)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
520 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
521 if (print_errors)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
522 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
523 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
524 char *buf;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
525
d5392bb5da3c Added generated files
lost
parents:
diff changeset
526 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
527 argv[0], argv[d->optind]) >= 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
528 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
529 _IO_flockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
530
d5392bb5da3c Added generated files
lost
parents:
diff changeset
531 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
532 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
533
d5392bb5da3c Added generated files
lost
parents:
diff changeset
534 __fxprintf (NULL, "%s", buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
535
d5392bb5da3c Added generated files
lost
parents:
diff changeset
536 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
537 _IO_funlockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
538
d5392bb5da3c Added generated files
lost
parents:
diff changeset
539 free (buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
540 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
541 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
542 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
543 argv[0], argv[d->optind]);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
544 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
545 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
546 d->__nextchar += strlen (d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
547 d->optind++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
548 d->optopt = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
549 return '?';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
550 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
551
d5392bb5da3c Added generated files
lost
parents:
diff changeset
552 if (pfound != NULL)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
553 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
554 option_index = indfound;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
555 d->optind++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
556 if (*nameend)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
557 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
558 /* Don't test has_arg with >, because some C compilers don't
d5392bb5da3c Added generated files
lost
parents:
diff changeset
559 allow it to be used on enums. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
560 if (pfound->has_arg)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
561 d->optarg = nameend + 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
562 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
563 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
564 if (print_errors)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
565 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
566 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
567 char *buf;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
568 int n;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
569 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
570
d5392bb5da3c Added generated files
lost
parents:
diff changeset
571 if (argv[d->optind - 1][1] == '-')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
572 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
573 /* --option */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
574 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
575 n = __asprintf (&buf, _("\
d5392bb5da3c Added generated files
lost
parents:
diff changeset
576 %s: option `--%s' doesn't allow an argument\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
577 argv[0], pfound->name);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
578 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
579 fprintf (stderr, _("\
d5392bb5da3c Added generated files
lost
parents:
diff changeset
580 %s: option `--%s' doesn't allow an argument\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
581 argv[0], pfound->name);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
582 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
583 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
584 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
585 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
586 /* +option or -option */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
587 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
588 n = __asprintf (&buf, _("\
d5392bb5da3c Added generated files
lost
parents:
diff changeset
589 %s: option `%c%s' doesn't allow an argument\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
590 argv[0], argv[d->optind - 1][0],
d5392bb5da3c Added generated files
lost
parents:
diff changeset
591 pfound->name);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
592 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
593 fprintf (stderr, _("\
d5392bb5da3c Added generated files
lost
parents:
diff changeset
594 %s: option `%c%s' doesn't allow an argument\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
595 argv[0], argv[d->optind - 1][0],
d5392bb5da3c Added generated files
lost
parents:
diff changeset
596 pfound->name);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
597 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
598 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
599
d5392bb5da3c Added generated files
lost
parents:
diff changeset
600 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
601 if (n >= 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
602 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
603 _IO_flockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
604
d5392bb5da3c Added generated files
lost
parents:
diff changeset
605 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
606 ((_IO_FILE *) stderr)->_flags2
d5392bb5da3c Added generated files
lost
parents:
diff changeset
607 |= _IO_FLAGS2_NOTCANCEL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
608
d5392bb5da3c Added generated files
lost
parents:
diff changeset
609 __fxprintf (NULL, "%s", buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
610
d5392bb5da3c Added generated files
lost
parents:
diff changeset
611 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
612 _IO_funlockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
613
d5392bb5da3c Added generated files
lost
parents:
diff changeset
614 free (buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
615 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
616 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
617 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
618
d5392bb5da3c Added generated files
lost
parents:
diff changeset
619 d->__nextchar += strlen (d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
620
d5392bb5da3c Added generated files
lost
parents:
diff changeset
621 d->optopt = pfound->val;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
622 return '?';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
623 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
624 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
625 else if (pfound->has_arg == 1)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
626 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
627 if (d->optind < argc)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
628 d->optarg = argv[d->optind++];
d5392bb5da3c Added generated files
lost
parents:
diff changeset
629 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
630 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
631 if (print_errors)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
632 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
633 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
634 char *buf;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
635
d5392bb5da3c Added generated files
lost
parents:
diff changeset
636 if (__asprintf (&buf, _("\
d5392bb5da3c Added generated files
lost
parents:
diff changeset
637 %s: option `%s' requires an argument\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
638 argv[0], argv[d->optind - 1]) >= 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
639 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
640 _IO_flockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
641
d5392bb5da3c Added generated files
lost
parents:
diff changeset
642 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
643 ((_IO_FILE *) stderr)->_flags2
d5392bb5da3c Added generated files
lost
parents:
diff changeset
644 |= _IO_FLAGS2_NOTCANCEL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
645
d5392bb5da3c Added generated files
lost
parents:
diff changeset
646 __fxprintf (NULL, "%s", buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
647
d5392bb5da3c Added generated files
lost
parents:
diff changeset
648 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
649 _IO_funlockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
650
d5392bb5da3c Added generated files
lost
parents:
diff changeset
651 free (buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
652 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
653 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
654 fprintf (stderr,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
655 _("%s: option `%s' requires an argument\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
656 argv[0], argv[d->optind - 1]);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
657 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
658 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
659 d->__nextchar += strlen (d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
660 d->optopt = pfound->val;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
661 return optstring[0] == ':' ? ':' : '?';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
662 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
663 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
664 d->__nextchar += strlen (d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
665 if (longind != NULL)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
666 *longind = option_index;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
667 if (pfound->flag)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
668 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
669 *(pfound->flag) = pfound->val;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
670 return 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
671 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
672 return pfound->val;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
673 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
674
d5392bb5da3c Added generated files
lost
parents:
diff changeset
675 /* Can't find it as a long option. If this is not getopt_long_only,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
676 or the option starts with '--' or is not a valid short
d5392bb5da3c Added generated files
lost
parents:
diff changeset
677 option, then it's an error.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
678 Otherwise interpret it as a short option. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
679 if (!long_only || argv[d->optind][1] == '-'
d5392bb5da3c Added generated files
lost
parents:
diff changeset
680 || strchr (optstring, *d->__nextchar) == NULL)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
681 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
682 if (print_errors)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
683 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
684 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
685 char *buf;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
686 int n;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
687 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
688
d5392bb5da3c Added generated files
lost
parents:
diff changeset
689 if (argv[d->optind][1] == '-')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
690 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
691 /* --option */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
692 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
693 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
694 argv[0], d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
695 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
696 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
697 argv[0], d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
698 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
699 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
700 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
701 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
702 /* +option or -option */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
703 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
704 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
705 argv[0], argv[d->optind][0], d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
706 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
707 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
708 argv[0], argv[d->optind][0], d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
709 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
710 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
711
d5392bb5da3c Added generated files
lost
parents:
diff changeset
712 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
713 if (n >= 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
714 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
715 _IO_flockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
716
d5392bb5da3c Added generated files
lost
parents:
diff changeset
717 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
718 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
719
d5392bb5da3c Added generated files
lost
parents:
diff changeset
720 __fxprintf (NULL, "%s", buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
721
d5392bb5da3c Added generated files
lost
parents:
diff changeset
722 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
723 _IO_funlockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
724
d5392bb5da3c Added generated files
lost
parents:
diff changeset
725 free (buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
726 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
727 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
728 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
729 d->__nextchar = (char *) "";
d5392bb5da3c Added generated files
lost
parents:
diff changeset
730 d->optind++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
731 d->optopt = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
732 return '?';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
733 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
734 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
735
d5392bb5da3c Added generated files
lost
parents:
diff changeset
736 /* Look at and handle the next short option-character. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
737
d5392bb5da3c Added generated files
lost
parents:
diff changeset
738 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
739 char c = *d->__nextchar++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
740 char *temp = strchr (optstring, c);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
741
d5392bb5da3c Added generated files
lost
parents:
diff changeset
742 /* Increment `optind' when we start to process its last character. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
743 if (*d->__nextchar == '\0')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
744 ++d->optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
745
d5392bb5da3c Added generated files
lost
parents:
diff changeset
746 if (temp == NULL || c == ':')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
747 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
748 if (print_errors)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
749 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
750 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
751 char *buf;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
752 int n;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
753 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
754
d5392bb5da3c Added generated files
lost
parents:
diff changeset
755 if (d->__posixly_correct)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
756 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
757 /* 1003.2 specifies the format of this message. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
758 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
759 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
760 argv[0], c);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
761 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
762 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
763 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
764 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
765 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
766 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
767 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
768 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
769 argv[0], c);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
770 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
771 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
772 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
773 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
774
d5392bb5da3c Added generated files
lost
parents:
diff changeset
775 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
776 if (n >= 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
777 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
778 _IO_flockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
779
d5392bb5da3c Added generated files
lost
parents:
diff changeset
780 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
781 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
782
d5392bb5da3c Added generated files
lost
parents:
diff changeset
783 __fxprintf (NULL, "%s", buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
784
d5392bb5da3c Added generated files
lost
parents:
diff changeset
785 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
786 _IO_funlockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
787
d5392bb5da3c Added generated files
lost
parents:
diff changeset
788 free (buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
789 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
790 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
791 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
792 d->optopt = c;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
793 return '?';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
794 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
795 /* Convenience. Treat POSIX -W foo same as long option --foo */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
796 if (temp[0] == 'W' && temp[1] == ';')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
797 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
798 char *nameend;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
799 const struct option *p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
800 const struct option *pfound = NULL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
801 int exact = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
802 int ambig = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
803 int indfound = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
804 int option_index;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
805
d5392bb5da3c Added generated files
lost
parents:
diff changeset
806 /* This is an option that requires an argument. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
807 if (*d->__nextchar != '\0')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
808 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
809 d->optarg = d->__nextchar;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
810 /* If we end this ARGV-element by taking the rest as an arg,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
811 we must advance to the next element now. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
812 d->optind++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
813 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
814 else if (d->optind == argc)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
815 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
816 if (print_errors)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
817 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
818 /* 1003.2 specifies the format of this message. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
819 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
820 char *buf;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
821
d5392bb5da3c Added generated files
lost
parents:
diff changeset
822 if (__asprintf (&buf,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
823 _("%s: option requires an argument -- %c\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
824 argv[0], c) >= 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
825 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
826 _IO_flockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
827
d5392bb5da3c Added generated files
lost
parents:
diff changeset
828 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
829 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
830
d5392bb5da3c Added generated files
lost
parents:
diff changeset
831 __fxprintf (NULL, "%s", buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
832
d5392bb5da3c Added generated files
lost
parents:
diff changeset
833 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
834 _IO_funlockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
835
d5392bb5da3c Added generated files
lost
parents:
diff changeset
836 free (buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
837 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
838 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
839 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
840 argv[0], c);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
841 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
842 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
843 d->optopt = c;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
844 if (optstring[0] == ':')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
845 c = ':';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
846 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
847 c = '?';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
848 return c;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
849 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
850 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
851 /* We already incremented `d->optind' once;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
852 increment it again when taking next ARGV-elt as argument. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
853 d->optarg = argv[d->optind++];
d5392bb5da3c Added generated files
lost
parents:
diff changeset
854
d5392bb5da3c Added generated files
lost
parents:
diff changeset
855 /* optarg is now the argument, see if it's in the
d5392bb5da3c Added generated files
lost
parents:
diff changeset
856 table of longopts. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
857
d5392bb5da3c Added generated files
lost
parents:
diff changeset
858 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
859 nameend++)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
860 /* Do nothing. */ ;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
861
d5392bb5da3c Added generated files
lost
parents:
diff changeset
862 /* Test all long options for either exact match
d5392bb5da3c Added generated files
lost
parents:
diff changeset
863 or abbreviated matches. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
864 for (p = longopts, option_index = 0; p->name; p++, option_index++)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
865 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
866 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
867 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
d5392bb5da3c Added generated files
lost
parents:
diff changeset
868 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
869 /* Exact match found. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
870 pfound = p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
871 indfound = option_index;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
872 exact = 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
873 break;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
874 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
875 else if (pfound == NULL)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
876 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
877 /* First nonexact match found. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
878 pfound = p;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
879 indfound = option_index;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
880 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
881 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
882 /* Second or later nonexact match found. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
883 ambig = 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
884 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
885 if (ambig && !exact)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
886 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
887 if (print_errors)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
888 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
889 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
890 char *buf;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
891
d5392bb5da3c Added generated files
lost
parents:
diff changeset
892 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
893 argv[0], argv[d->optind]) >= 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
894 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
895 _IO_flockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
896
d5392bb5da3c Added generated files
lost
parents:
diff changeset
897 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
898 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
899
d5392bb5da3c Added generated files
lost
parents:
diff changeset
900 __fxprintf (NULL, "%s", buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
901
d5392bb5da3c Added generated files
lost
parents:
diff changeset
902 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
903 _IO_funlockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
904
d5392bb5da3c Added generated files
lost
parents:
diff changeset
905 free (buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
906 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
907 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
908 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
909 argv[0], argv[d->optind]);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
910 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
911 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
912 d->__nextchar += strlen (d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
913 d->optind++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
914 return '?';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
915 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
916 if (pfound != NULL)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
917 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
918 option_index = indfound;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
919 if (*nameend)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
920 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
921 /* Don't test has_arg with >, because some C compilers don't
d5392bb5da3c Added generated files
lost
parents:
diff changeset
922 allow it to be used on enums. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
923 if (pfound->has_arg)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
924 d->optarg = nameend + 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
925 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
926 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
927 if (print_errors)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
928 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
929 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
930 char *buf;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
931
d5392bb5da3c Added generated files
lost
parents:
diff changeset
932 if (__asprintf (&buf, _("\
d5392bb5da3c Added generated files
lost
parents:
diff changeset
933 %s: option `-W %s' doesn't allow an argument\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
934 argv[0], pfound->name) >= 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
935 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
936 _IO_flockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
937
d5392bb5da3c Added generated files
lost
parents:
diff changeset
938 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
939 ((_IO_FILE *) stderr)->_flags2
d5392bb5da3c Added generated files
lost
parents:
diff changeset
940 |= _IO_FLAGS2_NOTCANCEL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
941
d5392bb5da3c Added generated files
lost
parents:
diff changeset
942 __fxprintf (NULL, "%s", buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
943
d5392bb5da3c Added generated files
lost
parents:
diff changeset
944 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
945 _IO_funlockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
946
d5392bb5da3c Added generated files
lost
parents:
diff changeset
947 free (buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
948 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
949 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
950 fprintf (stderr, _("\
d5392bb5da3c Added generated files
lost
parents:
diff changeset
951 %s: option `-W %s' doesn't allow an argument\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
952 argv[0], pfound->name);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
953 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
954 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
955
d5392bb5da3c Added generated files
lost
parents:
diff changeset
956 d->__nextchar += strlen (d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
957 return '?';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
958 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
959 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
960 else if (pfound->has_arg == 1)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
961 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
962 if (d->optind < argc)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
963 d->optarg = argv[d->optind++];
d5392bb5da3c Added generated files
lost
parents:
diff changeset
964 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
965 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
966 if (print_errors)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
967 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
968 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
969 char *buf;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
970
d5392bb5da3c Added generated files
lost
parents:
diff changeset
971 if (__asprintf (&buf, _("\
d5392bb5da3c Added generated files
lost
parents:
diff changeset
972 %s: option `%s' requires an argument\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
973 argv[0], argv[d->optind - 1]) >= 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
974 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
975 _IO_flockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
976
d5392bb5da3c Added generated files
lost
parents:
diff changeset
977 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
978 ((_IO_FILE *) stderr)->_flags2
d5392bb5da3c Added generated files
lost
parents:
diff changeset
979 |= _IO_FLAGS2_NOTCANCEL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
980
d5392bb5da3c Added generated files
lost
parents:
diff changeset
981 __fxprintf (NULL, "%s", buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
982
d5392bb5da3c Added generated files
lost
parents:
diff changeset
983 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
984 _IO_funlockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
985
d5392bb5da3c Added generated files
lost
parents:
diff changeset
986 free (buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
987 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
988 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
989 fprintf (stderr,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
990 _("%s: option `%s' requires an argument\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
991 argv[0], argv[d->optind - 1]);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
992 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
993 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
994 d->__nextchar += strlen (d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
995 return optstring[0] == ':' ? ':' : '?';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
996 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
997 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
998 d->__nextchar += strlen (d->__nextchar);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
999 if (longind != NULL)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1000 *longind = option_index;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1001 if (pfound->flag)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1002 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1003 *(pfound->flag) = pfound->val;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1004 return 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1005 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1006 return pfound->val;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1007 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1008 d->__nextchar = NULL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1009 return 'W'; /* Let the application handle it. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1010 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1011 if (temp[1] == ':')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1012 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1013 if (temp[2] == ':')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1014 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1015 /* This is an option that accepts an argument optionally. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1016 if (*d->__nextchar != '\0')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1017 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1018 d->optarg = d->__nextchar;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1019 d->optind++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1020 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1021 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1022 d->optarg = NULL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1023 d->__nextchar = NULL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1024 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1025 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1026 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1027 /* This is an option that requires an argument. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1028 if (*d->__nextchar != '\0')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1029 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1030 d->optarg = d->__nextchar;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1031 /* If we end this ARGV-element by taking the rest as an arg,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1032 we must advance to the next element now. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1033 d->optind++;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1034 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1035 else if (d->optind == argc)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1036 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1037 if (print_errors)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1038 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1039 /* 1003.2 specifies the format of this message. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1040 #if defined _LIBC && defined USE_IN_LIBIO
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1041 char *buf;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1042
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1043 if (__asprintf (&buf, _("\
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1044 %s: option requires an argument -- %c\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1045 argv[0], c) >= 0)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1046 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1047 _IO_flockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1048
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1049 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1050 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1051
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1052 __fxprintf (NULL, "%s", buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1053
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1054 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1055 _IO_funlockfile (stderr);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1056
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1057 free (buf);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1058 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1059 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1060 fprintf (stderr,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1061 _("%s: option requires an argument -- %c\n"),
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1062 argv[0], c);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1063 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1064 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1065 d->optopt = c;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1066 if (optstring[0] == ':')
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1067 c = ':';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1068 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1069 c = '?';
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1070 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1071 else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1072 /* We already incremented `optind' once;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1073 increment it again when taking next ARGV-elt as argument. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1074 d->optarg = argv[d->optind++];
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1075 d->__nextchar = NULL;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1076 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1077 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1078 return c;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1079 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1080 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1081
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1082 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1083 _getopt_internal (int argc, char **argv, const char *optstring,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1084 const struct option *longopts, int *longind,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1085 int long_only, int posixly_correct)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1086 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1087 int result;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1088
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1089 getopt_data.optind = optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1090 getopt_data.opterr = opterr;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1091
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1092 result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1093 long_only, posixly_correct, &getopt_data);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1094
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1095 optind = getopt_data.optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1096 optarg = getopt_data.optarg;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1097 optopt = getopt_data.optopt;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1098
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1099 return result;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1100 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1101
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1102 /* glibc gets a LSB-compliant getopt.
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1103 Standalone applications get a POSIX-compliant getopt. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1104 #if _LIBC
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1105 enum { POSIXLY_CORRECT = 0 };
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1106 #else
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1107 enum { POSIXLY_CORRECT = 1 };
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1108 #endif
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1109
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1110 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1111 getopt (int argc, char *const *argv, const char *optstring)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1112 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1113 return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1114 POSIXLY_CORRECT);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1115 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1116
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1117
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1118 #ifdef TEST
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1119
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1120 /* Compile with -DTEST to make an executable for use in testing
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1121 the above definition of `getopt'. */
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1122
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1123 int
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1124 main (int argc, char **argv)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1125 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1126 int c;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1127 int digit_optind = 0;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1128
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1129 while (1)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1130 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1131 int this_option_optind = optind ? optind : 1;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1132
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1133 c = getopt (argc, argv, "abc:d:0123456789");
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1134 if (c == -1)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1135 break;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1136
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1137 switch (c)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1138 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1139 case '0':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1140 case '1':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1141 case '2':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1142 case '3':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1143 case '4':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1144 case '5':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1145 case '6':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1146 case '7':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1147 case '8':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1148 case '9':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1149 if (digit_optind != 0 && digit_optind != this_option_optind)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1150 printf ("digits occur in two different argv-elements.\n");
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1151 digit_optind = this_option_optind;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1152 printf ("option %c\n", c);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1153 break;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1154
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1155 case 'a':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1156 printf ("option a\n");
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1157 break;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1158
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1159 case 'b':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1160 printf ("option b\n");
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1161 break;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1162
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1163 case 'c':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1164 printf ("option c with value `%s'\n", optarg);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1165 break;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1166
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1167 case '?':
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1168 break;
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1169
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1170 default:
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1171 printf ("?? getopt returned character code 0%o ??\n", c);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1172 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1173 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1174
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1175 if (optind < argc)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1176 {
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1177 printf ("non-option ARGV-elements: ");
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1178 while (optind < argc)
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1179 printf ("%s ", argv[optind++]);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1180 printf ("\n");
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1181 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1182
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1183 exit (0);
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1184 }
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1185
d5392bb5da3c Added generated files
lost
parents:
diff changeset
1186 #endif /* TEST */