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 8F1BA431FBC
\r
6 for <notmuch@notmuchmail.org>; Thu, 6 Dec 2012 03:45:19 -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 mWooN+fDq2Yn for <notmuch@notmuchmail.org>;
\r
17 Thu, 6 Dec 2012 03:45:17 -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 363F6431FAE
\r
22 for <notmuch@notmuchmail.org>; Thu, 6 Dec 2012 03:45:17 -0800 (PST)
\r
23 Received: by mail.nexoid.at (Postfix, from userid 1000)
\r
24 id 42DBD11C10B; Thu, 6 Dec 2012 12:45:14 +0100 (CET)
\r
25 From: Peter Feigl <craven@gmx.net>
\r
26 To: notmuch@notmuchmail.org
\r
27 Subject: [PATCH v4 1/5] Adding an S-expression structured output printer.
\r
28 Date: Thu, 6 Dec 2012 12:44:55 +0100
\r
30 <1409adf6cbd310d7313bdc3a69e7e78c25859d81.1354794428.git.craven@gmx.net>
\r
31 X-Mailer: git-send-email 1.8.0
\r
32 In-Reply-To: <cover.1354794428.git.craven@gmx.net>
\r
33 References: <cover.1354794428.git.craven@gmx.net>
\r
34 In-Reply-To: <cover.1354794428.git.craven@gmx.net>
\r
35 References: <cover.1354794428.git.craven@gmx.net>
\r
37 Content-Type: text/plain; charset=UTF-8
\r
38 Content-Transfer-Encoding: 8bit
\r
39 X-BeenThere: notmuch@notmuchmail.org
\r
40 X-Mailman-Version: 2.1.13
\r
42 List-Id: "Use and development of the notmuch mail system."
\r
43 <notmuch.notmuchmail.org>
\r
44 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
45 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
46 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
47 List-Post: <mailto:notmuch@notmuchmail.org>
\r
48 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
49 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
50 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
51 X-List-Received-Date: Thu, 06 Dec 2012 11:45:19 -0000
\r
53 This commit adds a structured output printer for Lisp
\r
54 S-Expressions. Later commits will use this printer in notmuch search,
\r
57 The structure is the same as json, but:
\r
58 - arrays are written as lists: ("foo" "bar" "baaz" 1 2 3)
\r
59 - maps are written as p-lists: (:key "value" :other-key "other-value")
\r
60 - true is written as t
\r
61 - false is written as nil
\r
62 - null is written as nil
\r
64 Makefile.local | 1 +
\r
65 sprinter-sexp.c | 238 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
67 3 files changed, 243 insertions(+)
\r
68 create mode 100644 sprinter-sexp.c
\r
70 diff --git a/Makefile.local b/Makefile.local
\r
71 index 2b91946..0db1713 100644
\r
72 --- a/Makefile.local
\r
73 +++ b/Makefile.local
\r
74 @@ -270,6 +270,7 @@ notmuch_client_srcs = \
\r
82 diff --git a/sprinter-sexp.c b/sprinter-sexp.c
\r
83 new file mode 100644
\r
84 index 0000000..8f84eed
\r
86 +++ b/sprinter-sexp.c
\r
88 +/* notmuch - Not much of an email program, (just index and search)
\r
90 + * Copyright © 2012 Peter Feigl
\r
92 + * This program is free software: you can redistribute it and/or modify
\r
93 + * it under the terms of the GNU General Public License as published by
\r
94 + * the Free Software Foundation, either version 3 of the License, or
\r
95 + * (at your option) any later version.
\r
97 + * This program is distributed in the hope that it will be useful,
\r
98 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
99 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
100 + * GNU General Public License for more details.
\r
102 + * You should have received a copy of the GNU General Public License
\r
103 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
105 + * Author: Peter Feigl <peter.feigl@gmx.at>
\r
108 +#include <stdbool.h>
\r
109 +#include <stdio.h>
\r
110 +#include <talloc.h>
\r
111 +#include "sprinter.h"
\r
113 +struct sprinter_sexp {
\r
114 + struct sprinter vtable;
\r
116 + /* Top of the state stack, or NULL if the printer is not currently
\r
117 + * inside any aggregate types. */
\r
118 + struct sexp_state *state;
\r
120 + /* A flag to signify that a separator should be inserted in the
\r
121 + * 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
133 +/* Helper function to set up the stream to print a value. If this
\r
134 + * value follows another value, prints a space. */
\r
135 +static struct sprinter_sexp *
\r
136 +sexp_begin_value (struct sprinter *sp)
\r
138 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
140 + if (sps->state) {
\r
141 + if (! sps->state->first) {
\r
142 + if (sps->insert_separator) {
\r
143 + fputc ('\n', sps->stream);
\r
144 + sps->insert_separator = FALSE;
\r
146 + fputc (' ', sps->stream);
\r
149 + sps->state->first = FALSE;
\r
155 +/* Helper function to begin an aggregate type. Prints the open
\r
156 + * character and pushes a new state frame. */
\r
158 +sexp_begin_aggregate (struct sprinter *sp)
\r
160 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
161 + struct sexp_state *state = talloc (sps, struct sexp_state);
\r
162 + fputc ('(', sps->stream);
\r
163 + state->parent = sps->state;
\r
164 + state->first = TRUE;
\r
165 + sps->state = state;
\r
169 +sexp_begin_map (struct sprinter *sp)
\r
171 + sexp_begin_aggregate (sp);
\r
175 +sexp_begin_list (struct sprinter *sp)
\r
177 + sexp_begin_aggregate (sp);
\r
181 +sexp_end (struct sprinter *sp)
\r
183 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
184 + struct sexp_state *state = sps->state;
\r
186 + fputc (')', sps->stream);
\r
187 + sps->state = state->parent;
\r
188 + talloc_free (state);
\r
189 + if (sps->state == NULL)
\r
190 + fputc ('\n', sps->stream);
\r
194 +sexp_string_len (struct sprinter *sp, const char *val, size_t len)
\r
196 + /* Some characters need escaping. " and \ work fine in all Lisps,
\r
197 + * \n is not supported in CL, but all others work fine.
\r
198 + * Characters below 32 are printed as \123o (three-digit
\r
199 + * octals), which work fine in most Schemes and Emacs. */
\r
200 + static const char *const escapes[] = {
\r
201 + ['\"'] = "\\\"", ['\\'] = "\\\\", ['\n'] = "\\n"
\r
203 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
205 + fputc ('"', sps->stream);
\r
206 + for (; len; ++val, --len) {
\r
207 + unsigned char ch = *val;
\r
208 + if (ch < ARRAY_SIZE (escapes) && escapes[ch])
\r
209 + fputs (escapes[ch], sps->stream);
\r
210 + else if (ch >= 32)
\r
211 + fputc (ch, sps->stream);
\r
213 + fprintf (sps->stream, "\\%03oo", ch);
\r
215 + fputc ('"', sps->stream);
\r
219 +sexp_string (struct sprinter *sp, const char *val)
\r
223 + sexp_string_len (sp, val, strlen (val));
\r
226 +/* Prints a symbol, i.e. the name preceded by a colon. This should work
\r
227 + * in all Lisps, at least as a symbol, if not as a proper keyword */
\r
229 +sexp_symbol (struct sprinter *sp, const char *val)
\r
231 + static const char illegal_characters[] = {
\r
234 + unsigned int i = 0;
\r
235 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
238 + INTERNAL_ERROR ("illegal symbol NULL");
\r
240 + for(i = 0; i < ARRAY_SIZE (illegal_characters); i++) {
\r
241 + if(strchr(val, illegal_characters[i]) != NULL) {
\r
242 + INTERNAL_ERROR ("illegal character in symbol %s", val);
\r
245 + fputc (':', sps->stream);
\r
246 + fputs (val, sps->stream);
\r
250 +sexp_integer (struct sprinter *sp, int val)
\r
252 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
254 + fprintf (sps->stream, "%d", val);
\r
258 +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)
\r
260 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
262 + fputs (val ? "t" : "nil", sps->stream);
\r
266 +sexp_null (struct sprinter *sp)
\r
268 + struct sprinter_sexp *sps = sexp_begin_value (sp);
\r
270 + fputs ("nil", sps->stream);
\r
274 +sexp_map_key (struct sprinter *sp, const char *key)
\r
276 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
277 + if (sps->state && ! sps->state->first)
\r
278 + fputc (' ', sps->stream);
\r
280 + sps->state->first = FALSE;
\r
281 + sexp_symbol (sp, key);
\r
285 +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))
\r
290 +sexp_separator (struct sprinter *sp)
\r
292 + struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
\r
294 + sps->insert_separator = TRUE;
\r
298 +sprinter_sexp_create (const void *ctx, FILE *stream)
\r
300 + static const struct sprinter_sexp template = {
\r
302 + .begin_map = sexp_begin_map,
\r
303 + .begin_list = sexp_begin_list,
\r
305 + .string = sexp_string,
\r
306 + .string_len = sexp_string_len,
\r
307 + .integer = sexp_integer,
\r
308 + .boolean = sexp_boolean,
\r
309 + .null = sexp_null,
\r
310 + .map_key = sexp_map_key,
\r
311 + .separator = sexp_separator,
\r
312 + .set_prefix = sexp_set_prefix,
\r
313 + .is_text_printer = FALSE,
\r
316 + struct sprinter_sexp *res;
\r
318 + res = talloc (ctx, struct sprinter_sexp);
\r
323 + res->stream = stream;
\r
324 + return &res->vtable;
\r
326 diff --git a/sprinter.h b/sprinter.h
\r
327 index 912a526..59776a9 100644
\r
330 @@ -70,4 +70,8 @@ sprinter_text_create (const void *ctx, FILE *stream);
\r
332 sprinter_json_create (const void *ctx, FILE *stream);
\r
334 +/* Create a new structure printer that emits S-Expressions. */
\r
336 +sprinter_sexp_create (const void *ctx, FILE *stream);
\r
338 #endif // NOTMUCH_SPRINTER_H
\r