comparison lib-src/getopt.c @ 155:43dd3413c7c7 r20-3b4

Import from CVS: tag r20-3b4
author cvs
date Mon, 13 Aug 2007 09:39:39 +0200
parents 131b0175ea99
children 15872534500d
comparison
equal deleted inserted replaced
154:94141801dd7e 155:43dd3413c7c7
1 /* Getopt for GNU. 1 /* Getopt for GNU.
2 NOTE: getopt is now part of the C library, so if you don't know what 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 3 "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
4 before changing it! 4 before changing it!
5 5
6 Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95 6 Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97
7 Free Software Foundation, Inc. 7 Free Software Foundation, Inc.
8 8
9 This program is free software; you can redistribute it and/or modify it 9 NOTE: The canonical source of this file is maintained with the GNU C Library.
10 under the terms of the GNU General Public License as published by the 10 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
11 Free Software Foundation; either version 2, or (at your option) any 11
12 later version. 12 This program is free software; you can redistribute it and/or modify it
13 13 under the terms of the GNU General Public License as published by the
14 This program is distributed in the hope that it will be useful, 14 Free Software Foundation; either version 2, or (at your option) any
15 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 later version.
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16
17 GNU General Public License for more details. 17 This program is distributed in the hope that it will be useful,
18 18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 You should have received a copy of the GNU General Public License 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 along with this program; see the file COPYING. If not, write to 20 GNU General Public License for more details.
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 21
22 Boston, MA 02111-1307, USA. */ 22 You should have received a copy of the GNU General Public License
23 23 along with this program; if not, write to the Free Software
24 /* Synched up with: FSF 19.28. */ 24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 25 USA. */
26 26
27 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. 27 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
28 Ditto for AIX 3.2 and <stdlib.h>. */ 28 Ditto for AIX 3.2 and <stdlib.h>. */
29 #ifndef _NO_PROTO 29 #ifndef _NO_PROTO
30 #define _NO_PROTO 30 #define _NO_PROTO
31 #endif 31 #endif
32 32
33 #ifdef HAVE_CONFIG_H 33 #ifdef HAVE_CONFIG_H
34 #if defined (emacs) || defined (CONFIG_BROKETS)
35 /* We use <config.h> instead of "config.h" so that a compilation
36 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
37 (which it would do because it found this file in $srcdir). */
38 #include <config.h> 34 #include <config.h>
39 #else
40 #include "config.h"
41 #endif
42 #endif 35 #endif
43 36
44 #if !defined (__STDC__) || !__STDC__ 37 #if !defined (__STDC__) || !__STDC__
45 /* This is a separate conditional since some stdc systems 38 /* This is a separate conditional since some stdc systems
46 reject `defined (const)'. */ 39 reject `defined (const)'. */
48 #define const 41 #define const
49 #endif 42 #endif
50 #endif 43 #endif
51 44
52 #include <stdio.h> 45 #include <stdio.h>
46 #ifdef STDC_HEADERS
47 #include <string.h>
53 #include <stdlib.h> 48 #include <stdlib.h>
54 #include <string.h> 49 #endif
55 50
56 /* Comment out all this code if we are using the GNU C Library, and are not 51 /* Comment out all this code if we are using the GNU C Library, and are not
57 actually compiling the library itself. This code is part of the GNU C 52 actually compiling the library itself. This code is part of the GNU C
58 Library, but also included in many other GNU distributions. Compiling 53 Library, but also included in many other GNU distributions. Compiling
59 and linking in this code is a waste when using the GNU C library 54 and linking in this code is a waste when using the GNU C library
60 (especially if it is a shared library). Rather than having every GNU 55 (especially if it is a shared library). Rather than having every GNU
61 program understand `configure --with-gnu-libc' and omit the object files, 56 program understand `configure --with-gnu-libc' and omit the object files,
62 it is simpler to just do this in the source for each such file. */ 57 it is simpler to just do this in the source for each such file. */
63 58
64 #if defined (_LIBC) || !defined (__GNU_LIBRARY__) 59 #define GETOPT_INTERFACE_VERSION 2
60 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2
61 #include <gnu-versions.h>
62 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
63 #define ELIDE_CODE
64 #endif
65 #endif
66
67 #ifndef ELIDE_CODE
65 68
66 69
67 /* This needs to come after some library #include 70 /* This needs to come after some library #include
68 to get __GNU_LIBRARY__ defined. */ 71 to get __GNU_LIBRARY__ defined. */
69 #ifdef __GNU_LIBRARY__ 72 #ifdef __GNU_LIBRARY__
70 /* Don't include stdlib.h for non-GNU C libraries because some of them 73 /* Don't include stdlib.h for non-GNU C libraries because some of them
71 contain conflicting prototypes for getopt. */ 74 contain conflicting prototypes for getopt. */
72 #include <stdlib.h> 75 #include <stdlib.h>
76 #include <unistd.h>
73 #endif /* GNU C library. */ 77 #endif /* GNU C library. */
74 78
79 #ifdef VMS
80 #include <unixlib.h>
81 #if HAVE_STRING_H - 0
82 #include <string.h>
83 #endif
84 #endif
85
86 #if defined (WIN32) && !defined (__CYGWIN32__)
87 /* It's not Unix, really. See? Capital letters. */
88 #include <windows.h>
89 #define getpid() GetCurrentProcessId()
90 #endif
91
92 #ifndef _
75 /* This is for other GNU distributions with internationalized messages. 93 /* This is for other GNU distributions with internationalized messages.
76 The GNU C Library itself does not yet support such messages. */ 94 When compiling libc, the _ macro is predefined. */
77 #if defined (I18N3) && defined (HAVE_LIBINTL_H) 95 #ifdef HAVE_LIBINTL_H
78 # include <libintl.h> 96 # include <libintl.h>
97 # define _(msgid) gettext (msgid)
79 #else 98 #else
80 # define gettext(msgid) (msgid) 99 # define _(msgid) (msgid)
100 #endif
81 #endif 101 #endif
82 102
83 /* This version of `getopt' appears to the caller like standard Unix `getopt' 103 /* This version of `getopt' appears to the caller like standard Unix `getopt'
84 but it behaves differently for the user, since it allows the user 104 but it behaves differently for the user, since it allows the user
85 to intersperse the options with the other arguments. 105 to intersperse the options with the other arguments.
108 This is used for communication to and from the caller 128 This is used for communication to and from the caller
109 and for communication between successive calls to `getopt'. 129 and for communication between successive calls to `getopt'.
110 130
111 On entry to `getopt', zero means this is the first call; initialize. 131 On entry to `getopt', zero means this is the first call; initialize.
112 132
113 When `getopt' returns EOF, this is the index of the first of the 133 When `getopt' returns -1, this is the index of the first of the
114 non-option elements that the caller should itself scan. 134 non-option elements that the caller should itself scan.
115 135
116 Otherwise, `optind' communicates from one call to the next 136 Otherwise, `optind' communicates from one call to the next
117 how much of ARGV has been scanned so far. */ 137 how much of ARGV has been scanned so far. */
118 138
119 /* XXX 1003.2 says this must be 1 before any call. */ 139 /* 1003.2 says this must be 1 before any call. */
120 int optind = 0; 140 int optind = 1;
141
142 /* Formerly, initialization of getopt depended on optind==0, which
143 causes problems with re-calling getopt as programs generally don't
144 know that. */
145
146 int __getopt_initialized = 0;
121 147
122 /* The next char to be scanned in the option-element 148 /* The next char to be scanned in the option-element
123 in which the last option character we returned was found. 149 in which the last option character we returned was found.
124 This allows us to pick up the scan where we left off. 150 This allows us to pick up the scan where we left off.
125 151
164 Using `-' as the first character of the list of option characters 190 Using `-' as the first character of the list of option characters
165 selects this mode of operation. 191 selects this mode of operation.
166 192
167 The special argument `--' forces an end of option-scanning regardless 193 The special argument `--' forces an end of option-scanning regardless
168 of the value of `ordering'. In the case of RETURN_IN_ORDER, only 194 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
169 `--' can cause `getopt' to return EOF with `optind' != ARGC. */ 195 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
170 196
171 static enum 197 static enum
172 { 198 {
173 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER 199 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
174 } ordering; 200 } ordering;
189 whose names are inconsistent. */ 215 whose names are inconsistent. */
190 216
191 char *getenv (); 217 char *getenv ();
192 218
193 static char * 219 static char *
194 my_index (str, chr) 220 my_index (const char *str, int chr)
195 const char *str;
196 int chr;
197 { 221 {
198 while (*str) 222 while (*str)
199 { 223 {
200 if (*str == chr) 224 if (*str == chr)
201 return (char *) str; 225 return (char *) str;
225 `last_nonopt' is the index after the last of them. */ 249 `last_nonopt' is the index after the last of them. */
226 250
227 static int first_nonopt; 251 static int first_nonopt;
228 static int last_nonopt; 252 static int last_nonopt;
229 253
254 #ifdef _LIBC
255 /* Bash 2.0 gives us an environment variable containing flags
256 indicating ARGV elements that should not be considered arguments. */
257
258 /* Defined in getopt_init.c */
259 extern char *__getopt_nonoption_flags;
260
261 static int nonoption_flags_max_len;
262 static int nonoption_flags_len;
263
264 static int original_argc;
265 static char *const *original_argv;
266
267 extern pid_t __libc_pid;
268
269 /* Make sure the environment variable bash 2.0 puts in the environment
270 is valid for the getopt call we must make sure that the ARGV passed
271 to getopt is that one passed to the process. */
272 static void
273 __attribute__ ((unused))
274 store_args_and_env (int argc, char *const *argv)
275 {
276 /* XXX This is no good solution. We should rather copy the args so
277 that we can compare them later. But we must not use malloc(3). */
278 original_argc = argc;
279 original_argv = argv;
280 }
281 text_set_element (__libc_subinit, store_args_and_env);
282
283 # define SWAP_FLAGS(ch1, ch2) \
284 if (nonoption_flags_len > 0) \
285 { \
286 char __tmp = __getopt_nonoption_flags[ch1]; \
287 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
288 __getopt_nonoption_flags[ch2] = __tmp; \
289 }
290 #else /* !_LIBC */
291 # define SWAP_FLAGS(ch1, ch2)
292 #endif /* _LIBC */
293
230 /* Exchange two adjacent subsequences of ARGV. 294 /* Exchange two adjacent subsequences of ARGV.
231 One subsequence is elements [first_nonopt,last_nonopt) 295 One subsequence is elements [first_nonopt,last_nonopt)
232 which contains all the non-options that have been skipped so far. 296 which contains all the non-options that have been skipped so far.
233 The other is elements [last_nonopt,optind), which contains all 297 The other is elements [last_nonopt,optind), which contains all
234 the options processed since those non-options were skipped. 298 the options processed since those non-options were skipped.
235 299
236 `first_nonopt' and `last_nonopt' are relocated so that they describe 300 `first_nonopt' and `last_nonopt' are relocated so that they describe
237 the new indices of the non-options in ARGV after they are moved. */ 301 the new indices of the non-options in ARGV after they are moved. */
238 302
303 #if defined (__STDC__) && __STDC__
304 static void exchange (char **);
305 #endif
306
239 static void 307 static void
240 exchange (argv) 308 exchange (char **argv)
241 char **argv;
242 { 309 {
243 int bottom = first_nonopt; 310 int bottom = first_nonopt;
244 int middle = last_nonopt; 311 int middle = last_nonopt;
245 int top = optind; 312 int top = optind;
246 char *tem; 313 char *tem;
247 314
248 /* Exchange the shorter segment with the far end of the longer segment. 315 /* Exchange the shorter segment with the far end of the longer segment.
249 That puts the shorter segment into the right place. 316 That puts the shorter segment into the right place.
250 It leaves the longer segment in the right place overall, 317 It leaves the longer segment in the right place overall,
251 but it consists of two parts that need to be swapped next. */ 318 but it consists of two parts that need to be swapped next. */
319
320 #ifdef _LIBC
321 /* First make sure the handling of the `__getopt_nonoption_flags'
322 string can work normally. Our top argument must be in the range
323 of the string. */
324 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
325 {
326 /* We must extend the array. The user plays games with us and
327 presents new arguments. */
328 char *new_str = malloc (top + 1);
329 if (new_str == NULL)
330 nonoption_flags_len = nonoption_flags_max_len = 0;
331 else
332 {
333 memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len);
334 memset (&new_str[nonoption_flags_max_len], '\0',
335 top + 1 - nonoption_flags_max_len);
336 nonoption_flags_max_len = top + 1;
337 __getopt_nonoption_flags = new_str;
338 }
339 }
340 #endif
252 341
253 while (top > middle && middle > bottom) 342 while (top > middle && middle > bottom)
254 { 343 {
255 if (top - middle > middle - bottom) 344 if (top - middle > middle - bottom)
256 { 345 {
262 for (i = 0; i < len; i++) 351 for (i = 0; i < len; i++)
263 { 352 {
264 tem = argv[bottom + i]; 353 tem = argv[bottom + i];
265 argv[bottom + i] = argv[top - (middle - bottom) + i]; 354 argv[bottom + i] = argv[top - (middle - bottom) + i];
266 argv[top - (middle - bottom) + i] = tem; 355 argv[top - (middle - bottom) + i] = tem;
356 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
267 } 357 }
268 /* Exclude the moved bottom segment from further swapping. */ 358 /* Exclude the moved bottom segment from further swapping. */
269 top -= len; 359 top -= len;
270 } 360 }
271 else 361 else
278 for (i = 0; i < len; i++) 368 for (i = 0; i < len; i++)
279 { 369 {
280 tem = argv[bottom + i]; 370 tem = argv[bottom + i];
281 argv[bottom + i] = argv[middle + i]; 371 argv[bottom + i] = argv[middle + i];
282 argv[middle + i] = tem; 372 argv[middle + i] = tem;
373 SWAP_FLAGS (bottom + i, middle + i);
283 } 374 }
284 /* Exclude the moved top segment from further swapping. */ 375 /* Exclude the moved top segment from further swapping. */
285 bottom += len; 376 bottom += len;
286 } 377 }
287 } 378 }
292 last_nonopt = optind; 383 last_nonopt = optind;
293 } 384 }
294 385
295 /* Initialize the internal data when the first call is made. */ 386 /* Initialize the internal data when the first call is made. */
296 387
388 #if defined (__STDC__) && __STDC__
389 static const char *_getopt_initialize (int, char *const *, const char *);
390 #endif
297 static const char * 391 static const char *
298 _getopt_initialize (optstring) 392 _getopt_initialize (int argc, char *const *argv, const char *optstring)
299 const char *optstring;
300 { 393 {
301 /* Start processing options with ARGV-element 1 (since ARGV-element 0 394 /* Start processing options with ARGV-element 1 (since ARGV-element 0
302 is the program name); the sequence of previously skipped 395 is the program name); the sequence of previously skipped
303 non-option ARGV-elements is empty. */ 396 non-option ARGV-elements is empty. */
304 397
305 first_nonopt = last_nonopt = optind = 1; 398 first_nonopt = last_nonopt = optind;
306 399
307 nextchar = NULL; 400 nextchar = NULL;
308 401
309 posixly_correct = getenv ("POSIXLY_CORRECT"); 402 posixly_correct = getenv ("POSIXLY_CORRECT");
310 403
322 } 415 }
323 else if (posixly_correct != NULL) 416 else if (posixly_correct != NULL)
324 ordering = REQUIRE_ORDER; 417 ordering = REQUIRE_ORDER;
325 else 418 else
326 ordering = PERMUTE; 419 ordering = PERMUTE;
420
421 #ifdef _LIBC
422 if (posixly_correct == NULL
423 && argc == original_argc && argv == original_argv)
424 {
425 if (nonoption_flags_max_len == 0)
426 {
427 if (__getopt_nonoption_flags == NULL
428 || __getopt_nonoption_flags[0] == '\0')
429 nonoption_flags_max_len = -1;
430 else
431 {
432 const char *orig_str = __getopt_nonoption_flags;
433 int len = nonoption_flags_max_len = strlen (orig_str);
434 if (nonoption_flags_max_len < argc)
435 nonoption_flags_max_len = argc;
436 __getopt_nonoption_flags =
437 (char *) malloc (nonoption_flags_max_len);
438 if (__getopt_nonoption_flags == NULL)
439 nonoption_flags_max_len = -1;
440 else
441 {
442 memcpy (__getopt_nonoption_flags, orig_str, len);
443 memset (&__getopt_nonoption_flags[len], '\0',
444 nonoption_flags_max_len - len);
445 }
446 }
447 }
448 nonoption_flags_len = nonoption_flags_max_len;
449 }
450 else
451 nonoption_flags_len = 0;
452 #endif
327 453
328 return optstring; 454 return optstring;
329 } 455 }
330 456
331 /* Scan elements of ARGV (whose length is ARGC) for option characters 457 /* Scan elements of ARGV (whose length is ARGC) for option characters
339 465
340 If `getopt' finds another option character, it returns that character, 466 If `getopt' finds another option character, it returns that character,
341 updating `optind' and `nextchar' so that the next call to `getopt' can 467 updating `optind' and `nextchar' so that the next call to `getopt' can
342 resume the scan with the following option character or ARGV-element. 468 resume the scan with the following option character or ARGV-element.
343 469
344 If there are no more option characters, `getopt' returns `EOF'. 470 If there are no more option characters, `getopt' returns -1.
345 Then `optind' is the index in ARGV of the first ARGV-element 471 Then `optind' is the index in ARGV of the first ARGV-element
346 that is not an option. (The ARGV-elements have been permuted 472 that is not an option. (The ARGV-elements have been permuted
347 so that those that are not options now come last.) 473 so that those that are not options now come last.)
348 474
349 OPTSTRING is a string containing the legitimate option characters. 475 OPTSTRING is a string containing the legitimate option characters.
383 509
384 If LONG_ONLY is nonzero, '-' as well as '--' can introduce 510 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
385 long-named options. */ 511 long-named options. */
386 512
387 int 513 int
388 _getopt_internal (argc, argv, optstring, longopts, longind, long_only) 514 _getopt_internal (int argc, char *const *argv, const char *optstring,
389 int argc; 515 const struct option *longopts, int *longind, int long_only)
390 char *const *argv;
391 const char *optstring;
392 const struct option *longopts;
393 int *longind;
394 int long_only;
395 { 516 {
396 optarg = NULL; 517 optarg = NULL;
397 518
398 if (optind == 0) 519 if (optind == 0 || !__getopt_initialized)
399 { 520 {
400 optstring = _getopt_initialize (optstring); 521 if (optind == 0)
401 optind = 1; /* Don't scan ARGV[0], the program name. */ 522 optind = 1; /* Don't scan ARGV[0], the program name. */
402 } 523 optstring = _getopt_initialize (argc, argv, optstring);
524 __getopt_initialized = 1;
525 }
526
527 /* Test whether ARGV[optind] points to a non-option argument.
528 Either it does not have option syntax, or there is an environment flag
529 from the shell indicating it is not an option. The later information
530 is only used when the used in the GNU libc. */
531 #ifdef _LIBC
532 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
533 || (optind < nonoption_flags_len \
534 && __getopt_nonoption_flags[optind] == '1'))
535 #else
536 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
537 #endif
403 538
404 if (nextchar == NULL || *nextchar == '\0') 539 if (nextchar == NULL || *nextchar == '\0')
405 { 540 {
406 /* Advance to the next ARGV-element. */ 541 /* Advance to the next ARGV-element. */
542
543 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
544 moved back by the user (who may also have changed the arguments). */
545 if (last_nonopt > optind)
546 last_nonopt = optind;
547 if (first_nonopt > optind)
548 first_nonopt = optind;
407 549
408 if (ordering == PERMUTE) 550 if (ordering == PERMUTE)
409 { 551 {
410 /* If we have just processed some options following some non-options, 552 /* If we have just processed some options following some non-options,
411 exchange them so that the options come first. */ 553 exchange them so that the options come first. */
416 first_nonopt = optind; 558 first_nonopt = optind;
417 559
418 /* Skip any additional non-options 560 /* Skip any additional non-options
419 and extend the range of non-options previously skipped. */ 561 and extend the range of non-options previously skipped. */
420 562
421 while (optind < argc 563 while (optind < argc && NONOPTION_P)
422 && (argv[optind][0] != '-' || argv[optind][1] == '\0'))
423 optind++; 564 optind++;
424 last_nonopt = optind; 565 last_nonopt = optind;
425 } 566 }
426 567
427 /* The special ARGV-element `--' means premature end of options. 568 /* The special ARGV-element `--' means premature end of options.
449 { 590 {
450 /* Set the next-arg-index to point at the non-options 591 /* Set the next-arg-index to point at the non-options
451 that we previously skipped, so the caller will digest them. */ 592 that we previously skipped, so the caller will digest them. */
452 if (first_nonopt != last_nonopt) 593 if (first_nonopt != last_nonopt)
453 optind = first_nonopt; 594 optind = first_nonopt;
454 return EOF; 595 return -1;
455 } 596 }
456 597
457 /* If we have come to a non-option and did not permute it, 598 /* If we have come to a non-option and did not permute it,
458 either stop the scan or describe it to the caller and pass it by. */ 599 either stop the scan or describe it to the caller and pass it by. */
459 600
460 if ((argv[optind][0] != '-' || argv[optind][1] == '\0')) 601 if (NONOPTION_P)
461 { 602 {
462 if (ordering == REQUIRE_ORDER) 603 if (ordering == REQUIRE_ORDER)
463 return EOF; 604 return -1;
464 optarg = argv[optind++]; 605 optarg = argv[optind++];
465 return 1; 606 return 1;
466 } 607 }
467 608
468 /* We have found another option-ARGV-element. 609 /* We have found another option-ARGV-element.
494 char *nameend; 635 char *nameend;
495 const struct option *p; 636 const struct option *p;
496 const struct option *pfound = NULL; 637 const struct option *pfound = NULL;
497 int exact = 0; 638 int exact = 0;
498 int ambig = 0; 639 int ambig = 0;
499 int indfound = 0; 640 int indfound = -1;
500 int option_index; 641 int option_index;
501 642
502 for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 643 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
503 /* Do nothing. */ ; 644 /* Do nothing. */ ;
504 645
505 /* Test all long options for either exact match 646 /* Test all long options for either exact match
506 or abbreviated matches. */ 647 or abbreviated matches. */
507 for (p = longopts, option_index = 0; p->name; p++, option_index++) 648 for (p = longopts, option_index = 0; p->name; p++, option_index++)
508 if (!strncmp (p->name, nextchar, nameend - nextchar)) 649 if (!strncmp (p->name, nextchar, nameend - nextchar))
509 { 650 {
510 if (nameend - nextchar == strlen (p->name)) 651 if ((unsigned int) (nameend - nextchar)
652 == (unsigned int) strlen (p->name))
511 { 653 {
512 /* Exact match found. */ 654 /* Exact match found. */
513 pfound = p; 655 pfound = p;
514 indfound = option_index; 656 indfound = option_index;
515 exact = 1; 657 exact = 1;
527 } 669 }
528 670
529 if (ambig && !exact) 671 if (ambig && !exact)
530 { 672 {
531 if (opterr) 673 if (opterr)
532 fprintf (stderr, gettext ("%s: option `%s' is ambiguous\n"), 674 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
533 argv[0], argv[optind]); 675 argv[0], argv[optind]);
534 nextchar += strlen (nextchar); 676 nextchar += strlen (nextchar);
535 optind++; 677 optind++;
678 optopt = 0;
536 return '?'; 679 return '?';
537 } 680 }
538 681
539 if (pfound != NULL) 682 if (pfound != NULL)
540 { 683 {
550 { 693 {
551 if (opterr) 694 if (opterr)
552 if (argv[optind - 1][1] == '-') 695 if (argv[optind - 1][1] == '-')
553 /* --option */ 696 /* --option */
554 fprintf (stderr, 697 fprintf (stderr,
555 gettext ("%s: option `--%s' doesn't allow an argument\n"), 698 _("%s: option `--%s' doesn't allow an argument\n"),
556 argv[0], pfound->name); 699 argv[0], pfound->name);
557 else 700 else
558 /* +option or -option */ 701 /* +option or -option */
559 fprintf (stderr, 702 fprintf (stderr,
560 gettext ("%s: option `%c%s' doesn't allow an argument\n"), 703 _("%s: option `%c%s' doesn't allow an argument\n"),
561 argv[0], argv[optind - 1][0], pfound->name); 704 argv[0], argv[optind - 1][0], pfound->name);
562 705
563 nextchar += strlen (nextchar); 706 nextchar += strlen (nextchar);
707
708 optopt = pfound->val;
564 return '?'; 709 return '?';
565 } 710 }
566 } 711 }
567 else if (pfound->has_arg == 1) 712 else if (pfound->has_arg == 1)
568 { 713 {
570 optarg = argv[optind++]; 715 optarg = argv[optind++];
571 else 716 else
572 { 717 {
573 if (opterr) 718 if (opterr)
574 fprintf (stderr, 719 fprintf (stderr,
575 gettext ("%s: option `%s' requires an argument\n"), 720 _("%s: option `%s' requires an argument\n"),
576 argv[0], argv[optind - 1]); 721 argv[0], argv[optind - 1]);
577 nextchar += strlen (nextchar); 722 nextchar += strlen (nextchar);
723 optopt = pfound->val;
578 return optstring[0] == ':' ? ':' : '?'; 724 return optstring[0] == ':' ? ':' : '?';
579 } 725 }
580 } 726 }
581 nextchar += strlen (nextchar); 727 nextchar += strlen (nextchar);
582 if (longind != NULL) 728 if (longind != NULL)
598 { 744 {
599 if (opterr) 745 if (opterr)
600 { 746 {
601 if (argv[optind][1] == '-') 747 if (argv[optind][1] == '-')
602 /* --option */ 748 /* --option */
603 fprintf (stderr, gettext ("%s: unrecognized option `--%s'\n"), 749 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
604 argv[0], nextchar); 750 argv[0], nextchar);
605 else 751 else
606 /* +option or -option */ 752 /* +option or -option */
607 fprintf (stderr, gettext ("%s: unrecognized option `%c%s'\n"), 753 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
608 argv[0], argv[optind][0], nextchar); 754 argv[0], argv[optind][0], nextchar);
609 } 755 }
610 nextchar = (char *) ""; 756 nextchar = (char *) "";
611 optind++; 757 optind++;
758 optopt = 0;
612 return '?'; 759 return '?';
613 } 760 }
614 } 761 }
615 762
616 /* Look at and handle the next short option-character. */ 763 /* Look at and handle the next short option-character. */
627 { 774 {
628 if (opterr) 775 if (opterr)
629 { 776 {
630 if (posixly_correct) 777 if (posixly_correct)
631 /* 1003.2 specifies the format of this message. */ 778 /* 1003.2 specifies the format of this message. */
632 fprintf (stderr, gettext ("%s: illegal option -- %c\n"), 779 fprintf (stderr, _("%s: illegal option -- %c\n"),
633 argv[0], c); 780 argv[0], c);
634 else 781 else
635 fprintf (stderr, gettext ("%s: invalid option -- %c\n"), 782 fprintf (stderr, _("%s: invalid option -- %c\n"),
636 argv[0], c); 783 argv[0], c);
637 } 784 }
638 optopt = c; 785 optopt = c;
639 return '?'; 786 return '?';
787 }
788 /* Convenience. Treat POSIX -W foo same as long option --foo */
789 if (temp[0] == 'W' && temp[1] == ';')
790 {
791 char *nameend;
792 const struct option *p;
793 const struct option *pfound = NULL;
794 int exact = 0;
795 int ambig = 0;
796 int indfound = 0;
797 int option_index;
798
799 /* This is an option that requires an argument. */
800 if (*nextchar != '\0')
801 {
802 optarg = nextchar;
803 /* If we end this ARGV-element by taking the rest as an arg,
804 we must advance to the next element now. */
805 optind++;
806 }
807 else if (optind == argc)
808 {
809 if (opterr)
810 {
811 /* 1003.2 specifies the format of this message. */
812 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
813 argv[0], c);
814 }
815 optopt = c;
816 if (optstring[0] == ':')
817 c = ':';
818 else
819 c = '?';
820 return c;
821 }
822 else
823 /* We already incremented `optind' once;
824 increment it again when taking next ARGV-elt as argument. */
825 optarg = argv[optind++];
826
827 /* optarg is now the argument, see if it's in the
828 table of longopts. */
829
830 for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
831 /* Do nothing. */ ;
832
833 /* Test all long options for either exact match
834 or abbreviated matches. */
835 for (p = longopts, option_index = 0; p->name; p++, option_index++)
836 if (!strncmp (p->name, nextchar, nameend - nextchar))
837 {
838 if ((unsigned int) (nameend - nextchar) == strlen (p->name))
839 {
840 /* Exact match found. */
841 pfound = p;
842 indfound = option_index;
843 exact = 1;
844 break;
845 }
846 else if (pfound == NULL)
847 {
848 /* First nonexact match found. */
849 pfound = p;
850 indfound = option_index;
851 }
852 else
853 /* Second or later nonexact match found. */
854 ambig = 1;
855 }
856 if (ambig && !exact)
857 {
858 if (opterr)
859 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
860 argv[0], argv[optind]);
861 nextchar += strlen (nextchar);
862 optind++;
863 return '?';
864 }
865 if (pfound != NULL)
866 {
867 option_index = indfound;
868 if (*nameend)
869 {
870 /* Don't test has_arg with >, because some C compilers don't
871 allow it to be used on enums. */
872 if (pfound->has_arg)
873 optarg = nameend + 1;
874 else
875 {
876 if (opterr)
877 fprintf (stderr, _("\
878 %s: option `-W %s' doesn't allow an argument\n"),
879 argv[0], pfound->name);
880
881 nextchar += strlen (nextchar);
882 return '?';
883 }
884 }
885 else if (pfound->has_arg == 1)
886 {
887 if (optind < argc)
888 optarg = argv[optind++];
889 else
890 {
891 if (opterr)
892 fprintf (stderr,
893 _("%s: option `%s' requires an argument\n"),
894 argv[0], argv[optind - 1]);
895 nextchar += strlen (nextchar);
896 return optstring[0] == ':' ? ':' : '?';
897 }
898 }
899 nextchar += strlen (nextchar);
900 if (longind != NULL)
901 *longind = option_index;
902 if (pfound->flag)
903 {
904 *(pfound->flag) = pfound->val;
905 return 0;
906 }
907 return pfound->val;
908 }
909 nextchar = NULL;
910 return 'W'; /* Let the application handle it. */
640 } 911 }
641 if (temp[1] == ':') 912 if (temp[1] == ':')
642 { 913 {
643 if (temp[2] == ':') 914 if (temp[2] == ':')
644 { 915 {
666 { 937 {
667 if (opterr) 938 if (opterr)
668 { 939 {
669 /* 1003.2 specifies the format of this message. */ 940 /* 1003.2 specifies the format of this message. */
670 fprintf (stderr, 941 fprintf (stderr,
671 gettext ("%s: option requires an argument -- %c\n"), 942 _("%s: option requires an argument -- %c\n"),
672 argv[0], c); 943 argv[0], c);
673 } 944 }
674 optopt = c; 945 optopt = c;
675 if (optstring[0] == ':') 946 if (optstring[0] == ':')
676 c = ':'; 947 c = ':';
687 return c; 958 return c;
688 } 959 }
689 } 960 }
690 961
691 int 962 int
692 getopt (argc, argv, optstring) 963 getopt (int argc, char *const *argv, const char *optstring)
693 int argc;
694 char *const *argv;
695 const char *optstring;
696 { 964 {
697 return _getopt_internal (argc, argv, optstring, 965 return _getopt_internal (argc, argv, optstring,
698 (const struct option *) 0, 966 (const struct option *) 0,
699 (int *) 0, 967 (int *) 0,
700 0); 968 0);
701 } 969 }
702 970
703 #endif /* _LIBC or not __GNU_LIBRARY__. */ 971 #endif /* Not ELIDE_CODE. */
704 972
705 #ifdef TEST 973 #ifdef TEST
706 974
707 /* Compile with -DTEST to make an executable for use in testing 975 /* Compile with -DTEST to make an executable for use in testing
708 the above definition of `getopt'. */ 976 the above definition of `getopt'. */
709 977
710 int 978 int
711 main (argc, argv) 979 main (int argc, char **argv)
712 int argc;
713 char **argv;
714 { 980 {
715 int c; 981 int c;
716 int digit_optind = 0; 982 int digit_optind = 0;
717 983
718 while (1) 984 while (1)
719 { 985 {
720 int this_option_optind = optind ? optind : 1; 986 int this_option_optind = optind ? optind : 1;
721 987
722 c = getopt (argc, argv, "abc:d:0123456789"); 988 c = getopt (argc, argv, "abc:d:0123456789");
723 if (c == EOF) 989 if (c == -1)
724 break; 990 break;
725 991
726 switch (c) 992 switch (c)
727 { 993 {
728 case '0': 994 case '0':