]> git.notmuchmail.org Git - notmuch/blob - command-line-arguments.c
Merge branch 'release'
[notmuch] / 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
7 typedef enum {
8     OPT_FAILED,         /* false */
9     OPT_OK,             /* good */
10     OPT_GIVEBACK,       /* pop one of the arguments you thought you were getting off the stack */
11 } opt_handled;
12
13 /*
14  * Search the array of keywords for a given argument, assigning the
15  * output variable to the corresponding value.  Return false if nothing
16  * matches.
17  */
18
19 static opt_handled
20 _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next,
21                       const char *arg_str, bool negate)
22 {
23     const notmuch_keyword_t *keywords;
24
25     if (next == '\0') {
26         /* No keyword given */
27         arg_str = "";
28     }
29
30     for (keywords = arg_desc->keywords; keywords->name; keywords++) {
31         if (strcmp (arg_str, keywords->name) != 0)
32             continue;
33
34         if (arg_desc->opt_flags && negate)
35             *arg_desc->opt_flags &= ~keywords->value;
36         else if (arg_desc->opt_flags)
37             *arg_desc->opt_flags |= keywords->value;
38         else
39             *arg_desc->opt_keyword = keywords->value;
40
41         return OPT_OK;
42     }
43
44     if (arg_desc->opt_keyword && arg_desc->keyword_no_arg_value && next != ':' && next != '=') {
45         for (keywords = arg_desc->keywords; keywords->name; keywords++) {
46             if (strcmp (arg_desc->keyword_no_arg_value, keywords->name) != 0)
47                 continue;
48
49             *arg_desc->opt_keyword = keywords->value;
50             fprintf (stderr, "Warning: No known keyword option given for \"%s\", choosing value \"%s\"."
51                      "  Please specify the argument explicitly!\n", arg_desc->name, arg_desc->keyword_no_arg_value);
52
53             return OPT_GIVEBACK;
54         }
55         fprintf (stderr, "No matching keyword for option \"%s\" and default value \"%s\" is invalid.\n", arg_str, arg_desc->name);
56         return OPT_FAILED;
57     }
58
59     if (next != '\0')
60         fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);
61     else
62         fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
63     return OPT_FAILED;
64 }
65
66 static opt_handled
67 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next,
68                       const char *arg_str, bool negate)
69 {
70     bool value;
71
72     if (next == '\0' || strcmp (arg_str, "true") == 0) {
73         value = true;
74     } else if (strcmp (arg_str, "false") == 0) {
75         value = false;
76     } else {
77         fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
78         return OPT_FAILED;
79     }
80
81     *arg_desc->opt_bool = negate ? (! value) : value;
82
83     return OPT_OK;
84 }
85
86 static opt_handled
87 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str)
88 {
89
90     char *endptr;
91
92     if (next == '\0' || arg_str[0] == '\0') {
93         fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
94         return OPT_FAILED;
95     }
96
97     *arg_desc->opt_int = strtol (arg_str, &endptr, 10);
98     if (*endptr == '\0')
99         return OPT_OK;
100
101     fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
102              arg_str, arg_desc->name);
103     return OPT_FAILED;
104 }
105
106 static opt_handled
107 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str)
108 {
109
110     if (next == '\0') {
111         fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
112         return OPT_FAILED;
113     }
114     if (arg_str[0] == '\0' && ! arg_desc->allow_empty) {
115         fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
116         return OPT_FAILED;
117     }
118     *arg_desc->opt_string = arg_str;
119     return OPT_OK;
120 }
121
122 /* Return number of non-NULL opt_* fields in opt_desc. */
123 static int
124 _opt_set_count (const notmuch_opt_desc_t *opt_desc)
125 {
126     return
127         (bool) opt_desc->opt_inherit +
128         (bool) opt_desc->opt_bool +
129         (bool) opt_desc->opt_int +
130         (bool) opt_desc->opt_keyword +
131         (bool) opt_desc->opt_flags +
132         (bool) opt_desc->opt_string +
133         (bool) opt_desc->opt_position;
134 }
135
136 /* Return true if opt_desc is valid. */
137 static bool
138 _opt_valid (const notmuch_opt_desc_t *opt_desc)
139 {
140     int n = _opt_set_count (opt_desc);
141
142     if (n > 1)
143         INTERNAL_ERROR ("more than one non-NULL opt_* field for argument \"%s\"",
144                         opt_desc->name);
145
146     return n > 0;
147 }
148
149 /*
150  * Search for the {pos_arg_index}th position argument, return false if
151  * that does not exist.
152  */
153
154 bool
155 parse_position_arg (const char *arg_str, int pos_arg_index,
156                     const notmuch_opt_desc_t *arg_desc)
157 {
158
159     int pos_arg_counter = 0;
160
161     while (_opt_valid (arg_desc)) {
162         if (arg_desc->opt_position) {
163             if (pos_arg_counter == pos_arg_index) {
164                 *arg_desc->opt_position = arg_str;
165                 if (arg_desc->present)
166                     *arg_desc->present = true;
167                 return true;
168             }
169             pos_arg_counter++;
170         }
171         arg_desc++;
172     }
173     return false;
174 }
175
176 #define NEGATIVE_PREFIX "no-"
177
178 /*
179  * Search for a non-positional (i.e. starting with --) argument matching arg,
180  * parse a possible value, and assign to *output_var
181  */
182
183 int
184 parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_index)
185 {
186     assert (argv);
187
188     const char *_arg = argv[opt_index];
189
190     assert (_arg);
191     assert (options);
192
193     const char *arg = _arg + 2; /* _arg starts with -- */
194     const char *negative_arg = NULL;
195
196     /* See if this is a --no-argument */
197     if (strlen (arg) > strlen (NEGATIVE_PREFIX) &&
198         strncmp (arg, NEGATIVE_PREFIX, strlen (NEGATIVE_PREFIX)) == 0) {
199         negative_arg = arg + strlen (NEGATIVE_PREFIX);
200     }
201
202     const notmuch_opt_desc_t *try;
203
204     const char *next_arg = NULL;
205     if (opt_index < argc - 1  && strncmp (argv[opt_index + 1], "--", 2) != 0)
206         next_arg = argv[opt_index + 1];
207
208     for (try = options; _opt_valid (try); try++) {
209         if (try->opt_inherit) {
210             int new_index = parse_option (argc, argv, try->opt_inherit, opt_index);
211             if (new_index >= 0)
212                 return new_index;
213         }
214
215         if (! try->name)
216             continue;
217
218         char next;
219         const char *value;
220         bool negate = false;
221
222         if (strncmp (arg, try->name, strlen (try->name)) == 0) {
223             next = arg[strlen (try->name)];
224             value = arg + strlen (try->name) + 1;
225         } else if (negative_arg && (try->opt_bool || try->opt_flags) &&
226                    strncmp (negative_arg, try->name, strlen (try->name)) == 0) {
227             next = negative_arg[strlen (try->name)];
228             value = negative_arg + strlen (try->name) + 1;
229             /* The argument part of --no-argument matches, negate the result. */
230             negate = true;
231         } else {
232             continue;
233         }
234
235         /*
236          * If we have not reached the end of the argument (i.e. the
237          * next character is not a space or delimiter) then the
238          * argument could still match a longer option name later in
239          * the option table.
240          */
241         if (next != '=' && next != ':' && next != '\0')
242             continue;
243
244         bool lookahead = (next == '\0' && next_arg != NULL && ! try->opt_bool);
245
246         if (lookahead) {
247             next = ' ';
248             value = next_arg;
249             opt_index++;
250         }
251
252         opt_handled opt_status = OPT_FAILED;
253         if (try->opt_keyword || try->opt_flags)
254             opt_status = _process_keyword_arg (try, next, value, negate);
255         else if (try->opt_bool)
256             opt_status = _process_boolean_arg (try, next, value, negate);
257         else if (try->opt_int)
258             opt_status = _process_int_arg (try, next, value);
259         else if (try->opt_string)
260             opt_status = _process_string_arg (try, next, value);
261         else
262             INTERNAL_ERROR ("unknown or unhandled option \"%s\"", try->name);
263
264         if (opt_status == OPT_FAILED)
265             return -1;
266
267         if (lookahead && opt_status == OPT_GIVEBACK)
268             opt_index--;
269
270         if (try->present)
271             *try->present = true;
272
273         return opt_index + 1;
274     }
275     return -1;
276 }
277
278 /* See command-line-arguments.h for description */
279 int
280 parse_arguments (int argc, char **argv,
281                  const notmuch_opt_desc_t *options, int opt_index)
282 {
283
284     int pos_arg_index = 0;
285     bool more_args = true;
286
287     while (more_args && opt_index < argc) {
288         if (strncmp (argv[opt_index], "--", 2) != 0) {
289
290             more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
291
292             if (more_args) {
293                 pos_arg_index++;
294                 opt_index++;
295             }
296
297         } else {
298             int prev_opt_index = opt_index;
299
300             if (strlen (argv[opt_index]) == 2)
301                 return opt_index + 1;
302
303             opt_index = parse_option (argc, argv, options, opt_index);
304             if (opt_index < 0) {
305                 fprintf (stderr, "Unrecognized option: %s\n", argv[prev_opt_index]);
306                 more_args = false;
307             }
308         }
309     }
310
311     return opt_index;
312 }