aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-09-09 18:36:01 +0000
committerChristian Grothoff <christian@grothoff.org>2012-09-09 18:36:01 +0000
commit15af56ceb3ae31463a79c95b34841fbdec920ba5 (patch)
tree1e1de33b94a3cd493179a80bcfeaa332b7e3fc10 /src
parent85869129442a971414710ebd8bef97433570083b (diff)
new getopt helper function to parse relative time command line argument
Diffstat (limited to 'src')
-rw-r--r--src/fs/gnunet-search.c23
-rw-r--r--src/include/gnunet_getopt_lib.h18
-rw-r--r--src/util/getopt_helpers.c32
3 files changed, 59 insertions, 14 deletions
diff --git a/src/fs/gnunet-search.c b/src/fs/gnunet-search.c
index 88507f608a..200dc77aba 100644
--- a/src/fs/gnunet-search.c
+++ b/src/fs/gnunet-search.c
@@ -42,11 +42,14 @@ static struct GNUNET_FS_DirectoryBuilder *db;
static unsigned int anonymity = 1;
-static unsigned long long timeout;
+/**
+ * Timeout for the search, 0 means to wait for CTRL-C.
+ */
+static struct GNUNET_TIME_Relative timeout;
static unsigned int results_limit;
-static unsigned int results = 0;
+static unsigned int results;
static int verbose;
@@ -220,7 +223,6 @@ run (void *cls, char *const *args, const char *cfgfile,
struct GNUNET_FS_Uri *uri;
unsigned int argc;
enum GNUNET_FS_SearchOptions options;
- struct GNUNET_TIME_Relative delay;
argc = 0;
while (NULL != args[argc])
@@ -257,16 +259,11 @@ run (void *cls, char *const *args, const char *cfgfile,
ret = 1;
return;
}
- if (timeout != 0)
- {
- delay.rel_value = timeout;
- GNUNET_SCHEDULER_add_delayed (delay, &shutdown_task, NULL);
- }
+ if (0 != timeout.rel_value)
+ GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_task, NULL);
else
- {
GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
NULL);
- }
}
@@ -290,9 +287,9 @@ main (int argc, char *const *argv)
{'o', "output", "PREFIX",
gettext_noop ("write search results to file starting with PREFIX"),
1, &GNUNET_GETOPT_set_string, &output_filename},
- {'t', "timeout", "VALUE",
- gettext_noop ("automatically terminate search after VALUE ms"),
- 1, &GNUNET_GETOPT_set_ulong, &timeout},
+ {'t', "timeout", "DELAY",
+ gettext_noop ("automatically terminate search after DELAY"),
+ 1, &GNUNET_GETOPT_set_relative_time, &timeout},
{'V', "verbose", NULL,
gettext_noop ("be verbose (print progress information)"),
0, &GNUNET_GETOPT_set_one, &verbose},
diff --git a/src/include/gnunet_getopt_lib.h b/src/include/gnunet_getopt_lib.h
index 4b1873c6ec..14ba15d669 100644
--- a/src/include/gnunet_getopt_lib.h
+++ b/src/include/gnunet_getopt_lib.h
@@ -232,6 +232,24 @@ GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
/**
+ * Set an option of type 'struct GNUNET_TIME_Relative' from the command line.
+ * A pointer to this function should be passed as part of the
+ * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
+ * of this type. It should be followed by a pointer to a value of
+ * type 'struct GNUNET_TIME_Relative'.
+ *
+ * @param ctx command line processing context
+ * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative')
+ * @param option name of the option
+ * @param value actual value of the option as a string.
+ * @return GNUNET_OK if parsing the value worked
+ */
+int
+GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
+ void *scls, const char *option, const char *value);
+
+
+/**
* Set an option of type 'unsigned int' from the command line.
* A pointer to this function should be passed as part of the
* 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
diff --git a/src/util/getopt_helpers.c b/src/util/getopt_helpers.c
index a31080fa0c..a80bff4a36 100644
--- a/src/util/getopt_helpers.c
+++ b/src/util/getopt_helpers.c
@@ -26,7 +26,7 @@
#include "platform.h"
#include "gnunet_common.h"
-#include "gnunet_getopt_lib.h"
+#include "gnunet_util_lib.h"
#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
@@ -263,6 +263,36 @@ GNUNET_GETOPT_set_ulong (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
/**
+ * Set an option of type 'struct GNUNET_TIME_Relative' from the command line.
+ * A pointer to this function should be passed as part of the
+ * 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options
+ * of this type. It should be followed by a pointer to a value of
+ * type 'struct GNUNET_TIME_Relative'.
+ *
+ * @param ctx command line processing context
+ * @param scls additional closure (will point to the 'struct GNUNET_TIME_Relative')
+ * @param option name of the option
+ * @param value actual value of the option as a string.
+ * @return GNUNET_OK if parsing the value worked
+ */
+int
+GNUNET_GETOPT_set_relative_time (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
+ void *scls, const char *option, const char *value)
+{
+ struct GNUNET_TIME_Relative *val = scls;
+
+ if (GNUNET_OK !=
+ GNUNET_STRINGS_fancy_time_to_relative (value,
+ val))
+ {
+ FPRINTF (stderr, _("You must pass relative time to the `%s' option.\n"), option);
+ return GNUNET_SYSERR;
+ }
+ return GNUNET_OK;
+}
+
+
+/**
* Set an option of type 'unsigned int' from the command line.
* A pointer to this function should be passed as part of the
* 'struct GNUNET_GETOPT_CommandLineOption' array to initialize options