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