]> git.notmuchmail.org Git - notmuch/blob - command-line-arguments.c
notmuch-dump: add --format=(batch-tag|sup)
[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 && strncmp (arg, try->name, strlen (try->name)) == 0) {
133             char next = arg[strlen (try->name)];
134             const char *value= arg+strlen(try->name)+1;
135
136             /* If we have not reached the end of the argument
137                (i.e. the next character is not a space or delimiter)
138                then the argument could still match a longer option
139                name later in the option table.
140             */
141             if (next != '=' && next != ':' && next != '\0')
142                 continue;
143
144             if (try->output_var == NULL)
145                 INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
146
147             switch (try->opt_type) {
148             case NOTMUCH_OPT_KEYWORD:
149                 return _process_keyword_arg (try, next, value);
150                 break;
151             case NOTMUCH_OPT_BOOLEAN:
152                 return _process_boolean_arg (try, next, value);
153                 break;
154             case NOTMUCH_OPT_INT:
155                 return _process_int_arg (try, next, value);
156                 break;
157             case NOTMUCH_OPT_STRING:
158                 return _process_string_arg (try, next, value);
159                 break;
160             case NOTMUCH_OPT_POSITION:
161             case NOTMUCH_OPT_END:
162             default:
163                 INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
164                 /*UNREACHED*/
165             }
166         }
167     }
168     fprintf (stderr, "Unrecognized option: --%s\n", arg);
169     return FALSE;
170 }
171
172 /* See command-line-arguments.h for description */
173 int
174 parse_arguments (int argc, char **argv,
175                  const notmuch_opt_desc_t *options, int opt_index) {
176
177     int pos_arg_index = 0;
178     notmuch_bool_t more_args = TRUE;
179
180     while (more_args && opt_index < argc) {
181         if (strncmp (argv[opt_index],"--",2) != 0) {
182
183             more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
184
185             if (more_args) {
186                 pos_arg_index++;
187                 opt_index++;
188             }
189
190         } else {
191
192             if (strlen (argv[opt_index]) == 2)
193                 return opt_index+1;
194
195             more_args = parse_option (argv[opt_index], options);
196             if (more_args) {
197                 opt_index++;
198             } else {
199                 opt_index = -1;
200             }
201
202         }
203     }
204
205     return opt_index;
206 }