1 Return-Path: <nex@nexoid.at>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id 17E8F431FC0
\r
6 for <notmuch@notmuchmail.org>; Thu, 6 Dec 2012 13:12:36 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=0.001 tagged_above=-999 required=5
\r
12 tests=[FREEMAIL_FROM=0.001] autolearn=disabled
\r
13 Received: from olra.theworths.org ([127.0.0.1])
\r
14 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id w17tk12DHJ6n for <notmuch@notmuchmail.org>;
\r
16 Thu, 6 Dec 2012 13:12:33 -0800 (PST)
\r
17 Received: from mail.nexoid.at (www.nexoid.at [178.79.130.240])
\r
18 (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
\r
19 (No client certificate requested)
\r
20 by olra.theworths.org (Postfix) with ESMTPS id 75589431FB6
\r
21 for <notmuch@notmuchmail.org>; Thu, 6 Dec 2012 13:12:29 -0800 (PST)
\r
22 Received: by mail.nexoid.at (Postfix, from userid 1000)
\r
23 id 44A4E11C110; Thu, 6 Dec 2012 22:12:23 +0100 (CET)
\r
24 From: Peter Feigl <craven@gmx.net>
\r
25 To: notmuch@notmuchmail.org
\r
26 Subject: [PATCH v5 1/5] Adding an S-expression structured output printer.
\r
27 Date: Thu, 6 Dec 2012 22:12:11 +0100
\r
28 Message-Id: <1354828335-18151-2-git-send-email-craven@gmx.net>
\r
29 X-Mailer: git-send-email 1.8.0
\r
30 In-Reply-To: <1354828335-18151-1-git-send-email-craven@gmx.net>
\r
31 References: <1354828335-18151-1-git-send-email-craven@gmx.net>
\r
33 Content-Type: text/plain; charset=UTF-8
\r
34 Content-Transfer-Encoding: 8bit
\r
35 X-BeenThere: notmuch@notmuchmail.org
\r
36 X-Mailman-Version: 2.1.13
\r
38 List-Id: "Use and development of the notmuch mail system."
\r
39 <notmuch.notmuchmail.org>
\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
41 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
43 List-Post: <mailto:notmuch@notmuchmail.org>
\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
46 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
47 X-List-Received-Date: Thu, 06 Dec 2012 21:12:36 -0000
\r
49 This commit adds a structured output printer for Lisp
\r
50 S-Expressions. Later commits will use this printer in notmuch search,
\r
53 The structure is the same as json, but:
\r
54 - arrays are written as lists: ("foo" "bar" "baaz" 1 2 3)
\r
55 - maps are written as p-lists: (:key "value" :other-key "other-value")
\r
56 - true is written as t
\r
57 - false is written as nil
\r
58 - null is written as nil
\r
60 Makefile.local | 1 +
\r
61 sprinter-sexp.c | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
63 3 files changed, 240 insertions(+)
\r
64 create mode 100644 sprinter-sexp.c
\r
66 diff --git a/Makefile.local b/Makefile.local
\r
67 index 2b91946..0db1713 100644
\r
68 --- a/Makefile.local
\r
69 +++ b/Makefile.local
\r
70 @@ -270,6 +270,7 @@ notmuch_client_srcs = \
\r
78 diff --git a/sprinter-sexp.c b/sprinter-sexp.c
\r
79 new file mode 100644
\r
80 index 0000000..06856f4
\r
82 +++ b/sprinter-sexp.c
\r
84 +/* notmuch - Not much of an email program, (just index and search)
\r
86 + * Copyright © 2012 Peter Feigl
\r
88 + * This program is free software: you can redistribute it and/or modify
\r
89 + * it under the terms of the GNU General Public License as published by
\r
90 + * the Free Software Foundation, either version 3 of the License, or
\r
91 + * (at your option) any later version.
\r
93 + * This program is distributed in the hope that it will be useful,
\r
94 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
95 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
96 + * GNU General Public License for more details.
\r
98 + * You should have received a copy of the GNU General Public License
\r
99 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
101 + * Author: Peter Feigl <peter.feigl@gmx.at>
\r
104 +#include <stdbool.h>
\r
105 +#include <stdio.h>
\r
106 +#include <talloc.h>
\r
107 +#include "sprinter.h"
\r
108 +#include <ctype.h>
\r
110 +struct sprinter_sexp {
\r
111 + struct sprinter vtable;
\r
113 + /* Top of the state stack, or NULL if the printer is not currently
\r
114 + * inside any aggregate types. */
\r
115 + struct sexp_state *state;
\r
117 + /* A flag to signify that a separator should be inserted in the
\r
118 + * output as soon as possible. */
\r
119 + notmuch_bool_t insert_separator;
\r
122 +struct sexp_state {
\r
123 + struct sexp_state *parent;
\r
125 + /* True if nothing has been printed in this aggregate yet.
\r
126 + * Suppresses the space before a value. */
\r
127 + notmuch_bool_t first;
\r
130 +/* Helper function to set up the stream to print a value. If this
\r
131 + * value follows another value, prints a space. */
\r
132 +static struct sprinter_sexp *
\r
133 +sexp_begin_value (struct sprinter *sp)
\r
135 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
137 + if (sps->state) {
\r
138 + if (! sps->state->first) {
\r
139 + if (sps->insert_separator) {
\r
140 + fputc ('\n', sps->stream);
\r
141 + sps->insert_separator = FALSE;
\r
143 + fputc (' ', sps->stream);
\r
146 + sps->state->first = FALSE;
\r
152 +/* Helper function to begin an aggregate type. Prints the open
\r
153 + * character and pushes a new state frame. */
\r
155 +sexp_begin_aggregate (struct sprinter *sp)
\r
157 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
158 + struct sexp_state *state = talloc (sps, struct sexp_state);
\r
159 + fputc ('(', sps->stream);
\r
160 + state->parent = sps->state;
\r
161 + state->first = TRUE;
\r
162 + sps->state = state;
\r
166 +sexp_begin_map (struct sprinter *sp)
\r
168 + sexp_begin_aggregate (sp);
\r
172 +sexp_begin_list (struct sprinter *sp)
\r
174 + sexp_begin_aggregate (sp);
\r
178 +sexp_end (struct sprinter *sp)
\r
180 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
181 + struct sexp_state *state = sps->state;
\r
183 + fputc (')', sps->stream);
\r
184 + sps->state = state->parent;
\r
185 + talloc_free (state);
\r
186 + if (sps->state == NULL)
\r
187 + fputc ('\n', sps->stream);
\r
191 +sexp_string_len (struct sprinter *sp, const char *val, size_t len)
\r
193 + /* Some characters need escaping. " and \ work fine in all Lisps,
\r
194 + * \n is not supported in CL, but all others work fine.
\r
195 + * Characters below 32 are printed as \123o (three-digit
\r
196 + * octals), which work fine in most Schemes and Emacs. */
\r
197 + static const char *const escapes[] = {
\r
198 + ['\"'] = "\\\"", ['\\'] = "\\\\", ['\n'] = "\\n"
\r
200 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
202 + fputc ('"', sps->stream);
\r
203 + for (; len; ++val, --len) {
\r
204 + unsigned char ch = *val;
\r
205 + if (ch < ARRAY_SIZE (escapes) && escapes[ch])
\r
206 + fputs (escapes[ch], sps->stream);
\r
207 + else if (ch >= 32)
\r
208 + fputc (ch, sps->stream);
\r
210 + fprintf (sps->stream, "\\%03o", ch);
\r
212 + fputc ('"', sps->stream);
\r
216 +sexp_string (struct sprinter *sp, const char *val)
\r
220 + sexp_string_len (sp, val, strlen (val));
\r
223 +/* Prints a symbol, i.e. the name preceded by a colon. This should work
\r
224 + * in all Lisps, at least as a symbol, if not as a proper keyword */
\r
226 +sexp_keyword (struct sprinter *sp, const char *val)
\r
228 + unsigned int i = 0;
\r
229 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
233 + INTERNAL_ERROR ("illegal symbol NULL");
\r
235 + for (i = 0; i < strlen (val); i++) {
\r
237 + if (! (isalnum(ch) || (ch == '-') || (ch == '_'))) {
\r
238 + INTERNAL_ERROR ("illegal character in symbol %s: %c", val, ch);
\r
241 + fputc (':', sps->stream);
\r
242 + fputs (val, sps->stream);
\r
246 +sexp_integer (struct sprinter *sp, int val)
\r
248 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
250 + fprintf (sps->stream, "%d", val);
\r
254 +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)
\r
256 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
258 + fputs (val ? "t" : "nil", sps->stream);
\r
262 +sexp_null (struct sprinter *sp)
\r
264 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
266 + fputs ("nil", sps->stream);
\r
270 +sexp_map_key (struct sprinter *sp, const char *key)
\r
272 + sexp_begin_value (sp);
\r
274 + sexp_keyword (sp, key);
\r
278 +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))
\r
283 +sexp_separator (struct sprinter *sp)
\r
285 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
287 + sps->insert_separator = TRUE;
\r
291 +sprinter_sexp_create (const void *ctx, FILE *stream)
\r
293 + static const struct sprinter_sexp template = {
\r
295 + .begin_map = sexp_begin_map,
\r
296 + .begin_list = sexp_begin_list,
\r
298 + .string = sexp_string,
\r
299 + .string_len = sexp_string_len,
\r
300 + .integer = sexp_integer,
\r
301 + .boolean = sexp_boolean,
\r
302 + .null = sexp_null,
\r
303 + .map_key = sexp_map_key,
\r
304 + .separator = sexp_separator,
\r
305 + .set_prefix = sexp_set_prefix,
\r
306 + .is_text_printer = FALSE,
\r
309 + struct sprinter_sexp *res;
\r
311 + res = talloc (ctx, struct sprinter_sexp);
\r
316 + res->stream = stream;
\r
317 + return &res->vtable;
\r
319 diff --git a/sprinter.h b/sprinter.h
\r
320 index 912a526..59776a9 100644
\r
323 @@ -70,4 +70,8 @@ sprinter_text_create (const void *ctx, FILE *stream);
\r
325 sprinter_json_create (const void *ctx, FILE *stream);
\r
327 +/* Create a new structure printer that emits S-Expressions. */
\r
329 +sprinter_sexp_create (const void *ctx, FILE *stream);
\r
331 #endif // NOTMUCH_SPRINTER_H
\r