[PATCH v2 1/5] Adding an S-expression structured output printer.
[notmuch-archives.git] / 82 / 13c2fd48b5f116dfd6d67aff46b908dffaeae1
1 Return-Path: <nex@nexoid.at>\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 A83A8431FD7\r
6         for <notmuch@notmuchmail.org>; Tue,  4 Dec 2012 06:46:44 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: 0.001\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=0.001 tagged_above=-999 required=5\r
13         tests=[FREEMAIL_FROM=0.001] 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 9ZmVoi5CtxlL for <notmuch@notmuchmail.org>;\r
17         Tue,  4 Dec 2012 06:46:40 -0800 (PST)\r
18 Received: from mail.nexoid.at (www.nexoid.at [178.79.130.240])\r
19         (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id DE08D431FC7\r
22         for <notmuch@notmuchmail.org>; Tue,  4 Dec 2012 06:46:39 -0800 (PST)\r
23 Received: by mail.nexoid.at (Postfix, from userid 1000)\r
24         id 8BC6711C109; Tue,  4 Dec 2012 15:46:38 +0100 (CET)\r
25 From: Peter Feigl <craven@gmx.net>\r
26 To: notmuch@notmuchmail.org\r
27 Subject: [PATCH v2 1/5] Adding an S-expression structured output printer.\r
28 Date: Tue,  4 Dec 2012 15:46:18 +0100\r
29 Message-Id: <1354632382-15609-2-git-send-email-craven@gmx.net>\r
30 X-Mailer: git-send-email 1.8.0\r
31 In-Reply-To: <1354632382-15609-1-git-send-email-craven@gmx.net>\r
32 References: <1354632382-15609-1-git-send-email-craven@gmx.net>\r
33 In-Reply-To: <1354264143-30173-1-git-send-email-craven@gmx.net>\r
34 References: <1354264143-30173-1-git-send-email-craven@gmx.net>\r
35 MIME-Version: 1.0\r
36 Content-Type: text/plain; charset=UTF-8\r
37 Content-Transfer-Encoding: 8bit\r
38 X-BeenThere: notmuch@notmuchmail.org\r
39 X-Mailman-Version: 2.1.13\r
40 Precedence: list\r
41 List-Id: "Use and development of the notmuch mail system."\r
42         <notmuch.notmuchmail.org>\r
43 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
44         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
45 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
46 List-Post: <mailto:notmuch@notmuchmail.org>\r
47 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
48 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
50 X-List-Received-Date: Tue, 04 Dec 2012 14:46:45 -0000\r
51 \r
52 This commit adds a structured output printer for Lisp\r
53 S-Expressions. Later commits will use this printer in notmuch search,\r
54 show and reply.\r
55 \r
56 The structure is the same as json, but:\r
57 - arrays are written as lists: ("foo" "bar" "baaz" 1 2 3)\r
58 - maps are written as a-lists: ((key "value") (other-key "other-value"))\r
59 - true is written as t\r
60 - false is written as nil\r
61 - null is written as nil\r
62 ---\r
63  Makefile.local  |   1 +\r
64  sprinter-sexp.c | 250 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
65  sprinter.h      |   4 +\r
66  3 files changed, 255 insertions(+)\r
67  create mode 100644 sprinter-sexp.c\r
68 \r
69 diff --git a/Makefile.local b/Makefile.local\r
70 index 2b91946..0db1713 100644\r
71 --- a/Makefile.local\r
72 +++ b/Makefile.local\r
73 @@ -270,6 +270,7 @@ notmuch_client_srcs =               \\r
74         notmuch-tag.c           \\r
75         notmuch-time.c          \\r
76         sprinter-json.c         \\r
77 +       sprinter-sexp.c         \\r
78         sprinter-text.c         \\r
79         query-string.c          \\r
80         mime-node.c             \\r
81 diff --git a/sprinter-sexp.c b/sprinter-sexp.c\r
82 new file mode 100644\r
83 index 0000000..6d6bbad\r
84 --- /dev/null\r
85 +++ b/sprinter-sexp.c\r
86 @@ -0,0 +1,250 @@\r
87 +/* notmuch - Not much of an email program, (just index and search)\r
88 + *\r
89 + * Copyright © 2012 Carl Worth\r
90 + *\r
91 + * This program is free software: you can redistribute it and/or modify\r
92 + * it under the terms of the GNU General Public License as published by\r
93 + * the Free Software Foundation, either version 3 of the License, or\r
94 + * (at your option) any later version.\r
95 + *\r
96 + * This program is distributed in the hope that it will be useful,\r
97 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
98 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
99 + * GNU General Public License for more details.\r
100 + *\r
101 + * You should have received a copy of the GNU General Public License\r
102 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
103 + *\r
104 + * Author: Carl Worth <cworth@cworth.org>\r
105 + */\r
106 +\r
107 +#include <stdbool.h>\r
108 +#include <stdio.h>\r
109 +#include <talloc.h>\r
110 +#include "sprinter.h"\r
111 +\r
112 +struct sprinter_sexp {\r
113 +    struct sprinter vtable;\r
114 +    FILE *stream;\r
115 +    /* Top of the state stack, or NULL if the printer is not currently\r
116 +     * inside any aggregate types. */\r
117 +    struct sexp_state *state;\r
118 +\r
119 +    /* A flag to signify that a separator should be inserted in the\r
120 +     * output as soon as possible.\r
121 +     */\r
122 +    notmuch_bool_t insert_separator;\r
123 +};\r
124 +\r
125 +struct sexp_state {\r
126 +    struct sexp_state *parent;\r
127 +\r
128 +    /* True if nothing has been printed in this aggregate yet.\r
129 +     * Suppresses the space before a value. */\r
130 +    notmuch_bool_t first;\r
131 +\r
132 +    /* True if the state is a map state.\r
133 +     * Used to add a space between key/value pairs. */\r
134 +    notmuch_bool_t in_map;\r
135 +\r
136 +    /* The character that closes the current aggregate. */\r
137 +    char close;\r
138 +};\r
139 +\r
140 +/* Helper function to set up the stream to print a value.  If this\r
141 + * value follows another value, prints a space. */\r
142 +static struct sprinter_sexp *\r
143 +sexp_begin_value (struct sprinter *sp)\r
144 +{\r
145 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
146 +\r
147 +    if (sps->state) {\r
148 +       if (! sps->state->first) {\r
149 +           if (sps->insert_separator) {\r
150 +               fputc ('\n', sps->stream);\r
151 +               sps->insert_separator = FALSE;\r
152 +           } else {\r
153 +               if (! sps->state->in_map)\r
154 +                   fputc (' ', sps->stream);\r
155 +           }\r
156 +       } else {\r
157 +           sps->state->first = FALSE;\r
158 +       }\r
159 +    }\r
160 +    return sps;\r
161 +}\r
162 +\r
163 +/* Helper function to begin an aggregate type.  Prints the open\r
164 + * character and pushes a new state frame. */\r
165 +static void\r
166 +sexp_begin_aggregate (struct sprinter *sp, char open, char close)\r
167 +{\r
168 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
169 +    struct sexp_state *state = talloc (sps, struct sexp_state);\r
170 +    fputc (open, sps->stream);\r
171 +    state->parent = sps->state;\r
172 +    state->first = TRUE;\r
173 +    state->in_map = FALSE;\r
174 +    state->close = close;\r
175 +    sps->state = state;\r
176 +}\r
177 +\r
178 +static void\r
179 +sexp_begin_map (struct sprinter *sp)\r
180 +{\r
181 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
182 +    sexp_begin_aggregate (sp, '(', ')');\r
183 +    sps->state->in_map = TRUE;\r
184 +}\r
185 +\r
186 +static void\r
187 +sexp_begin_list (struct sprinter *sp)\r
188 +{\r
189 +    sexp_begin_aggregate (sp, '(', ')');\r
190 +}\r
191 +\r
192 +static void\r
193 +sexp_end (struct sprinter *sp)\r
194 +{\r
195 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
196 +    struct sexp_state *state = sps->state;\r
197 +\r
198 +    if (sps->state->in_map)\r
199 +       fputc (')', sps->stream);\r
200 +    fputc (sps->state->close, sps->stream);\r
201 +    sps->state = state->parent;\r
202 +    talloc_free (state);\r
203 +    if (sps->state == NULL)\r
204 +       fputc ('\n', sps->stream);\r
205 +}\r
206 +\r
207 +static void\r
208 +sexp_string_len_internal (struct sprinter *sp, const char *val, size_t len, notmuch_bool_t quote)\r
209 +{\r
210 +    static const char *const escapes[] = {\r
211 +       ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
212 +       ['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"\r
213 +    };\r
214 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
215 +\r
216 +    if(quote)\r
217 +       fputc ('"', sps->stream);\r
218 +    for (; len; ++val, --len) {\r
219 +       unsigned char ch = *val;\r
220 +       if (ch < ARRAY_SIZE (escapes) && escapes[ch])\r
221 +           fputs (escapes[ch], sps->stream);\r
222 +       else if (ch >= 32)\r
223 +           fputc (ch, sps->stream);\r
224 +       else\r
225 +           fprintf (sps->stream, "\\u%04x", ch);\r
226 +    }\r
227 +    if(quote)\r
228 +       fputc ('"', sps->stream);\r
229 +}\r
230 +\r
231 +static void\r
232 +sexp_string_len (struct sprinter *sp, const char *val, size_t len)\r
233 +{\r
234 +    sexp_string_len_internal (sp, val, len, TRUE); /* print quoted */\r
235 +}\r
236 +\r
237 +static void\r
238 +sexp_symbol_len (struct sprinter *sp, const char *val, size_t len)\r
239 +{\r
240 +    sexp_string_len_internal (sp, val, len, FALSE); /* print unquoted */\r
241 +}\r
242 +\r
243 +static void\r
244 +sexp_string (struct sprinter *sp, const char *val)\r
245 +{\r
246 +    if (val == NULL)\r
247 +       val = "";\r
248 +    sexp_string_len (sp, val, strlen (val));\r
249 +}\r
250 +\r
251 +static void\r
252 +sexp_symbol (struct sprinter *sp, const char *val)\r
253 +{\r
254 +    if (val == NULL)\r
255 +       val = "";\r
256 +    sexp_symbol_len (sp, val, strlen (val));\r
257 +}\r
258 +\r
259 +static void\r
260 +sexp_integer (struct sprinter *sp, int val)\r
261 +{\r
262 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
263 +\r
264 +    fprintf (sps->stream, "%d", val);\r
265 +}\r
266 +\r
267 +static void\r
268 +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)\r
269 +{\r
270 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
271 +\r
272 +    fputs (val ? "t" : "nil", sps->stream);\r
273 +}\r
274 +\r
275 +static void\r
276 +sexp_null (struct sprinter *sp)\r
277 +{\r
278 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
279 +\r
280 +    fputs ("nil", sps->stream);\r
281 +}\r
282 +\r
283 +static void\r
284 +sexp_map_key (struct sprinter *sp, const char *key)\r
285 +{\r
286 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
287 +\r
288 +    if (sps->state->in_map && ! sps->state->first)\r
289 +       fputs (") ", sps->stream);\r
290 +    fputc ('(', sps->stream);\r
291 +    sexp_symbol (sp, key);\r
292 +    fputc (' ', sps->stream);\r
293 +}\r
294 +\r
295 +static void\r
296 +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))\r
297 +{\r
298 +}\r
299 +\r
300 +static void\r
301 +sexp_separator (struct sprinter *sp)\r
302 +{\r
303 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
304 +\r
305 +    sps->insert_separator = TRUE;\r
306 +}\r
307 +\r
308 +struct sprinter *\r
309 +sprinter_sexp_create (const void *ctx, FILE *stream)\r
310 +{\r
311 +    static const struct sprinter_sexp template = {\r
312 +       .vtable = {\r
313 +           .begin_map = sexp_begin_map,\r
314 +           .begin_list = sexp_begin_list,\r
315 +           .end = sexp_end,\r
316 +           .string = sexp_string,\r
317 +           .string_len = sexp_string_len,\r
318 +           .integer = sexp_integer,\r
319 +           .boolean = sexp_boolean,\r
320 +           .null = sexp_null,\r
321 +           .map_key = sexp_map_key,\r
322 +           .separator = sexp_separator,\r
323 +           .set_prefix = sexp_set_prefix,\r
324 +           .is_text_printer = FALSE,\r
325 +       }\r
326 +    };\r
327 +    struct sprinter_sexp *res;\r
328 +\r
329 +    res = talloc (ctx, struct sprinter_sexp);\r
330 +    if (! res)\r
331 +       return NULL;\r
332 +\r
333 +    *res = template;\r
334 +    res->stream = stream;\r
335 +    return &res->vtable;\r
336 +}\r
337 diff --git a/sprinter.h b/sprinter.h\r
338 index 912a526..59776a9 100644\r
339 --- a/sprinter.h\r
340 +++ b/sprinter.h\r
341 @@ -70,4 +70,8 @@ sprinter_text_create (const void *ctx, FILE *stream);\r
342  struct sprinter *\r
343  sprinter_json_create (const void *ctx, FILE *stream);\r
344  \r
345 +/* Create a new structure printer that emits S-Expressions. */\r
346 +struct sprinter *\r
347 +sprinter_sexp_create (const void *ctx, FILE *stream);\r
348 +\r
349  #endif // NOTMUCH_SPRINTER_H\r
350 -- \r
351 1.8.0\r
352 \r