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, const char *arg_str) {
16 const notmuch_keyword_t *keywords;
19 /* No keyword given */
23 for (keywords = arg_desc->keywords; keywords->name; keywords++) {
24 if (strcmp (arg_str, keywords->name) != 0)
27 if (arg_desc->opt_flags)
28 *arg_desc->opt_flags |= keywords->value;
30 *arg_desc->opt_keyword = keywords->value;
35 fprintf (stderr, "Unknown keyword argument \"%s\" for option \"%s\".\n", arg_str, arg_desc->name);
37 fprintf (stderr, "Option \"%s\" needs a keyword argument.\n", arg_desc->name);
42 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
45 if (next == '\0' || strcmp (arg_str, "true") == 0) {
47 } else if (strcmp (arg_str, "false") == 0) {
50 fprintf (stderr, "Unknown argument \"%s\" for (boolean) option \"%s\".\n", arg_str, arg_desc->name);
54 *arg_desc->opt_bool = value;
60 _process_int_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
63 if (next == '\0' || arg_str[0] == '\0') {
64 fprintf (stderr, "Option \"%s\" needs an integer argument.\n", arg_desc->name);
68 *arg_desc->opt_int = strtol (arg_str, &endptr, 10);
72 fprintf (stderr, "Unable to parse argument \"%s\" for option \"%s\" as an integer.\n",
73 arg_str, arg_desc->name);
78 _process_string_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
81 fprintf (stderr, "Option \"%s\" needs a string argument.\n", arg_desc->name);
84 if (arg_str[0] == '\0') {
85 fprintf (stderr, "String argument for option \"%s\" must be non-empty.\n", arg_desc->name);
88 *arg_desc->opt_string = arg_str;
92 /* Return number of non-NULL opt_* fields in opt_desc. */
93 static int _opt_set_count (const notmuch_opt_desc_t *opt_desc)
96 !!opt_desc->opt_inherit +
97 !!opt_desc->opt_bool +
99 !!opt_desc->opt_keyword +
100 !!opt_desc->opt_flags +
101 !!opt_desc->opt_string +
102 !!opt_desc->opt_position;
105 /* Return TRUE if opt_desc is valid. */
106 static notmuch_bool_t _opt_valid (const notmuch_opt_desc_t *opt_desc)
108 int n = _opt_set_count (opt_desc);
111 INTERNAL_ERROR ("more than one non-NULL opt_* field for argument \"%s\"",
118 Search for the {pos_arg_index}th position argument, return FALSE if
123 parse_position_arg (const char *arg_str, int pos_arg_index,
124 const notmuch_opt_desc_t *arg_desc) {
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;
143 * Search for a non-positional (i.e. starting with --) argument matching arg,
144 * parse a possible value, and assign to *output_var
148 parse_option (int argc, char **argv, const notmuch_opt_desc_t *options, int opt_index)
152 const char *_arg = argv[opt_index];
157 const char *arg = _arg + 2; /* _arg starts with -- */
158 const notmuch_opt_desc_t *try;
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];
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);
174 if (strncmp (arg, try->name, strlen (try->name)) != 0)
177 char next = arg[strlen (try->name)];
178 const char *value = arg + strlen(try->name) + 1;
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
186 if (next != '=' && next != ':' && next != '\0')
189 if (next == '\0' && next_arg != NULL && ! try->opt_bool) {
195 notmuch_bool_t 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);
205 INTERNAL_ERROR ("unknown or unhandled option \"%s\"", try->name);
211 *try->present = TRUE;
218 /* See command-line-arguments.h for description */
220 parse_arguments (int argc, char **argv,
221 const notmuch_opt_desc_t *options, int opt_index) {
223 int pos_arg_index = 0;
224 notmuch_bool_t more_args = TRUE;
226 while (more_args && opt_index < argc) {
227 if (strncmp (argv[opt_index],"--",2) != 0) {
229 more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
237 int prev_opt_index = opt_index;
239 if (strlen (argv[opt_index]) == 2)
242 opt_index = parse_option (argc, argv, options, opt_index);
244 fprintf (stderr, "Unrecognized option: %s\n", argv[prev_opt_index]);