4 #include "error_util.h"
5 #include "command-line-arguments.h"
8 OPT_FAILED, /* false */
10 OPT_GIVEBACK, /* pop one of the arguments you thought you were getting off the stack */
14 * Search the array of keywords for a given argument, assigning the
15 * output variable to the corresponding value. Return false if nothing
20 _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next,
21 const char *arg_str, bool negate)
23 const notmuch_keyword_t *keywords;
26 /* No keyword given */
30 for (keywords = arg_desc->keywords; keywords->name; keywords++) {
31 if (strcmp (arg_str, keywords->name) != 0)
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;
39 *arg_desc->opt_keyword = keywords->value;
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)
49 *arg_desc->opt_keyword = keywords->value;
50 fprintf (stderr, "Warning: No known keyword option given for \"%s\", choosing value \"%s\"."
51 " Please specify the argument explicitly!\n", arg_desc->name, arg_desc->keyword_no_arg_value);
55 fprintf (stderr, "No matching keyword for option \"%s\" and default value \"%s\" is invalid.\n", arg_str, arg_desc->name);
60 fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);
62 fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
67 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next,
68 const char *arg_str, bool negate)
72 if (next == '\0' || strcmp (arg_str, "true") == 0) {
74 } else if (strcmp (arg_str, "false") == 0) {
77 fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
81 *arg_desc->opt_bool = negate ? (! value) : value;
87 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str)
92 if (next == '\0' || arg_str[0] == '\0') {
93 fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
97 *arg_desc->opt_int = strtol (arg_str, &endptr, 10);
101 fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
102 arg_str, arg_desc->name);
107 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str)
111 fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
114 if (arg_str[0] == '\0' && ! arg_desc->allow_empty) {
115 fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
118 *arg_desc->opt_string = arg_str;
122 /* Return number of non-NULL opt_* fields in opt_desc. */
124 _opt_set_count (const notmuch_opt_desc_t *opt_desc)
127 (bool) opt_desc->opt_inherit +
128 (bool) opt_desc->opt_bool +
129 (bool) opt_desc->opt_int +
130 (bool) opt_desc->opt_keyword +
131 (bool) opt_desc->opt_flags +
132 (bool) opt_desc->opt_string +
133 (bool) opt_desc->opt_position;
136 /* Return true if opt_desc is valid. */
138 _opt_valid (const notmuch_opt_desc_t *opt_desc)
140 int n = _opt_set_count (opt_desc);
143 INTERNAL_ERROR ("more than one non-NULL opt_* field for argument \"%s\"",
150 * Search for the {pos_arg_index}th position argument, return false if
151 * that does not exist.
155 parse_position_arg (const char *arg_str, int pos_arg_index,
156 const notmuch_opt_desc_t *arg_desc)
159 int pos_arg_counter = 0;
161 while (_opt_valid (arg_desc)) {
162 if (arg_desc->opt_position) {
163 if (pos_arg_counter == pos_arg_index) {
164 *arg_desc->opt_position = arg_str;
165 if (arg_desc->present)
166 *arg_desc->present = true;
176 #define NEGATIVE_PREFIX "no-"
179 * Search for a non-positional (i.e. starting with --) argument matching arg,
180 * parse a possible value, and assign to *output_var
184 parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_index)
188 const char *_arg = argv[opt_index];
193 const char *arg = _arg + 2; /* _arg starts with -- */
194 const char *negative_arg = NULL;
196 /* See if this is a --no-argument */
197 if (strlen (arg) > strlen (NEGATIVE_PREFIX) &&
198 strncmp (arg, NEGATIVE_PREFIX, strlen (NEGATIVE_PREFIX)) == 0) {
199 negative_arg = arg + strlen (NEGATIVE_PREFIX);
202 const notmuch_opt_desc_t *try;
204 const char *next_arg = NULL;
205 if (opt_index < argc - 1 && strncmp (argv[opt_index + 1], "--", 2) != 0)
206 next_arg = argv[opt_index + 1];
208 for (try = options; _opt_valid (try); try++) {
209 if (try->opt_inherit) {
210 int new_index = parse_option (argc, argv, try->opt_inherit, opt_index);
222 if (strncmp (arg, try->name, strlen (try->name)) == 0) {
223 next = arg[strlen (try->name)];
224 value = arg + strlen (try->name) + 1;
225 } else if (negative_arg && (try->opt_bool || try->opt_flags) &&
226 strncmp (negative_arg, try->name, strlen (try->name)) == 0) {
227 next = negative_arg[strlen (try->name)];
228 value = negative_arg + strlen (try->name) + 1;
229 /* The argument part of --no-argument matches, negate the result. */
236 * If we have not reached the end of the argument (i.e. the
237 * next character is not a space or delimiter) then the
238 * argument could still match a longer option name later in
241 if (next != '=' && next != ':' && next != '\0')
244 bool lookahead = (next == '\0' && next_arg != NULL && ! try->opt_bool);
252 opt_handled opt_status = OPT_FAILED;
253 if (try->opt_keyword || try->opt_flags)
254 opt_status = _process_keyword_arg (try, next, value, negate);
255 else if (try->opt_bool)
256 opt_status = _process_boolean_arg (try, next, value, negate);
257 else if (try->opt_int)
258 opt_status = _process_int_arg (try, next, value);
259 else if (try->opt_string)
260 opt_status = _process_string_arg (try, next, value);
262 INTERNAL_ERROR ("unknown or unhandled option \"%s\"", try->name);
264 if (opt_status == OPT_FAILED)
267 if (lookahead && opt_status == OPT_GIVEBACK)
271 *try->present = true;
273 return opt_index + 1;
278 /* See command-line-arguments.h for description */
280 parse_arguments (int argc, char **argv,
281 const notmuch_opt_desc_t *options, int opt_index)
284 int pos_arg_index = 0;
285 bool more_args = true;
287 while (more_args && opt_index < argc) {
288 if (strncmp (argv[opt_index], "--", 2) != 0) {
290 more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
298 int prev_opt_index = opt_index;
300 if (strlen (argv[opt_index]) == 2)
301 return opt_index + 1;
303 opt_index = parse_option (argc, argv, options, opt_index);
305 fprintf (stderr, "Unrecognized option: %s\n", argv[prev_opt_index]);