51#define LOG(kind, ...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__)
53#define LOG_STRERROR(kind, syscall) \
54 GNUNET_log_from_strerror (kind, "util-getopt", syscall)
61#define _(msgid) gettext (msgid)
63#define _(msgid) (msgid)
180#if defined(__GNU_LIBRARY__) || defined(__GLIBC__) || defined(DARWIN)
186#define my_index strchr
213#if ! defined(__STDC__) || ! __STDC__
217strlen (
const char *);
233#define SWAP_FLAGS(ch1, ch2)
244#if defined(__STDC__) && __STDC__
263 while (top > middle && middle > bottom)
265 if (top - middle > middle - bottom)
268 int len = middle - bottom;
272 for (i = 0; i < len; i++)
274 tem = argv[bottom + i];
275 argv[bottom + i] = argv[top - (middle - bottom) + i];
276 argv[top - (middle - bottom) + i] = tem;
277 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
285 int len = top - middle;
289 for (i = 0; i < len; i++)
291 tem = argv[bottom + i];
292 argv[bottom + i] = argv[middle + i];
293 argv[middle + i] = tem;
310#if defined(__STDC__) && __STDC__
330 if (optstring[0] ==
'-')
335 else if (optstring[0] ==
'+')
408 const char *optstring,
413 static int __getopt_initialized = 0;
414 static int GNopterr = 1;
418 if ((
GNoptind == 0) || ! __getopt_initialized)
423 __getopt_initialized = 1;
430#define NONOPTION_P (argv[GNoptind][0] != '-' || argv[GNoptind][1] == '\0')
521if ((longopts != NULL) &&
528 const struct GNoption *pfound = NULL;
534 for (nameend =
nextchar; *nameend && *nameend !=
'='; nameend++)
539 for (
p = longopts, option_index = 0;
p->name;
p++, option_index++)
542 if ((
unsigned int) (nameend -
nextchar) ==
543 (
unsigned int) strlen (
p->name))
547 indfound = option_index;
551 else if (pfound == NULL)
555 indfound = option_index;
562 if (ambig && ! exact)
566 _ (
"%s: option `%s' is ambiguous\n"),
576 option_index = indfound;
591 _ (
"%s: option `--%s' does not allow an argument\n"),
597 _ (
"%s: option `%c%s' does not allow an argument\n"),
617 _ (
"%s: option `%s' requires an argument\n"),
622 return (optstring[0] ==
':') ?
':' :
'?';
627 *longind = option_index;
640 if (! long_only || (argv[
GNoptind][1] ==
'-') ||
648 _ (
"%s: unrecognized option `--%s'\n"),
654 _ (
"%s: unrecognized option `%c%s'\n"),
669 char *temp =
my_index (optstring, c);
675 if ((temp == NULL) || (c ==
':'))
681 fprintf (stderr,
_ (
"%s: illegal option -- %c\n"), argv[0], c);
683 fprintf (stderr,
_ (
"%s: invalid option -- %c\n"), argv[0], c);
688 if ((temp[0] ==
'W') && (temp[1] ==
';'))
692 const struct GNoption *pfound = NULL;
712 _ (
"%s: option requires an argument -- %c\n"),
716 if (optstring[0] ==
':')
736 if (longopts != NULL)
737 for (
p = longopts, option_index = 0;
p->name;
p++, option_index++)
740 if ((
unsigned int) (nameend -
nextchar) == strlen (
p->name))
744 indfound = option_index;
748 else if (pfound == NULL)
752 indfound = option_index;
758 if (ambig && ! exact)
762 _ (
"%s: option `-W %s' is ambiguous\n"),
771 option_index = indfound;
782 _ (
"%s: option `-W %s' does not allow an argument\n"),
798 _ (
"%s: option `%s' requires an argument\n"),
802 return optstring[0] ==
':' ?
':' :
'?';
807 *longind = option_index;
848 _ (
"%s: option requires an argument -- %c\n"),
852 if (optstring[0] ==
':')
873 const struct GNoption *long_options,
896 unsigned int optmatch = 0;
897 const char *have_exclusive = NULL;
915 for (
unsigned i = 0; i < count; i++)
919 long_options[i].
flag = NULL;
923 shorts[spos++] =
':';
925 long_options[count].
name = NULL;
926 long_options[count].
has_arg = 0;
927 long_options[count].
flag = NULL;
928 long_options[count].
val =
'\0';
935 int option_index = 0;
948 for (i = 0; i < count; i++)
971 _ (
"Use %s to get a list of options.\n"),
981 if ((NULL != have_exclusive) && (optmatch > 1))
984 _ (
"Option `%s' can't be used with other options.\n"),
993 if ((0 == seen[count]) && (
allOptions[count].option_mandatory))
996 _ (
"Missing mandatory option `%s'.\n"),
struct GNUNET_GETOPT_CommandLineOption options[]
static int GNgetopt_long(int argc, char *const *argv, const char *options, const struct GNoption *long_options, int *opt_index)
static void exchange(char **argv)
static const char * _getopt_initialize(int argc, char *const *argv, const char *optstring)
static char * my_index(const char *str, int chr)
static int GN_getopt_internal(int argc, char *const *argv, const char *optstring, const struct GNoption *longopts, int *longind, int long_only)
#define SWAP_FLAGS(ch1, ch2)
static char * posixly_correct
static char * name
Name (label) of the records to list.
static struct GNUNET_OS_Process * p
Helper process we started.
int GNUNET_GETOPT_run(const char *binaryOptions, const struct GNUNET_GETOPT_CommandLineOption *allOptions, unsigned int argc, char *const *argv)
Parse the command line.
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
Definition of a command line option.
int require_argument
Is an argument required? GNUNET_NO (includes optional) or GNUNET_YES (required)
const char * name
Long name of the option (may not be NULL)
GNUNET_GETOPT_CommandLineOptionProcessor processor
Handler for the option.
const char shortName
Short name of the option.
void(* cleaner)(void *cls)
Function to call on scls to clean up after processing all the arguments.
General context for command line processors.
const char * binaryName
Name of the application.
const char * binaryOptions
Name of application with option summary.
const struct GNUNET_GETOPT_CommandLineOption * allOptions
Array with all command line options.
char *const * argv
Original command line.
unsigned int argc
Total number of argv's.
unsigned int currentArgument
Current argument.