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 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030 #include <stdio.h>
00031
00032
00033
00034
00035
00036 #define GNU_GETOPT_INTERFACE_VERSION 2
00037 #if defined __GLIBC__ && __GLIBC__ >= 2
00038 # include <gnu-versions.h>
00039 # if _GNU_GETOPT_INTERFACE_VERSION == GNU_GETOPT_INTERFACE_VERSION
00040 # define ELIDE_CODE
00041 # endif
00042 #endif
00043
00044 #ifndef ELIDE_CODE
00045
00046 #ifdef VMS
00047 # include <unixlib.h>
00048 #endif
00049
00050 #ifndef _
00051
00052 # if 0 //defined HAVE_LIBINTL_H || defined _LIBC
00053 # include <libintl.h>
00054 # ifndef _
00055 # define _(msgid) gettext (msgid)
00056 # endif
00057 # else
00058 # define _(msgid) (msgid)
00059 # endif
00060 #endif
00061
00062 #ifndef __CYGWIN__
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 #include "gnu_getopt.h"
00078
00079
00080
00081
00082
00083
00084
00085 char *optarg;
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 int optind = 1;
00101
00102
00103
00104
00105 int opterr = 1;
00106
00107
00108
00109
00110
00111 int optopt = '?';
00112 #endif
00113
00114
00115
00116
00117
00118 int __getopt_initialized;
00119
00120
00121
00122
00123
00124
00125
00126
00127 static char *nextchar;
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 static enum
00159 {
00160 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
00161 } ordering;
00162
00163
00164 static char *posixly_correct;
00165
00166 #include <cstring>
00167 using std::strlen;
00168 using std::strcmp;
00169 using std::strncmp;
00170 using std::strchr;
00171
00172 #include <cstdlib>
00173 using std::getenv;
00174
00175
00176
00177
00178
00179
00180
00181
00182 static int first_nonopt;
00183 static int last_nonopt;
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 static void
00195 exchange (char **argv)
00196 {
00197 int bottom = first_nonopt;
00198 int middle = last_nonopt;
00199 int top = optind;
00200 char *tem;
00201
00202
00203
00204
00205
00206
00207 while (top > middle && middle > bottom)
00208 {
00209 if (top - middle > middle - bottom)
00210 {
00211
00212 int len = middle - bottom;
00213 register int i;
00214
00215
00216 for (i = 0; i < len; i++)
00217 {
00218 tem = argv[bottom + i];
00219 argv[bottom + i] = argv[top - (middle - bottom) + i];
00220 argv[top - (middle - bottom) + i] = tem;
00221 }
00222
00223 top -= len;
00224 }
00225 else
00226 {
00227
00228 int len = top - middle;
00229 register int i;
00230
00231
00232 for (i = 0; i < len; i++)
00233 {
00234 tem = argv[bottom + i];
00235 argv[bottom + i] = argv[middle + i];
00236 argv[middle + i] = tem;
00237 }
00238
00239 bottom += len;
00240 }
00241 }
00242
00243
00244
00245 first_nonopt += (optind - last_nonopt);
00246 last_nonopt = optind;
00247 }
00248
00249
00250
00251 static const char *
00252 _getopt_initialize (int argc, char *const *argv, const char *optstring)
00253 {
00254
00255 (void)argc;
00256 (void)argv;
00257
00258
00259
00260
00261
00262 first_nonopt = last_nonopt = optind;
00263
00264 nextchar = NULL;
00265
00266 posixly_correct = getenv ("POSIXLY_CORRECT");
00267
00268
00269
00270 if (optstring[0] == '-')
00271 {
00272 ordering = RETURN_IN_ORDER;
00273 ++optstring;
00274 }
00275 else if (optstring[0] == '+')
00276 {
00277 ordering = REQUIRE_ORDER;
00278 ++optstring;
00279 }
00280 else if (posixly_correct != NULL)
00281 ordering = REQUIRE_ORDER;
00282 else
00283 ordering = PERMUTE;
00284
00285 return optstring;
00286 }
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 int
00345 gnu_getopt_internal_(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *longind, int long_only)
00346 {
00347 int print_errors = opterr;
00348 if (optstring[0] == ':')
00349 print_errors = 0;
00350
00351 if (argc < 1)
00352 return -1;
00353
00354 optarg = NULL;
00355
00356 if (optind == 0 || !__getopt_initialized)
00357 {
00358 if (optind == 0)
00359 optind = 1;
00360 optstring = _getopt_initialize (argc, argv, optstring);
00361 __getopt_initialized = 1;
00362 }
00363
00364
00365
00366 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
00367
00368 if (nextchar == NULL || *nextchar == '\0')
00369 {
00370
00371
00372
00373
00374 if (last_nonopt > optind)
00375 last_nonopt = optind;
00376 if (first_nonopt > optind)
00377 first_nonopt = optind;
00378
00379 if (ordering == PERMUTE)
00380 {
00381
00382
00383
00384 if (first_nonopt != last_nonopt && last_nonopt != optind)
00385 exchange (const_cast<char **>(argv));
00386 else if (last_nonopt != optind)
00387 first_nonopt = optind;
00388
00389
00390
00391
00392 while (optind < argc && NONOPTION_P)
00393 optind++;
00394 last_nonopt = optind;
00395 }
00396
00397
00398
00399
00400
00401
00402 if (optind != argc && !strcmp (argv[optind], "--"))
00403 {
00404 optind++;
00405
00406 if (first_nonopt != last_nonopt && last_nonopt != optind)
00407 exchange (const_cast<char **>(argv));
00408 else if (first_nonopt == last_nonopt)
00409 first_nonopt = optind;
00410 last_nonopt = argc;
00411
00412 optind = argc;
00413 }
00414
00415
00416
00417
00418 if (optind == argc)
00419 {
00420
00421
00422 if (first_nonopt != last_nonopt)
00423 optind = first_nonopt;
00424 return -1;
00425 }
00426
00427
00428
00429
00430 if (NONOPTION_P)
00431 {
00432 if (ordering == REQUIRE_ORDER)
00433 return -1;
00434 optarg = argv[optind++];
00435 return 1;
00436 }
00437
00438
00439
00440
00441 nextchar = (argv[optind] + 1
00442 + (longopts != NULL && argv[optind][1] == '-'));
00443 }
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 if (longopts != NULL
00461 && (argv[optind][1] == '-'
00462 || (long_only && (argv[optind][2] || !strchr (optstring, argv[optind][1])))))
00463 {
00464 char *nameend;
00465 const struct option *p;
00466 const struct option *pfound = NULL;
00467 int exact = 0;
00468 int ambig = 0;
00469 int indfound = -1;
00470 int option_index;
00471
00472 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
00473 ;
00474
00475
00476
00477 for (p = longopts, option_index = 0; p->name; p++, option_index++)
00478 if (!strncmp (p->name, nextchar, nameend - nextchar))
00479 {
00480 if (unsigned(nameend - nextchar) == unsigned(strlen(p->name)))
00481 {
00482
00483 pfound = p;
00484 indfound = option_index;
00485 exact = 1;
00486 break;
00487 }
00488 else if (pfound == NULL)
00489 {
00490
00491 pfound = p;
00492 indfound = option_index;
00493 }
00494 else if (long_only
00495 || pfound->has_arg != p->has_arg
00496 || pfound->flag != p->flag
00497 || pfound->val != p->val)
00498
00499 ambig = 1;
00500 }
00501
00502 if (ambig && !exact)
00503 {
00504 if (print_errors)
00505 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
00506 argv[0], argv[optind]);
00507 nextchar += strlen (nextchar);
00508 optind++;
00509 optopt = 0;
00510 return '?';
00511 }
00512
00513 if (pfound != NULL)
00514 {
00515 option_index = indfound;
00516 optind++;
00517 if (*nameend)
00518 {
00519
00520
00521 if (pfound->has_arg)
00522 optarg = nameend + 1;
00523 else
00524 {
00525 if (print_errors)
00526 {
00527 if (argv[optind - 1][1] == '-')
00528
00529 fprintf (stderr,
00530 _("%s: option `--%s' doesn't allow an argument\n"),
00531 argv[0], pfound->name);
00532 else
00533
00534 fprintf (stderr,
00535 _("%s: option `%c%s' doesn't allow an argument\n"),
00536 argv[0], argv[optind - 1][0], pfound->name);
00537 }
00538
00539 nextchar += strlen (nextchar);
00540
00541 optopt = pfound->val;
00542 return '?';
00543 }
00544 }
00545 else if (pfound->has_arg == 1)
00546 {
00547 if (optind < argc)
00548 optarg = argv[optind++];
00549 else
00550 {
00551 if (print_errors)
00552 fprintf (stderr,
00553 _("%s: option `%s' requires an argument\n"),
00554 argv[0], argv[optind - 1]);
00555 nextchar += strlen (nextchar);
00556 optopt = pfound->val;
00557 return optstring[0] == ':' ? ':' : '?';
00558 }
00559 }
00560 nextchar += strlen (nextchar);
00561 if (longind != NULL)
00562 *longind = option_index;
00563 if (pfound->flag)
00564 {
00565 *(pfound->flag) = pfound->val;
00566 return 0;
00567 }
00568 return pfound->val;
00569 }
00570
00571
00572
00573
00574
00575 if (!long_only || argv[optind][1] == '-'
00576 || strchr (optstring, *nextchar) == NULL)
00577 {
00578 if (print_errors)
00579 {
00580 if (argv[optind][1] == '-')
00581
00582 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
00583 argv[0], nextchar);
00584 else
00585
00586 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
00587 argv[0], argv[optind][0], nextchar);
00588 }
00589 nextchar = const_cast<char *>("");
00590 optind++;
00591 optopt = 0;
00592 return '?';
00593 }
00594 }
00595
00596
00597
00598 {
00599 char c = *nextchar++;
00600 const char *temp = strchr (optstring, c);
00601
00602
00603 if (*nextchar == '\0')
00604 ++optind;
00605
00606 if (temp == NULL || c == ':')
00607 {
00608 if (print_errors)
00609 {
00610 if (posixly_correct)
00611
00612 fprintf (stderr, _("%s: illegal option -- %c\n"),
00613 argv[0], c);
00614 else
00615 fprintf (stderr, _("%s: invalid option -- %c\n"),
00616 argv[0], c);
00617 }
00618 optopt = c;
00619 return '?';
00620 }
00621
00622 if (temp[0] == 'W' && temp[1] == ';')
00623 {
00624 char *nameend;
00625 const struct option *p;
00626 const struct option *pfound = NULL;
00627 int exact = 0;
00628 int ambig = 0;
00629 int indfound = 0;
00630 int option_index;
00631
00632
00633 if (*nextchar != '\0')
00634 {
00635 optarg = nextchar;
00636
00637
00638 optind++;
00639 }
00640 else if (optind == argc)
00641 {
00642 if (print_errors)
00643 {
00644
00645 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
00646 argv[0], c);
00647 }
00648 optopt = c;
00649 if (optstring[0] == ':')
00650 c = ':';
00651 else
00652 c = '?';
00653 return c;
00654 }
00655 else
00656
00657
00658 optarg = argv[optind++];
00659
00660
00661
00662
00663 for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
00664 ;
00665
00666
00667
00668 for (p = longopts, option_index = 0; p->name; p++, option_index++)
00669 if (!strncmp (p->name, nextchar, nameend - nextchar))
00670 {
00671 if (unsigned(nameend - nextchar) == unsigned(strlen(p->name)))
00672 {
00673
00674 pfound = p;
00675 indfound = option_index;
00676 exact = 1;
00677 break;
00678 }
00679 else if (pfound == NULL)
00680 {
00681
00682 pfound = p;
00683 indfound = option_index;
00684 }
00685 else
00686
00687 ambig = 1;
00688 }
00689 if (ambig && !exact)
00690 {
00691 if (print_errors)
00692 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
00693 argv[0], argv[optind]);
00694 nextchar += strlen (nextchar);
00695 optind++;
00696 return '?';
00697 }
00698 if (pfound != NULL)
00699 {
00700 option_index = indfound;
00701 if (*nameend)
00702 {
00703
00704
00705 if (pfound->has_arg)
00706 optarg = nameend + 1;
00707 else
00708 {
00709 if (print_errors)
00710 fprintf (stderr, _("\
00711 %s: option `-W %s' doesn't allow an argument\n"),
00712 argv[0], pfound->name);
00713
00714 nextchar += strlen (nextchar);
00715 return '?';
00716 }
00717 }
00718 else if (pfound->has_arg == 1)
00719 {
00720 if (optind < argc)
00721 optarg = argv[optind++];
00722 else
00723 {
00724 if (print_errors)
00725 fprintf (stderr,
00726 _("%s: option `%s' requires an argument\n"),
00727 argv[0], argv[optind - 1]);
00728 nextchar += strlen (nextchar);
00729 return optstring[0] == ':' ? ':' : '?';
00730 }
00731 }
00732 nextchar += strlen (nextchar);
00733 if (longind != NULL)
00734 *longind = option_index;
00735 if (pfound->flag)
00736 {
00737 *(pfound->flag) = pfound->val;
00738 return 0;
00739 }
00740 return pfound->val;
00741 }
00742 nextchar = NULL;
00743 return 'W';
00744 }
00745 if (temp[1] == ':')
00746 {
00747 if (temp[2] == ':')
00748 {
00749
00750 if (*nextchar != '\0')
00751 {
00752 optarg = nextchar;
00753 optind++;
00754 }
00755 else
00756 optarg = NULL;
00757 nextchar = NULL;
00758 }
00759 else
00760 {
00761
00762 if (*nextchar != '\0')
00763 {
00764 optarg = nextchar;
00765
00766
00767 optind++;
00768 }
00769 else if (optind == argc)
00770 {
00771 if (print_errors)
00772 {
00773
00774 fprintf (stderr,
00775 _("%s: option requires an argument -- %c\n"),
00776 argv[0], c);
00777 }
00778 optopt = c;
00779 if (optstring[0] == ':')
00780 c = ':';
00781 else
00782 c = '?';
00783 }
00784 else
00785
00786
00787 optarg = argv[optind++];
00788 nextchar = NULL;
00789 }
00790 }
00791 return c;
00792 }
00793 }
00794
00795 #endif