diff options
| -rw-r--r-- | CMakeLists.txt | 4 | ||||
| -rw-r--r-- | externals/getopt/CMakeLists.txt | 11 | ||||
| -rw-r--r-- | externals/getopt/getopt.c | 963 | ||||
| -rw-r--r-- | externals/getopt/getopt.h | 136 | ||||
| -rw-r--r-- | src/citra/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | src/citra/citra.cpp | 40 |
6 files changed, 1156 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 3658ef48e..59f76fa93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
| @@ -203,6 +203,10 @@ add_subdirectory(${INI_PREFIX}) | |||
| 203 | 203 | ||
| 204 | include_directories(externals/nihstro/include) | 204 | include_directories(externals/nihstro/include) |
| 205 | 205 | ||
| 206 | if (MSVC) | ||
| 207 | add_subdirectory(externals/getopt) | ||
| 208 | endif() | ||
| 209 | |||
| 206 | # process subdirectories | 210 | # process subdirectories |
| 207 | if(ENABLE_QT) | 211 | if(ENABLE_QT) |
| 208 | include_directories(externals/qhexedit) | 212 | include_directories(externals/qhexedit) |
diff --git a/externals/getopt/CMakeLists.txt b/externals/getopt/CMakeLists.txt new file mode 100644 index 000000000..b4709a506 --- /dev/null +++ b/externals/getopt/CMakeLists.txt | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | set(SRCS | ||
| 2 | getopt.c | ||
| 3 | ) | ||
| 4 | set(HEADERS | ||
| 5 | getopt.h | ||
| 6 | ) | ||
| 7 | |||
| 8 | create_directory_groups(${SRCS} ${HEADERS}) | ||
| 9 | add_library(getopt ${SRCS} ${HEADERS}) | ||
| 10 | target_compile_definitions(getopt INTERFACE STATIC_GETOPT) | ||
| 11 | target_include_directories(getopt INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file | ||
diff --git a/externals/getopt/getopt.c b/externals/getopt/getopt.c new file mode 100644 index 000000000..faf84089f --- /dev/null +++ b/externals/getopt/getopt.c | |||
| @@ -0,0 +1,963 @@ | |||
| 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 | 10/15/2012 - Ludvik Jerabek - Modified to match latest GNU features | ||
| 23 | 06/19/2015 - Ludvik Jerabek - Fixed maximum option limitation caused by option_a (255) and option_w (65535) structure val variable | ||
| 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. | ||
| 37 | */ | ||
| 38 | #define _CRT_SECURE_NO_WARNINGS | ||
| 39 | #include <stdlib.h> | ||
| 40 | #include <stdio.h> | ||
| 41 | #ifdef _MSC_VER | ||
| 42 | #include <malloc.h> | ||
| 43 | #endif | ||
| 44 | #include "getopt.h" | ||
| 45 | |||
| 46 | #ifdef __cplusplus | ||
| 47 | #define _GETOPT_THROW throw() | ||
| 48 | #else | ||
| 49 | #define _GETOPT_THROW | ||
| 50 | #endif | ||
| 51 | |||
| 52 | int optind = 1; | ||
| 53 | int opterr = 1; | ||
| 54 | int optopt = '?'; | ||
| 55 | enum ENUM_ORDERING { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER }; | ||
| 56 | |||
| 57 | |||
| 58 | static struct _getopt_data_a | ||
| 59 | { | ||
| 60 | int optind; | ||
| 61 | int opterr; | ||
| 62 | int optopt; | ||
| 63 | char *optarg; | ||
| 64 | int __initialized; | ||
| 65 | char *__nextchar; | ||
| 66 | enum ENUM_ORDERING __ordering; | ||
| 67 | int __posixly_correct; | ||
| 68 | int __first_nonopt; | ||
| 69 | int __last_nonopt; | ||
| 70 | } getopt_data_a; | ||
| 71 | char *optarg_a; | ||
| 72 | |||
| 73 | static void exchange_a(char **argv, struct _getopt_data_a *d) | ||
| 74 | { | ||
| 75 | int bottom = d->__first_nonopt; | ||
| 76 | int middle = d->__last_nonopt; | ||
| 77 | int top = d->optind; | ||
| 78 | char *tem; | ||
| 79 | while (top > middle && middle > bottom) | ||
| 80 | { | ||
| 81 | if (top - middle > middle - bottom) | ||
| 82 | { | ||
| 83 | int len = middle - bottom; | ||
| 84 | register int i; | ||
| 85 | for (i = 0; i < len; i++) | ||
| 86 | { | ||
| 87 | tem = argv[bottom + i]; | ||
| 88 | argv[bottom + i] = argv[top - (middle - bottom) + i]; | ||
| 89 | argv[top - (middle - bottom) + i] = tem; | ||
| 90 | } | ||
| 91 | top -= len; | ||
| 92 | } | ||
| 93 | else | ||
| 94 | { | ||
| 95 | int len = top - middle; | ||
| 96 | register int i; | ||
| 97 | for (i = 0; i < len; i++) | ||
| 98 | { | ||
| 99 | tem = argv[bottom + i]; | ||
| 100 | argv[bottom + i] = argv[middle + i]; | ||
| 101 | argv[middle + i] = tem; | ||
| 102 | } | ||
| 103 | bottom += len; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | d->__first_nonopt += (d->optind - d->__last_nonopt); | ||
| 107 | d->__last_nonopt = d->optind; | ||
| 108 | } | ||
| 109 | static const char *_getopt_initialize_a(const char *optstring, struct _getopt_data_a *d, int posixly_correct) | ||
| 110 | { | ||
| 111 | d->__first_nonopt = d->__last_nonopt = d->optind; | ||
| 112 | d->__nextchar = NULL; | ||
| 113 | d->__posixly_correct = posixly_correct | !!getenv("POSIXLY_CORRECT"); | ||
| 114 | if (optstring[0] == '-') | ||
| 115 | { | ||
| 116 | d->__ordering = RETURN_IN_ORDER; | ||
| 117 | ++optstring; | ||
| 118 | } | ||
| 119 | else if (optstring[0] == '+') | ||
| 120 | { | ||
| 121 | d->__ordering = REQUIRE_ORDER; | ||
| 122 | ++optstring; | ||
| 123 | } | ||
| 124 | else if (d->__posixly_correct) | ||
| 125 | d->__ordering = REQUIRE_ORDER; | ||
| 126 | else | ||
| 127 | d->__ordering = PERMUTE; | ||
| 128 | return optstring; | ||
| 129 | } | ||
| 130 | 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) | ||
| 131 | { | ||
| 132 | int print_errors = d->opterr; | ||
| 133 | if (argc < 1) | ||
| 134 | return -1; | ||
| 135 | d->optarg = NULL; | ||
| 136 | if (d->optind == 0 || !d->__initialized) | ||
| 137 | { | ||
| 138 | if (d->optind == 0) | ||
| 139 | d->optind = 1; | ||
| 140 | optstring = _getopt_initialize_a(optstring, d, posixly_correct); | ||
| 141 | d->__initialized = 1; | ||
| 142 | } | ||
| 143 | else if (optstring[0] == '-' || optstring[0] == '+') | ||
| 144 | optstring++; | ||
| 145 | if (optstring[0] == ':') | ||
| 146 | print_errors = 0; | ||
| 147 | if (d->__nextchar == NULL || *d->__nextchar == '\0') | ||
| 148 | { | ||
| 149 | if (d->__last_nonopt > d->optind) | ||
| 150 | d->__last_nonopt = d->optind; | ||
| 151 | if (d->__first_nonopt > d->optind) | ||
| 152 | d->__first_nonopt = d->optind; | ||
| 153 | if (d->__ordering == PERMUTE) | ||
| 154 | { | ||
| 155 | if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) | ||
| 156 | exchange_a((char **)argv, d); | ||
| 157 | else if (d->__last_nonopt != d->optind) | ||
| 158 | d->__first_nonopt = d->optind; | ||
| 159 | while (d->optind < argc && (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')) | ||
| 160 | d->optind++; | ||
| 161 | d->__last_nonopt = d->optind; | ||
| 162 | } | ||
| 163 | if (d->optind != argc && !strcmp(argv[d->optind], "--")) | ||
| 164 | { | ||
| 165 | d->optind++; | ||
| 166 | if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) | ||
| 167 | exchange_a((char **)argv, d); | ||
| 168 | else if (d->__first_nonopt == d->__last_nonopt) | ||
| 169 | d->__first_nonopt = d->optind; | ||
| 170 | d->__last_nonopt = argc; | ||
| 171 | d->optind = argc; | ||
| 172 | } | ||
| 173 | if (d->optind == argc) | ||
| 174 | { | ||
| 175 | if (d->__first_nonopt != d->__last_nonopt) | ||
| 176 | d->optind = d->__first_nonopt; | ||
| 177 | return -1; | ||
| 178 | } | ||
| 179 | if ((argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')) | ||
| 180 | { | ||
| 181 | if (d->__ordering == REQUIRE_ORDER) | ||
| 182 | return -1; | ||
| 183 | d->optarg = argv[d->optind++]; | ||
| 184 | return 1; | ||
| 185 | } | ||
| 186 | d->__nextchar = (argv[d->optind] + 1 + (longopts != NULL && argv[d->optind][1] == '-')); | ||
| 187 | } | ||
| 188 | if (longopts != NULL && (argv[d->optind][1] == '-' || (long_only && (argv[d->optind][2] || !strchr(optstring, argv[d->optind][1]))))) | ||
| 189 | { | ||
| 190 | char *nameend; | ||
| 191 | unsigned int namelen; | ||
| 192 | const struct option_a *p; | ||
| 193 | const struct option_a *pfound = NULL; | ||
| 194 | struct option_list | ||
| 195 | { | ||
| 196 | const struct option_a *p; | ||
| 197 | struct option_list *next; | ||
| 198 | } *ambig_list = NULL; | ||
| 199 | int exact = 0; | ||
| 200 | int indfound = -1; | ||
| 201 | int option_index; | ||
| 202 | for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++); | ||
| 203 | namelen = (unsigned int)(nameend - d->__nextchar); | ||
| 204 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | ||
| 205 | if (!strncmp(p->name, d->__nextchar, namelen)) | ||
| 206 | { | ||
| 207 | if (namelen == (unsigned int)strlen(p->name)) | ||
| 208 | { | ||
| 209 | pfound = p; | ||
| 210 | indfound = option_index; | ||
| 211 | exact = 1; | ||
| 212 | break; | ||
| 213 | } | ||
| 214 | else if (pfound == NULL) | ||
| 215 | { | ||
| 216 | pfound = p; | ||
| 217 | indfound = option_index; | ||
| 218 | } | ||
| 219 | else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) | ||
| 220 | { | ||
| 221 | struct option_list *newp = (struct option_list*)alloca(sizeof(*newp)); | ||
| 222 | newp->p = p; | ||
| 223 | newp->next = ambig_list; | ||
| 224 | ambig_list = newp; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | if (ambig_list != NULL && !exact) | ||
| 228 | { | ||
| 229 | if (print_errors) | ||
| 230 | { | ||
| 231 | struct option_list first; | ||
| 232 | first.p = pfound; | ||
| 233 | first.next = ambig_list; | ||
| 234 | ambig_list = &first; | ||
| 235 | fprintf(stderr, "%s: option '%s' is ambiguous; possibilities:", argv[0], argv[d->optind]); | ||
| 236 | do | ||
| 237 | { | ||
| 238 | fprintf(stderr, " '--%s'", ambig_list->p->name); | ||
| 239 | ambig_list = ambig_list->next; | ||
| 240 | } while (ambig_list != NULL); | ||
| 241 | fputc('\n', stderr); | ||
| 242 | } | ||
| 243 | d->__nextchar += strlen(d->__nextchar); | ||
| 244 | d->optind++; | ||
| 245 | d->optopt = 0; | ||
| 246 | return '?'; | ||
| 247 | } | ||
| 248 | if (pfound != NULL) | ||
| 249 | { | ||
| 250 | option_index = indfound; | ||
| 251 | d->optind++; | ||
| 252 | if (*nameend) | ||
| 253 | { | ||
| 254 | if (pfound->has_arg) | ||
| 255 | d->optarg = nameend + 1; | ||
| 256 | else | ||
| 257 | { | ||
| 258 | if (print_errors) | ||
| 259 | { | ||
| 260 | if (argv[d->optind - 1][1] == '-') | ||
| 261 | { | ||
| 262 | fprintf(stderr, "%s: option '--%s' doesn't allow an argument\n", argv[0], pfound->name); | ||
| 263 | } | ||
| 264 | else | ||
| 265 | { | ||
| 266 | fprintf(stderr, "%s: option '%c%s' doesn't allow an argument\n", argv[0], argv[d->optind - 1][0], pfound->name); | ||
| 267 | } | ||
| 268 | } | ||
| 269 | d->__nextchar += strlen(d->__nextchar); | ||
| 270 | d->optopt = pfound->val; | ||
| 271 | return '?'; | ||
| 272 | } | ||
| 273 | } | ||
| 274 | else if (pfound->has_arg == 1) | ||
| 275 | { | ||
| 276 | if (d->optind < argc) | ||
| 277 | d->optarg = argv[d->optind++]; | ||
| 278 | else | ||
| 279 | { | ||
| 280 | if (print_errors) | ||
| 281 | { | ||
| 282 | fprintf(stderr, "%s: option '--%s' requires an argument\n", argv[0], pfound->name); | ||
| 283 | } | ||
| 284 | d->__nextchar += strlen(d->__nextchar); | ||
| 285 | d->optopt = pfound->val; | ||
| 286 | return optstring[0] == ':' ? ':' : '?'; | ||
| 287 | } | ||
| 288 | } | ||
| 289 | d->__nextchar += strlen(d->__nextchar); | ||
| 290 | if (longind != NULL) | ||
| 291 | *longind = option_index; | ||
| 292 | if (pfound->flag) | ||
| 293 | { | ||
| 294 | *(pfound->flag) = pfound->val; | ||
| 295 | return 0; | ||
| 296 | } | ||
| 297 | return pfound->val; | ||
| 298 | } | ||
| 299 | if (!long_only || argv[d->optind][1] == '-' || strchr(optstring, *d->__nextchar) == NULL) | ||
| 300 | { | ||
| 301 | if (print_errors) | ||
| 302 | { | ||
| 303 | if (argv[d->optind][1] == '-') | ||
| 304 | { | ||
| 305 | fprintf(stderr, "%s: unrecognized option '--%s'\n", argv[0], d->__nextchar); | ||
| 306 | } | ||
| 307 | else | ||
| 308 | { | ||
| 309 | fprintf(stderr, "%s: unrecognized option '%c%s'\n", argv[0], argv[d->optind][0], d->__nextchar); | ||
| 310 | } | ||
| 311 | } | ||
| 312 | d->__nextchar = (char *)""; | ||
| 313 | d->optind++; | ||
| 314 | d->optopt = 0; | ||
| 315 | return '?'; | ||
| 316 | } | ||
| 317 | } | ||
| 318 | { | ||
| 319 | char c = *d->__nextchar++; | ||
| 320 | char *temp = (char*)strchr(optstring, c); | ||
| 321 | if (*d->__nextchar == '\0') | ||
| 322 | ++d->optind; | ||
| 323 | if (temp == NULL || c == ':' || c == ';') | ||
| 324 | { | ||
| 325 | if (print_errors) | ||
| 326 | { | ||
| 327 | fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], c); | ||
| 328 | } | ||
| 329 | d->optopt = c; | ||
| 330 | return '?'; | ||
| 331 | } | ||
| 332 | if (temp[0] == 'W' && temp[1] == ';') | ||
| 333 | { | ||
| 334 | char *nameend; | ||
| 335 | const struct option_a *p; | ||
| 336 | const struct option_a *pfound = NULL; | ||
| 337 | int exact = 0; | ||
| 338 | int ambig = 0; | ||
| 339 | int indfound = 0; | ||
| 340 | int option_index; | ||
| 341 | if (longopts == NULL) | ||
| 342 | goto no_longs; | ||
| 343 | if (*d->__nextchar != '\0') | ||
| 344 | { | ||
| 345 | d->optarg = d->__nextchar; | ||
| 346 | d->optind++; | ||
| 347 | } | ||
| 348 | else if (d->optind == argc) | ||
| 349 | { | ||
| 350 | if (print_errors) | ||
| 351 | { | ||
| 352 | fprintf(stderr, "%s: option requires an argument -- '%c'\n", argv[0], c); | ||
| 353 | } | ||
| 354 | d->optopt = c; | ||
| 355 | if (optstring[0] == ':') | ||
| 356 | c = ':'; | ||
| 357 | else | ||
| 358 | c = '?'; | ||
| 359 | return c; | ||
| 360 | } | ||
| 361 | else | ||
| 362 | d->optarg = argv[d->optind++]; | ||
| 363 | for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '='; nameend++); | ||
| 364 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | ||
| 365 | if (!strncmp(p->name, d->__nextchar, nameend - d->__nextchar)) | ||
| 366 | { | ||
| 367 | if ((unsigned int)(nameend - d->__nextchar) == strlen(p->name)) | ||
| 368 | { | ||
| 369 | pfound = p; | ||
| 370 | indfound = option_index; | ||
| 371 | exact = 1; | ||
| 372 | break; | ||
| 373 | } | ||
| 374 | else if (pfound == NULL) | ||
| 375 | { | ||
| 376 | pfound = p; | ||
| 377 | indfound = option_index; | ||
| 378 | } | ||
| 379 | else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) | ||
| 380 | ambig = 1; | ||
| 381 | } | ||
| 382 | if (ambig && !exact) | ||
| 383 | { | ||
| 384 | if (print_errors) | ||
| 385 | { | ||
| 386 | fprintf(stderr, "%s: option '-W %s' is ambiguous\n", argv[0], d->optarg); | ||
| 387 | } | ||
| 388 | d->__nextchar += strlen(d->__nextchar); | ||
| 389 | d->optind++; | ||
| 390 | return '?'; | ||
| 391 | } | ||
| 392 | if (pfound != NULL) | ||
| 393 | { | ||
| 394 | option_index = indfound; | ||
| 395 | if (*nameend) | ||
| 396 | { | ||
| 397 | if (pfound->has_arg) | ||
| 398 | d->optarg = nameend + 1; | ||
| 399 | else | ||
| 400 | { | ||
| 401 | if (print_errors) | ||
| 402 | { | ||
| 403 | fprintf(stderr, "%s: option '-W %s' doesn't allow an argument\n", argv[0], pfound->name); | ||
| 404 | } | ||
| 405 | d->__nextchar += strlen(d->__nextchar); | ||
| 406 | return '?'; | ||
| 407 | } | ||
| 408 | } | ||
| 409 | else if (pfound->has_arg == 1) | ||
| 410 | { | ||
| 411 | if (d->optind < argc) | ||
| 412 | d->optarg = argv[d->optind++]; | ||
| 413 | else | ||
| 414 | { | ||
| 415 | if (print_errors) | ||
| 416 | { | ||
| 417 | fprintf(stderr, "%s: option '-W %s' requires an argument\n", argv[0], pfound->name); | ||
| 418 | } | ||
| 419 | d->__nextchar += strlen(d->__nextchar); | ||
| 420 | return optstring[0] == ':' ? ':' : '?'; | ||
| 421 | } | ||
| 422 | } | ||
| 423 | else | ||
| 424 | d->optarg = NULL; | ||
| 425 | d->__nextchar += strlen(d->__nextchar); | ||
| 426 | if (longind != NULL) | ||
| 427 | *longind = option_index; | ||
| 428 | if (pfound->flag) | ||
| 429 | { | ||
| 430 | *(pfound->flag) = pfound->val; | ||
| 431 | return 0; | ||
| 432 | } | ||
| 433 | return pfound->val; | ||
| 434 | } | ||
| 435 | no_longs: | ||
| 436 | d->__nextchar = NULL; | ||
| 437 | return 'W'; | ||
| 438 | } | ||
| 439 | if (temp[1] == ':') | ||
| 440 | { | ||
| 441 | if (temp[2] == ':') | ||
| 442 | { | ||
| 443 | if (*d->__nextchar != '\0') | ||
| 444 | { | ||
| 445 | d->optarg = d->__nextchar; | ||
| 446 | d->optind++; | ||
| 447 | } | ||
| 448 | else | ||
| 449 | d->optarg = NULL; | ||
| 450 | d->__nextchar = NULL; | ||
| 451 | } | ||
| 452 | else | ||
| 453 | { | ||
| 454 | if (*d->__nextchar != '\0') | ||
| 455 | { | ||
| 456 | d->optarg = d->__nextchar; | ||
| 457 | d->optind++; | ||
| 458 | } | ||
| 459 | else if (d->optind == argc) | ||
| 460 | { | ||
| 461 | if (print_errors) | ||
| 462 | { | ||
| 463 | fprintf(stderr, "%s: option requires an argument -- '%c'\n", argv[0], c); | ||
| 464 | } | ||
| 465 | d->optopt = c; | ||
| 466 | if (optstring[0] == ':') | ||
| 467 | c = ':'; | ||
| 468 | else | ||
| 469 | c = '?'; | ||
| 470 | } | ||
| 471 | else | ||
| 472 | d->optarg = argv[d->optind++]; | ||
| 473 | d->__nextchar = NULL; | ||
| 474 | } | ||
| 475 | } | ||
| 476 | return c; | ||
| 477 | } | ||
| 478 | } | ||
| 479 | 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) | ||
| 480 | { | ||
| 481 | int result; | ||
| 482 | getopt_data_a.optind = optind; | ||
| 483 | getopt_data_a.opterr = opterr; | ||
| 484 | result = _getopt_internal_r_a(argc, argv, optstring, longopts, longind, long_only, &getopt_data_a, posixly_correct); | ||
| 485 | optind = getopt_data_a.optind; | ||
| 486 | optarg_a = getopt_data_a.optarg; | ||
| 487 | optopt = getopt_data_a.optopt; | ||
| 488 | return result; | ||
| 489 | } | ||
| 490 | int getopt_a(int argc, char *const *argv, const char *optstring) _GETOPT_THROW | ||
| 491 | { | ||
| 492 | return _getopt_internal_a(argc, argv, optstring, (const struct option_a *) 0, (int *)0, 0, 0); | ||
| 493 | } | ||
| 494 | int getopt_long_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW | ||
| 495 | { | ||
| 496 | return _getopt_internal_a(argc, argv, options, long_options, opt_index, 0, 0); | ||
| 497 | } | ||
| 498 | int getopt_long_only_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW | ||
| 499 | { | ||
| 500 | return _getopt_internal_a(argc, argv, options, long_options, opt_index, 1, 0); | ||
| 501 | } | ||
| 502 | 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) | ||
| 503 | { | ||
| 504 | return _getopt_internal_r_a(argc, argv, options, long_options, opt_index, 0, d, 0); | ||
| 505 | } | ||
| 506 | 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) | ||
| 507 | { | ||
| 508 | return _getopt_internal_r_a(argc, argv, options, long_options, opt_index, 1, d, 0); | ||
| 509 | } | ||
| 510 | |||
| 511 | |||
| 512 | static struct _getopt_data_w | ||
| 513 | { | ||
| 514 | int optind; | ||
| 515 | int opterr; | ||
| 516 | int optopt; | ||
| 517 | wchar_t *optarg; | ||
| 518 | int __initialized; | ||
| 519 | wchar_t *__nextchar; | ||
| 520 | enum ENUM_ORDERING __ordering; | ||
| 521 | int __posixly_correct; | ||
| 522 | int __first_nonopt; | ||
| 523 | int __last_nonopt; | ||
| 524 | } getopt_data_w; | ||
| 525 | wchar_t *optarg_w; | ||
| 526 | |||
| 527 | static void exchange_w(wchar_t **argv, struct _getopt_data_w *d) | ||
| 528 | { | ||
| 529 | int bottom = d->__first_nonopt; | ||
| 530 | int middle = d->__last_nonopt; | ||
| 531 | int top = d->optind; | ||
| 532 | wchar_t *tem; | ||
| 533 | while (top > middle && middle > bottom) | ||
| 534 | { | ||
| 535 | if (top - middle > middle - bottom) | ||
| 536 | { | ||
| 537 | int len = middle - bottom; | ||
| 538 | register int i; | ||
| 539 | for (i = 0; i < len; i++) | ||
| 540 | { | ||
| 541 | tem = argv[bottom + i]; | ||
| 542 | argv[bottom + i] = argv[top - (middle - bottom) + i]; | ||
| 543 | argv[top - (middle - bottom) + i] = tem; | ||
| 544 | } | ||
| 545 | top -= len; | ||
| 546 | } | ||
| 547 | else | ||
| 548 | { | ||
| 549 | int len = top - middle; | ||
| 550 | register int i; | ||
| 551 | for (i = 0; i < len; i++) | ||
| 552 | { | ||
| 553 | tem = argv[bottom + i]; | ||
| 554 | argv[bottom + i] = argv[middle + i]; | ||
| 555 | argv[middle + i] = tem; | ||
| 556 | } | ||
| 557 | bottom += len; | ||
| 558 | } | ||
| 559 | } | ||
| 560 | d->__first_nonopt += (d->optind - d->__last_nonopt); | ||
| 561 | d->__last_nonopt = d->optind; | ||
| 562 | } | ||
| 563 | static const wchar_t *_getopt_initialize_w(const wchar_t *optstring, struct _getopt_data_w *d, int posixly_correct) | ||
| 564 | { | ||
| 565 | d->__first_nonopt = d->__last_nonopt = d->optind; | ||
| 566 | d->__nextchar = NULL; | ||
| 567 | d->__posixly_correct = posixly_correct | !!_wgetenv(L"POSIXLY_CORRECT"); | ||
| 568 | if (optstring[0] == L'-') | ||
| 569 | { | ||
| 570 | d->__ordering = RETURN_IN_ORDER; | ||
| 571 | ++optstring; | ||
| 572 | } | ||
| 573 | else if (optstring[0] == L'+') | ||
| 574 | { | ||
| 575 | d->__ordering = REQUIRE_ORDER; | ||
| 576 | ++optstring; | ||
| 577 | } | ||
| 578 | else if (d->__posixly_correct) | ||
| 579 | d->__ordering = REQUIRE_ORDER; | ||
| 580 | else | ||
| 581 | d->__ordering = PERMUTE; | ||
| 582 | return optstring; | ||
| 583 | } | ||
| 584 | 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) | ||
| 585 | { | ||
| 586 | int print_errors = d->opterr; | ||
| 587 | if (argc < 1) | ||
| 588 | return -1; | ||
| 589 | d->optarg = NULL; | ||
| 590 | if (d->optind == 0 || !d->__initialized) | ||
| 591 | { | ||
| 592 | if (d->optind == 0) | ||
| 593 | d->optind = 1; | ||
| 594 | optstring = _getopt_initialize_w(optstring, d, posixly_correct); | ||
| 595 | d->__initialized = 1; | ||
| 596 | } | ||
| 597 | else if (optstring[0] == L'-' || optstring[0] == L'+') | ||
| 598 | optstring++; | ||
| 599 | if (optstring[0] == L':') | ||
| 600 | print_errors = 0; | ||
| 601 | if (d->__nextchar == NULL || *d->__nextchar == L'\0') | ||
| 602 | { | ||
| 603 | if (d->__last_nonopt > d->optind) | ||
| 604 | d->__last_nonopt = d->optind; | ||
| 605 | if (d->__first_nonopt > d->optind) | ||
| 606 | d->__first_nonopt = d->optind; | ||
| 607 | if (d->__ordering == PERMUTE) | ||
| 608 | { | ||
| 609 | if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) | ||
| 610 | exchange_w((wchar_t **)argv, d); | ||
| 611 | else if (d->__last_nonopt != d->optind) | ||
| 612 | d->__first_nonopt = d->optind; | ||
| 613 | while (d->optind < argc && (argv[d->optind][0] != L'-' || argv[d->optind][1] == L'\0')) | ||
| 614 | d->optind++; | ||
| 615 | d->__last_nonopt = d->optind; | ||
| 616 | } | ||
| 617 | if (d->optind != argc && !wcscmp(argv[d->optind], L"--")) | ||
| 618 | { | ||
| 619 | d->optind++; | ||
| 620 | if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) | ||
| 621 | exchange_w((wchar_t **)argv, d); | ||
| 622 | else if (d->__first_nonopt == d->__last_nonopt) | ||
| 623 | d->__first_nonopt = d->optind; | ||
| 624 | d->__last_nonopt = argc; | ||
| 625 | d->optind = argc; | ||
| 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 | if ((argv[d->optind][0] != L'-' || argv[d->optind][1] == L'\0')) | ||
| 634 | { | ||
| 635 | if (d->__ordering == REQUIRE_ORDER) | ||
| 636 | return -1; | ||
| 637 | d->optarg = argv[d->optind++]; | ||
| 638 | return 1; | ||
| 639 | } | ||
| 640 | d->__nextchar = (argv[d->optind] + 1 + (longopts != NULL && argv[d->optind][1] == L'-')); | ||
| 641 | } | ||
| 642 | if (longopts != NULL && (argv[d->optind][1] == L'-' || (long_only && (argv[d->optind][2] || !wcschr(optstring, argv[d->optind][1]))))) | ||
| 643 | { | ||
| 644 | wchar_t *nameend; | ||
| 645 | unsigned int namelen; | ||
| 646 | const struct option_w *p; | ||
| 647 | const struct option_w *pfound = NULL; | ||
| 648 | struct option_list | ||
| 649 | { | ||
| 650 | const struct option_w *p; | ||
| 651 | struct option_list *next; | ||
| 652 | } *ambig_list = NULL; | ||
| 653 | int exact = 0; | ||
| 654 | int indfound = -1; | ||
| 655 | int option_index; | ||
| 656 | for (nameend = d->__nextchar; *nameend && *nameend != L'='; nameend++); | ||
| 657 | namelen = (unsigned int)(nameend - d->__nextchar); | ||
| 658 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | ||
| 659 | if (!wcsncmp(p->name, d->__nextchar, namelen)) | ||
| 660 | { | ||
| 661 | if (namelen == (unsigned int)wcslen(p->name)) | ||
| 662 | { | ||
| 663 | pfound = p; | ||
| 664 | indfound = option_index; | ||
| 665 | exact = 1; | ||
| 666 | break; | ||
| 667 | } | ||
| 668 | else if (pfound == NULL) | ||
| 669 | { | ||
| 670 | pfound = p; | ||
| 671 | indfound = option_index; | ||
| 672 | } | ||
| 673 | else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) | ||
| 674 | { | ||
| 675 | struct option_list *newp = (struct option_list*)alloca(sizeof(*newp)); | ||
| 676 | newp->p = p; | ||
| 677 | newp->next = ambig_list; | ||
| 678 | ambig_list = newp; | ||
| 679 | } | ||
| 680 | } | ||
| 681 | if (ambig_list != NULL && !exact) | ||
| 682 | { | ||
| 683 | if (print_errors) | ||
| 684 | { | ||
| 685 | struct option_list first; | ||
| 686 | first.p = pfound; | ||
| 687 | first.next = ambig_list; | ||
| 688 | ambig_list = &first; | ||
| 689 | fwprintf(stderr, L"%s: option '%s' is ambiguous; possibilities:", argv[0], argv[d->optind]); | ||
| 690 | do | ||
| 691 | { | ||
| 692 | fwprintf(stderr, L" '--%s'", ambig_list->p->name); | ||
| 693 | ambig_list = ambig_list->next; | ||
| 694 | } while (ambig_list != NULL); | ||
| 695 | fputwc(L'\n', stderr); | ||
| 696 | } | ||
| 697 | d->__nextchar += wcslen(d->__nextchar); | ||
| 698 | d->optind++; | ||
| 699 | d->optopt = 0; | ||
| 700 | return L'?'; | ||
| 701 | } | ||
| 702 | if (pfound != NULL) | ||
| 703 | { | ||
| 704 | option_index = indfound; | ||
| 705 | d->optind++; | ||
| 706 | if (*nameend) | ||
| 707 | { | ||
| 708 | if (pfound->has_arg) | ||
| 709 | d->optarg = nameend + 1; | ||
| 710 | else | ||
| 711 | { | ||
| 712 | if (print_errors) | ||
| 713 | { | ||
| 714 | if (argv[d->optind - 1][1] == L'-') | ||
| 715 | { | ||
| 716 | fwprintf(stderr, L"%s: option '--%s' doesn't allow an argument\n", argv[0], pfound->name); | ||
| 717 | } | ||
| 718 | else | ||
| 719 | { | ||
| 720 | fwprintf(stderr, L"%s: option '%c%s' doesn't allow an argument\n", argv[0], argv[d->optind - 1][0], pfound->name); | ||
| 721 | } | ||
| 722 | } | ||
| 723 | d->__nextchar += wcslen(d->__nextchar); | ||
| 724 | d->optopt = pfound->val; | ||
| 725 | return L'?'; | ||
| 726 | } | ||
| 727 | } | ||
| 728 | else if (pfound->has_arg == 1) | ||
| 729 | { | ||
| 730 | if (d->optind < argc) | ||
| 731 | d->optarg = argv[d->optind++]; | ||
| 732 | else | ||
| 733 | { | ||
| 734 | if (print_errors) | ||
| 735 | { | ||
| 736 | fwprintf(stderr, L"%s: option '--%s' requires an argument\n", argv[0], pfound->name); | ||
| 737 | } | ||
| 738 | d->__nextchar += wcslen(d->__nextchar); | ||
| 739 | d->optopt = pfound->val; | ||
| 740 | return optstring[0] == L':' ? L':' : L'?'; | ||
| 741 | } | ||
| 742 | } | ||
| 743 | d->__nextchar += wcslen(d->__nextchar); | ||
| 744 | if (longind != NULL) | ||
| 745 | *longind = option_index; | ||
| 746 | if (pfound->flag) | ||
| 747 | { | ||
| 748 | *(pfound->flag) = pfound->val; | ||
| 749 | return 0; | ||
| 750 | } | ||
| 751 | return pfound->val; | ||
| 752 | } | ||
| 753 | if (!long_only || argv[d->optind][1] == L'-' || wcschr(optstring, *d->__nextchar) == NULL) | ||
| 754 | { | ||
| 755 | if (print_errors) | ||
| 756 | { | ||
| 757 | if (argv[d->optind][1] == L'-') | ||
| 758 | { | ||
| 759 | fwprintf(stderr, L"%s: unrecognized option '--%s'\n", argv[0], d->__nextchar); | ||
| 760 | } | ||
| 761 | else | ||
| 762 | { | ||
| 763 | fwprintf(stderr, L"%s: unrecognized option '%c%s'\n", argv[0], argv[d->optind][0], d->__nextchar); | ||
| 764 | } | ||
| 765 | } | ||
| 766 | d->__nextchar = (wchar_t *)L""; | ||
| 767 | d->optind++; | ||
| 768 | d->optopt = 0; | ||
| 769 | return L'?'; | ||
| 770 | } | ||
| 771 | } | ||
| 772 | { | ||
| 773 | wchar_t c = *d->__nextchar++; | ||
| 774 | wchar_t *temp = (wchar_t*)wcschr(optstring, c); | ||
| 775 | if (*d->__nextchar == L'\0') | ||
| 776 | ++d->optind; | ||
| 777 | if (temp == NULL || c == L':' || c == L';') | ||
| 778 | { | ||
| 779 | if (print_errors) | ||
| 780 | { | ||
| 781 | fwprintf(stderr, L"%s: invalid option -- '%c'\n", argv[0], c); | ||
| 782 | } | ||
| 783 | d->optopt = c; | ||
| 784 | return L'?'; | ||
| 785 | } | ||
| 786 | if (temp[0] == L'W' && temp[1] == L';') | ||
| 787 | { | ||
| 788 | wchar_t *nameend; | ||
| 789 | const struct option_w *p; | ||
| 790 | const struct option_w *pfound = NULL; | ||
| 791 | int exact = 0; | ||
| 792 | int ambig = 0; | ||
| 793 | int indfound = 0; | ||
| 794 | int option_index; | ||
| 795 | if (longopts == NULL) | ||
| 796 | goto no_longs; | ||
| 797 | if (*d->__nextchar != L'\0') | ||
| 798 | { | ||
| 799 | d->optarg = d->__nextchar; | ||
| 800 | d->optind++; | ||
| 801 | } | ||
| 802 | else if (d->optind == argc) | ||
| 803 | { | ||
| 804 | if (print_errors) | ||
| 805 | { | ||
| 806 | fwprintf(stderr, L"%s: option requires an argument -- '%c'\n", argv[0], c); | ||
| 807 | } | ||
| 808 | d->optopt = c; | ||
| 809 | if (optstring[0] == L':') | ||
| 810 | c = L':'; | ||
| 811 | else | ||
| 812 | c = L'?'; | ||
| 813 | return c; | ||
| 814 | } | ||
| 815 | else | ||
| 816 | d->optarg = argv[d->optind++]; | ||
| 817 | for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != L'='; nameend++); | ||
| 818 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | ||
| 819 | if (!wcsncmp(p->name, d->__nextchar, nameend - d->__nextchar)) | ||
| 820 | { | ||
| 821 | if ((unsigned int)(nameend - d->__nextchar) == wcslen(p->name)) | ||
| 822 | { | ||
| 823 | pfound = p; | ||
| 824 | indfound = option_index; | ||
| 825 | exact = 1; | ||
| 826 | break; | ||
| 827 | } | ||
| 828 | else if (pfound == NULL) | ||
| 829 | { | ||
| 830 | pfound = p; | ||
| 831 | indfound = option_index; | ||
| 832 | } | ||
| 833 | else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) | ||
| 834 | ambig = 1; | ||
| 835 | } | ||
| 836 | if (ambig && !exact) | ||
| 837 | { | ||
| 838 | if (print_errors) | ||
| 839 | { | ||
| 840 | fwprintf(stderr, L"%s: option '-W %s' is ambiguous\n", 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"%s: option '-W %s' doesn't allow an argument\n", argv[0], pfound->name); | ||
| 858 | } | ||
| 859 | d->__nextchar += wcslen(d->__nextchar); | ||
| 860 | return L'?'; | ||
| 861 | } | ||
| 862 | } | ||
| 863 | else if (pfound->has_arg == 1) | ||
| 864 | { | ||
| 865 | if (d->optind < argc) | ||
| 866 | d->optarg = argv[d->optind++]; | ||
| 867 | else | ||
| 868 | { | ||
| 869 | if (print_errors) | ||
| 870 | { | ||
| 871 | fwprintf(stderr, L"%s: option '-W %s' requires an argument\n", argv[0], pfound->name); | ||
| 872 | } | ||
| 873 | d->__nextchar += wcslen(d->__nextchar); | ||
| 874 | return optstring[0] == L':' ? L':' : L'?'; | ||
| 875 | } | ||
| 876 | } | ||
| 877 | else | ||
| 878 | d->optarg = NULL; | ||
| 879 | d->__nextchar += wcslen(d->__nextchar); | ||
| 880 | if (longind != NULL) | ||
| 881 | *longind = option_index; | ||
| 882 | if (pfound->flag) | ||
| 883 | { | ||
| 884 | *(pfound->flag) = pfound->val; | ||
| 885 | return 0; | ||
| 886 | } | ||
| 887 | return pfound->val; | ||
| 888 | } | ||
| 889 | no_longs: | ||
| 890 | d->__nextchar = NULL; | ||
| 891 | return L'W'; | ||
| 892 | } | ||
| 893 | if (temp[1] == L':') | ||
| 894 | { | ||
| 895 | if (temp[2] == L':') | ||
| 896 | { | ||
| 897 | if (*d->__nextchar != L'\0') | ||
| 898 | { | ||
| 899 | d->optarg = d->__nextchar; | ||
| 900 | d->optind++; | ||
| 901 | } | ||
| 902 | else | ||
| 903 | d->optarg = NULL; | ||
| 904 | d->__nextchar = NULL; | ||
| 905 | } | ||
| 906 | else | ||
| 907 | { | ||
| 908 | if (*d->__nextchar != L'\0') | ||
| 909 | { | ||
| 910 | d->optarg = d->__nextchar; | ||
| 911 | d->optind++; | ||
| 912 | } | ||
| 913 | else if (d->optind == argc) | ||
| 914 | { | ||
| 915 | if (print_errors) | ||
| 916 | { | ||
| 917 | fwprintf(stderr, L"%s: option requires an argument -- '%c'\n", argv[0], c); | ||
| 918 | } | ||
| 919 | d->optopt = c; | ||
| 920 | if (optstring[0] == L':') | ||
| 921 | c = L':'; | ||
| 922 | else | ||
| 923 | c = L'?'; | ||
| 924 | } | ||
| 925 | else | ||
| 926 | d->optarg = argv[d->optind++]; | ||
| 927 | d->__nextchar = NULL; | ||
| 928 | } | ||
| 929 | } | ||
| 930 | return c; | ||
| 931 | } | ||
| 932 | } | ||
| 933 | 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) | ||
| 934 | { | ||
| 935 | int result; | ||
| 936 | getopt_data_w.optind = optind; | ||
| 937 | getopt_data_w.opterr = opterr; | ||
| 938 | result = _getopt_internal_r_w(argc, argv, optstring, longopts, longind, long_only, &getopt_data_w, posixly_correct); | ||
| 939 | optind = getopt_data_w.optind; | ||
| 940 | optarg_w = getopt_data_w.optarg; | ||
| 941 | optopt = getopt_data_w.optopt; | ||
| 942 | return result; | ||
| 943 | } | ||
| 944 | int getopt_w(int argc, wchar_t *const *argv, const wchar_t *optstring) _GETOPT_THROW | ||
| 945 | { | ||
| 946 | return _getopt_internal_w(argc, argv, optstring, (const struct option_w *) 0, (int *)0, 0, 0); | ||
| 947 | } | ||
| 948 | 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 | ||
| 949 | { | ||
| 950 | return _getopt_internal_w(argc, argv, options, long_options, opt_index, 0, 0); | ||
| 951 | } | ||
| 952 | 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 | ||
| 953 | { | ||
| 954 | return _getopt_internal_w(argc, argv, options, long_options, opt_index, 1, 0); | ||
| 955 | } | ||
| 956 | 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) | ||
| 957 | { | ||
| 958 | return _getopt_internal_r_w(argc, argv, options, long_options, opt_index, 0, d, 0); | ||
| 959 | } | ||
| 960 | 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) | ||
| 961 | { | ||
| 962 | return _getopt_internal_r_w(argc, argv, options, long_options, opt_index, 1, d, 0); | ||
| 963 | } \ No newline at end of file | ||
diff --git a/externals/getopt/getopt.h b/externals/getopt/getopt.h new file mode 100644 index 000000000..87e2ca396 --- /dev/null +++ b/externals/getopt/getopt.h | |||
| @@ -0,0 +1,136 @@ | |||
| 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 | 10/15/2012 - Ludvik Jerabek - Modified to match latest GNU features | ||
| 23 | 06/19/2015 - Ludvik Jerabek - Fixed maximum option limitation caused by option_a (255) and option_w (65535) structure val variable | ||
| 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. | ||
| 37 | */ | ||
| 38 | #ifndef __GETOPT_H_ | ||
| 39 | #define __GETOPT_H_ | ||
| 40 | |||
| 41 | #ifdef _GETOPT_API | ||
| 42 | #undef _GETOPT_API | ||
| 43 | #endif | ||
| 44 | |||
| 45 | #if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT) | ||
| 46 | #error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT can only be used individually" | ||
| 47 | #elif defined(STATIC_GETOPT) | ||
| 48 | #pragma message("Warning static builds of getopt violate the Lesser GNU Public License") | ||
| 49 | #define _GETOPT_API | ||
| 50 | #elif defined(EXPORTS_GETOPT) | ||
| 51 | #pragma message("Exporting getopt library") | ||
| 52 | #define _GETOPT_API __declspec(dllexport) | ||
| 53 | #else | ||
| 54 | #pragma message("Importing getopt library") | ||
| 55 | #define _GETOPT_API __declspec(dllimport) | ||
| 56 | #endif | ||
| 57 | |||
| 58 | // Change behavior for C\C++ | ||
| 59 | #ifdef __cplusplus | ||
| 60 | #define _BEGIN_EXTERN_C extern "C" { | ||
| 61 | #define _END_EXTERN_C } | ||
| 62 | #define _GETOPT_THROW throw() | ||
| 63 | #else | ||
| 64 | #define _BEGIN_EXTERN_C | ||
| 65 | #define _END_EXTERN_C | ||
| 66 | #define _GETOPT_THROW | ||
| 67 | #endif | ||
| 68 | |||
| 69 | // Standard GNU options | ||
| 70 | #define null_argument 0 | ||
| 71 | #define no_argument 0 | ||
| 72 | #define required_argument 1 | ||
| 73 | #define optional_argument 2 | ||
| 74 | |||
| 75 | // Shorter Options | ||
| 76 | #define ARG_NULL 0 | ||
| 77 | #define ARG_NONE 0 | ||
| 78 | #define ARG_REQ 1 | ||
| 79 | #define ARG_OPT 2 | ||
| 80 | |||
| 81 | #include <string.h> | ||
| 82 | #include <wchar.h> | ||
| 83 | |||
| 84 | _BEGIN_EXTERN_C | ||
| 85 | |||
| 86 | extern _GETOPT_API int optind; | ||
| 87 | extern _GETOPT_API int opterr; | ||
| 88 | extern _GETOPT_API int optopt; | ||
| 89 | |||
| 90 | // Ansi | ||
| 91 | struct option_a | ||
| 92 | { | ||
| 93 | const char* name; | ||
| 94 | int has_arg; | ||
| 95 | int *flag; | ||
| 96 | int val; | ||
| 97 | }; | ||
| 98 | extern _GETOPT_API char *optarg_a; | ||
| 99 | extern _GETOPT_API int getopt_a(int argc, char *const *argv, const char *optstring) _GETOPT_THROW; | ||
| 100 | extern _GETOPT_API int getopt_long_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW; | ||
| 101 | extern _GETOPT_API int getopt_long_only_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW; | ||
| 102 | |||
| 103 | // Unicode | ||
| 104 | struct option_w | ||
| 105 | { | ||
| 106 | const wchar_t* name; | ||
| 107 | int has_arg; | ||
| 108 | int *flag; | ||
| 109 | int val; | ||
| 110 | }; | ||
| 111 | extern _GETOPT_API wchar_t *optarg_w; | ||
| 112 | extern _GETOPT_API int getopt_w(int argc, wchar_t *const *argv, const wchar_t *optstring) _GETOPT_THROW; | ||
| 113 | extern _GETOPT_API 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; | ||
| 114 | extern _GETOPT_API 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; | ||
| 115 | |||
| 116 | _END_EXTERN_C | ||
| 117 | |||
| 118 | #undef _BEGIN_EXTERN_C | ||
| 119 | #undef _END_EXTERN_C | ||
| 120 | #undef _GETOPT_THROW | ||
| 121 | #undef _GETOPT_API | ||
| 122 | |||
| 123 | #ifdef _UNICODE | ||
| 124 | #define getopt getopt_w | ||
| 125 | #define getopt_long getopt_long_w | ||
| 126 | #define getopt_long_only getopt_long_only_w | ||
| 127 | #define option option_w | ||
| 128 | #define optarg optarg_w | ||
| 129 | #else | ||
| 130 | #define getopt getopt_a | ||
| 131 | #define getopt_long getopt_long_a | ||
| 132 | #define getopt_long_only getopt_long_only_a | ||
| 133 | #define option option_a | ||
| 134 | #define optarg optarg_a | ||
| 135 | #endif | ||
| 136 | #endif // __GETOPT_H_ | ||
diff --git a/src/citra/CMakeLists.txt b/src/citra/CMakeLists.txt index 5e8cbfa35..918687312 100644 --- a/src/citra/CMakeLists.txt +++ b/src/citra/CMakeLists.txt | |||
| @@ -16,8 +16,11 @@ create_directory_groups(${SRCS} ${HEADERS}) | |||
| 16 | add_executable(citra ${SRCS} ${HEADERS}) | 16 | add_executable(citra ${SRCS} ${HEADERS}) |
| 17 | target_link_libraries(citra core common video_core) | 17 | target_link_libraries(citra core common video_core) |
| 18 | target_link_libraries(citra ${GLFW_LIBRARIES} ${OPENGL_gl_LIBRARY} inih) | 18 | target_link_libraries(citra ${GLFW_LIBRARIES} ${OPENGL_gl_LIBRARY} inih) |
| 19 | if (MSVC) | ||
| 20 | target_link_libraries(citra getopt) | ||
| 21 | endif() | ||
| 19 | target_link_libraries(citra ${PLATFORM_LIBRARIES}) | 22 | target_link_libraries(citra ${PLATFORM_LIBRARIES}) |
| 20 | 23 | ||
| 21 | if(${CMAKE_SYSTEM_NAME} MATCHES "Linux|FreeBSD|OpenBSD|NetBSD") | 24 | if(${CMAKE_SYSTEM_NAME} MATCHES "Linux|FreeBSD|OpenBSD|NetBSD") |
| 22 | install(TARGETS citra RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin") | 25 | install(TARGETS citra RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/bin") |
| 23 | endif() | 26 | endif() \ No newline at end of file |
diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp index a59726c78..182646f4c 100644 --- a/src/citra/citra.cpp +++ b/src/citra/citra.cpp | |||
| @@ -3,6 +3,15 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <string> | 5 | #include <string> |
| 6 | #include <thread> | ||
| 7 | #include <iostream> | ||
| 8 | |||
| 9 | #ifdef _MSC_VER | ||
| 10 | #include <getopt.h> | ||
| 11 | #else | ||
| 12 | #include <unistd.h> | ||
| 13 | #include <getopt.h> | ||
| 14 | #endif | ||
| 6 | 15 | ||
| 7 | #include "common/logging/log.h" | 16 | #include "common/logging/log.h" |
| 8 | #include "common/logging/backend.h" | 17 | #include "common/logging/backend.h" |
| @@ -18,12 +27,39 @@ | |||
| 18 | 27 | ||
| 19 | #include "video_core/video_core.h" | 28 | #include "video_core/video_core.h" |
| 20 | 29 | ||
| 30 | |||
| 31 | static void PrintHelp() | ||
| 32 | { | ||
| 33 | std::cout << "Usage: citra <filename>" << std::endl; | ||
| 34 | } | ||
| 35 | |||
| 21 | /// Application entry point | 36 | /// Application entry point |
| 22 | int main(int argc, char **argv) { | 37 | int main(int argc, char **argv) { |
| 38 | int option_index = 0; | ||
| 39 | std::string boot_filename; | ||
| 40 | static struct option long_options[] = { | ||
| 41 | { "help", no_argument, 0, 'h' }, | ||
| 42 | { 0, 0, 0, 0 } | ||
| 43 | }; | ||
| 44 | |||
| 45 | while (optind < argc) { | ||
| 46 | char arg = getopt_long(argc, argv, ":h", long_options, &option_index); | ||
| 47 | if (arg != -1) { | ||
| 48 | switch (arg) { | ||
| 49 | case 'h': | ||
| 50 | PrintHelp(); | ||
| 51 | return 0; | ||
| 52 | } | ||
| 53 | } else { | ||
| 54 | boot_filename = argv[optind]; | ||
| 55 | optind++; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 23 | Log::Filter log_filter(Log::Level::Debug); | 59 | Log::Filter log_filter(Log::Level::Debug); |
| 24 | Log::SetFilter(&log_filter); | 60 | Log::SetFilter(&log_filter); |
| 25 | 61 | ||
| 26 | if (argc < 2) { | 62 | if (boot_filename.empty()) { |
| 27 | LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified"); | 63 | LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified"); |
| 28 | return -1; | 64 | return -1; |
| 29 | } | 65 | } |
| @@ -31,7 +67,7 @@ int main(int argc, char **argv) { | |||
| 31 | Config config; | 67 | Config config; |
| 32 | log_filter.ParseFilterString(Settings::values.log_filter); | 68 | log_filter.ParseFilterString(Settings::values.log_filter); |
| 33 | 69 | ||
| 34 | std::string boot_filename = argv[1]; | 70 | |
| 35 | EmuWindow_GLFW* emu_window = new EmuWindow_GLFW; | 71 | EmuWindow_GLFW* emu_window = new EmuWindow_GLFW; |
| 36 | 72 | ||
| 37 | VideoCore::g_hw_renderer_enabled = Settings::values.use_hw_renderer; | 73 | VideoCore::g_hw_renderer_enabled = Settings::values.use_hw_renderer; |