[PATCH 1/7] doc/mkdocdeps.py: Convert execfile to import
[notmuch-archives.git] / 49 / 437f37fd2f1582a6ea0e4c2f7305875926ab05
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
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         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 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
26 In-Reply-To:\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
37 MIME-Version: 1.0\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
42 Precedence: list\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
53 \r
54 On Thu, Dec 06 2012, Peter Feigl <craven@gmx.net> wrote:\r
55 \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
58 > show and reply.\r
59 >\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
66 > ---\r
67 >  Makefile.local  |   1 +\r
68 >  sprinter-sexp.c | 238 ++++++++++++++++++++++++++++++++++++++++++++++++++=\r
69 ++++++\r
70 >  sprinter.h      |   4 +\r
71 >  3 files changed, 243 insertions(+)\r
72 >  create mode 100644 sprinter-sexp.c\r
73 >\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
79 >       notmuch-tag.c           \\r
80 >       notmuch-time.c          \\r
81 >       sprinter-json.c         \\r
82 > +     sprinter-sexp.c         \\r
83 >       sprinter-text.c         \\r
84 >       query-string.c          \\r
85 >       mime-node.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
89 > --- /dev/null\r
90 > +++ b/sprinter-sexp.c\r
91 > @@ -0,0 +1,238 @@\r
92 > +/* notmuch - Not much of an email program, (just index and search)\r
93 > + *\r
94 > + * Copyright =C2=A9 2012 Peter Feigl\r
95 > + *\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
100 > + *\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
105 > + *\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
108 > + *\r
109 > + * Author: Peter Feigl <peter.feigl@gmx.at>\r
110 > + */\r
111 > +\r
112 > +#include <stdbool.h>\r
113 > +#include <stdio.h>\r
114 > +#include <talloc.h>\r
115 > +#include "sprinter.h"\r
116 > +\r
117 > +struct sprinter_sexp {\r
118 > +    struct sprinter vtable;\r
119 > +    FILE *stream;\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
123 > +\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
127 > +};\r
128 > +\r
129 > +struct sexp_state {\r
130 > +    struct sexp_state *parent;\r
131 > +\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
135 > +};\r
136 > +\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
141 > +{\r
142 > +    struct sprinter_sexp *sps =3D (struct sprinter_sexp *) sp;\r
143 > +\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
149 > +         } else {\r
150 > +             fputc (' ', sps->stream);\r
151 > +         }\r
152 > +     } else {\r
153 > +         sps->state->first =3D FALSE;\r
154 > +     }\r
155 > +    }\r
156 > +    return sps;\r
157 > +}\r
158 > +\r
159 > +/* Helper function to begin an aggregate type.  Prints the open\r
160 > + * character and pushes a new state frame. */\r
161 > +static void\r
162 > +sexp_begin_aggregate (struct sprinter *sp)\r
163 > +{\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
170 > +}\r
171 > +\r
172 > +static void\r
173 > +sexp_begin_map (struct sprinter *sp)\r
174 > +{\r
175 > +    sexp_begin_aggregate (sp);\r
176 > +}\r
177 > +\r
178 > +static void\r
179 > +sexp_begin_list (struct sprinter *sp)\r
180 > +{\r
181 > +    sexp_begin_aggregate (sp);\r
182 > +}\r
183 > +\r
184 > +static void\r
185 > +sexp_end (struct sprinter *sp)\r
186 > +{\r
187 > +    struct sprinter_sexp *sps =3D (struct sprinter_sexp *) sp;\r
188 > +    struct sexp_state *state =3D sps->state;\r
189 > +\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
195 > +}\r
196 > +\r
197 > +static void\r
198 > +sexp_string_len (struct sprinter *sp, const char *val, size_t len)\r
199 > +{\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
206 > +    };\r
207 > +    struct sprinter_sexp *sps =3D sexp_begin_value (sp);\r
208 > +\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
216 > +     else\r
217 > +         fprintf (sps->stream, "\\%03oo", ch);\r
218 > +    }\r
219 > +    fputc ('"', sps->stream);\r
220 > +}\r
221 > +\r
222 > +static void\r
223 > +sexp_string (struct sprinter *sp, const char *val)\r
224 > +{\r
225 > +    if (val =3D=3D NULL)\r
226 > +     val =3D "";\r
227 > +    sexp_string_len (sp, val, strlen (val));\r
228 > +}\r
229 > +\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
232 > +static void\r
233 > +sexp_symbol (struct sprinter *sp, const char *val)\r
234 > +{\r
235 > +    static const char illegal_characters[] =3D {\r
236 > +     ' ', '\t', '\n'\r
237 > +    };\r
238 > +    unsigned int i =3D 0;\r
239 > +    struct sprinter_sexp *sps =3D (struct sprinter_sexp *) sp;\r
240 > +\r
241 > +    if (val =3D=3D NULL)\r
242 > +     INTERNAL_ERROR ("illegal symbol NULL");\r
243 > +\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
247 \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
250 \r
251 \r
252 > +     }\r
253 > +    }\r
254 > +    fputc (':', sps->stream);\r
255 > +    fputs (val, sps->stream);\r
256 > +}\r
257 > +\r
258 > +static void\r
259 > +sexp_integer (struct sprinter *sp, int val)\r
260 > +{\r
261 > +    struct sprinter_sexp *sps =3D sexp_begin_value (sp);\r
262 > +\r
263 > +    fprintf (sps->stream, "%d", val);\r
264 > +}\r
265 > +\r
266 > +static void\r
267 > +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)\r
268 > +{\r
269 > +    struct sprinter_sexp *sps =3D sexp_begin_value (sp);\r
270 > +\r
271 > +    fputs (val ? "t" : "nil", sps->stream);\r
272 > +}\r
273 > +\r
274 > +static void\r
275 > +sexp_null (struct sprinter *sp)\r
276 > +{\r
277 > +    struct sprinter_sexp *sps =3D sexp_begin_value (sp);\r
278 > +\r
279 > +    fputs ("nil", sps->stream);\r
280 > +}\r
281 > +\r
282 > +static void\r
283 > +sexp_map_key (struct sprinter *sp, const char *key)\r
284 > +{\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
288 > +\r
289 > +    sps->state->first =3D FALSE;\r
290 > +    sexp_symbol (sp, key);\r
291 > +}\r
292 > +\r
293 > +static void\r
294 > +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))\r
295 > +{\r
296 > +}\r
297 > +\r
298 > +static void\r
299 > +sexp_separator (struct sprinter *sp)\r
300 > +{\r
301 > +    struct sprinter_sexp *sps =3D (struct sprinter_sexp *) sp;\r
302 > +\r
303 > +    sps->insert_separator =3D TRUE;\r
304 > +}\r
305 > +\r
306 > +struct sprinter *\r
307 > +sprinter_sexp_create (const void *ctx, FILE *stream)\r
308 > +{\r
309 > +    static const struct sprinter_sexp template =3D {\r
310 > +     .vtable =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
323 > +     }\r
324 > +    };\r
325 > +    struct sprinter_sexp *res;\r
326 > +\r
327 > +    res =3D talloc (ctx, struct sprinter_sexp);\r
328 > +    if (! res)\r
329 > +     return NULL;\r
330 > +\r
331 > +    *res =3D template;\r
332 > +    res->stream =3D stream;\r
333 > +    return &res->vtable;\r
334 > +}\r
335 > diff --git a/sprinter.h b/sprinter.h\r
336 > index 912a526..59776a9 100644\r
337 > --- a/sprinter.h\r
338 > +++ b/sprinter.h\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
342 >=20=20\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
346 > +\r
347 >  #endif // NOTMUCH_SPRINTER_H\r
348 > --=20\r
349 > 1.8.0\r
350 >\r
351 > _______________________________________________\r
352 > notmuch mailing list\r
353 > notmuch@notmuchmail.org\r
354 > http://notmuchmail.org/mailman/listinfo/notmuch\r