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 typedef enum {
8     OPT_FAILED,         /* false */
9     OPT_OK,             /* good */
10     OPT_GIVEBACK,       /* pop one of the arguments you thought you were getting off the stack */
11 } opt_handled;
12 
13 /*
14  * Search the array of keywords for a given argument, assigning the
15  * output variable to the corresponding value.  Return false if nothing
16  * matches.
17  */
18 
19 static opt_handled
_process_keyword_arg(const notmuch_opt_desc_t * arg_desc,char next,const char * arg_str,bool negate)20 _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next,
21 		      const char *arg_str, bool negate)
22 {
23     const notmuch_keyword_t *keywords;
24 
25     if (next == '\0') {
26 	/* No keyword given */
27 	arg_str = "";
28     }
29 
30     for (keywords = arg_desc->keywords; keywords->name; keywords++) {
31 	if (strcmp (arg_str, keywords->name) != 0)
32 	    continue;
33 
34 	if (arg_desc->opt_flags && negate)
35 	    *arg_desc->opt_flags &= ~keywords->value;
36 	else if (arg_desc->opt_flags)
37 	    *arg_desc->opt_flags |= keywords->value;
38 	else
39 	    *arg_desc->opt_keyword = keywords->value;
40 
41 	return OPT_OK;
42     }
43 
44     if (arg_desc->opt_keyword && arg_desc->keyword_no_arg_value && next != ':' && next != '=') {
45 	for (keywords = arg_desc->keywords; keywords->name; keywords++) {
46 	    if (strcmp (arg_desc->keyword_no_arg_value, keywords->name) != 0)
47 		continue;
48 
49 	    *arg_desc->opt_keyword = keywords->value;
50 	    fprintf (stderr,
51 		     "Warning: No known keyword option given for \"%s\", choosing value \"%s\"."
52 		     "  Please specify the argument explicitly!\n", arg_desc->name,
53 		     arg_desc->keyword_no_arg_value);
54 
55 	    return OPT_GIVEBACK;
56 	}
57 	fprintf (stderr,
58 		 "No matching keyword for option \"%s\" and default value \"%s\" is invalid.\n",
59 		 arg_str,
60 		 arg_desc->name);
61 	return OPT_FAILED;
62     }
63 
64     if (next != '\0')
65 	fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str,
66 		 arg_desc->name);
67     else
68 	fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
69     return OPT_FAILED;
70 }
71 
72 static opt_handled
_process_boolean_arg(const notmuch_opt_desc_t * arg_desc,char next,const char * arg_str,bool negate)73 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next,
74 		      const char *arg_str, bool negate)
75 {
76     bool value;
77 
78     if (next == '\0' || strcmp (arg_str, "true") == 0) {
79 	value = true;
80     } else if (strcmp (arg_str, "false") == 0) {
81 	value = false;
82     } else {
83 	fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str,
84 		 arg_desc->name);
85 	return OPT_FAILED;
86     }
87 
88     *arg_desc->opt_bool = negate ? (! value) : value;
89 
90     return OPT_OK;
91 }
92 
93 static opt_handled
_process_int_arg(const notmuch_opt_desc_t * arg_desc,char next,const char * arg_str)94 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str)
95 {
96 
97     char *endptr;
98 
99     if (next == '\0' || arg_str[0] == '\0') {
100 	fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
101 	return OPT_FAILED;
102     }
103 
104     *arg_desc->opt_int = strtol (arg_str, &endptr, 10);
105     if (*endptr == '\0')
106 	return OPT_OK;
107 
108     fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
109 	     arg_str, arg_desc->name);
110     return OPT_FAILED;
111 }
112 
113 static opt_handled
_process_string_arg(const notmuch_opt_desc_t * arg_desc,char next,const char * arg_str)114 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str)
115 {
116 
117     if (next == '\0') {
118 	fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
119 	return OPT_FAILED;
120     }
121     if (arg_str[0] == '\0' && ! arg_desc->allow_empty) {
122 	fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
123 	return OPT_FAILED;
124     }
125     *arg_desc->opt_string = arg_str;
126     return OPT_OK;
127 }
128 
129 /* Return number of non-NULL opt_* fields in opt_desc. */
130 static int
_opt_set_count(const notmuch_opt_desc_t * opt_desc)131 _opt_set_count (const notmuch_opt_desc_t *opt_desc)
132 {
133     return
134 	(bool) opt_desc->opt_inherit +
135 	(bool) opt_desc->opt_bool +
136 	(bool) opt_desc->opt_int +
137 	(bool) opt_desc->opt_keyword +
138 	(bool) opt_desc->opt_flags +
139 	(bool) opt_desc->opt_string +
140 	(bool) opt_desc->opt_position;
141 }
142 
143 /* Return true if opt_desc is valid. */
144 static bool
_opt_valid(const notmuch_opt_desc_t * opt_desc)145 _opt_valid (const notmuch_opt_desc_t *opt_desc)
146 {
147     int n = _opt_set_count (opt_desc);
148 
149     if (n > 1)
150 	INTERNAL_ERROR ("more than one non-NULL opt_* field for argument \"%s\"",
151 			opt_desc->name);
152 
153     return n > 0;
154 }
155 
156 /*
157  * Search for the {pos_arg_index}th position argument, return false if
158  * that does not exist.
159  */
160 
161 bool
parse_position_arg(const char * arg_str,int pos_arg_index,const notmuch_opt_desc_t * arg_desc)162 parse_position_arg (const char *arg_str, int pos_arg_index,
163 		    const notmuch_opt_desc_t *arg_desc)
164 {
165 
166     int pos_arg_counter = 0;
167 
168     while (_opt_valid (arg_desc)) {
169 	if (arg_desc->opt_position) {
170 	    if (pos_arg_counter == pos_arg_index) {
171 		*arg_desc->opt_position = arg_str;
172 		if (arg_desc->present)
173 		    *arg_desc->present = true;
174 		return true;
175 	    }
176 	    pos_arg_counter++;
177 	}
178 	arg_desc++;
179     }
180     return false;
181 }
182 
183 #define NEGATIVE_PREFIX "no-"
184 
185 /*
186  * Search for a non-positional (i.e. starting with --) argument matching arg,
187  * parse a possible value, and assign to *output_var
188  */
189 
190 int
parse_option(int argc,char ** argv,const notmuch_opt_desc_t * options,int opt_index)191 parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_index)
192 {
193     assert (argv);
194 
195     const char *_arg = argv[opt_index];
196 
197     assert (_arg);
198     assert (options);
199 
200     const char *arg = _arg + 2; /* _arg starts with -- */
201     const char *negative_arg = NULL;
202 
203     /* See if this is a --no-argument */
204     if (strlen (arg) > strlen (NEGATIVE_PREFIX) &&
205 	strncmp (arg, NEGATIVE_PREFIX, strlen (NEGATIVE_PREFIX)) == 0) {
206 	negative_arg = arg + strlen (NEGATIVE_PREFIX);
207     }
208 
209     const notmuch_opt_desc_t *try;
210 
211     const char *next_arg = NULL;
212 
213     if (opt_index < argc - 1  && strncmp (argv[opt_index + 1], "--", 2) != 0)
214 	next_arg = argv[opt_index + 1];
215 
216     for (try = options; _opt_valid (try); try++) {
217 	if (try->opt_inherit) {
218 	    int new_index = parse_option (argc, argv, try->opt_inherit, opt_index);
219 	    if (new_index >= 0)
220 		return new_index;
221 	}
222 
223 	if (! try->name)
224 	    continue;
225 
226 	char next;
227 	const char *value;
228 	bool negate = false;
229 
230 	if (strncmp (arg, try->name, strlen (try->name)) == 0) {
231 	    next = arg[strlen (try->name)];
232 	    value = arg + strlen (try->name) + 1;
233 	} else if (negative_arg && (try->opt_bool || try->opt_flags) &&
234 		   strncmp (negative_arg, try->name, strlen (try->name)) == 0) {
235 	    next = negative_arg[strlen (try->name)];
236 	    value = negative_arg + strlen (try->name) + 1;
237 	    /* The argument part of --no-argument matches, negate the result. */
238 	    negate = true;
239 	} else {
240 	    continue;
241 	}
242 
243 	/*
244 	 * If we have not reached the end of the argument (i.e. the
245 	 * next character is not a space or delimiter) then the
246 	 * argument could still match a longer option name later in
247 	 * the option table.
248 	 */
249 	if (next != '=' && next != ':' && next != '\0')
250 	    continue;
251 
252 	bool lookahead = (next == '\0' && next_arg != NULL && ! try->opt_bool);
253 
254 	if (lookahead) {
255 	    next = ' ';
256 	    value = next_arg;
257 	    opt_index++;
258 	}
259 
260 	opt_handled opt_status = OPT_FAILED;
261 	if (try->opt_keyword || try->opt_flags)
262 	    opt_status = _process_keyword_arg (try, next, value, negate);
263 	else if (try->opt_bool)
264 	    opt_status = _process_boolean_arg (try, next, value, negate);
265 	else if (try->opt_int)
266 	    opt_status = _process_int_arg (try, next, value);
267 	else if (try->opt_string)
268 	    opt_status = _process_string_arg (try, next, value);
269 	else
270 	    INTERNAL_ERROR ("unknown or unhandled option \"%s\"", try->name);
271 
272 	if (opt_status == OPT_FAILED)
273 	    return -1;
274 
275 	if (lookahead && opt_status == OPT_GIVEBACK)
276 	    opt_index--;
277 
278 	if (try->present)
279 	    *try->present = true;
280 
281 	return opt_index + 1;
282     }
283     return -1;
284 }
285 
286 /* See command-line-arguments.h for description */
287 int
parse_arguments(int argc,char ** argv,const notmuch_opt_desc_t * options,int opt_index)288 parse_arguments (int argc, char **argv,
289 		 const notmuch_opt_desc_t *options, int opt_index)
290 {
291 
292     int pos_arg_index = 0;
293     bool more_args = true;
294 
295     while (more_args && opt_index < argc) {
296 	if (strncmp (argv[opt_index], "--", 2) != 0) {
297 
298 	    more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
299 
300 	    if (more_args) {
301 		pos_arg_index++;
302 		opt_index++;
303 	    }
304 
305 	} else {
306 	    int prev_opt_index = opt_index;
307 
308 	    if (strlen (argv[opt_index]) == 2)
309 		return opt_index + 1;
310 
311 	    opt_index = parse_option (argc, argv, options, opt_index);
312 	    if (opt_index < 0) {
313 		fprintf (stderr, "Unrecognized option: %s\n", argv[prev_opt_index]);
314 		more_args = false;
315 	    }
316 	}
317     }
318 
319     return opt_index;
320 }
321