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