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 |
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. |
37 |
*/ |
38 |
#define _CRT_SECURE_NO_WARNINGS |
39 |
#include <stdlib.h> |
40 |
#include <stdio.h> |
41 |
#include <malloc.h> |
42 |
#include "wingetopt.h" |
43 |
|
44 |
#ifdef __cplusplus |
45 |
#define _GETOPT_THROW throw() |
46 |
#else |
47 |
#define _GETOPT_THROW |
48 |
#endif |
49 |
|
50 |
#ifdef _MSC_VER |
51 |
//Disable -> warning C4273: 'abc' : inconsistent dll linkage |
52 |
#pragma warning (disable : 4273) |
53 |
#endif |
54 |
|
55 |
int optind = 1; |
56 |
int opterr = 1; |
57 |
int optopt = '?'; |
58 |
enum ENUM_ORDERING { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER }; |
59 |
|
60 |
// |
61 |
// |
62 |
// Ansi structures and functions follow |
63 |
// |
64 |
// |
65 |
|
66 |
static struct _getopt_data_a |
67 |
{ |
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 void exchange_a(char **argv, struct _getopt_data_a *d) |
82 |
{ |
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 |
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 | !!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 |
} |
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 |
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 |
// |
521 |
// |
522 |
// Unicode Structures and Functions |
523 |
// |
524 |
// |
525 |
|
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 |
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 |
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 |
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 |
} |
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; |
958 |
} |
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); |
962 |
} |
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); |
966 |
} |
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_w (argc, argv, options, long_options, opt_index, 1, 0); |
970 |
} |
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); |
974 |
} |
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); |
978 |
} |