diff options
author | Thomas Guyot-Sionnest <dermoth@aei.ca> | 2010-04-07 21:11:46 -0400 |
---|---|---|
committer | Thomas Guyot-Sionnest <dermoth@aei.ca> | 2010-04-12 21:26:35 -0400 |
commit | 74da141e618ef99959d509cb2e7be35a348a39db (patch) | |
tree | 88ebc38b381a1021fc2d74864a71e230ae591c3d /gl/getopt.c | |
parent | c63a4f726a0b6ad8cf6040f947754a81fd4683bb (diff) | |
download | monitoring-plugins-74da141e618ef99959d509cb2e7be35a348a39db.tar.gz |
Sync with the latest Gnulib code (177f525)
Signed-off-by: Thomas Guyot-Sionnest <dermoth@aei.ca>
Diffstat (limited to 'gl/getopt.c')
-rw-r--r-- | gl/getopt.c | 1275 |
1 files changed, 637 insertions, 638 deletions
diff --git a/gl/getopt.c b/gl/getopt.c index f1e6d1f7..aaabc8d1 100644 --- a/gl/getopt.c +++ b/gl/getopt.c | |||
@@ -1,9 +1,9 @@ | |||
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 part of the C library, so if you don't know what |
3 | "Keep this file name-space clean" means, talk to drepper@gnu.org | 3 | "Keep this file name-space clean" means, talk to drepper@gnu.org |
4 | before changing it! | 4 | before changing it! |
5 | Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004,2006,2008 | 5 | Copyright (C) 1987-1996, 1998-2004, 2006, 2008-2010 Free Software |
6 | Free Software Foundation, Inc. | 6 | Foundation, Inc. |
7 | This file is part of the GNU C Library. | 7 | This file is part of the GNU C Library. |
8 | 8 | ||
9 | This program is free software: you can redistribute it and/or modify | 9 | This program is free software: you can redistribute it and/or modify |
@@ -41,12 +41,9 @@ | |||
41 | # include <wchar.h> | 41 | # include <wchar.h> |
42 | #endif | 42 | #endif |
43 | 43 | ||
44 | #ifndef attribute_hidden | 44 | /* This version of `getopt' appears to the caller like standard Unix `getopt' |
45 | # define attribute_hidden | 45 | but it behaves differently for the user, since it allows the user |
46 | #endif | 46 | to intersperse the options with the other arguments. |
47 | |||
48 | /* Unlike standard Unix `getopt', functions like `getopt_long' | ||
49 | let the user intersperse the options with the other arguments. | ||
50 | 47 | ||
51 | As `getopt_long' works, it permutes the elements of ARGV so that, | 48 | As `getopt_long' works, it permutes the elements of ARGV so that, |
52 | when it is done, all the options precede everything else. Thus | 49 | when it is done, all the options precede everything else. Thus |
@@ -54,7 +51,7 @@ | |||
54 | 51 | ||
55 | Using `getopt' or setting the environment variable POSIXLY_CORRECT | 52 | Using `getopt' or setting the environment variable POSIXLY_CORRECT |
56 | disables permutation. | 53 | disables permutation. |
57 | Then the application's behavior is completely standard. | 54 | Then the behavior is completely standard. |
58 | 55 | ||
59 | GNU application programs can use a third alternative mode in which | 56 | GNU application programs can use a third alternative mode in which |
60 | they can distinguish the relative order of options and other arguments. */ | 57 | they can distinguish the relative order of options and other arguments. */ |
@@ -121,18 +118,18 @@ extern char *__getopt_nonoption_flags; | |||
121 | 118 | ||
122 | # ifdef USE_NONOPTION_FLAGS | 119 | # ifdef USE_NONOPTION_FLAGS |
123 | # define SWAP_FLAGS(ch1, ch2) \ | 120 | # define SWAP_FLAGS(ch1, ch2) \ |
124 | if (d->__nonoption_flags_len > 0) \ | 121 | if (d->__nonoption_flags_len > 0) \ |
125 | { \ | 122 | { \ |
126 | char __tmp = __getopt_nonoption_flags[ch1]; \ | 123 | char __tmp = __getopt_nonoption_flags[ch1]; \ |
127 | __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ | 124 | __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ |
128 | __getopt_nonoption_flags[ch2] = __tmp; \ | 125 | __getopt_nonoption_flags[ch2] = __tmp; \ |
129 | } | 126 | } |
130 | # else | 127 | # else |
131 | # define SWAP_FLAGS(ch1, ch2) | 128 | # define SWAP_FLAGS(ch1, ch2) |
132 | # endif | 129 | # endif |
133 | #else /* !_LIBC */ | 130 | #else /* !_LIBC */ |
134 | # define SWAP_FLAGS(ch1, ch2) | 131 | # define SWAP_FLAGS(ch1, ch2) |
135 | #endif /* _LIBC */ | 132 | #endif /* _LIBC */ |
136 | 133 | ||
137 | /* Exchange two adjacent subsequences of ARGV. | 134 | /* Exchange two adjacent subsequences of ARGV. |
138 | One subsequence is elements [first_nonopt,last_nonopt) | 135 | One subsequence is elements [first_nonopt,last_nonopt) |
@@ -163,57 +160,57 @@ exchange (char **argv, struct _getopt_data *d) | |||
163 | if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len) | 160 | if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len) |
164 | { | 161 | { |
165 | /* We must extend the array. The user plays games with us and | 162 | /* We must extend the array. The user plays games with us and |
166 | presents new arguments. */ | 163 | presents new arguments. */ |
167 | char *new_str = malloc (top + 1); | 164 | char *new_str = malloc (top + 1); |
168 | if (new_str == NULL) | 165 | if (new_str == NULL) |
169 | d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0; | 166 | d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0; |
170 | else | 167 | else |
171 | { | 168 | { |
172 | memset (__mempcpy (new_str, __getopt_nonoption_flags, | 169 | memset (__mempcpy (new_str, __getopt_nonoption_flags, |
173 | d->__nonoption_flags_max_len), | 170 | d->__nonoption_flags_max_len), |
174 | '\0', top + 1 - d->__nonoption_flags_max_len); | 171 | '\0', top + 1 - d->__nonoption_flags_max_len); |
175 | d->__nonoption_flags_max_len = top + 1; | 172 | d->__nonoption_flags_max_len = top + 1; |
176 | __getopt_nonoption_flags = new_str; | 173 | __getopt_nonoption_flags = new_str; |
177 | } | 174 | } |
178 | } | 175 | } |
179 | #endif | 176 | #endif |
180 | 177 | ||
181 | while (top > middle && middle > bottom) | 178 | while (top > middle && middle > bottom) |
182 | { | 179 | { |
183 | if (top - middle > middle - bottom) | 180 | if (top - middle > middle - bottom) |
184 | { | 181 | { |
185 | /* Bottom segment is the short one. */ | 182 | /* Bottom segment is the short one. */ |
186 | int len = middle - bottom; | 183 | int len = middle - bottom; |
187 | register int i; | 184 | register int i; |
188 | 185 | ||
189 | /* Swap it with the top part of the top segment. */ | 186 | /* Swap it with the top part of the top segment. */ |
190 | for (i = 0; i < len; i++) | 187 | for (i = 0; i < len; i++) |
191 | { | 188 | { |
192 | tem = argv[bottom + i]; | 189 | tem = argv[bottom + i]; |
193 | argv[bottom + i] = argv[top - (middle - bottom) + i]; | 190 | argv[bottom + i] = argv[top - (middle - bottom) + i]; |
194 | argv[top - (middle - bottom) + i] = tem; | 191 | argv[top - (middle - bottom) + i] = tem; |
195 | SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); | 192 | SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); |
196 | } | 193 | } |
197 | /* Exclude the moved bottom segment from further swapping. */ | 194 | /* Exclude the moved bottom segment from further swapping. */ |
198 | top -= len; | 195 | top -= len; |
199 | } | 196 | } |
200 | else | 197 | else |
201 | { | 198 | { |
202 | /* Top segment is the short one. */ | 199 | /* Top segment is the short one. */ |
203 | int len = top - middle; | 200 | int len = top - middle; |
204 | register int i; | 201 | register int i; |
205 | 202 | ||
206 | /* Swap it with the bottom part of the bottom segment. */ | 203 | /* Swap it with the bottom part of the bottom segment. */ |
207 | for (i = 0; i < len; i++) | 204 | for (i = 0; i < len; i++) |
208 | { | 205 | { |
209 | tem = argv[bottom + i]; | 206 | tem = argv[bottom + i]; |
210 | argv[bottom + i] = argv[middle + i]; | 207 | argv[bottom + i] = argv[middle + i]; |
211 | argv[middle + i] = tem; | 208 | argv[middle + i] = tem; |
212 | SWAP_FLAGS (bottom + i, middle + i); | 209 | SWAP_FLAGS (bottom + i, middle + i); |
213 | } | 210 | } |
214 | /* Exclude the moved top segment from further swapping. */ | 211 | /* Exclude the moved top segment from further swapping. */ |
215 | bottom += len; | 212 | bottom += len; |
216 | } | 213 | } |
217 | } | 214 | } |
218 | 215 | ||
219 | /* Update records for the slots the non-options now occupy. */ | 216 | /* Update records for the slots the non-options now occupy. */ |
@@ -225,8 +222,9 @@ exchange (char **argv, struct _getopt_data *d) | |||
225 | /* Initialize the internal data when the first call is made. */ | 222 | /* Initialize the internal data when the first call is made. */ |
226 | 223 | ||
227 | static const char * | 224 | static const char * |
228 | _getopt_initialize (int argc, char **argv, const char *optstring, | 225 | _getopt_initialize (int argc _GL_UNUSED, |
229 | int posixly_correct, struct _getopt_data *d) | 226 | char **argv _GL_UNUSED, const char *optstring, |
227 | struct _getopt_data *d, int posixly_correct) | ||
230 | { | 228 | { |
231 | /* Start processing options with ARGV-element 1 (since ARGV-element 0 | 229 | /* Start processing options with ARGV-element 1 (since ARGV-element 0 |
232 | is the program name); the sequence of previously skipped | 230 | is the program name); the sequence of previously skipped |
@@ -260,25 +258,25 @@ _getopt_initialize (int argc, char **argv, const char *optstring, | |||
260 | && argc == __libc_argc && argv == __libc_argv) | 258 | && argc == __libc_argc && argv == __libc_argv) |
261 | { | 259 | { |
262 | if (d->__nonoption_flags_max_len == 0) | 260 | if (d->__nonoption_flags_max_len == 0) |
263 | { | 261 | { |
264 | if (__getopt_nonoption_flags == NULL | 262 | if (__getopt_nonoption_flags == NULL |
265 | || __getopt_nonoption_flags[0] == '\0') | 263 | || __getopt_nonoption_flags[0] == '\0') |
266 | d->__nonoption_flags_max_len = -1; | 264 | d->__nonoption_flags_max_len = -1; |
267 | else | 265 | else |
268 | { | 266 | { |
269 | const char *orig_str = __getopt_nonoption_flags; | 267 | const char *orig_str = __getopt_nonoption_flags; |
270 | int len = d->__nonoption_flags_max_len = strlen (orig_str); | 268 | int len = d->__nonoption_flags_max_len = strlen (orig_str); |
271 | if (d->__nonoption_flags_max_len < argc) | 269 | if (d->__nonoption_flags_max_len < argc) |
272 | d->__nonoption_flags_max_len = argc; | 270 | d->__nonoption_flags_max_len = argc; |
273 | __getopt_nonoption_flags = | 271 | __getopt_nonoption_flags = |
274 | (char *) malloc (d->__nonoption_flags_max_len); | 272 | (char *) malloc (d->__nonoption_flags_max_len); |
275 | if (__getopt_nonoption_flags == NULL) | 273 | if (__getopt_nonoption_flags == NULL) |
276 | d->__nonoption_flags_max_len = -1; | 274 | d->__nonoption_flags_max_len = -1; |
277 | else | 275 | else |
278 | memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), | 276 | memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), |
279 | '\0', d->__nonoption_flags_max_len - len); | 277 | '\0', d->__nonoption_flags_max_len - len); |
280 | } | 278 | } |
281 | } | 279 | } |
282 | d->__nonoption_flags_len = d->__nonoption_flags_max_len; | 280 | d->__nonoption_flags_len = d->__nonoption_flags_max_len; |
283 | } | 281 | } |
284 | else | 282 | else |
@@ -330,6 +328,10 @@ _getopt_initialize (int argc, char **argv, const char *optstring, | |||
330 | `flag' field is nonzero, the value of the option's `val' field | 328 | `flag' field is nonzero, the value of the option's `val' field |
331 | if the `flag' field is zero. | 329 | if the `flag' field is zero. |
332 | 330 | ||
331 | The elements of ARGV aren't really const, because we permute them. | ||
332 | But we pretend they're const in the prototype to be compatible | ||
333 | with other systems. | ||
334 | |||
333 | LONGOPTS is a vector of `struct option' terminated by an | 335 | LONGOPTS is a vector of `struct option' terminated by an |
334 | element containing a name which is zero. | 336 | element containing a name which is zero. |
335 | 337 | ||
@@ -338,15 +340,12 @@ _getopt_initialize (int argc, char **argv, const char *optstring, | |||
338 | recent call. | 340 | recent call. |
339 | 341 | ||
340 | If LONG_ONLY is nonzero, '-' as well as '--' can introduce | 342 | If LONG_ONLY is nonzero, '-' as well as '--' can introduce |
341 | long-named options. | 343 | long-named options. */ |
342 | |||
343 | If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT | ||
344 | environment variable were set. */ | ||
345 | 344 | ||
346 | int | 345 | int |
347 | _getopt_internal_r (int argc, char **argv, const char *optstring, | 346 | _getopt_internal_r (int argc, char **argv, const char *optstring, |
348 | const struct option *longopts, int *longind, | 347 | const struct option *longopts, int *longind, |
349 | int long_only, int posixly_correct, struct _getopt_data *d) | 348 | int long_only, struct _getopt_data *d, int posixly_correct) |
350 | { | 349 | { |
351 | int print_errors = d->opterr; | 350 | int print_errors = d->opterr; |
352 | if (optstring[0] == ':') | 351 | if (optstring[0] == ':') |
@@ -360,9 +359,9 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, | |||
360 | if (d->optind == 0 || !d->__initialized) | 359 | if (d->optind == 0 || !d->__initialized) |
361 | { | 360 | { |
362 | if (d->optind == 0) | 361 | if (d->optind == 0) |
363 | d->optind = 1; /* Don't scan ARGV[0], the program name. */ | 362 | d->optind = 1; /* Don't scan ARGV[0], the program name. */ |
364 | optstring = _getopt_initialize (argc, argv, optstring, | 363 | optstring = _getopt_initialize (argc, argv, optstring, d, |
365 | posixly_correct, d); | 364 | posixly_correct); |
366 | d->__initialized = 1; | 365 | d->__initialized = 1; |
367 | } | 366 | } |
368 | 367 | ||
@@ -372,8 +371,8 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, | |||
372 | is only used when the used in the GNU libc. */ | 371 | is only used when the used in the GNU libc. */ |
373 | #if defined _LIBC && defined USE_NONOPTION_FLAGS | 372 | #if defined _LIBC && defined USE_NONOPTION_FLAGS |
374 | # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \ | 373 | # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \ |
375 | || (d->optind < d->__nonoption_flags_len \ | 374 | || (d->optind < d->__nonoption_flags_len \ |
376 | && __getopt_nonoption_flags[d->optind] == '1')) | 375 | && __getopt_nonoption_flags[d->optind] == '1')) |
377 | #else | 376 | #else |
378 | # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0') | 377 | # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0') |
379 | #endif | 378 | #endif |
@@ -383,78 +382,78 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, | |||
383 | /* Advance to the next ARGV-element. */ | 382 | /* Advance to the next ARGV-element. */ |
384 | 383 | ||
385 | /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been | 384 | /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been |
386 | moved back by the user (who may also have changed the arguments). */ | 385 | moved back by the user (who may also have changed the arguments). */ |
387 | if (d->__last_nonopt > d->optind) | 386 | if (d->__last_nonopt > d->optind) |
388 | d->__last_nonopt = d->optind; | 387 | d->__last_nonopt = d->optind; |
389 | if (d->__first_nonopt > d->optind) | 388 | if (d->__first_nonopt > d->optind) |
390 | d->__first_nonopt = d->optind; | 389 | d->__first_nonopt = d->optind; |
391 | 390 | ||
392 | if (d->__ordering == PERMUTE) | 391 | if (d->__ordering == PERMUTE) |
393 | { | 392 | { |
394 | /* If we have just processed some options following some non-options, | 393 | /* If we have just processed some options following some non-options, |
395 | exchange them so that the options come first. */ | 394 | exchange them so that the options come first. */ |
396 | 395 | ||
397 | if (d->__first_nonopt != d->__last_nonopt | 396 | if (d->__first_nonopt != d->__last_nonopt |
398 | && d->__last_nonopt != d->optind) | 397 | && d->__last_nonopt != d->optind) |
399 | exchange ((char **) argv, d); | 398 | exchange ((char **) argv, d); |
400 | else if (d->__last_nonopt != d->optind) | 399 | else if (d->__last_nonopt != d->optind) |
401 | d->__first_nonopt = d->optind; | 400 | d->__first_nonopt = d->optind; |
402 | 401 | ||
403 | /* Skip any additional non-options | 402 | /* Skip any additional non-options |
404 | and extend the range of non-options previously skipped. */ | 403 | and extend the range of non-options previously skipped. */ |
405 | 404 | ||
406 | while (d->optind < argc && NONOPTION_P) | 405 | while (d->optind < argc && NONOPTION_P) |
407 | d->optind++; | 406 | d->optind++; |
408 | d->__last_nonopt = d->optind; | 407 | d->__last_nonopt = d->optind; |
409 | } | 408 | } |
410 | 409 | ||
411 | /* The special ARGV-element `--' means premature end of options. | 410 | /* The special ARGV-element `--' means premature end of options. |
412 | Skip it like a null option, | 411 | Skip it like a null option, |
413 | then exchange with previous non-options as if it were an option, | 412 | then exchange with previous non-options as if it were an option, |
414 | then skip everything else like a non-option. */ | 413 | then skip everything else like a non-option. */ |
415 | 414 | ||
416 | if (d->optind != argc && !strcmp (argv[d->optind], "--")) | 415 | if (d->optind != argc && !strcmp (argv[d->optind], "--")) |
417 | { | 416 | { |
418 | d->optind++; | 417 | d->optind++; |
419 | 418 | ||
420 | if (d->__first_nonopt != d->__last_nonopt | 419 | if (d->__first_nonopt != d->__last_nonopt |
421 | && d->__last_nonopt != d->optind) | 420 | && d->__last_nonopt != d->optind) |
422 | exchange ((char **) argv, d); | 421 | exchange ((char **) argv, d); |
423 | else if (d->__first_nonopt == d->__last_nonopt) | 422 | else if (d->__first_nonopt == d->__last_nonopt) |
424 | d->__first_nonopt = d->optind; | 423 | d->__first_nonopt = d->optind; |
425 | d->__last_nonopt = argc; | 424 | d->__last_nonopt = argc; |
426 | 425 | ||
427 | d->optind = argc; | 426 | d->optind = argc; |
428 | } | 427 | } |
429 | 428 | ||
430 | /* If we have done all the ARGV-elements, stop the scan | 429 | /* If we have done all the ARGV-elements, stop the scan |
431 | and back over any non-options that we skipped and permuted. */ | 430 | and back over any non-options that we skipped and permuted. */ |
432 | 431 | ||
433 | if (d->optind == argc) | 432 | if (d->optind == argc) |
434 | { | 433 | { |
435 | /* Set the next-arg-index to point at the non-options | 434 | /* Set the next-arg-index to point at the non-options |
436 | that we previously skipped, so the caller will digest them. */ | 435 | that we previously skipped, so the caller will digest them. */ |
437 | if (d->__first_nonopt != d->__last_nonopt) | 436 | if (d->__first_nonopt != d->__last_nonopt) |
438 | d->optind = d->__first_nonopt; | 437 | d->optind = d->__first_nonopt; |
439 | return -1; | 438 | return -1; |
440 | } | 439 | } |
441 | 440 | ||
442 | /* If we have come to a non-option and did not permute it, | 441 | /* If we have come to a non-option and did not permute it, |
443 | either stop the scan or describe it to the caller and pass it by. */ | 442 | either stop the scan or describe it to the caller and pass it by. */ |
444 | 443 | ||
445 | if (NONOPTION_P) | 444 | if (NONOPTION_P) |
446 | { | 445 | { |
447 | if (d->__ordering == REQUIRE_ORDER) | 446 | if (d->__ordering == REQUIRE_ORDER) |
448 | return -1; | 447 | return -1; |
449 | d->optarg = argv[d->optind++]; | 448 | d->optarg = argv[d->optind++]; |
450 | return 1; | 449 | return 1; |
451 | } | 450 | } |
452 | 451 | ||
453 | /* We have found another option-ARGV-element. | 452 | /* We have found another option-ARGV-element. |
454 | Skip the initial punctuation. */ | 453 | Skip the initial punctuation. */ |
455 | 454 | ||
456 | d->__nextchar = (argv[d->optind] + 1 | 455 | d->__nextchar = (argv[d->optind] + 1 |
457 | + (longopts != NULL && argv[d->optind][1] == '-')); | 456 | + (longopts != NULL && argv[d->optind][1] == '-')); |
458 | } | 457 | } |
459 | 458 | ||
460 | /* Decode the current option-ARGV-element. */ | 459 | /* Decode the current option-ARGV-element. */ |
@@ -474,8 +473,8 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, | |||
474 | 473 | ||
475 | if (longopts != NULL | 474 | if (longopts != NULL |
476 | && (argv[d->optind][1] == '-' | 475 | && (argv[d->optind][1] == '-' |
477 | || (long_only && (argv[d->optind][2] | 476 | || (long_only && (argv[d->optind][2] |
478 | || !strchr (optstring, argv[d->optind][1]))))) | 477 | || !strchr (optstring, argv[d->optind][1]))))) |
479 | { | 478 | { |
480 | char *nameend; | 479 | char *nameend; |
481 | const struct option *p; | 480 | const struct option *p; |
@@ -486,251 +485,251 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, | |||
486 | int option_index; | 485 | int option_index; |
487 | 486 | ||
488 | for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++) | 487 | for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++) |
489 | /* Do nothing. */ ; | 488 | /* Do nothing. */ ; |
490 | 489 | ||
491 | /* Test all long options for either exact match | 490 | /* Test all long options for either exact match |
492 | or abbreviated matches. */ | 491 | or abbreviated matches. */ |
493 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | 492 | for (p = longopts, option_index = 0; p->name; p++, option_index++) |
494 | if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar)) | 493 | if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar)) |
495 | { | 494 | { |
496 | if ((unsigned int) (nameend - d->__nextchar) | 495 | if ((unsigned int) (nameend - d->__nextchar) |
497 | == (unsigned int) strlen (p->name)) | 496 | == (unsigned int) strlen (p->name)) |
498 | { | 497 | { |
499 | /* Exact match found. */ | 498 | /* Exact match found. */ |
500 | pfound = p; | 499 | pfound = p; |
501 | indfound = option_index; | 500 | indfound = option_index; |
502 | exact = 1; | 501 | exact = 1; |
503 | break; | 502 | break; |
504 | } | 503 | } |
505 | else if (pfound == NULL) | 504 | else if (pfound == NULL) |
506 | { | 505 | { |
507 | /* First nonexact match found. */ | 506 | /* First nonexact match found. */ |
508 | pfound = p; | 507 | pfound = p; |
509 | indfound = option_index; | 508 | indfound = option_index; |
510 | } | 509 | } |
511 | else if (long_only | 510 | else if (long_only |
512 | || pfound->has_arg != p->has_arg | 511 | || pfound->has_arg != p->has_arg |
513 | || pfound->flag != p->flag | 512 | || pfound->flag != p->flag |
514 | || pfound->val != p->val) | 513 | || pfound->val != p->val) |
515 | /* Second or later nonexact match found. */ | 514 | /* Second or later nonexact match found. */ |
516 | ambig = 1; | 515 | ambig = 1; |
517 | } | 516 | } |
518 | 517 | ||
519 | if (ambig && !exact) | 518 | if (ambig && !exact) |
520 | { | 519 | { |
521 | if (print_errors) | 520 | if (print_errors) |
522 | { | 521 | { |
523 | #if defined _LIBC && defined USE_IN_LIBIO | 522 | #if defined _LIBC && defined USE_IN_LIBIO |
524 | char *buf; | 523 | char *buf; |
525 | 524 | ||
526 | if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"), | 525 | if (__asprintf (&buf, _("%s: option '%s' is ambiguous\n"), |
527 | argv[0], argv[d->optind]) >= 0) | 526 | argv[0], argv[d->optind]) >= 0) |
528 | { | 527 | { |
529 | _IO_flockfile (stderr); | 528 | _IO_flockfile (stderr); |
530 | 529 | ||
531 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; | 530 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
532 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; | 531 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
533 | 532 | ||
534 | __fxprintf (NULL, "%s", buf); | 533 | __fxprintf (NULL, "%s", buf); |
535 | 534 | ||
536 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; | 535 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; |
537 | _IO_funlockfile (stderr); | 536 | _IO_funlockfile (stderr); |
538 | 537 | ||
539 | free (buf); | 538 | free (buf); |
540 | } | 539 | } |
541 | #else | 540 | #else |
542 | fprintf (stderr, _("%s: option `%s' is ambiguous\n"), | 541 | fprintf (stderr, _("%s: option '%s' is ambiguous\n"), |
543 | argv[0], argv[d->optind]); | 542 | argv[0], argv[d->optind]); |
544 | #endif | 543 | #endif |
545 | } | 544 | } |
546 | d->__nextchar += strlen (d->__nextchar); | 545 | d->__nextchar += strlen (d->__nextchar); |
547 | d->optind++; | 546 | d->optind++; |
548 | d->optopt = 0; | 547 | d->optopt = 0; |
549 | return '?'; | 548 | return '?'; |
550 | } | 549 | } |
551 | 550 | ||
552 | if (pfound != NULL) | 551 | if (pfound != NULL) |
553 | { | 552 | { |
554 | option_index = indfound; | 553 | option_index = indfound; |
555 | d->optind++; | 554 | d->optind++; |
556 | if (*nameend) | 555 | if (*nameend) |
557 | { | 556 | { |
558 | /* Don't test has_arg with >, because some C compilers don't | 557 | /* Don't test has_arg with >, because some C compilers don't |
559 | allow it to be used on enums. */ | 558 | allow it to be used on enums. */ |
560 | if (pfound->has_arg) | 559 | if (pfound->has_arg) |
561 | d->optarg = nameend + 1; | 560 | d->optarg = nameend + 1; |
562 | else | 561 | else |
563 | { | 562 | { |
564 | if (print_errors) | 563 | if (print_errors) |
565 | { | 564 | { |
566 | #if defined _LIBC && defined USE_IN_LIBIO | 565 | #if defined _LIBC && defined USE_IN_LIBIO |
567 | char *buf; | 566 | char *buf; |
568 | int n; | 567 | int n; |
569 | #endif | 568 | #endif |
570 | 569 | ||
571 | if (argv[d->optind - 1][1] == '-') | 570 | if (argv[d->optind - 1][1] == '-') |
572 | { | 571 | { |
573 | /* --option */ | 572 | /* --option */ |
574 | #if defined _LIBC && defined USE_IN_LIBIO | 573 | #if defined _LIBC && defined USE_IN_LIBIO |
575 | n = __asprintf (&buf, _("\ | 574 | n = __asprintf (&buf, _("\ |
576 | %s: option `--%s' doesn't allow an argument\n"), | 575 | %s: option '--%s' doesn't allow an argument\n"), |
577 | argv[0], pfound->name); | 576 | argv[0], pfound->name); |
578 | #else | 577 | #else |
579 | fprintf (stderr, _("\ | 578 | fprintf (stderr, _("\ |
580 | %s: option `--%s' doesn't allow an argument\n"), | 579 | %s: option '--%s' doesn't allow an argument\n"), |
581 | argv[0], pfound->name); | 580 | argv[0], pfound->name); |
582 | #endif | 581 | #endif |
583 | } | 582 | } |
584 | else | 583 | else |
585 | { | 584 | { |
586 | /* +option or -option */ | 585 | /* +option or -option */ |
587 | #if defined _LIBC && defined USE_IN_LIBIO | 586 | #if defined _LIBC && defined USE_IN_LIBIO |
588 | n = __asprintf (&buf, _("\ | 587 | n = __asprintf (&buf, _("\ |
589 | %s: option `%c%s' doesn't allow an argument\n"), | 588 | %s: option '%c%s' doesn't allow an argument\n"), |
590 | argv[0], argv[d->optind - 1][0], | 589 | argv[0], argv[d->optind - 1][0], |
591 | pfound->name); | 590 | pfound->name); |
592 | #else | 591 | #else |
593 | fprintf (stderr, _("\ | 592 | fprintf (stderr, _("\ |
594 | %s: option `%c%s' doesn't allow an argument\n"), | 593 | %s: option '%c%s' doesn't allow an argument\n"), |
595 | argv[0], argv[d->optind - 1][0], | 594 | argv[0], argv[d->optind - 1][0], |
596 | pfound->name); | 595 | pfound->name); |
597 | #endif | 596 | #endif |
598 | } | 597 | } |
599 | 598 | ||
600 | #if defined _LIBC && defined USE_IN_LIBIO | 599 | #if defined _LIBC && defined USE_IN_LIBIO |
601 | if (n >= 0) | 600 | if (n >= 0) |
602 | { | 601 | { |
603 | _IO_flockfile (stderr); | 602 | _IO_flockfile (stderr); |
604 | 603 | ||
605 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; | 604 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
606 | ((_IO_FILE *) stderr)->_flags2 | 605 | ((_IO_FILE *) stderr)->_flags2 |
607 | |= _IO_FLAGS2_NOTCANCEL; | 606 | |= _IO_FLAGS2_NOTCANCEL; |
608 | 607 | ||
609 | __fxprintf (NULL, "%s", buf); | 608 | __fxprintf (NULL, "%s", buf); |
610 | 609 | ||
611 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; | 610 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; |
612 | _IO_funlockfile (stderr); | 611 | _IO_funlockfile (stderr); |
613 | 612 | ||
614 | free (buf); | 613 | free (buf); |
615 | } | 614 | } |
616 | #endif | 615 | #endif |
617 | } | 616 | } |
618 | 617 | ||
619 | d->__nextchar += strlen (d->__nextchar); | 618 | d->__nextchar += strlen (d->__nextchar); |
620 | 619 | ||
621 | d->optopt = pfound->val; | 620 | d->optopt = pfound->val; |
622 | return '?'; | 621 | return '?'; |
623 | } | 622 | } |
624 | } | 623 | } |
625 | else if (pfound->has_arg == 1) | 624 | else if (pfound->has_arg == 1) |
626 | { | 625 | { |
627 | if (d->optind < argc) | 626 | if (d->optind < argc) |
628 | d->optarg = argv[d->optind++]; | 627 | d->optarg = argv[d->optind++]; |
629 | else | 628 | else |
630 | { | 629 | { |
631 | if (print_errors) | 630 | if (print_errors) |
632 | { | 631 | { |
633 | #if defined _LIBC && defined USE_IN_LIBIO | 632 | #if defined _LIBC && defined USE_IN_LIBIO |
634 | char *buf; | 633 | char *buf; |
635 | 634 | ||
636 | if (__asprintf (&buf, _("\ | 635 | if (__asprintf (&buf, _("\ |
637 | %s: option `%s' requires an argument\n"), | 636 | %s: option '%s' requires an argument\n"), |
638 | argv[0], argv[d->optind - 1]) >= 0) | 637 | argv[0], argv[d->optind - 1]) >= 0) |
639 | { | 638 | { |
640 | _IO_flockfile (stderr); | 639 | _IO_flockfile (stderr); |
641 | 640 | ||
642 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; | 641 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
643 | ((_IO_FILE *) stderr)->_flags2 | 642 | ((_IO_FILE *) stderr)->_flags2 |
644 | |= _IO_FLAGS2_NOTCANCEL; | 643 | |= _IO_FLAGS2_NOTCANCEL; |
645 | 644 | ||
646 | __fxprintf (NULL, "%s", buf); | 645 | __fxprintf (NULL, "%s", buf); |
647 | 646 | ||
648 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; | 647 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; |
649 | _IO_funlockfile (stderr); | 648 | _IO_funlockfile (stderr); |
650 | 649 | ||
651 | free (buf); | 650 | free (buf); |
652 | } | 651 | } |
653 | #else | 652 | #else |
654 | fprintf (stderr, | 653 | fprintf (stderr, |
655 | _("%s: option `%s' requires an argument\n"), | 654 | _("%s: option '%s' requires an argument\n"), |
656 | argv[0], argv[d->optind - 1]); | 655 | argv[0], argv[d->optind - 1]); |
657 | #endif | 656 | #endif |
658 | } | 657 | } |
659 | d->__nextchar += strlen (d->__nextchar); | 658 | d->__nextchar += strlen (d->__nextchar); |
660 | d->optopt = pfound->val; | 659 | d->optopt = pfound->val; |
661 | return optstring[0] == ':' ? ':' : '?'; | 660 | return optstring[0] == ':' ? ':' : '?'; |
662 | } | 661 | } |
663 | } | 662 | } |
664 | d->__nextchar += strlen (d->__nextchar); | 663 | d->__nextchar += strlen (d->__nextchar); |
665 | if (longind != NULL) | 664 | if (longind != NULL) |
666 | *longind = option_index; | 665 | *longind = option_index; |
667 | if (pfound->flag) | 666 | if (pfound->flag) |
668 | { | 667 | { |
669 | *(pfound->flag) = pfound->val; | 668 | *(pfound->flag) = pfound->val; |
670 | return 0; | 669 | return 0; |
671 | } | 670 | } |
672 | return pfound->val; | 671 | return pfound->val; |
673 | } | 672 | } |
674 | 673 | ||
675 | /* Can't find it as a long option. If this is not getopt_long_only, | 674 | /* Can't find it as a long option. If this is not getopt_long_only, |
676 | or the option starts with '--' or is not a valid short | 675 | or the option starts with '--' or is not a valid short |
677 | option, then it's an error. | 676 | option, then it's an error. |
678 | Otherwise interpret it as a short option. */ | 677 | Otherwise interpret it as a short option. */ |
679 | if (!long_only || argv[d->optind][1] == '-' | 678 | if (!long_only || argv[d->optind][1] == '-' |
680 | || strchr (optstring, *d->__nextchar) == NULL) | 679 | || strchr (optstring, *d->__nextchar) == NULL) |
681 | { | 680 | { |
682 | if (print_errors) | 681 | if (print_errors) |
683 | { | 682 | { |
684 | #if defined _LIBC && defined USE_IN_LIBIO | 683 | #if defined _LIBC && defined USE_IN_LIBIO |
685 | char *buf; | 684 | char *buf; |
686 | int n; | 685 | int n; |
687 | #endif | 686 | #endif |
688 | 687 | ||
689 | if (argv[d->optind][1] == '-') | 688 | if (argv[d->optind][1] == '-') |
690 | { | 689 | { |
691 | /* --option */ | 690 | /* --option */ |
692 | #if defined _LIBC && defined USE_IN_LIBIO | 691 | #if defined _LIBC && defined USE_IN_LIBIO |
693 | n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"), | 692 | n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"), |
694 | argv[0], d->__nextchar); | 693 | argv[0], d->__nextchar); |
695 | #else | 694 | #else |
696 | fprintf (stderr, _("%s: unrecognized option `--%s'\n"), | 695 | fprintf (stderr, _("%s: unrecognized option '--%s'\n"), |
697 | argv[0], d->__nextchar); | 696 | argv[0], d->__nextchar); |
698 | #endif | 697 | #endif |
699 | } | 698 | } |
700 | else | 699 | else |
701 | { | 700 | { |
702 | /* +option or -option */ | 701 | /* +option or -option */ |
703 | #if defined _LIBC && defined USE_IN_LIBIO | 702 | #if defined _LIBC && defined USE_IN_LIBIO |
704 | n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"), | 703 | n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"), |
705 | argv[0], argv[d->optind][0], d->__nextchar); | 704 | argv[0], argv[d->optind][0], d->__nextchar); |
706 | #else | 705 | #else |
707 | fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), | 706 | fprintf (stderr, _("%s: unrecognized option '%c%s'\n"), |
708 | argv[0], argv[d->optind][0], d->__nextchar); | 707 | argv[0], argv[d->optind][0], d->__nextchar); |
709 | #endif | 708 | #endif |
710 | } | 709 | } |
711 | 710 | ||
712 | #if defined _LIBC && defined USE_IN_LIBIO | 711 | #if defined _LIBC && defined USE_IN_LIBIO |
713 | if (n >= 0) | 712 | if (n >= 0) |
714 | { | 713 | { |
715 | _IO_flockfile (stderr); | 714 | _IO_flockfile (stderr); |
716 | 715 | ||
717 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; | 716 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
718 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; | 717 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
719 | 718 | ||
720 | __fxprintf (NULL, "%s", buf); | 719 | __fxprintf (NULL, "%s", buf); |
721 | 720 | ||
722 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; | 721 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; |
723 | _IO_funlockfile (stderr); | 722 | _IO_funlockfile (stderr); |
724 | 723 | ||
725 | free (buf); | 724 | free (buf); |
726 | } | 725 | } |
727 | #endif | 726 | #endif |
728 | } | 727 | } |
729 | d->__nextchar = (char *) ""; | 728 | d->__nextchar = (char *) ""; |
730 | d->optind++; | 729 | d->optind++; |
731 | d->optopt = 0; | 730 | d->optopt = 0; |
732 | return '?'; | 731 | return '?'; |
733 | } | 732 | } |
734 | } | 733 | } |
735 | 734 | ||
736 | /* Look at and handle the next short option-character. */ | 735 | /* Look at and handle the next short option-character. */ |
@@ -745,335 +744,321 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, | |||
745 | 744 | ||
746 | if (temp == NULL || c == ':') | 745 | if (temp == NULL || c == ':') |
747 | { | 746 | { |
748 | if (print_errors) | 747 | if (print_errors) |
749 | { | 748 | { |
750 | #if defined _LIBC && defined USE_IN_LIBIO | 749 | #if defined _LIBC && defined USE_IN_LIBIO |
751 | char *buf; | 750 | char *buf; |
752 | int n; | 751 | int n; |
753 | #endif | 752 | #endif |
754 | 753 | ||
755 | if (d->__posixly_correct) | ||
756 | { | ||
757 | /* 1003.2 specifies the format of this message. */ | ||
758 | #if defined _LIBC && defined USE_IN_LIBIO | ||
759 | n = __asprintf (&buf, _("%s: illegal option -- %c\n"), | ||
760 | argv[0], c); | ||
761 | #else | ||
762 | fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c); | ||
763 | #endif | ||
764 | } | ||
765 | else | ||
766 | { | ||
767 | #if defined _LIBC && defined USE_IN_LIBIO | 754 | #if defined _LIBC && defined USE_IN_LIBIO |
768 | n = __asprintf (&buf, _("%s: invalid option -- %c\n"), | 755 | n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"), |
769 | argv[0], c); | 756 | argv[0], c); |
770 | #else | 757 | #else |
771 | fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c); | 758 | fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c); |
772 | #endif | 759 | #endif |
773 | } | ||
774 | 760 | ||
775 | #if defined _LIBC && defined USE_IN_LIBIO | 761 | #if defined _LIBC && defined USE_IN_LIBIO |
776 | if (n >= 0) | 762 | if (n >= 0) |
777 | { | 763 | { |
778 | _IO_flockfile (stderr); | 764 | _IO_flockfile (stderr); |
779 | 765 | ||
780 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; | 766 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
781 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; | 767 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
782 | 768 | ||
783 | __fxprintf (NULL, "%s", buf); | 769 | __fxprintf (NULL, "%s", buf); |
784 | 770 | ||
785 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; | 771 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; |
786 | _IO_funlockfile (stderr); | 772 | _IO_funlockfile (stderr); |
787 | 773 | ||
788 | free (buf); | 774 | free (buf); |
789 | } | 775 | } |
790 | #endif | 776 | #endif |
791 | } | 777 | } |
792 | d->optopt = c; | 778 | d->optopt = c; |
793 | return '?'; | 779 | return '?'; |
794 | } | 780 | } |
795 | /* Convenience. Treat POSIX -W foo same as long option --foo */ | 781 | /* Convenience. Treat POSIX -W foo same as long option --foo */ |
796 | if (temp[0] == 'W' && temp[1] == ';') | 782 | if (temp[0] == 'W' && temp[1] == ';') |
797 | { | 783 | { |
798 | char *nameend; | 784 | char *nameend; |
799 | const struct option *p; | 785 | const struct option *p; |
800 | const struct option *pfound = NULL; | 786 | const struct option *pfound = NULL; |
801 | int exact = 0; | 787 | int exact = 0; |
802 | int ambig = 0; | 788 | int ambig = 0; |
803 | int indfound = 0; | 789 | int indfound = 0; |
804 | int option_index; | 790 | int option_index; |
805 | 791 | ||
806 | /* This is an option that requires an argument. */ | 792 | /* This is an option that requires an argument. */ |
807 | if (*d->__nextchar != '\0') | 793 | if (*d->__nextchar != '\0') |
808 | { | 794 | { |
809 | d->optarg = d->__nextchar; | 795 | d->optarg = d->__nextchar; |
810 | /* If we end this ARGV-element by taking the rest as an arg, | 796 | /* If we end this ARGV-element by taking the rest as an arg, |
811 | we must advance to the next element now. */ | 797 | we must advance to the next element now. */ |
812 | d->optind++; | 798 | d->optind++; |
813 | } | 799 | } |
814 | else if (d->optind == argc) | 800 | else if (d->optind == argc) |
815 | { | 801 | { |
816 | if (print_errors) | 802 | if (print_errors) |
817 | { | 803 | { |
818 | /* 1003.2 specifies the format of this message. */ | ||
819 | #if defined _LIBC && defined USE_IN_LIBIO | 804 | #if defined _LIBC && defined USE_IN_LIBIO |
820 | char *buf; | 805 | char *buf; |
821 | 806 | ||
822 | if (__asprintf (&buf, | 807 | if (__asprintf (&buf, |
823 | _("%s: option requires an argument -- %c\n"), | 808 | _("%s: option requires an argument -- '%c'\n"), |
824 | argv[0], c) >= 0) | 809 | argv[0], c) >= 0) |
825 | { | 810 | { |
826 | _IO_flockfile (stderr); | 811 | _IO_flockfile (stderr); |
827 | 812 | ||
828 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; | 813 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
829 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; | 814 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
830 | 815 | ||
831 | __fxprintf (NULL, "%s", buf); | 816 | __fxprintf (NULL, "%s", buf); |
832 | 817 | ||
833 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; | 818 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; |
834 | _IO_funlockfile (stderr); | 819 | _IO_funlockfile (stderr); |
835 | 820 | ||
836 | free (buf); | 821 | free (buf); |
837 | } | 822 | } |
838 | #else | 823 | #else |
839 | fprintf (stderr, _("%s: option requires an argument -- %c\n"), | 824 | fprintf (stderr, |
840 | argv[0], c); | 825 | _("%s: option requires an argument -- '%c'\n"), |
826 | argv[0], c); | ||
841 | #endif | 827 | #endif |
842 | } | 828 | } |
843 | d->optopt = c; | 829 | d->optopt = c; |
844 | if (optstring[0] == ':') | 830 | if (optstring[0] == ':') |
845 | c = ':'; | 831 | c = ':'; |
846 | else | 832 | else |
847 | c = '?'; | 833 | c = '?'; |
848 | return c; | 834 | return c; |
849 | } | 835 | } |
850 | else | 836 | else |
851 | /* We already incremented `d->optind' once; | 837 | /* We already incremented `d->optind' once; |
852 | increment it again when taking next ARGV-elt as argument. */ | 838 | increment it again when taking next ARGV-elt as argument. */ |
853 | d->optarg = argv[d->optind++]; | 839 | d->optarg = argv[d->optind++]; |
854 | 840 | ||
855 | /* optarg is now the argument, see if it's in the | 841 | /* optarg is now the argument, see if it's in the |
856 | table of longopts. */ | 842 | table of longopts. */ |
857 | 843 | ||
858 | for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '='; | 844 | for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '='; |
859 | nameend++) | 845 | nameend++) |
860 | /* Do nothing. */ ; | 846 | /* Do nothing. */ ; |
861 | 847 | ||
862 | /* Test all long options for either exact match | 848 | /* Test all long options for either exact match |
863 | or abbreviated matches. */ | 849 | or abbreviated matches. */ |
864 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | 850 | for (p = longopts, option_index = 0; p->name; p++, option_index++) |
865 | if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar)) | 851 | if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar)) |
866 | { | 852 | { |
867 | if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name)) | 853 | if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name)) |
868 | { | 854 | { |
869 | /* Exact match found. */ | 855 | /* Exact match found. */ |
870 | pfound = p; | 856 | pfound = p; |
871 | indfound = option_index; | 857 | indfound = option_index; |
872 | exact = 1; | 858 | exact = 1; |
873 | break; | 859 | break; |
874 | } | 860 | } |
875 | else if (pfound == NULL) | 861 | else if (pfound == NULL) |
876 | { | 862 | { |
877 | /* First nonexact match found. */ | 863 | /* First nonexact match found. */ |
878 | pfound = p; | 864 | pfound = p; |
879 | indfound = option_index; | 865 | indfound = option_index; |
880 | } | 866 | } |
881 | else | 867 | else |
882 | /* Second or later nonexact match found. */ | 868 | /* Second or later nonexact match found. */ |
883 | ambig = 1; | 869 | ambig = 1; |
884 | } | 870 | } |
885 | if (ambig && !exact) | 871 | if (ambig && !exact) |
886 | { | 872 | { |
887 | if (print_errors) | 873 | if (print_errors) |
888 | { | 874 | { |
889 | #if defined _LIBC && defined USE_IN_LIBIO | 875 | #if defined _LIBC && defined USE_IN_LIBIO |
890 | char *buf; | 876 | char *buf; |
891 | 877 | ||
892 | if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"), | 878 | if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"), |
893 | argv[0], argv[d->optind]) >= 0) | 879 | argv[0], argv[d->optind]) >= 0) |
894 | { | 880 | { |
895 | _IO_flockfile (stderr); | 881 | _IO_flockfile (stderr); |
896 | 882 | ||
897 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; | 883 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
898 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; | 884 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
899 | 885 | ||
900 | __fxprintf (NULL, "%s", buf); | 886 | __fxprintf (NULL, "%s", buf); |
901 | 887 | ||
902 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; | 888 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; |
903 | _IO_funlockfile (stderr); | 889 | _IO_funlockfile (stderr); |
904 | 890 | ||
905 | free (buf); | 891 | free (buf); |
906 | } | 892 | } |
907 | #else | 893 | #else |
908 | fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), | 894 | fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"), |
909 | argv[0], argv[d->optind]); | 895 | argv[0], argv[d->optind]); |
910 | #endif | 896 | #endif |
911 | } | 897 | } |
912 | d->__nextchar += strlen (d->__nextchar); | 898 | d->__nextchar += strlen (d->__nextchar); |
913 | d->optind++; | 899 | d->optind++; |
914 | return '?'; | 900 | return '?'; |
915 | } | 901 | } |
916 | if (pfound != NULL) | 902 | if (pfound != NULL) |
917 | { | 903 | { |
918 | option_index = indfound; | 904 | option_index = indfound; |
919 | if (*nameend) | 905 | if (*nameend) |
920 | { | 906 | { |
921 | /* Don't test has_arg with >, because some C compilers don't | 907 | /* Don't test has_arg with >, because some C compilers don't |
922 | allow it to be used on enums. */ | 908 | allow it to be used on enums. */ |
923 | if (pfound->has_arg) | 909 | if (pfound->has_arg) |
924 | d->optarg = nameend + 1; | 910 | d->optarg = nameend + 1; |
925 | else | 911 | else |
926 | { | 912 | { |
927 | if (print_errors) | 913 | if (print_errors) |
928 | { | 914 | { |
929 | #if defined _LIBC && defined USE_IN_LIBIO | 915 | #if defined _LIBC && defined USE_IN_LIBIO |
930 | char *buf; | 916 | char *buf; |
931 | 917 | ||
932 | if (__asprintf (&buf, _("\ | 918 | if (__asprintf (&buf, _("\ |
933 | %s: option `-W %s' doesn't allow an argument\n"), | 919 | %s: option '-W %s' doesn't allow an argument\n"), |
934 | argv[0], pfound->name) >= 0) | 920 | argv[0], pfound->name) >= 0) |
935 | { | 921 | { |
936 | _IO_flockfile (stderr); | 922 | _IO_flockfile (stderr); |
937 | 923 | ||
938 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; | 924 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
939 | ((_IO_FILE *) stderr)->_flags2 | 925 | ((_IO_FILE *) stderr)->_flags2 |
940 | |= _IO_FLAGS2_NOTCANCEL; | 926 | |= _IO_FLAGS2_NOTCANCEL; |
941 | 927 | ||
942 | __fxprintf (NULL, "%s", buf); | 928 | __fxprintf (NULL, "%s", buf); |
943 | 929 | ||
944 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; | 930 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; |
945 | _IO_funlockfile (stderr); | 931 | _IO_funlockfile (stderr); |
946 | 932 | ||
947 | free (buf); | 933 | free (buf); |
948 | } | 934 | } |
949 | #else | 935 | #else |
950 | fprintf (stderr, _("\ | 936 | fprintf (stderr, _("\ |
951 | %s: option `-W %s' doesn't allow an argument\n"), | 937 | %s: option '-W %s' doesn't allow an argument\n"), |
952 | argv[0], pfound->name); | 938 | argv[0], pfound->name); |
953 | #endif | 939 | #endif |
954 | } | 940 | } |
955 | 941 | ||
956 | d->__nextchar += strlen (d->__nextchar); | 942 | d->__nextchar += strlen (d->__nextchar); |
957 | return '?'; | 943 | return '?'; |
958 | } | 944 | } |
959 | } | 945 | } |
960 | else if (pfound->has_arg == 1) | 946 | else if (pfound->has_arg == 1) |
961 | { | 947 | { |
962 | if (d->optind < argc) | 948 | if (d->optind < argc) |
963 | d->optarg = argv[d->optind++]; | 949 | d->optarg = argv[d->optind++]; |
964 | else | 950 | else |
965 | { | 951 | { |
966 | if (print_errors) | 952 | if (print_errors) |
967 | { | 953 | { |
968 | #if defined _LIBC && defined USE_IN_LIBIO | 954 | #if defined _LIBC && defined USE_IN_LIBIO |
969 | char *buf; | 955 | char *buf; |
970 | 956 | ||
971 | if (__asprintf (&buf, _("\ | 957 | if (__asprintf (&buf, _("\ |
972 | %s: option `%s' requires an argument\n"), | 958 | %s: option '%s' requires an argument\n"), |
973 | argv[0], argv[d->optind - 1]) >= 0) | 959 | argv[0], argv[d->optind - 1]) >= 0) |
974 | { | 960 | { |
975 | _IO_flockfile (stderr); | 961 | _IO_flockfile (stderr); |
976 | 962 | ||
977 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; | 963 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
978 | ((_IO_FILE *) stderr)->_flags2 | 964 | ((_IO_FILE *) stderr)->_flags2 |
979 | |= _IO_FLAGS2_NOTCANCEL; | 965 | |= _IO_FLAGS2_NOTCANCEL; |
980 | 966 | ||
981 | __fxprintf (NULL, "%s", buf); | 967 | __fxprintf (NULL, "%s", buf); |
982 | 968 | ||
983 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; | 969 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; |
984 | _IO_funlockfile (stderr); | 970 | _IO_funlockfile (stderr); |
985 | 971 | ||
986 | free (buf); | 972 | free (buf); |
987 | } | 973 | } |
988 | #else | 974 | #else |
989 | fprintf (stderr, | 975 | fprintf (stderr, |
990 | _("%s: option `%s' requires an argument\n"), | 976 | _("%s: option '%s' requires an argument\n"), |
991 | argv[0], argv[d->optind - 1]); | 977 | argv[0], argv[d->optind - 1]); |
992 | #endif | 978 | #endif |
993 | } | 979 | } |
994 | d->__nextchar += strlen (d->__nextchar); | 980 | d->__nextchar += strlen (d->__nextchar); |
995 | return optstring[0] == ':' ? ':' : '?'; | 981 | return optstring[0] == ':' ? ':' : '?'; |
996 | } | 982 | } |
997 | } | 983 | } |
998 | d->__nextchar += strlen (d->__nextchar); | 984 | d->__nextchar += strlen (d->__nextchar); |
999 | if (longind != NULL) | 985 | if (longind != NULL) |
1000 | *longind = option_index; | 986 | *longind = option_index; |
1001 | if (pfound->flag) | 987 | if (pfound->flag) |
1002 | { | 988 | { |
1003 | *(pfound->flag) = pfound->val; | 989 | *(pfound->flag) = pfound->val; |
1004 | return 0; | 990 | return 0; |
1005 | } | 991 | } |
1006 | return pfound->val; | 992 | return pfound->val; |
1007 | } | 993 | } |
1008 | d->__nextchar = NULL; | 994 | d->__nextchar = NULL; |
1009 | return 'W'; /* Let the application handle it. */ | 995 | return 'W'; /* Let the application handle it. */ |
1010 | } | 996 | } |
1011 | if (temp[1] == ':') | 997 | if (temp[1] == ':') |
1012 | { | 998 | { |
1013 | if (temp[2] == ':') | 999 | if (temp[2] == ':') |
1014 | { | 1000 | { |
1015 | /* This is an option that accepts an argument optionally. */ | 1001 | /* This is an option that accepts an argument optionally. */ |
1016 | if (*d->__nextchar != '\0') | 1002 | if (*d->__nextchar != '\0') |
1017 | { | 1003 | { |
1018 | d->optarg = d->__nextchar; | 1004 | d->optarg = d->__nextchar; |
1019 | d->optind++; | 1005 | d->optind++; |
1020 | } | 1006 | } |
1021 | else | 1007 | else |
1022 | d->optarg = NULL; | 1008 | d->optarg = NULL; |
1023 | d->__nextchar = NULL; | 1009 | d->__nextchar = NULL; |
1024 | } | 1010 | } |
1025 | else | 1011 | else |
1026 | { | 1012 | { |
1027 | /* This is an option that requires an argument. */ | 1013 | /* This is an option that requires an argument. */ |
1028 | if (*d->__nextchar != '\0') | 1014 | if (*d->__nextchar != '\0') |
1029 | { | 1015 | { |
1030 | d->optarg = d->__nextchar; | 1016 | d->optarg = d->__nextchar; |
1031 | /* If we end this ARGV-element by taking the rest as an arg, | 1017 | /* If we end this ARGV-element by taking the rest as an arg, |
1032 | we must advance to the next element now. */ | 1018 | we must advance to the next element now. */ |
1033 | d->optind++; | 1019 | d->optind++; |
1034 | } | 1020 | } |
1035 | else if (d->optind == argc) | 1021 | else if (d->optind == argc) |
1036 | { | 1022 | { |
1037 | if (print_errors) | 1023 | if (print_errors) |
1038 | { | 1024 | { |
1039 | /* 1003.2 specifies the format of this message. */ | ||
1040 | #if defined _LIBC && defined USE_IN_LIBIO | 1025 | #if defined _LIBC && defined USE_IN_LIBIO |
1041 | char *buf; | 1026 | char *buf; |
1042 | 1027 | ||
1043 | if (__asprintf (&buf, _("\ | 1028 | if (__asprintf (&buf, _("\ |
1044 | %s: option requires an argument -- %c\n"), | 1029 | %s: option requires an argument -- '%c'\n"), |
1045 | argv[0], c) >= 0) | 1030 | argv[0], c) >= 0) |
1046 | { | 1031 | { |
1047 | _IO_flockfile (stderr); | 1032 | _IO_flockfile (stderr); |
1048 | 1033 | ||
1049 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; | 1034 | int old_flags2 = ((_IO_FILE *) stderr)->_flags2; |
1050 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; | 1035 | ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; |
1051 | 1036 | ||
1052 | __fxprintf (NULL, "%s", buf); | 1037 | __fxprintf (NULL, "%s", buf); |
1053 | 1038 | ||
1054 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; | 1039 | ((_IO_FILE *) stderr)->_flags2 = old_flags2; |
1055 | _IO_funlockfile (stderr); | 1040 | _IO_funlockfile (stderr); |
1056 | 1041 | ||
1057 | free (buf); | 1042 | free (buf); |
1058 | } | 1043 | } |
1059 | #else | 1044 | #else |
1060 | fprintf (stderr, | 1045 | fprintf (stderr, |
1061 | _("%s: option requires an argument -- %c\n"), | 1046 | _("%s: option requires an argument -- '%c'\n"), |
1062 | argv[0], c); | 1047 | argv[0], c); |
1063 | #endif | 1048 | #endif |
1064 | } | 1049 | } |
1065 | d->optopt = c; | 1050 | d->optopt = c; |
1066 | if (optstring[0] == ':') | 1051 | if (optstring[0] == ':') |
1067 | c = ':'; | 1052 | c = ':'; |
1068 | else | 1053 | else |
1069 | c = '?'; | 1054 | c = '?'; |
1070 | } | 1055 | } |
1071 | else | 1056 | else |
1072 | /* We already incremented `optind' once; | 1057 | /* We already incremented `optind' once; |
1073 | increment it again when taking next ARGV-elt as argument. */ | 1058 | increment it again when taking next ARGV-elt as argument. */ |
1074 | d->optarg = argv[d->optind++]; | 1059 | d->optarg = argv[d->optind++]; |
1075 | d->__nextchar = NULL; | 1060 | d->__nextchar = NULL; |
1076 | } | 1061 | } |
1077 | } | 1062 | } |
1078 | return c; | 1063 | return c; |
1079 | } | 1064 | } |
@@ -1081,16 +1066,17 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, | |||
1081 | 1066 | ||
1082 | int | 1067 | int |
1083 | _getopt_internal (int argc, char **argv, const char *optstring, | 1068 | _getopt_internal (int argc, char **argv, const char *optstring, |
1084 | const struct option *longopts, int *longind, | 1069 | const struct option *longopts, int *longind, int long_only, |
1085 | int long_only, int posixly_correct) | 1070 | int posixly_correct) |
1086 | { | 1071 | { |
1087 | int result; | 1072 | int result; |
1088 | 1073 | ||
1089 | getopt_data.optind = optind; | 1074 | getopt_data.optind = optind; |
1090 | getopt_data.opterr = opterr; | 1075 | getopt_data.opterr = opterr; |
1091 | 1076 | ||
1092 | result = _getopt_internal_r (argc, argv, optstring, longopts, longind, | 1077 | result = _getopt_internal_r (argc, argv, optstring, longopts, |
1093 | long_only, posixly_correct, &getopt_data); | 1078 | longind, long_only, &getopt_data, |
1079 | posixly_correct); | ||
1094 | 1080 | ||
1095 | optind = getopt_data.optind; | 1081 | optind = getopt_data.optind; |
1096 | optarg = getopt_data.optarg; | 1082 | optarg = getopt_data.optarg; |
@@ -1110,10 +1096,23 @@ enum { POSIXLY_CORRECT = 1 }; | |||
1110 | int | 1096 | int |
1111 | getopt (int argc, char *const *argv, const char *optstring) | 1097 | getopt (int argc, char *const *argv, const char *optstring) |
1112 | { | 1098 | { |
1113 | return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0, | 1099 | return _getopt_internal (argc, (char **) argv, optstring, |
1114 | POSIXLY_CORRECT); | 1100 | (const struct option *) 0, |
1101 | (int *) 0, | ||
1102 | 0, POSIXLY_CORRECT); | ||
1115 | } | 1103 | } |
1116 | 1104 | ||
1105 | #ifdef _LIBC | ||
1106 | int | ||
1107 | __posix_getopt (int argc, char *const *argv, const char *optstring) | ||
1108 | { | ||
1109 | return _getopt_internal (argc, argv, optstring, | ||
1110 | (const struct option *) 0, | ||
1111 | (int *) 0, | ||
1112 | 0, 1); | ||
1113 | } | ||
1114 | #endif | ||
1115 | |||
1117 | 1116 | ||
1118 | #ifdef TEST | 1117 | #ifdef TEST |
1119 | 1118 | ||
@@ -1132,51 +1131,51 @@ main (int argc, char **argv) | |||
1132 | 1131 | ||
1133 | c = getopt (argc, argv, "abc:d:0123456789"); | 1132 | c = getopt (argc, argv, "abc:d:0123456789"); |
1134 | if (c == -1) | 1133 | if (c == -1) |
1135 | break; | 1134 | break; |
1136 | 1135 | ||
1137 | switch (c) | 1136 | switch (c) |
1138 | { | 1137 | { |
1139 | case '0': | 1138 | case '0': |
1140 | case '1': | 1139 | case '1': |
1141 | case '2': | 1140 | case '2': |
1142 | case '3': | 1141 | case '3': |
1143 | case '4': | 1142 | case '4': |
1144 | case '5': | 1143 | case '5': |
1145 | case '6': | 1144 | case '6': |
1146 | case '7': | 1145 | case '7': |
1147 | case '8': | 1146 | case '8': |
1148 | case '9': | 1147 | case '9': |
1149 | if (digit_optind != 0 && digit_optind != this_option_optind) | 1148 | if (digit_optind != 0 && digit_optind != this_option_optind) |
1150 | printf ("digits occur in two different argv-elements.\n"); | 1149 | printf ("digits occur in two different argv-elements.\n"); |
1151 | digit_optind = this_option_optind; | 1150 | digit_optind = this_option_optind; |
1152 | printf ("option %c\n", c); | 1151 | printf ("option %c\n", c); |
1153 | break; | 1152 | break; |
1154 | 1153 | ||
1155 | case 'a': | 1154 | case 'a': |
1156 | printf ("option a\n"); | 1155 | printf ("option a\n"); |
1157 | break; | 1156 | break; |
1158 | 1157 | ||
1159 | case 'b': | 1158 | case 'b': |
1160 | printf ("option b\n"); | 1159 | printf ("option b\n"); |
1161 | break; | 1160 | break; |
1162 | 1161 | ||
1163 | case 'c': | 1162 | case 'c': |
1164 | printf ("option c with value `%s'\n", optarg); | 1163 | printf ("option c with value '%s'\n", optarg); |
1165 | break; | 1164 | break; |
1166 | 1165 | ||
1167 | case '?': | 1166 | case '?': |
1168 | break; | 1167 | break; |
1169 | 1168 | ||
1170 | default: | 1169 | default: |
1171 | printf ("?? getopt returned character code 0%o ??\n", c); | 1170 | printf ("?? getopt returned character code 0%o ??\n", c); |
1172 | } | 1171 | } |
1173 | } | 1172 | } |
1174 | 1173 | ||
1175 | if (optind < argc) | 1174 | if (optind < argc) |
1176 | { | 1175 | { |
1177 | printf ("non-option ARGV-elements: "); | 1176 | printf ("non-option ARGV-elements: "); |
1178 | while (optind < argc) | 1177 | while (optind < argc) |
1179 | printf ("%s ", argv[optind++]); | 1178 | printf ("%s ", argv[optind++]); |
1180 | printf ("\n"); | 1179 | printf ("\n"); |
1181 | } | 1180 | } |
1182 | 1181 | ||