[PATCH v3 1/5] Adding an S-expression structured output printer.
authorPeter Feigl <craven@gmx.net>
Thu, 6 Dec 2012 07:33:05 +0000 (08:33 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:51:33 +0000 (09:51 -0800)
61/47f6de49c879b9fca064015df5aeb67de23ace [new file with mode: 0644]

diff --git a/61/47f6de49c879b9fca064015df5aeb67de23ace b/61/47f6de49c879b9fca064015df5aeb67de23ace
new file mode 100644 (file)
index 0000000..cf494e6
--- /dev/null
@@ -0,0 +1,340 @@
+Return-Path: <nex@nexoid.at>\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 DD1C5429E2F\r
+       for <notmuch@notmuchmail.org>; Wed,  5 Dec 2012 23:33:28 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.001\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.001 tagged_above=-999 required=5\r
+       tests=[FREEMAIL_FROM=0.001] 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 jxEwh6i0UIeA for <notmuch@notmuchmail.org>;\r
+       Wed,  5 Dec 2012 23:33:24 -0800 (PST)\r
+Received: from mail.nexoid.at (www.nexoid.at [178.79.130.240])\r
+       (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 11484431FC4\r
+       for <notmuch@notmuchmail.org>; Wed,  5 Dec 2012 23:33:19 -0800 (PST)\r
+Received: by mail.nexoid.at (Postfix, from userid 1000)\r
+       id A1D3511C10B; Thu,  6 Dec 2012 08:33:12 +0100 (CET)\r
+From: Peter Feigl <craven@gmx.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 1/5] Adding an S-expression structured output printer.\r
+Date: Thu,  6 Dec 2012 08:33:05 +0100\r
+Message-Id: <1354779189-12231-2-git-send-email-craven@gmx.net>\r
+X-Mailer: git-send-email 1.8.0\r
+In-Reply-To: <1354779189-12231-1-git-send-email-craven@gmx.net>\r
+References: <1354779189-12231-1-git-send-email-craven@gmx.net>\r
+In-Reply-To: <1354632382-15609-1-git-send-email-craven@gmx.net>\r
+References: <1354632382-15609-1-git-send-email-craven@gmx.net>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\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, 06 Dec 2012 07:33:29 -0000\r
+\r
+This commit adds a structured output printer for Lisp\r
+S-Expressions. Later commits will use this printer in notmuch search,\r
+show and reply.\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 p-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 | 238 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ sprinter.h      |   4 +\r
+ 3 files changed, 243 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..8f84eed\r
+--- /dev/null\r
++++ b/sprinter-sexp.c\r
+@@ -0,0 +1,238 @@\r
++/* notmuch - Not much of an email program, (just index and search)\r
++ *\r
++ * Copyright © 2012 Peter Feigl\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 3 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
++ *\r
++ * Author: Peter Feigl <peter.feigl@gmx.at>\r
++ */\r
++\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
++    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
++\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
++              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)\r
++{\r
++    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
++    struct sexp_state *state = talloc (sps, struct sexp_state);\r
++    fputc ('(', sps->stream);\r
++    state->parent = sps->state;\r
++    state->first = TRUE;\r
++    sps->state = state;\r
++}\r
++\r
++static void\r
++sexp_begin_map (struct sprinter *sp)\r
++{\r
++    sexp_begin_aggregate (sp);\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
++    fputc (')', 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_string_len (struct sprinter *sp, const char *val, size_t len)\r
++{\r
++    /* Some characters need escaping. " and \ work fine in all Lisps,\r
++     * \n is not supported in CL, but all others work fine.\r
++     * Characters below 32 are printed as \123o (three-digit \r
++     * octals), which work fine in most Schemes and Emacs. */\r
++    static const char *const escapes[] = {\r
++      ['\"'] = "\\\"", ['\\'] = "\\\\",  ['\n'] = "\\n"\r
++    };\r
++    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
++\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, "\\%03oo", ch);\r
++    }\r
++    fputc ('"', sps->stream);\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
++/* Prints a symbol, i.e. the name preceded by a colon. This should work\r
++ * in all Lisps, at least as a symbol, if not as a proper keyword */\r
++static void\r
++sexp_symbol (struct sprinter *sp, const char *val)\r
++{\r
++    static const char illegal_characters[] = {\r
++      ' ', '\t', '\n'\r
++    };\r
++    unsigned int i = 0;\r
++    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
++\r
++    if (val == NULL)\r
++      INTERNAL_ERROR ("illegal symbol NULL");\r
++\r
++    for(i = 0; i < ARRAY_SIZE (illegal_characters); i++) {\r
++      if(strchr(val, illegal_characters[i]) != NULL) {\r
++          INTERNAL_ERROR ("illegal character in symbol %s", val);\r
++      }\r
++    }\r
++    fputc (':', sps->stream);\r
++    fputs (val, sps->stream);\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
++    if (sps->state && ! sps->state->first)\r
++      fputc (' ', sps->stream);\r
++\r
++    sps->state->first = FALSE;\r
++    sexp_symbol (sp, key);\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
+diff --git a/sprinter.h b/sprinter.h\r
+index 912a526..59776a9 100644\r
+--- a/sprinter.h\r
++++ b/sprinter.h\r
+@@ -70,4 +70,8 @@ sprinter_text_create (const void *ctx, FILE *stream);\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
++\r
+ #endif // NOTMUCH_SPRINTER_H\r
+-- \r
+1.8.0\r
+\r