Re: [PATCH v4 01/16] add util/search-path.{c, h} to test for executables in $PATH
[notmuch-archives.git] / ee / 74dbb84337de1d303be53161ca8c05e08e359e
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
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 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
23         [83.164.197.182]\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
27         4TVSVY4y4htGHU\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
39 X-Y-GMX-Trusted: 0\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: Mon, 23 Jul 2012 10:38:19 -0000\r
53 \r
54 From: <craven@gmx.net>\r
55 \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
61 \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
64 ---\r
65  Makefile.local  |   2 +\r
66  sprinter-json.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++\r
67  sprinter-text.c | 126 ++++++++++++++++++++++++++++++++\r
68  sprinter.h      |  10 +++\r
69  4 files changed, 325 insertions(+)\r
70 \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
76         notmuch-show.c          \\r
77         notmuch-tag.c           \\r
78         notmuch-time.c          \\r
79 +       sprinter-json.c         \\r
80 +       sprinter-text.c         \\r
81         query-string.c          \\r
82         mime-node.c             \\r
83         crypto.c                \\r
84 diff --git a/sprinter-json.c b/sprinter-json.c\r
85 new file mode 100644\r
86 index 0000000..4649655\r
87 --- /dev/null\r
88 +++ b/sprinter-json.c\r
89 @@ -0,0 +1,187 @@\r
90 +#include <stdbool.h>\r
91 +#include <stdio.h>\r
92 +#include <talloc.h>\r
93 +#include "sprinter.h"\r
94 +\r
95 +struct sprinter_json {\r
96 +    struct sprinter vtable;\r
97 +    FILE *stream;\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
101 +\r
102 +    /* A flag to signify that a separator should be inserted in the\r
103 +     * output as soon as possible.\r
104 +     */\r
105 +    notmuch_bool_t insert_separator;\r
106 +};\r
107 +\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
114 +    char close;\r
115 +};\r
116 +\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
121 +{\r
122 +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
123 +\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
130 +           } else {\r
131 +               fputc (' ', spj->stream);\r
132 +           }\r
133 +       } else {\r
134 +           spj->state->first = FALSE;\r
135 +       }\r
136 +    }\r
137 +    return spj;\r
138 +}\r
139 +\r
140 +/* Helper function to begin an aggregate type.  Prints the open\r
141 + * character and pushes a new state frame. */\r
142 +static void\r
143 +json_begin_aggregate (struct sprinter *sp, char open, char close)\r
144 +{\r
145 +    struct sprinter_json *spj = json_begin_value (sp);\r
146 +    struct json_state *state = talloc (spj, struct json_state);\r
147 +\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
153 +}\r
154 +\r
155 +static void\r
156 +json_begin_map (struct sprinter *sp)\r
157 +{\r
158 +    json_begin_aggregate (sp, '{', '}');\r
159 +}\r
160 +\r
161 +static void\r
162 +json_begin_list (struct sprinter *sp)\r
163 +{\r
164 +    json_begin_aggregate (sp, '[', ']');\r
165 +}\r
166 +\r
167 +static void\r
168 +json_end (struct sprinter *sp)\r
169 +{\r
170 +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
171 +    struct json_state *state = spj->state;\r
172 +\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
178 +}\r
179 +\r
180 +static void\r
181 +json_string (struct sprinter *sp, const char *val)\r
182 +{\r
183 +    static const char *const escapes[] = {\r
184 +       ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
185 +       ['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"\r
186 +    };\r
187 +    struct sprinter_json *spj = json_begin_value (sp);\r
188 +\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
196 +       else\r
197 +           fprintf (spj->stream, "\\u%04x", ch);\r
198 +    }\r
199 +    fputc ('"', spj->stream);\r
200 +}\r
201 +\r
202 +static void\r
203 +json_integer (struct sprinter *sp, int val)\r
204 +{\r
205 +    struct sprinter_json *spj = json_begin_value (sp);\r
206 +\r
207 +    fprintf (spj->stream, "%d", val);\r
208 +}\r
209 +\r
210 +static void\r
211 +json_boolean (struct sprinter *sp, notmuch_bool_t val)\r
212 +{\r
213 +    struct sprinter_json *spj = json_begin_value (sp);\r
214 +\r
215 +    fputs (val ? "true" : "false", spj->stream);\r
216 +}\r
217 +\r
218 +static void\r
219 +json_null (struct sprinter *sp)\r
220 +{\r
221 +    struct sprinter_json *spj = json_begin_value (sp);\r
222 +\r
223 +    fputs ("null", spj->stream);\r
224 +}\r
225 +\r
226 +static void\r
227 +json_map_key (struct sprinter *sp, const char *key)\r
228 +{\r
229 +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
230 +\r
231 +    json_string (sp, key);\r
232 +    fputs (": ", spj->stream);\r
233 +    spj->state->first = TRUE;\r
234 +}\r
235 +\r
236 +static void\r
237 +json_set_prefix (unused (struct sprinter *sp), unused (const char *name))\r
238 +{\r
239 +}\r
240 +\r
241 +static void\r
242 +json_separator (struct sprinter *sp)\r
243 +{\r
244 +    struct sprinter_json *spj = (struct sprinter_json *) sp;\r
245 +\r
246 +    spj->insert_separator = TRUE;\r
247 +}\r
248 +\r
249 +struct sprinter *\r
250 +sprinter_json_create (const void *ctx, FILE *stream)\r
251 +{\r
252 +    static const struct sprinter_json template = {\r
253 +       .vtable = {\r
254 +           .begin_map = json_begin_map,\r
255 +           .begin_list = json_begin_list,\r
256 +           .end = json_end,\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
265 +       }\r
266 +    };\r
267 +    struct sprinter_json *res;\r
268 +\r
269 +    res = talloc (ctx, struct sprinter_json);\r
270 +    if (! res)\r
271 +       return NULL;\r
272 +\r
273 +    *res = template;\r
274 +    res->stream = stream;\r
275 +    return &res->vtable;\r
276 +}\r
277 diff --git a/sprinter-text.c b/sprinter-text.c\r
278 new file mode 100644\r
279 index 0000000..b208840\r
280 --- /dev/null\r
281 +++ b/sprinter-text.c\r
282 @@ -0,0 +1,126 @@\r
283 +#include <stdbool.h>\r
284 +#include <stdio.h>\r
285 +#include <talloc.h>\r
286 +#include "sprinter.h"\r
287 +\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
291 + * output types.\r
292 + */\r
293 +\r
294 +struct sprinter_text {\r
295 +    struct sprinter vtable;\r
296 +    FILE *stream;\r
297 +\r
298 +    /* The current prefix to be printed with string/integer/boolean\r
299 +     * data.\r
300 +     */\r
301 +    const char *current_prefix;\r
302 +\r
303 +    /* A flag to indicate if this is the first tag. Used in list of tags\r
304 +     * for summary.\r
305 +     */\r
306 +    notmuch_bool_t first_tag;\r
307 +};\r
308 +\r
309 +static void\r
310 +text_string (struct sprinter *sp, const char *val)\r
311 +{\r
312 +    struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
313 +\r
314 +    if (sptxt->current_prefix != NULL)\r
315 +       fprintf (sptxt->stream, "%s:", sptxt->current_prefix);\r
316 +\r
317 +    fputs(val, sptxt->stream);\r
318 +}\r
319 +\r
320 +static void\r
321 +text_integer (struct sprinter *sp, int val)\r
322 +{\r
323 +    struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
324 +\r
325 +    fprintf (sptxt->stream, "%d", val);\r
326 +}\r
327 +\r
328 +static void\r
329 +text_boolean (struct sprinter *sp, notmuch_bool_t val)\r
330 +{\r
331 +    struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
332 +\r
333 +    fputs (val ? "true" : "false", sptxt->stream);\r
334 +}\r
335 +\r
336 +static void\r
337 +text_separator (struct sprinter *sp)\r
338 +{\r
339 +    struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
340 +\r
341 +    fputc ('\n', sptxt->stream);\r
342 +}\r
343 +\r
344 +static void\r
345 +text_set_prefix (struct sprinter *sp, const char *prefix)\r
346 +{\r
347 +    struct sprinter_text *sptxt = (struct sprinter_text *) sp;\r
348 +\r
349 +    sptxt->current_prefix = prefix;\r
350 +}\r
351 +\r
352 +/* The structure functions begin_map, begin_list, end and map_key\r
353 + * don't do anything in the text formatter.\r
354 + */\r
355 +\r
356 +static void\r
357 +text_begin_map (unused (struct sprinter *sp))\r
358 +{\r
359 +}\r
360 +\r
361 +static void\r
362 +text_begin_list (unused (struct sprinter *sp))\r
363 +{\r
364 +}\r
365 +\r
366 +static void\r
367 +text_end (unused (struct sprinter *sp))\r
368 +{\r
369 +}\r
370 +\r
371 +static void\r
372 +text_null (unused (struct sprinter *sp))\r
373 +{\r
374 +}\r
375 +\r
376 +static void\r
377 +text_map_key (unused (struct sprinter *sp), unused (const char *key))\r
378 +{\r
379 +}\r
380 +\r
381 +struct sprinter *\r
382 +sprinter_text_create (const void *ctx, FILE *stream)\r
383 +{\r
384 +    static const struct sprinter_text template = {\r
385 +       .vtable = {\r
386 +           .begin_map = text_begin_map,\r
387 +           .begin_list = text_begin_list,\r
388 +           .end = text_end,\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
397 +       },\r
398 +    };\r
399 +    struct sprinter_text *res;\r
400 +\r
401 +    res = talloc (ctx, struct sprinter_text);\r
402 +    if (! res)\r
403 +       return NULL;\r
404 +\r
405 +    *res = template;\r
406 +    res->stream = stream;\r
407 +    return &res->vtable;\r
408 +}\r
409 diff --git a/sprinter.h b/sprinter.h\r
410 index 77dc26f..6680d41 100644\r
411 --- a/sprinter.h\r
412 +++ b/sprinter.h\r
413 @@ -55,4 +55,14 @@ typedef struct sprinter {\r
414      notmuch_bool_t is_text_printer;\r
415  } sprinter_t;\r
416  \r
417 +\r
418 +/* Create a new unstructured printer that emits the default text format\r
419 + * for "notmuch search". */\r
420 +struct sprinter *\r
421 +sprinter_text_create (const void *ctx, FILE *stream);\r
422 +\r
423 +/* Create a new structure printer that emits JSON. */\r
424 +struct sprinter *\r
425 +sprinter_json_create (const void *ctx, FILE *stream);\r
426 +\r
427  #endif // NOTMUCH_SPRINTER_H\r
428 -- \r
429 1.7.11.2\r
430 \r