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
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
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
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
52 This commit adds a structured output printer for Lisp
\r
53 S-Expressions. Later commits will use this printer in notmuch search,
\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
63 Makefile.local | 1 +
\r
64 sprinter-sexp.c | 250 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
66 3 files changed, 255 insertions(+)
\r
67 create mode 100644 sprinter-sexp.c
\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
81 diff --git a/sprinter-sexp.c b/sprinter-sexp.c
\r
82 new file mode 100644
\r
83 index 0000000..6d6bbad
\r
85 +++ b/sprinter-sexp.c
\r
87 +/* notmuch - Not much of an email program, (just index and search)
\r
89 + * Copyright © 2012 Carl Worth
\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
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
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
104 + * Author: Carl Worth <cworth@cworth.org>
\r
107 +#include <stdbool.h>
\r
108 +#include <stdio.h>
\r
109 +#include <talloc.h>
\r
110 +#include "sprinter.h"
\r
112 +struct sprinter_sexp {
\r
113 + struct sprinter vtable;
\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
119 + /* A flag to signify that a separator should be inserted in the
\r
120 + * output as soon as possible.
\r
122 + notmuch_bool_t insert_separator;
\r
125 +struct sexp_state {
\r
126 + struct sexp_state *parent;
\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
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
136 + /* The character that closes the current aggregate. */
\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
145 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\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
153 + if (! sps->state->in_map)
\r
154 + fputc (' ', sps->stream);
\r
157 + sps->state->first = FALSE;
\r
163 +/* Helper function to begin an aggregate type. Prints the open
\r
164 + * character and pushes a new state frame. */
\r
166 +sexp_begin_aggregate (struct sprinter *sp, char open, char close)
\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
179 +sexp_begin_map (struct sprinter *sp)
\r
181 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
182 + sexp_begin_aggregate (sp, '(', ')');
\r
183 + sps->state->in_map = TRUE;
\r
187 +sexp_begin_list (struct sprinter *sp)
\r
189 + sexp_begin_aggregate (sp, '(', ')');
\r
193 +sexp_end (struct sprinter *sp)
\r
195 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
196 + struct sexp_state *state = sps->state;
\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
208 +sexp_string_len_internal (struct sprinter *sp, const char *val, size_t len, notmuch_bool_t quote)
\r
210 + static const char *const escapes[] = {
\r
211 + ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",
\r
212 + ['\f'] = "\\f", ['\n'] = "\\n", ['\t'] = "\\t"
\r
214 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\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
225 + fprintf (sps->stream, "\\u%04x", ch);
\r
228 + fputc ('"', sps->stream);
\r
232 +sexp_string_len (struct sprinter *sp, const char *val, size_t len)
\r
234 + sexp_string_len_internal (sp, val, len, TRUE); /* print quoted */
\r
238 +sexp_symbol_len (struct sprinter *sp, const char *val, size_t len)
\r
240 + sexp_string_len_internal (sp, val, len, FALSE); /* print unquoted */
\r
244 +sexp_string (struct sprinter *sp, const char *val)
\r
248 + sexp_string_len (sp, val, strlen (val));
\r
252 +sexp_symbol (struct sprinter *sp, const char *val)
\r
256 + sexp_symbol_len (sp, val, strlen (val));
\r
260 +sexp_integer (struct sprinter *sp, int val)
\r
262 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
264 + fprintf (sps->stream, "%d", val);
\r
268 +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)
\r
270 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
272 + fputs (val ? "t" : "nil", sps->stream);
\r
276 +sexp_null (struct sprinter *sp)
\r
278 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
280 + fputs ("nil", sps->stream);
\r
284 +sexp_map_key (struct sprinter *sp, const char *key)
\r
286 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\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
296 +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))
\r
301 +sexp_separator (struct sprinter *sp)
\r
303 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
305 + sps->insert_separator = TRUE;
\r
309 +sprinter_sexp_create (const void *ctx, FILE *stream)
\r
311 + static const struct sprinter_sexp template = {
\r
313 + .begin_map = sexp_begin_map,
\r
314 + .begin_list = sexp_begin_list,
\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
327 + struct sprinter_sexp *res;
\r
329 + res = talloc (ctx, struct sprinter_sexp);
\r
334 + res->stream = stream;
\r
335 + return &res->vtable;
\r
337 diff --git a/sprinter.h b/sprinter.h
\r
338 index 912a526..59776a9 100644
\r
341 @@ -70,4 +70,8 @@ sprinter_text_create (const void *ctx, FILE *stream);
\r
343 sprinter_json_create (const void *ctx, FILE *stream);
\r
345 +/* Create a new structure printer that emits S-Expressions. */
\r
347 +sprinter_sexp_create (const void *ctx, FILE *stream);
\r
349 #endif // NOTMUCH_SPRINTER_H
\r