[PATCH] This patch is a little finger excercise for working with git. I found a piece...
[notmuch-archives.git] / 2e / 4be8918afb58ef038c812171b3c5bf0c96a0e7
1 Return-Path: <craven@gmx.net>\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 C1321431FC2\r
6         for <notmuch@notmuchmail.org>; Mon, 16 Jul 2012 23:04:36 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0.001\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0.001 tagged_above=-999 required=5\r
12         tests=[FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001]\r
13         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 yTBRAJmm6IBF for <notmuch@notmuchmail.org>;\r
17         Mon, 16 Jul 2012 23:04:35 -0700 (PDT)\r
18 Received: from mailout-de.gmx.net (mailout-de.gmx.net [213.165.64.22])\r
19         by olra.theworths.org (Postfix) with SMTP id B0791431FBC\r
20         for <notmuch@notmuchmail.org>; Mon, 16 Jul 2012 23:04:34 -0700 (PDT)\r
21 Received: (qmail invoked by alias); 17 Jul 2012 06:04:33 -0000\r
22 Received: from gw.arelion.cust.net.lagis.at (EHLO dodekanex.arelion.at)\r
23         [83.164.197.182]\r
24         by mail.gmx.net (mp028) with SMTP; 17 Jul 2012 08:04:33 +0200\r
25 X-Authenticated: #201305\r
26 X-Provags-ID: V01U2FsdGVkX18NvpYKxHMNnofWVlzLB5B2cLwbfRhenTPUMs4N8D\r
27         W7y0sz+y8tvJnF\r
28 Received: by dodekanex.arelion.at (Postfix, from userid 1000)\r
29         id D49A630271E; Mon, 16 Jul 2012 10:35:05 +0200 (CEST)\r
30 From: <craven@gmx.net>\r
31 To: notmuch@notmuchmail.org\r
32 Subject: [PATCH v6 2/3] Add structured output formatter for JSON and plain\r
33         text.\r
34 Date: Mon, 16 Jul 2012 10:35:01 +0200\r
35 Message-Id: <1342427702-23316-3-git-send-email-craven@gmx.net>\r
36 X-Mailer: git-send-email 1.7.11.1\r
37 In-Reply-To: <1342427702-23316-1-git-send-email-craven@gmx.net>\r
38 References: <20120714020954.GD31670@mit.edu>\r
39         <1342427702-23316-1-git-send-email-craven@gmx.net>\r
40 X-Y-GMX-Trusted: 0\r
41 X-BeenThere: notmuch@notmuchmail.org\r
42 X-Mailman-Version: 2.1.13\r
43 Precedence: list\r
44 List-Id: "Use and development of the notmuch mail system."\r
45         <notmuch.notmuchmail.org>\r
46 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
47         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
48 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
49 List-Post: <mailto:notmuch@notmuchmail.org>\r
50 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
51 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
52         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
53 X-List-Received-Date: Tue, 17 Jul 2012 06:04:37 -0000\r
54 \r
55 Using the new structured printer support in sprinter.h, implement\r
56 sprinter_json_create, which returns a new JSON structured output\r
57 formatter. The formatter prints output similar to the existing JSON, but\r
58 with differences in whitespace (mostly newlines, --output=summary prints\r
59 the entire message summary on one line, not split across multiple lines).\r
60 \r
61 Also implement a "structured" formatter for plain text that prints\r
62 prefixed strings, to be used with notmuch-search.c plain text output.\r
63 ---\r
64  Makefile.local         |   2 +\r
65  sprinter-json.c        | 191 +++++++++++++++++++++++++++++++++++++++++++++++++\r
66  sprinter-text-search.c | 146 +++++++++++++++++++++++++++++++++++++\r
67  sprinter.h             |   9 +++\r
68  4 files changed, 348 insertions(+)\r
69  create mode 100644 sprinter-json.c\r
70  create mode 100644 sprinter-text-search.c\r
71 \r
72 diff --git a/Makefile.local b/Makefile.local\r
73 index a890df2..4f534f1 100644\r
74 --- a/Makefile.local\r
75 +++ b/Makefile.local\r
76 @@ -290,6 +290,8 @@ notmuch_client_srcs =               \\r
77         notmuch-show.c          \\r
78         notmuch-tag.c           \\r
79         notmuch-time.c          \\r
80 +       sprinter-text-search.c  \\r
81 +       sprinter-json.c         \\r
82         query-string.c          \\r
83         mime-node.c             \\r
84         crypto.c                \\r
85 diff --git a/sprinter-json.c b/sprinter-json.c\r
86 new file mode 100644\r
87 index 0000000..a93a390\r
88 --- /dev/null\r
89 +++ b/sprinter-json.c\r
90 @@ -0,0 +1,191 @@\r
91 +#include <stdbool.h>\r
92 +#include <stdio.h>\r
93 +#include <talloc.h>\r
94 +#include "sprinter.h"\r
95 +\r
96 +struct sprinter_json {\r
97 +    struct sprinter vtable;\r
98 +    FILE *stream;\r
99 +    /* Top of the state stack, or NULL if the printer is not currently\r
100 +     * inside any aggregate types. */\r
101 +    struct json_state *state;\r
102 +\r
103 +    /* A flag to signify that a separator should be inserted in the\r
104 +     * output as soon as possible.\r
105 +     */\r
106 +    notmuch_bool_t insert_separator;\r
107 +};\r
108 +\r
109 +struct json_state {\r
110 +    struct json_state *parent;\r
111 +    /* True if nothing has been printed in this aggregate yet.\r
112 +     * Suppresses the comma before a value. */\r
113 +    notmuch_bool_t first;\r
114 +    /* The character that closes the current aggregate. */\r
115 +    char close;\r
116 +};\r
117 +\r
118 +/* Helper function to set up the stream to print a value.  If this\r
119 + * value follows another value, prints a comma. */\r
120 +static struct sprinter_json *\r
121 +json_begin_value (struct sprinter *sp)\r
122 +{\r
123 +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
124 +\r
125 +    if (spj->state) {\r
126 +       if (! spj->state->first) {\r
127 +           fputc (',', spj->stream);\r
128 +           if (spj->insert_separator) {\r
129 +               fputc ('\n', spj->stream);\r
130 +               spj->insert_separator = FALSE;\r
131 +           } else\r
132 +               fputc (' ', spj->stream);\r
133 +       } else\r
134 +           spj->state->first = FALSE;\r
135 +    }\r
136 +    return spj;\r
137 +}\r
138 +\r
139 +/* Helper function to begin an aggregate type.  Prints the open\r
140 + * character and pushes a new state frame. */\r
141 +static void\r
142 +json_begin_aggregate (struct sprinter *sp, char open, char close)\r
143 +{\r
144 +    struct sprinter_json *spj = json_begin_value (sp);\r
145 +    struct json_state *state = talloc (spj, struct json_state);\r
146 +\r
147 +    fputc (open, spj->stream);\r
148 +    state->parent = spj->state;\r
149 +    state->first = TRUE;\r
150 +    state->close = close;\r
151 +    spj->state = state;\r
152 +}\r
153 +\r
154 +static void\r
155 +json_begin_map (struct sprinter *sp)\r
156 +{\r
157 +    json_begin_aggregate (sp, '{', '}');\r
158 +}\r
159 +\r
160 +static void\r
161 +json_begin_list (struct sprinter *sp)\r
162 +{\r
163 +    json_begin_aggregate (sp, '[', ']');\r
164 +}\r
165 +\r
166 +static void\r
167 +json_end (struct sprinter *sp)\r
168 +{\r
169 +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
170 +    struct json_state *state = spj->state;\r
171 +\r
172 +    fputc (spj->state->close, spj->stream);\r
173 +    spj->state = state->parent;\r
174 +    talloc_free (state);\r
175 +    if (spj->state == NULL)\r
176 +       fputc ('\n', spj->stream);\r
177 +}\r
178 +\r
179 +static void\r
180 +json_string (struct sprinter *sp, const char *val)\r
181 +{\r
182 +    static const char *const escapes[] = {\r
183 +       ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
184 +       ['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"\r
185 +    };\r
186 +    struct sprinter_json *spj = json_begin_value (sp);\r
187 +\r
188 +    fputc ('"', spj->stream);\r
189 +    for (; *val; ++val) {\r
190 +       unsigned char ch = *val;\r
191 +       if (ch < ARRAY_SIZE (escapes) && escapes[ch])\r
192 +           fputs (escapes[ch], spj->stream);\r
193 +       else if (ch >= 32)\r
194 +           fputc (ch, spj->stream);\r
195 +       else\r
196 +           fprintf (spj->stream, "\\u%04x", ch);\r
197 +    }\r
198 +    fputc ('"', spj->stream);\r
199 +}\r
200 +\r
201 +static void\r
202 +json_integer (struct sprinter *sp, int val)\r
203 +{\r
204 +    struct sprinter_json *spj = json_begin_value (sp);\r
205 +\r
206 +    fprintf (spj->stream, "%d", val);\r
207 +}\r
208 +\r
209 +static void\r
210 +json_boolean (struct sprinter *sp, notmuch_bool_t val)\r
211 +{\r
212 +    struct sprinter_json *spj = json_begin_value (sp);\r
213 +\r
214 +    fputs (val ? "true" : "false", spj->stream);\r
215 +}\r
216 +\r
217 +static void\r
218 +json_null (struct sprinter *sp)\r
219 +{\r
220 +    struct sprinter_json *spj = json_begin_value (sp);\r
221 +\r
222 +    fputs ("null", spj->stream);\r
223 +}\r
224 +\r
225 +static void\r
226 +json_map_key (struct sprinter *sp, const char *key)\r
227 +{\r
228 +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
229 +\r
230 +    json_string (sp, key);\r
231 +    fputs (": ", spj->stream);\r
232 +    spj->state->first = TRUE;\r
233 +}\r
234 +\r
235 +static void\r
236 +json_set_prefix (unused (struct sprinter *sp), unused (const char *name))\r
237 +{\r
238 +}\r
239 +\r
240 +static void\r
241 +json_separator (struct sprinter *sp)\r
242 +{\r
243 +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
244 +\r
245 +    spj->insert_separator = TRUE;\r
246 +}\r
247 +\r
248 +static notmuch_bool_t\r
249 +json_is_text_printer (unused (struct sprinter *sp))\r
250 +{\r
251 +    return FALSE;\r
252 +}\r
253 +\r
254 +struct sprinter *\r
255 +sprinter_json_create (const void *ctx, FILE *stream)\r
256 +{\r
257 +    static const struct sprinter_json template = {\r
258 +       .vtable = {\r
259 +           .begin_map = json_begin_map,\r
260 +           .begin_list = json_begin_list,\r
261 +           .end = json_end,\r
262 +           .string = json_string,\r
263 +           .integer = json_integer,\r
264 +           .boolean = json_boolean,\r
265 +           .null = json_null,\r
266 +           .map_key = json_map_key,\r
267 +           .separator = json_separator,\r
268 +           .set_prefix = json_set_prefix,\r
269 +           .is_text_printer = json_is_text_printer,\r
270 +       }\r
271 +    };\r
272 +    struct sprinter_json *res;\r
273 +\r
274 +    res = talloc (ctx, struct sprinter_json);\r
275 +    if (! res)\r
276 +       return NULL;\r
277 +\r
278 +    *res = template;\r
279 +    res->stream = stream;\r
280 +    return &res->vtable;\r
281 +}\r
282 diff --git a/sprinter-text-search.c b/sprinter-text-search.c\r
283 new file mode 100644\r
284 index 0000000..b115722\r
285 --- /dev/null\r
286 +++ b/sprinter-text-search.c\r
287 @@ -0,0 +1,146 @@\r
288 +#include <stdbool.h>\r
289 +#include <stdio.h>\r
290 +#include <talloc.h>\r
291 +#include "sprinter.h"\r
292 +\r
293 +/* "Structured printer" interface for unstructured text printing.\r
294 + * Note that --output=summary is dispatched and formatted in\r
295 + * notmuch-search.c, the code in this file is only used for all other\r
296 + * output types.\r
297 + */\r
298 +\r
299 +struct sprinter_text_search {\r
300 +    struct sprinter vtable;\r
301 +    FILE *stream;\r
302 +\r
303 +    /* The current prefix to be printed with string/integer/boolean\r
304 +     * data.\r
305 +     */\r
306 +    const char *current_prefix;\r
307 +\r
308 +    /* A flag to indicate if this is the first tag. Used in list of tags\r
309 +     * for summary.\r
310 +     */\r
311 +    notmuch_bool_t first_tag;\r
312 +};\r
313 +\r
314 +static void\r
315 +print_sanitized_string (FILE *stream, const char *str)\r
316 +{\r
317 +    if (NULL == str)\r
318 +       return;\r
319 +\r
320 +    for (; *str; str++) {\r
321 +       if ((unsigned char) (*str) < 32)\r
322 +           fputc ('?', stream);\r
323 +       else\r
324 +           fputc (*str, stream);\r
325 +    }\r
326 +}\r
327 +\r
328 +static void\r
329 +text_search_string (struct sprinter *sp, const char *val)\r
330 +{\r
331 +    struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp;\r
332 +\r
333 +    if (sptxt->current_prefix != NULL)\r
334 +       fprintf (sptxt->stream, "%s:", sptxt->current_prefix);\r
335 +\r
336 +    print_sanitized_string (sptxt->stream, val);\r
337 +}\r
338 +\r
339 +static void\r
340 +text_search_integer (struct sprinter *sp, int val)\r
341 +{\r
342 +    struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp;\r
343 +\r
344 +    fprintf (sptxt->stream, "%d", val);\r
345 +}\r
346 +\r
347 +static void\r
348 +text_search_boolean (struct sprinter *sp, notmuch_bool_t val)\r
349 +{\r
350 +    struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp;\r
351 +\r
352 +    fputs (val ? "true" : "false", sptxt->stream);\r
353 +}\r
354 +\r
355 +static void\r
356 +text_search_separator (struct sprinter *sp)\r
357 +{\r
358 +    struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp;\r
359 +\r
360 +    fputc ('\n', sptxt->stream);\r
361 +}\r
362 +\r
363 +static void\r
364 +text_search_set_prefix (struct sprinter *sp, const char *prefix)\r
365 +{\r
366 +    struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp;\r
367 +\r
368 +    sptxt->current_prefix = prefix;\r
369 +}\r
370 +\r
371 +static notmuch_bool_t\r
372 +text_search_is_text_printer (unused (struct sprinter *sp))\r
373 +{\r
374 +    return TRUE;\r
375 +}\r
376 +\r
377 +/* The structure functions begin_map, begin_list, end and map_key\r
378 + * don't do anything in the text formatter.\r
379 + */\r
380 +\r
381 +static void\r
382 +text_search_begin_map (unused (struct sprinter *sp))\r
383 +{\r
384 +}\r
385 +\r
386 +static void\r
387 +text_search_begin_list (unused (struct sprinter *sp))\r
388 +{\r
389 +}\r
390 +\r
391 +static void\r
392 +text_search_end (unused (struct sprinter *sp))\r
393 +{\r
394 +}\r
395 +\r
396 +static void\r
397 +text_search_null (unused (struct sprinter *sp))\r
398 +{\r
399 +}\r
400 +\r
401 +static void\r
402 +text_search_map_key (unused (struct sprinter *sp), unused (const char *key))\r
403 +{\r
404 +}\r
405 +\r
406 +struct sprinter *\r
407 +sprinter_text_search_create (const void *ctx, FILE *stream)\r
408 +{\r
409 +    static const struct sprinter_text_search template = {\r
410 +       .vtable = {\r
411 +           .begin_map = text_search_begin_map,\r
412 +           .begin_list = text_search_begin_list,\r
413 +           .end = text_search_end,\r
414 +           .string = text_search_string,\r
415 +           .integer = text_search_integer,\r
416 +           .boolean = text_search_boolean,\r
417 +           .null = text_search_null,\r
418 +           .map_key = text_search_map_key,\r
419 +           .separator = text_search_separator,\r
420 +           .set_prefix = text_search_set_prefix,\r
421 +           .is_text_printer = text_search_is_text_printer,\r
422 +       }\r
423 +    };\r
424 +    struct sprinter_text_search *res;\r
425 +\r
426 +    res = talloc (ctx, struct sprinter_text_search);\r
427 +    if (! res)\r
428 +       return NULL;\r
429 +\r
430 +    *res = template;\r
431 +    res->stream = stream;\r
432 +    return &res->vtable;\r
433 +}\r
434 diff --git a/sprinter.h b/sprinter.h\r
435 index dc09a15..7ec6344 100644\r
436 --- a/sprinter.h\r
437 +++ b/sprinter.h\r
438 @@ -57,4 +57,13 @@ typedef struct sprinter {\r
439      notmuch_bool_t (*is_text_printer) (struct sprinter *);\r
440  } sprinter_t;\r
441  \r
442 +/* Create a new unstructured printer that emits the default text format\r
443 + * for "notmuch search". */\r
444 +struct sprinter *\r
445 +sprinter_text_search_create (const void *ctx, FILE *stream);\r
446 +\r
447 +/* Create a new structure printer that emits JSON. */\r
448 +struct sprinter *\r
449 +sprinter_json_create (const void *ctx, FILE *stream);\r
450 +\r
451  #endif // NOTMUCH_SPRINTER_H\r
452 -- \r
453 1.7.11.1\r
454 \r