summaryrefslogtreecommitdiff
path: root/src/util/getopt_helpers.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/getopt_helpers.c')
-rw-r--r--src/util/getopt_helpers.c441
1 files changed, 208 insertions, 233 deletions
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c
index d6ba85733..05856beee 100644
--- a/src/util/getopt_helpers.c
+++ b/src/util/getopt_helpers.c
@@ -26,7 +26,7 @@
#include "platform.h"
#include "gnunet_util_lib.h"
-#define LOG(kind,...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__)
+#define LOG(kind, ...) GNUNET_log_from (kind, "util-getopt", __VA_ARGS__)
/**
@@ -48,9 +48,7 @@ print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
(void) option;
(void) value;
- printf ("%s v%s\n",
- ctx->binaryName,
- version);
+ printf ("%s v%s\n", ctx->binaryName, version);
return GNUNET_NO;
}
@@ -64,14 +62,13 @@ print_version (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_version (const char *version)
{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = 'v',
- .name = "version",
- .description = gettext_noop("print the version number"),
- .option_exclusive = 1,
- .processor = &print_version,
- .scls = (void *) version
- };
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'v',
+ .name = "version",
+ .description = gettext_noop (
+ "print the version number"),
+ .option_exclusive = 1,
+ .processor = &print_version,
+ .scls = (void *) version};
return clo;
}
@@ -111,10 +108,9 @@ format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
(void) value;
if (NULL != about)
{
- printf ("%s\n%s\n",
- ctx->binaryOptions,
- gettext (about));
- printf (_("Arguments mandatory for long options are also mandatory for short options.\n"));
+ printf ("%s\n%s\n", ctx->binaryOptions, gettext (about));
+ printf (_ (
+ "Arguments mandatory for long options are also mandatory for short options.\n"));
}
i = 0;
opt = ctx->allOptions;
@@ -147,7 +143,7 @@ format_help (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
trans = "";
ml = strlen (trans);
p = 0;
-OUTER:
+ OUTER:
while (ml - p > 78 - slen)
{
for (j = p + 78 - slen; j > (int) p; j--)
@@ -199,13 +195,13 @@ OUTER:
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_help (const char *about)
{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = 'h',
- .name = "help",
- .description = gettext_noop("print this help"),
- .processor = format_help,
- .scls = (void *) about
- };
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'h',
+ .name = "help",
+ .description = gettext_noop (
+ "print this help"),
+ .option_exclusive = 1,
+ .processor = format_help,
+ .scls = (void *) about};
return clo;
}
@@ -252,17 +248,15 @@ increment_value (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
*/
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_increment_uint (char shortName,
- const char *name,
- const char *description,
- unsigned int *val)
-{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = shortName,
- .name = name,
- .description = description,
- .processor = &increment_value,
- .scls = (void *) val
- };
+ const char *name,
+ const char *description,
+ unsigned int *val)
+{
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
+ .name = name,
+ .description = description,
+ .processor = &increment_value,
+ .scls = (void *) val};
return clo;
}
@@ -277,13 +271,12 @@ GNUNET_GETOPT_option_increment_uint (char shortName,
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_verbose (unsigned int *level)
{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = 'V',
- .name = "verbose",
- .description = gettext_noop("be verbose"),
- .processor = &increment_value,
- .scls = (void *) level
- };
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = 'V',
+ .name = "verbose",
+ .description =
+ gettext_noop ("be verbose"),
+ .processor = &increment_value,
+ .scls = (void *) level};
return clo;
}
@@ -331,17 +324,15 @@ set_one (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
*/
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_flag (char shortName,
- const char *name,
- const char *description,
- int *val)
-{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = shortName,
- .name = name,
- .description = description,
- .processor = &set_one,
- .scls = (void *) val
- };
+ const char *name,
+ const char *description,
+ int *val)
+{
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
+ .name = name,
+ .description = description,
+ .processor = &set_one,
+ .scls = (void *) val};
return clo;
}
@@ -394,15 +385,13 @@ GNUNET_GETOPT_option_string (char shortName,
const char *description,
char **str)
{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = shortName,
- .name = name,
- .argumentHelp = argumentHelp,
- .description = description,
- .require_argument = 1,
- .processor = &set_string,
- .scls = (void *) str
- };
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
+ .name = name,
+ .argumentHelp = argumentHelp,
+ .description = description,
+ .require_argument = 1,
+ .processor = &set_string,
+ .scls = (void *) str};
return clo;
}
@@ -417,15 +406,14 @@ GNUNET_GETOPT_option_string (char shortName,
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_loglevel (char **level)
{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = 'L',
- .name = "log",
- .argumentHelp = "LOGLEVEL",
- .description = gettext_noop("configure logging to use LOGLEVEL"),
- .require_argument = 1,
- .processor = &set_string,
- .scls = (void *) level
- };
+ struct GNUNET_GETOPT_CommandLineOption clo =
+ {.shortName = 'L',
+ .name = "log",
+ .argumentHelp = "LOGLEVEL",
+ .description = gettext_noop ("configure logging to use LOGLEVEL"),
+ .require_argument = 1,
+ .processor = &set_string,
+ .scls = (void *) level};
return clo;
}
@@ -470,20 +458,18 @@ set_filename (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
*/
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_filename (char shortName,
- const char *name,
- const char *argumentHelp,
- const char *description,
- char **str)
-{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = shortName,
- .name = name,
- .argumentHelp = argumentHelp,
- .description = description,
- .require_argument = 1,
- .processor = &set_filename,
- .scls = (void *) str
- };
+ const char *name,
+ const char *argumentHelp,
+ const char *description,
+ char **str)
+{
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
+ .name = name,
+ .argumentHelp = argumentHelp,
+ .description = description,
+ .require_argument = 1,
+ .processor = &set_filename,
+ .scls = (void *) str};
return clo;
}
@@ -497,15 +483,15 @@ GNUNET_GETOPT_option_filename (char shortName,
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_logfile (char **logfn)
{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = 'l',
- .name = "logfile",
- .argumentHelp = "FILENAME",
- .description = gettext_noop ("configure logging to write logs to FILENAME"),
- .require_argument = 1,
- .processor = &set_filename,
- .scls = (void *) logfn
- };
+ struct GNUNET_GETOPT_CommandLineOption clo =
+ {.shortName = 'l',
+ .name = "logfile",
+ .argumentHelp = "FILENAME",
+ .description =
+ gettext_noop ("configure logging to write logs to FILENAME"),
+ .require_argument = 1,
+ .processor = &set_filename,
+ .scls = (void *) logfn};
return clo;
}
@@ -519,15 +505,14 @@ GNUNET_GETOPT_option_logfile (char **logfn)
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_cfgfile (char **fn)
{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = 'c',
- .name = "config",
- .argumentHelp = "FILENAME",
- .description = gettext_noop("use configuration file FILENAME"),
- .require_argument = 1,
- .processor = &set_filename,
- .scls = (void *) fn
- };
+ struct GNUNET_GETOPT_CommandLineOption clo =
+ {.shortName = 'c',
+ .name = "config",
+ .argumentHelp = "FILENAME",
+ .description = gettext_noop ("use configuration file FILENAME"),
+ .require_argument = 1,
+ .processor = &set_filename,
+ .scls = (void *) fn};
return clo;
}
@@ -556,13 +541,10 @@ set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
char dummy[2];
(void) ctx;
- if (1 != SSCANF (value,
- "%llu%1s",
- val,
- dummy))
+ if (1 != SSCANF (value, "%llu%1s", val, dummy))
{
FPRINTF (stderr,
- _("You must pass a number to the `%s' option.\n"),
+ _ ("You must pass a number to the `%s' option.\n"),
option);
return GNUNET_SYSERR;
}
@@ -581,20 +563,18 @@ set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
*/
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_ulong (char shortName,
- const char *name,
- const char *argumentHelp,
- const char *description,
- unsigned long long *val)
-{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = shortName,
- .name = name,
- .argumentHelp = argumentHelp,
- .description = description,
- .require_argument = 1,
- .processor = &set_ulong,
- .scls = (void *) val
- };
+ const char *name,
+ const char *argumentHelp,
+ const char *description,
+ unsigned long long *val)
+{
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
+ .name = name,
+ .argumentHelp = argumentHelp,
+ .description = description,
+ .require_argument = 1,
+ .processor = &set_ulong,
+ .scls = (void *) val};
return clo;
}
@@ -620,14 +600,12 @@ set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
const char *value)
{
struct GNUNET_TIME_Relative *val = scls;
-
- (void) ctx;
- if (GNUNET_OK !=
- GNUNET_STRINGS_fancy_time_to_relative (value,
- val))
+
+ (void) ctx;
+ if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_relative (value, val))
{
FPRINTF (stderr,
- _("You must pass relative time to the `%s' option.\n"),
+ _ ("You must pass relative time to the `%s' option.\n"),
option);
return GNUNET_SYSERR;
}
@@ -647,20 +625,18 @@ set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
*/
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_relative_time (char shortName,
- const char *name,
- const char *argumentHelp,
- const char *description,
- struct GNUNET_TIME_Relative *val)
-{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = shortName,
- .name = name,
- .argumentHelp = argumentHelp,
- .description = description,
- .require_argument = 1,
- .processor = &set_relative_time,
- .scls = (void *) val
- };
+ const char *name,
+ const char *argumentHelp,
+ const char *description,
+ struct GNUNET_TIME_Relative *val)
+{
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
+ .name = name,
+ .argumentHelp = argumentHelp,
+ .description = description,
+ .require_argument = 1,
+ .processor = &set_relative_time,
+ .scls = (void *) val};
return clo;
}
@@ -688,12 +664,10 @@ set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
struct GNUNET_TIME_Absolute *val = scls;
(void) ctx;
- if (GNUNET_OK !=
- GNUNET_STRINGS_fancy_time_to_absolute (value,
- val))
+ if (GNUNET_OK != GNUNET_STRINGS_fancy_time_to_absolute (value, val))
{
FPRINTF (stderr,
- _("You must pass absolute time to the `%s' option.\n"),
+ _ ("You must pass absolute time to the `%s' option.\n"),
option);
return GNUNET_SYSERR;
}
@@ -713,20 +687,18 @@ set_absolute_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
*/
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_absolute_time (char shortName,
- const char *name,
- const char *argumentHelp,
- const char *description,
- struct GNUNET_TIME_Absolute *val)
-{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = shortName,
- .name = name,
- .argumentHelp = argumentHelp,
- .description = description,
- .require_argument = 1,
- .processor = &set_absolute_time,
- .scls = (void *) val
- };
+ const char *name,
+ const char *argumentHelp,
+ const char *description,
+ struct GNUNET_TIME_Absolute *val)
+{
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
+ .name = name,
+ .argumentHelp = argumentHelp,
+ .description = description,
+ .require_argument = 1,
+ .processor = &set_absolute_time,
+ .scls = (void *) val};
return clo;
}
@@ -755,20 +727,18 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
char dummy[2];
(void) ctx;
- if('-' == *value)
+ if ('-' == *value)
{
- FPRINTF (stderr,
- _("Your input for the '%s' option has to be a non negative number \n"),
- option);
- return GNUNET_SYSERR;
+ FPRINTF (stderr,
+ _ (
+ "Your input for the '%s' option has to be a non negative number \n"),
+ option);
+ return GNUNET_SYSERR;
}
- if (1 != SSCANF (value,
- "%u%1s",
- val,
- dummy))
+ if (1 != SSCANF (value, "%u%1s", val, dummy))
{
FPRINTF (stderr,
- _("You must pass a number to the `%s' option.\n"),
+ _ ("You must pass a number to the `%s' option.\n"),
option);
return GNUNET_SYSERR;
}
@@ -787,26 +757,23 @@ set_uint (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
*/
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_uint (char shortName,
- const char *name,
- const char *argumentHelp,
- const char *description,
- unsigned int *val)
-{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = shortName,
- .name = name,
- .argumentHelp = argumentHelp,
- .description = description,
- .require_argument = 1,
- .processor = &set_uint,
- .scls = (void *) val
- };
+ const char *name,
+ const char *argumentHelp,
+ const char *description,
+ unsigned int *val)
+{
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
+ .name = name,
+ .argumentHelp = argumentHelp,
+ .description = description,
+ .require_argument = 1,
+ .processor = &set_uint,
+ .scls = (void *) val};
return clo;
}
-
/**
* Set an option of type 'uint16_t' from the command line.
* A pointer to this function should be passed as part of the
@@ -822,30 +789,27 @@ GNUNET_GETOPT_option_uint (char shortName,
*/
static int
set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
- void *scls,
- const char *option,
- const char *value)
+ void *scls,
+ const char *option,
+ const char *value)
{
uint16_t *val = scls;
unsigned int v;
char dummy[2];
-
+
(void) ctx;
- if (1 != SSCANF (value,
- "%u%1s",
- &v,
- dummy))
+ if (1 != SSCANF (value, "%u%1s", &v, dummy))
{
FPRINTF (stderr,
- _("You must pass a number to the `%s' option.\n"),
+ _ ("You must pass a number to the `%s' option.\n"),
option);
return GNUNET_SYSERR;
}
if (v > UINT16_MAX)
{
FPRINTF (stderr,
- _("You must pass a number below %u to the `%s' option.\n"),
- (unsigned int) UINT16_MAX,
+ _ ("You must pass a number below %u to the `%s' option.\n"),
+ (unsigned int) UINT16_MAX,
option);
return GNUNET_SYSERR;
}
@@ -865,20 +829,18 @@ set_uint16 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
*/
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_uint16 (char shortName,
- const char *name,
- const char *argumentHelp,
- const char *description,
- uint16_t *val)
-{
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = shortName,
- .name = name,
- .argumentHelp = argumentHelp,
- .description = description,
- .require_argument = 1,
- .processor = &set_uint16,
- .scls = (void *) val
- };
+ const char *name,
+ const char *argumentHelp,
+ const char *description,
+ uint16_t *val)
+{
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
+ .name = name,
+ .argumentHelp = argumentHelp,
+ .description = description,
+ .require_argument = 1,
+ .processor = &set_uint16,
+ .scls = (void *) val};
return clo;
}
@@ -923,15 +885,16 @@ set_base32 (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
struct Base32Context *bc = scls;
(void) ctx;
- if (GNUNET_OK !=
- GNUNET_STRINGS_string_to_data (value,
- strlen (value),
- bc->val,
- bc->val_size))
+ if (GNUNET_OK != GNUNET_STRINGS_string_to_data (value,
+ strlen (value),
+ bc->val,
+ bc->val_size))
{
- fprintf (stderr,
- _("Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"),
- option);
+ fprintf (
+ stderr,
+ _ (
+ "Argument `%s' malformed. Expected base32 (Crockford) encoded value.\n"),
+ option);
return GNUNET_SYSERR;
}
return GNUNET_OK;
@@ -964,23 +927,21 @@ free_bc (void *cls)
*/
struct GNUNET_GETOPT_CommandLineOption
GNUNET_GETOPT_option_base32_fixed_size (char shortName,
- const char *name,
- const char *argumentHelp,
- const char *description,
- void *val,
- size_t val_size)
+ const char *name,
+ const char *argumentHelp,
+ const char *description,
+ void *val,
+ size_t val_size)
{
struct Base32Context *bc = GNUNET_new (struct Base32Context);
- struct GNUNET_GETOPT_CommandLineOption clo = {
- .shortName = shortName,
- .name = name,
- .argumentHelp = argumentHelp,
- .description = description,
- .require_argument = 1,
- .processor = &set_base32,
- .cleaner = &free_bc,
- .scls = (void *) bc
- };
+ struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName,
+ .name = name,
+ .argumentHelp = argumentHelp,
+ .description = description,
+ .require_argument = 1,
+ .processor = &set_base32,
+ .cleaner = &free_bc,
+ .scls = (void *) bc};
bc->val = val;
bc->val_size = val_size;
@@ -1002,4 +963,18 @@ GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt)
}
+/**
+ * Make the given option mutually exclusive with other options.
+ *
+ * @param opt option to modify
+ * @return @a opt with the exclusive flag set.
+ */
+struct GNUNET_GETOPT_CommandLineOption
+GNUNET_GETOPT_option_exclusive (struct GNUNET_GETOPT_CommandLineOption opt)
+{
+ opt.option_exclusive = 1;
+ return opt;
+}
+
+
/* end of getopt_helpers.c */