Re: [PATCH v7 2/3] Add structured output formatter for JSON and plain text (but don...
authorAustin Clements <amdragon@MIT.EDU>
Sun, 22 Jul 2012 16:08:43 +0000 (12:08 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:48:28 +0000 (09:48 -0800)
94/1a12cbfa527b0041eb8d1a33947a7ea68b8bcd [new file with mode: 0644]

diff --git a/94/1a12cbfa527b0041eb8d1a33947a7ea68b8bcd b/94/1a12cbfa527b0041eb8d1a33947a7ea68b8bcd
new file mode 100644 (file)
index 0000000..24dbbe2
--- /dev/null
@@ -0,0 +1,459 @@
+Return-Path: <amdragon@mit.edu>\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 1CD36431FAF\r
+       for <notmuch@notmuchmail.org>; Sun, 22 Jul 2012 09:08:50 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 XINKmzhsxIti for <notmuch@notmuchmail.org>;\r
+       Sun, 22 Jul 2012 09:08:49 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-4.mit.edu (DMZ-MAILSEC-SCANNER-4.MIT.EDU\r
+       [18.9.25.15])\r
+       by olra.theworths.org (Postfix) with ESMTP id D2AA5431FAE\r
+       for <notmuch@notmuchmail.org>; Sun, 22 Jul 2012 09:08:48 -0700 (PDT)\r
+X-AuditID: 1209190f-b7f306d0000008b4-b8-500c258f283e\r
+Received: from mailhub-auth-1.mit.edu ( [18.9.21.35])\r
+       by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id B3.D5.02228.F852C005; Sun, 22 Jul 2012 12:08:47 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id q6MG8lvG010878; \r
+       Sun, 22 Jul 2012 12:08:47 -0400\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
+       (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id q6MG8iZl004254\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Sun, 22 Jul 2012 12:08:46 -0400 (EDT)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1Ssyi4-0007E5-EH; Sun, 22 Jul 2012 12:08:44 -0400\r
+Date: Sun, 22 Jul 2012 12:08:43 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: craven@gmx.net\r
+Subject: Re: [PATCH v7 2/3] Add structured output formatter for JSON and\r
+       plain text (but don't use them yet).\r
+Message-ID: <20120722160843.GC31834@mit.edu>\r
+References: <20120718194819.GP31670@mit.edu>\r
+       <1342766173-1344-1-git-send-email-craven@gmx.net>\r
+       <1342766173-1344-3-git-send-email-craven@gmx.net>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <1342766173-1344-3-git-send-email-craven@gmx.net>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFmpmleLIzCtJLcpLzFFi42IR4hRV1u1X5QkwOPPN0mJvQzujxfWbM5kd\r
+       mDwWb9rP5vFs1S3mAKYoLpuU1JzMstQifbsEroybBx8xFewIrtjy4ANzA+M3uy5GDg4JAROJ\r
+       PzuKuxg5gUwxiQv31rOB2EIC+xgljjxK7GLkArI3MErs6DnPDOGcZJK4vesflLOEUeLhkudg\r
+       LSwCqhIf+jsYQWw2AQ2JbfuXg9kiAkISk768YgGxmQWkJb79bmYCsYUF8iVWruhlBbF5BXQk\r
+       jj15wQYxdAqjROupVkaIhKDEyZlPoJq1JG78e8kEcjbIoOX/OEDCnAJ2Ek83PGYHsUUFVCSm\r
+       nNzGNoFRaBaS7llIumchdC9gZF7FKJuSW6Wbm5iZU5yarFucnJiXl1qka6KXm1mil5pSuokR\r
+       HNaS/DsYvx1UOsQowMGoxMNr6M4dIMSaWFZcmXuIUZKDSUmUd4U0T4AQX1J+SmVGYnFGfFFp\r
+       TmrxIUYJDmYlEd7L14HKeVMSK6tSi/JhUtIcLErivFdTbvoLCaQnlqRmp6YWpBbBZGU4OJQk\r
+       eG+oAA0VLEpNT61Iy8wpQUgzcXCCDOcBGr4BpIa3uCAxtzgzHSJ/ilFRSpx3BkhCACSRUZoH\r
+       1wtLO68YxYFeEebdCVLFA0xZcN2vgAYzAQ2WzuICGVySiJCSamBkSH1aeEfxUIQOe0BAtNFh\r
+       loSOwkecu4U1nJjvi70QfJ742tFVUv0Rq+MnlVWNR51PXN7Vtu7kLul7k4+Uzl2wuLN/6o38\r
+       CKNDnSKMawKztglu3jjLV8/W/qF+VsDjxF+KZ2oVzv3JSP8k+dP2W67kCjYDU/fN/8rVZUOL\r
+       /xn3f7boex/73UqJpTgj0VCLuag4EQDaJVlnFgMAAA==\r
+Cc: notmuch@notmuchmail.org\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: Sun, 22 Jul 2012 16:08:50 -0000\r
+\r
+Quoth craven@gmx.net on Jul 20 at  8:36 am:\r
+> From: <craven@gmx.net>\r
+> \r
+> Using the new structured printer support in sprinter.h, implement\r
+> sprinter_json_create, which returns a new JSON structured output\r
+> formatter. The formatter prints output similar to the existing JSON, but\r
+> with differences in whitespace (mostly newlines, --output=summary prints\r
+> the entire message summary on one line, not split across multiple lines).\r
+> \r
+> Also implement a "structured" formatter for plain text that prints\r
+> prefixed strings, to be used with notmuch-search.c plain text output.\r
+> ---\r
+>  Makefile.local  |   2 +\r
+>  sprinter-json.c | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+>  sprinter-text.c | 126 ++++++++++++++++++++++++++++++++++++++\r
+>  sprinter.h      |  10 +++\r
+>  4 files changed, 323 insertions(+)\r
+>  create mode 100644 sprinter-json.c\r
+>  create mode 100644 sprinter-text.c\r
+> \r
+> diff --git a/Makefile.local b/Makefile.local\r
+> index a890df2..296995d 100644\r
+> --- a/Makefile.local\r
+> +++ b/Makefile.local\r
+> @@ -290,6 +290,8 @@ notmuch_client_srcs =            \\r
+>      notmuch-show.c          \\r
+>      notmuch-tag.c           \\r
+>      notmuch-time.c          \\r
+> +    sprinter-json.c         \\r
+> +    sprinter-text.c         \\r
+>      query-string.c          \\r
+>      mime-node.c             \\r
+>      crypto.c                \\r
+> diff --git a/sprinter-json.c b/sprinter-json.c\r
+> new file mode 100644\r
+> index 0000000..32daa5a\r
+> --- /dev/null\r
+> +++ b/sprinter-json.c\r
+> @@ -0,0 +1,185 @@\r
+> +#include <stdbool.h>\r
+> +#include <stdio.h>\r
+> +#include <talloc.h>\r
+> +#include "sprinter.h"\r
+> +\r
+> +struct sprinter_json {\r
+> +    struct sprinter vtable;\r
+> +    FILE *stream;\r
+> +    /* Top of the state stack, or NULL if the printer is not currently\r
+> +     * inside any aggregate types. */\r
+> +    struct json_state *state;\r
+> +\r
+> +    /* A flag to signify that a separator should be inserted in the\r
+> +     * output as soon as possible.\r
+> +     */\r
+> +    notmuch_bool_t insert_separator;\r
+> +};\r
+> +\r
+> +struct json_state {\r
+> +    struct json_state *parent;\r
+> +    /* True if nothing has been printed in this aggregate yet.\r
+> +     * Suppresses the comma before a value. */\r
+> +    notmuch_bool_t first;\r
+> +    /* The character that closes the current aggregate. */\r
+> +    char close;\r
+> +};\r
+> +\r
+> +/* Helper function to set up the stream to print a value.  If this\r
+> + * value follows another value, prints a comma. */\r
+> +static struct sprinter_json *\r
+> +json_begin_value (struct sprinter *sp)\r
+> +{\r
+> +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
+> +\r
+> +    if (spj->state) {\r
+> +    if (! spj->state->first) {\r
+> +        fputc (',', spj->stream);\r
+> +        if (spj->insert_separator) {\r
+> +            fputc ('\n', spj->stream);\r
+> +            spj->insert_separator = FALSE;\r
+> +        } else\r
+> +            fputc (' ', spj->stream);\r
+> +    } else\r
+> +        spj->state->first = FALSE;\r
+\r
+Just a nit: if you roll a v8, it would be nice to have braces around\r
+both of these else parts to match the braces on the then parts.\r
+Obviously this doesn't warrant a v8 on its own, though.\r
+\r
+> +    }\r
+> +    return spj;\r
+> +}\r
+> +\r
+> +/* Helper function to begin an aggregate type.  Prints the open\r
+> + * character and pushes a new state frame. */\r
+> +static void\r
+> +json_begin_aggregate (struct sprinter *sp, char open, char close)\r
+> +{\r
+> +    struct sprinter_json *spj = json_begin_value (sp);\r
+> +    struct json_state *state = talloc (spj, struct json_state);\r
+> +\r
+> +    fputc (open, spj->stream);\r
+> +    state->parent = spj->state;\r
+> +    state->first = TRUE;\r
+> +    state->close = close;\r
+> +    spj->state = state;\r
+> +}\r
+> +\r
+> +static void\r
+> +json_begin_map (struct sprinter *sp)\r
+> +{\r
+> +    json_begin_aggregate (sp, '{', '}');\r
+> +}\r
+> +\r
+> +static void\r
+> +json_begin_list (struct sprinter *sp)\r
+> +{\r
+> +    json_begin_aggregate (sp, '[', ']');\r
+> +}\r
+> +\r
+> +static void\r
+> +json_end (struct sprinter *sp)\r
+> +{\r
+> +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
+> +    struct json_state *state = spj->state;\r
+> +\r
+> +    fputc (spj->state->close, spj->stream);\r
+> +    spj->state = state->parent;\r
+> +    talloc_free (state);\r
+> +    if (spj->state == NULL)\r
+> +    fputc ('\n', spj->stream);\r
+> +}\r
+> +\r
+> +static void\r
+> +json_string (struct sprinter *sp, const char *val)\r
+> +{\r
+> +    static const char *const escapes[] = {\r
+> +    ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
+> +    ['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"\r
+> +    };\r
+> +    struct sprinter_json *spj = json_begin_value (sp);\r
+> +\r
+> +    fputc ('"', spj->stream);\r
+> +    for (; *val; ++val) {\r
+> +    unsigned char ch = *val;\r
+> +    if (ch < ARRAY_SIZE (escapes) && escapes[ch])\r
+> +        fputs (escapes[ch], spj->stream);\r
+> +    else if (ch >= 32)\r
+> +        fputc (ch, spj->stream);\r
+> +    else\r
+> +        fprintf (spj->stream, "\\u%04x", ch);\r
+> +    }\r
+> +    fputc ('"', spj->stream);\r
+> +}\r
+> +\r
+> +static void\r
+> +json_integer (struct sprinter *sp, int val)\r
+> +{\r
+> +    struct sprinter_json *spj = json_begin_value (sp);\r
+> +\r
+> +    fprintf (spj->stream, "%d", val);\r
+> +}\r
+> +\r
+> +static void\r
+> +json_boolean (struct sprinter *sp, notmuch_bool_t val)\r
+> +{\r
+> +    struct sprinter_json *spj = json_begin_value (sp);\r
+> +\r
+> +    fputs (val ? "true" : "false", spj->stream);\r
+> +}\r
+> +\r
+> +static void\r
+> +json_null (struct sprinter *sp)\r
+> +{\r
+> +    struct sprinter_json *spj = json_begin_value (sp);\r
+> +\r
+> +    fputs ("null", spj->stream);\r
+> +}\r
+> +\r
+> +static void\r
+> +json_map_key (struct sprinter *sp, const char *key)\r
+> +{\r
+> +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
+> +\r
+> +    json_string (sp, key);\r
+> +    fputs (": ", spj->stream);\r
+> +    spj->state->first = TRUE;\r
+> +}\r
+> +\r
+> +static void\r
+> +json_set_prefix (unused (struct sprinter *sp), unused (const char *name))\r
+> +{\r
+> +}\r
+> +\r
+> +static void\r
+> +json_separator (struct sprinter *sp)\r
+> +{\r
+> +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
+> +\r
+> +    spj->insert_separator = TRUE;\r
+> +}\r
+> +\r
+> +struct sprinter *\r
+> +sprinter_json_create (const void *ctx, FILE *stream)\r
+> +{\r
+> +    static const struct sprinter_json template = {\r
+> +    .vtable = {\r
+> +        .begin_map = json_begin_map,\r
+> +        .begin_list = json_begin_list,\r
+> +        .end = json_end,\r
+> +        .string = json_string,\r
+> +        .integer = json_integer,\r
+> +        .boolean = json_boolean,\r
+> +        .null = json_null,\r
+> +        .map_key = json_map_key,\r
+> +        .separator = json_separator,\r
+> +        .set_prefix = json_set_prefix,\r
+> +        .is_text_printer = FALSE,\r
+> +    }\r
+> +    };\r
+> +    struct sprinter_json *res;\r
+> +\r
+> +    res = talloc (ctx, struct sprinter_json);\r
+> +    if (! res)\r
+> +    return NULL;\r
+> +\r
+> +    *res = template;\r
+> +    res->stream = stream;\r
+> +    return &res->vtable;\r
+> +}\r
+> diff --git a/sprinter-text.c b/sprinter-text.c\r
+> new file mode 100644\r
+> index 0000000..b208840\r
+> --- /dev/null\r
+> +++ b/sprinter-text.c\r
+> @@ -0,0 +1,126 @@\r
+> +#include <stdbool.h>\r
+> +#include <stdio.h>\r
+> +#include <talloc.h>\r
+> +#include "sprinter.h"\r
+> +\r
+> +/* "Structured printer" interface for unstructured text printing.\r
+> + * Note that --output=summary is dispatched and formatted in\r
+> + * notmuch-search.c, the code in this file is only used for all other\r
+> + * output types.\r
+> + */\r
+> +\r
+> +struct sprinter_text {\r
+> +    struct sprinter vtable;\r
+> +    FILE *stream;\r
+> +\r
+> +    /* The current prefix to be printed with string/integer/boolean\r
+> +     * data.\r
+> +     */\r
+> +    const char *current_prefix;\r
+> +\r
+> +    /* A flag to indicate if this is the first tag. Used in list of tags\r
+> +     * for summary.\r
+> +     */\r
+> +    notmuch_bool_t first_tag;\r
+> +};\r
+> +\r
+> +static void\r
+> +text_string (struct sprinter *sp, const char *val)\r
+> +{\r
+> +    struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
+> +\r
+> +    if (sptxt->current_prefix != NULL)\r
+> +    fprintf (sptxt->stream, "%s:", sptxt->current_prefix);\r
+> +\r
+> +    fputs(val, sptxt->stream);\r
+> +}\r
+> +\r
+> +static void\r
+> +text_integer (struct sprinter *sp, int val)\r
+> +{\r
+> +    struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
+> +\r
+> +    fprintf (sptxt->stream, "%d", val);\r
+> +}\r
+> +\r
+> +static void\r
+> +text_boolean (struct sprinter *sp, notmuch_bool_t val)\r
+> +{\r
+> +    struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
+> +\r
+> +    fputs (val ? "true" : "false", sptxt->stream);\r
+> +}\r
+> +\r
+> +static void\r
+> +text_separator (struct sprinter *sp)\r
+> +{\r
+> +    struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
+> +\r
+> +    fputc ('\n', sptxt->stream);\r
+> +}\r
+> +\r
+> +static void\r
+> +text_set_prefix (struct sprinter *sp, const char *prefix)\r
+> +{\r
+> +    struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
+> +\r
+> +    sptxt->current_prefix = prefix;\r
+> +}\r
+> +\r
+> +/* The structure functions begin_map, begin_list, end and map_key\r
+> + * don't do anything in the text formatter.\r
+> + */\r
+> +\r
+> +static void\r
+> +text_begin_map (unused (struct sprinter *sp))\r
+> +{\r
+> +}\r
+> +\r
+> +static void\r
+> +text_begin_list (unused (struct sprinter *sp))\r
+> +{\r
+> +}\r
+> +\r
+> +static void\r
+> +text_end (unused (struct sprinter *sp))\r
+> +{\r
+> +}\r
+> +\r
+> +static void\r
+> +text_null (unused (struct sprinter *sp))\r
+> +{\r
+> +}\r
+> +\r
+> +static void\r
+> +text_map_key (unused (struct sprinter *sp), unused (const char *key))\r
+> +{\r
+> +}\r
+> +\r
+> +struct sprinter *\r
+> +sprinter_text_create (const void *ctx, FILE *stream)\r
+> +{\r
+> +    static const struct sprinter_text template = {\r
+> +    .vtable = {\r
+> +        .begin_map = text_begin_map,\r
+> +        .begin_list = text_begin_list,\r
+> +        .end = text_end,\r
+> +        .string = text_string,\r
+> +        .integer = text_integer,\r
+> +        .boolean = text_boolean,\r
+> +        .null = text_null,\r
+> +        .map_key = text_map_key,\r
+> +        .separator = text_separator,\r
+> +        .set_prefix = text_set_prefix,\r
+> +        .is_text_printer = TRUE,\r
+> +    },\r
+> +    };\r
+> +    struct sprinter_text *res;\r
+> +\r
+> +    res = talloc (ctx, struct sprinter_text);\r
+> +    if (! res)\r
+> +    return NULL;\r
+> +\r
+> +    *res = template;\r
+> +    res->stream = stream;\r
+> +    return &res->vtable;\r
+> +}\r
+> diff --git a/sprinter.h b/sprinter.h\r
+> index 77dc26f..6680d41 100644\r
+> --- a/sprinter.h\r
+> +++ b/sprinter.h\r
+> @@ -55,4 +55,14 @@ typedef struct sprinter {\r
+>      notmuch_bool_t is_text_printer;\r
+>  } sprinter_t;\r
+>  \r
+> +\r
+> +/* Create a new unstructured printer that emits the default text format\r
+> + * for "notmuch search". */\r
+> +struct sprinter *\r
+> +sprinter_text_create (const void *ctx, FILE *stream);\r
+> +\r
+> +/* Create a new structure printer that emits JSON. */\r
+> +struct sprinter *\r
+> +sprinter_json_create (const void *ctx, FILE *stream);\r
+> +\r
+>  #endif // NOTMUCH_SPRINTER_H\r