unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Austin Clements <amdragon@MIT.EDU>
To: notmuch@notmuchmail.org
Subject: [PATCH v2 3/8] show: Allow formatters to return errors
Date: Tue,  6 Mar 2012 18:48:39 +0000	[thread overview]
Message-ID: <1331059724-14653-4-git-send-email-amdragon@mit.edu> (raw)
In-Reply-To: <1331059724-14653-1-git-send-email-amdragon@mit.edu>

Formatter errors are propagated to the exit status of notmuch show.

This isn't used by the JSON or text formatters, but it will be useful
for the raw format, which is pickier.
---
 notmuch-client.h |    6 ++--
 notmuch-show.c   |   71 +++++++++++++++++++++++++++++++++++-------------------
 2 files changed, 49 insertions(+), 28 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index f4a62cc..a220fe4 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -67,9 +67,9 @@ struct notmuch_show_params;
 
 typedef struct notmuch_show_format {
     const char *message_set_start;
-    void (*part) (const void *ctx,
-		  struct mime_node *node, int indent,
-		  const struct notmuch_show_params *params);
+    notmuch_status_t (*part) (const void *ctx,
+			      struct mime_node *node, int indent,
+			      const struct notmuch_show_params *params);
     const char *message_start;
     void (*message) (const void *ctx,
 		     notmuch_message_t *message,
diff --git a/notmuch-show.c b/notmuch-show.c
index 6a171a4..648f468 100644
--- a/notmuch-show.c
+++ b/notmuch-show.c
@@ -23,7 +23,7 @@
 static void
 format_headers_message_part_text (GMimeMessage *message);
 
-static void
+static notmuch_status_t
 format_part_text (const void *ctx, mime_node_t *node,
 		  int indent, const notmuch_show_params_t *params);
 
@@ -34,7 +34,7 @@ static const notmuch_show_format_t format_text = {
     .message_set_end = ""
 };
 
-static void
+static notmuch_status_t
 format_part_json_entry (const void *ctx, mime_node_t *node,
 			int indent, const notmuch_show_params_t *params);
 
@@ -562,7 +562,7 @@ format_part_content_raw (GMimeObject *part)
 	g_object_unref(stream_stdout);
 }
 
-static void
+static notmuch_status_t
 format_part_text (const void *ctx, mime_node_t *node,
 		  int indent, const notmuch_show_params_t *params)
 {
@@ -650,6 +650,8 @@ format_part_text (const void *ctx, mime_node_t *node,
 	printf ("\fbody}\n");
 
     printf ("\f%s}\n", part_type);
+
+    return NOTMUCH_STATUS_SUCCESS;
 }
 
 static void
@@ -751,14 +753,16 @@ format_part_json (const void *ctx, mime_node_t *node, notmuch_bool_t first)
     printf ("%s}", terminator);
 }
 
-static void
+static notmuch_status_t
 format_part_json_entry (const void *ctx, mime_node_t *node, unused (int indent),
 			unused (const notmuch_show_params_t *params))
 {
     format_part_json (ctx, node, TRUE);
+
+    return NOTMUCH_STATUS_SUCCESS;
 }
 
-static void
+static notmuch_status_t
 show_message (void *ctx,
 	      const notmuch_show_format_t *format,
 	      notmuch_message_t *message,
@@ -768,14 +772,18 @@ show_message (void *ctx,
     if (format->part) {
 	void *local = talloc_new (ctx);
 	mime_node_t *root, *part;
-
-	if (mime_node_open (local, message, params->cryptoctx, params->decrypt,
-			    &root) == NOTMUCH_STATUS_SUCCESS &&
-	    (part = mime_node_seek_dfs (root, (params->part < 0 ?
-					       0 : params->part))))
-	    format->part (local, part, indent, params);
+	notmuch_status_t status;
+
+	status = mime_node_open (local, message, params->cryptoctx,
+				 params->decrypt, &root);
+	if (status)
+	    goto DONE;
+	part = mime_node_seek_dfs (root, (params->part < 0 ? 0 : params->part));
+	if (part)
+	    status = format->part (local, part, indent, params);
+      DONE:
 	talloc_free (local);
-	return;
+	return status;
     }
 
     if (params->part <= 0) {
@@ -799,9 +807,11 @@ show_message (void *ctx,
 
 	fputs (format->message_end, stdout);
     }
+
+    return NOTMUCH_STATUS_SUCCESS;
 }
 
-static void
+static notmuch_status_t
 show_messages (void *ctx,
 	       const notmuch_show_format_t *format,
 	       notmuch_messages_t *messages,
@@ -812,6 +822,7 @@ show_messages (void *ctx,
     notmuch_bool_t match;
     int first_set = 1;
     int next_indent;
+    notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
 
     fputs (format->message_set_start, stdout);
 
@@ -832,17 +843,22 @@ show_messages (void *ctx,
 	next_indent = indent;
 
 	if (match || params->entire_thread) {
-	    show_message (ctx, format, message, indent, params);
+	    status = show_message (ctx, format, message, indent, params);
+	    if (status && !res)
+		res = status;
 	    next_indent = indent + 1;
 
-	    fputs (format->message_set_sep, stdout);
+	    if (!status)
+		fputs (format->message_set_sep, stdout);
 	}
 
-	show_messages (ctx,
-		       format,
-		       notmuch_message_get_replies (message),
-		       next_indent,
-		       params);
+	status = show_messages (ctx,
+				format,
+				notmuch_message_get_replies (message),
+				next_indent,
+				params);
+	if (status && !res)
+	    res = status;
 
 	notmuch_message_destroy (message);
 
@@ -850,6 +866,8 @@ show_messages (void *ctx,
     }
 
     fputs (format->message_set_end, stdout);
+
+    return res;
 }
 
 /* Formatted output of single message */
@@ -914,13 +932,13 @@ do_show_single (void *ctx,
 
 	fclose (file);
 
+	return 0;
+
     } else {
 
-	show_message (ctx, format, message, 0, params);
+	return show_message (ctx, format, message, 0, params) != NOTMUCH_STATUS_SUCCESS;
 
     }
-
-    return 0;
 }
 
 /* Formatted output of threads */
@@ -934,6 +952,7 @@ do_show (void *ctx,
     notmuch_thread_t *thread;
     notmuch_messages_t *messages;
     int first_toplevel = 1;
+    notmuch_status_t status, res = NOTMUCH_STATUS_SUCCESS;
 
     fputs (format->message_set_start, stdout);
 
@@ -953,7 +972,9 @@ do_show (void *ctx,
 	    fputs (format->message_set_sep, stdout);
 	first_toplevel = 0;
 
-	show_messages (ctx, format, messages, 0, params);
+	status = show_messages (ctx, format, messages, 0, params);
+	if (status && !res)
+	    res = status;
 
 	notmuch_thread_destroy (thread);
 
@@ -961,7 +982,7 @@ do_show (void *ctx,
 
     fputs (format->message_set_end, stdout);
 
-    return 0;
+    return res != NOTMUCH_STATUS_SUCCESS;
 }
 
 enum {
-- 
1.7.7.3

  parent reply	other threads:[~2012-03-06 18:48 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-03  5:20 [PATCH 0/5] Rewrite mbox and raw show formats Austin Clements
2012-03-03  5:20 ` [PATCH 1/5] show: Allow formatters to return errors Austin Clements
2012-03-03  5:20 ` [PATCH 2/5] show: Move format_message_mbox with the other new-style formats Austin Clements
2012-03-03  5:20 ` [PATCH 3/5] show: Convert mbox format to new self-recursive style Austin Clements
2012-03-03  5:20 ` [PATCH 4/5] show: Move format_part_content_raw with the other new-style formats Austin Clements
2012-03-03  5:20 ` [PATCH 5/5] show: Convert raw format to the new self-recursive style Austin Clements
2012-03-03 22:05   ` Jameson Graef Rollins
2012-03-06 18:43     ` Austin Clements
2012-03-03  8:18 ` [PATCH 0/5] Rewrite mbox and raw show formats Tomi Ollila
2012-03-06 18:48 ` [PATCH v2 0/8] " Austin Clements
2012-03-06 18:48   ` [PATCH v2 1/8] test: Fix typo in test description Austin Clements
2012-03-06 18:48   ` [PATCH v2 2/8] test: Fix malformed multipart message Austin Clements
2012-03-06 18:48   ` Austin Clements [this message]
2012-03-06 21:22     ` [PATCH v2 3/8] show: Allow formatters to return errors Mark Walters
2012-03-07 20:18       ` Tomi Ollila
2012-03-11 23:34       ` Austin Clements
2012-03-06 18:48   ` [PATCH v2 4/8] show: Move format_message_mbox with the other new-style formats Austin Clements
2012-03-06 18:48   ` [PATCH v2 5/8] show: Convert mbox format to new self-recursive style Austin Clements
2012-03-06 18:48   ` [PATCH v2 6/8] show: Move format_part_content_raw with the other new-style formats Austin Clements
2012-03-06 18:48   ` [PATCH v2 7/8] show: Convert raw format to the new self-recursive style, properly support interior parts Austin Clements
2012-03-06 18:48   ` [PATCH v2 8/8] man: Update raw format documentation Austin Clements
2012-03-09  3:25   ` [PATCH v2 0/8] Rewrite mbox and raw show formats Adam Wolfe Gordon
2012-03-18 12:51   ` David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1331059724-14653-4-git-send-email-amdragon@mit.edu \
    --to=amdragon@mit.edu \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).