35 #ifndef USE_GLIBC_GNUGETOPT
50 # define _(msgid) gettext (msgid)
53 # define _(msgid) (msgid)
200 while (top > middle && middle > bottom)
202 if (top - middle > middle - bottom)
205 int len = middle - bottom;
209 for (i = 0; i < len; i++)
211 tem = argv[bottom + i];
212 argv[bottom + i] = argv[top - (middle - bottom) + i];
213 argv[top - (middle - bottom) + i] = tem;
221 int len = top - middle;
225 for (i = 0; i < len; i++)
227 tem = argv[bottom + i];
228 argv[bottom + i] = argv[middle + i];
229 argv[middle + i] = tem;
263 if (optstring[0] ==
'-')
268 else if (optstring[0] ==
'+')
340 int print_errors =
opterr;
341 if (optstring[0] ==
':')
359 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
378 exchange (
const_cast<char **
>(argv));
400 exchange (
const_cast<char **
>(argv));
435 + (longopts != NULL && argv[
optind][1] ==
'-'));
454 && (argv[
optind][1] ==
'-'
455 || (long_only && (argv[
optind][2] || !strchr (optstring, argv[
optind][1])))))
459 const struct option *pfound = NULL;
465 for (nameend =
nextchar; *nameend && *nameend !=
'='; nameend++)
470 for (
p = longopts, option_index = 0;
p->name;
p++, option_index++)
473 if (
unsigned(nameend -
nextchar) == unsigned(strlen(
p->name)))
477 indfound = option_index;
481 else if (pfound == NULL)
485 indfound = option_index;
489 || pfound->
flag !=
p->flag
490 || pfound->
val !=
p->val)
498 fprintf (stderr,
_(
"%s: option '%s' is ambiguous\n"),
508 option_index = indfound;
520 if (argv[
optind - 1][1] ==
'-')
523 _(
"%s: option '--%s' doesn't allow an argument\n"),
524 argv[0], pfound->
name);
528 _(
"%s: option '%c%s' doesn't allow an argument\n"),
546 _(
"%s: option '%s' requires an argument\n"),
547 argv[0], argv[
optind - 1]);
550 return optstring[0] ==
':' ?
':' :
'?';
555 *longind = option_index;
568 if (!long_only || argv[
optind][1] ==
'-'
569 || strchr (optstring, *
nextchar) == NULL)
573 if (argv[
optind][1] ==
'-')
575 fprintf (stderr,
_(
"%s: unrecognized option '--%s'\n"),
579 fprintf (stderr,
_(
"%s: unrecognized option '%c%s'\n"),
593 const char *temp = strchr (optstring, c);
599 if (temp == NULL || c ==
':')
605 fprintf (stderr,
_(
"%s: illegal option -- %c\n"),
608 fprintf (stderr,
_(
"%s: invalid option -- %c\n"),
615 if (temp[0] ==
'W' && temp[1] ==
';')
619 const struct option *pfound = NULL;
638 fprintf (stderr,
_(
"%s: option requires an argument -- %c\n"),
642 if (optstring[0] ==
':')
656 for (
nextchar = nameend =
optarg; *nameend && *nameend !=
'='; nameend++)
661 for (
p = longopts, option_index = 0;
p->name;
p++, option_index++)
664 if (
unsigned(nameend -
nextchar) ==
unsigned(strlen(
p->name)))
668 indfound = option_index;
672 else if (pfound == NULL)
676 indfound = option_index;
685 fprintf (stderr,
_(
"%s: option '-W %s' is ambiguous\n"),
693 option_index = indfound;
703 fprintf (stderr,
_(
"\
704 %s: option '-W %s' doesn't allow an argument\n"),
705 argv[0], pfound->
name);
719 _(
"%s: option '%s' requires an argument\n"),
720 argv[0], argv[
optind - 1]);
722 return optstring[0] ==
':' ?
':' :
'?';
727 *longind = option_index;
768 _(
"%s: option requires an argument -- %c\n"),
772 if (optstring[0] ==
':')
static int getopt_initialized
static void exchange(char **argv)
static const char * getopt_initialize(int argc, char *const *argv, const char *optstring)
static char * posixly_correct
int gnu_getopt_internal_(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *longind, int long_only)
Wrappers to allow GNU getopt to be used cleanly from C++ code.