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 185E0431FAF
\r
6 for <notmuch@notmuchmail.org>; Mon, 23 Jul 2012 03:38:19 -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 kYT4aPoe70dx for <notmuch@notmuchmail.org>;
\r
17 Mon, 23 Jul 2012 03:38:17 -0700 (PDT)
\r
18 Received: from mailout-de.gmx.net (mailout-de.gmx.net [213.165.64.23])
\r
19 by olra.theworths.org (Postfix) with SMTP id 276A2431FAE
\r
20 for <notmuch@notmuchmail.org>; Mon, 23 Jul 2012 03:38:17 -0700 (PDT)
\r
21 Received: (qmail invoked by alias); 23 Jul 2012 10:38:15 -0000
\r
22 Received: from gw.arelion.cust.net.lagis.at (EHLO dodekanex.arelion.at)
\r
24 by mail.gmx.net (mp071) with SMTP; 23 Jul 2012 12:38:15 +0200
\r
25 X-Authenticated: #201305
\r
26 X-Provags-ID: V01U2FsdGVkX1+YUQ8honOYuWCeL42Obr4DGJgqN+GyHAr6k0icdC
\r
28 Received: by dodekanex.arelion.at (Postfix, from userid 1000)
\r
29 id EAA873034D4; Mon, 23 Jul 2012 12:39:50 +0200 (CEST)
\r
30 From: craven@gmx.net
\r
31 To: notmuch@notmuchmail.org
\r
32 Subject: [PATCH v8 2/3] Add structured output formatter for JSON and plain
\r
33 text (but don't use them yet).
\r
34 Date: Mon, 23 Jul 2012 12:39:45 +0200
\r
35 Message-Id: <1343039986-2732-3-git-send-email-craven@gmx.net>
\r
36 X-Mailer: git-send-email 1.7.11.2
\r
37 In-Reply-To: <1343039986-2732-1-git-send-email-craven@gmx.net>
\r
38 References: <1343039986-2732-1-git-send-email-craven@gmx.net>
\r
40 X-BeenThere: notmuch@notmuchmail.org
\r
41 X-Mailman-Version: 2.1.13
\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: Mon, 23 Jul 2012 10:38:19 -0000
\r
54 From: <craven@gmx.net>
\r
56 Using the new structured printer support in sprinter.h, implement
\r
57 sprinter_json_create, which returns a new JSON structured output
\r
58 formatter. The formatter prints output similar to the existing JSON, but
\r
59 with differences in whitespace (mostly newlines, --output=summary prints
\r
60 the entire message summary on one line, not split across multiple lines).
\r
62 Also implement a "structured" formatter for plain text that prints
\r
63 prefixed strings, to be used with notmuch-search.c plain text output.
\r
65 Makefile.local | 2 +
\r
66 sprinter-json.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++
\r
67 sprinter-text.c | 126 ++++++++++++++++++++++++++++++++
\r
69 4 files changed, 325 insertions(+)
\r
71 diff --git a/Makefile.local b/Makefile.local
\r
72 index a890df2..296995d 100644
\r
73 --- a/Makefile.local
\r
74 +++ b/Makefile.local
\r
75 @@ -290,6 +290,8 @@ notmuch_client_srcs = \
\r
84 diff --git a/sprinter-json.c b/sprinter-json.c
\r
85 new file mode 100644
\r
86 index 0000000..4649655
\r
88 +++ b/sprinter-json.c
\r
90 +#include <stdbool.h>
\r
92 +#include <talloc.h>
\r
93 +#include "sprinter.h"
\r
95 +struct sprinter_json {
\r
96 + struct sprinter vtable;
\r
98 + /* Top of the state stack, or NULL if the printer is not currently
\r
99 + * inside any aggregate types. */
\r
100 + struct json_state *state;
\r
102 + /* A flag to signify that a separator should be inserted in the
\r
103 + * output as soon as possible.
\r
105 + notmuch_bool_t insert_separator;
\r
108 +struct json_state {
\r
109 + struct json_state *parent;
\r
110 + /* True if nothing has been printed in this aggregate yet.
\r
111 + * Suppresses the comma before a value. */
\r
112 + notmuch_bool_t first;
\r
113 + /* The character that closes the current aggregate. */
\r
117 +/* Helper function to set up the stream to print a value. If this
\r
118 + * value follows another value, prints a comma. */
\r
119 +static struct sprinter_json *
\r
120 +json_begin_value (struct sprinter *sp)
\r
122 + struct sprinter_json *spj = (struct sprinter_json *) sp;
\r
124 + if (spj->state) {
\r
125 + if (! spj->state->first) {
\r
126 + fputc (',', spj->stream);
\r
127 + if (spj->insert_separator) {
\r
128 + fputc ('\n', spj->stream);
\r
129 + spj->insert_separator = FALSE;
\r
131 + fputc (' ', spj->stream);
\r
134 + spj->state->first = FALSE;
\r
140 +/* Helper function to begin an aggregate type. Prints the open
\r
141 + * character and pushes a new state frame. */
\r
143 +json_begin_aggregate (struct sprinter *sp, char open, char close)
\r
145 + struct sprinter_json *spj = json_begin_value (sp);
\r
146 + struct json_state *state = talloc (spj, struct json_state);
\r
148 + fputc (open, spj->stream);
\r
149 + state->parent = spj->state;
\r
150 + state->first = TRUE;
\r
151 + state->close = close;
\r
152 + spj->state = state;
\r
156 +json_begin_map (struct sprinter *sp)
\r
158 + json_begin_aggregate (sp, '{', '}');
\r
162 +json_begin_list (struct sprinter *sp)
\r
164 + json_begin_aggregate (sp, '[', ']');
\r
168 +json_end (struct sprinter *sp)
\r
170 + struct sprinter_json *spj = (struct sprinter_json *) sp;
\r
171 + struct json_state *state = spj->state;
\r
173 + fputc (spj->state->close, spj->stream);
\r
174 + spj->state = state->parent;
\r
175 + talloc_free (state);
\r
176 + if (spj->state == NULL)
\r
177 + fputc ('\n', spj->stream);
\r
181 +json_string (struct sprinter *sp, const char *val)
\r
183 + static const char *const escapes[] = {
\r
184 + ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",
\r
185 + ['\f'] = "\\f", ['\n'] = "\\n", ['\t'] = "\\t"
\r
187 + struct sprinter_json *spj = json_begin_value (sp);
\r
189 + fputc ('"', spj->stream);
\r
190 + for (; *val; ++val) {
\r
191 + unsigned char ch = *val;
\r
192 + if (ch < ARRAY_SIZE (escapes) && escapes[ch])
\r
193 + fputs (escapes[ch], spj->stream);
\r
194 + else if (ch >= 32)
\r
195 + fputc (ch, spj->stream);
\r
197 + fprintf (spj->stream, "\\u%04x", ch);
\r
199 + fputc ('"', spj->stream);
\r
203 +json_integer (struct sprinter *sp, int val)
\r
205 + struct sprinter_json *spj = json_begin_value (sp);
\r
207 + fprintf (spj->stream, "%d", val);
\r
211 +json_boolean (struct sprinter *sp, notmuch_bool_t val)
\r
213 + struct sprinter_json *spj = json_begin_value (sp);
\r
215 + fputs (val ? "true" : "false", spj->stream);
\r
219 +json_null (struct sprinter *sp)
\r
221 + struct sprinter_json *spj = json_begin_value (sp);
\r
223 + fputs ("null", spj->stream);
\r
227 +json_map_key (struct sprinter *sp, const char *key)
\r
229 + struct sprinter_json *spj = (struct sprinter_json *) sp;
\r
231 + json_string (sp, key);
\r
232 + fputs (": ", spj->stream);
\r
233 + spj->state->first = TRUE;
\r
237 +json_set_prefix (unused (struct sprinter *sp), unused (const char *name))
\r
242 +json_separator (struct sprinter *sp)
\r
244 + struct sprinter_json *spj = (struct sprinter_json *) sp;
\r
246 + spj->insert_separator = TRUE;
\r
250 +sprinter_json_create (const void *ctx, FILE *stream)
\r
252 + static const struct sprinter_json template = {
\r
254 + .begin_map = json_begin_map,
\r
255 + .begin_list = json_begin_list,
\r
257 + .string = json_string,
\r
258 + .integer = json_integer,
\r
259 + .boolean = json_boolean,
\r
260 + .null = json_null,
\r
261 + .map_key = json_map_key,
\r
262 + .separator = json_separator,
\r
263 + .set_prefix = json_set_prefix,
\r
264 + .is_text_printer = FALSE,
\r
267 + struct sprinter_json *res;
\r
269 + res = talloc (ctx, struct sprinter_json);
\r
274 + res->stream = stream;
\r
275 + return &res->vtable;
\r
277 diff --git a/sprinter-text.c b/sprinter-text.c
\r
278 new file mode 100644
\r
279 index 0000000..b208840
\r
281 +++ b/sprinter-text.c
\r
283 +#include <stdbool.h>
\r
284 +#include <stdio.h>
\r
285 +#include <talloc.h>
\r
286 +#include "sprinter.h"
\r
288 +/* "Structured printer" interface for unstructured text printing.
\r
289 + * Note that --output=summary is dispatched and formatted in
\r
290 + * notmuch-search.c, the code in this file is only used for all other
\r
294 +struct sprinter_text {
\r
295 + struct sprinter vtable;
\r
298 + /* The current prefix to be printed with string/integer/boolean
\r
301 + const char *current_prefix;
\r
303 + /* A flag to indicate if this is the first tag. Used in list of tags
\r
306 + notmuch_bool_t first_tag;
\r
310 +text_string (struct sprinter *sp, const char *val)
\r
312 + struct sprinter_text *sptxt = (struct sprinter_text *) sp;
\r
314 + if (sptxt->current_prefix != NULL)
\r
315 + fprintf (sptxt->stream, "%s:", sptxt->current_prefix);
\r
317 + fputs(val, sptxt->stream);
\r
321 +text_integer (struct sprinter *sp, int val)
\r
323 + struct sprinter_text *sptxt = (struct sprinter_text *) sp;
\r
325 + fprintf (sptxt->stream, "%d", val);
\r
329 +text_boolean (struct sprinter *sp, notmuch_bool_t val)
\r
331 + struct sprinter_text *sptxt = (struct sprinter_text *) sp;
\r
333 + fputs (val ? "true" : "false", sptxt->stream);
\r
337 +text_separator (struct sprinter *sp)
\r
339 + struct sprinter_text *sptxt = (struct sprinter_text *) sp;
\r
341 + fputc ('\n', sptxt->stream);
\r
345 +text_set_prefix (struct sprinter *sp, const char *prefix)
\r
347 + struct sprinter_text *sptxt = (struct sprinter_text *) sp;
\r
349 + sptxt->current_prefix = prefix;
\r
352 +/* The structure functions begin_map, begin_list, end and map_key
\r
353 + * don't do anything in the text formatter.
\r
357 +text_begin_map (unused (struct sprinter *sp))
\r
362 +text_begin_list (unused (struct sprinter *sp))
\r
367 +text_end (unused (struct sprinter *sp))
\r
372 +text_null (unused (struct sprinter *sp))
\r
377 +text_map_key (unused (struct sprinter *sp), unused (const char *key))
\r
382 +sprinter_text_create (const void *ctx, FILE *stream)
\r
384 + static const struct sprinter_text template = {
\r
386 + .begin_map = text_begin_map,
\r
387 + .begin_list = text_begin_list,
\r
389 + .string = text_string,
\r
390 + .integer = text_integer,
\r
391 + .boolean = text_boolean,
\r
392 + .null = text_null,
\r
393 + .map_key = text_map_key,
\r
394 + .separator = text_separator,
\r
395 + .set_prefix = text_set_prefix,
\r
396 + .is_text_printer = TRUE,
\r
399 + struct sprinter_text *res;
\r
401 + res = talloc (ctx, struct sprinter_text);
\r
406 + res->stream = stream;
\r
407 + return &res->vtable;
\r
409 diff --git a/sprinter.h b/sprinter.h
\r
410 index 77dc26f..6680d41 100644
\r
413 @@ -55,4 +55,14 @@ typedef struct sprinter {
\r
414 notmuch_bool_t is_text_printer;
\r
418 +/* Create a new unstructured printer that emits the default text format
\r
419 + * for "notmuch search". */
\r
421 +sprinter_text_create (const void *ctx, FILE *stream);
\r
423 +/* Create a new structure printer that emits JSON. */
\r
425 +sprinter_json_create (const void *ctx, FILE *stream);
\r
427 #endif // NOTMUCH_SPRINTER_H
\r