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