Re: [PATCH 1/3] Adding an S-expression structured output printer.
authorJani Nikula <jani@nikula.org>
Fri, 30 Nov 2012 19:11:43 +0000 (21:11 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:51:13 +0000 (09:51 -0800)
44/ef7ef5cfff31d73e894ec436b1ccc9d4f75e5e [new file with mode: 0644]

diff --git a/44/ef7ef5cfff31d73e894ec436b1ccc9d4f75e5e b/44/ef7ef5cfff31d73e894ec436b1ccc9d4f75e5e
new file mode 100644 (file)
index 0000000..9d131cb
--- /dev/null
@@ -0,0 +1,354 @@
+Return-Path: <jani@nikula.org>\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 B8226431FAF\r
+       for <notmuch@notmuchmail.org>; Fri, 30 Nov 2012 11:11:53 -0800 (PST)\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 SM37fGwOVq7p for <notmuch@notmuchmail.org>;\r
+       Fri, 30 Nov 2012 11:11:49 -0800 (PST)\r
+Received: from mail-la0-f53.google.com (mail-la0-f53.google.com\r
+       [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 422F9431FAE\r
+       for <notmuch@notmuchmail.org>; Fri, 30 Nov 2012 11:11:49 -0800 (PST)\r
+Received: by mail-la0-f53.google.com with SMTP id w12so675603lag.26\r
+       for <notmuch@notmuchmail.org>; Fri, 30 Nov 2012 11:11:47 -0800 (PST)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=google.com; s=20120113;\r
+       h=from:to:subject:in-reply-to:references:user-agent:date:message-id\r
+       :mime-version:content-type:x-gm-message-state;\r
+       bh=hGsHK8uZZiXqFWd/4WRNvd2BcsyaLP4JF8XC2w4HvxQ=;\r
+       b=ODcjzpYKy1uCjFalG2F4OCPz6MfPruAmMphsBu8v0irBPExHsNd/838ULqW6POWh75\r
+       PvsbcU6Y2NiARxNHKEZ+2VRYIR2qMTjgx9vWugJQg7Pucq6YF+A/zcygX5xPNku0icUY\r
+       dBFhJdKROv4EBYxAxqJhoZ59dMgUh2/ZNpU4BbnXvqdnmiYHK58TFS2fjIV35ydY2o2y\r
+       iph8iwZI+qWPPuaCKT2SqqQh1opt55y+3XtL8ijiWwfW1psd0sb7dQWxQAtwsZ4sGzpr\r
+       qLoXSLqAnYOM2IZmXxf3WrMs2sm3eVizE16c38vt0nTB5H1FDxJB1U/MM656SDG/6jDJ\r
+       f2+A==\r
+Received: by 10.152.144.201 with SMTP id so9mr2300540lab.24.1354302706506;\r
+       Fri, 30 Nov 2012 11:11:46 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi.\r
+       [80.223.81.27])\r
+       by mx.google.com with ESMTPS id d5sm2407084lbk.10.2012.11.30.11.11.44\r
+       (version=SSLv3 cipher=OTHER); Fri, 30 Nov 2012 11:11:45 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: Peter Feigl <craven@gmx.net>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 1/3] Adding an S-expression structured output printer.\r
+In-Reply-To: <1354264143-30173-1-git-send-email-craven@gmx.net>\r
+References: <1354264143-30173-1-git-send-email-craven@gmx.net>\r
+User-Agent: Notmuch/0.14+124~g3b17402 (http://notmuchmail.org) Emacs/23.4.1\r
+       (i686-pc-linux-gnu)\r
+Date: Fri, 30 Nov 2012 21:11:43 +0200\r
+Message-ID: <87zk1yj47k.fsf@nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Gm-Message-State:\r
+ ALoCoQls9cfnR8npdFLCu7SkQ0yxCJKnCs/Tzsq5lrhYgTefYmqDQp6UCduRK3jAEcVJjdN11OW5\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: Fri, 30 Nov 2012 19:11:53 -0000\r
+\r
+On Fri, 30 Nov 2012, Peter Feigl <craven@gmx.net> wrote:\r
+> This commit adds an sprinter for Lisp S-Expressions. Later commits will\r
+> use this printer.\r
+\r
+Overall the series looks good, apart from the small trivial\r
+nitpicks. We'll also need tests and man pages for the cli command\r
+changes. IMO small smoke tests are a good start and better than\r
+nothing. This is especially important as the sexp format is not used by\r
+other components yet. Man pages will be mostly copy paste from the json\r
+sections.\r
+\r
+BR,\r
+Jani.\r
+\r
+>\r
+> The structure is the same as json, but:\r
+> - arrays are written as lists: ("foo" "bar" "baaz" 1 2 3)\r
+> - maps are written as a-lists: ((key "value") (other-key "other-value"))\r
+> - true is written as t\r
+> - false is written as nil\r
+> - null is written as nil\r
+> ---\r
+>  Makefile.local  |   1 +\r
+>  sprinter-sexp.c | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+>  2 files changed, 236 insertions(+)\r
+>  create mode 100644 sprinter-sexp.c\r
+>\r
+> diff --git a/Makefile.local b/Makefile.local\r
+> index 2b91946..0db1713 100644\r
+> --- a/Makefile.local\r
+> +++ b/Makefile.local\r
+> @@ -270,6 +270,7 @@ notmuch_client_srcs =            \\r
+>      notmuch-tag.c           \\r
+>      notmuch-time.c          \\r
+>      sprinter-json.c         \\r
+> +    sprinter-sexp.c         \\r
+>      sprinter-text.c         \\r
+>      query-string.c          \\r
+>      mime-node.c             \\r
+> diff --git a/sprinter-sexp.c b/sprinter-sexp.c\r
+> new file mode 100644\r
+> index 0000000..8401c52\r
+> --- /dev/null\r
+> +++ b/sprinter-sexp.c\r
+> @@ -0,0 +1,235 @@\r
+> +#include <stdbool.h>\r
+> +#include <stdio.h>\r
+> +#include <talloc.h>\r
+> +#include "sprinter.h"\r
+> +\r
+> +struct sprinter_sexp {\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 sexp_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 sexp_state {\r
+> +    struct sexp_state *parent;\r
+> +\r
+> +    /* True if nothing has been printed in this aggregate yet.\r
+> +     * Suppresses the space before a value. */\r
+> +    notmuch_bool_t first;\r
+> +\r
+> +    /* True if the state is a map state.\r
+> +       Used to add a space between key/value pairs. */\r
+> +    notmuch_bool_t in_map;\r
+> +\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 space. */\r
+> +static struct sprinter_sexp *\r
+> +sexp_begin_value (struct sprinter *sp)\r
+> +{\r
+> +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
+> +\r
+> +    if (sps->state) {\r
+> +        if (! sps->state->first) {\r
+> +            if (sps->insert_separator) {\r
+> +                fputc ('\n', sps->stream);\r
+> +                sps->insert_separator = FALSE;\r
+> +            } else {\r
+> +                if( ! sps->state->in_map)\r
+> +                    fputc (' ', sps->stream);\r
+> +            }\r
+> +        } else {\r
+> +            sps->state->first = FALSE;\r
+> +        }\r
+> +    }\r
+> +    return sps;\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
+> +sexp_begin_aggregate (struct sprinter *sp, char open, char close)\r
+> +{\r
+> +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
+> +    struct sexp_state *state = talloc (sps, struct sexp_state);\r
+> +    fputc (open, sps->stream);\r
+> +    state->parent = sps->state;\r
+> +    state->first = TRUE;\r
+> +    state->in_map = FALSE;\r
+> +    state->close = close;\r
+> +    sps->state = state;\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_begin_map (struct sprinter *sp)\r
+> +{\r
+> +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
+> +    sexp_begin_aggregate (sp, '(', ')');\r
+> +    sps->state->in_map = TRUE;\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_begin_list (struct sprinter *sp)\r
+> +{\r
+> +    sexp_begin_aggregate (sp, '(', ')');\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_end (struct sprinter *sp)\r
+> +{\r
+> +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
+> +    struct sexp_state *state = sps->state;\r
+> +\r
+> +    if (sps->state->in_map)\r
+> +        fputc (')', sps->stream);\r
+> +    fputc (sps->state->close, sps->stream);\r
+> +    sps->state = state->parent;\r
+> +    talloc_free (state);\r
+> +    if (sps->state == NULL)\r
+> +        fputc ('\n', sps->stream);\r
+> +}\r
+> +\r
+> +/* This implementation supports embedded NULs as allowed by the JSON\r
+> + * specification and Unicode.  Support for *parsing* embedded NULs\r
+> + * varies, but is generally not a problem outside of C-based parsers\r
+> + * (Python's json module and Emacs' json.el take embedded NULs in\r
+> + * stride). */\r
+> +static void\r
+> +sexp_string_len_internal (struct sprinter *sp, const char *val, size_t len, notmuch_bool_t quote)\r
+> +{\r
+> +    static const char *const escapes[] = {\r
+> +        ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
+> +        ['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"\r
+> +    };\r
+> +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
+> +\r
+> +    if(quote)\r
+> +        fputc ('"', sps->stream);\r
+> +    for (; len; ++val, --len) {\r
+> +        unsigned char ch = *val;\r
+> +        if (ch < ARRAY_SIZE (escapes) && escapes[ch])\r
+> +            fputs (escapes[ch], sps->stream);\r
+> +        else if (ch >= 32)\r
+> +            fputc (ch, sps->stream);\r
+> +        else\r
+> +            fprintf (sps->stream, "\\u%04x", ch);\r
+> +    }\r
+> +    if(quote)\r
+> +        fputc ('"', sps->stream);\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_string_len (struct sprinter *sp, const char *val, size_t len)\r
+> +{\r
+> +    sexp_string_len_internal (sp, val, len, TRUE); /* print quoted */\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_symbol_len (struct sprinter *sp, const char *val, size_t len)\r
+> +{\r
+> +    sexp_string_len_internal (sp, val, len, FALSE); /* print unquoted */\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_string (struct sprinter *sp, const char *val)\r
+> +{\r
+> +    if (val == NULL)\r
+> +        val = "";\r
+> +    sexp_string_len (sp, val, strlen (val));\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_symbol (struct sprinter *sp, const char *val)\r
+> +{\r
+> +    if (val == NULL)\r
+> +        val = "";\r
+> +    sexp_symbol_len (sp, val, strlen (val));\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_integer (struct sprinter *sp, int val)\r
+> +{\r
+> +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
+> +\r
+> +    fprintf (sps->stream, "%d", val);\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)\r
+> +{\r
+> +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
+> +\r
+> +    fputs (val ? "t" : "nil", sps->stream);\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_null (struct sprinter *sp)\r
+> +{\r
+> +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
+> +\r
+> +    fputs ("nil", sps->stream);\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_map_key (struct sprinter *sp, const char *key)\r
+> +{\r
+> +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
+> +\r
+> +    if( sps->state->in_map && ! sps->state->first)\r
+> +        fputs (") ", sps->stream);\r
+> +    fputc ('(', sps->stream);\r
+> +    sexp_symbol (sp, key);\r
+> +    fputc (' ', sps->stream);\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))\r
+> +{\r
+> +}\r
+> +\r
+> +static void\r
+> +sexp_separator (struct sprinter *sp)\r
+> +{\r
+> +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
+> +\r
+> +    sps->insert_separator = TRUE;\r
+> +}\r
+> +\r
+> +struct sprinter *\r
+> +sprinter_sexp_create (const void *ctx, FILE *stream)\r
+> +{\r
+> +    static const struct sprinter_sexp template = {\r
+> +        .vtable = {\r
+> +            .begin_map = sexp_begin_map,\r
+> +            .begin_list = sexp_begin_list,\r
+> +            .end = sexp_end,\r
+> +            .string = sexp_string,\r
+> +            .string_len = sexp_string_len,\r
+> +            .integer = sexp_integer,\r
+> +            .boolean = sexp_boolean,\r
+> +            .null = sexp_null,\r
+> +            .map_key = sexp_map_key,\r
+> +            .separator = sexp_separator,\r
+> +            .set_prefix = sexp_set_prefix,\r
+> +            .is_text_printer = FALSE,\r
+> +        }\r
+> +    };\r
+> +    struct sprinter_sexp *res;\r
+> +\r
+> +    res = talloc (ctx, struct sprinter_sexp);\r
+> +    if (! res)\r
+> +        return NULL;\r
+> +\r
+> +    *res = template;\r
+> +    res->stream = stream;\r
+> +    return &res->vtable;\r
+> +}\r
+> -- \r
+> 1.8.0\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r