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