43 #define _CRT_SECURE_NO_DEPRECATE
46 #include "getopt_W32.h"
62 extern char opterrmsg[128];
65 static void warnx(
int print_error,
const char *fmt, ...)
70 _vsnprintf(opterrmsg, 128, fmt, ap);
75 fprintf(stderr, opterrmsg);
76 fprintf(stderr,
"\n");
84 # define _DIAGASSERT(X)
87 #if HAVE_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
88 # define REPLACE_GETOPT
93 __weak_alias(getopt,_getopt)
101 #elif HAVE_CONFIG_H && !HAVE_DECL_OPTRESET
106 __weak_alias(getopt_long,_getopt_long)
109 #if !HAVE_GETOPT_LONG
110 #define IGNORE_FIRST (*options == '-' || *options == '+')
111 #define PRINT_ERROR ((opterr) && ((*options != ':') \
112 || (IGNORE_FIRST && options[1] != ':')))
113 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
114 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
116 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
119 #define BADCH (int)'?'
120 #define BADARG ((IGNORE_FIRST && options[1] == ':') \
121 || (*options == ':') ? (int)':' : (int)'?')
122 #define INORDER (int)1
126 static int getopt_internal(
int,
char *
const *,
const char *);
127 static int gcd(
int,
int);
128 static void permute_args(
int,
int,
int,
char *
const *);
130 static char *place = EMSG;
133 static int nonopt_start = -1;
134 static int nonopt_end = -1;
137 static const char recargchar[] =
"option requires an argument -- %c";
138 static const char recargstring[] =
"option requires an argument -- %s";
139 static const char ambig[] =
"ambiguous option -- %.*s";
140 static const char noarg[] =
"option doesn't take an argument -- %.*s";
141 static const char illoptchar[] =
"unknown option -- %c";
142 static const char illoptstring[] =
"unknown option -- %s";
169 permute_args(
int panonopt_start,
int panonopt_end,
int opt_end,
char *
const * nargv)
171 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
174 _DIAGASSERT(nargv != NULL);
179 nnonopts = panonopt_end - panonopt_start;
180 nopts = opt_end - panonopt_end;
181 ncycle = gcd(nnonopts, nopts);
182 cyclelen = (opt_end - panonopt_start) / ncycle;
184 for (i = 0; i < ncycle; i++) {
185 cstart = panonopt_end+i;
187 for (j = 0; j < cyclelen; j++) {
188 if (pos >= panonopt_end)
194 ((
char **) nargv)[pos] = nargv[cstart];
196 ((
char **)nargv)[cstart] = swap;
207 int getopt_internal (
int nargc,
char *
const *nargv,
const char *options)
212 _DIAGASSERT(nargv != NULL);
213 _DIAGASSERT(options != NULL);
226 nonopt_start = nonopt_end = -1;
228 if (optreset || !*place) {
230 if (optind >= nargc) {
232 if (nonopt_end != -1) {
234 permute_args(nonopt_start, nonopt_end,
236 optind -= nonopt_end - nonopt_start;
238 else if (nonopt_start != -1) {
243 optind = nonopt_start;
245 nonopt_start = nonopt_end = -1;
248 if ((*(place = nargv[optind]) !=
'-')
249 || (place[1] ==
'\0')) {
256 optarg = nargv[optind++];
267 if (nonopt_start == -1)
268 nonopt_start = optind;
269 else if (nonopt_end != -1) {
270 permute_args(nonopt_start, nonopt_end,
272 nonopt_start = optind -
273 (nonopt_end - nonopt_start);
280 if (nonopt_start != -1 && nonopt_end == -1)
282 if (place[1] && *++place ==
'-') {
287 if ((optchar = (
int)*place++) == (int)
':' ||
288 (oli = (
char*)strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
294 warnx(illoptchar, optchar);
296 warnx(PRINT_ERROR, illoptchar, optchar);
301 if (optchar ==
'W' && oli[1] ==
';') {
306 if (++optind >= nargc) {
310 warnx(recargchar, optchar);
312 warnx(PRINT_ERROR, recargchar, optchar);
317 place = nargv[optind];
332 else if (oli[1] !=
':') {
333 if (++optind >= nargc) {
337 warnx(recargchar, optchar);
339 warnx(PRINT_ERROR, recargchar, optchar);
344 optarg = nargv[optind];
353 #ifdef REPLACE_GETOPT
361 BIASCommon_EXPORT getopt(nargc, nargv, options)
368 _DIAGASSERT(nargv != NULL);
369 _DIAGASSERT(options != NULL);
371 if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
377 if (nonopt_end != -1) {
378 permute_args(nonopt_start, nonopt_end, optind,
380 optind -= nonopt_end - nonopt_start;
382 nonopt_start = nonopt_end = -1;
394 getopt_long(
int nargc,
char *
const * nargv,
const char *options,
395 const struct option *long_options,
int *idx)
399 _DIAGASSERT(nargv != NULL);
400 _DIAGASSERT(options != NULL);
401 _DIAGASSERT(long_options != NULL);
404 if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
405 char *current_argv, *has_equal;
406 size_t current_argv_len;
409 current_argv = place;
415 if (*current_argv ==
'\0') {
420 if (nonopt_end != -1) {
421 permute_args(nonopt_start, nonopt_end,
423 optind -= nonopt_end - nonopt_start;
425 nonopt_start = nonopt_end = -1;
428 if ((has_equal = strchr(current_argv,
'=')) != NULL) {
430 current_argv_len = has_equal - current_argv;
433 current_argv_len = strlen(current_argv);
435 for (i = 0; long_options[i].
name; i++) {
437 if (strncmp(current_argv, long_options[i].name,
441 if (strlen(long_options[i].name) ==
442 (
unsigned)current_argv_len) {
453 warnx(ambig, (
int)current_argv_len,
456 warnx(PRINT_ERROR, ambig, (
int)current_argv_len,
464 if (long_options[match].has_arg == no_argument
468 warnx(noarg, (
int)current_argv_len,
471 warnx(PRINT_ERROR, noarg, (
int)current_argv_len,
478 if (long_options[match].flag == NULL)
479 optopt = long_options[match].
val;
484 if (long_options[match].has_arg == required_argument ||
485 long_options[match].has_arg == optional_argument) {
488 else if (long_options[match].has_arg ==
494 optarg = nargv[optind++];
497 if ((long_options[match].has_arg == required_argument)
498 && (optarg == NULL)) {
505 warnx(recargstring, current_argv);
507 warnx(PRINT_ERROR, recargstring, current_argv);
513 if (long_options[match].flag == NULL)
514 optopt = long_options[match].
val;
523 warnx(illoptstring, current_argv);
525 warnx(PRINT_ERROR, illoptstring, current_argv);
530 if (long_options[match].flag) {
531 *long_options[match].
flag = long_options[match].
val;
534 retval = long_options[match].
val;