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