Re: [PATCH 1/3] Adding an S-expression structured output printer.
[notmuch-archives.git] / 47 / 9b8546e0c8d0e044b6a42022eef9b9ae4fa23f
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 8AA77431FAF\r
6         for <notmuch@notmuchmail.org>; Sat,  1 Dec 2012 01:59:28 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -1.098\r
10 X-Spam-Level: \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 9w0+H9COuHbJ for <notmuch@notmuchmail.org>;\r
17         Sat,  1 Dec 2012 01:59:24 -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 4DF58431FAE\r
22         for <notmuch@notmuchmail.org>; Sat,  1 Dec 2012 01:59:24 -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 1Tejqw-0002DH-68; Sat, 01 Dec 2012 09:59:20 +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 1Tejqv-000602-LD; Sat, 01 Dec 2012 09:59:18 +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 09:59:17 +0000\r
39 Message-ID: <871ufa9jpm.fsf@qmul.ac.uk>\r
40 MIME-Version: 1.0\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: 3a05a2b84a908d85c6d9bd98faa9b411 (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
48         determine if it is\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
53         *      medium trust\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
61 Precedence: list\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 09:59:28 -0000\r
72 \r
73 \r
74 Hi\r
75 \r
76 Overall I like the series: I think I agree with all of Jani's\r
77 comments. \r
78 \r
79 My one extra comment is that I think we should decide on whether we also\r
80 want a sexp plist version. I think we might want one for the emacs\r
81 front-end as that currently uses plists for everything.\r
82 \r
83 If we do we might want to change the names a little, both for functions\r
84 and options (eg sexp_a and sexp_p or something). Probably a lot of\r
85 sprinter-sexp would be common to both versions.\r
86 \r
87 Best wishes\r
88 \r
89 Mark\r
90 \r
91 \r
92 On Fri, 30 Nov 2012, Peter Feigl <craven@gmx.net> wrote:\r
93 > This commit adds an sprinter for Lisp S-Expressions. Later commits will\r
94 > use this printer.\r
95 >\r
96 > The structure is the same as json, but:\r
97 > - arrays are written as lists: ("foo" "bar" "baaz" 1 2 3)\r
98 > - maps are written as a-lists: ((key "value") (other-key "other-value"))\r
99 > - true is written as t\r
100 > - false is written as nil\r
101 > - null is written as nil\r
102 > ---\r
103 >  Makefile.local  |   1 +\r
104 >  sprinter-sexp.c | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
105 >  2 files changed, 236 insertions(+)\r
106 >  create mode 100644 sprinter-sexp.c\r
107 >\r
108 > diff --git a/Makefile.local b/Makefile.local\r
109 > index 2b91946..0db1713 100644\r
110 > --- a/Makefile.local\r
111 > +++ b/Makefile.local\r
112 > @@ -270,6 +270,7 @@ notmuch_client_srcs =             \\r
113 >       notmuch-tag.c           \\r
114 >       notmuch-time.c          \\r
115 >       sprinter-json.c         \\r
116 > +     sprinter-sexp.c         \\r
117 >       sprinter-text.c         \\r
118 >       query-string.c          \\r
119 >       mime-node.c             \\r
120 > diff --git a/sprinter-sexp.c b/sprinter-sexp.c\r
121 > new file mode 100644\r
122 > index 0000000..8401c52\r
123 > --- /dev/null\r
124 > +++ b/sprinter-sexp.c\r
125 > @@ -0,0 +1,235 @@\r
126 > +#include <stdbool.h>\r
127 > +#include <stdio.h>\r
128 > +#include <talloc.h>\r
129 > +#include "sprinter.h"\r
130 > +\r
131 > +struct sprinter_sexp {\r
132 > +    struct sprinter vtable;\r
133 > +    FILE *stream;\r
134 > +    /* Top of the state stack, or NULL if the printer is not currently\r
135 > +     * inside any aggregate types. */\r
136 > +    struct sexp_state *state;\r
137 > +\r
138 > +    /* A flag to signify that a separator should be inserted in the\r
139 > +     * output as soon as possible.\r
140 > +     */\r
141 > +    notmuch_bool_t insert_separator;\r
142 > +};\r
143 > +\r
144 > +struct sexp_state {\r
145 > +    struct sexp_state *parent;\r
146 > +\r
147 > +    /* True if nothing has been printed in this aggregate yet.\r
148 > +     * Suppresses the space before a value. */\r
149 > +    notmuch_bool_t first;\r
150 > +\r
151 > +    /* True if the state is a map state.\r
152 > +       Used to add a space between key/value pairs. */\r
153 > +    notmuch_bool_t in_map;\r
154 > +\r
155 > +    /* The character that closes the current aggregate. */\r
156 > +    char close;\r
157 > +};\r
158 > +\r
159 > +/* Helper function to set up the stream to print a value.  If this\r
160 > + * value follows another value, prints a space. */\r
161 > +static struct sprinter_sexp *\r
162 > +sexp_begin_value (struct sprinter *sp)\r
163 > +{\r
164 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
165 > +\r
166 > +    if (sps->state) {\r
167 > +        if (! sps->state->first) {\r
168 > +            if (sps->insert_separator) {\r
169 > +                fputc ('\n', sps->stream);\r
170 > +                sps->insert_separator = FALSE;\r
171 > +            } else {\r
172 > +                if( ! sps->state->in_map)\r
173 > +                    fputc (' ', sps->stream);\r
174 > +            }\r
175 > +        } else {\r
176 > +            sps->state->first = FALSE;\r
177 > +        }\r
178 > +    }\r
179 > +    return sps;\r
180 > +}\r
181 > +\r
182 > +/* Helper function to begin an aggregate type.  Prints the open\r
183 > + * character and pushes a new state frame. */\r
184 > +static void\r
185 > +sexp_begin_aggregate (struct sprinter *sp, char open, char close)\r
186 > +{\r
187 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
188 > +    struct sexp_state *state = talloc (sps, struct sexp_state);\r
189 > +    fputc (open, sps->stream);\r
190 > +    state->parent = sps->state;\r
191 > +    state->first = TRUE;\r
192 > +    state->in_map = FALSE;\r
193 > +    state->close = close;\r
194 > +    sps->state = state;\r
195 > +}\r
196 > +\r
197 > +static void\r
198 > +sexp_begin_map (struct sprinter *sp)\r
199 > +{\r
200 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
201 > +    sexp_begin_aggregate (sp, '(', ')');\r
202 > +    sps->state->in_map = TRUE;\r
203 > +}\r
204 > +\r
205 > +static void\r
206 > +sexp_begin_list (struct sprinter *sp)\r
207 > +{\r
208 > +    sexp_begin_aggregate (sp, '(', ')');\r
209 > +}\r
210 > +\r
211 > +static void\r
212 > +sexp_end (struct sprinter *sp)\r
213 > +{\r
214 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
215 > +    struct sexp_state *state = sps->state;\r
216 > +\r
217 > +    if (sps->state->in_map)\r
218 > +        fputc (')', sps->stream);\r
219 > +    fputc (sps->state->close, sps->stream);\r
220 > +    sps->state = state->parent;\r
221 > +    talloc_free (state);\r
222 > +    if (sps->state == NULL)\r
223 > +        fputc ('\n', sps->stream);\r
224 > +}\r
225 > +\r
226 > +/* This implementation supports embedded NULs as allowed by the JSON\r
227 > + * specification and Unicode.  Support for *parsing* embedded NULs\r
228 > + * varies, but is generally not a problem outside of C-based parsers\r
229 > + * (Python's json module and Emacs' json.el take embedded NULs in\r
230 > + * stride). */\r
231 > +static void\r
232 > +sexp_string_len_internal (struct sprinter *sp, const char *val, size_t len, notmuch_bool_t quote)\r
233 > +{\r
234 > +    static const char *const escapes[] = {\r
235 > +        ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
236 > +        ['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"\r
237 > +    };\r
238 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
239 > +\r
240 > +    if(quote)\r
241 > +        fputc ('"', sps->stream);\r
242 > +    for (; len; ++val, --len) {\r
243 > +        unsigned char ch = *val;\r
244 > +        if (ch < ARRAY_SIZE (escapes) && escapes[ch])\r
245 > +            fputs (escapes[ch], sps->stream);\r
246 > +        else if (ch >= 32)\r
247 > +            fputc (ch, sps->stream);\r
248 > +        else\r
249 > +            fprintf (sps->stream, "\\u%04x", ch);\r
250 > +    }\r
251 > +    if(quote)\r
252 > +        fputc ('"', sps->stream);\r
253 > +}\r
254 > +\r
255 > +static void\r
256 > +sexp_string_len (struct sprinter *sp, const char *val, size_t len)\r
257 > +{\r
258 > +    sexp_string_len_internal (sp, val, len, TRUE); /* print quoted */\r
259 > +}\r
260 > +\r
261 > +static void\r
262 > +sexp_symbol_len (struct sprinter *sp, const char *val, size_t len)\r
263 > +{\r
264 > +    sexp_string_len_internal (sp, val, len, FALSE); /* print unquoted */\r
265 > +}\r
266 > +\r
267 > +static void\r
268 > +sexp_string (struct sprinter *sp, const char *val)\r
269 > +{\r
270 > +    if (val == NULL)\r
271 > +        val = "";\r
272 > +    sexp_string_len (sp, val, strlen (val));\r
273 > +}\r
274 > +\r
275 > +static void\r
276 > +sexp_symbol (struct sprinter *sp, const char *val)\r
277 > +{\r
278 > +    if (val == NULL)\r
279 > +        val = "";\r
280 > +    sexp_symbol_len (sp, val, strlen (val));\r
281 > +}\r
282 > +\r
283 > +static void\r
284 > +sexp_integer (struct sprinter *sp, int val)\r
285 > +{\r
286 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
287 > +\r
288 > +    fprintf (sps->stream, "%d", val);\r
289 > +}\r
290 > +\r
291 > +static void\r
292 > +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)\r
293 > +{\r
294 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
295 > +\r
296 > +    fputs (val ? "t" : "nil", sps->stream);\r
297 > +}\r
298 > +\r
299 > +static void\r
300 > +sexp_null (struct sprinter *sp)\r
301 > +{\r
302 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
303 > +\r
304 > +    fputs ("nil", sps->stream);\r
305 > +}\r
306 > +\r
307 > +static void\r
308 > +sexp_map_key (struct sprinter *sp, const char *key)\r
309 > +{\r
310 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
311 > +\r
312 > +    if( sps->state->in_map && ! sps->state->first)\r
313 > +        fputs (") ", sps->stream);\r
314 > +    fputc ('(', sps->stream);\r
315 > +    sexp_symbol (sp, key);\r
316 > +    fputc (' ', sps->stream);\r
317 > +}\r
318 > +\r
319 > +static void\r
320 > +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))\r
321 > +{\r
322 > +}\r
323 > +\r
324 > +static void\r
325 > +sexp_separator (struct sprinter *sp)\r
326 > +{\r
327 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
328 > +\r
329 > +    sps->insert_separator = TRUE;\r
330 > +}\r
331 > +\r
332 > +struct sprinter *\r
333 > +sprinter_sexp_create (const void *ctx, FILE *stream)\r
334 > +{\r
335 > +    static const struct sprinter_sexp template = {\r
336 > +        .vtable = {\r
337 > +            .begin_map = sexp_begin_map,\r
338 > +            .begin_list = sexp_begin_list,\r
339 > +            .end = sexp_end,\r
340 > +            .string = sexp_string,\r
341 > +            .string_len = sexp_string_len,\r
342 > +            .integer = sexp_integer,\r
343 > +            .boolean = sexp_boolean,\r
344 > +            .null = sexp_null,\r
345 > +            .map_key = sexp_map_key,\r
346 > +            .separator = sexp_separator,\r
347 > +            .set_prefix = sexp_set_prefix,\r
348 > +            .is_text_printer = FALSE,\r
349 > +        }\r
350 > +    };\r
351 > +    struct sprinter_sexp *res;\r
352 > +\r
353 > +    res = talloc (ctx, struct sprinter_sexp);\r
354 > +    if (! res)\r
355 > +        return NULL;\r
356 > +\r
357 > +    *res = template;\r
358 > +    res->stream = stream;\r
359 > +    return &res->vtable;\r
360 > +}\r
361 > -- \r
362 > 1.8.0\r
363 >\r
364 > _______________________________________________\r
365 > notmuch mailing list\r
366 > notmuch@notmuchmail.org\r
367 > http://notmuchmail.org/mailman/listinfo/notmuch\r