1 Return-Path: <m.walters@qmul.ac.uk>
\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 0A6AA431FBC
\r
6 for <notmuch@notmuchmail.org>; Sat, 1 Dec 2012 08:14:54 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5
\r
12 tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,
\r
13 NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled
\r
14 Received: from olra.theworths.org ([127.0.0.1])
\r
15 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id r9MEK1+-76UV for <notmuch@notmuchmail.org>;
\r
17 Sat, 1 Dec 2012 08:14:50 -0800 (PST)
\r
18 Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])
\r
19 (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
\r
20 (No client certificate requested)
\r
21 by olra.theworths.org (Postfix) with ESMTPS id C13BA431FAF
\r
22 for <notmuch@notmuchmail.org>; Sat, 1 Dec 2012 08:14:49 -0800 (PST)
\r
23 Received: from smtp.qmul.ac.uk ([138.37.6.40])
\r
24 by mail2.qmul.ac.uk with esmtp (Exim 4.71)
\r
25 (envelope-from <m.walters@qmul.ac.uk>)
\r
26 id 1TepiI-0003yf-19; Sat, 01 Dec 2012 16:14:48 +0000
\r
27 Received: from 93-97-24-31.zone5.bethere.co.uk ([93.97.24.31] helo=localhost)
\r
28 by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)
\r
29 (envelope-from <m.walters@qmul.ac.uk>)
\r
30 id 1TepiH-00049c-EH; Sat, 01 Dec 2012 16:14:45 +0000
\r
31 From: Mark Walters <markwalters1009@gmail.com>
\r
32 To: Peter Feigl <craven@gmx.net>, notmuch@notmuchmail.org
\r
33 Subject: Re: [PATCH 1/3] Adding an S-expression structured output printer.
\r
34 In-Reply-To: <1354264143-30173-1-git-send-email-craven@gmx.net>
\r
35 References: <1354264143-30173-1-git-send-email-craven@gmx.net>
\r
36 User-Agent: Notmuch/0.14+81~g9730584 (http://notmuchmail.org) Emacs/23.4.1
\r
37 (x86_64-pc-linux-gnu)
\r
38 Date: Sat, 01 Dec 2012 16:14:44 +0000
\r
39 Message-ID: <87sj7p3g23.fsf@qmul.ac.uk>
\r
41 Content-Type: text/plain; charset=us-ascii
\r
42 X-Sender-Host-Address: 93.97.24.31
\r
43 X-QM-SPAM-Info: Sender has good ham record. :)
\r
44 X-QM-Body-MD5: 4787153bdec301a1bb606d636b5488a5 (of first 20000 bytes)
\r
45 X-SpamAssassin-Score: -1.7
\r
46 X-SpamAssassin-SpamBar: -
\r
47 X-SpamAssassin-Report: The QM spam filters have analysed this message to
\r
49 spam. We require at least 5.0 points to mark a message as spam.
\r
50 This message scored -1.7 points.
\r
51 Summary of the scoring:
\r
52 * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,
\r
54 * [138.37.6.40 listed in list.dnswl.org]
\r
55 * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail
\r
56 provider * (markwalters1009[at]gmail.com)
\r
57 * 0.6 AWL AWL: From: address is in the auto white-list
\r
58 X-QM-Scan-Virus: ClamAV says the message is clean
\r
59 X-BeenThere: notmuch@notmuchmail.org
\r
60 X-Mailman-Version: 2.1.13
\r
62 List-Id: "Use and development of the notmuch mail system."
\r
63 <notmuch.notmuchmail.org>
\r
64 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
65 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
66 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
67 List-Post: <mailto:notmuch@notmuchmail.org>
\r
68 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
69 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
70 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
71 X-List-Received-Date: Sat, 01 Dec 2012 16:14:54 -0000
\r
74 One small niggle with this patch: it seems to have lots of spaces rather
\r
75 than tabs (unless I am misreading it)
\r
81 On Fri, 30 Nov 2012, Peter Feigl <craven@gmx.net> wrote:
\r
82 > This commit adds an sprinter for Lisp S-Expressions. Later commits will
\r
85 > The structure is the same as json, but:
\r
86 > - arrays are written as lists: ("foo" "bar" "baaz" 1 2 3)
\r
87 > - maps are written as a-lists: ((key "value") (other-key "other-value"))
\r
88 > - true is written as t
\r
89 > - false is written as nil
\r
90 > - null is written as nil
\r
92 > Makefile.local | 1 +
\r
93 > sprinter-sexp.c | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
94 > 2 files changed, 236 insertions(+)
\r
95 > create mode 100644 sprinter-sexp.c
\r
97 > diff --git a/Makefile.local b/Makefile.local
\r
98 > index 2b91946..0db1713 100644
\r
99 > --- a/Makefile.local
\r
100 > +++ b/Makefile.local
\r
101 > @@ -270,6 +270,7 @@ notmuch_client_srcs = \
\r
104 > sprinter-json.c \
\r
105 > + sprinter-sexp.c \
\r
106 > sprinter-text.c \
\r
109 > diff --git a/sprinter-sexp.c b/sprinter-sexp.c
\r
110 > new file mode 100644
\r
111 > index 0000000..8401c52
\r
113 > +++ b/sprinter-sexp.c
\r
114 > @@ -0,0 +1,235 @@
\r
115 > +#include <stdbool.h>
\r
116 > +#include <stdio.h>
\r
117 > +#include <talloc.h>
\r
118 > +#include "sprinter.h"
\r
120 > +struct sprinter_sexp {
\r
121 > + struct sprinter vtable;
\r
123 > + /* Top of the state stack, or NULL if the printer is not currently
\r
124 > + * inside any aggregate types. */
\r
125 > + struct sexp_state *state;
\r
127 > + /* A flag to signify that a separator should be inserted in the
\r
128 > + * output as soon as possible.
\r
130 > + notmuch_bool_t insert_separator;
\r
133 > +struct sexp_state {
\r
134 > + struct sexp_state *parent;
\r
136 > + /* True if nothing has been printed in this aggregate yet.
\r
137 > + * Suppresses the space before a value. */
\r
138 > + notmuch_bool_t first;
\r
140 > + /* True if the state is a map state.
\r
141 > + Used to add a space between key/value pairs. */
\r
142 > + notmuch_bool_t in_map;
\r
144 > + /* The character that closes the current aggregate. */
\r
148 > +/* Helper function to set up the stream to print a value. If this
\r
149 > + * value follows another value, prints a space. */
\r
150 > +static struct sprinter_sexp *
\r
151 > +sexp_begin_value (struct sprinter *sp)
\r
153 > + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
155 > + if (sps->state) {
\r
156 > + if (! sps->state->first) {
\r
157 > + if (sps->insert_separator) {
\r
158 > + fputc ('\n', sps->stream);
\r
159 > + sps->insert_separator = FALSE;
\r
161 > + if( ! sps->state->in_map)
\r
162 > + fputc (' ', sps->stream);
\r
165 > + sps->state->first = FALSE;
\r
171 > +/* Helper function to begin an aggregate type. Prints the open
\r
172 > + * character and pushes a new state frame. */
\r
174 > +sexp_begin_aggregate (struct sprinter *sp, char open, char close)
\r
176 > + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
177 > + struct sexp_state *state = talloc (sps, struct sexp_state);
\r
178 > + fputc (open, sps->stream);
\r
179 > + state->parent = sps->state;
\r
180 > + state->first = TRUE;
\r
181 > + state->in_map = FALSE;
\r
182 > + state->close = close;
\r
183 > + sps->state = state;
\r
187 > +sexp_begin_map (struct sprinter *sp)
\r
189 > + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
190 > + sexp_begin_aggregate (sp, '(', ')');
\r
191 > + sps->state->in_map = TRUE;
\r
195 > +sexp_begin_list (struct sprinter *sp)
\r
197 > + sexp_begin_aggregate (sp, '(', ')');
\r
201 > +sexp_end (struct sprinter *sp)
\r
203 > + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
204 > + struct sexp_state *state = sps->state;
\r
206 > + if (sps->state->in_map)
\r
207 > + fputc (')', sps->stream);
\r
208 > + fputc (sps->state->close, sps->stream);
\r
209 > + sps->state = state->parent;
\r
210 > + talloc_free (state);
\r
211 > + if (sps->state == NULL)
\r
212 > + fputc ('\n', sps->stream);
\r
215 > +/* This implementation supports embedded NULs as allowed by the JSON
\r
216 > + * specification and Unicode. Support for *parsing* embedded NULs
\r
217 > + * varies, but is generally not a problem outside of C-based parsers
\r
218 > + * (Python's json module and Emacs' json.el take embedded NULs in
\r
221 > +sexp_string_len_internal (struct sprinter *sp, const char *val, size_t len, notmuch_bool_t quote)
\r
223 > + static const char *const escapes[] = {
\r
224 > + ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",
\r
225 > + ['\f'] = "\\f", ['\n'] = "\\n", ['\t'] = "\\t"
\r
227 > + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
230 > + fputc ('"', sps->stream);
\r
231 > + for (; len; ++val, --len) {
\r
232 > + unsigned char ch = *val;
\r
233 > + if (ch < ARRAY_SIZE (escapes) && escapes[ch])
\r
234 > + fputs (escapes[ch], sps->stream);
\r
235 > + else if (ch >= 32)
\r
236 > + fputc (ch, sps->stream);
\r
238 > + fprintf (sps->stream, "\\u%04x", ch);
\r
241 > + fputc ('"', sps->stream);
\r
245 > +sexp_string_len (struct sprinter *sp, const char *val, size_t len)
\r
247 > + sexp_string_len_internal (sp, val, len, TRUE); /* print quoted */
\r
251 > +sexp_symbol_len (struct sprinter *sp, const char *val, size_t len)
\r
253 > + sexp_string_len_internal (sp, val, len, FALSE); /* print unquoted */
\r
257 > +sexp_string (struct sprinter *sp, const char *val)
\r
259 > + if (val == NULL)
\r
261 > + sexp_string_len (sp, val, strlen (val));
\r
265 > +sexp_symbol (struct sprinter *sp, const char *val)
\r
267 > + if (val == NULL)
\r
269 > + sexp_symbol_len (sp, val, strlen (val));
\r
273 > +sexp_integer (struct sprinter *sp, int val)
\r
275 > + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
277 > + fprintf (sps->stream, "%d", val);
\r
281 > +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)
\r
283 > + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
285 > + fputs (val ? "t" : "nil", sps->stream);
\r
289 > +sexp_null (struct sprinter *sp)
\r
291 > + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
293 > + fputs ("nil", sps->stream);
\r
297 > +sexp_map_key (struct sprinter *sp, const char *key)
\r
299 > + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
301 > + if( sps->state->in_map && ! sps->state->first)
\r
302 > + fputs (") ", sps->stream);
\r
303 > + fputc ('(', sps->stream);
\r
304 > + sexp_symbol (sp, key);
\r
305 > + fputc (' ', sps->stream);
\r
309 > +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))
\r
314 > +sexp_separator (struct sprinter *sp)
\r
316 > + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
318 > + sps->insert_separator = TRUE;
\r
321 > +struct sprinter *
\r
322 > +sprinter_sexp_create (const void *ctx, FILE *stream)
\r
324 > + static const struct sprinter_sexp template = {
\r
326 > + .begin_map = sexp_begin_map,
\r
327 > + .begin_list = sexp_begin_list,
\r
328 > + .end = sexp_end,
\r
329 > + .string = sexp_string,
\r
330 > + .string_len = sexp_string_len,
\r
331 > + .integer = sexp_integer,
\r
332 > + .boolean = sexp_boolean,
\r
333 > + .null = sexp_null,
\r
334 > + .map_key = sexp_map_key,
\r
335 > + .separator = sexp_separator,
\r
336 > + .set_prefix = sexp_set_prefix,
\r
337 > + .is_text_printer = FALSE,
\r
340 > + struct sprinter_sexp *res;
\r
342 > + res = talloc (ctx, struct sprinter_sexp);
\r
346 > + *res = template;
\r
347 > + res->stream = stream;
\r
348 > + return &res->vtable;
\r
353 > _______________________________________________
\r
354 > notmuch mailing list
\r
355 > notmuch@notmuchmail.org
\r
356 > http://notmuchmail.org/mailman/listinfo/notmuch
\r