+ return 0;
+}
+
+static void
+_notmuch_search_cleanup (search_context_t *ctx)
+{
+ notmuch_query_destroy (ctx->query);
+ notmuch_database_destroy (ctx->notmuch);
+
+ talloc_free (ctx->format);
+}
+
+static search_context_t search_context = {
+ .format_sel = NOTMUCH_FORMAT_TEXT,
+ .exclude = NOTMUCH_EXCLUDE_TRUE,
+ .sort = NOTMUCH_SORT_NEWEST_FIRST,
+ .output = 0,
+ .offset = 0,
+ .limit = -1, /* unlimited */
+ .dupe = -1,
+ .dedup = DEDUP_MAILBOX,
+};
+
+static const notmuch_opt_desc_t common_options[] = {
+ { .opt_keyword = &search_context.sort, .name = "sort", .keywords =
+ (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
+ { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
+ { 0, 0 } } },
+ { .opt_keyword = &search_context.format_sel, .name = "format", .keywords =
+ (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
+ { "sexp", NOTMUCH_FORMAT_SEXP },
+ { "text", NOTMUCH_FORMAT_TEXT },
+ { "text0", NOTMUCH_FORMAT_TEXT0 },
+ { 0, 0 } } },
+ { .opt_int = ¬much_format_version, .name = "format-version" },
+ { }
+};
+
+int
+notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
+{
+ search_context_t *ctx = &search_context;
+ int opt_index, ret;
+
+ notmuch_opt_desc_t options[] = {
+ { .opt_keyword = &ctx->output, .name = "output", .keywords =
+ (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
+ { "threads", OUTPUT_THREADS },
+ { "messages", OUTPUT_MESSAGES },
+ { "files", OUTPUT_FILES },
+ { "tags", OUTPUT_TAGS },
+ { 0, 0 } } },
+ { .opt_keyword = &ctx->exclude, .name = "exclude", .keywords =
+ (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
+ { "false", NOTMUCH_EXCLUDE_FALSE },
+ { "flag", NOTMUCH_EXCLUDE_FLAG },
+ { "all", NOTMUCH_EXCLUDE_ALL },
+ { 0, 0 } } },
+ { .opt_int = &ctx->offset, .name = "offset" },
+ { .opt_int = &ctx->limit, .name = "limit" },
+ { .opt_int = &ctx->dupe, .name = "duplicate" },
+ { .opt_inherit = common_options },
+ { .opt_inherit = notmuch_shared_options },
+ { }
+ };
+
+ ctx->output = OUTPUT_SUMMARY;
+ opt_index = parse_arguments (argc, argv, options, 1);
+ if (opt_index < 0)
+ return EXIT_FAILURE;
+
+ notmuch_process_shared_options (argv[0]);
+
+ if (ctx->output != OUTPUT_FILES && ctx->output != OUTPUT_MESSAGES &&
+ ctx->dupe != -1) {
+ fprintf (stderr, "Error: --duplicate=N is only supported with --output=files and --output=messages.\n");
+ return EXIT_FAILURE;