aboutsummaryrefslogtreecommitdiff
path: root/src/util/getopt.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
committerChristian Grothoff <christian@grothoff.org>2011-08-15 21:46:35 +0000
commit502af2167f7c218366666ca4944bd7cc54b5b19a (patch)
treea91fec5cc9769d260640bd91c6633cb9cf395524 /src/util/getopt.c
parent03af5a603b7cc53432249d5854cd412aa90dde0d (diff)
downloadgnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.tar.gz
gnunet-502af2167f7c218366666ca4944bd7cc54b5b19a.zip
indentation
Diffstat (limited to 'src/util/getopt.c')
-rw-r--r--src/util/getopt.c990
1 files changed, 494 insertions, 496 deletions
diff --git a/src/util/getopt.c b/src/util/getopt.c
index b8a295fae..2a35ab278 100644
--- a/src/util/getopt.c
+++ b/src/util/getopt.c
@@ -43,27 +43,27 @@ Copyright (C) 2006 Christian Grothoff
43#include "gnunet_getopt_lib.h" 43#include "gnunet_getopt_lib.h"
44 44
45#ifdef VMS 45#ifdef VMS
46# include <unixlib.h> 46#include <unixlib.h>
47# if HAVE_STRING_H - 0 47#if HAVE_STRING_H - 0
48# include <string.h> 48#include <string.h>
49# endif 49#endif
50#endif 50#endif
51 51
52#if defined (WIN32) && !defined (__CYGWIN32__) 52#if defined (WIN32) && !defined (__CYGWIN32__)
53/* It's not Unix, really. See? Capital letters. */ 53/* It's not Unix, really. See? Capital letters. */
54# include <windows.h> 54#include <windows.h>
55# define getpid() GetCurrentProcessId() 55#define getpid() GetCurrentProcessId()
56#endif 56#endif
57 57
58#ifndef _ 58#ifndef _
59/* This is for other GNU distributions with internationalized messages. 59/* This is for other GNU distributions with internationalized messages.
60 When compiling libc, the _ macro is predefined. */ 60 When compiling libc, the _ macro is predefined. */
61# ifdef HAVE_LIBINTL_H 61#ifdef HAVE_LIBINTL_H
62# include <libintl.h> 62#include <libintl.h>
63# define _(msgid) gettext (msgid) 63#define _(msgid) gettext (msgid)
64# else 64#else
65# define _(msgid) (msgid) 65#define _(msgid) (msgid)
66# endif 66#endif
67#endif 67#endif
68 68
69/* Describe the long-named options requested by the application. 69/* Describe the long-named options requested by the application.
@@ -91,7 +91,7 @@ struct GNoption
91{ 91{
92 const char *name; 92 const char *name;
93 /* has_arg can't be an enum because some compilers complain about 93 /* has_arg can't be an enum because some compilers complain about
94 type mismatches in all the code that assumes it is an int. */ 94 * type mismatches in all the code that assumes it is an int. */
95 int has_arg; 95 int has_arg;
96 int *flag; 96 int *flag;
97 int val; 97 int val;
@@ -198,15 +198,15 @@ char *getenv ();
198 198
199static char * 199static char *
200my_index (str, chr) 200my_index (str, chr)
201 const char *str; 201 const char *str;
202 int chr; 202 int chr;
203{ 203{
204 while (*str) 204 while (*str)
205 { 205 {
206 if (*str == chr) 206 if (*str == chr)
207 return (char *) str; 207 return (char *) str;
208 str++; 208 str++;
209 } 209 }
210 return 0; 210 return 0;
211} 211}
212 212
@@ -252,17 +252,17 @@ extern pid_t __libc_pid;
252 is valid for the getopt call we must make sure that the ARGV passed 252 is valid for the getopt call we must make sure that the ARGV passed
253 to getopt is that one passed to the process. */ 253 to getopt is that one passed to the process. */
254static void 254static void
255 __attribute__ ((unused)) store_args_and_env (int argc, char *const *argv) 255 __attribute__ ((unused)) store_args_and_env (int argc, char *const *argv)
256{ 256{
257 /* XXX This is no good solution. We should rather copy the args so 257 /* XXX This is no good solution. We should rather copy the args so
258 that we can compare them later. But we must not use malloc(3). */ 258 * that we can compare them later. But we must not use malloc(3). */
259 original_argc = argc; 259 original_argc = argc;
260 original_argv = argv; 260 original_argv = argv;
261} 261}
262 262
263text_set_element (__libc_subinit, store_args_and_env); 263text_set_element (__libc_subinit, store_args_and_env);
264 264
265# define SWAP_FLAGS(ch1, ch2) \ 265#define SWAP_FLAGS(ch1, ch2) \
266 if (nonoption_flags_len > 0) \ 266 if (nonoption_flags_len > 0) \
267 { \ 267 { \
268 char __tmp = __getopt_nonoption_flags[ch1]; \ 268 char __tmp = __getopt_nonoption_flags[ch1]; \
@@ -270,7 +270,7 @@ text_set_element (__libc_subinit, store_args_and_env);
270 __getopt_nonoption_flags[ch2] = __tmp; \ 270 __getopt_nonoption_flags[ch2] = __tmp; \
271 } 271 }
272#else /* !_LIBC */ 272#else /* !_LIBC */
273# define SWAP_FLAGS(ch1, ch2) 273#define SWAP_FLAGS(ch1, ch2)
274#endif /* _LIBC */ 274#endif /* _LIBC */
275 275
276/* Exchange two adjacent subsequences of ARGV. 276/* Exchange two adjacent subsequences of ARGV.
@@ -288,7 +288,7 @@ static void exchange (char **);
288 288
289static void 289static void
290exchange (argv) 290exchange (argv)
291 char **argv; 291 char **argv;
292{ 292{
293 int bottom = first_nonopt; 293 int bottom = first_nonopt;
294 int middle = last_nonopt; 294 int middle = last_nonopt;
@@ -296,69 +296,70 @@ exchange (argv)
296 char *tem; 296 char *tem;
297 297
298 /* Exchange the shorter segment with the far end of the longer segment. 298 /* Exchange the shorter segment with the far end of the longer segment.
299 That puts the shorter segment into the right place. 299 * That puts the shorter segment into the right place.
300 It leaves the longer segment in the right place overall, 300 * It leaves the longer segment in the right place overall,
301 but it consists of two parts that need to be swapped next. */ 301 * but it consists of two parts that need to be swapped next. */
302 302
303#ifdef _LIBC 303#ifdef _LIBC
304 /* First make sure the handling of the `__getopt_nonoption_flags' 304 /* First make sure the handling of the `__getopt_nonoption_flags'
305 string can work normally. Our top argument must be in the range 305 * string can work normally. Our top argument must be in the range
306 of the string. */ 306 * of the string. */
307 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) 307 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
308 {
309 /* We must extend the array. The user plays games with us and
310 * presents new arguments. */
311 char *new_str = malloc (top + 1);
312
313 if (new_str == NULL)
314 nonoption_flags_len = nonoption_flags_max_len = 0;
315 else
308 { 316 {
309 /* We must extend the array. The user plays games with us and 317 memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len);
310 presents new arguments. */ 318 memset (&new_str[nonoption_flags_max_len], '\0',
311 char *new_str = malloc (top + 1); 319 top + 1 - nonoption_flags_max_len);
312 if (new_str == NULL) 320 nonoption_flags_max_len = top + 1;
313 nonoption_flags_len = nonoption_flags_max_len = 0; 321 __getopt_nonoption_flags = new_str;
314 else
315 {
316 memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len);
317 memset (&new_str[nonoption_flags_max_len], '\0',
318 top + 1 - nonoption_flags_max_len);
319 nonoption_flags_max_len = top + 1;
320 __getopt_nonoption_flags = new_str;
321 }
322 } 322 }
323 }
323#endif 324#endif
324 325
325 while (top > middle && middle > bottom) 326 while (top > middle && middle > bottom)
327 {
328 if (top - middle > middle - bottom)
326 { 329 {
327 if (top - middle > middle - bottom) 330 /* Bottom segment is the short one. */
328 { 331 int len = middle - bottom;
329 /* Bottom segment is the short one. */ 332 register int i;
330 int len = middle - bottom;
331 register int i;
332 333
333 /* Swap it with the top part of the top segment. */ 334 /* Swap it with the top part of the top segment. */
334 for (i = 0; i < len; i++) 335 for (i = 0; i < len; i++)
335 { 336 {
336 tem = argv[bottom + i]; 337 tem = argv[bottom + i];
337 argv[bottom + i] = argv[top - (middle - bottom) + i]; 338 argv[bottom + i] = argv[top - (middle - bottom) + i];
338 argv[top - (middle - bottom) + i] = tem; 339 argv[top - (middle - bottom) + i] = tem;
339 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); 340 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
340 } 341 }
341 /* Exclude the moved bottom segment from further swapping. */ 342 /* Exclude the moved bottom segment from further swapping. */
342 top -= len; 343 top -= len;
343 } 344 }
344 else 345 else
345 { 346 {
346 /* Top segment is the short one. */ 347 /* Top segment is the short one. */
347 int len = top - middle; 348 int len = top - middle;
348 register int i; 349 register int i;
349 350
350 /* Swap it with the bottom part of the bottom segment. */ 351 /* Swap it with the bottom part of the bottom segment. */
351 for (i = 0; i < len; i++) 352 for (i = 0; i < len; i++)
352 { 353 {
353 tem = argv[bottom + i]; 354 tem = argv[bottom + i];
354 argv[bottom + i] = argv[middle + i]; 355 argv[bottom + i] = argv[middle + i];
355 argv[middle + i] = tem; 356 argv[middle + i] = tem;
356 SWAP_FLAGS (bottom + i, middle + i); 357 SWAP_FLAGS (bottom + i, middle + i);
357 } 358 }
358 /* Exclude the moved top segment from further swapping. */ 359 /* Exclude the moved top segment from further swapping. */
359 bottom += len; 360 bottom += len;
360 }
361 } 361 }
362 }
362 363
363 /* Update records for the slots the non-options now occupy. */ 364 /* Update records for the slots the non-options now occupy. */
364 365
@@ -373,13 +374,13 @@ static const char *_getopt_initialize (int, char *const *, const char *);
373#endif 374#endif
374static const char * 375static const char *
375_getopt_initialize (argc, argv, optstring) 376_getopt_initialize (argc, argv, optstring)
376 int argc; 377 int argc;
377 char *const *argv; 378 char *const *argv;
378 const char *optstring; 379 const char *optstring;
379{ 380{
380 /* Start processing options with ARGV-element 1 (since ARGV-element 0 381 /* Start processing options with ARGV-element 1 (since ARGV-element 0
381 is the program name); the sequence of previously skipped 382 * is the program name); the sequence of previously skipped
382 non-option ARGV-elements is empty. */ 383 * non-option ARGV-elements is empty. */
383 384
384 first_nonopt = last_nonopt = GNoptind; 385 first_nonopt = last_nonopt = GNoptind;
385 386
@@ -390,49 +391,48 @@ _getopt_initialize (argc, argv, optstring)
390 /* Determine how to handle the ordering of options and nonoptions. */ 391 /* Determine how to handle the ordering of options and nonoptions. */
391 392
392 if (optstring[0] == '-') 393 if (optstring[0] == '-')
393 { 394 {
394 ordering = RETURN_IN_ORDER; 395 ordering = RETURN_IN_ORDER;
395 ++optstring; 396 ++optstring;
396 } 397 }
397 else if (optstring[0] == '+') 398 else if (optstring[0] == '+')
398 { 399 {
399 ordering = REQUIRE_ORDER; 400 ordering = REQUIRE_ORDER;
400 ++optstring; 401 ++optstring;
401 } 402 }
402 else if (posixly_correct != NULL) 403 else if (posixly_correct != NULL)
403 ordering = REQUIRE_ORDER; 404 ordering = REQUIRE_ORDER;
404 else 405 else
405 ordering = PERMUTE; 406 ordering = PERMUTE;
406 407
407#ifdef _LIBC 408#ifdef _LIBC
408 if (posixly_correct == NULL 409 if (posixly_correct == NULL && argc == original_argc && argv == original_argv)
409 && argc == original_argc && argv == original_argv) 410 {
411 if (nonoption_flags_max_len == 0)
410 { 412 {
411 if (nonoption_flags_max_len == 0) 413 if (__getopt_nonoption_flags == NULL
414 || __getopt_nonoption_flags[0] == '\0')
415 nonoption_flags_max_len = -1;
416 else
417 {
418 const char *orig_str = __getopt_nonoption_flags;
419 int len = nonoption_flags_max_len = strlen (orig_str);
420
421 if (nonoption_flags_max_len < argc)
422 nonoption_flags_max_len = argc;
423 __getopt_nonoption_flags = (char *) malloc (nonoption_flags_max_len);
424 if (__getopt_nonoption_flags == NULL)
425 nonoption_flags_max_len = -1;
426 else
412 { 427 {
413 if (__getopt_nonoption_flags == NULL 428 memcpy (__getopt_nonoption_flags, orig_str, len);
414 || __getopt_nonoption_flags[0] == '\0') 429 memset (&__getopt_nonoption_flags[len], '\0',
415 nonoption_flags_max_len = -1; 430 nonoption_flags_max_len - len);
416 else
417 {
418 const char *orig_str = __getopt_nonoption_flags;
419 int len = nonoption_flags_max_len = strlen (orig_str);
420 if (nonoption_flags_max_len < argc)
421 nonoption_flags_max_len = argc;
422 __getopt_nonoption_flags =
423 (char *) malloc (nonoption_flags_max_len);
424 if (__getopt_nonoption_flags == NULL)
425 nonoption_flags_max_len = -1;
426 else
427 {
428 memcpy (__getopt_nonoption_flags, orig_str, len);
429 memset (&__getopt_nonoption_flags[len], '\0',
430 nonoption_flags_max_len - len);
431 }
432 }
433 } 431 }
434 nonoption_flags_len = nonoption_flags_max_len; 432 }
435 } 433 }
434 nonoption_flags_len = nonoption_flags_max_len;
435 }
436 else 436 else
437 nonoption_flags_len = 0; 437 nonoption_flags_len = 0;
438#endif 438#endif
@@ -498,11 +498,10 @@ _getopt_initialize (argc, argv, optstring)
498 498
499static int 499static int
500GN_getopt_internal (int argc, 500GN_getopt_internal (int argc,
501 char *const *argv, 501 char *const *argv,
502 const char *optstring, 502 const char *optstring,
503 const struct GNoption *longopts, 503 const struct GNoption *longopts,
504 int *longind, 504 int *longind, int long_only)
505 int long_only)
506{ 505{
507 static int __getopt_initialized = 0; 506 static int __getopt_initialized = 0;
508 static int GNopterr = 1; 507 static int GNopterr = 1;
@@ -510,17 +509,17 @@ GN_getopt_internal (int argc,
510 GNoptarg = NULL; 509 GNoptarg = NULL;
511 510
512 if (GNoptind == 0 || !__getopt_initialized) 511 if (GNoptind == 0 || !__getopt_initialized)
513 { 512 {
514 if (GNoptind == 0) 513 if (GNoptind == 0)
515 GNoptind = 1; /* Don't scan ARGV[0], the program name. */ 514 GNoptind = 1; /* Don't scan ARGV[0], the program name. */
516 optstring = _getopt_initialize (argc, argv, optstring); 515 optstring = _getopt_initialize (argc, argv, optstring);
517 __getopt_initialized = 1; 516 __getopt_initialized = 1;
518 } 517 }
519 518
520 /* Test whether ARGV[GNoptind] points to a non-option argument. 519 /* Test whether ARGV[GNoptind] points to a non-option argument.
521 Either it does not have option syntax, or there is an environment flag 520 * Either it does not have option syntax, or there is an environment flag
522 from the shell indicating it is not an option. The later information 521 * from the shell indicating it is not an option. The later information
523 is only used when the used in the GNU libc. */ 522 * is only used when the used in the GNU libc. */
524#ifdef _LIBC 523#ifdef _LIBC
525#define NONOPTION_P (argv[GNoptind][0] != '-' || argv[GNoptind][1] == '\0' \ 524#define NONOPTION_P (argv[GNoptind][0] != '-' || argv[GNoptind][1] == '\0' \
526 || (GNoptind < nonoption_flags_len \ 525 || (GNoptind < nonoption_flags_len \
@@ -530,233 +529,232 @@ GN_getopt_internal (int argc,
530#endif 529#endif
531 530
532 if (nextchar == NULL || *nextchar == '\0') 531 if (nextchar == NULL || *nextchar == '\0')
533 { 532 {
534 /* Advance to the next ARGV-element. */ 533 /* Advance to the next ARGV-element. */
535 534
536 /* Give FIRST_NONOPT & LAST_NONOPT rational values if GNoptind has been 535 /* Give FIRST_NONOPT & LAST_NONOPT rational values if GNoptind has been
537 moved back by the user (who may also have changed the arguments). */ 536 * moved back by the user (who may also have changed the arguments). */
538 if (last_nonopt > GNoptind) 537 if (last_nonopt > GNoptind)
539 last_nonopt = GNoptind; 538 last_nonopt = GNoptind;
540 if (first_nonopt > GNoptind) 539 if (first_nonopt > GNoptind)
541 first_nonopt = GNoptind; 540 first_nonopt = GNoptind;
542 541
543 if (ordering == PERMUTE) 542 if (ordering == PERMUTE)
544 { 543 {
545 /* If we have just processed some options following some non-options, 544 /* If we have just processed some options following some non-options,
546 exchange them so that the options come first. */ 545 * exchange them so that the options come first. */
547 546
548 if (first_nonopt != last_nonopt && last_nonopt != GNoptind) 547 if (first_nonopt != last_nonopt && last_nonopt != GNoptind)
549 exchange ((char **) argv); 548 exchange ((char **) argv);
550 else if (last_nonopt != GNoptind) 549 else if (last_nonopt != GNoptind)
551 first_nonopt = GNoptind; 550 first_nonopt = GNoptind;
552 551
553 /* Skip any additional non-options 552 /* Skip any additional non-options
554 and extend the range of non-options previously skipped. */ 553 * and extend the range of non-options previously skipped. */
555 554
556 while (GNoptind < argc && NONOPTION_P) 555 while (GNoptind < argc && NONOPTION_P)
557 GNoptind++; 556 GNoptind++;
558 last_nonopt = GNoptind; 557 last_nonopt = GNoptind;
559 } 558 }
560 559
561 /* The special ARGV-element `--' means premature end of options. 560 /* The special ARGV-element `--' means premature end of options.
562 Skip it like a null option, 561 * Skip it like a null option,
563 then exchange with previous non-options as if it were an option, 562 * then exchange with previous non-options as if it were an option,
564 then skip everything else like a non-option. */ 563 * then skip everything else like a non-option. */
565 if (GNoptind != argc && !strcmp (argv[GNoptind], "--")) 564 if (GNoptind != argc && !strcmp (argv[GNoptind], "--"))
566 { 565 {
567 GNoptind++; 566 GNoptind++;
568 567
569 if (first_nonopt != last_nonopt && last_nonopt != GNoptind) 568 if (first_nonopt != last_nonopt && last_nonopt != GNoptind)
570 exchange ((char **) argv); 569 exchange ((char **) argv);
571 else if (first_nonopt == last_nonopt) 570 else if (first_nonopt == last_nonopt)
572 first_nonopt = GNoptind; 571 first_nonopt = GNoptind;
573 last_nonopt = argc; 572 last_nonopt = argc;
574 573
575 GNoptind = argc; 574 GNoptind = argc;
576 } 575 }
577 576
578 /* If we have done all the ARGV-elements, stop the scan 577 /* If we have done all the ARGV-elements, stop the scan
579 and back over any non-options that we skipped and permuted. */ 578 * and back over any non-options that we skipped and permuted. */
580 579
581 if (GNoptind == argc) 580 if (GNoptind == argc)
582 { 581 {
583 /* Set the next-arg-index to point at the non-options 582 /* Set the next-arg-index to point at the non-options
584 that we previously skipped, so the caller will digest them. */ 583 * that we previously skipped, so the caller will digest them. */
585 if (first_nonopt != last_nonopt) 584 if (first_nonopt != last_nonopt)
586 GNoptind = first_nonopt; 585 GNoptind = first_nonopt;
587 return -1; 586 return -1;
588 } 587 }
589 588
590 /* If we have come to a non-option and did not permute it, 589 /* 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. */ 590 * either stop the scan or describe it to the caller and pass it by. */
592 591
593 if (NONOPTION_P) 592 if (NONOPTION_P)
594 { 593 {
595 if (ordering == REQUIRE_ORDER) 594 if (ordering == REQUIRE_ORDER)
596 return -1; 595 return -1;
597 GNoptarg = argv[GNoptind++]; 596 GNoptarg = argv[GNoptind++];
598 return 1; 597 return 1;
599 } 598 }
600 599
601 /* We have found another option-ARGV-element. 600 /* We have found another option-ARGV-element.
602 Skip the initial punctuation. */ 601 * Skip the initial punctuation. */
603 602
604 nextchar = (argv[GNoptind] + 1 603 nextchar = (argv[GNoptind] + 1
605 + (longopts != NULL && argv[GNoptind][1] == '-')); 604 + (longopts != NULL && argv[GNoptind][1] == '-'));
606 } 605 }
607 606
608 /* Decode the current option-ARGV-element. */ 607 /* Decode the current option-ARGV-element. */
609 608
610 /* Check whether the ARGV-element is a long option. 609 /* Check whether the ARGV-element is a long option.
611 610 *
612 If long_only and the ARGV-element has the form "-f", where f is 611 * 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 612 * 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 613 * a long option that starts with f. Otherwise there would be no
615 way to give the -f short option. 614 * way to give the -f short option.
616 615 *
617 On the other hand, if there's a long option "fubar" and 616 * On the other hand, if there's a long option "fubar" and
618 the ARGV-element is "-fu", do consider that an abbreviation of 617 * the ARGV-element is "-fu", do consider that an abbreviation of
619 the long option, just like "--fu", and not "-f" with arg "u". 618 * the long option, just like "--fu", and not "-f" with arg "u".
620 619 *
621 This distinction seems to be the most useful approach. */ 620 * This distinction seems to be the most useful approach. */
622 621
623 if (longopts != NULL 622 if (longopts != NULL
624 && (argv[GNoptind][1] == '-' 623 && (argv[GNoptind][1] == '-'
625 || (long_only 624 || (long_only
626 && (argv[GNoptind][2] 625 && (argv[GNoptind][2]
627 || !my_index (optstring, argv[GNoptind][1]))))) 626 || !my_index (optstring, argv[GNoptind][1])))))
628 { 627 {
629 char *nameend; 628 char *nameend;
630 const struct GNoption *p; 629 const struct GNoption *p;
631 const struct GNoption *pfound = NULL; 630 const struct GNoption *pfound = NULL;
632 int exact = 0; 631 int exact = 0;
633 int ambig = 0; 632 int ambig = 0;
634 int indfound = -1; 633 int indfound = -1;
635 int option_index; 634 int option_index;
635
636 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
637 /* Do nothing. */ ;
638
639 /* Test all long options for either exact match
640 * or abbreviated matches. */
641 for (p = longopts, option_index = 0; p->name; p++, option_index++)
642 if (!strncmp (p->name, nextchar, nameend - nextchar))
643 {
644 if ((unsigned int) (nameend - nextchar)
645 == (unsigned int) strlen (p->name))
646 {
647 /* Exact match found. */
648 pfound = p;
649 indfound = option_index;
650 exact = 1;
651 break;
652 }
653 else if (pfound == NULL)
654 {
655 /* First nonexact match found. */
656 pfound = p;
657 indfound = option_index;
658 }
659 else
660 /* Second or later nonexact match found. */
661 ambig = 1;
662 }
636 663
637 for (nameend = nextchar; *nameend && *nameend != '='; nameend++) 664 if (ambig && !exact)
638 /* Do nothing. */ ; 665 {
666 if (GNopterr)
667 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
668 argv[0], argv[GNoptind]);
669 nextchar += strlen (nextchar);
670 GNoptind++;
671 return '?';
672 }
639 673
640 /* Test all long options for either exact match 674 if (pfound != NULL)
641 or abbreviated matches. */ 675 {
642 for (p = longopts, option_index = 0; p->name; p++, option_index++) 676 option_index = indfound;
643 if (!strncmp (p->name, nextchar, nameend - nextchar)) 677 GNoptind++;
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 GNoptarg = nameend + 1;
684 else
685 {
686 if (GNopterr)
644 { 687 {
645 if ((unsigned int) (nameend - nextchar) 688 if (argv[GNoptind - 1][1] == '-')
646 == (unsigned int) strlen (p->name)) 689 /* --option */
647 { 690 fprintf (stderr,
648 /* Exact match found. */ 691 _
649 pfound = p; 692 ("%s: option `--%s' does not allow an argument\n"),
650 indfound = option_index; 693 argv[0], pfound->name);
651 exact = 1;
652 break;
653 }
654 else if (pfound == NULL)
655 {
656 /* First nonexact match found. */
657 pfound = p;
658 indfound = option_index;
659 }
660 else 694 else
661 /* Second or later nonexact match found. */ 695 /* +option or -option */
662 ambig = 1; 696 fprintf (stderr,
697 _
698 ("%s: option `%c%s' does not allow an argument\n"),
699 argv[0], argv[GNoptind - 1][0], pfound->name);
663 } 700 }
664
665 if (ambig && !exact)
666 {
667 if (GNopterr)
668 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
669 argv[0], argv[GNoptind]);
670 nextchar += strlen (nextchar); 701 nextchar += strlen (nextchar);
671 GNoptind++;
672 return '?'; 702 return '?';
673 } 703 }
674 704 }
675 if (pfound != NULL) 705 else if (pfound->has_arg == 1)
706 {
707 if (GNoptind < argc)
676 { 708 {
677 option_index = indfound; 709 GNoptarg = argv[GNoptind++];
678 GNoptind++;
679 if (*nameend)
680 {
681 /* Don't test has_arg with >, because some C compilers don't
682 allow it to be used on enums. */
683 if (pfound->has_arg)
684 GNoptarg = nameend + 1;
685 else
686 {
687 if (GNopterr)
688 {
689 if (argv[GNoptind - 1][1] == '-')
690 /* --option */
691 fprintf (stderr,
692 _
693 ("%s: option `--%s' does not allow an argument\n"),
694 argv[0], pfound->name);
695 else
696 /* +option or -option */
697 fprintf (stderr,
698 _
699 ("%s: option `%c%s' does not allow an argument\n"),
700 argv[0], argv[GNoptind - 1][0],
701 pfound->name);
702 }
703 nextchar += strlen (nextchar);
704 return '?';
705 }
706 }
707 else if (pfound->has_arg == 1)
708 {
709 if (GNoptind < argc)
710 {
711 GNoptarg = argv[GNoptind++];
712 }
713 else
714 {
715 if (GNopterr)
716 {
717 fprintf (stderr,
718 _("%s: option `%s' requires an argument\n"),
719 argv[0], argv[GNoptind - 1]);
720 }
721 nextchar += strlen (nextchar);
722 return (optstring[0] == ':') ? ':' : '?';
723 }
724 }
725 nextchar += strlen (nextchar);
726 if (longind != NULL)
727 *longind = option_index;
728 if (pfound->flag)
729 {
730 *(pfound->flag) = pfound->val;
731 return 0;
732 }
733 return pfound->val;
734 } 710 }
735 711 else
736 /* Can't find it as a long option. If this is not getopt_long_only,
737 or the option starts with '--' or is not a valid short
738 option, then it's an error.
739 Otherwise interpret it as a short option. */
740 if (!long_only || argv[GNoptind][1] == '-'
741 || my_index (optstring, *nextchar) == NULL)
742 { 712 {
743 if (GNopterr) 713 if (GNopterr)
744 { 714 {
745 if (argv[GNoptind][1] == '-') 715 fprintf (stderr,
746 /* --option */ 716 _("%s: option `%s' requires an argument\n"),
747 fprintf (stderr, _("%s: unrecognized option `--%s'\n"), 717 argv[0], argv[GNoptind - 1]);
748 argv[0], nextchar); 718 }
749 else 719 nextchar += strlen (nextchar);
750 /* +option or -option */ 720 return (optstring[0] == ':') ? ':' : '?';
751 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
752 argv[0], argv[GNoptind][0], nextchar);
753 }
754 nextchar = (char *) "";
755 GNoptind++;
756 return '?';
757 } 721 }
722 }
723 nextchar += strlen (nextchar);
724 if (longind != NULL)
725 *longind = option_index;
726 if (pfound->flag)
727 {
728 *(pfound->flag) = pfound->val;
729 return 0;
730 }
731 return pfound->val;
758 } 732 }
759 733
734 /* Can't find it as a long option. If this is not getopt_long_only,
735 * or the option starts with '--' or is not a valid short
736 * option, then it's an error.
737 * Otherwise interpret it as a short option. */
738 if (!long_only || argv[GNoptind][1] == '-'
739 || my_index (optstring, *nextchar) == NULL)
740 {
741 if (GNopterr)
742 {
743 if (argv[GNoptind][1] == '-')
744 /* --option */
745 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
746 argv[0], nextchar);
747 else
748 /* +option or -option */
749 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
750 argv[0], argv[GNoptind][0], nextchar);
751 }
752 nextchar = (char *) "";
753 GNoptind++;
754 return '?';
755 }
756 }
757
760 /* Look at and handle the next short option-character. */ 758 /* Look at and handle the next short option-character. */
761 759
762 { 760 {
@@ -768,186 +766,185 @@ GN_getopt_internal (int argc,
768 ++GNoptind; 766 ++GNoptind;
769 767
770 if (temp == NULL || c == ':') 768 if (temp == NULL || c == ':')
769 {
770 if (GNopterr)
771 { 771 {
772 if (GNopterr) 772 if (posixly_correct)
773 { 773 /* 1003.2 specifies the format of this message. */
774 if (posixly_correct) 774 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
775 /* 1003.2 specifies the format of this message. */ 775 else
776 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c); 776 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
777 else
778 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
779 }
780 return '?';
781 } 777 }
778 return '?';
779 }
782 /* Convenience. Treat POSIX -W foo same as long option --foo */ 780 /* Convenience. Treat POSIX -W foo same as long option --foo */
783 if (temp[0] == 'W' && temp[1] == ';') 781 if (temp[0] == 'W' && temp[1] == ';')
784 { 782 {
785 char *nameend; 783 char *nameend;
786 const struct GNoption *p; 784 const struct GNoption *p;
787 const struct GNoption *pfound = NULL; 785 const struct GNoption *pfound = NULL;
788 int exact = 0; 786 int exact = 0;
789 int ambig = 0; 787 int ambig = 0;
790 int indfound = 0; 788 int indfound = 0;
791 int option_index; 789 int option_index;
792 790
793 /* This is an option that requires an argument. */ 791 /* This is an option that requires an argument. */
794 if (*nextchar != '\0') 792 if (*nextchar != '\0')
795 { 793 {
796 GNoptarg = nextchar; 794 GNoptarg = nextchar;
797 /* If we end this ARGV-element by taking the rest as an arg, 795 /* If we end this ARGV-element by taking the rest as an arg,
798 we must advance to the next element now. */ 796 * we must advance to the next element now. */
799 GNoptind++; 797 GNoptind++;
800 } 798 }
801 else if (GNoptind == argc) 799 else if (GNoptind == argc)
802 { 800 {
803 if (GNopterr) 801 if (GNopterr)
804 { 802 {
805 /* 1003.2 specifies the format of this message. */ 803 /* 1003.2 specifies the format of this message. */
806 fprintf (stderr, _("%s: option requires an argument -- %c\n"), 804 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
807 argv[0], c); 805 argv[0], c);
808 } 806 }
809 if (optstring[0] == ':') 807 if (optstring[0] == ':')
810 c = ':'; 808 c = ':';
811 else
812 c = '?';
813 return c;
814 }
815 else 809 else
816 /* We already incremented `GNoptind' once; 810 c = '?';
817 increment it again when taking next ARGV-elt as argument. */ 811 return c;
818 GNoptarg = argv[GNoptind++]; 812 }
813 else
814 /* We already incremented `GNoptind' once;
815 * increment it again when taking next ARGV-elt as argument. */
816 GNoptarg = argv[GNoptind++];
819 817
820 /* GNoptarg is now the argument, see if it's in the 818 /* GNoptarg is now the argument, see if it's in the
821 table of longopts. */ 819 * table of longopts. */
822 820
823 for (nextchar = nameend = GNoptarg; *nameend && *nameend != '='; 821 for (nextchar = nameend = GNoptarg; *nameend && *nameend != '=';
824 nameend++) 822 nameend++)
825 /* Do nothing. */ ; 823 /* Do nothing. */ ;
826 824
827 /* Test all long options for either exact match 825 /* Test all long options for either exact match
828 or abbreviated matches. */ 826 * or abbreviated matches. */
829 if (longopts != NULL) 827 if (longopts != NULL)
830 for (p = longopts, option_index = 0; p->name; p++, option_index++) 828 for (p = longopts, option_index = 0; p->name; p++, option_index++)
831 if (!strncmp (p->name, nextchar, nameend - nextchar)) 829 if (!strncmp (p->name, nextchar, nameend - nextchar))
830 {
831 if ((unsigned int) (nameend - nextchar) == strlen (p->name))
832 {
833 /* Exact match found. */
834 pfound = p;
835 indfound = option_index;
836 exact = 1;
837 break;
838 }
839 else if (pfound == NULL)
832 { 840 {
833 if ((unsigned int) (nameend - nextchar) == strlen (p->name)) 841 /* First nonexact match found. */
834 { 842 pfound = p;
835 /* Exact match found. */ 843 indfound = option_index;
836 pfound = p;
837 indfound = option_index;
838 exact = 1;
839 break;
840 }
841 else if (pfound == NULL)
842 {
843 /* First nonexact match found. */
844 pfound = p;
845 indfound = option_index;
846 }
847 else
848 /* Second or later nonexact match found. */
849 ambig = 1;
850 } 844 }
851 if (ambig && !exact) 845 else
846 /* Second or later nonexact match found. */
847 ambig = 1;
848 }
849 if (ambig && !exact)
850 {
851 if (GNopterr)
852 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
853 argv[0], argv[GNoptind]);
854 nextchar += strlen (nextchar);
855 GNoptind++;
856 return '?';
857 }
858 if (pfound != NULL)
859 {
860 option_index = indfound;
861 if (*nameend)
862 {
863 /* Don't test has_arg with >, because some C compilers don't
864 * allow it to be used on enums. */
865 if (pfound->has_arg)
866 GNoptarg = nameend + 1;
867 else
852 { 868 {
853 if (GNopterr) 869 if (GNopterr)
854 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), 870 fprintf (stderr, _("\
855 argv[0], argv[GNoptind]); 871%s: option `-W %s' does not allow an argument\n"), argv[0], pfound->name);
872
856 nextchar += strlen (nextchar); 873 nextchar += strlen (nextchar);
857 GNoptind++;
858 return '?'; 874 return '?';
859 } 875 }
860 if (pfound != NULL) 876 }
877 else if (pfound->has_arg == 1)
878 {
879 if (GNoptind < argc)
880 GNoptarg = argv[GNoptind++];
881 else
861 { 882 {
862 option_index = indfound; 883 if (GNopterr)
863 if (*nameend) 884 fprintf (stderr,
864 { 885 _("%s: option `%s' requires an argument\n"),
865 /* Don't test has_arg with >, because some C compilers don't 886 argv[0], argv[GNoptind - 1]);
866 allow it to be used on enums. */
867 if (pfound->has_arg)
868 GNoptarg = nameend + 1;
869 else
870 {
871 if (GNopterr)
872 fprintf (stderr, _("\
873%s: option `-W %s' does not allow an argument\n"), argv[0], pfound->name);
874
875 nextchar += strlen (nextchar);
876 return '?';
877 }
878 }
879 else if (pfound->has_arg == 1)
880 {
881 if (GNoptind < argc)
882 GNoptarg = argv[GNoptind++];
883 else
884 {
885 if (GNopterr)
886 fprintf (stderr,
887 _("%s: option `%s' requires an argument\n"),
888 argv[0], argv[GNoptind - 1]);
889 nextchar += strlen (nextchar);
890 return optstring[0] == ':' ? ':' : '?';
891 }
892 }
893 nextchar += strlen (nextchar); 887 nextchar += strlen (nextchar);
894 if (longind != NULL) 888 return optstring[0] == ':' ? ':' : '?';
895 *longind = option_index;
896 if (pfound->flag)
897 {
898 *(pfound->flag) = pfound->val;
899 return 0;
900 }
901 return pfound->val;
902 } 889 }
903 nextchar = NULL; 890 }
904 return 'W'; /* Let the application handle it. */ 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;
905 } 900 }
901 nextchar = NULL;
902 return 'W'; /* Let the application handle it. */
903 }
906 if (temp[1] == ':') 904 if (temp[1] == ':')
905 {
906 if (temp[2] == ':')
907 { 907 {
908 if (temp[2] == ':') 908 /* This is an option that accepts an argument optionally. */
909 { 909 if (*nextchar != '\0')
910 /* This is an option that accepts an argument optionally. */ 910 {
911 if (*nextchar != '\0') 911 GNoptarg = nextchar;
912 { 912 GNoptind++;
913 GNoptarg = nextchar; 913 }
914 GNoptind++;
915 }
916 else
917 GNoptarg = NULL;
918 nextchar = NULL;
919 }
920 else 914 else
915 GNoptarg = NULL;
916 nextchar = NULL;
917 }
918 else
919 {
920 /* This is an option that requires an argument. */
921 if (*nextchar != '\0')
922 {
923 GNoptarg = 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 GNoptind++;
927 }
928 else if (GNoptind == argc)
929 {
930 if (GNopterr)
921 { 931 {
922 /* This is an option that requires an argument. */ 932 /* 1003.2 specifies the format of this message. */
923 if (*nextchar != '\0') 933 fprintf (stderr,
924 { 934 _("%s: option requires an argument -- %c\n"), argv[0], c);
925 GNoptarg = nextchar;
926 /* If we end this ARGV-element by taking the rest as an arg,
927 we must advance to the next element now. */
928 GNoptind++;
929 }
930 else if (GNoptind == argc)
931 {
932 if (GNopterr)
933 {
934 /* 1003.2 specifies the format of this message. */
935 fprintf (stderr,
936 _("%s: option requires an argument -- %c\n"),
937 argv[0], c);
938 }
939 if (optstring[0] == ':')
940 c = ':';
941 else
942 c = '?';
943 }
944 else
945 /* We already incremented `GNoptind' once;
946 increment it again when taking next ARGV-elt as argument. */
947 GNoptarg = argv[GNoptind++];
948 nextchar = NULL;
949 } 935 }
936 if (optstring[0] == ':')
937 c = ':';
938 else
939 c = '?';
940 }
941 else
942 /* We already incremented `GNoptind' once;
943 * increment it again when taking next ARGV-elt as argument. */
944 GNoptarg = argv[GNoptind++];
945 nextchar = NULL;
950 } 946 }
947 }
951 return c; 948 return c;
952 } 949 }
953} 950}
@@ -1001,15 +998,15 @@ GNUNET_GETOPT_run (const char *binaryOptions,
1001 shorts = GNUNET_malloc (count * 2 + 1); 998 shorts = GNUNET_malloc (count * 2 + 1);
1002 spos = 0; 999 spos = 0;
1003 for (i = 0; i < count; i++) 1000 for (i = 0; i < count; i++)
1004 { 1001 {
1005 long_options[i].name = allOptions[i].name; 1002 long_options[i].name = allOptions[i].name;
1006 long_options[i].has_arg = allOptions[i].require_argument; 1003 long_options[i].has_arg = allOptions[i].require_argument;
1007 long_options[i].flag = NULL; 1004 long_options[i].flag = NULL;
1008 long_options[i].val = allOptions[i].shortName; 1005 long_options[i].val = allOptions[i].shortName;
1009 shorts[spos++] = allOptions[i].shortName; 1006 shorts[spos++] = allOptions[i].shortName;
1010 if (allOptions[i].require_argument != 0) 1007 if (allOptions[i].require_argument != 0)
1011 shorts[spos++] = ':'; 1008 shorts[spos++] = ':';
1012 } 1009 }
1013 long_options[count].name = NULL; 1010 long_options[count].name = NULL;
1014 long_options[count].has_arg = 0; 1011 long_options[count].has_arg = 0;
1015 long_options[count].flag = NULL; 1012 long_options[count].flag = NULL;
@@ -1018,30 +1015,31 @@ GNUNET_GETOPT_run (const char *binaryOptions,
1018 cont = GNUNET_OK; 1015 cont = GNUNET_OK;
1019 /* main getopt loop */ 1016 /* main getopt loop */
1020 while (cont == GNUNET_OK) 1017 while (cont == GNUNET_OK)
1021 { 1018 {
1022 int option_index = 0; 1019 int option_index = 0;
1023 c = GNgetopt_long (argc, argv, shorts, long_options, &option_index);
1024 1020
1025 if (c == GNUNET_SYSERR) 1021 c = GNgetopt_long (argc, argv, shorts, long_options, &option_index);
1026 break; /* No more flags to process */
1027 1022
1028 for (i = 0; i < count; i++) 1023 if (c == GNUNET_SYSERR)
1029 { 1024 break; /* No more flags to process */
1030 clpc.currentArgument = GNoptind - 1; 1025
1031 if ((char) c == allOptions[i].shortName) 1026 for (i = 0; i < count; i++)
1032 { 1027 {
1033 cont = allOptions[i].processor (&clpc, 1028 clpc.currentArgument = GNoptind - 1;
1034 allOptions[i].scls, 1029 if ((char) c == allOptions[i].shortName)
1035 allOptions[i].name, GNoptarg); 1030 {
1036 break; 1031 cont = allOptions[i].processor (&clpc,
1037 } 1032 allOptions[i].scls,
1038 } 1033 allOptions[i].name, GNoptarg);
1039 if (i == count) 1034 break;
1040 { 1035 }
1041 fprintf (stderr, _("Use --help to get a list of options.\n")); 1036 }
1042 cont = GNUNET_SYSERR; 1037 if (i == count)
1043 } 1038 {
1039 fprintf (stderr, _("Use --help to get a list of options.\n"));
1040 cont = GNUNET_SYSERR;
1044 } 1041 }
1042 }
1045 1043
1046 GNUNET_free (shorts); 1044 GNUNET_free (shorts);
1047 GNUNET_free (long_options); 1045 GNUNET_free (long_options);