| 1 | /* Getopt for Microsoft C | 
| 2 | This code is a modification of the Free Software Foundation, Inc. | 
| 3 | Getopt library for parsing command line argument the purpose was | 
| 4 | to provide a Microsoft Visual C friendly derivative. This code | 
| 5 | provides functionality for both Unicode and Multibyte builds. | 
| 6 |  | 
| 7 | Date: 02/03/2011 - Ludvik Jerabek - Initial Release | 
| 8 | Version: 1.0 | 
| 9 | Comment: Supports getopt, getopt_long, and getopt_long_only | 
| 10 | and POSIXLY_CORRECT environment flag | 
| 11 | License: LGPL | 
| 12 |  | 
| 13 | Revisions: | 
| 14 |  | 
| 15 | 02/03/2011 - Ludvik Jerabek - Initial Release | 
| 16 | 02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4 | 
| 17 | 07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs | 
| 18 | 08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception | 
| 19 | 08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB | 
| 20 | 02/15/2012 - Ludvik Jerabek - Fixed _GETOPT_THROW definition missing in implementation file | 
| 21 | 08/01/2012 - Ludvik Jerabek - Created separate functions for char and wchar_t characters so single dll can do both unicode and ansi | 
| 22 |  | 
| 23 | **DISCLAIMER** | 
| 24 | THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | 
| 25 | EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE | 
| 26 | IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | 
| 27 | PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE | 
| 28 | EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT | 
| 29 | APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY | 
| 30 | DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY | 
| 31 | USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST | 
| 32 | PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON | 
| 33 | YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE | 
| 34 | EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | 
| 35 | */ | 
| 36 |  | 
| 37 | #define _CRT_SECURE_NO_WARNINGS | 
| 38 | #include <stdlib.h> | 
| 39 | #include <stdio.h> | 
| 40 | #include "utils/wingetopt.h" | 
| 41 |  | 
| 42 | #ifdef __cplusplus | 
| 43 | #define _GETOPT_THROW throw() | 
| 44 | #else | 
| 45 | #define _GETOPT_THROW | 
| 46 | #endif | 
| 47 |  | 
| 48 | enum ENUM_ORDERING { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER }; | 
| 49 |  | 
| 50 | struct _getopt_data_a | 
| 51 | { | 
| 52 | int optind; | 
| 53 | int opterr; | 
| 54 | int optopt; | 
| 55 | char *optarg; | 
| 56 | int __initialized; | 
| 57 | char *__nextchar; | 
| 58 | int __ordering; | 
| 59 | int __posixly_correct; | 
| 60 | int __first_nonopt; | 
| 61 | int __last_nonopt; | 
| 62 | }; | 
| 63 | struct _getopt_data_w | 
| 64 | { | 
| 65 | int optind; | 
| 66 | int opterr; | 
| 67 | int optopt; | 
| 68 | wchar_t *optarg; | 
| 69 | int __initialized; | 
| 70 | wchar_t *__nextchar; | 
| 71 | int __ordering; | 
| 72 | int __posixly_correct; | 
| 73 | int __first_nonopt; | 
| 74 | int __last_nonopt; | 
| 75 | }; | 
| 76 |  | 
| 77 | static struct _getopt_data_a getopt_data_a; | 
| 78 | static struct _getopt_data_w getopt_data_w; | 
| 79 | char *optarg_a; | 
| 80 | wchar_t *optarg_w; | 
| 81 |  | 
| 82 | int optind = 1; | 
| 83 | int opterr = 1; | 
| 84 | int optopt = '?'; | 
| 85 |  | 
| 86 | static void exchange_a(char **argv, struct _getopt_data_a *d) | 
| 87 | { | 
| 88 | int bottom = d->__first_nonopt; | 
| 89 | int middle = d->__last_nonopt; | 
| 90 | int top = d->optind; | 
| 91 | char *tem; | 
| 92 | while (top > middle && middle > bottom) | 
| 93 | { | 
| 94 | if (top - middle > middle - bottom) | 
| 95 | { | 
| 96 | int len = middle - bottom; | 
| 97 | register int i; | 
| 98 | for (i = 0; i < len; i++) | 
| 99 | { | 
| 100 | tem = argv[bottom + i]; | 
| 101 | argv[bottom + i] = argv[top - (middle - bottom) + i]; | 
| 102 | argv[top - (middle - bottom) + i] = tem; | 
| 103 | } | 
| 104 | top -= len; | 
| 105 | } | 
| 106 | else | 
| 107 | { | 
| 108 | int len = top - middle; | 
| 109 | register int i; | 
| 110 | for (i = 0; i < len; i++) | 
| 111 | { | 
| 112 | tem = argv[bottom + i]; | 
| 113 | argv[bottom + i] = argv[middle + i]; | 
| 114 | argv[middle + i] = tem; | 
| 115 | } | 
| 116 | bottom += len; | 
| 117 | } | 
| 118 | } | 
| 119 | d->__first_nonopt += (d->optind - d->__last_nonopt); | 
| 120 | d->__last_nonopt = d->optind; | 
| 121 | } | 
| 122 |  | 
| 123 | static void exchange_w(wchar_t **argv, struct _getopt_data_w *d) | 
| 124 | { | 
| 125 | int bottom = d->__first_nonopt; | 
| 126 | int middle = d->__last_nonopt; | 
| 127 | int top = d->optind; | 
| 128 | wchar_t *tem; | 
| 129 | while (top > middle && middle > bottom) | 
| 130 | { | 
| 131 | if (top - middle > middle - bottom) | 
| 132 | { | 
| 133 | int len = middle - bottom; | 
| 134 | register int i; | 
| 135 | for (i = 0; i < len; i++) | 
| 136 | { | 
| 137 | tem = argv[bottom + i]; | 
| 138 | argv[bottom + i] = argv[top - (middle - bottom) + i]; | 
| 139 | argv[top - (middle - bottom) + i] = tem; | 
| 140 | } | 
| 141 | top -= len; | 
| 142 | } | 
| 143 | else | 
| 144 | { | 
| 145 | int len = top - middle; | 
| 146 | register int i; | 
| 147 | for (i = 0; i < len; i++) | 
| 148 | { | 
| 149 | tem = argv[bottom + i]; | 
| 150 | argv[bottom + i] = argv[middle + i]; | 
| 151 | argv[middle + i] = tem; | 
| 152 | } | 
| 153 | bottom += len; | 
| 154 | } | 
| 155 | } | 
| 156 | d->__first_nonopt += (d->optind - d->__last_nonopt); | 
| 157 | d->__last_nonopt = d->optind; | 
| 158 | } | 
| 159 |  | 
| 160 | static const char *_getopt_initialize_a (const char *optstring, struct _getopt_data_a *d, int posixly_correct) | 
| 161 | { | 
| 162 | d->__first_nonopt = d->__last_nonopt = d->optind; | 
| 163 | d->__nextchar = NULL; | 
| 164 | d->__posixly_correct = posixly_correct | !!getenv("POSIXLY_CORRECT"); | 
| 165 |  | 
| 166 |  | 
| 167 | if (optstring[0] == '-') | 
| 168 | { | 
| 169 | d->__ordering = RETURN_IN_ORDER; | 
| 170 | ++optstring; | 
| 171 | } | 
| 172 | else if (optstring[0] == '+') | 
| 173 | { | 
| 174 | d->__ordering = REQUIRE_ORDER; | 
| 175 | ++optstring; | 
| 176 | } | 
| 177 | else if (d->__posixly_correct) | 
| 178 | d->__ordering = REQUIRE_ORDER; | 
| 179 | else | 
| 180 | d->__ordering = PERMUTE; | 
| 181 | return optstring; | 
| 182 | } | 
| 183 |  | 
| 184 | static const wchar_t *_getopt_initialize_w (const wchar_t *optstring, struct _getopt_data_w *d, int posixly_correct) | 
| 185 | { | 
| 186 | d->__first_nonopt = d->__last_nonopt = d->optind; | 
| 187 | d->__nextchar = NULL; | 
| 188 | d->__posixly_correct = posixly_correct | !!_wgetenv(L"POSIXLY_CORRECT"); | 
| 189 |  | 
| 190 |  | 
| 191 | if (optstring[0] == L'-') | 
| 192 | { | 
| 193 | d->__ordering = RETURN_IN_ORDER; | 
| 194 | ++optstring; | 
| 195 | } | 
| 196 | else if (optstring[0] == L'+') | 
| 197 | { | 
| 198 | d->__ordering = REQUIRE_ORDER; | 
| 199 | ++optstring; | 
| 200 | } | 
| 201 | else if (d->__posixly_correct) | 
| 202 | d->__ordering = REQUIRE_ORDER; | 
| 203 | else | 
| 204 | d->__ordering = PERMUTE; | 
| 205 | return optstring; | 
| 206 | } | 
| 207 |  | 
| 208 | int _getopt_internal_r_a (int argc, char *const *argv, const char *optstring, const struct option_a *longopts, int *longind, int long_only, struct _getopt_data_a *d, int posixly_correct) | 
| 209 | { | 
| 210 | int print_errors = d->opterr; | 
| 211 |  | 
| 212 | if (argc < 1) | 
| 213 | return -1; | 
| 214 |  | 
| 215 | d->optarg = NULL; | 
| 216 |  | 
| 217 | if (d->optind == 0 || !d->__initialized) | 
| 218 | { | 
| 219 | if (d->optind == 0) | 
| 220 | d->optind = 1; | 
| 221 | optstring = _getopt_initialize_a (optstring, d, posixly_correct); | 
| 222 | d->__initialized = 1; | 
| 223 | } | 
| 224 | else if (optstring[0] == '-' || optstring[0] == '+') | 
| 225 | optstring++; | 
| 226 | if (optstring[0] == ':') | 
| 227 | print_errors = 0; | 
| 228 |  | 
| 229 | if (d->__nextchar == NULL || *d->__nextchar == '\0') | 
| 230 | { | 
| 231 | if (d->__last_nonopt > d->optind) | 
| 232 | d->__last_nonopt = d->optind; | 
| 233 | if (d->__first_nonopt > d->optind) | 
| 234 | d->__first_nonopt = d->optind; | 
| 235 |  | 
| 236 | if (d->__ordering == PERMUTE) | 
| 237 | { | 
| 238 | if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) | 
| 239 | exchange_a ((char **) argv, d); | 
| 240 | else if (d->__last_nonopt != d->optind) | 
| 241 | d->__first_nonopt = d->optind; | 
| 242 |  | 
| 243 | while (d->optind < argc && (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')) | 
| 244 | d->optind++; | 
| 245 | d->__last_nonopt = d->optind; | 
| 246 | } | 
| 247 |  | 
| 248 | if (d->optind != argc && !strcmp(argv[d->optind], "--")) | 
| 249 | { | 
| 250 | d->optind++; | 
| 251 |  | 
| 252 | if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) | 
| 253 | exchange_a((char **) argv, d); | 
| 254 | else if (d->__first_nonopt == d->__last_nonopt) | 
| 255 | d->__first_nonopt = d->optind; | 
| 256 | d->__last_nonopt = argc; | 
| 257 |  | 
| 258 | d->optind = argc; | 
| 259 | } | 
| 260 |  | 
| 261 | if (d->optind == argc) | 
| 262 | { | 
| 263 | if (d->__first_nonopt != d->__last_nonopt) | 
| 264 | d->optind = d->__first_nonopt; | 
| 265 | return -1; | 
| 266 | } | 
| 267 |  | 
| 268 | if ((argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')) | 
| 269 | { | 
| 270 | if (d->__ordering == REQUIRE_ORDER) | 
| 271 | return -1; | 
| 272 | d->optarg = argv[d->optind++]; | 
| 273 | return 1; | 
| 274 | } | 
| 275 |  | 
| 276 | d->__nextchar = (argv[d->optind] + 1 + (longopts != NULL && argv[d->optind][1] == '-')); | 
| 277 | } | 
| 278 |  | 
| 279 | if (longopts != NULL && (argv[d->optind][1] == '-' || (long_only && (argv[d->optind][2] || !strchr(optstring, argv[d->optind][1]))))) | 
| 280 | { | 
| 281 | char *nameend; | 
| 282 | const struct option_a *p; | 
| 283 | const struct option_a *pfound = NULL; | 
| 284 | int exact = 0; | 
| 285 | int ambig = 0; | 
| 286 | int indfound = -1; | 
| 287 | int option_index; | 
| 288 |  | 
| 289 | for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++); | 
| 290 |  | 
| 291 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | 
| 292 | if (!strncmp(p->name, d->__nextchar, nameend - d->__nextchar)) | 
| 293 | { | 
| 294 | if ((unsigned int)(nameend - d->__nextchar) == (unsigned int)strlen(p->name)) | 
| 295 | { | 
| 296 | pfound = p; | 
| 297 | indfound = option_index; | 
| 298 | exact = 1; | 
| 299 | break; | 
| 300 | } | 
| 301 | else if (pfound == NULL) | 
| 302 | { | 
| 303 | pfound = p; | 
| 304 | indfound = option_index; | 
| 305 | } | 
| 306 | else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) | 
| 307 | ambig = 1; | 
| 308 | } | 
| 309 |  | 
| 310 | if (ambig && !exact) | 
| 311 | { | 
| 312 | if (print_errors) | 
| 313 | { | 
| 314 | fprintf(stderr, "%s: option '%s' is ambiguous\n", | 
| 315 | argv[0], argv[d->optind]); | 
| 316 | } | 
| 317 | d->__nextchar += strlen(d->__nextchar); | 
| 318 | d->optind++; | 
| 319 | d->optopt = 0; | 
| 320 | return '?'; | 
| 321 | } | 
| 322 |  | 
| 323 | if (pfound != NULL) | 
| 324 | { | 
| 325 | option_index = indfound; | 
| 326 | d->optind++; | 
| 327 | if (*nameend) | 
| 328 | { | 
| 329 | if (pfound->has_arg) | 
| 330 | d->optarg = nameend + 1; | 
| 331 | else | 
| 332 | { | 
| 333 | if (print_errors) | 
| 334 | { | 
| 335 | if (argv[d->optind - 1][1] == '-') | 
| 336 | { | 
| 337 | fprintf(stderr, "%s: option '--%s' doesn't allow an argument\n",argv[0], pfound->name); | 
| 338 | } | 
| 339 | else | 
| 340 | { | 
| 341 | fprintf(stderr, "%s: option '%c%s' doesn't allow an argument\n",argv[0], argv[d->optind - 1][0],pfound->name); | 
| 342 | } | 
| 343 |  | 
| 344 | } | 
| 345 |  | 
| 346 | d->__nextchar += strlen(d->__nextchar); | 
| 347 |  | 
| 348 | d->optopt = pfound->val; | 
| 349 | return '?'; | 
| 350 | } | 
| 351 | } | 
| 352 | else if (pfound->has_arg == 1) | 
| 353 | { | 
| 354 | if (d->optind < argc) | 
| 355 | d->optarg = argv[d->optind++]; | 
| 356 | else | 
| 357 | { | 
| 358 | if (print_errors) | 
| 359 | { | 
| 360 | fprintf(stderr,"%s: option '--%s' requires an argument\n",argv[0], pfound->name); | 
| 361 | } | 
| 362 | d->__nextchar += strlen(d->__nextchar); | 
| 363 | d->optopt = pfound->val; | 
| 364 | return optstring[0] == ':' ? ':' : '?'; | 
| 365 | } | 
| 366 | } | 
| 367 | d->__nextchar += strlen(d->__nextchar); | 
| 368 | if (longind != NULL) | 
| 369 | *longind = option_index; | 
| 370 | if (pfound->flag) | 
| 371 | { | 
| 372 | *(pfound->flag) = pfound->val; | 
| 373 | return 0; | 
| 374 | } | 
| 375 | return pfound->val; | 
| 376 | } | 
| 377 |  | 
| 378 | if (!long_only || argv[d->optind][1] == '-' || strchr(optstring, *d->__nextchar) == NULL) | 
| 379 | { | 
| 380 | if (print_errors) | 
| 381 | { | 
| 382 | if (argv[d->optind][1] == '-') | 
| 383 | { | 
| 384 | /* --option */ | 
| 385 | fprintf(stderr, "%s: unrecognized option '--%s'\n",argv[0], d->__nextchar); | 
| 386 | } | 
| 387 | else | 
| 388 | { | 
| 389 | /* +option or -option */ | 
| 390 | fprintf(stderr, "%s: unrecognized option '%c%s'\n",argv[0], argv[d->optind][0], d->__nextchar); | 
| 391 | } | 
| 392 | } | 
| 393 | d->__nextchar = (char *)""; | 
| 394 | d->optind++; | 
| 395 | d->optopt = 0; | 
| 396 | return '?'; | 
| 397 | } | 
| 398 | } | 
| 399 |  | 
| 400 | { | 
| 401 | char c = *d->__nextchar++; | 
| 402 | char *temp = (char*)strchr(optstring, c); | 
| 403 |  | 
| 404 | if (*d->__nextchar == '\0') | 
| 405 | ++d->optind; | 
| 406 |  | 
| 407 | if (temp == NULL || c == ':' || c == ';') | 
| 408 | { | 
| 409 | if (print_errors) | 
| 410 | { | 
| 411 | fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], c); | 
| 412 | } | 
| 413 | d->optopt = c; | 
| 414 | return '?'; | 
| 415 | } | 
| 416 | if (temp[0] == 'W' && temp[1] == ';') | 
| 417 | { | 
| 418 | char *nameend; | 
| 419 | const struct option_a *p; | 
| 420 | const struct option_a *pfound = NULL; | 
| 421 | int exact = 0; | 
| 422 | int ambig = 0; | 
| 423 | int indfound = 0; | 
| 424 | int option_index; | 
| 425 |  | 
| 426 | if (*d->__nextchar != '\0') | 
| 427 | { | 
| 428 | d->optarg = d->__nextchar; | 
| 429 | d->optind++; | 
| 430 | } | 
| 431 | else if (d->optind == argc) | 
| 432 | { | 
| 433 | if (print_errors) | 
| 434 | { | 
| 435 | fprintf(stderr, | 
| 436 | "%s: option requires an argument -- '%c'\n", | 
| 437 | argv[0], c); | 
| 438 | } | 
| 439 | d->optopt = c; | 
| 440 | if (optstring[0] == ':') | 
| 441 | c = ':'; | 
| 442 | else | 
| 443 | c = '?'; | 
| 444 | return c; | 
| 445 | } | 
| 446 | else | 
| 447 | d->optarg = argv[d->optind++]; | 
| 448 |  | 
| 449 | for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '='; nameend++); | 
| 450 |  | 
| 451 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | 
| 452 | if (!strncmp(p->name, d->__nextchar, nameend - d->__nextchar)) | 
| 453 | { | 
| 454 | if ((unsigned int) (nameend - d->__nextchar) == strlen(p->name)) | 
| 455 | { | 
| 456 | pfound = p; | 
| 457 | indfound = option_index; | 
| 458 | exact = 1; | 
| 459 | break; | 
| 460 | } | 
| 461 | else if (pfound == NULL) | 
| 462 | { | 
| 463 | pfound = p; | 
| 464 | indfound = option_index; | 
| 465 | } | 
| 466 | else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) | 
| 467 | ambig = 1; | 
| 468 | } | 
| 469 | if (ambig && !exact) | 
| 470 | { | 
| 471 | if (print_errors) | 
| 472 | { | 
| 473 | fprintf(stderr, "%s: option '-W %s' is ambiguous\n", | 
| 474 | argv[0], d->optarg); | 
| 475 | } | 
| 476 | d->__nextchar += strlen(d->__nextchar); | 
| 477 | d->optind++; | 
| 478 | return '?'; | 
| 479 | } | 
| 480 | if (pfound != NULL) | 
| 481 | { | 
| 482 | option_index = indfound; | 
| 483 | if (*nameend) | 
| 484 | { | 
| 485 | if (pfound->has_arg) | 
| 486 | d->optarg = nameend + 1; | 
| 487 | else | 
| 488 | { | 
| 489 | if (print_errors) | 
| 490 | { | 
| 491 | fprintf(stderr, "\ | 
| 492 | %s: option '-W %s' doesn't allow an argument\n", | 
| 493 | argv[0], pfound->name); | 
| 494 | } | 
| 495 |  | 
| 496 | d->__nextchar += strlen(d->__nextchar); | 
| 497 | return '?'; | 
| 498 | } | 
| 499 | } | 
| 500 | else if (pfound->has_arg == 1) | 
| 501 | { | 
| 502 | if (d->optind < argc) | 
| 503 | d->optarg = argv[d->optind++]; | 
| 504 | else | 
| 505 | { | 
| 506 | if (print_errors) | 
| 507 | { | 
| 508 | fprintf(stderr, "\ | 
| 509 | %s: option '-W %s' requires an argument\n", | 
| 510 | argv[0], pfound->name); | 
| 511 | } | 
| 512 | d->__nextchar += strlen(d->__nextchar); | 
| 513 | return optstring[0] == ':' ? ':' : '?'; | 
| 514 | } | 
| 515 | } | 
| 516 | else | 
| 517 | d->optarg = NULL; | 
| 518 | d->__nextchar += strlen(d->__nextchar); | 
| 519 | if (longind != NULL) | 
| 520 | *longind = option_index; | 
| 521 | if (pfound->flag) | 
| 522 | { | 
| 523 | *(pfound->flag) = pfound->val; | 
| 524 | return 0; | 
| 525 | } | 
| 526 | return pfound->val; | 
| 527 | } | 
| 528 | d->__nextchar = NULL; | 
| 529 | return 'W'; | 
| 530 | } | 
| 531 | if (temp[1] == ':') | 
| 532 | { | 
| 533 | if (temp[2] == ':') | 
| 534 | { | 
| 535 | if (*d->__nextchar != '\0') | 
| 536 | { | 
| 537 | d->optarg = d->__nextchar; | 
| 538 | d->optind++; | 
| 539 | } | 
| 540 | else | 
| 541 | d->optarg = NULL; | 
| 542 | d->__nextchar = NULL; | 
| 543 | } | 
| 544 | else | 
| 545 | { | 
| 546 | if (*d->__nextchar != '\0') | 
| 547 | { | 
| 548 | d->optarg = d->__nextchar; | 
| 549 | d->optind++; | 
| 550 | } | 
| 551 | else if (d->optind == argc) | 
| 552 | { | 
| 553 | if (print_errors) | 
| 554 | { | 
| 555 | fprintf(stderr, | 
| 556 | "%s: option requires an argument -- '%c'\n", | 
| 557 | argv[0], c); | 
| 558 | } | 
| 559 | d->optopt = c; | 
| 560 | if (optstring[0] == ':') | 
| 561 | c = ':'; | 
| 562 | else | 
| 563 | c = '?'; | 
| 564 | } | 
| 565 | else | 
| 566 | d->optarg = argv[d->optind++]; | 
| 567 | d->__nextchar = NULL; | 
| 568 | } | 
| 569 | } | 
| 570 | return c; | 
| 571 | } | 
| 572 | } | 
| 573 |  | 
| 574 | int _getopt_internal_r_w (int argc, wchar_t *const *argv, const wchar_t *optstring, const struct option_w *longopts, int *longind, int long_only, struct _getopt_data_w *d, int posixly_correct) | 
| 575 | { | 
| 576 | int print_errors = d->opterr; | 
| 577 |  | 
| 578 | if (argc < 1) | 
| 579 | return -1; | 
| 580 |  | 
| 581 | d->optarg = NULL; | 
| 582 |  | 
| 583 | if (d->optind == 0 || !d->__initialized) | 
| 584 | { | 
| 585 | if (d->optind == 0) | 
| 586 | d->optind = 1; | 
| 587 | optstring = _getopt_initialize_w (optstring, d, posixly_correct); | 
| 588 | d->__initialized = 1; | 
| 589 | } | 
| 590 | else if (optstring[0] == L'-' || optstring[0] == L'+') | 
| 591 | optstring++; | 
| 592 | if (optstring[0] == L':') | 
| 593 | print_errors = 0; | 
| 594 |  | 
| 595 | if (d->__nextchar == NULL || *d->__nextchar == L'\0') | 
| 596 | { | 
| 597 | if (d->__last_nonopt > d->optind) | 
| 598 | d->__last_nonopt = d->optind; | 
| 599 | if (d->__first_nonopt > d->optind) | 
| 600 | d->__first_nonopt = d->optind; | 
| 601 |  | 
| 602 | if (d->__ordering == PERMUTE) | 
| 603 | { | 
| 604 | if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) | 
| 605 | exchange_w((wchar_t **) argv, d); | 
| 606 | else if (d->__last_nonopt != d->optind) | 
| 607 | d->__first_nonopt = d->optind; | 
| 608 |  | 
| 609 | while (d->optind < argc && (argv[d->optind][0] != L'-' || argv[d->optind][1] == L'\0')) | 
| 610 | d->optind++; | 
| 611 | d->__last_nonopt = d->optind; | 
| 612 | } | 
| 613 |  | 
| 614 | if (d->optind != argc && !wcscmp(argv[d->optind], L"--")) | 
| 615 | { | 
| 616 | d->optind++; | 
| 617 |  | 
| 618 | if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) | 
| 619 | exchange_w((wchar_t **) argv, d); | 
| 620 | else if (d->__first_nonopt == d->__last_nonopt) | 
| 621 | d->__first_nonopt = d->optind; | 
| 622 | d->__last_nonopt = argc; | 
| 623 |  | 
| 624 | d->optind = argc; | 
| 625 | } | 
| 626 |  | 
| 627 | if (d->optind == argc) | 
| 628 | { | 
| 629 | if (d->__first_nonopt != d->__last_nonopt) | 
| 630 | d->optind = d->__first_nonopt; | 
| 631 | return -1; | 
| 632 | } | 
| 633 |  | 
| 634 | if ((argv[d->optind][0] != L'-' || argv[d->optind][1] == L'\0')) | 
| 635 | { | 
| 636 | if (d->__ordering == REQUIRE_ORDER) | 
| 637 | return -1; | 
| 638 | d->optarg = argv[d->optind++]; | 
| 639 | return 1; | 
| 640 | } | 
| 641 |  | 
| 642 | d->__nextchar = (argv[d->optind] + 1 + (longopts != NULL && argv[d->optind][1] == L'-')); | 
| 643 | } | 
| 644 |  | 
| 645 | if (longopts != NULL && (argv[d->optind][1] == L'-' || (long_only && (argv[d->optind][2] || !wcschr(optstring, argv[d->optind][1]))))) | 
| 646 | { | 
| 647 | wchar_t *nameend; | 
| 648 | const struct option_w *p; | 
| 649 | const struct option_w *pfound = NULL; | 
| 650 | int exact = 0; | 
| 651 | int ambig = 0; | 
| 652 | int indfound = -1; | 
| 653 | int option_index; | 
| 654 |  | 
| 655 | for (nameend = d->__nextchar; *nameend && *nameend != L'='; nameend++); | 
| 656 |  | 
| 657 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | 
| 658 | if (!wcsncmp(p->name, d->__nextchar, nameend - d->__nextchar)) | 
| 659 | { | 
| 660 | if ((unsigned int)(nameend - d->__nextchar) == (unsigned int)wcslen(p->name)) | 
| 661 | { | 
| 662 | pfound = p; | 
| 663 | indfound = option_index; | 
| 664 | exact = 1; | 
| 665 | break; | 
| 666 | } | 
| 667 | else if (pfound == NULL) | 
| 668 | { | 
| 669 | pfound = p; | 
| 670 | indfound = option_index; | 
| 671 | } | 
| 672 | else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) | 
| 673 | ambig = 1; | 
| 674 | } | 
| 675 |  | 
| 676 | if (ambig && !exact) | 
| 677 | { | 
| 678 | if (print_errors) | 
| 679 | { | 
| 680 | fwprintf(stderr, L"%s: option '%s' is ambiguous\n", | 
| 681 | argv[0], argv[d->optind]); | 
| 682 | } | 
| 683 | d->__nextchar += wcslen(d->__nextchar); | 
| 684 | d->optind++; | 
| 685 | d->optopt = 0; | 
| 686 | return L'?'; | 
| 687 | } | 
| 688 |  | 
| 689 | if (pfound != NULL) | 
| 690 | { | 
| 691 | option_index = indfound; | 
| 692 | d->optind++; | 
| 693 | if (*nameend) | 
| 694 | { | 
| 695 | if (pfound->has_arg) | 
| 696 | d->optarg = nameend + 1; | 
| 697 | else | 
| 698 | { | 
| 699 | if (print_errors) | 
| 700 | { | 
| 701 | if (argv[d->optind - 1][1] == L'-') | 
| 702 | { | 
| 703 | fwprintf(stderr, L"%s: option '--%s' doesn't allow an argument\n",argv[0], pfound->name); | 
| 704 | } | 
| 705 | else | 
| 706 | { | 
| 707 | fwprintf(stderr, L"%s: option '%c%s' doesn't allow an argument\n",argv[0], argv[d->optind - 1][0],pfound->name); | 
| 708 | } | 
| 709 |  | 
| 710 | } | 
| 711 |  | 
| 712 | d->__nextchar += wcslen(d->__nextchar); | 
| 713 |  | 
| 714 | d->optopt = pfound->val; | 
| 715 | return L'?'; | 
| 716 | } | 
| 717 | } | 
| 718 | else if (pfound->has_arg == 1) | 
| 719 | { | 
| 720 | if (d->optind < argc) | 
| 721 | d->optarg = argv[d->optind++]; | 
| 722 | else | 
| 723 | { | 
| 724 | if (print_errors) | 
| 725 | { | 
| 726 | fwprintf(stderr,L"%s: option '--%s' requires an argument\n",argv[0], pfound->name); | 
| 727 | } | 
| 728 | d->__nextchar += wcslen(d->__nextchar); | 
| 729 | d->optopt = pfound->val; | 
| 730 | return optstring[0] == L':' ? L':' : L'?'; | 
| 731 | } | 
| 732 | } | 
| 733 | d->__nextchar += wcslen(d->__nextchar); | 
| 734 | if (longind != NULL) | 
| 735 | *longind = option_index; | 
| 736 | if (pfound->flag) | 
| 737 | { | 
| 738 | *(pfound->flag) = pfound->val; | 
| 739 | return 0; | 
| 740 | } | 
| 741 | return pfound->val; | 
| 742 | } | 
| 743 |  | 
| 744 | if (!long_only || argv[d->optind][1] == L'-' || wcschr(optstring, *d->__nextchar) == NULL) | 
| 745 | { | 
| 746 | if (print_errors) | 
| 747 | { | 
| 748 | if (argv[d->optind][1] == L'-') | 
| 749 | { | 
| 750 | /* --option */ | 
| 751 | fwprintf(stderr, L"%s: unrecognized option '--%s'\n",argv[0], d->__nextchar); | 
| 752 | } | 
| 753 | else | 
| 754 | { | 
| 755 | /* +option or -option */ | 
| 756 | fwprintf(stderr, L"%s: unrecognized option '%c%s'\n",argv[0], argv[d->optind][0], d->__nextchar); | 
| 757 | } | 
| 758 | } | 
| 759 | d->__nextchar = (wchar_t *)L""; | 
| 760 | d->optind++; | 
| 761 | d->optopt = 0; | 
| 762 | return L'?'; | 
| 763 | } | 
| 764 | } | 
| 765 |  | 
| 766 | { | 
| 767 | wchar_t c = *d->__nextchar++; | 
| 768 | wchar_t *temp = (wchar_t*)wcschr(optstring, c); | 
| 769 |  | 
| 770 | if (*d->__nextchar == L'\0') | 
| 771 | ++d->optind; | 
| 772 |  | 
| 773 | if (temp == NULL || c == L':' || c == L';') | 
| 774 | { | 
| 775 | if (print_errors) | 
| 776 | { | 
| 777 | fwprintf(stderr, L"%s: invalid option -- '%c'\n", argv[0], c); | 
| 778 | } | 
| 779 | d->optopt = c; | 
| 780 | return L'?'; | 
| 781 | } | 
| 782 | if (temp[0] == L'W' && temp[1] == L';') | 
| 783 | { | 
| 784 | wchar_t *nameend; | 
| 785 | const struct option_w *p; | 
| 786 | const struct option_w *pfound = NULL; | 
| 787 | int exact = 0; | 
| 788 | int ambig = 0; | 
| 789 | int indfound = 0; | 
| 790 | int option_index; | 
| 791 |  | 
| 792 | if (*d->__nextchar != L'\0') | 
| 793 | { | 
| 794 | d->optarg = d->__nextchar; | 
| 795 | d->optind++; | 
| 796 | } | 
| 797 | else if (d->optind == argc) | 
| 798 | { | 
| 799 | if (print_errors) | 
| 800 | { | 
| 801 | fwprintf(stderr, | 
| 802 | L"%s: option requires an argument -- '%c'\n", | 
| 803 | argv[0], c); | 
| 804 | } | 
| 805 | d->optopt = c; | 
| 806 | if (optstring[0] == L':') | 
| 807 | c = L':'; | 
| 808 | else | 
| 809 | c = L'?'; | 
| 810 | return c; | 
| 811 | } | 
| 812 | else | 
| 813 | d->optarg = argv[d->optind++]; | 
| 814 |  | 
| 815 | for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != L'='; nameend++); | 
| 816 |  | 
| 817 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | 
| 818 | if (!wcsncmp(p->name, d->__nextchar, nameend - d->__nextchar)) | 
| 819 | { | 
| 820 | if ((unsigned int) (nameend - d->__nextchar) == wcslen(p->name)) | 
| 821 | { | 
| 822 | pfound = p; | 
| 823 | indfound = option_index; | 
| 824 | exact = 1; | 
| 825 | break; | 
| 826 | } | 
| 827 | else if (pfound == NULL) | 
| 828 | { | 
| 829 | pfound = p; | 
| 830 | indfound = option_index; | 
| 831 | } | 
| 832 | else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) | 
| 833 | ambig = 1; | 
| 834 | } | 
| 835 | if (ambig && !exact) | 
| 836 | { | 
| 837 | if (print_errors) | 
| 838 | { | 
| 839 | fwprintf(stderr, L"%s: option '-W %s' is ambiguous\n", | 
| 840 | argv[0], d->optarg); | 
| 841 | } | 
| 842 | d->__nextchar += wcslen(d->__nextchar); | 
| 843 | d->optind++; | 
| 844 | return L'?'; | 
| 845 | } | 
| 846 | if (pfound != NULL) | 
| 847 | { | 
| 848 | option_index = indfound; | 
| 849 | if (*nameend) | 
| 850 | { | 
| 851 | if (pfound->has_arg) | 
| 852 | d->optarg = nameend + 1; | 
| 853 | else | 
| 854 | { | 
| 855 | if (print_errors) | 
| 856 | { | 
| 857 | fwprintf(stderr, L"\ | 
| 858 | %s: option '-W %s' doesn't allow an argument\n", | 
| 859 | argv[0], pfound->name); | 
| 860 | } | 
| 861 |  | 
| 862 | d->__nextchar += wcslen(d->__nextchar); | 
| 863 | return L'?'; | 
| 864 | } | 
| 865 | } | 
| 866 | else if (pfound->has_arg == 1) | 
| 867 | { | 
| 868 | if (d->optind < argc) | 
| 869 | d->optarg = argv[d->optind++]; | 
| 870 | else | 
| 871 | { | 
| 872 | if (print_errors) | 
| 873 | { | 
| 874 | fwprintf(stderr, L"\ | 
| 875 | %s: option '-W %s' requires an argument\n", | 
| 876 | argv[0], pfound->name); | 
| 877 | } | 
| 878 | d->__nextchar += wcslen(d->__nextchar); | 
| 879 | return optstring[0] == L':' ? L':' : L'?'; | 
| 880 | } | 
| 881 | } | 
| 882 | else | 
| 883 | d->optarg = NULL; | 
| 884 | d->__nextchar += wcslen(d->__nextchar); | 
| 885 | if (longind != NULL) | 
| 886 | *longind = option_index; | 
| 887 | if (pfound->flag) | 
| 888 | { | 
| 889 | *(pfound->flag) = pfound->val; | 
| 890 | return 0; | 
| 891 | } | 
| 892 | return pfound->val; | 
| 893 | } | 
| 894 | d->__nextchar = NULL; | 
| 895 | return L'W'; | 
| 896 | } | 
| 897 | if (temp[1] == L':') | 
| 898 | { | 
| 899 | if (temp[2] == L':') | 
| 900 | { | 
| 901 | if (*d->__nextchar != L'\0') | 
| 902 | { | 
| 903 | d->optarg = d->__nextchar; | 
| 904 | d->optind++; | 
| 905 | } | 
| 906 | else | 
| 907 | d->optarg = NULL; | 
| 908 | d->__nextchar = NULL; | 
| 909 | } | 
| 910 | else | 
| 911 | { | 
| 912 | if (*d->__nextchar != L'\0') | 
| 913 | { | 
| 914 | d->optarg = d->__nextchar; | 
| 915 | d->optind++; | 
| 916 | } | 
| 917 | else if (d->optind == argc) | 
| 918 | { | 
| 919 | if (print_errors) | 
| 920 | { | 
| 921 | fwprintf(stderr, | 
| 922 | L"%s: option requires an argument -- '%c'\n", | 
| 923 | argv[0], c); | 
| 924 | } | 
| 925 | d->optopt = c; | 
| 926 | if (optstring[0] == L':') | 
| 927 | c = L':'; | 
| 928 | else | 
| 929 | c = L'?'; | 
| 930 | } | 
| 931 | else | 
| 932 | d->optarg = argv[d->optind++]; | 
| 933 | d->__nextchar = NULL; | 
| 934 | } | 
| 935 | } | 
| 936 | return c; | 
| 937 | } | 
| 938 | } | 
| 939 |  | 
| 940 | int _getopt_internal_a (int argc, char *const *argv, const char *optstring, const struct option_a *longopts, int *longind, int long_only, int posixly_correct) | 
| 941 | { | 
| 942 | int result; | 
| 943 | getopt_data_a.optind = optind; | 
| 944 | getopt_data_a.opterr = opterr; | 
| 945 | result = _getopt_internal_r_a (argc, argv, optstring, longopts,longind, long_only, &getopt_data_a,posixly_correct); | 
| 946 | optind = getopt_data_a.optind; | 
| 947 | optarg_a = getopt_data_a.optarg; | 
| 948 | optopt = getopt_data_a.optopt; | 
| 949 | return result; | 
| 950 | } | 
| 951 |  | 
| 952 | int _getopt_internal_w (int argc, wchar_t *const *argv, const wchar_t *optstring, const struct option_w *longopts, int *longind, int long_only, int posixly_correct) | 
| 953 | { | 
| 954 | int result; | 
| 955 | getopt_data_w.optind = optind; | 
| 956 | getopt_data_w.opterr = opterr; | 
| 957 | result = _getopt_internal_r_w (argc, argv, optstring, longopts,longind, long_only, &getopt_data_w,posixly_correct); | 
| 958 | optind = getopt_data_w.optind; | 
| 959 | optarg_w = getopt_data_w.optarg; | 
| 960 | optopt = getopt_data_w.optopt; | 
| 961 | return result; | 
| 962 | } | 
| 963 |  | 
| 964 | int getopt_a (int argc, char *const *argv, const char *optstring) _GETOPT_THROW | 
| 965 | { | 
| 966 | return _getopt_internal_a (argc, argv, optstring, (const struct option_a *) 0, (int *) 0, 0, 0); | 
| 967 | } | 
| 968 |  | 
| 969 | int getopt_w (int argc, wchar_t *const *argv, const wchar_t *optstring) _GETOPT_THROW | 
| 970 | { | 
| 971 | return _getopt_internal_w (argc, argv, optstring, (const struct option_w *) 0, (int *) 0, 0, 0); | 
| 972 | } | 
| 973 |  | 
| 974 | int getopt_long_a (int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW | 
| 975 | { | 
| 976 | return _getopt_internal_a (argc, argv, options, long_options, opt_index, 0, 0); | 
| 977 | } | 
| 978 |  | 
| 979 | int getopt_long_w (int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index) _GETOPT_THROW | 
| 980 | { | 
| 981 | return _getopt_internal_w (argc, argv, options, long_options, opt_index, 0, 0); | 
| 982 | } | 
| 983 |  | 
| 984 | int _getopt_long_r_a (int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index, struct _getopt_data_a *d) | 
| 985 | { | 
| 986 | return _getopt_internal_r_a (argc, argv, options, long_options, opt_index,0, d, 0); | 
| 987 | } | 
| 988 |  | 
| 989 | int _getopt_long_r_w (int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index, struct _getopt_data_w *d) | 
| 990 | { | 
| 991 | return _getopt_internal_r_w (argc, argv, options, long_options, opt_index,0, d, 0); | 
| 992 | } | 
| 993 |  | 
| 994 | int getopt_long_only_a (int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW | 
| 995 | { | 
| 996 | return _getopt_internal_a (argc, argv, options, long_options, opt_index, 1, 0); | 
| 997 | } | 
| 998 |  | 
| 999 | int getopt_long_only_w (int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index) _GETOPT_THROW | 
| 1000 | { | 
| 1001 | return _getopt_internal_w (argc, argv, options, long_options, opt_index, 1, 0); | 
| 1002 | } | 
| 1003 |  | 
| 1004 | int _getopt_long_only_r_a (int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index, struct _getopt_data_a *d) | 
| 1005 | { | 
| 1006 | return _getopt_internal_r_a (argc, argv, options, long_options, opt_index, 1, d, 0); | 
| 1007 | } | 
| 1008 |  | 
| 1009 | int _getopt_long_only_r_w (int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index, struct _getopt_data_w *d) | 
| 1010 | { | 
| 1011 | return _getopt_internal_r_w (argc, argv, options, long_options, opt_index, 1, d, 0); | 
| 1012 | } |