Re: [PATCH 1/3] Adding an S-expression structured output printer.
authorMark Walters <markwalters1009@gmail.com>
Sat, 1 Dec 2012 14:26:46 +0000 (14:26 +0000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:51:16 +0000 (09:51 -0800)
20/16cd86500f09f73ce7f5aebaf0701eebc2c7f5 [new file with mode: 0644]

diff --git a/20/16cd86500f09f73ce7f5aebaf0701eebc2c7f5 b/20/16cd86500f09f73ce7f5aebaf0701eebc2c7f5
new file mode 100644 (file)
index 0000000..d11a492
--- /dev/null
@@ -0,0 +1,340 @@
+Return-Path: <m.walters@qmul.ac.uk>\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 D5A56431FAF\r
+       for <notmuch@notmuchmail.org>; Sat,  1 Dec 2012 06:26:54 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.098\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
+       tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+       NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] 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 kPv0EXB5WJNN for <notmuch@notmuchmail.org>;\r
+       Sat,  1 Dec 2012 06:26:51 -0800 (PST)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 87E30431FBC\r
+       for <notmuch@notmuchmail.org>; Sat,  1 Dec 2012 06:26:50 -0800 (PST)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+       by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1Teo1m-0005fa-Lc; Sat, 01 Dec 2012 14:26:47 +0000\r
+Received: from 93-97-24-31.zone5.bethere.co.uk ([93.97.24.31] helo=localhost)\r
+       by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1Teo1m-0002dU-3I; Sat, 01 Dec 2012 14:26:46 +0000\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Tomi Ollila <tomi.ollila@iki.fi>, Peter Feigl <craven@gmx.net>,\r
+       notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 1/3] Adding an S-expression structured output printer.\r
+In-Reply-To: <874nk5ansj.fsf@qmul.ac.uk>\r
+References: <1354264143-30173-1-git-send-email-craven@gmx.net>\r
+       <871ufa9jpm.fsf@qmul.ac.uk> <m2txs6vwdy.fsf@guru.guru-group.fi>\r
+       <87d2yu559y.fsf@qmul.ac.uk> <m2fw3pq4sf.fsf@guru.guru-group.fi>\r
+       <874nk5ansj.fsf@qmul.ac.uk>\r
+User-Agent: Notmuch/0.14+81~g9730584 (http://notmuchmail.org) Emacs/23.4.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Sat, 01 Dec 2012 14:26:46 +0000\r
+Message-ID: <87k3t1etll.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Sender-Host-Address: 93.97.24.31\r
+X-QM-SPAM-Info: Sender has good ham record.  :)\r
+X-QM-Body-MD5: 6402eb8e03afa4bd0b3f006e5504ea54 (of first 20000 bytes)\r
+X-SpamAssassin-Score: -1.7\r
+X-SpamAssassin-SpamBar: -\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+       determine if it is\r
+       spam. We require at least 5.0 points to mark a message as spam.\r
+       This message scored -1.7 points.\r
+       Summary of the scoring: \r
+       * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,\r
+       *      medium trust\r
+       *      [138.37.6.40 listed in list.dnswl.org]\r
+       * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+       provider *      (markwalters1009[at]gmail.com)\r
+       *  0.6 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\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: Sat, 01 Dec 2012 14:26:55 -0000\r
+\r
+On Sat, 01 Dec 2012, Mark Walters <markwalters1009@gmail.com> wrote:\r
+> On Sat, 01 Dec 2012, Tomi Ollila <tomi.ollila@iki.fi> wrote:\r
+>> On Sat, Dec 01 2012, Mark Walters <markwalters1009@gmail.com> wrote:\r
+>>\r
+>>> On Sat, 01 Dec 2012, Tomi Ollila <tomi.ollila@iki.fi> wrote:\r
+>>>> On Sat, Dec 01 2012, Mark Walters wrote:\r
+>>>>\r
+>>>>> Hi\r
+>>>>>\r
+>>>>> Overall I like the series: I think I agree with all of Jani's\r
+>>>>> comments. \r
+>>>>>\r
+>>>>> My one extra comment is that I think we should decide on whether we also\r
+>>>>> want a sexp plist version. I think we might want one for the emacs\r
+>>>>> front-end as that currently uses plists for everything.\r
+>>>>>\r
+>>>>> If we do we might want to change the names a little, both for functions\r
+>>>>> and options (eg sexp_a and sexp_p or something). Probably a lot of\r
+>>>>> sprinter-sexp would be common to both versions.\r
+>>>>\r
+>>>> This is an important question that needs to be addressed fast: options\r
+>>>> are:\r
+>>>>\r
+>>>> 1) have options to spit both alist & plist formats\r
+>>>> 2) when converting emacs to use s-expressions, convert it to use alists\r
+>>>> 3) start using plists instead of alists in Peter's android client\r
+>>>\r
+>>> Ok I have looked at this and the changes needed to output plist (or\r
+>>> both) are pretty small: the only functions from sprinter-sexp.c that\r
+>>> need to be changed are sexp_end and sexp_map_key. The total diff from\r
+>>> alist to plist is about 10 lines. I have a version which allows both\r
+>>> (the same sprinter file creates both possibilities) and have hooked it\r
+>>> into emacs/notmuch-show.el and it all seems to work. \r
+>>>\r
+>>> (Search is more difficult as that uses the async parser; indeed even for\r
+>>> show I used sexp-at-point as suggested by Tomi which seems rather\r
+>>> underdocumented but does seem to work)\r
+>>>\r
+>>> Given the ease with which we can allow both I think that would be my\r
+>>> preference: the biggest problem is that slightly more cluttered option\r
+>>> list (i.e., we have to allow both --format=sexpa and --format=sexpp or\r
+>>> similar).\r
+>>>\r
+>>> (I can post the patch doing the above but almost all of it is modifying the\r
+>>> commands to choose alist or plist rather than modifying the\r
+>>> sprinter-sexp itself)\r
+>>\r
+>> As the diff is so small I agree that supporting 2 formats is good\r
+>> option.\r
+>>\r
+>> In case this is done I suggest that we proceed the following way:\r
+>>\r
+>> 1) Agree how we call these formats (sexpa & sexpp or something else)\r
+>> 2) Peter does his updates, including to call the format as will be decided\r
+>> 3) Mark posts his patches after Peter's work is pushed\r
+>\r
+> I think this seems a good approach. I should note that while the diff\r
+> between alist and plist is only about 10 lines I do need to duplicate\r
+> some of the surrounding code to allow both: its not huge but definitely\r
+> more than 10 lines. \r
+>\r
+> I will clean it up a little and then post (as a diff on top of Peter's\r
+> patches) just so people can judge if it looks basically acceptable.\r
+\r
+Here is my current diff: note this is just the sprinter diff: it does\r
+not contain the plumbing into the cli functions. The largest part is the\r
+duplicated create function: I couldn't persuade the compiler to let me\r
+unify this (as the static const thing needs to be constant)\r
+\r
+The patch is -U6 as this makes it easier to see what is going on.\r
+\r
+Best wishes\r
+\r
+Mark\r
+\r
+---\r
+ sprinter-sexp.c |   75 +++++++++++++++++++++++++++++++++++++++++++++++++-----\r
+ sprinter.h      |    6 +++-\r
+ 2 files changed, 73 insertions(+), 8 deletions(-)\r
+\r
+diff --git a/sprinter-sexp.c b/sprinter-sexp.c\r
+index 8401c52..51bab68 100644\r
+--- a/sprinter-sexp.c\r
++++ b/sprinter-sexp.c\r
+@@ -1,11 +1,16 @@\r
+ #include <stdbool.h>\r
+ #include <stdio.h>\r
+ #include <talloc.h>\r
+ #include "sprinter.h"\r
\r
++typedef enum {\r
++    SEXP_ALIST,\r
++    SEXP_PLIST\r
++} sexp_list_t;\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
+@@ -81,26 +86,39 @@ 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
++sexp_end (struct sprinter *sp, sexp_list_t list_type)\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
++    if (list_type == SEXP_ALIST)\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
++static void\r
++sexp_end_alist (struct sprinter *sp)\r
++{\r
++    sexp_end(sp, SEXP_ALIST);\r
++}\r
++\r
++static void\r
++sexp_end_plist (struct sprinter *sp)\r
++{\r
++    sexp_end(sp, SEXP_PLIST);\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
+@@ -177,24 +195,36 @@ sexp_null (struct sprinter *sp)\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
++sexp_map_key_alist (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_map_key_plist (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
+@@ -202,25 +232,56 @@ sexp_separator (struct sprinter *sp)\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
++sprinter_sexp_alist_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_alist,\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_alist,\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
++\r
++struct sprinter *\r
++sprinter_sexp_plist_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
++            .end = sexp_end_plist,\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
++            .map_key = sexp_map_key_plist,\r
+             .separator = sexp_separator,\r
+             .set_prefix = sexp_set_prefix,\r
+             .is_text_printer = FALSE,\r
+         }\r
+     };\r
+     struct sprinter_sexp *res;\r
+diff --git a/sprinter.h b/sprinter.h\r
+index 59776a9..9a496cd 100644\r
+--- a/sprinter.h\r
++++ b/sprinter.h\r
+@@ -69,9 +69,13 @@ sprinter_text_create (const void *ctx, FILE *stream);\r
+ /* Create a new structure printer that emits JSON. */\r
+ struct sprinter *\r
+ sprinter_json_create (const void *ctx, FILE *stream);\r
\r
+ /* Create a new structure printer that emits S-Expressions. */\r
+ struct sprinter *\r
+-sprinter_sexp_create (const void *ctx, FILE *stream);\r
++sprinter_sexp_alist_create (const void *ctx, FILE *stream);\r
++\r
++/* Create a new structure printer that emits S-Expressions. */\r
++struct sprinter *\r
++sprinter_sexp_plist_create (const void *ctx, FILE *stream);\r
\r
+ #endif // NOTMUCH_SPRINTER_H\r
+-- \r
+1.7.9.1\r
+\r