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
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
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
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
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
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
41 X-BeenThere: notmuch@notmuchmail.org
\r
42 X-Mailman-Version: 2.1.13
\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
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
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
64 Makefile.local | 2 +
\r
65 sprinter-json.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++++
\r
66 sprinter-text-search.c | 146 +++++++++++++++++++++++++++++++++++++
\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
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
80 + sprinter-text-search.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
89 +++ b/sprinter-json.c
\r
91 +#include <stdbool.h>
\r
93 +#include <talloc.h>
\r
94 +#include "sprinter.h"
\r
96 +struct sprinter_json {
\r
97 + struct sprinter vtable;
\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
103 + /* A flag to signify that a separator should be inserted in the
\r
104 + * output as soon as possible.
\r
106 + notmuch_bool_t insert_separator;
\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
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
123 + struct sprinter_json *spj = (struct sprinter_json *) sp;
\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
132 + fputc (' ', spj->stream);
\r
134 + spj->state->first = FALSE;
\r
139 +/* Helper function to begin an aggregate type. Prints the open
\r
140 + * character and pushes a new state frame. */
\r
142 +json_begin_aggregate (struct sprinter *sp, char open, char close)
\r
144 + struct sprinter_json *spj = json_begin_value (sp);
\r
145 + struct json_state *state = talloc (spj, struct json_state);
\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
155 +json_begin_map (struct sprinter *sp)
\r
157 + json_begin_aggregate (sp, '{', '}');
\r
161 +json_begin_list (struct sprinter *sp)
\r
163 + json_begin_aggregate (sp, '[', ']');
\r
167 +json_end (struct sprinter *sp)
\r
169 + struct sprinter_json *spj = (struct sprinter_json *) sp;
\r
170 + struct json_state *state = spj->state;
\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
180 +json_string (struct sprinter *sp, const char *val)
\r
182 + static const char *const escapes[] = {
\r
183 + ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",
\r
184 + ['\f'] = "\\f", ['\n'] = "\\n", ['\t'] = "\\t"
\r
186 + struct sprinter_json *spj = json_begin_value (sp);
\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
196 + fprintf (spj->stream, "\\u%04x", ch);
\r
198 + fputc ('"', spj->stream);
\r
202 +json_integer (struct sprinter *sp, int val)
\r
204 + struct sprinter_json *spj = json_begin_value (sp);
\r
206 + fprintf (spj->stream, "%d", val);
\r
210 +json_boolean (struct sprinter *sp, notmuch_bool_t val)
\r
212 + struct sprinter_json *spj = json_begin_value (sp);
\r
214 + fputs (val ? "true" : "false", spj->stream);
\r
218 +json_null (struct sprinter *sp)
\r
220 + struct sprinter_json *spj = json_begin_value (sp);
\r
222 + fputs ("null", spj->stream);
\r
226 +json_map_key (struct sprinter *sp, const char *key)
\r
228 + struct sprinter_json *spj = (struct sprinter_json *) sp;
\r
230 + json_string (sp, key);
\r
231 + fputs (": ", spj->stream);
\r
232 + spj->state->first = TRUE;
\r
236 +json_set_prefix (unused (struct sprinter *sp), unused (const char *name))
\r
241 +json_separator (struct sprinter *sp)
\r
243 + struct sprinter_json *spj = (struct sprinter_json *) sp;
\r
245 + spj->insert_separator = TRUE;
\r
248 +static notmuch_bool_t
\r
249 +json_is_text_printer (unused (struct sprinter *sp))
\r
255 +sprinter_json_create (const void *ctx, FILE *stream)
\r
257 + static const struct sprinter_json template = {
\r
259 + .begin_map = json_begin_map,
\r
260 + .begin_list = json_begin_list,
\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
272 + struct sprinter_json *res;
\r
274 + res = talloc (ctx, struct sprinter_json);
\r
279 + res->stream = stream;
\r
280 + return &res->vtable;
\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
286 +++ b/sprinter-text-search.c
\r
288 +#include <stdbool.h>
\r
289 +#include <stdio.h>
\r
290 +#include <talloc.h>
\r
291 +#include "sprinter.h"
\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
299 +struct sprinter_text_search {
\r
300 + struct sprinter vtable;
\r
303 + /* The current prefix to be printed with string/integer/boolean
\r
306 + const char *current_prefix;
\r
308 + /* A flag to indicate if this is the first tag. Used in list of tags
\r
311 + notmuch_bool_t first_tag;
\r
315 +print_sanitized_string (FILE *stream, const char *str)
\r
320 + for (; *str; str++) {
\r
321 + if ((unsigned char) (*str) < 32)
\r
322 + fputc ('?', stream);
\r
324 + fputc (*str, stream);
\r
329 +text_search_string (struct sprinter *sp, const char *val)
\r
331 + struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp;
\r
333 + if (sptxt->current_prefix != NULL)
\r
334 + fprintf (sptxt->stream, "%s:", sptxt->current_prefix);
\r
336 + print_sanitized_string (sptxt->stream, val);
\r
340 +text_search_integer (struct sprinter *sp, int val)
\r
342 + struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp;
\r
344 + fprintf (sptxt->stream, "%d", val);
\r
348 +text_search_boolean (struct sprinter *sp, notmuch_bool_t val)
\r
350 + struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp;
\r
352 + fputs (val ? "true" : "false", sptxt->stream);
\r
356 +text_search_separator (struct sprinter *sp)
\r
358 + struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp;
\r
360 + fputc ('\n', sptxt->stream);
\r
364 +text_search_set_prefix (struct sprinter *sp, const char *prefix)
\r
366 + struct sprinter_text_search *sptxt = (struct sprinter_text_search *) sp;
\r
368 + sptxt->current_prefix = prefix;
\r
371 +static notmuch_bool_t
\r
372 +text_search_is_text_printer (unused (struct sprinter *sp))
\r
377 +/* The structure functions begin_map, begin_list, end and map_key
\r
378 + * don't do anything in the text formatter.
\r
382 +text_search_begin_map (unused (struct sprinter *sp))
\r
387 +text_search_begin_list (unused (struct sprinter *sp))
\r
392 +text_search_end (unused (struct sprinter *sp))
\r
397 +text_search_null (unused (struct sprinter *sp))
\r
402 +text_search_map_key (unused (struct sprinter *sp), unused (const char *key))
\r
407 +sprinter_text_search_create (const void *ctx, FILE *stream)
\r
409 + static const struct sprinter_text_search template = {
\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
424 + struct sprinter_text_search *res;
\r
426 + res = talloc (ctx, struct sprinter_text_search);
\r
431 + res->stream = stream;
\r
432 + return &res->vtable;
\r
434 diff --git a/sprinter.h b/sprinter.h
\r
435 index dc09a15..7ec6344 100644
\r
438 @@ -57,4 +57,13 @@ typedef struct sprinter {
\r
439 notmuch_bool_t (*is_text_printer) (struct sprinter *);
\r
442 +/* Create a new unstructured printer that emits the default text format
\r
443 + * for "notmuch search". */
\r
445 +sprinter_text_search_create (const void *ctx, FILE *stream);
\r
447 +/* Create a new structure printer that emits JSON. */
\r
449 +sprinter_json_create (const void *ctx, FILE *stream);
\r
451 #endif // NOTMUCH_SPRINTER_H
\r