+
+ }
+
+ sp->end (sp);
+
+ return res != NOTMUCH_STATUS_SUCCESS;
+}
+
+enum {
+ NOTMUCH_FORMAT_NOT_SPECIFIED,
+ NOTMUCH_FORMAT_JSON,
+ NOTMUCH_FORMAT_SEXP,
+ NOTMUCH_FORMAT_TEXT,
+ NOTMUCH_FORMAT_MBOX,
+ NOTMUCH_FORMAT_RAW
+};
+
+static const notmuch_show_format_t format_json = {
+ .new_sprinter = sprinter_json_create,
+ .part = format_part_sprinter_entry,
+};
+
+static const notmuch_show_format_t format_sexp = {
+ .new_sprinter = sprinter_sexp_create,
+ .part = format_part_sprinter_entry,
+};
+
+static const notmuch_show_format_t format_text = {
+ .new_sprinter = sprinter_text_create,
+ .part = format_part_text,
+};
+
+static const notmuch_show_format_t format_mbox = {
+ .new_sprinter = sprinter_text_create,
+ .part = format_part_mbox,
+};
+
+static const notmuch_show_format_t format_raw = {
+ .new_sprinter = sprinter_text_create,
+ .part = format_part_raw,
+};
+
+static const notmuch_show_format_t *formatters[] = {
+ [NOTMUCH_FORMAT_JSON] = &format_json,
+ [NOTMUCH_FORMAT_SEXP] = &format_sexp,
+ [NOTMUCH_FORMAT_TEXT] = &format_text,
+ [NOTMUCH_FORMAT_MBOX] = &format_mbox,
+ [NOTMUCH_FORMAT_RAW] = &format_raw,
+};
+
+int
+notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
+{
+ notmuch_database_t *notmuch;
+ notmuch_query_t *query;
+ char *query_string;
+ int opt_index, ret;
+ const notmuch_show_format_t *formatter;
+ sprinter_t *sprinter;
+ notmuch_show_params_t params = {
+ .part = -1,
+ .omit_excluded = true,
+ .output_body = true,
+ };
+ int format = NOTMUCH_FORMAT_NOT_SPECIFIED;
+ bool exclude = true;
+ bool entire_thread_set = false;
+ bool single_message;
+
+ notmuch_opt_desc_t options[] = {
+ { .opt_keyword = &format, .name = "format", .keywords =
+ (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
+ { "text", NOTMUCH_FORMAT_TEXT },
+ { "sexp", NOTMUCH_FORMAT_SEXP },
+ { "mbox", NOTMUCH_FORMAT_MBOX },
+ { "raw", NOTMUCH_FORMAT_RAW },
+ { 0, 0 } } },
+ { .opt_int = ¬much_format_version, .name = "format-version" },
+ { .opt_bool = &exclude, .name = "exclude" },
+ { .opt_bool = ¶ms.entire_thread, .name = "entire-thread",
+ .present = &entire_thread_set },
+ { .opt_int = ¶ms.part, .name = "part" },
+ { .opt_bool = ¶ms.crypto.decrypt, .name = "decrypt" },
+ { .opt_bool = ¶ms.crypto.verify, .name = "verify" },
+ { .opt_bool = ¶ms.output_body, .name = "body" },
+ { .opt_bool = ¶ms.include_html, .name = "include-html" },
+ { .opt_inherit = notmuch_shared_options },
+ { }
+ };
+
+ opt_index = parse_arguments (argc, argv, options, 1);
+ if (opt_index < 0)
+ return EXIT_FAILURE;
+
+ notmuch_process_shared_options (argv[0]);
+
+ /* decryption implies verification */
+ if (params.crypto.decrypt)
+ params.crypto.verify = true;
+
+ /* specifying a part implies single message display */
+ single_message = params.part >= 0;
+
+ if (format == NOTMUCH_FORMAT_NOT_SPECIFIED) {
+ /* if part was requested and format was not specified, use format=raw */
+ if (params.part >= 0)
+ format = NOTMUCH_FORMAT_RAW;
+ else
+ format = NOTMUCH_FORMAT_TEXT;
+ }
+
+ if (format == NOTMUCH_FORMAT_MBOX) {
+ if (params.part > 0) {
+ fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
+ return EXIT_FAILURE;
+ }
+ } else if (format == NOTMUCH_FORMAT_RAW) {
+ /* raw format only supports single message display */
+ single_message = true;
+ }
+
+ notmuch_exit_if_unsupported_format ();
+
+ /* Default is entire-thread = false except for format=json and
+ * format=sexp. */
+ if (! entire_thread_set &&
+ (format == NOTMUCH_FORMAT_JSON || format == NOTMUCH_FORMAT_SEXP))
+ params.entire_thread = true;
+
+ if (!params.output_body) {
+ if (params.part > 0) {
+ fprintf (stderr, "Warning: --body=false is incompatible with --part > 0. Disabling.\n");
+ params.output_body = true;
+ } else {
+ if (format != NOTMUCH_FORMAT_JSON && format != NOTMUCH_FORMAT_SEXP)
+ fprintf (stderr,
+ "Warning: --body=false only implemented for format=json and format=sexp\n");
+ }
+ }
+
+ if (params.include_html &&
+ (format != NOTMUCH_FORMAT_JSON && format != NOTMUCH_FORMAT_SEXP)) {
+ fprintf (stderr, "Warning: --include-html only implemented for format=json and format=sexp\n");
+ }
+
+ query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
+ if (query_string == NULL) {
+ fprintf (stderr, "Out of memory\n");
+ return EXIT_FAILURE;
+ }
+
+ if (*query_string == '\0') {
+ fprintf (stderr, "Error: notmuch show requires at least one search term.\n");
+ return EXIT_FAILURE;