4 #include "error_util.h"
5 #include "command-line-arguments.h"
8 Search the array of keywords for a given argument, assigning the
9 output variable to the corresponding value. Return false if nothing
14 _process_keyword_arg (const notmuch_opt_desc_t *arg_desc, char next,
15 const char *arg_str, bool negate)
17 const notmuch_keyword_t *keywords;
20 /* No keyword given */
24 for (keywords = arg_desc->keywords; keywords->name; keywords++) {
25 if (strcmp (arg_str, keywords->name) != 0)
28 if (arg_desc->opt_flags && negate)
29 *arg_desc->opt_flags &= ~keywords->value;
30 else if (arg_desc->opt_flags)
31 *arg_desc->opt_flags |= keywords->value;
33 *arg_desc->opt_keyword = keywords->value;
38 fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);
40 fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
45 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next,
46 const char *arg_str, bool negate)
50 if (next == '\0' || strcmp (arg_str, "true") == 0) {
52 } else if (strcmp (arg_str, "false") == 0) {
55 fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
59 *arg_desc->opt_bool = negate ? !value : value;
65 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
68 if (next == '\0' || arg_str[0] == '\0') {
69 fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
73 *arg_desc->opt_int = strtol (arg_str, &endptr, 10);
77 fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
78 arg_str, arg_desc->name);
83 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
86 fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
89 if (arg_str[0] == '\0' && ! arg_desc->allow_empty) {
90 fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
93 *arg_desc->opt_string = arg_str;
97 /* Return number of non-NULL opt_* fields in opt_desc. */
98 static int _opt_set_count (const notmuch_opt_desc_t *opt_desc)
101 !!opt_desc->opt_inherit +
102 !!opt_desc->opt_bool +
103 !!opt_desc->opt_int +
104 !!opt_desc->opt_keyword +
105 !!opt_desc->opt_flags +
106 !!opt_desc->opt_string +
107 !!opt_desc->opt_position;
110 /* Return true if opt_desc is valid. */
111 static bool _opt_valid (const notmuch_opt_desc_t *opt_desc)
113 int n = _opt_set_count (opt_desc);
116 INTERNAL_ERROR ("more than one non-NULL opt_* field for argument \"%s\"",
123 Search for the {pos_arg_index}th position argument, return false if
128 parse_position_arg (const char *arg_str, int pos_arg_index,
129 const notmuch_opt_desc_t *arg_desc) {
131 int pos_arg_counter = 0;
132 while (_opt_valid (arg_desc)) {
133 if (arg_desc->opt_position) {
134 if (pos_arg_counter == pos_arg_index) {
135 *arg_desc->opt_position = arg_str;
136 if (arg_desc->present)
137 *arg_desc->present = true;
147 #define NEGATIVE_PREFIX "no-"
150 * Search for a non-positional (i.e. starting with --) argument matching arg,
151 * parse a possible value, and assign to *output_var
155 parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_index)
159 const char *_arg = argv[opt_index];
164 const char *arg = _arg + 2; /* _arg starts with -- */
165 const char *negative_arg = NULL;
167 /* See if this is a --no-argument */
168 if (strlen (arg) > strlen (NEGATIVE_PREFIX) &&
169 strncmp (arg, NEGATIVE_PREFIX, strlen (NEGATIVE_PREFIX)) == 0) {
170 negative_arg = arg + strlen (NEGATIVE_PREFIX);
173 const notmuch_opt_desc_t *try;
175 const char *next_arg = NULL;
176 if (opt_index < argc - 1 && strncmp (argv[opt_index + 1], "--", 2) != 0)
177 next_arg = argv[opt_index + 1];
179 for (try = options; _opt_valid (try); try++) {
180 if (try->opt_inherit) {
181 int new_index = parse_option (argc, argv, try->opt_inherit, opt_index);
193 if (strncmp (arg, try->name, strlen (try->name)) == 0) {
194 next = arg[strlen (try->name)];
195 value = arg + strlen (try->name) + 1;
196 } else if (negative_arg && (try->opt_bool || try->opt_flags) &&
197 strncmp (negative_arg, try->name, strlen (try->name)) == 0) {
198 next = negative_arg[strlen (try->name)];
199 value = negative_arg + strlen (try->name) + 1;
200 /* The argument part of --no-argument matches, negate the result. */
207 * If we have not reached the end of the argument (i.e. the
208 * next character is not a space or delimiter) then the
209 * argument could still match a longer option name later in
212 if (next != '=' && next != ':' && next != '\0')
215 if (next == '\0' && next_arg != NULL && ! try->opt_bool) {
221 bool opt_status = false;
222 if (try->opt_keyword || try->opt_flags)
223 opt_status = _process_keyword_arg (try, next, value, negate);
224 else if (try->opt_bool)
225 opt_status = _process_boolean_arg (try, next, value, negate);
226 else if (try->opt_int)
227 opt_status = _process_int_arg (try, next, value);
228 else if (try->opt_string)
229 opt_status = _process_string_arg (try, next, value);
231 INTERNAL_ERROR ("unknown or unhandled option \"%s\"", try->name);
237 *try->present = true;
244 /* See command-line-arguments.h for description */
246 parse_arguments (int argc, char **argv,
247 const notmuch_opt_desc_t *options, int opt_index) {
249 int pos_arg_index = 0;
250 bool more_args = true;
252 while (more_args && opt_index < argc) {
253 if (strncmp (argv[opt_index],"--",2) != 0) {
255 more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
263 int prev_opt_index = opt_index;
265 if (strlen (argv[opt_index]) == 2)
268 opt_index = parse_option (argc, argv, options, opt_index);
270 fprintf (stderr, "Unrecognized option: %s\n", argv[prev_opt_index]);