1 Return-Path: <tomi.ollila@iki.fi>
\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 E34D2431FBC
\r
6 for <notmuch@notmuchmail.org>; Thu, 6 Dec 2012 07:02:26 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]
\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 kHGZk0VnDRSw for <notmuch@notmuchmail.org>;
\r
16 Thu, 6 Dec 2012 07:02:24 -0800 (PST)
\r
17 Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])
\r
18 by olra.theworths.org (Postfix) with ESMTP id 82C37431FB6
\r
19 for <notmuch@notmuchmail.org>; Thu, 6 Dec 2012 07:02:24 -0800 (PST)
\r
20 Received: from guru.guru-group.fi (localhost [IPv6:::1])
\r
21 by guru.guru-group.fi (Postfix) with ESMTP id 9E42D100045;
\r
22 Thu, 6 Dec 2012 17:02:19 +0200 (EET)
\r
23 From: Tomi Ollila <tomi.ollila@iki.fi>
\r
24 To: Peter Feigl <craven@gmx.net>, notmuch@notmuchmail.org
\r
25 Subject: Re: [PATCH v4 1/5] Adding an S-expression structured output printer.
\r
27 <1409adf6cbd310d7313bdc3a69e7e78c25859d81.1354794428.git.craven@gmx.net>
\r
28 References: <cover.1354794428.git.craven@gmx.net>
\r
29 <1409adf6cbd310d7313bdc3a69e7e78c25859d81.1354794428.git.craven@gmx.net>
\r
30 User-Agent: Notmuch/0.14+116~g29fcdb5 (http://notmuchmail.org) Emacs/24.2.1
\r
31 (x86_64-unknown-linux-gnu)
\r
32 X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL
\r
33 $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F
\r
34 !)g;OY^,BjTbr)Np:%c_o'jj,Z
\r
35 Date: Thu, 06 Dec 2012 17:02:19 +0200
\r
36 Message-ID: <m24njzql50.fsf@guru.guru-group.fi>
\r
38 Content-Type: text/plain; charset=utf-8
\r
39 Content-Transfer-Encoding: quoted-printable
\r
40 X-BeenThere: notmuch@notmuchmail.org
\r
41 X-Mailman-Version: 2.1.13
\r
43 List-Id: "Use and development of the notmuch mail system."
\r
44 <notmuch.notmuchmail.org>
\r
45 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
46 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
47 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
48 List-Post: <mailto:notmuch@notmuchmail.org>
\r
49 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
50 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
51 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
52 X-List-Received-Date: Thu, 06 Dec 2012 15:02:27 -0000
\r
54 On Thu, Dec 06 2012, Peter Feigl <craven@gmx.net> wrote:
\r
56 > This commit adds a structured output printer for Lisp
\r
57 > S-Expressions. Later commits will use this printer in notmuch search,
\r
60 > The structure is the same as json, but:
\r
61 > - arrays are written as lists: ("foo" "bar" "baaz" 1 2 3)
\r
62 > - maps are written as p-lists: (:key "value" :other-key "other-value")
\r
63 > - true is written as t
\r
64 > - false is written as nil
\r
65 > - null is written as nil
\r
67 > Makefile.local | 1 +
\r
68 > sprinter-sexp.c | 238 ++++++++++++++++++++++++++++++++++++++++++++++++++=
\r
71 > 3 files changed, 243 insertions(+)
\r
72 > create mode 100644 sprinter-sexp.c
\r
74 > diff --git a/Makefile.local b/Makefile.local
\r
75 > index 2b91946..0db1713 100644
\r
76 > --- a/Makefile.local
\r
77 > +++ b/Makefile.local
\r
78 > @@ -270,6 +270,7 @@ notmuch_client_srcs =3D \
\r
82 > + sprinter-sexp.c \
\r
86 > diff --git a/sprinter-sexp.c b/sprinter-sexp.c
\r
87 > new file mode 100644
\r
88 > index 0000000..8f84eed
\r
90 > +++ b/sprinter-sexp.c
\r
92 > +/* notmuch - Not much of an email program, (just index and search)
\r
94 > + * Copyright =C2=A9 2012 Peter Feigl
\r
96 > + * This program is free software: you can redistribute it and/or modify
\r
97 > + * it under the terms of the GNU General Public License as published by
\r
98 > + * the Free Software Foundation, either version 3 of the License, or
\r
99 > + * (at your option) any later version.
\r
101 > + * This program is distributed in the hope that it will be useful,
\r
102 > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
103 > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
104 > + * GNU General Public License for more details.
\r
106 > + * You should have received a copy of the GNU General Public License
\r
107 > + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
109 > + * Author: Peter Feigl <peter.feigl@gmx.at>
\r
112 > +#include <stdbool.h>
\r
113 > +#include <stdio.h>
\r
114 > +#include <talloc.h>
\r
115 > +#include "sprinter.h"
\r
117 > +struct sprinter_sexp {
\r
118 > + struct sprinter vtable;
\r
120 > + /* Top of the state stack, or NULL if the printer is not currently
\r
121 > + * inside any aggregate types. */
\r
122 > + struct sexp_state *state;
\r
124 > + /* A flag to signify that a separator should be inserted in the
\r
125 > + * output as soon as possible. */
\r
126 > + notmuch_bool_t insert_separator;
\r
129 > +struct sexp_state {
\r
130 > + struct sexp_state *parent;
\r
132 > + /* True if nothing has been printed in this aggregate yet.
\r
133 > + * Suppresses the space before a value. */
\r
134 > + notmuch_bool_t first;
\r
137 > +/* Helper function to set up the stream to print a value. If this
\r
138 > + * value follows another value, prints a space. */
\r
139 > +static struct sprinter_sexp *
\r
140 > +sexp_begin_value (struct sprinter *sp)
\r
142 > + struct sprinter_sexp *sps =3D (struct sprinter_sexp *) sp;
\r
144 > + if (sps->state) {
\r
145 > + if (! sps->state->first) {
\r
146 > + if (sps->insert_separator) {
\r
147 > + fputc ('\n', sps->stream);
\r
148 > + sps->insert_separator =3D FALSE;
\r
150 > + fputc (' ', sps->stream);
\r
153 > + sps->state->first =3D FALSE;
\r
159 > +/* Helper function to begin an aggregate type. Prints the open
\r
160 > + * character and pushes a new state frame. */
\r
162 > +sexp_begin_aggregate (struct sprinter *sp)
\r
164 > + struct sprinter_sexp *sps =3D sexp_begin_value (sp);
\r
165 > + struct sexp_state *state =3D talloc (sps, struct sexp_state);
\r
166 > + fputc ('(', sps->stream);
\r
167 > + state->parent =3D sps->state;
\r
168 > + state->first =3D TRUE;
\r
169 > + sps->state =3D state;
\r
173 > +sexp_begin_map (struct sprinter *sp)
\r
175 > + sexp_begin_aggregate (sp);
\r
179 > +sexp_begin_list (struct sprinter *sp)
\r
181 > + sexp_begin_aggregate (sp);
\r
185 > +sexp_end (struct sprinter *sp)
\r
187 > + struct sprinter_sexp *sps =3D (struct sprinter_sexp *) sp;
\r
188 > + struct sexp_state *state =3D sps->state;
\r
190 > + fputc (')', sps->stream);
\r
191 > + sps->state =3D state->parent;
\r
192 > + talloc_free (state);
\r
193 > + if (sps->state =3D=3D NULL)
\r
194 > + fputc ('\n', sps->stream);
\r
198 > +sexp_string_len (struct sprinter *sp, const char *val, size_t len)
\r
200 > + /* Some characters need escaping. " and \ work fine in all Lisps,
\r
201 > + * \n is not supported in CL, but all others work fine.
\r
202 > + * Characters below 32 are printed as \123o (three-digit=20
\r
203 > + * octals), which work fine in most Schemes and Emacs. */
\r
204 > + static const char *const escapes[] =3D {
\r
205 > + ['\"'] =3D "\\\"", ['\\'] =3D "\\\\", ['\n'] =3D "\\n"
\r
207 > + struct sprinter_sexp *sps =3D sexp_begin_value (sp);
\r
209 > + fputc ('"', sps->stream);
\r
210 > + for (; len; ++val, --len) {
\r
211 > + unsigned char ch =3D *val;
\r
212 > + if (ch < ARRAY_SIZE (escapes) && escapes[ch])
\r
213 > + fputs (escapes[ch], sps->stream);
\r
214 > + else if (ch >=3D 32)
\r
215 > + fputc (ch, sps->stream);
\r
217 > + fprintf (sps->stream, "\\%03oo", ch);
\r
219 > + fputc ('"', sps->stream);
\r
223 > +sexp_string (struct sprinter *sp, const char *val)
\r
225 > + if (val =3D=3D NULL)
\r
227 > + sexp_string_len (sp, val, strlen (val));
\r
230 > +/* Prints a symbol, i.e. the name preceded by a colon. This should work
\r
231 > + * in all Lisps, at least as a symbol, if not as a proper keyword */
\r
233 > +sexp_symbol (struct sprinter *sp, const char *val)
\r
235 > + static const char illegal_characters[] =3D {
\r
236 > + ' ', '\t', '\n'
\r
238 > + unsigned int i =3D 0;
\r
239 > + struct sprinter_sexp *sps =3D (struct sprinter_sexp *) sp;
\r
241 > + if (val =3D=3D NULL)
\r
242 > + INTERNAL_ERROR ("illegal symbol NULL");
\r
244 > + for(i =3D 0; i < ARRAY_SIZE (illegal_characters); i++) {
\r
245 > + if(strchr(val, illegal_characters[i]) !=3D NULL) {
\r
246 > + INTERNAL_ERROR ("illegal character in symbol %s", val);
\r
248 The code that compiler cares looks ok to me, so I'm commenting these:
\r
249 spaces after for & if (and strchr) above.
\r
254 > + fputc (':', sps->stream);
\r
255 > + fputs (val, sps->stream);
\r
259 > +sexp_integer (struct sprinter *sp, int val)
\r
261 > + struct sprinter_sexp *sps =3D sexp_begin_value (sp);
\r
263 > + fprintf (sps->stream, "%d", val);
\r
267 > +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)
\r
269 > + struct sprinter_sexp *sps =3D sexp_begin_value (sp);
\r
271 > + fputs (val ? "t" : "nil", sps->stream);
\r
275 > +sexp_null (struct sprinter *sp)
\r
277 > + struct sprinter_sexp *sps =3D sexp_begin_value (sp);
\r
279 > + fputs ("nil", sps->stream);
\r
283 > +sexp_map_key (struct sprinter *sp, const char *key)
\r
285 > + struct sprinter_sexp *sps =3D (struct sprinter_sexp *) sp;
\r
286 > + if (sps->state && ! sps->state->first)
\r
287 > + fputc (' ', sps->stream);
\r
289 > + sps->state->first =3D FALSE;
\r
290 > + sexp_symbol (sp, key);
\r
294 > +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))
\r
299 > +sexp_separator (struct sprinter *sp)
\r
301 > + struct sprinter_sexp *sps =3D (struct sprinter_sexp *) sp;
\r
303 > + sps->insert_separator =3D TRUE;
\r
306 > +struct sprinter *
\r
307 > +sprinter_sexp_create (const void *ctx, FILE *stream)
\r
309 > + static const struct sprinter_sexp template =3D {
\r
311 > + .begin_map =3D sexp_begin_map,
\r
312 > + .begin_list =3D sexp_begin_list,
\r
313 > + .end =3D sexp_end,
\r
314 > + .string =3D sexp_string,
\r
315 > + .string_len =3D sexp_string_len,
\r
316 > + .integer =3D sexp_integer,
\r
317 > + .boolean =3D sexp_boolean,
\r
318 > + .null =3D sexp_null,
\r
319 > + .map_key =3D sexp_map_key,
\r
320 > + .separator =3D sexp_separator,
\r
321 > + .set_prefix =3D sexp_set_prefix,
\r
322 > + .is_text_printer =3D FALSE,
\r
325 > + struct sprinter_sexp *res;
\r
327 > + res =3D talloc (ctx, struct sprinter_sexp);
\r
331 > + *res =3D template;
\r
332 > + res->stream =3D stream;
\r
333 > + return &res->vtable;
\r
335 > diff --git a/sprinter.h b/sprinter.h
\r
336 > index 912a526..59776a9 100644
\r
339 > @@ -70,4 +70,8 @@ sprinter_text_create (const void *ctx, FILE *stream);
\r
340 > struct sprinter *
\r
341 > sprinter_json_create (const void *ctx, FILE *stream);
\r
343 > +/* Create a new structure printer that emits S-Expressions. */
\r
344 > +struct sprinter *
\r
345 > +sprinter_sexp_create (const void *ctx, FILE *stream);
\r
347 > #endif // NOTMUCH_SPRINTER_H
\r
351 > _______________________________________________
\r
352 > notmuch mailing list
\r
353 > notmuch@notmuchmail.org
\r
354 > http://notmuchmail.org/mailman/listinfo/notmuch
\r