From 36505a40eafc783c9a5573470148481f2b5187f4 Mon Sep 17 00:00:00 2001 From: Peter Feigl Date: Fri, 13 Jul 2012 10:11:36 +0200 Subject: [PATCH] [PATCH v5 2/3] Add structured output formatter for JSON and text. --- 69/33bb28be80b843d871eb988d6fec11a28195ff | 492 ++++++++++++++++++++++ 1 file changed, 492 insertions(+) create mode 100644 69/33bb28be80b843d871eb988d6fec11a28195ff diff --git a/69/33bb28be80b843d871eb988d6fec11a28195ff b/69/33bb28be80b843d871eb988d6fec11a28195ff new file mode 100644 index 000000000..59a8aeb6a --- /dev/null +++ b/69/33bb28be80b843d871eb988d6fec11a28195ff @@ -0,0 +1,492 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id B4B78431FAE + for ; Fri, 13 Jul 2012 01:13:45 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: 0.001 +X-Spam-Level: +X-Spam-Status: No, score=0.001 tagged_above=-999 required=5 + tests=[FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001] + autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id QEqTQsT8bffN for ; + Fri, 13 Jul 2012 01:13:42 -0700 (PDT) +Received: from mailout-de.gmx.net (mailout-de.gmx.net [213.165.64.23]) + by olra.theworths.org (Postfix) with SMTP id 33915431FC4 + for ; Fri, 13 Jul 2012 01:13:41 -0700 (PDT) +Received: (qmail invoked by alias); 13 Jul 2012 08:13:38 -0000 +Received: from cm56-137-203.liwest.at (EHLO nexork.localdomain) + [86.56.137.203] by mail.gmx.net (mp019) with SMTP; 13 Jul 2012 10:13:38 +0200 +X-Authenticated: #201305 +X-Provags-ID: V01U2FsdGVkX1/1LBN6uRX16FYH/ks8KYjbl0uzKhUbcIT8wucqYT + RWNNvnA9tM0Dn7 +Received: by nexork.localdomain (Postfix, from userid 1000) + id CCBFF4527803; Fri, 13 Jul 2012 10:11:42 +0200 (CEST) +From: Peter Feigl +To: notmuch@notmuchmail.org +Subject: [PATCH v5 2/3] Add structured output formatter for JSON and text. +Date: Fri, 13 Jul 2012 10:11:36 +0200 +Message-Id: <1342167097-25012-3-git-send-email-craven@gmx.net> +X-Mailer: git-send-email 1.7.11.1 +In-Reply-To: <1342167097-25012-1-git-send-email-craven@gmx.net> +References: <20120710191331.GE7332@mit.edu> + <1342167097-25012-1-git-send-email-craven@gmx.net> +X-Y-GMX-Trusted: 0 +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Fri, 13 Jul 2012 08:13:46 -0000 + +From: + +Using the new structured printer support in sprinter.h, implement +sprinter_json_create, which returns a new JSON structured output +formatter. The formatter prints output similar to the existing JSON, but +with differences in whitespace (mostly newlines, --output=summary prints +the entire message summary on one line, not split across multiple lines). + +Also implement a "structured" formatter that prints the current plain +text format. This passes all tests, but the exact formatting is probably +specific to notmuch-search and cannot easily (if at all) be adapted to +be used across all of notmuch-{search,reply,show,...}. +--- + Makefile.local | 2 + + sprinter-json.c | 184 +++++++++++++++++++++++++++++++++++++++++++++++ + sprinter-text-search.c | 190 +++++++++++++++++++++++++++++++++++++++++++++++++ + sprinter.h | 8 +++ + 4 files changed, 384 insertions(+) + create mode 100644 sprinter-json.c + create mode 100644 sprinter-text-search.c + +diff --git a/Makefile.local b/Makefile.local +index a890df2..b6c7e0c 100644 +--- a/Makefile.local ++++ b/Makefile.local +@@ -290,6 +290,8 @@ notmuch_client_srcs = \ + notmuch-show.c \ + notmuch-tag.c \ + notmuch-time.c \ ++ sprinter-json.c \ ++ sprinter-text-search.c \ + query-string.c \ + mime-node.c \ + crypto.c \ +diff --git a/sprinter-json.c b/sprinter-json.c +new file mode 100644 +index 0000000..215151d +--- /dev/null ++++ b/sprinter-json.c +@@ -0,0 +1,184 @@ ++#include ++#include ++#include ++#include "sprinter.h" ++ ++struct sprinter_json { ++ struct sprinter vtable; ++ FILE *stream; ++ /* Top of the state stack, or NULL if the printer is not currently ++ * inside any aggregate types. */ ++ struct json_state *state; ++ ++ /* A flag to signify that a separator should be inserted in the ++ * output as soon as possible. ++ */ ++ notmuch_bool_t insert_separator; ++}; ++ ++struct json_state { ++ struct json_state *parent; ++ /* True if nothing has been printed in this aggregate yet. ++ * Suppresses the comma before a value. */ ++ notmuch_bool_t first; ++ /* The character that closes the current aggregate. */ ++ char close; ++}; ++ ++/* Helper function to set up the stream to print a value. If this ++ * value follows another value, prints a comma. */ ++static struct sprinter_json * ++json_begin_value (struct sprinter *sp) ++{ ++ struct sprinter_json *spj = (struct sprinter_json *) sp; ++ ++ if (spj->state) { ++ if (! spj->state->first) { ++ fputc (',', spj->stream); ++ if (spj->insert_separator) { ++ fputc ('\n', spj->stream); ++ spj->insert_separator = FALSE; ++ } else ++ fputc (' ', spj->stream); ++ } else ++ spj->state->first = FALSE; ++ } ++ return spj; ++} ++ ++/* Helper function to begin an aggregate type. Prints the open ++ * character and pushes a new state frame. */ ++static void ++json_begin_aggregate (struct sprinter *sp, char open, char close) ++{ ++ struct sprinter_json *spj = json_begin_value (sp); ++ struct json_state *state = talloc (spj, struct json_state); ++ ++ fputc (open, spj->stream); ++ state->parent = spj->state; ++ state->first = TRUE; ++ state->close = close; ++ spj->state = state; ++} ++ ++static void ++json_begin_map (struct sprinter *sp) ++{ ++ json_begin_aggregate (sp, '{', '}'); ++} ++ ++static void ++json_begin_list (struct sprinter *sp) ++{ ++ json_begin_aggregate (sp, '[', ']'); ++} ++ ++static void ++json_end (struct sprinter *sp) ++{ ++ struct sprinter_json *spj = (struct sprinter_json *) sp; ++ struct json_state *state = spj->state; ++ ++ fputc (spj->state->close, spj->stream); ++ spj->state = state->parent; ++ talloc_free (state); ++ if (spj->state == NULL) ++ fputc ('\n', spj->stream); ++} ++ ++static void ++json_string (struct sprinter *sp, const char *val) ++{ ++ static const char *const escapes[] = { ++ ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b", ++ ['\f'] = "\\f", ['\n'] = "\\n", ['\t'] = "\\t" ++ }; ++ struct sprinter_json *spj = json_begin_value (sp); ++ ++ fputc ('"', spj->stream); ++ for (; *val; ++val) { ++ unsigned char ch = *val; ++ if (ch < ARRAY_SIZE (escapes) && escapes[ch]) ++ fputs (escapes[ch], spj->stream); ++ else if (ch >= 32) ++ fputc (ch, spj->stream); ++ else ++ fprintf (spj->stream, "\\u%04x", ch); ++ } ++ fputc ('"', spj->stream); ++} ++ ++static void ++json_integer (struct sprinter *sp, int val) ++{ ++ struct sprinter_json *spj = json_begin_value (sp); ++ ++ fprintf (spj->stream, "%d", val); ++} ++ ++static void ++json_boolean (struct sprinter *sp, notmuch_bool_t val) ++{ ++ struct sprinter_json *spj = json_begin_value (sp); ++ ++ fputs (val ? "true" : "false", spj->stream); ++} ++ ++static void ++json_null (struct sprinter *sp) ++{ ++ struct sprinter_json *spj = json_begin_value (sp); ++ ++ fputs ("null", spj->stream); ++} ++ ++static void ++json_map_key (struct sprinter *sp, const char *key) ++{ ++ struct sprinter_json *spj = (struct sprinter_json *) sp; ++ ++ json_string (sp, key); ++ fputs (": ", spj->stream); ++ spj->state->first = TRUE; ++} ++ ++static void ++json_set_prefix (unused (struct sprinter *sp), unused (const char *name)) ++{ ++} ++ ++static void ++json_separator (struct sprinter *sp) ++{ ++ struct sprinter_json *spj = (struct sprinter_json *) sp; ++ ++ spj->insert_separator = TRUE; ++} ++ ++struct sprinter * ++sprinter_json_create (const void *ctx, FILE *stream) ++{ ++ static const struct sprinter_json template = { ++ .vtable = { ++ .begin_map = json_begin_map, ++ .begin_list = json_begin_list, ++ .end = json_end, ++ .string = json_string, ++ .integer = json_integer, ++ .boolean = json_boolean, ++ .null = json_null, ++ .map_key = json_map_key, ++ .separator = json_separator, ++ .set_prefix = json_set_prefix, ++ } ++ }; ++ struct sprinter_json *res; ++ ++ res = talloc (ctx, struct sprinter_json); ++ if (! res) ++ return NULL; ++ ++ *res = template; ++ res->stream = stream; ++ return &res->vtable; ++} +diff --git a/sprinter-text-search.c b/sprinter-text-search.c +new file mode 100644 +index 0000000..95ed9cb +--- /dev/null ++++ b/sprinter-text-search.c +@@ -0,0 +1,190 @@ ++#include ++#include ++#include ++#include "sprinter.h" ++ ++/* "Structured printer" interface for unstructured text printing. ++ * This is at best a misuse of the interface, but it simplifies the code ++ * in notmuch-search.c considerably. ++ */ ++ ++struct sprinter_text_search { ++ struct sprinter vtable; ++ FILE *stream; ++ ++ /* The current name or prefix to be printed with string/integer/boolean ++ * data. ++ */ ++ const char *current_name; ++ ++ /* A flag to indicate if this is the first tag. Used in list of tags ++ * for summary. ++ */ ++ notmuch_bool_t first_tag; ++}; ++ ++/* struct text_search_state { */ ++/* struct text_search_state *parent; */ ++/* }; */ ++ ++static notmuch_bool_t ++current_context (struct sprinter_text_search *sptxt, const char *marker) ++{ ++ return (sptxt->current_name != NULL ++ && ! strncmp (marker, sptxt->current_name, strlen (marker))); ++} ++ ++static void ++print_sanitized_string (FILE *stream, const char *str) ++{ ++ if (NULL == str) ++ return; ++ ++ for (; *str; str++) { ++ if ((unsigned char) (*str) < 32) ++ fputc ('?', stream); ++ else ++ fputc (*str, stream); ++ } ++} ++ ++static void ++text_search_begin_map (unused (struct sprinter *sp)) ++{ ++} ++ ++static void ++text_search_begin_list (struct sprinter *sp) ++{ ++ struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp; ++ ++ if (current_context (sptxt, "tags")) { ++ fputs (" (", sptxt->stream); ++ sptxt->first_tag = TRUE; ++ } ++} ++ ++static void ++text_search_end (struct sprinter *sp) ++{ ++ struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp; ++ ++ if (current_context (sptxt, "tags")) { ++ fputc (')', sptxt->stream); ++ sptxt->current_name = NULL; ++ } ++} ++ ++static void ++text_search_string (struct sprinter *sp, const char *val) ++{ ++ struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp; ++ ++ if (sptxt->current_name != NULL) { ++ if (current_context (sptxt, "thread")) ++ fprintf ( sptxt->stream, "thread:%s ", val); ++ else if (current_context (sptxt, "date_relative")) ++ fprintf ( sptxt->stream, "%12s ", val); ++ else if (current_context (sptxt, "authors")) { ++ print_sanitized_string (sptxt->stream, val); ++ fputs ("; ", sptxt->stream); ++ } else if (current_context (sptxt, "subject")) ++ print_sanitized_string (sptxt->stream, val); ++ else if (current_context (sptxt, "tags")) { ++ if (! sptxt->first_tag) ++ fputc (' ', sptxt->stream); ++ else ++ sptxt->first_tag = FALSE; ++ ++ fputs (val, sptxt->stream); ++ } else { ++ fputs (sptxt->current_name, sptxt->stream); ++ fputc (':', sptxt->stream); ++ fputs (val, sptxt->stream); ++ } ++ } else { ++ fputs (val, sptxt->stream); ++ } ++} ++ ++static void ++text_search_integer (struct sprinter *sp, int val) ++{ ++ struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp; ++ ++ if (sptxt->current_name != NULL) { ++ if (current_context (sptxt, "matched")) ++ fprintf ( sptxt->stream, "[%d/", val); ++ else if (current_context (sptxt, "total")) ++ fprintf ( sptxt->stream, "%d] ", val); ++ } else ++ fprintf (sptxt->stream, "%d", val); ++} ++ ++static void ++text_search_boolean (struct sprinter *sp, notmuch_bool_t val) ++{ ++ struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp; ++ ++ fputs (val ? "true" : "false", sptxt->stream); ++} ++ ++static void ++text_search_null (struct sprinter *sp) ++{ ++ struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp; ++ ++ fputs ("null", sptxt->stream); ++} ++ ++static void ++text_search_map_key (struct sprinter *sp, const char *key) ++{ ++ struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp; ++ ++ sptxt->current_name = key; ++} ++ ++static void ++text_search_separator (struct sprinter *sp) ++{ ++ struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp; ++ ++ fputc ('\n', sptxt->stream); ++} ++ ++static void ++text_search_set_prefix (struct sprinter *sp, const char *name) ++{ ++ struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp; ++ ++ sptxt->current_name = name; ++} ++ ++struct sprinter * ++sprinter_text_search_create (const void *ctx, FILE *stream) ++{ ++ static const struct sprinter_text_search template = { ++ .vtable = { ++ .begin_map = text_search_begin_map, ++ .begin_list = text_search_begin_list, ++ .end = text_search_end, ++ .string = text_search_string, ++ .integer = text_search_integer, ++ .boolean = text_search_boolean, ++ .null = text_search_null, ++ .map_key = text_search_map_key, ++ .separator = text_search_separator, ++ .set_prefix = text_search_set_prefix, ++ } ++ }; ++ struct sprinter_text_search *res; ++ ++ res = talloc (ctx, struct sprinter_text_search); ++ if (! res) ++ return NULL; ++ ++ *res = template; ++ res->stream = stream; ++ return &res->vtable; ++} +diff --git a/sprinter.h b/sprinter.h +index c9cd6a6..4241d65 100644 +--- a/sprinter.h ++++ b/sprinter.h +@@ -47,4 +47,12 @@ typedef struct sprinter { + void (*separator)(struct sprinter *); + } sprinter_t; + ++/* Create a new unstructured printer that emits the default Text format for search. */ ++struct sprinter * ++sprinter_text_search_create (const void *ctx, FILE *stream); ++ ++/* Create a new structure printer that emits JSON. */ ++struct sprinter * ++sprinter_json_create (const void *ctx, FILE *stream); ++ + #endif // NOTMUCH_SPRINTER_H +-- +1.7.11.1 + -- 2.26.2