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