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 = arg_desc->keywords;
19 /* No keyword given */
23 while (keywords->name) {
24 if (strcmp (arg_str, keywords->name) == 0) {
25 if (arg_desc->output_var) {
26 *((int *)arg_desc->output_var) = keywords->value;
33 fprintf (stderr, "unknown keyword: %s\n", arg_str);
35 fprintf (stderr, "option %s needs a keyword\n", arg_desc->name);
40 _process_boolean_arg (const notmuch_opt_desc_t *arg_desc, char next, const char *arg_str) {
43 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
46 if (strcmp (arg_str, "false") == 0) {
47 *((notmuch_bool_t *)arg_desc->output_var) = FALSE;
50 if (strcmp (arg_str, "true") == 0) {
51 *((notmuch_bool_t *)arg_desc->output_var) = TRUE;
58 Search for the {pos_arg_index}th position argument, return FALSE if
63 parse_position_arg (const char *arg_str, int pos_arg_index,
64 const notmuch_opt_desc_t *arg_desc) {
66 int pos_arg_counter = 0;
67 while (arg_desc->opt_type != NOTMUCH_OPT_END){
68 if (arg_desc->opt_type == NOTMUCH_OPT_POSITION) {
69 if (pos_arg_counter == pos_arg_index) {
70 if (arg_desc->output_var) {
71 *((const char **)arg_desc->output_var) = arg_str;
83 * Search for a non-positional (i.e. starting with --) argument matching arg,
84 * parse a possible value, and assign to *output_var
88 parse_option (const char *arg,
89 const notmuch_opt_desc_t *options) {
96 const notmuch_opt_desc_t *try = options;
97 while (try->opt_type != NOTMUCH_OPT_END) {
98 if (try->name && strncmp (arg, try->name, strlen (try->name)) == 0) {
99 char next = arg[strlen (try->name)];
100 const char *value= arg+strlen(try->name)+1;
104 /* Everything but boolean arguments (switches) needs a
105 * delimiter, and a non-zero length value. Boolean
106 * arguments may take an optional =true or =false value.
108 if (next != '=' && next != ':' && next != 0) return FALSE;
110 if (try->opt_type != NOTMUCH_OPT_BOOLEAN &&
111 try->opt_type != NOTMUCH_OPT_KEYWORD)
114 if (value[0] == 0) return FALSE;
117 if (try->output_var == NULL)
118 INTERNAL_ERROR ("output pointer NULL for option %s", try->name);
120 switch (try->opt_type) {
121 case NOTMUCH_OPT_KEYWORD:
122 return _process_keyword_arg (try, next, value);
124 case NOTMUCH_OPT_BOOLEAN:
125 return _process_boolean_arg (try, next, value);
127 case NOTMUCH_OPT_INT:
128 *((int *)try->output_var) = strtol (value, &endptr, 10);
129 return (*endptr == 0);
131 case NOTMUCH_OPT_STRING:
132 *((const char **)try->output_var) = value;
135 case NOTMUCH_OPT_POSITION:
136 case NOTMUCH_OPT_END:
138 INTERNAL_ERROR ("unknown or unhandled option type %d", try->opt_type);
144 fprintf (stderr, "Unrecognized option: --%s\n", arg);
148 /* See command-line-arguments.h for description */
150 parse_arguments (int argc, char **argv,
151 const notmuch_opt_desc_t *options, int opt_index) {
153 int pos_arg_index = 0;
154 notmuch_bool_t more_args = TRUE;
156 while (more_args && opt_index < argc) {
157 if (strncmp (argv[opt_index],"--",2) != 0) {
159 more_args = parse_position_arg (argv[opt_index], pos_arg_index, options);
168 if (strlen (argv[opt_index]) == 2)
171 more_args = parse_option (argv[opt_index], options);