Re: [PATCH v4 2/3] Add structured output formatter for JSON.
authorAustin Clements <amdragon@MIT.EDU>
Thu, 12 Jul 2012 22:07:04 +0000 (18:07 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:48:13 +0000 (09:48 -0800)
05/ea0a953804a7e91620d0bda1e19cae8d8fadd5 [new file with mode: 0644]

diff --git a/05/ea0a953804a7e91620d0bda1e19cae8d8fadd5 b/05/ea0a953804a7e91620d0bda1e19cae8d8fadd5
new file mode 100644 (file)
index 0000000..38da7f7
--- /dev/null
@@ -0,0 +1,300 @@
+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 CBB7F429E25\r
+       for <notmuch@notmuchmail.org>; Thu, 12 Jul 2012 15:07:09 -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 jEGa1-+WJEDK for <notmuch@notmuchmail.org>;\r
+       Thu, 12 Jul 2012 15:07:08 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-8.mit.edu (DMZ-MAILSEC-SCANNER-8.MIT.EDU\r
+       [18.7.68.37])\r
+       by olra.theworths.org (Postfix) with ESMTP id 4F29C431FC4\r
+       for <notmuch@notmuchmail.org>; Thu, 12 Jul 2012 15:07:08 -0700 (PDT)\r
+X-AuditID: 12074425-b7f9b6d0000008c4-2d-4fff4a8a9e35\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-8.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id F6.63.02244.A8A4FFF4; Thu, 12 Jul 2012 18:07:06 -0400 (EDT)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id q6CM75Jq002528; \r
+       Thu, 12 Jul 2012 18:07:05 -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 q6CM744i028805\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Thu, 12 Jul 2012 18:07:05 -0400 (EDT)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1SpRXM-0004sD-2W; Thu, 12 Jul 2012 18:07:04 -0400\r
+Date: Thu, 12 Jul 2012 18:07:04 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: craven@gmx.net\r
+Subject: Re: [PATCH v4 2/3] Add structured output formatter for JSON.\r
+Message-ID: <20120712220703.GI7332@mit.edu>\r
+References: <87d34hsdx8.fsf@awakening.csail.mit.edu>\r
+       <1342079004-5300-1-git-send-email-craven@gmx.net>\r
+       <1342079004-5300-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: <1342079004-5300-3-git-send-email-craven@gmx.net>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFmplleLIzCtJLcpLzFFi42IRYrdT1+3y+u9v8H82q8XehnZGi+s3ZzI7\r
+       MHks3rSfzePZqlvMAUxRXDYpqTmZZalF+nYJXBm7G3ewF0zTrzj09xRzA+M+5S5GTg4JAROJ\r
+       49s+sUDYYhIX7q1n62Lk4hAS2McoseX9GXYIZwOjxMtt/1ghnJNMErP+wDhLGCX+Lv0D1s8i\r
+       oCpx4c9GMJtNQENi2/7ljCC2iICQxKQvr8DizALSEt9+NzOB2MICrhKtT9+A2bwC2hKLL/yF\r
+       2j2HUaL1+RtWiISgxMmZT6CatSRu/HsJ1MABNmj5Pw6QMKeAncSdGz3sILaogIrElJPb2CYw\r
+       Cs1C0j0LSfcshO4FjMyrGGVTcqt0cxMzc4pTk3WLkxPz8lKLdC30cjNL9FJTSjcxgkPbRXUH\r
+       44RDSocYBTgYlXh4d6776y/EmlhWXJl7iFGSg0lJlNfa9b+/EF9SfkplRmJxRnxRaU5q8SFG\r
+       CQ5mJRHedfZAOd6UxMqq1KJ8mJQ0B4uSOO+NlJv+QgLpiSWp2ampBalFMFkZDg4lCd4tnkCN\r
+       gkWp6akVaZk5JQhpJg5OkOE8QMNngdTwFhck5hZnpkPkTzEqSonzzgdJCIAkMkrz4HphqecV\r
+       ozjQK8K8C0CqeIBpC677FdBgJpDBP/+BDC5JREhJNTBuWqHr58561eXNtkO/rv+vbjIIFJvN\r
+       3nm1RiDCT6dtwuNlZ9Yp3xMvjhe/9CfIO818+45t/947TI+oDfywMqJX8cakN7P3Se2ebuIz\r
+       wfrJ9V8MEqVLduX/k/auDZ/xl8/5zZq1dTVvl/qnSa0VLN6uJuDWtjY+z9lmS0LM6WexBXZS\r
+       a/4f/2ekxFKckWioxVxUnAgAfQM9CRgDAAA=\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: Thu, 12 Jul 2012 22:07:10 -0000\r
+\r
+Quoth craven@gmx.net on Jul 12 at  9:43 am:\r
+> Using the new structured printer support in sprinter.h, implement\r
+> sprinter_json_new, which returns a new JSON structured output\r
+> formatter.\r
+> \r
+> The formatter prints output similar to the existing JSON, but with\r
+> differences in whitespace (mostly newlines).\r
+> ---\r
+>  Makefile.local |   1 +\r
+>  sprinter.c     | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+>  2 files changed, 173 insertions(+)\r
+>  create mode 100644 sprinter.c\r
+> \r
+> diff --git a/Makefile.local b/Makefile.local\r
+> index a890df2..8baf0c2 100644\r
+> --- a/Makefile.local\r
+> +++ b/Makefile.local\r
+> @@ -290,6 +290,7 @@ notmuch_client_srcs =            \\r
+>      notmuch-show.c          \\r
+>      notmuch-tag.c           \\r
+>      notmuch-time.c          \\r
+> +    sprinter.c              \\r
+>      query-string.c          \\r
+>      mime-node.c             \\r
+>      crypto.c                \\r
+> diff --git a/sprinter.c b/sprinter.c\r
+> new file mode 100644\r
+> index 0000000..649f79a\r
+> --- /dev/null\r
+> +++ b/sprinter.c\r
+> @@ -0,0 +1,172 @@\r
+> +#include <stdbool.h>\r
+> +#include <stdio.h>\r
+> +#include <talloc.h>\r
+> +#include "sprinter.h"\r
+> +\r
+> +#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))\r
+> +\r
+> +struct sprinter *\r
+> +sprinter_text = NULL;\r
+> +\r
+> +/*\r
+> + * Every below here is the implementation of the JSON printer.\r
+\r
+s/Every/Everything/\r
+\r
+Or this can be shortened to just\r
+\r
+/*\r
+ * JSON printer\r
+ */\r
+\r
+If we wind up with separate files for the JSON and S-exp printers, I\r
+don't think this comment is necessary at all.\r
+\r
+> + */\r
+> +\r
+> +struct sprinter_json\r
+> +{\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
+> +\r
+> +struct json_state\r
+> +{\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
+As Tomi pointed out, there should be spaces before the parameter\r
+lists (sorry).\r
+\r
+> +{\r
+> +    struct sprinter_json *spj = (struct sprinter_json*)sp;\r
+> +    if (spj->state) {\r
+> +    if (!spj->state->first)\r
+> +        fputs (", ", spj->stream);\r
+> +    else\r
+> +        spj->state->first = false;\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
+\r
+Ah, another missing space.\r
+\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
+> +    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
+> +    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
+> +    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
+> +    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
+> +    json_string (sp, key);\r
+> +    fputs (": ", spj->stream);\r
+> +    spj->state->first = true;\r
+> +}\r
+> +\r
+> +static void\r
+> +json_frame(struct sprinter *sp)\r
+> +{\r
+> +    struct sprinter_json *spj = (struct sprinter_json*)sp;\r
+> +    fputc ('\n', spj->stream);\r
+> +}\r
+> +\r
+> +struct sprinter *\r
+> +sprinter_json_new(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
+> +        .frame = json_frame,\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