+ notmuch_tags_destroy (tags);
+
+ if (messages)
+ notmuch_messages_destroy (messages);
+
+ format->end (format);
+
+ return 0;
+}
+
+enum {
+ EXCLUDE_TRUE,
+ EXCLUDE_FALSE,
+ EXCLUDE_FLAG,
+};
+
+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;
+ sprinter_t *format = NULL;
+ int opt_index, ret;
+ output_t output = OUTPUT_SUMMARY;
+ int offset = 0;
+ int limit = -1; /* unlimited */
+ int exclude = EXCLUDE_TRUE;
+ 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_KEYWORD, &exclude, "exclude", 'x',
+ (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
+ { "false", EXCLUDE_FALSE },
+ { "flag", EXCLUDE_FLAG },
+ { 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 = sprinter_text_create (ctx, stdout);
+ break;
+ case NOTMUCH_FORMAT_JSON:
+ format = sprinter_json_create (ctx, stdout);
+ break;
+ default:
+ /* this should never happen */
+ INTERNAL_ERROR("no output format selected");
+ }