On Sun, 25 Nov 2012, Peter Wang <novalazy@gmail.com> wrote: > Factor out parsing of +tag, -tag operations from argv > into a separate function. > --- > notmuch-tag.c | 66 +++++++++++++++++++++++++++++++++++++---------------------- > 1 file changed, 41 insertions(+), 25 deletions(-) > > diff --git a/notmuch-tag.c b/notmuch-tag.c > index 88d559b..35a76db 100644 > --- a/notmuch-tag.c > +++ b/notmuch-tag.c > @@ -167,11 +167,48 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string, > return interrupted; > } > > +/* Parse +tag and -tag operations between argv[i] and argv[argc-1]. > + * The array tag_ops must be at least argc - i elements long. > + * Returns the index into argv where parsing stopped, or -1 on error. */ Perhaps mention tag_ops_count (as the number of tags parsed or something)? > +static int > +parse_tag_operations (int i, int argc, char *argv[], > + tag_operation_t *tag_ops, int *tag_ops_count) > +{ > + *tag_ops_count = 0; > + > + for (; i < argc; i++) { > + if (strcmp (argv[i], "--") == 0) { > + i++; > + break; > + } > + if (argv[i][0] == '+' || argv[i][0] == '-') { > + if (argv[i][0] == '+' && argv[i][1] == '\0') { > + fprintf (stderr, "Error: tag names cannot be empty.\n"); > + return -1; > + } > + if (argv[i][0] == '+' && argv[i][1] == '-') { > + /* This disallows adding the non-removable tag "-" and > + * enables notmuch tag to take long options in the > + * future. */ > + fprintf (stderr, "Error: tag names must not start with '-'.\n"); > + return -1; > + } > + tag_ops[*tag_ops_count].tag = argv[i] + 1; > + tag_ops[*tag_ops_count].remove = (argv[i][0] == '-'); > + (*tag_ops_count)++; > + } else { > + break; > + } > + } > + > + return i; > +} > + > int > notmuch_tag_command (void *ctx, int argc, char *argv[]) > { > tag_operation_t *tag_ops; > - int tag_ops_count = 0; > + int tag_ops_count; > char *query_string; > notmuch_config_t *config; > notmuch_database_t *notmuch; > @@ -197,30 +234,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[]) > return 1; > } > > - for (i = 0; i < argc; i++) { > - if (strcmp (argv[i], "--") == 0) { > - i++; > - break; > - } > - if (argv[i][0] == '+' || argv[i][0] == '-') { > - if (argv[i][0] == '+' && argv[i][1] == '\0') { > - fprintf (stderr, "Error: tag names cannot be empty.\n"); > - return 1; > - } > - if (argv[i][0] == '+' && argv[i][1] == '-') { > - /* This disallows adding the non-removable tag "-" and > - * enables notmuch tag to take long options in the > - * future. */ > - fprintf (stderr, "Error: tag names must not start with '-'.\n"); > - return 1; > - } > - tag_ops[tag_ops_count].tag = argv[i] + 1; > - tag_ops[tag_ops_count].remove = (argv[i][0] == '-'); > - tag_ops_count++; > - } else { > - break; > - } > - } > + i = parse_tag_operations (0, argc, argv, tag_ops, &tag_ops_count); > + if (i < 0) > + return 1; > > tag_ops[tag_ops_count].tag = NULL; > > -- > 1.7.12.1 > > _______________________________________________ > notmuch mailing list > notmuch@notmuchmail.org > http://notmuchmail.org/mailman/listinfo/notmuch