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;
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);
58 "No matching keyword for option \"%s\" and default value \"%s\" is invalid.\n",
65 fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str,
68 fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
73 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next,
74 const char *arg_str, bool negate)
78 if (next == '\0' || strcmp (arg_str, "true") == 0) {
80 } else if (strcmp (arg_str, "false") == 0) {
83 fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str,
88 *arg_desc->opt_bool = negate ? (! value) : value;
94 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str)
99 if (next == '\0' || arg_str[0] == '\0') {
100 fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
104 *arg_desc->opt_int = strtol (arg_str, &endptr, 10);
108 fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
109 arg_str, arg_desc->name);
114 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str)
118 fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
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);
125 *arg_desc->opt_string = arg_str;
129 /* Return number of non-NULL opt_* fields in opt_desc. */
131 _opt_set_count (const notmuch_opt_desc_t *opt_desc)
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;
143 /* Return true if opt_desc is valid. */
145 _opt_valid (const notmuch_opt_desc_t *opt_desc)
147 int n = _opt_set_count (opt_desc);
150 INTERNAL_ERROR ("more than one non-NULL opt_* field for argument \"%s\"",
157 * Search for the {pos_arg_index}th position argument, return false if
158 * that does not exist.
162 parse_position_arg (const char *arg_str, int pos_arg_index,
163 const notmuch_opt_desc_t *arg_desc)
166 int pos_arg_counter = 0;
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;
183 #define NEGATIVE_PREFIX "no-"
186 * Search for a non-positional (i.e. starting with --) argument matching arg,
187 * parse a possible value, and assign to *output_var
191 parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_index)
195 const char *_arg = argv[opt_index];
200 const char *arg = _arg + 2; /* _arg starts with -- */
201 const char *negative_arg = NULL;
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);
209 const notmuch_opt_desc_t *try;
211 const char *next_arg = NULL;
213 if (opt_index < argc - 1 && strncmp (argv[opt_index + 1], "--", 2) != 0)
214 next_arg = argv[opt_index + 1];
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);
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. */
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
249 if (next != '=' && next != ':' && next != '\0')
252 bool lookahead = (next == '\0' && next_arg != NULL && ! try->opt_bool);
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);
270 INTERNAL_ERROR ("unknown or unhandled option \"%s\"", try->name);
272 if (opt_status == OPT_FAILED)
275 if (lookahead && opt_status == OPT_GIVEBACK)
279 *try->present = true;
281 return opt_index + 1;
286 /* See command-line-arguments.h for description */
288 parse_arguments (int argc, char **argv,
289 const notmuch_opt_desc_t *options, int opt_index)
292 int pos_arg_index = 0;
293 bool more_args = true;
295 while (more_args && opt_index < argc) {
296 if (strncmp (argv[opt_index], "--", 2) != 0) {
298 more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
306 int prev_opt_index = opt_index;
308 if (strlen (argv[opt_index]) == 2)
309 return opt_index + 1;
311 opt_index = parse_option (argc, argv, options, opt_index);
313 fprintf (stderr, "Unrecognized option: %s\n", argv[prev_opt_index]);