00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _NO_PROTO
00030 #define _NO_PROTO
00031 #endif
00032
00033 #include "config.h"
00034
00035 #if !defined (__STDC__) || !__STDC__
00036
00037
00038 #ifndef const
00039 #define const
00040 #endif
00041 #endif
00042
00043 #include <stdio.h>
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #define GETOPT_INTERFACE_VERSION 2
00054 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2
00055 #include <gnu-versions.h>
00056 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
00057 #define ELIDE_CODE
00058 #endif
00059 #endif
00060
00061 #ifndef ELIDE_CODE
00062
00063
00064
00065
00066 #ifdef __GNU_LIBRARY__
00067
00068
00069 #include <stdlib.h>
00070 #include <unistd.h>
00071 #endif
00072
00073 #ifdef VMS
00074 #include <unixlib.h>
00075 #if HAVE_STRING_H - 0
00076 #include <string.h>
00077 #endif
00078 #endif
00079
00080 #if defined (WIN32) && !defined (__CYGWIN32__)
00081
00082 #include <windows.h>
00083 #define getpid() GetCurrentProcessId()
00084 #endif
00085
00086 #ifndef _
00087
00088
00089 #ifdef NEVER_HAVE_LIBINTL_H
00090 # include <libintl.h>
00091 # define _(msgid) gettext (msgid)
00092 #else
00093 # define _(msgid) (msgid)
00094 #endif
00095 #endif
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 #include "getopt.h"
00112
00113
00114
00115
00116
00117
00118
00119 char *optarg = NULL;
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 int optind = 1;
00135
00136
00137
00138
00139
00140 int __getopt_initialized = 0;
00141
00142
00143
00144
00145
00146
00147
00148
00149 static char *nextchar;
00150
00151
00152
00153
00154 int opterr = 1;
00155
00156
00157
00158
00159
00160 int optopt = '?';
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 static enum
00192 {
00193 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
00194 } ordering;
00195
00196
00197 static char *posixly_correct;
00198
00199 #ifdef __GNU_LIBRARY__
00200
00201
00202
00203
00204 #include <string.h>
00205 #define my_index strchr
00206 #else
00207
00208
00209
00210
00211 char *getenv ();
00212
00213 static char *
00214 my_index (str, chr)
00215 const char *str;
00216 int chr;
00217 {
00218 while (*str)
00219 {
00220 if (*str == chr)
00221 return (char *) str;
00222 str++;
00223 }
00224 return 0;
00225 }
00226
00227
00228
00229 #ifdef __GNUC__
00230
00231
00232 #if !defined (__STDC__) || !__STDC__
00233
00234
00235 extern int strlen (const char *);
00236 #endif
00237 #if defined(__APPLE__)
00238 extern size_t strlen (const char *);
00239 #endif
00240 #endif
00241
00242 #endif
00243
00244
00245
00246
00247
00248
00249
00250 static int first_nonopt;
00251 static int last_nonopt;
00252
00253 #ifdef _LIBC
00254
00255
00256
00257
00258 extern char *__getopt_nonoption_flags;
00259
00260 static int nonoption_flags_max_len;
00261 static int nonoption_flags_len;
00262
00263 static int original_argc;
00264 static char *const *original_argv;
00265
00266 extern pid_t __libc_pid;
00267
00268
00269
00270
00271 static void
00272 __attribute__ ((unused)) store_args_and_env (int argc, char *const *argv)
00273 {
00274
00275
00276 original_argc = argc;
00277 original_argv = argv;
00278 }
00279
00280 text_set_element (__libc_subinit, store_args_and_env);
00281
00282 # define SWAP_FLAGS(ch1, ch2) \
00283 if (nonoption_flags_len > 0) \
00284 { \
00285 char __tmp = __getopt_nonoption_flags[ch1]; \
00286 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
00287 __getopt_nonoption_flags[ch2] = __tmp; \
00288 }
00289 #else
00290 # define SWAP_FLAGS(ch1, ch2)
00291 #endif
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 #if defined (__STDC__) && __STDC__
00303 static void exchange (char **);
00304 #endif
00305
00306 static void
00307 exchange (argv)
00308 char **argv;
00309 {
00310 int bottom = first_nonopt;
00311 int middle = last_nonopt;
00312 int top = optind;
00313 char *tem;
00314
00315
00316
00317
00318
00319
00320 #ifdef _LIBC
00321
00322
00323
00324 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
00325 {
00326
00327
00328 char *new_str = malloc (top + 1);
00329 if (new_str == NULL)
00330 nonoption_flags_len = nonoption_flags_max_len = 0;
00331 else
00332 {
00333 memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len);
00334 memset (&new_str[nonoption_flags_max_len], '\0',
00335 top + 1 - nonoption_flags_max_len);
00336 nonoption_flags_max_len = top + 1;
00337 __getopt_nonoption_flags = new_str;
00338 }
00339 }
00340 #endif
00341
00342 while (top > middle && middle > bottom)
00343 {
00344 if (top - middle > middle - bottom)
00345 {
00346
00347 int len = middle - bottom;
00348 register int i;
00349
00350
00351 for (i = 0; i < len; i++)
00352 {
00353 tem = argv[bottom + i];
00354 argv[bottom + i] = argv[top - (middle - bottom) + i];
00355 argv[top - (middle - bottom) + i] = tem;
00356 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
00357 }
00358
00359 top -= len;
00360 }
00361 else
00362 {
00363
00364 int len = top - middle;
00365 register int i;
00366
00367
00368 for (i = 0; i < len; i++)
00369 {
00370 tem = argv[bottom + i];
00371 argv[bottom + i] = argv[middle + i];
00372 argv[middle + i] = tem;
00373 SWAP_FLAGS (bottom + i, middle + i);
00374 }
00375
00376 bottom += len;
00377 }
00378 }
00379
00380
00381
00382 first_nonopt += (optind - last_nonopt);
00383 last_nonopt = optind;
00384 }
00385
00386
00387
00388 #if defined (__STDC__) && __STDC__
00389 static const char *_getopt_initialize (int, char *const *, const char *);
00390 #endif
00391 static const char *
00392 _getopt_initialize (argc, argv, optstring)
00393 int argc;
00394 char *const *argv;
00395 const char *optstring;
00396 {
00397
00398
00399
00400
00401 first_nonopt = last_nonopt = optind;
00402
00403 nextchar = NULL;
00404
00405 posixly_correct = getenv ("POSIXLY_CORRECT");
00406
00407
00408
00409 if (optstring[0] == '-')
00410 {
00411 ordering = RETURN_IN_ORDER;
00412 ++optstring;
00413 }
00414 else if (optstring[0] == '+')
00415 {
00416 ordering = REQUIRE_ORDER;
00417 ++optstring;
00418 }
00419 else if (posixly_correct != NULL)
00420 ordering = REQUIRE_ORDER;
00421 else
00422 ordering = PERMUTE;
00423
00424 #ifdef _LIBC
00425 if (posixly_correct == NULL
00426 && argc == original_argc && argv == original_argv)
00427 {
00428 if (nonoption_flags_max_len == 0)
00429 {
00430 if (__getopt_nonoption_flags == NULL
00431 || __getopt_nonoption_flags[0] == '\0')
00432 nonoption_flags_max_len = -1;
00433 else
00434 {
00435 const char *orig_str = __getopt_nonoption_flags;
00436 int len = nonoption_flags_max_len = strlen (orig_str);
00437 if (nonoption_flags_max_len < argc)
00438 nonoption_flags_max_len = argc;
00439 __getopt_nonoption_flags =
00440 (char *) malloc (nonoption_flags_max_len);
00441 if (__getopt_nonoption_flags == NULL)
00442 nonoption_flags_max_len = -1;
00443 else
00444 {
00445 memcpy (__getopt_nonoption_flags, orig_str, len);
00446 memset (&__getopt_nonoption_flags[len], '\0',
00447 nonoption_flags_max_len - len);
00448 }
00449 }
00450 }
00451 nonoption_flags_len = nonoption_flags_max_len;
00452 }
00453 else
00454 nonoption_flags_len = 0;
00455 #endif
00456
00457 return optstring;
00458 }
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516 int
00517 _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
00518 int argc;
00519 char *const *argv;
00520 const char *optstring;
00521 const struct option *longopts;
00522 int *longind;
00523 int long_only;
00524 {
00525 optarg = NULL;
00526
00527 if (optind == 0 || !__getopt_initialized)
00528 {
00529 if (optind == 0)
00530 optind = 1;
00531 optstring = _getopt_initialize (argc, argv, optstring);
00532 __getopt_initialized = 1;
00533 }
00534
00535
00536
00537
00538
00539 #ifdef _LIBC
00540 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
00541 || (optind < nonoption_flags_len \
00542 && __getopt_nonoption_flags[optind] == '1'))
00543 #else
00544 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
00545 #endif
00546
00547 if (nextchar == NULL || *nextchar == '\0')
00548 {
00549
00550
00551
00552
00553 if (last_nonopt > optind)
00554 last_nonopt = optind;
00555 if (first_nonopt > optind)
00556 first_nonopt = optind;
00557
00558 if (ordering == PERMUTE)
00559 {
00560
00561
00562
00563 if (first_nonopt != last_nonopt && last_nonopt != optind)
00564 exchange ((char **) argv);
00565 else if (last_nonopt != optind)
00566 first_nonopt = optind;
00567
00568
00569
00570
00571 while (optind < argc && NONOPTION_P)
00572 optind++;
00573 last_nonopt = optind;
00574 }
00575
00576
00577
00578
00579
00580
00581 if (optind != argc && !strcmp (argv[optind], "--"))
00582 {
00583 optind++;
00584
00585 if (first_nonopt != last_nonopt && last_nonopt != optind)
00586 exchange ((char **) argv);
00587 else if (first_nonopt == last_nonopt)
00588 first_nonopt = optind;
00589 last_nonopt = argc;
00590
00591 optind = argc;
00592 }
00593
00594
00595
00596
00597 if (optind == argc)
00598 {
00599
00600
00601 if (first_nonopt != last_nonopt)
00602 optind = first_nonopt;
00603 return -1;
00604 }
00605
00606
00607
00608
00609 if (NONOPTION_P)
00610 {
00611 if (ordering == REQUIRE_ORDER)
00612 return -1;
00613 optarg = argv[optind++];
00614 return 1;
00615 }
00616
00617
00618
00619
00620 nextchar = (argv[optind] + 1
00621 + (longopts != NULL && argv[optind][1] == '-'));
00622 }
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639 if (longopts != NULL
00640 && (argv[optind][1] == '-'
00641 || (long_only
00642 && (argv[optind][2]
00643 || !my_index (optstring, argv[optind][1])))))
00644 {
00645 char *nameend;
00646 const struct option *p;
00647 const struct option *pfound = NULL;
00648 int exact = 0;
00649 int ambig = 0;
00650 int indfound = -1;
00651 int option_index;
00652
00653 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
00654 ;
00655
00656
00657
00658 for (p = longopts, option_index = 0; p->name; p++, option_index++)
00659 if (!strncmp (p->name, nextchar, nameend - nextchar))
00660 {
00661 if ((unsigned int) (nameend - nextchar)
00662 == (unsigned int) strlen (p->name))
00663 {
00664
00665 pfound = p;
00666 indfound = option_index;
00667 exact = 1;
00668 break;
00669 }
00670 else if (pfound == NULL)
00671 {
00672
00673 pfound = p;
00674 indfound = option_index;
00675 }
00676 else
00677
00678 ambig = 1;
00679 }
00680
00681 if (ambig && !exact)
00682 {
00683 if (opterr)
00684 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
00685 argv[0], argv[optind]);
00686 nextchar += strlen (nextchar);
00687 optind++;
00688 optopt = 0;
00689 return '?';
00690 }
00691
00692 if (pfound != NULL)
00693 {
00694 option_index = indfound;
00695 optind++;
00696 if (*nameend)
00697 {
00698
00699
00700 if (pfound->has_arg)
00701 optarg = nameend + 1;
00702 else
00703 {
00704 if (opterr)
00705 if (argv[optind - 1][1] == '-')
00706
00707 fprintf (stderr,
00708 _
00709 ("%s: option `--%s' doesn't allow an argument\n"),
00710 argv[0], pfound->name);
00711 else
00712
00713 fprintf (stderr,
00714 _
00715 ("%s: option `%c%s' doesn't allow an argument\n"),
00716 argv[0], argv[optind - 1][0], pfound->name);
00717
00718 nextchar += strlen (nextchar);
00719
00720 optopt = pfound->val;
00721 return '?';
00722 }
00723 }
00724 else if (pfound->has_arg == 1)
00725 {
00726 if (optind < argc)
00727 optarg = argv[optind++];
00728 else
00729 {
00730 if (opterr)
00731 fprintf (stderr,
00732 _("%s: option `%s' requires an argument\n"),
00733 argv[0], argv[optind - 1]);
00734 nextchar += strlen (nextchar);
00735 optopt = pfound->val;
00736 return optstring[0] == ':' ? ':' : '?';
00737 }
00738 }
00739 nextchar += strlen (nextchar);
00740 if (longind != NULL)
00741 *longind = option_index;
00742 if (pfound->flag)
00743 {
00744 *(pfound->flag) = pfound->val;
00745 return 0;
00746 }
00747 return pfound->val;
00748 }
00749
00750
00751
00752
00753
00754 if (!long_only || argv[optind][1] == '-'
00755 || my_index (optstring, *nextchar) == NULL)
00756 {
00757 if (opterr)
00758 {
00759 if (argv[optind][1] == '-')
00760
00761 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
00762 argv[0], nextchar);
00763 else
00764
00765 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
00766 argv[0], argv[optind][0], nextchar);
00767 }
00768 nextchar = (char *) "";
00769 optind++;
00770 optopt = 0;
00771 return '?';
00772 }
00773 }
00774
00775
00776
00777 {
00778 char c = *nextchar++;
00779 char *temp = my_index (optstring, c);
00780
00781
00782 if (*nextchar == '\0')
00783 ++optind;
00784
00785 if (temp == NULL || c == ':')
00786 {
00787 if (opterr)
00788 {
00789 if (posixly_correct)
00790
00791 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
00792 else
00793 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
00794 }
00795 optopt = c;
00796 return '?';
00797 }
00798
00799 if (temp[0] == 'W' && temp[1] == ';')
00800 {
00801 char *nameend;
00802 const struct option *p;
00803 const struct option *pfound = NULL;
00804 int exact = 0;
00805 int ambig = 0;
00806 int indfound = 0;
00807 int option_index;
00808
00809
00810 if (*nextchar != '\0')
00811 {
00812 optarg = nextchar;
00813
00814
00815 optind++;
00816 }
00817 else if (optind == argc)
00818 {
00819 if (opterr)
00820 {
00821
00822 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
00823 argv[0], c);
00824 }
00825 optopt = c;
00826 if (optstring[0] == ':')
00827 c = ':';
00828 else
00829 c = '?';
00830 return c;
00831 }
00832 else
00833
00834
00835 optarg = argv[optind++];
00836
00837
00838
00839
00840 for (nextchar = nameend = optarg; *nameend && *nameend != '=';
00841 nameend++)
00842 ;
00843
00844
00845
00846 for (p = longopts, option_index = 0; p->name; p++, option_index++)
00847 if (!strncmp (p->name, nextchar, nameend - nextchar))
00848 {
00849 if ((unsigned int) (nameend - nextchar) == strlen (p->name))
00850 {
00851
00852 pfound = p;
00853 indfound = option_index;
00854 exact = 1;
00855 break;
00856 }
00857 else if (pfound == NULL)
00858 {
00859
00860 pfound = p;
00861 indfound = option_index;
00862 }
00863 else
00864
00865 ambig = 1;
00866 }
00867 if (ambig && !exact)
00868 {
00869 if (opterr)
00870 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
00871 argv[0], argv[optind]);
00872 nextchar += strlen (nextchar);
00873 optind++;
00874 return '?';
00875 }
00876 if (pfound != NULL)
00877 {
00878 option_index = indfound;
00879 if (*nameend)
00880 {
00881
00882
00883 if (pfound->has_arg)
00884 optarg = nameend + 1;
00885 else
00886 {
00887 if (opterr)
00888 fprintf (stderr, _("\
00889 %s: option `-W %s' doesn't allow an argument\n"), argv[0], pfound->name);
00890
00891 nextchar += strlen (nextchar);
00892 return '?';
00893 }
00894 }
00895 else if (pfound->has_arg == 1)
00896 {
00897 if (optind < argc)
00898 optarg = argv[optind++];
00899 else
00900 {
00901 if (opterr)
00902 fprintf (stderr,
00903 _("%s: option `%s' requires an argument\n"),
00904 argv[0], argv[optind - 1]);
00905 nextchar += strlen (nextchar);
00906 return optstring[0] == ':' ? ':' : '?';
00907 }
00908 }
00909 nextchar += strlen (nextchar);
00910 if (longind != NULL)
00911 *longind = option_index;
00912 if (pfound->flag)
00913 {
00914 *(pfound->flag) = pfound->val;
00915 return 0;
00916 }
00917 return pfound->val;
00918 }
00919 nextchar = NULL;
00920 return 'W';
00921 }
00922 if (temp[1] == ':')
00923 {
00924 if (temp[2] == ':')
00925 {
00926
00927 if (*nextchar != '\0')
00928 {
00929 optarg = nextchar;
00930 optind++;
00931 }
00932 else
00933 optarg = NULL;
00934 nextchar = NULL;
00935 }
00936 else
00937 {
00938
00939 if (*nextchar != '\0')
00940 {
00941 optarg = nextchar;
00942
00943
00944 optind++;
00945 }
00946 else if (optind == argc)
00947 {
00948 if (opterr)
00949 {
00950
00951 fprintf (stderr,
00952 _("%s: option requires an argument -- %c\n"),
00953 argv[0], c);
00954 }
00955 optopt = c;
00956 if (optstring[0] == ':')
00957 c = ':';
00958 else
00959 c = '?';
00960 }
00961 else
00962
00963
00964 optarg = argv[optind++];
00965 nextchar = NULL;
00966 }
00967 }
00968 return c;
00969 }
00970 }
00971
00972 int
00973 getopt (argc, argv, optstring)
00974 int argc;
00975 char *const *argv;
00976 const char *optstring;
00977 {
00978 return _getopt_internal (argc, argv, optstring,
00979 (const struct option *) 0, (int *) 0, 0);
00980 }
00981
00982 #endif
00983
00984 #ifdef TEST
00985
00986
00987
00988
00989 int
00990 main (argc, argv)
00991 int argc;
00992 char **argv;
00993 {
00994 int c;
00995 int digit_optind = 0;
00996
00997 while (1)
00998 {
00999 int this_option_optind = optind ? optind : 1;
01000
01001 c = getopt (argc, argv, "abc:d:0123456789");
01002 if (c == -1)
01003 break;
01004
01005 switch (c)
01006 {
01007 case '0':
01008 case '1':
01009 case '2':
01010 case '3':
01011 case '4':
01012 case '5':
01013 case '6':
01014 case '7':
01015 case '8':
01016 case '9':
01017 if (digit_optind != 0 && digit_optind != this_option_optind)
01018 printf ("digits occur in two different argv-elements.\n");
01019 digit_optind = this_option_optind;
01020 printf ("option %c\n", c);
01021 break;
01022
01023 case 'a':
01024 printf ("option a\n");
01025 break;
01026
01027 case 'b':
01028 printf ("option b\n");
01029 break;
01030
01031 case 'c':
01032 printf ("option c with value `%s'\n", optarg);
01033 break;
01034
01035 case '?':
01036 break;
01037
01038 default:
01039 printf ("?? getopt returned character code 0%o ??\n", c);
01040 }
01041 }
01042
01043 if (optind < argc)
01044 {
01045 printf ("non-option ARGV-elements: ");
01046 while (optind < argc)
01047 printf ("%s ", argv[optind++]);
01048 printf ("\n");
01049 }
01050
01051 exit (0);
01052 }
01053
01054 #endif