[PATCH 1/5] show: Allow formatters to return errors

Austin Clements amdragon at MIT.EDU
Fri Mar 2 21:20:21 PST 2012


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   |   69 +++++++++++++++++++++++++++++++++++------------------
 2 files changed, 48 insertions(+), 27 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;
+	notmuch_status_t status;
 
-	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);
+	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



More information about the notmuch mailing list