[PATCH 06/10] cli: Introduce "notmuch address" command
[notmuch-archives.git] / 1b / 07f8a000e8df00c744b808a9b0cfe0952877dc
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
9 X-Spam-Flag: NO\r
10 X-Spam-Score: 0.001\r
11 X-Spam-Level: \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
29 Message-Id:\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
36 MIME-Version: 1.0\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
41 Precedence: list\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
52 \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
55 show and reply.\r
56 \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
63 ---\r
64  Makefile.local  |   1 +\r
65  sprinter-sexp.c | 238 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
66  sprinter.h      |   4 +\r
67  3 files changed, 243 insertions(+)\r
68  create mode 100644 sprinter-sexp.c\r
69 \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
75         notmuch-tag.c           \\r
76         notmuch-time.c          \\r
77         sprinter-json.c         \\r
78 +       sprinter-sexp.c         \\r
79         sprinter-text.c         \\r
80         query-string.c          \\r
81         mime-node.c             \\r
82 diff --git a/sprinter-sexp.c b/sprinter-sexp.c\r
83 new file mode 100644\r
84 index 0000000..8f84eed\r
85 --- /dev/null\r
86 +++ b/sprinter-sexp.c\r
87 @@ -0,0 +1,238 @@\r
88 +/* notmuch - Not much of an email program, (just index and search)\r
89 + *\r
90 + * Copyright © 2012 Peter Feigl\r
91 + *\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
96 + *\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
101 + *\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
104 + *\r
105 + * Author: Peter Feigl <peter.feigl@gmx.at>\r
106 + */\r
107 +\r
108 +#include <stdbool.h>\r
109 +#include <stdio.h>\r
110 +#include <talloc.h>\r
111 +#include "sprinter.h"\r
112 +\r
113 +struct sprinter_sexp {\r
114 +    struct sprinter vtable;\r
115 +    FILE *stream;\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
119 +\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
123 +};\r
124 +\r
125 +struct sexp_state {\r
126 +    struct sexp_state *parent;\r
127 +\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
131 +};\r
132 +\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
137 +{\r
138 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
139 +\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
145 +           } else {\r
146 +               fputc (' ', sps->stream);\r
147 +           }\r
148 +       } else {\r
149 +           sps->state->first = FALSE;\r
150 +       }\r
151 +    }\r
152 +    return sps;\r
153 +}\r
154 +\r
155 +/* Helper function to begin an aggregate type.  Prints the open\r
156 + * character and pushes a new state frame. */\r
157 +static void\r
158 +sexp_begin_aggregate (struct sprinter *sp)\r
159 +{\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
166 +}\r
167 +\r
168 +static void\r
169 +sexp_begin_map (struct sprinter *sp)\r
170 +{\r
171 +    sexp_begin_aggregate (sp);\r
172 +}\r
173 +\r
174 +static void\r
175 +sexp_begin_list (struct sprinter *sp)\r
176 +{\r
177 +    sexp_begin_aggregate (sp);\r
178 +}\r
179 +\r
180 +static void\r
181 +sexp_end (struct sprinter *sp)\r
182 +{\r
183 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
184 +    struct sexp_state *state = sps->state;\r
185 +\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
191 +}\r
192 +\r
193 +static void\r
194 +sexp_string_len (struct sprinter *sp, const char *val, size_t len)\r
195 +{\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
202 +    };\r
203 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
204 +\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
212 +       else\r
213 +           fprintf (sps->stream, "\\%03oo", ch);\r
214 +    }\r
215 +    fputc ('"', sps->stream);\r
216 +}\r
217 +\r
218 +static void\r
219 +sexp_string (struct sprinter *sp, const char *val)\r
220 +{\r
221 +    if (val == NULL)\r
222 +       val = "";\r
223 +    sexp_string_len (sp, val, strlen (val));\r
224 +}\r
225 +\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
228 +static void\r
229 +sexp_symbol (struct sprinter *sp, const char *val)\r
230 +{\r
231 +    static const char illegal_characters[] = {\r
232 +       ' ', '\t', '\n'\r
233 +    };\r
234 +    unsigned int i = 0;\r
235 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
236 +\r
237 +    if (val == NULL)\r
238 +       INTERNAL_ERROR ("illegal symbol NULL");\r
239 +\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
243 +       }\r
244 +    }\r
245 +    fputc (':', sps->stream);\r
246 +    fputs (val, sps->stream);\r
247 +}\r
248 +\r
249 +static void\r
250 +sexp_integer (struct sprinter *sp, int val)\r
251 +{\r
252 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
253 +\r
254 +    fprintf (sps->stream, "%d", val);\r
255 +}\r
256 +\r
257 +static void\r
258 +sexp_boolean (struct sprinter *sp, notmuch_bool_t val)\r
259 +{\r
260 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
261 +\r
262 +    fputs (val ? "t" : "nil", sps->stream);\r
263 +}\r
264 +\r
265 +static void\r
266 +sexp_null (struct sprinter *sp)\r
267 +{\r
268 +    struct sprinter_sexp *sps = sexp_begin_value (sp);\r
269 +\r
270 +    fputs ("nil", sps->stream);\r
271 +}\r
272 +\r
273 +static void\r
274 +sexp_map_key (struct sprinter *sp, const char *key)\r
275 +{\r
276 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
277 +    if (sps->state && ! sps->state->first)\r
278 +       fputc (' ', sps->stream);\r
279 +\r
280 +    sps->state->first = FALSE;\r
281 +    sexp_symbol (sp, key);\r
282 +}\r
283 +\r
284 +static void\r
285 +sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))\r
286 +{\r
287 +}\r
288 +\r
289 +static void\r
290 +sexp_separator (struct sprinter *sp)\r
291 +{\r
292 +    struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;\r
293 +\r
294 +    sps->insert_separator = TRUE;\r
295 +}\r
296 +\r
297 +struct sprinter *\r
298 +sprinter_sexp_create (const void *ctx, FILE *stream)\r
299 +{\r
300 +    static const struct sprinter_sexp template = {\r
301 +       .vtable = {\r
302 +           .begin_map = sexp_begin_map,\r
303 +           .begin_list = sexp_begin_list,\r
304 +           .end = sexp_end,\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
314 +       }\r
315 +    };\r
316 +    struct sprinter_sexp *res;\r
317 +\r
318 +    res = talloc (ctx, struct sprinter_sexp);\r
319 +    if (! res)\r
320 +       return NULL;\r
321 +\r
322 +    *res = template;\r
323 +    res->stream = stream;\r
324 +    return &res->vtable;\r
325 +}\r
326 diff --git a/sprinter.h b/sprinter.h\r
327 index 912a526..59776a9 100644\r
328 --- a/sprinter.h\r
329 +++ b/sprinter.h\r
330 @@ -70,4 +70,8 @@ sprinter_text_create (const void *ctx, FILE *stream);\r
331  struct sprinter *\r
332  sprinter_json_create (const void *ctx, FILE *stream);\r
333  \r
334 +/* Create a new structure printer that emits S-Expressions. */\r
335 +struct sprinter *\r
336 +sprinter_sexp_create (const void *ctx, FILE *stream);\r
337 +\r
338  #endif // NOTMUCH_SPRINTER_H\r
339 -- \r
340 1.8.0\r
341 \r