--- /dev/null
+Return-Path: <bremner@tesseract.cs.unb.ca>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 53D236DE0939\r
+ for <notmuch@notmuchmail.org>; Sat, 6 Aug 2016 06:54:13 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.003\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.003 tagged_above=-999 required=5\r
+ tests=[AWL=-0.004, HEADER_FROM_DIFFERENT_DOMAINS=0.001]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id OQsEQXvnwvQ0 for <notmuch@notmuchmail.org>;\r
+ Sat, 6 Aug 2016 06:54:05 -0700 (PDT)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 6139D6DE0363\r
+ for <notmuch@notmuchmail.org>; Sat, 6 Aug 2016 06:53:06 -0700 (PDT)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84_2)\r
+ (envelope-from <bremner@tesseract.cs.unb.ca>)\r
+ id 1bW22T-0007ER-7L; Sat, 06 Aug 2016 09:53:21 -0400\r
+Received: (nullmailer pid 4135 invoked by uid 1000);\r
+ Sat, 06 Aug 2016 13:52:44 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 6/9] CLI: refactor dumping of tags.\r
+Date: Sat, 6 Aug 2016 22:52:36 +0900\r
+Message-Id: <1470491559-3946-7-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.8.1\r
+In-Reply-To: <1470491559-3946-1-git-send-email-david@tethera.net>\r
+References: <1470491559-3946-1-git-send-email-david@tethera.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 06 Aug 2016 13:54:13 -0000\r
+\r
+This is mainly code movement, to make room in the loop over messages for\r
+dumping properties.\r
+---\r
+ notmuch-dump.c | 127 +++++++++++++++++++++++++++++++--------------------------\r
+ 1 file changed, 69 insertions(+), 58 deletions(-)\r
+\r
+diff --git a/notmuch-dump.c b/notmuch-dump.c\r
+index cae1db8..d80ed8b8 100644\r
+--- a/notmuch-dump.c\r
++++ b/notmuch-dump.c\r
+@@ -78,13 +78,78 @@ print_dump_header (gzFile output, int output_format, int include)\r
+ }\r
+ \r
+ static int\r
++dump_tags_message (void *ctx,\r
++ notmuch_message_t *message, int output_format,\r
++ gzFile output,\r
++ char **buffer_p, size_t *size_p)\r
++{\r
++ int first = 1;\r
++ const char *message_id;\r
++\r
++ message_id = notmuch_message_get_message_id (message);\r
++\r
++ if (output_format == DUMP_FORMAT_BATCH_TAG &&\r
++ strchr (message_id, '\n')) {\r
++ /* This will produce a line break in the output, which\r
++ * would be difficult to handle in tools. However, it's\r
++ * also impossible to produce an email containing a line\r
++ * break in a message ID because of unfolding, so we can\r
++ * safely disallow it. */\r
++ fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);\r
++ return EXIT_SUCCESS;\r
++ }\r
++\r
++ if (output_format == DUMP_FORMAT_SUP) {\r
++ gzprintf (output, "%s (", message_id);\r
++ }\r
++\r
++ for (notmuch_tags_t *tags = notmuch_message_get_tags (message);\r
++ notmuch_tags_valid (tags);\r
++ notmuch_tags_move_to_next (tags)) {\r
++ const char *tag_str = notmuch_tags_get (tags);\r
++\r
++ if (! first)\r
++ gzputs (output, " ");\r
++\r
++ first = 0;\r
++\r
++ if (output_format == DUMP_FORMAT_SUP) {\r
++ gzputs (output, tag_str);\r
++ } else {\r
++ if (hex_encode (ctx, tag_str,\r
++ buffer_p, size_p) != HEX_SUCCESS) {\r
++ fprintf (stderr, "Error: failed to hex-encode tag %s\n",\r
++ tag_str);\r
++ return EXIT_FAILURE;\r
++ }\r
++ gzprintf (output, "+%s", *buffer_p);\r
++ }\r
++ }\r
++\r
++ if (output_format == DUMP_FORMAT_SUP) {\r
++ gzputs (output, ")\n");\r
++ } else {\r
++ if (make_boolean_term (ctx, "id", message_id,\r
++ buffer_p, size_p)) {\r
++ fprintf (stderr, "Error quoting message id %s: %s\n",\r
++ message_id, strerror (errno));\r
++ return EXIT_FAILURE;\r
++ }\r
++ gzprintf (output, " -- %s\n", *buffer_p);\r
++ }\r
++ return EXIT_SUCCESS;\r
++}\r
++\r
++static int\r
+ database_dump_file (notmuch_database_t *notmuch, gzFile output,\r
+ const char *query_str, int output_format, int include)\r
+ {\r
+ notmuch_query_t *query;\r
+ notmuch_messages_t *messages;\r
+ notmuch_message_t *message;\r
+- notmuch_tags_t *tags;\r
++ notmuch_status_t status;\r
++ char *buffer = NULL;\r
++ size_t buffer_size = 0;\r
+ \r
+ print_dump_header (output, output_format, include);\r
+ \r
+@@ -110,10 +175,6 @@ database_dump_file (notmuch_database_t *notmuch, gzFile output,\r
+ */\r
+ notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);\r
+ \r
+- char *buffer = NULL;\r
+- size_t buffer_size = 0;\r
+- notmuch_status_t status;\r
+-\r
+ status = notmuch_query_search_messages_st (query, &messages);\r
+ if (print_status_query ("notmuch dump", query, status))\r
+ return EXIT_FAILURE;\r
+@@ -121,62 +182,12 @@ database_dump_file (notmuch_database_t *notmuch, gzFile output,\r
+ for (;\r
+ notmuch_messages_valid (messages);\r
+ notmuch_messages_move_to_next (messages)) {\r
+- int first = 1;\r
+- const char *message_id;\r
+ \r
+ message = notmuch_messages_get (messages);\r
+- message_id = notmuch_message_get_message_id (message);\r
+-\r
+- if (output_format == DUMP_FORMAT_BATCH_TAG &&\r
+- strchr (message_id, '\n')) {\r
+- /* This will produce a line break in the output, which\r
+- * would be difficult to handle in tools. However, it's\r
+- * also impossible to produce an email containing a line\r
+- * break in a message ID because of unfolding, so we can\r
+- * safely disallow it. */\r
+- fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);\r
+- notmuch_message_destroy (message);\r
+- continue;\r
+- }\r
+ \r
+- if (output_format == DUMP_FORMAT_SUP) {\r
+- gzprintf (output, "%s (", message_id);\r
+- }\r
+-\r
+- for (tags = notmuch_message_get_tags (message);\r
+- notmuch_tags_valid (tags);\r
+- notmuch_tags_move_to_next (tags)) {\r
+- const char *tag_str = notmuch_tags_get (tags);\r
+-\r
+- if (! first)\r
+- gzputs (output, " ");\r
+-\r
+- first = 0;\r
+-\r
+- if (output_format == DUMP_FORMAT_SUP) {\r
+- gzputs (output, tag_str);\r
+- } else {\r
+- if (hex_encode (notmuch, tag_str,\r
+- &buffer, &buffer_size) != HEX_SUCCESS) {\r
+- fprintf (stderr, "Error: failed to hex-encode tag %s\n",\r
+- tag_str);\r
+- return EXIT_FAILURE;\r
+- }\r
+- gzprintf (output, "+%s", buffer);\r
+- }\r
+- }\r
+-\r
+- if (output_format == DUMP_FORMAT_SUP) {\r
+- gzputs (output, ")\n");\r
+- } else {\r
+- if (make_boolean_term (notmuch, "id", message_id,\r
+- &buffer, &buffer_size)) {\r
+- fprintf (stderr, "Error quoting message id %s: %s\n",\r
+- message_id, strerror (errno));\r
+- return EXIT_FAILURE;\r
+- }\r
+- gzprintf (output, " -- %s\n", buffer);\r
+- }\r
++ if (dump_tags_message (notmuch, message, output_format, output,\r
++ &buffer, &buffer_size))\r
++ return EXIT_FAILURE;\r
+ \r
+ notmuch_message_destroy (message);\r
+ }\r
+-- \r
+2.8.1\r
+\r