[PATCH v3 3/3] Use the JSON structure printer in notmuch-search.
authorcraven <craven@gmx.net>
Wed, 11 Jul 2012 08:26:35 +0000 (10:26 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:48:10 +0000 (09:48 -0800)
a7/cf8f351011839eb54386607133d4f81b543445 [new file with mode: 0644]

diff --git a/a7/cf8f351011839eb54386607133d4f81b543445 b/a7/cf8f351011839eb54386607133d4f81b543445
new file mode 100644 (file)
index 0000000..f420cf8
--- /dev/null
@@ -0,0 +1,532 @@
+Return-Path: <craven@gmx.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 7CC12431FC7\r
+       for <notmuch@notmuchmail.org>; Wed, 11 Jul 2012 01:25:12 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.001\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.001 tagged_above=-999 required=5\r
+       tests=[FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001]\r
+       autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id qYmoeOLsQiDk for <notmuch@notmuchmail.org>;\r
+       Wed, 11 Jul 2012 01:25:10 -0700 (PDT)\r
+Received: from mailout-de.gmx.net (mailout-de.gmx.net [213.165.64.22])\r
+       by olra.theworths.org (Postfix) with SMTP id 50A62431FAE\r
+       for <notmuch@notmuchmail.org>; Wed, 11 Jul 2012 01:25:10 -0700 (PDT)\r
+Received: (qmail invoked by alias); 11 Jul 2012 08:25:09 -0000\r
+Received: from gw.arelion.cust.net.lagis.at (EHLO dodekanex.arelion.at)\r
+       [83.164.197.182]\r
+       by mail.gmx.net (mp032) with SMTP; 11 Jul 2012 10:25:09 +0200\r
+X-Authenticated: #201305\r
+X-Provags-ID: V01U2FsdGVkX1/IO9IRSDPIMxIhkl5XHIWKb7Q2PE0PO9rF1UJChC\r
+       5PJbGz768JJ1Lf\r
+Received: by dodekanex.arelion.at (Postfix, from userid 1000)\r
+       id E798030313E; Wed, 11 Jul 2012 10:26:36 +0200 (CEST)\r
+From: <craven@gmx.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 3/3] Use the JSON structure printer in notmuch-search.\r
+Date: Wed, 11 Jul 2012 10:26:35 +0200\r
+Message-Id: <1341995195-2497-4-git-send-email-craven@gmx.net>\r
+X-Mailer: git-send-email 1.7.11.1\r
+In-Reply-To: <1341995195-2497-1-git-send-email-craven@gmx.net>\r
+References: <20120710191331.GE7332@mit.edu>\r
+       <1341995195-2497-1-git-send-email-craven@gmx.net>\r
+X-Y-GMX-Trusted: 0\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://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: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 11 Jul 2012 08:25:12 -0000\r
+\r
+This patch uses the JSON structure printer for notmuch search. The\r
+changes necessary for switching to the structure printer are minor, a\r
+large part of this patch is concerned with keeping the normal\r
+unstructured text output.\r
+---\r
+ notmuch-search.c | 278 ++++++++++++++++++++++++++++++-------------------------\r
+ 1 file changed, 150 insertions(+), 128 deletions(-)\r
+\r
+diff --git a/notmuch-search.c b/notmuch-search.c\r
+index 3be296d..84e4cbc 100644\r
+--- a/notmuch-search.c\r
++++ b/notmuch-search.c\r
+@@ -19,6 +19,7 @@\r
+  */\r
+ \r
+ #include "notmuch-client.h"\r
++#include "structured-output.h"\r
+ \r
+ typedef enum {\r
+     OUTPUT_SUMMARY,\r
+@@ -28,6 +29,7 @@ typedef enum {\r
+     OUTPUT_TAGS\r
+ } output_t;\r
+ \r
++/* legacy, only needed for non-structured text output */\r
+ typedef struct search_format {\r
+     const char *results_start;\r
+     const char *item_start;\r
+@@ -64,6 +66,7 @@ format_thread_text (const void *ctx,\r
+                   const int total,\r
+                   const char *authors,\r
+                   const char *subject);\r
++\r
+ static const search_format_t format_text = {\r
+     "",\r
+       "",\r
+@@ -78,35 +81,6 @@ static const search_format_t format_text = {\r
+ };\r
+ \r
+ static void\r
+-format_item_id_json (const void *ctx,\r
+-                   const char *item_type,\r
+-                   const char *item_id);\r
+-\r
+-static void\r
+-format_thread_json (const void *ctx,\r
+-                  const char *thread_id,\r
+-                  const time_t date,\r
+-                  const int matched,\r
+-                  const int total,\r
+-                  const char *authors,\r
+-                  const char *subject);\r
+-\r
+-/* Any changes to the JSON format should be reflected in the file\r
+- * devel/schemata. */\r
+-static const search_format_t format_json = {\r
+-    "[",\r
+-      "{",\r
+-          format_item_id_json,\r
+-          format_thread_json,\r
+-          "\"tags\": [",\r
+-              "\"%s\"", ", ",\r
+-          "]", ",\n",\r
+-      "}",\r
+-    "]\n",\r
+-    "]\n",\r
+-};\r
+-\r
+-static void\r
+ format_item_id_text (unused (const void *ctx),\r
+                    const char *item_type,\r
+                    const char *item_id)\r
+@@ -153,50 +127,9 @@ format_thread_text (const void *ctx,\r
+     talloc_free (ctx_quote);\r
+ }\r
+ \r
+-static void\r
+-format_item_id_json (const void *ctx,\r
+-                   unused (const char *item_type),\r
+-                   const char *item_id)\r
+-{\r
+-    void *ctx_quote = talloc_new (ctx);\r
+-\r
+-    printf ("%s", json_quote_str (ctx_quote, item_id));\r
+-\r
+-    talloc_free (ctx_quote);\r
+-    \r
+-}\r
+-\r
+-static void\r
+-format_thread_json (const void *ctx,\r
+-                  const char *thread_id,\r
+-                  const time_t date,\r
+-                  const int matched,\r
+-                  const int total,\r
+-                  const char *authors,\r
+-                  const char *subject)\r
+-{\r
+-    void *ctx_quote = talloc_new (ctx);\r
+-\r
+-    printf ("\"thread\": %s,\n"\r
+-          "\"timestamp\": %ld,\n"\r
+-          "\"date_relative\": \"%s\",\n"\r
+-          "\"matched\": %d,\n"\r
+-          "\"total\": %d,\n"\r
+-          "\"authors\": %s,\n"\r
+-          "\"subject\": %s,\n",\r
+-          json_quote_str (ctx_quote, thread_id),\r
+-          date,\r
+-          notmuch_time_relative_date (ctx, date),\r
+-          matched,\r
+-          total,\r
+-          json_quote_str (ctx_quote, authors),\r
+-          json_quote_str (ctx_quote, subject));\r
+-\r
+-    talloc_free (ctx_quote);\r
+-}\r
+-\r
+ static int\r
+-do_search_threads (const search_format_t *format,\r
++do_search_threads (const structure_printer_t *format,\r
++                 void *state,\r
+                  notmuch_query_t *query,\r
+                  notmuch_sort_t sort,\r
+                  output_t output,\r
+@@ -209,6 +142,8 @@ do_search_threads (const search_format_t *format,\r
+     time_t date;\r
+     int first_thread = 1;\r
+     int i;\r
++    int outermost_level = 0;\r
++    int items_level = 0;\r
+ \r
+     if (offset < 0) {\r
+       offset += notmuch_query_count_threads (query);\r
+@@ -220,7 +155,11 @@ do_search_threads (const search_format_t *format,\r
+     if (threads == NULL)\r
+       return 1;\r
+ \r
+-    fputs (format->results_start, stdout);\r
++    if (format == unstructured_text_printer) {\r
++      fputs(format_text.results_start, stdout);\r
++    } else { /* structured output */\r
++      outermost_level = format->list(state);\r
++    }\r
+ \r
+     for (i = 0;\r
+        notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);\r
+@@ -235,43 +174,85 @@ do_search_threads (const search_format_t *format,\r
+           continue;\r
+       }\r
+ \r
+-      if (! first_thread)\r
+-          fputs (format->item_sep, stdout);\r
++      if (format == unstructured_text_printer && ! first_thread)\r
++          fputs (format_text.item_sep, stdout);\r
+ \r
+       if (output == OUTPUT_THREADS) {\r
+-          format->item_id (thread, "thread:",\r
+-                           notmuch_thread_get_thread_id (thread));\r
++          if (format == unstructured_text_printer) {\r
++              format_text.item_id (thread, "thread:",\r
++                                   notmuch_thread_get_thread_id (thread));\r
++          } else { /* structured output */\r
++              format->string(state, notmuch_thread_get_thread_id (thread));\r
++          }\r
+       } else { /* output == OUTPUT_SUMMARY */\r
+-          fputs (format->item_start, stdout);\r
++\r
++          if (format == unstructured_text_printer) {\r
++              fputs (format_text.item_start, stdout);\r
++          } else { /* structured output */\r
++              items_level = format->map(state);\r
++          }\r
+ \r
+           if (sort == NOTMUCH_SORT_OLDEST_FIRST)\r
+               date = notmuch_thread_get_oldest_date (thread);\r
+           else\r
+               date = notmuch_thread_get_newest_date (thread);\r
+ \r
+-          format->thread_summary (thread,\r
+-                                  notmuch_thread_get_thread_id (thread),\r
+-                                  date,\r
+-                                  notmuch_thread_get_matched_messages (thread),\r
+-                                  notmuch_thread_get_total_messages (thread),\r
+-                                  notmuch_thread_get_authors (thread),\r
+-                                  notmuch_thread_get_subject (thread));\r
+-\r
+-          fputs (format->tag_start, stdout);\r
++          if (format == unstructured_text_printer) {\r
++              format_text.thread_summary (thread,\r
++                                          notmuch_thread_get_thread_id (thread),\r
++                                          date,\r
++                                          notmuch_thread_get_matched_messages (thread),\r
++                                          notmuch_thread_get_total_messages (thread),\r
++                                          notmuch_thread_get_authors (thread),\r
++                                          notmuch_thread_get_subject (thread));\r
++          } else { /* structured output */\r
++              void *ctx = talloc_new (0);\r
++              format->map_key(state, "thread");\r
++              format->string(state, notmuch_thread_get_thread_id (thread));\r
++              format->map_key(state, "timestamp");\r
++              format->number(state, date);\r
++              format->map_key(state, "date_relative");\r
++              format->string(state, notmuch_time_relative_date(ctx, date));\r
++              format->map_key(state, "matched");\r
++              format->number(state, notmuch_thread_get_matched_messages(thread));\r
++              format->map_key(state, "total");\r
++              format->number(state, notmuch_thread_get_total_messages(thread));\r
++              format->map_key(state, "authors");\r
++              format->string(state, notmuch_thread_get_authors(thread));\r
++              format->map_key(state, "subject");\r
++              format->string(state, notmuch_thread_get_subject(thread));\r
++              talloc_free(ctx);\r
++          };\r
++\r
++          if (format == unstructured_text_printer) {\r
++              fputs (format_text.tag_start, stdout);\r
++          } else { /* structured output */\r
++              format->map_key(state, "tags");\r
++              format->list(state);\r
++          }\r
+ \r
+           for (tags = notmuch_thread_get_tags (thread);\r
+                notmuch_tags_valid (tags);\r
+                notmuch_tags_move_to_next (tags))\r
+           {\r
+-              if (! first_tag)\r
+-                  fputs (format->tag_sep, stdout);\r
+-              printf (format->tag, notmuch_tags_get (tags));\r
++              if (format == unstructured_text_printer && ! first_tag) {\r
++                  fputs (format_text.tag_sep, stdout);\r
++              }\r
++\r
++              if(format == unstructured_text_printer) {\r
++                  printf (format_text.tag, notmuch_tags_get (tags));\r
++              } else {\r
++                  format->string(state, notmuch_tags_get(tags));\r
++              }\r
+               first_tag = 0;\r
+           }\r
+ \r
+-          fputs (format->tag_end, stdout);\r
+-\r
+-          fputs (format->item_end, stdout);\r
++          if(format == unstructured_text_printer) {\r
++              fputs (format_text.tag_end, stdout);\r
++              fputs (format_text.item_end, stdout);\r
++          } else { /* structured output */\r
++              format->pop(state, items_level);\r
++          }\r
+       }\r
+ \r
+       first_thread = 0;\r
+@@ -279,16 +260,21 @@ do_search_threads (const search_format_t *format,\r
+       notmuch_thread_destroy (thread);\r
+     }\r
+ \r
+-    if (first_thread)\r
+-      fputs (format->results_null, stdout);\r
+-    else\r
+-      fputs (format->results_end, stdout);\r
++    if (format == unstructured_text_printer) {\r
++      if (first_thread)\r
++          fputs (format_text.results_null, stdout);\r
++      else\r
++          fputs (format_text.results_end, stdout);\r
++    } else { /* structured output */\r
++      format->pop(state, outermost_level);\r
++    }\r
+ \r
+     return 0;\r
+ }\r
+ \r
+ static int\r
+-do_search_messages (const search_format_t *format,\r
++do_search_messages (const structure_printer_t *format,\r
++                  void *state,\r
+                   notmuch_query_t *query,\r
+                   output_t output,\r
+                   int offset,\r
+@@ -299,6 +285,7 @@ do_search_messages (const search_format_t *format,\r
+     notmuch_filenames_t *filenames;\r
+     int first_message = 1;\r
+     int i;\r
++    int outermost_level = 0;\r
+ \r
+     if (offset < 0) {\r
+       offset += notmuch_query_count_messages (query);\r
+@@ -310,7 +297,11 @@ do_search_messages (const search_format_t *format,\r
+     if (messages == NULL)\r
+       return 1;\r
+ \r
+-    fputs (format->results_start, stdout);\r
++    if(format == unstructured_text_printer) {\r
++      fputs (format_text.results_start, stdout);\r
++    } else { /* structured output */\r
++      outermost_level = format->list(state);\r
++    }\r
+ \r
+     for (i = 0;\r
+        notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);\r
+@@ -328,23 +319,32 @@ do_search_messages (const search_format_t *format,\r
+                notmuch_filenames_valid (filenames);\r
+                notmuch_filenames_move_to_next (filenames))\r
+           {\r
+-              if (! first_message)\r
+-                  fputs (format->item_sep, stdout);\r
++              if (format == unstructured_text_printer) {\r
++                  if (! first_message)\r
++                      fputs (format_text.item_sep, stdout);\r
+ \r
+-              format->item_id (message, "",\r
+-                               notmuch_filenames_get (filenames));\r
++                  format_text.item_id (message, "",\r
++                                       notmuch_filenames_get (filenames));\r
++              } else { /* structured output */\r
++              format->string(state, notmuch_filenames_get (filenames));\r
++              }\r
+ \r
+               first_message = 0;\r
+           }\r
+-          \r
++\r
+           notmuch_filenames_destroy( filenames );\r
+ \r
+       } else { /* output == OUTPUT_MESSAGES */\r
+-          if (! first_message)\r
+-              fputs (format->item_sep, stdout);\r
++          if (format == unstructured_text_printer) {\r
++              if (! first_message)\r
++                  fputs (format_text.item_sep, stdout);\r
++\r
++              format_text.item_id (message, "id:",\r
++                                   notmuch_message_get_message_id (message));\r
++          } else { /* structured output */\r
++              format->string(state, notmuch_message_get_message_id (message));\r
++          }\r
+ \r
+-          format->item_id (message, "id:",\r
+-                           notmuch_message_get_message_id (message));\r
+           first_message = 0;\r
+       }\r
+ \r
+@@ -353,23 +353,29 @@ do_search_messages (const search_format_t *format,\r
+ \r
+     notmuch_messages_destroy (messages);\r
+ \r
+-    if (first_message)\r
+-      fputs (format->results_null, stdout);\r
+-    else\r
+-      fputs (format->results_end, stdout);\r
++    if (format == unstructured_text_printer) {\r
++      if (first_message)\r
++          fputs (format_text.results_null, stdout);\r
++      else\r
++          fputs (format_text.results_end, stdout);\r
++    } else { /* structured output */\r
++      format->pop(state, outermost_level);\r
++    };\r
+ \r
+     return 0;\r
+ }\r
+ \r
+ static int\r
+ do_search_tags (notmuch_database_t *notmuch,\r
+-              const search_format_t *format,\r
++              const structure_printer_t *format,\r
++              void *state,\r
+               notmuch_query_t *query)\r
+ {\r
+     notmuch_messages_t *messages = NULL;\r
+     notmuch_tags_t *tags;\r
+     const char *tag;\r
+     int first_tag = 1;\r
++    int outermost_level = 0;\r
+ \r
+     /* should the following only special case if no excluded terms\r
+      * specified? */\r
+@@ -387,7 +393,11 @@ do_search_tags (notmuch_database_t *notmuch,\r
+     if (tags == NULL)\r
+       return 1;\r
+ \r
+-    fputs (format->results_start, stdout);\r
++    if (format == unstructured_text_printer) {\r
++      fputs (format_text.results_start, stdout);\r
++    } else { /* structured output */\r
++      outermost_level = format->list(state);\r
++    }\r
+ \r
+     for (;\r
+        notmuch_tags_valid (tags);\r
+@@ -395,10 +405,14 @@ do_search_tags (notmuch_database_t *notmuch,\r
+     {\r
+       tag = notmuch_tags_get (tags);\r
+ \r
+-      if (! first_tag)\r
+-          fputs (format->item_sep, stdout);\r
+-\r
+-      format->item_id (tags, "", tag);\r
++      if (format == unstructured_text_printer) {\r
++          if (! first_tag) {\r
++              fputs (format_text.item_sep, stdout);\r
++          }\r
++          format_text.item_id (tags, "", tag);\r
++      } else { /* structured output */\r
++          format->string(state, tag);\r
++      }\r
+ \r
+       first_tag = 0;\r
+     }\r
+@@ -408,10 +422,14 @@ do_search_tags (notmuch_database_t *notmuch,\r
+     if (messages)\r
+       notmuch_messages_destroy (messages);\r
+ \r
+-    if (first_tag)\r
+-      fputs (format->results_null, stdout);\r
+-    else\r
+-      fputs (format->results_end, stdout);\r
++    if (format == unstructured_text_printer) {\r
++      if (first_tag)\r
++          fputs (format_text.results_null, stdout);\r
++      else\r
++          fputs (format_text.results_end, stdout);\r
++    } else { /* structured output */\r
++      format->pop(state, outermost_level);\r
++    };\r
+ \r
+     return 0;\r
+ }\r
+@@ -430,7 +448,8 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+     notmuch_query_t *query;\r
+     char *query_str;\r
+     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;\r
+-    const search_format_t *format = &format_text;\r
++    const structure_printer_t *format = unstructured_text_printer;\r
++    void *state = NULL;\r
+     int opt_index, ret;\r
+     output_t output = OUTPUT_SUMMARY;\r
+     int offset = 0;\r
+@@ -475,13 +494,16 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+ \r
+     switch (format_sel) {\r
+     case NOTMUCH_FORMAT_TEXT:\r
+-      format = &format_text;\r
++      format = unstructured_text_printer;\r
+       break;\r
+     case NOTMUCH_FORMAT_JSON:\r
+-      format = &format_json;\r
++      format = &json_structure_printer;\r
+       break;\r
+     }\r
+ \r
++    if(format != unstructured_text_printer)\r
++      state = format->initial_state(format, stdout);\r
++\r
+     config = notmuch_config_open (ctx, NULL, NULL);\r
+     if (config == NULL)\r
+       return 1;\r
+@@ -532,14 +554,14 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
+     default:\r
+     case OUTPUT_SUMMARY:\r
+     case OUTPUT_THREADS:\r
+-      ret = do_search_threads (format, query, sort, output, offset, limit);\r
++      ret = do_search_threads (format, state, query, sort, output, offset, limit);\r
+       break;\r
+     case OUTPUT_MESSAGES:\r
+     case OUTPUT_FILES:\r
+-      ret = do_search_messages (format, query, output, offset, limit);\r
++      ret = do_search_messages (format, state, query, output, offset, limit);\r
+       break;\r
+     case OUTPUT_TAGS:\r
+-      ret = do_search_tags (notmuch, format, query);\r
++      ret = do_search_tags (notmuch, format, state, query);\r
+       break;\r
+     }\r
+ \r
+-- \r
+1.7.11.1\r
+\r