4 #include "error_util.h"
5 #include "command-line-arguments.h"
6 #include "string-util.h"
9 Search the array of keywords for a given argument, assigning the
10 output variable to the corresponding value. Return FALSE if nothing
15 _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
17 const notmuch_keyword_t *keywords = arg_desc->keywords;
20 /* No keyword given */
24 while (keywords->name) {
25 if (strcmp (arg_str, keywords->name) == 0) {
26 if (arg_desc->output_var) {
27 *((int *)arg_desc->output_var) = keywords->value;
34 fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);
36 fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
41 An arguments composed of comma-separated flags is parsed and the
42 output variable is assigned the ORed flag values. Return FALSE in
46 _process_flags_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
48 const notmuch_keyword_t *keyword;
49 const char *flag = arg_str;
53 if (next == '\0' || *arg_str == '\0') {
55 fprintf (stderr, "Option \"%s\" needs an flags argument.\n", arg_desc->name);
59 while ((flag = strtok_len_c (flag + flen, ",", &flen))) {
60 for (keyword = arg_desc->keywords, match = FALSE; keyword->name; keyword++) {
61 if (strncmp (flag, keyword->name, flen) == 0 &&
62 flen == strlen (keyword->name)) {
63 *((int *)arg_desc->output_var) |= keyword->value;
69 fprintf (stderr, "Unknown flag argument \"%.*s\" for option \"%s\".\n", (int)flen, flag, arg_desc->name);
78 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
81 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
84 if (strcmp (arg_str, "false") == 0) {
85 *((notmuch_bool_t *)arg_desc->output_var) = FALSE;
88 if (strcmp (arg_str, "true") == 0) {
89 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
92 fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
97 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
100 if (next == '\0' || arg_str[0] == '\0') {
101 fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
105 *((int *)arg_desc->output_var) = strtol (arg_str, &endptr, 10);
109 fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
110 arg_str, arg_desc->name);
114 static notmuch_bool_t
115 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
118 fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
121 if (arg_str[0] == '\0') {
122 fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
125 *((const char **)arg_desc->output_var) = arg_str;
130 Search for the {pos_arg_index}th position argument, return FALSE if
135 parse_position_arg (const char *arg_str, int pos_arg_index,
136 const notmuch_opt_desc_t *arg_desc) {
138 int pos_arg_counter = 0;
139 while (arg_desc->opt_type != NOTMUCH_OPT_END){
140 if (arg_desc->opt_type == NOTMUCH_OPT_POSITION) {
141 if (pos_arg_counter == pos_arg_index) {
142 if (arg_desc->output_var) {
143 *((const char **)arg_desc->output_var) = arg_str;
155 * Search for a non-positional (i.e. starting with --) argument matching arg,
156 * parse a possible value, and assign to *output_var
160 parse_option (const char *arg,
161 const notmuch_opt_desc_t *options) {
168 const notmuch_opt_desc_t *try;
169 for (try = options; try->opt_type != NOTMUCH_OPT_END; try++) {
173 if (strncmp (arg, try->name, strlen (try->name)) != 0)
176 char next = arg[strlen (try->name)];
177 const char *value = arg + strlen(try->name) + 1;
180 * If we have not reached the end of the argument (i.e. the
181 * next character is not a space or delimiter) then the
182 * argument could still match a longer option name later in
185 if (next != '=' && next != ':' && next != '\0')
188 if (try->output_var == NULL)
189 INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
191 switch (try->opt_type) {
192 case NOTMUCH_OPT_KEYWORD:
193 return _process_keyword_arg (try, next, value);
194 case NOTMUCH_OPT_FLAGS:
195 return _process_flags_arg (try, next, value);
196 case NOTMUCH_OPT_BOOLEAN:
197 return _process_boolean_arg (try, next, value);
198 case NOTMUCH_OPT_INT:
199 return _process_int_arg (try, next, value);
200 case NOTMUCH_OPT_STRING:
201 return _process_string_arg (try, next, value);
202 case NOTMUCH_OPT_POSITION:
203 case NOTMUCH_OPT_END:
205 INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
209 fprintf (stderr, "Unrecognized option: --%s\n", arg);
213 /* See command-line-arguments.h for description */
215 parse_arguments (int argc, char **argv,
216 const notmuch_opt_desc_t *options, int opt_index) {
218 int pos_arg_index = 0;
219 notmuch_bool_t more_args = TRUE;
221 while (more_args && opt_index < argc) {
222 if (strncmp (argv[opt_index],"--",2) != 0) {
224 more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
233 if (strlen (argv[opt_index]) == 2)
236 more_args = parse_option (argv[opt_index], options);