]> git.notmuchmail.org Git - notmuch/blob - command-line-arguments.c
cli/insert: require succesful message indexing for success status
[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 = arg_desc->keywords;
17
18     if (next == '\0') {
19         /* No keyword given */
20         arg_str = "";
21     }
22
23     while (keywords->name) {
24         if (strcmp (arg_str, keywords->name) == 0) {
25             if (arg_desc->output_var) {
26                 *((int *)arg_desc->output_var) = keywords->value;
27             }
28             return TRUE;
29         }
30         keywords++;
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
42     if (next == '\0') {
43         *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
44         return TRUE;
45     }
46     if (strcmp (arg_str, "false") == 0) {
47         *((notmuch_bool_t *)arg_desc->output_var) = FALSE;
48         return TRUE;
49     }
50     if (strcmp (arg_str, "true") == 0) {
51         *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
52         return TRUE;
53     }
54     fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
55     return FALSE;
56 }
57
58 static notmuch_bool_t
59 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
60
61     char *endptr;
62     if (next == '\0' || arg_str[0] == '\0') {
63         fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
64         return FALSE;
65     }
66
67     *((int *)arg_desc->output_var) = strtol (arg_str, &endptr, 10);
68     if (*endptr == '\0')
69         return TRUE;
70
71     fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
72              arg_str, arg_desc->name);
73     return FALSE;
74 }
75
76 static notmuch_bool_t
77 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
78
79     if (next == '\0') {
80         fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
81         return FALSE;
82     }
83     if (arg_str[0] == '\0') {
84         fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
85         return FALSE;
86     }
87     *((const char **)arg_desc->output_var) = arg_str;
88     return TRUE;
89 }
90
91 /*
92    Search for the {pos_arg_index}th position argument, return FALSE if
93    that does not exist.
94 */
95
96 notmuch_bool_t
97 parse_position_arg (const char *arg_str, int pos_arg_index,
98                     const notmuch_opt_desc_t *arg_desc) {
99
100     int pos_arg_counter = 0;
101     while (arg_desc->opt_type != NOTMUCH_OPT_END){
102         if (arg_desc->opt_type == NOTMUCH_OPT_POSITION) {
103             if (pos_arg_counter == pos_arg_index) {
104                 if (arg_desc->output_var) {
105                     *((const char **)arg_desc->output_var) = arg_str;
106                 }
107                 return TRUE;
108             }
109             pos_arg_counter++;
110         }
111         arg_desc++;
112     }
113     return FALSE;
114 }
115
116 /*
117  * Search for a non-positional (i.e. starting with --) argument matching arg,
118  * parse a possible value, and assign to *output_var
119  */
120
121 notmuch_bool_t
122 parse_option (const char *arg,
123               const notmuch_opt_desc_t *options) {
124
125     assert(arg);
126     assert(options);
127
128     arg += 2;
129
130     const notmuch_opt_desc_t *try;
131     for (try = options; try->opt_type != NOTMUCH_OPT_END; try++) {
132         if (! try->name)
133             continue;
134
135         if (strncmp (arg, try->name, strlen (try->name)) != 0)
136             continue;
137
138         char next = arg[strlen (try->name)];
139         const char *value = arg + strlen(try->name) + 1;
140
141         /*
142          * If we have not reached the end of the argument (i.e. the
143          * next character is not a space or delimiter) then the
144          * argument could still match a longer option name later in
145          * the option table.
146          */
147         if (next != '=' && next != ':' && next != '\0')
148             continue;
149
150         if (try->output_var == NULL)
151             INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
152
153         switch (try->opt_type) {
154         case NOTMUCH_OPT_KEYWORD:
155             return _process_keyword_arg (try, next, value);
156         case NOTMUCH_OPT_BOOLEAN:
157             return _process_boolean_arg (try, next, value);
158         case NOTMUCH_OPT_INT:
159             return _process_int_arg (try, next, value);
160         case NOTMUCH_OPT_STRING:
161             return _process_string_arg (try, next, value);
162         case NOTMUCH_OPT_POSITION:
163         case NOTMUCH_OPT_END:
164         default:
165             INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
166             /*UNREACHED*/
167         }
168     }
169     fprintf (stderr, "Unrecognized option: --%s\n", arg);
170     return FALSE;
171 }
172
173 /* See command-line-arguments.h for description */
174 int
175 parse_arguments (int argc, char **argv,
176                  const notmuch_opt_desc_t *options, int opt_index) {
177
178     int pos_arg_index = 0;
179     notmuch_bool_t more_args = TRUE;
180
181     while (more_args && opt_index < argc) {
182         if (strncmp (argv[opt_index],"--",2) != 0) {
183
184             more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
185
186             if (more_args) {
187                 pos_arg_index++;
188                 opt_index++;
189             }
190
191         } else {
192
193             if (strlen (argv[opt_index]) == 2)
194                 return opt_index+1;
195
196             more_args = parse_option (argv[opt_index], options);
197             if (more_args) {
198                 opt_index++;
199             } else {
200                 opt_index = -1;
201             }
202
203         }
204     }
205
206     return opt_index;
207 }