#include <stdio.h>
#include "error_util.h"
#include "command-line-arguments.h"
+#include "string-util.h"
/*
Search the array of keywords for a given argument, assigning the
return FALSE;
}
+/*
+ An arguments composed of comma-separated flags is parsed and the
+ output variable is assigned the ORed flag values. Return FALSE in
+ case of error.
+*/
+static notmuch_bool_t
+_process_flags_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
+
+ const notmuch_keyword_t *keyword;
+ const char *flag = arg_str;
+ size_t flen = 0;
+ notmuch_bool_t match;
+
+ if (next == '\0' || *arg_str == '\0') {
+ /* No flag given */
+ fprintf (stderr, "Option \"%s\" needs an flags argument.\n", arg_desc->name);
+ return FALSE;
+ }
+
+ while ((flag = strtok_len_c (flag + flen, ",", &flen))) {
+ for (keyword = arg_desc->keywords, match = FALSE; keyword->name; keyword++) {
+ if (strncmp (flag, keyword->name, flen) == 0 &&
+ flen == strlen (keyword->name)) {
+ *((int *)arg_desc->output_var) |= keyword->value;
+ match = TRUE;
+ break;
+ }
+ }
+ if (! match) {
+ fprintf (stderr, "Unknown flag argument \"%.*s\" for option \"%s\".\n", (int)flen, flag, arg_desc->name);
+ return FALSE;
+ }
+ }
+ return TRUE;
+}
+
+
static notmuch_bool_t
_process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
const notmuch_opt_desc_t *try;
for (try = options; try->opt_type != NOTMUCH_OPT_END; try++) {
- if (try->name && strncmp (arg, try->name, strlen (try->name)) == 0) {
- char next = arg[strlen (try->name)];
- const char *value= arg+strlen(try->name)+1;
-
- /* If we have not reached the end of the argument
- (i.e. the next character is not a space or delimiter)
- then the argument could still match a longer option
- name later in the option table.
- */
- if (next != '=' && next != ':' && next != '\0')
- continue;
-
- if (try->output_var == NULL)
- INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
-
- switch (try->opt_type) {
- case NOTMUCH_OPT_KEYWORD:
- return _process_keyword_arg (try, next, value);
- break;
- case NOTMUCH_OPT_BOOLEAN:
- return _process_boolean_arg (try, next, value);
- break;
- case NOTMUCH_OPT_INT:
- return _process_int_arg (try, next, value);
- break;
- case NOTMUCH_OPT_STRING:
- return _process_string_arg (try, next, value);
- break;
- case NOTMUCH_OPT_POSITION:
- case NOTMUCH_OPT_END:
- default:
- INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
- /*UNREACHED*/
- }
+ if (! try->name)
+ continue;
+
+ if (strncmp (arg, try->name, strlen (try->name)) != 0)
+ continue;
+
+ char next = arg[strlen (try->name)];
+ const char *value = arg + strlen(try->name) + 1;
+
+ /*
+ * If we have not reached the end of the argument (i.e. the
+ * next character is not a space or delimiter) then the
+ * argument could still match a longer option name later in
+ * the option table.
+ */
+ if (next != '=' && next != ':' && next != '\0')
+ continue;
+
+ if (try->output_var == NULL)
+ INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
+
+ switch (try->opt_type) {
+ case NOTMUCH_OPT_KEYWORD:
+ return _process_keyword_arg (try, next, value);
+ case NOTMUCH_OPT_FLAGS:
+ return _process_flags_arg (try, next, value);
+ case NOTMUCH_OPT_BOOLEAN:
+ return _process_boolean_arg (try, next, value);
+ case NOTMUCH_OPT_INT:
+ return _process_int_arg (try, next, value);
+ case NOTMUCH_OPT_STRING:
+ return _process_string_arg (try, next, value);
+ case NOTMUCH_OPT_POSITION:
+ case NOTMUCH_OPT_END:
+ default:
+ INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
+ /*UNREACHED*/
}
}
fprintf (stderr, "Unrecognized option: --%s\n", arg);