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