[v2 2/3] test-lib: Add dummy subject to force empty subject
[notmuch-archives.git] / 68 / 9d6081eaa100e74167dc9197d344ed83d3e9b6
1 Return-Path: <jani@nikula.org>\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 CFD96431FAF\r
6         for <notmuch@notmuchmail.org>; Fri, 30 Nov 2012 09:26:16 -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: -0.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 tFelwKdreYMo for <notmuch@notmuchmail.org>;\r
16         Fri, 30 Nov 2012 09:26:12 -0800 (PST)\r
17 Received: from mail-la0-f53.google.com (mail-la0-f53.google.com\r
18         [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 66E6E431FAE\r
21         for <notmuch@notmuchmail.org>; Fri, 30 Nov 2012 09:26:12 -0800 (PST)\r
22 Received: by mail-la0-f53.google.com with SMTP id w12so590012lag.26\r
23         for <notmuch@notmuchmail.org>; Fri, 30 Nov 2012 09:26:09 -0800 (PST)\r
24 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
25         d=google.com; s=20120113;\r
26         h=from:to:subject:in-reply-to:references:user-agent:date:message-id\r
27         :mime-version:content-type:x-gm-message-state;\r
28         bh=HPMQqgOfEDcMzhu8+WSPqDPq7KmQnfSQ0fywEx4OWkk=;\r
29         b=fikZmAVJMXLfJYBB5hGZ/l4LOydrGh+z1a7zTgS1sQkF9+bx6+HYq1Q6Dk/YOL0NHm\r
30         Df7j3E0qVK8OhN+C6dEvzem9a/14lCpJbBL6ukGbyMo2wIhm7n2rXxf+++Tw1+YPprZR\r
31         kUKxqadNnmHEY/xg6KOQpTm43qAPIH6yboACAuWZdufKCNLQ3vARfuO6xeeuDVKL1mG3\r
32         LfMCt6vKtXBdmiuXDzjOloDlLzsxEHiz4CI0k33CYF+MFknboREkZM5pNiXHWmN+DnU9\r
33         WkPBAkqhtDDDLcR+1d4Yr0/Nlzc+DX7BUwKUAn2g4UqdWcszjDdWt5mundYbo7SQvaTT\r
34         4FIg==\r
35 Received: by 10.112.30.70 with SMTP id q6mr1145589lbh.51.1354296369343;\r
36         Fri, 30 Nov 2012 09:26:09 -0800 (PST)\r
37 Received: from localhost (dsl-hkibrasgw4-fe51df00-27.dhcp.inet.fi.\r
38         [80.223.81.27])\r
39         by mx.google.com with ESMTPS id q2sm2320546lbd.14.2012.11.30.09.26.05\r
40         (version=SSLv3 cipher=OTHER); Fri, 30 Nov 2012 09:26:06 -0800 (PST)\r
41 From: Jani Nikula <jani@nikula.org>\r
42 To: Peter Feigl <craven@gmx.net>, notmuch@notmuchmail.org\r
43 Subject: Re: [PATCH 1/3] Adding an S-expression structured output printer.\r
44 In-Reply-To: <1354264143-30173-1-git-send-email-craven@gmx.net>\r
45 References: <1354264143-30173-1-git-send-email-craven@gmx.net>\r
46 User-Agent: Notmuch/0.14+124~g3b17402 (http://notmuchmail.org) Emacs/23.4.1\r
47         (i686-pc-linux-gnu)\r
48 Date: Fri, 30 Nov 2012 19:26:04 +0200\r
49 Message-ID: <87a9tzj93n.fsf@nikula.org>\r
50 MIME-Version: 1.0\r
51 Content-Type: text/plain; charset=us-ascii\r
52 X-Gm-Message-State:\r
53  ALoCoQk0AEL6uGu+djMyN5G1XsAjOMnlJHHDtQqzZA+tRUY2Wbn1GeSML5dts5nYF3VdnrMGaE1q\r
54 X-BeenThere: notmuch@notmuchmail.org\r
55 X-Mailman-Version: 2.1.13\r
56 Precedence: list\r
57 List-Id: "Use and development of the notmuch mail system."\r
58         <notmuch.notmuchmail.org>\r
59 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
60         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
61 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
62 List-Post: <mailto:notmuch@notmuchmail.org>\r
63 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
64 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
65         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
66 X-List-Received-Date: Fri, 30 Nov 2012 17:26:16 -0000\r
67 \r
68 \r
69 Hi Peter, looks good save for a few nitpicks (see comments\r
70 inline). Someone more experienced in lisp should still have a look.\r
71 \r
72 BR,\r
73 Jani.\r
74 \r
75 \r
76 On Fri, 30 Nov 2012, Peter Feigl <craven@gmx.net> wrote:\r
77 > This commit adds an sprinter for Lisp S-Expressions. Later commits will\r
78 > use this printer.\r
79 >\r
80 > The structure is the same as json, but:\r
81 > - arrays are written as lists: ("foo" "bar" "baaz" 1 2 3)\r
82 > - maps are written as a-lists: ((key "value") (other-key "other-value"))\r
83 > - true is written as t\r
84 > - false is written as nil\r
85 > - null is written as nil\r
86 > ---\r
87 >  Makefile.local  |   1 +\r
88 >  sprinter-sexp.c | 235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
89 >  2 files changed, 236 insertions(+)\r
90 >  create mode 100644 sprinter-sexp.c\r
91 >\r
92 > diff --git a/Makefile.local b/Makefile.local\r
93 > index 2b91946..0db1713 100644\r
94 > --- a/Makefile.local\r
95 > +++ b/Makefile.local\r
96 > @@ -270,6 +270,7 @@ notmuch_client_srcs =             \\r
97 >       notmuch-tag.c           \\r
98 >       notmuch-time.c          \\r
99 >       sprinter-json.c         \\r
100 > +     sprinter-sexp.c         \\r
101 >       sprinter-text.c         \\r
102 >       query-string.c          \\r
103 >       mime-node.c             \\r
104 > diff --git a/sprinter-sexp.c b/sprinter-sexp.c\r
105 > new file mode 100644\r
106 > index 0000000..8401c52\r
107 > --- /dev/null\r
108 > +++ b/sprinter-sexp.c\r
109 > @@ -0,0 +1,235 @@\r
110 \r
111 Copyright comment is missing. (Seems to be missing from sprinter-json.c\r
112 too, but that's no excuse! ;)\r
113 \r
114 > +#include <stdbool.h>\r
115 > +#include <stdio.h>\r
116 > +#include <talloc.h>\r
117 > +#include "sprinter.h"\r
118 > +\r
119 > +struct sprinter_sexp {\r
120 > +    struct sprinter vtable;\r
121 > +    FILE *stream;\r
122 > +    /* Top of the state stack, or NULL if the printer is not currently\r
123 > +     * inside any aggregate types. */\r
124 > +    struct sexp_state *state;\r
125 > +\r
126 > +    /* A flag to signify that a separator should be inserted in the\r
127 > +     * output as soon as possible.\r
128 > +     */\r
129 > +    notmuch_bool_t insert_separator;\r
130 > +};\r
131 > +\r
132 > +struct sexp_state {\r
133 > +    struct sexp_state *parent;\r
134 > +\r
135 > +    /* True if nothing has been printed in this aggregate yet.\r
136 > +     * Suppresses the space before a value. */\r
137 > +    notmuch_bool_t first;\r
138 > +\r
139 > +    /* True if the state is a map state.\r
140 > +       Used to add a space between key/value pairs. */\r
141 \r
142 The multi-line comments should use the same style here and elsewhere.\r
143 \r
144 > +    notmuch_bool_t in_map;\r
145 > +\r
146 > +    /* The character that closes the current aggregate. */\r
147 > +    char close;\r
148 > +};\r
149 > +\r
150 > +/* Helper function to set up the stream to print a value.  If this\r
151 > + * value follows another value, prints a space. */\r
152 > +static struct sprinter_sexp *\r
153 > +sexp_begin_value (struct sprinter *sp)\r
154 > +{\r
155 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
156 > +\r
157 > +    if (sps->state) {\r
158 > +        if (! sps->state->first) {\r
159 > +            if (sps->insert_separator) {\r
160 > +                fputc ('\n', sps->stream);\r
161 > +                sps->insert_separator = FALSE;\r
162 > +            } else {\r
163 > +                if( ! sps->state->in_map)\r
164 \r
165 Spacing should be "if (! " ...\r
166 \r
167 > +                    fputc (' ', sps->stream);\r
168 > +            }\r
169 > +        } else {\r
170 > +            sps->state->first = FALSE;\r
171 > +        }\r
172 > +    }\r
173 > +    return sps;\r
174 > +}\r
175 > +\r
176 > +/* Helper function to begin an aggregate type.  Prints the open\r
177 > + * character and pushes a new state frame. */\r
178 > +static void\r
179 > +sexp_begin_aggregate (struct sprinter *sp, char open, char close)\r
180 > +{\r
181 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
182 > +    struct sexp_state *state = talloc (sps, struct sexp_state);\r
183 > +    fputc (open, sps->stream);\r
184 > +    state->parent = sps->state;\r
185 > +    state->first = TRUE;\r
186 > +    state->in_map = FALSE;\r
187 > +    state->close = close;\r
188 > +    sps->state = state;\r
189 > +}\r
190 > +\r
191 > +static void\r
192 > +sexp_begin_map (struct sprinter *sp)\r
193 > +{\r
194 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
195 > +    sexp_begin_aggregate (sp, '(', ')');\r
196 > +    sps->state->in_map = TRUE;\r
197 > +}\r
198 > +\r
199 > +static void\r
200 > +sexp_begin_list (struct sprinter *sp)\r
201 > +{\r
202 > +    sexp_begin_aggregate (sp, '(', ')');\r
203 > +}\r
204 > +\r
205 > +static void\r
206 > +sexp_end (struct sprinter *sp)\r
207 > +{\r
208 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
209 > +    struct sexp_state *state = sps->state;\r
210 > +\r
211 > +    if (sps->state->in_map)\r
212 > +        fputc (')', sps->stream);\r
213 > +    fputc (sps->state->close, sps->stream);\r
214 > +    sps->state = state->parent;\r
215 > +    talloc_free (state);\r
216 > +    if (sps->state == NULL)\r
217 > +        fputc ('\n', sps->stream);\r
218 > +}\r
219 > +\r
220 > +/* This implementation supports embedded NULs as allowed by the JSON\r
221 > + * specification and Unicode.  Support for *parsing* embedded NULs\r
222 > + * varies, but is generally not a problem outside of C-based parsers\r
223 > + * (Python's json module and Emacs' json.el take embedded NULs in\r
224 > + * stride). */\r
225 \r
226 References to JSON seem out of place here.\r
227 \r
228 > +static void\r
229 > +sexp_string_len_internal (struct sprinter *sp, const char *val, size_t len, notmuch_bool_t quote)\r
230 > +{\r
231 > +    static const char *const escapes[] = {\r
232 > +        ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
233 > +        ['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"\r
234 > +    };\r
235 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
236 > +\r
237 > +    if(quote)\r
238 > +        fputc ('"', sps->stream);\r
239 > +    for (; len; ++val, --len) {\r
240 > +        unsigned char ch = *val;\r
241 > +        if (ch < ARRAY_SIZE (escapes) && escapes[ch])\r
242 > +            fputs (escapes[ch], sps->stream);\r
243 > +        else if (ch >= 32)\r
244 > +            fputc (ch, sps->stream);\r
245 > +        else\r
246 > +            fprintf (sps->stream, "\\u%04x", ch);\r
247 > +    }\r
248 > +    if(quote)\r
249 > +        fputc ('"', sps->stream);\r
250 > +}\r
251 > +\r
252 > +static void\r
253 > +sexp_string_len (struct sprinter *sp, const char *val, size_t len)\r
254 > +{\r
255 > +    sexp_string_len_internal (sp, val, len, TRUE); /* print quoted */\r
256 > +}\r
257 > +\r
258 > +static void\r
259 > +sexp_symbol_len (struct sprinter *sp, const char *val, size_t len)\r
260 > +{\r
261 > +    sexp_string_len_internal (sp, val, len, FALSE); /* print unquoted */\r
262 > +}\r
263 > +\r
264 > +static void\r
265 > +sexp_string (struct sprinter *sp, const char *val)\r
266 > +{\r
267 > +    if (val == NULL)\r
268 > +        val = "";\r
269 > +    sexp_string_len (sp, val, strlen (val));\r
270 > +}\r
271 > +\r
272 > +static void\r
273 > +sexp_symbol (struct sprinter *sp, const char *val)\r
274 > +{\r
275 > +    if (val == NULL)\r
276 > +        val = "";\r
277 > +    sexp_symbol_len (sp, val, strlen (val));\r
278 > +}\r
279 > +\r
280 > +static void\r
281 > +sexp_integer (struct sprinter *sp, int val)\r
282 > +{\r
283 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
284 > +\r
285 > +    fprintf (sps->stream, "%d", val);\r
286 > +}\r
287 > +\r
288 > +static void\r
289 > +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)\r
290 > +{\r
291 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
292 > +\r
293 > +    fputs (val ? "t" : "nil", sps->stream);\r
294 > +}\r
295 > +\r
296 > +static void\r
297 > +sexp_null (struct sprinter *sp)\r
298 > +{\r
299 > +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
300 > +\r
301 > +    fputs ("nil", sps->stream);\r
302 > +}\r
303 > +\r
304 > +static void\r
305 > +sexp_map_key (struct sprinter *sp, const char *key)\r
306 > +{\r
307 > +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
308 > +\r
309 > +    if( sps->state->in_map && ! sps->state->first)\r
310 \r
311 Spacing should be "if (sps" ...\r
312 \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