]> rtime.felk.cvut.cz Git - notmuch.git/blob - command-line-arguments.c
cli: Add tests for 'search --output=addresses' and similar
[notmuch.git] / command-line-arguments.c
1 #include <assert.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include "error_util.h"
5 #include "command-line-arguments.h"
6 #include "string-util.h"
7
8 /*
9   Search the array of keywords for a given argument, assigning the
10   output variable to the corresponding value.  Return FALSE if nothing
11   matches.
12 */
13
14 static notmuch_bool_t
15 _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
16
17     const notmuch_keyword_t *keywords = arg_desc->keywords;
18
19     if (next == '\0') {
20         /* No keyword given */
21         arg_str = "";
22     }
23
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;
28             }
29             return TRUE;
30         }
31         keywords++;
32     }
33     if (next != '\0')
34         fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);
35     else
36         fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
37     return FALSE;
38 }
39
40 /*
41   An arguments composed of comma-separated flags is parsed and the
42   output variable is assigned the ORed flag values. Return FALSE in
43   case of error.
44 */
45 static notmuch_bool_t
46 _process_flags_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
47
48     const notmuch_keyword_t *keyword;
49     const char *flag = arg_str;
50     size_t flen = 0;
51     notmuch_bool_t match;
52
53     if (next == '\0' || *arg_str == '\0') {
54         /* No flag given */
55         fprintf (stderr, "Option \"%s\" needs an flags argument.\n", arg_desc->name);
56         return FALSE;
57     }
58
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;
64                 match = TRUE;
65                 break;
66             }
67         }
68         if (! match) {
69             fprintf (stderr, "Unknown flag argument \"%.*s\" for option \"%s\".\n", (int)flen, flag, arg_desc->name);
70             return FALSE;
71         }
72     }
73     return TRUE;
74 }
75
76
77 static notmuch_bool_t
78 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
79
80     if (next == '\0') {
81         *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
82         return TRUE;
83     }
84     if (strcmp (arg_str, "false") == 0) {
85         *((notmuch_bool_t *)arg_desc->output_var) = FALSE;
86         return TRUE;
87     }
88     if (strcmp (arg_str, "true") == 0) {
89         *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
90         return TRUE;
91     }
92     fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
93     return FALSE;
94 }
95
96 static notmuch_bool_t
97 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
98
99     char *endptr;
100     if (next == '\0' || arg_str[0] == '\0') {
101         fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
102         return FALSE;
103     }
104
105     *((int *)arg_desc->output_var) = strtol (arg_str, &endptr, 10);
106     if (*endptr == '\0')
107         return TRUE;
108
109     fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
110              arg_str, arg_desc->name);
111     return FALSE;
112 }
113
114 static notmuch_bool_t
115 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
116
117     if (next == '\0') {
118         fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
119         return FALSE;
120     }
121     if (arg_str[0] == '\0') {
122         fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
123         return FALSE;
124     }
125     *((const char **)arg_desc->output_var) = arg_str;
126     return TRUE;
127 }
128
129 /*
130    Search for the {pos_arg_index}th position argument, return FALSE if
131    that does not exist.
132 */
133
134 notmuch_bool_t
135 parse_position_arg (const char *arg_str, int pos_arg_index,
136                     const notmuch_opt_desc_t *arg_desc) {
137
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;
144                 }
145                 return TRUE;
146             }
147             pos_arg_counter++;
148         }
149         arg_desc++;
150     }
151     return FALSE;
152 }
153
154 /*
155  * Search for a non-positional (i.e. starting with --) argument matching arg,
156  * parse a possible value, and assign to *output_var
157  */
158
159 notmuch_bool_t
160 parse_option (const char *arg,
161               const notmuch_opt_desc_t *options) {
162
163     assert(arg);
164     assert(options);
165
166     arg += 2;
167
168     const notmuch_opt_desc_t *try;
169     for (try = options; try->opt_type != NOTMUCH_OPT_END; try++) {
170         if (! try->name)
171             continue;
172
173         if (strncmp (arg, try->name, strlen (try->name)) != 0)
174             continue;
175
176         char next = arg[strlen (try->name)];
177         const char *value = arg + strlen(try->name) + 1;
178
179         /*
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
183          * the option table.
184          */
185         if (next != '=' && next != ':' && next != '\0')
186             continue;
187
188         if (try->output_var == NULL)
189             INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
190
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:
204         default:
205             INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
206             /*UNREACHED*/
207         }
208     }
209     fprintf (stderr, "Unrecognized option: --%s\n", arg);
210     return FALSE;
211 }
212
213 /* See command-line-arguments.h for description */
214 int
215 parse_arguments (int argc, char **argv,
216                  const notmuch_opt_desc_t *options, int opt_index) {
217
218     int pos_arg_index = 0;
219     notmuch_bool_t more_args = TRUE;
220
221     while (more_args && opt_index < argc) {
222         if (strncmp (argv[opt_index],"--",2) != 0) {
223
224             more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
225
226             if (more_args) {
227                 pos_arg_index++;
228                 opt_index++;
229             }
230
231         } else {
232
233             if (strlen (argv[opt_index]) == 2)
234                 return opt_index+1;
235
236             more_args = parse_option (argv[opt_index], options);
237             if (more_args) {
238                 opt_index++;
239             } else {
240                 opt_index = -1;
241             }
242
243         }
244     }
245
246     return opt_index;
247 }