]> git.notmuchmail.org Git - notmuch/blob - command-line-arguments.c
cli: change while to for in keyword argument processing
[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 /*
8   Search the array of keywords for a given argument, assigning the
9   output variable to the corresponding value.  Return FALSE if nothing
10   matches.
11 */
12
13 static notmuch_bool_t
14 _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
15
16     const notmuch_keyword_t *keywords;
17
18     if (next == '\0') {
19         /* No keyword given */
20         arg_str = "";
21     }
22
23     for (keywords = arg_desc->keywords; keywords->name; keywords++) {
24         if (strcmp (arg_str, keywords->name) == 0) {
25             if (arg_desc->opt_flags)
26                 *arg_desc->opt_flags |= keywords->value;
27             else
28                 *arg_desc->opt_keyword = keywords->value;
29             return TRUE;
30         }
31     }
32     if (next != '\0')
33         fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);
34     else
35         fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
36     return FALSE;
37 }
38
39 static notmuch_bool_t
40 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
41     notmuch_bool_t value;
42
43     if (next == '\0' || strcmp (arg_str, "true") == 0) {
44         value = TRUE;
45     } else if (strcmp (arg_str, "false") == 0) {
46         value = FALSE;
47     } else {
48         fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
49         return FALSE;
50     }
51
52     *arg_desc->opt_bool = value;
53
54     return TRUE;
55 }
56
57 static notmuch_bool_t
58 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
59
60     char *endptr;
61     if (next == '\0' || arg_str[0] == '\0') {
62         fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
63         return FALSE;
64     }
65
66     *arg_desc->opt_int = strtol (arg_str, &endptr, 10);
67     if (*endptr == '\0')
68         return TRUE;
69
70     fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
71              arg_str, arg_desc->name);
72     return FALSE;
73 }
74
75 static notmuch_bool_t
76 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
77
78     if (next == '\0') {
79         fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
80         return FALSE;
81     }
82     if (arg_str[0] == '\0') {
83         fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
84         return FALSE;
85     }
86     *arg_desc->opt_string = arg_str;
87     return TRUE;
88 }
89
90 /* Return number of non-NULL opt_* fields in opt_desc. */
91 static int _opt_set_count (const notmuch_opt_desc_t *opt_desc)
92 {
93     return
94         !!opt_desc->opt_inherit +
95         !!opt_desc->opt_bool +
96         !!opt_desc->opt_int +
97         !!opt_desc->opt_keyword +
98         !!opt_desc->opt_flags +
99         !!opt_desc->opt_string +
100         !!opt_desc->opt_position;
101 }
102
103 /* Return TRUE if opt_desc is valid. */
104 static notmuch_bool_t _opt_valid (const notmuch_opt_desc_t *opt_desc)
105 {
106     int n = _opt_set_count (opt_desc);
107
108     if (n > 1)
109         INTERNAL_ERROR ("more than one non-NULL opt_* field for argument \"%s\"",
110                         opt_desc->name);
111
112     return n > 0;
113 }
114
115 /*
116    Search for the {pos_arg_index}th position argument, return FALSE if
117    that does not exist.
118 */
119
120 notmuch_bool_t
121 parse_position_arg (const char *arg_str, int pos_arg_index,
122                     const notmuch_opt_desc_t *arg_desc) {
123
124     int pos_arg_counter = 0;
125     while (_opt_valid (arg_desc)) {
126         if (arg_desc->opt_position) {
127             if (pos_arg_counter == pos_arg_index) {
128                 *arg_desc->opt_position = arg_str;
129                 if (arg_desc->present)
130                     *arg_desc->present = TRUE;
131                 return TRUE;
132             }
133             pos_arg_counter++;
134         }
135         arg_desc++;
136     }
137     return FALSE;
138 }
139
140 /*
141  * Search for a non-positional (i.e. starting with --) argument matching arg,
142  * parse a possible value, and assign to *output_var
143  */
144
145 int
146 parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_index)
147 {
148     assert(argv);
149
150     const char *_arg = argv[opt_index];
151
152     assert(_arg);
153     assert(options);
154
155     const char *arg = _arg + 2; /* _arg starts with -- */
156     const notmuch_opt_desc_t *try;
157
158     const char *next_arg = NULL;
159     if (opt_index < argc - 1  && strncmp (argv[opt_index + 1], "--", 2) != 0)
160         next_arg = argv[opt_index + 1];
161
162     for (try = options; _opt_valid (try); try++) {
163         if (try->opt_inherit) {
164             int new_index = parse_option (argc, argv, try->opt_inherit, opt_index);
165             if (new_index >= 0)
166                 return new_index;
167         }
168
169         if (! try->name)
170             continue;
171
172         if (strncmp (arg, try->name, strlen (try->name)) != 0)
173             continue;
174
175         char next = arg[strlen (try->name)];
176         const char *value = arg + strlen(try->name) + 1;
177
178         /*
179          * If we have not reached the end of the argument (i.e. the
180          * next character is not a space or delimiter) then the
181          * argument could still match a longer option name later in
182          * the option table.
183          */
184         if (next != '=' && next != ':' && next != '\0')
185             continue;
186
187         if (next == '\0' && next_arg != NULL && ! try->opt_bool) {
188             next = ' ';
189             value = next_arg;
190             opt_index ++;
191         }
192
193         notmuch_bool_t opt_status = FALSE;
194         if (try->opt_keyword || try->opt_flags)
195             opt_status = _process_keyword_arg (try, next, value);
196         else if (try->opt_bool)
197             opt_status = _process_boolean_arg (try, next, value);
198         else if (try->opt_int)
199             opt_status = _process_int_arg (try, next, value);
200         else if (try->opt_string)
201             opt_status = _process_string_arg (try, next, value);
202         else
203             INTERNAL_ERROR ("unknown or unhandled option \"%s\"", try->name);
204
205         if (! opt_status)
206             return -1;
207
208         if (try->present)
209             *try->present = TRUE;
210
211         return opt_index+1;
212     }
213     return -1;
214 }
215
216 /* See command-line-arguments.h for description */
217 int
218 parse_arguments (int argc, char **argv,
219                  const notmuch_opt_desc_t *options, int opt_index) {
220
221     int pos_arg_index = 0;
222     notmuch_bool_t more_args = TRUE;
223
224     while (more_args && opt_index < argc) {
225         if (strncmp (argv[opt_index],"--",2) != 0) {
226
227             more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
228
229             if (more_args) {
230                 pos_arg_index++;
231                 opt_index++;
232             }
233
234         } else {
235             int prev_opt_index = opt_index;
236
237             if (strlen (argv[opt_index]) == 2)
238                 return opt_index+1;
239
240             opt_index = parse_option (argc, argv, options, opt_index);
241             if (opt_index < 0) {
242                 fprintf (stderr, "Unrecognized option: %s\n", argv[prev_opt_index]);
243                 more_args = FALSE;
244             }
245         }
246     }
247
248     return opt_index;
249 }