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