+ return interrupted;
+}
+
+int
+notmuch_tag_command (void *ctx, int argc, char *argv[])
+{
+ tag_operation_t *tag_ops;
+ int tag_ops_count = 0;
+ char *query_string;
+ notmuch_config_t *config;
+ notmuch_database_t *notmuch;
+ struct sigaction action;
+ notmuch_bool_t synchronize_flags;
+ int i;
+ int ret;
+
+ /* Setup our handler for SIGINT */
+ memset (&action, 0, sizeof (struct sigaction));
+ action.sa_handler = handle_sigint;
+ sigemptyset (&action.sa_mask);
+ action.sa_flags = SA_RESTART;
+ sigaction (SIGINT, &action, NULL);
+
+ argc--; argv++; /* skip subcommand argument */
+
+ /* Array of tagging operations (add or remove), terminated with an
+ * empty element. */
+ tag_ops = talloc_array (ctx, tag_operation_t, argc + 1);
+ if (tag_ops == NULL) {
+ fprintf (stderr, "Out of memory.\n");
+ return 1;
+ }
+
+ for (i = 0; i < argc; i++) {
+ if (strcmp (argv[i], "--") == 0) {
+ i++;
+ break;
+ }
+ if (argv[i][0] == '+' || argv[i][0] == '-') {
+ tag_ops[tag_ops_count].tag = argv[i] + 1;
+ tag_ops[tag_ops_count].remove = (argv[i][0] == '-');
+ tag_ops_count++;
+ } else {
+ break;
+ }
+ }
+
+ tag_ops[tag_ops_count].tag = NULL;
+
+ if (tag_ops_count == 0) {
+ fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
+ return 1;
+ }
+
+ query_string = query_string_from_args (ctx, argc - i, &argv[i]);
+
+ if (*query_string == '\0') {
+ fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
+ return 1;
+ }
+
+ config = notmuch_config_open (ctx, NULL, NULL);
+ if (config == NULL)
+ return 1;
+
+ if (notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
+ return 1;
+
+ synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
+
+ ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);