+ 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,
+ .query_syntax = NOTMUCH_QUERY_SYNTAX_XAPIAN,
+ .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_database_t *notmuch, 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->notmuch = notmuch;
+ ctx->output = OUTPUT_SUMMARY;
+ opt_index = parse_arguments (argc, argv, options, 1);
+ if (opt_index < 0)
+ return EXIT_FAILURE;
+
+ notmuch_process_shared_options (notmuch, 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;
+ }
+
+ if (_notmuch_search_prepare (ctx, argc - opt_index, argv + opt_index))
+ return EXIT_FAILURE;
+
+ switch (ctx->output) {
+ case OUTPUT_SUMMARY:
+ case OUTPUT_THREADS:
+ ret = do_search_threads (ctx);
+ break;
+ case OUTPUT_MESSAGES:
+ case OUTPUT_FILES:
+ ret = do_search_messages (ctx);
+ break;
+ case OUTPUT_TAGS:
+ ret = do_search_tags (ctx);
+ break;
+ default:
+ INTERNAL_ERROR ("Unexpected output");
+ }
+
+ _notmuch_search_cleanup (ctx);
+
+ return ret ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
+int
+notmuch_address_command (notmuch_database_t *notmuch, int argc, char *argv[])
+{
+ search_context_t *ctx = &search_context;
+ int opt_index, ret;
+
+ notmuch_opt_desc_t options[] = {
+ { .opt_flags = &ctx->output, .name = "output", .keywords =
+ (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
+ { "recipients", OUTPUT_RECIPIENTS },
+ { "count", OUTPUT_COUNT },
+ { "address", OUTPUT_ADDRESS },
+ { 0, 0 } } },
+ { .opt_keyword = &ctx->exclude, .name = "exclude", .keywords =
+ (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
+ { "false", NOTMUCH_EXCLUDE_FALSE },
+ { 0, 0 } } },
+ { .opt_keyword = &ctx->dedup, .name = "deduplicate", .keywords =
+ (notmuch_keyword_t []){ { "no", DEDUP_NONE },
+ { "mailbox", DEDUP_MAILBOX },
+ { "address", DEDUP_ADDRESS },
+ { 0, 0 } } },
+ { .opt_inherit = common_options },
+ { .opt_inherit = notmuch_shared_options },
+ { }
+ };
+
+ ctx->notmuch = notmuch;
+
+ opt_index = parse_arguments (argc, argv, options, 1);
+ if (opt_index < 0)
+ return EXIT_FAILURE;
+
+ notmuch_process_shared_options (notmuch, argv[0]);
+
+ if (! (ctx->output & (OUTPUT_SENDER | OUTPUT_RECIPIENTS)))
+ ctx->output |= OUTPUT_SENDER;
+
+ if (ctx->output & OUTPUT_COUNT && ctx->dedup == DEDUP_NONE) {
+ fprintf (stderr, "--output=count is not applicable with --deduplicate=no\n");
+ return EXIT_FAILURE;