+ notmuch_messages_destroy (messages);
+
+ if (first_message)
+ fputs (format->results_null, stdout);
+ else
+ fputs (format->results_end, stdout);
+
+ return 0;
+}
+
+static int
+do_search_tags (notmuch_database_t *notmuch,
+ const search_format_t *format,
+ notmuch_query_t *query)
+{
+ notmuch_messages_t *messages = NULL;
+ notmuch_tags_t *tags;
+ const char *tag;
+ int first_tag = 1;
+
+ /* Special-case query of "*" for better performance. */
+ if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
+ tags = notmuch_database_get_all_tags (notmuch);
+ } else {
+ messages = notmuch_query_search_messages (query);
+ if (messages == NULL)
+ return 1;
+
+ tags = notmuch_messages_collect_tags (messages);
+ }
+ if (tags == NULL)
+ return 1;
+
+ fputs (format->results_start, stdout);
+
+ for (;
+ notmuch_tags_valid (tags);
+ notmuch_tags_move_to_next (tags))
+ {
+ tag = notmuch_tags_get (tags);
+
+ if (! first_tag)
+ fputs (format->item_sep, stdout);
+
+ format->item_id (tags, "", tag);
+
+ first_tag = 0;
+ }
+
+ notmuch_tags_destroy (tags);
+
+ if (messages)
+ notmuch_messages_destroy (messages);
+
+ if (first_tag)
+ fputs (format->results_null, stdout);
+ else
+ fputs (format->results_end, stdout);
+
+ return 0;
+}
+
+int
+notmuch_search_command (void *ctx, int argc, char *argv[])
+{
+ notmuch_config_t *config;
+ notmuch_database_t *notmuch;
+ notmuch_query_t *query;
+ char *query_str;
+ notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
+ const search_format_t *format = &format_text;
+ int opt_index, ret;
+ output_t output = OUTPUT_SUMMARY;
+ int offset = 0;
+ int limit = -1; /* unlimited */
+ const char **search_exclude_tags;
+ size_t search_exclude_tags_length;
+ unsigned int i;
+
+ enum { NOTMUCH_FORMAT_JSON, NOTMUCH_FORMAT_TEXT }
+ format_sel = NOTMUCH_FORMAT_TEXT;
+
+ notmuch_opt_desc_t options[] = {
+ { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',
+ (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
+ { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
+ (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
+ { "text", NOTMUCH_FORMAT_TEXT },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
+ (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
+ { "threads", OUTPUT_THREADS },
+ { "messages", OUTPUT_MESSAGES },
+ { "files", OUTPUT_FILES },
+ { "tags", OUTPUT_TAGS },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },
+ { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0 },
+ { 0, 0, 0, 0, 0 }
+ };
+
+ opt_index = parse_arguments (argc, argv, options, 1);
+
+ if (opt_index < 0) {
+ return 1;
+ }
+
+ switch (format_sel) {
+ case NOTMUCH_FORMAT_TEXT:
+ format = &format_text;
+ break;
+ case NOTMUCH_FORMAT_JSON:
+ format = &format_json;
+ break;
+ }