Re: [PATCH 0/4] Allow specifying alternate names for addresses in other_email
[notmuch-archives.git] / cb / 3eed81a6b0743fa367886d39a2755faad8135d
1 Return-Path: <tomi.ollila@iki.fi>\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 BBB41429E25\r
6         for <notmuch@notmuchmail.org>; Thu, 12 Jul 2012 03:10:16 -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\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id GjXzP1qn9fnk for <notmuch@notmuchmail.org>;\r
16         Thu, 12 Jul 2012 03:10:16 -0700 (PDT)\r
17 Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
18         by olra.theworths.org (Postfix) with ESMTP id ACAC8431FAE\r
19         for <notmuch@notmuchmail.org>; Thu, 12 Jul 2012 03:10:15 -0700 (PDT)\r
20 Received: by guru.guru-group.fi (Postfix, from userid 501)\r
21         id 14508100386; Thu, 12 Jul 2012 13:10:27 +0300 (EEST)\r
22 From: Tomi Ollila <tomi.ollila@iki.fi>\r
23 To: craven@gmx.net, notmuch@notmuchmail.org\r
24 Subject: Re: [PATCH v4 2/3] Add structured output formatter for JSON.\r
25 In-Reply-To: <1342079004-5300-3-git-send-email-craven@gmx.net>\r
26 References: <87d34hsdx8.fsf@awakening.csail.mit.edu>\r
27         <1342079004-5300-1-git-send-email-craven@gmx.net>\r
28         <1342079004-5300-3-git-send-email-craven@gmx.net>\r
29 User-Agent: Notmuch/0.13.2+74~g65b26b0 (http://notmuchmail.org) Emacs/23.1.1\r
30         (x86_64-redhat-linux-gnu)\r
31 X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL\r
32         $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F\r
33         !)g;OY^,BjTbr)Np:%c_o'jj,Z\r
34 Date: Thu, 12 Jul 2012 13:10:27 +0300\r
35 Message-ID: <m2vchtthm4.fsf@guru.guru-group.fi>\r
36 MIME-Version: 1.0\r
37 Content-Type: text/plain; charset=us-ascii\r
38 X-BeenThere: notmuch@notmuchmail.org\r
39 X-Mailman-Version: 2.1.13\r
40 Precedence: list\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: Thu, 12 Jul 2012 10:10:16 -0000\r
51 \r
52 On Thu, Jul 12 2012, craven@gmx.net wrote:\r
53 \r
54 > Using the new structured printer support in sprinter.h, implement\r
55 > sprinter_json_new, which returns a new JSON structured output\r
56 > formatter.\r
57 >\r
58 > The formatter prints output similar to the existing JSON, but with\r
59 > differences in whitespace (mostly newlines).\r
60 > ---\r
61 >  Makefile.local |   1 +\r
62 >  sprinter.c     | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
63 >  2 files changed, 173 insertions(+)\r
64 >  create mode 100644 sprinter.c\r
65 >\r
66 > diff --git a/Makefile.local b/Makefile.local\r
67 > index a890df2..8baf0c2 100644\r
68 > --- a/Makefile.local\r
69 > +++ b/Makefile.local\r
70 > @@ -290,6 +290,7 @@ notmuch_client_srcs =             \\r
71 >       notmuch-show.c          \\r
72 >       notmuch-tag.c           \\r
73 >       notmuch-time.c          \\r
74 > +     sprinter.c              \\r
75 >       query-string.c          \\r
76 >       mime-node.c             \\r
77 >       crypto.c                \\r
78 > diff --git a/sprinter.c b/sprinter.c\r
79 > new file mode 100644\r
80 > index 0000000..649f79a\r
81 > --- /dev/null\r
82 > +++ b/sprinter.c\r
83 > @@ -0,0 +1,172 @@\r
84 > +#include <stdbool.h>\r
85 > +#include <stdio.h>\r
86 > +#include <talloc.h>\r
87 > +#include "sprinter.h"\r
88 > +\r
89 > +#define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))\r
90 \r
91 You're including sprinter.h which includes notmuch-client.h which\r
92 defines ARRAY_SIZE (Interesting that you did not get error(/warning?)\r
93 about this)\r
94 \r
95 Rest looks good -- except the whitespace -- as I looked through Austin's\r
96 code yesterday you're just replicated the same lines :D. Easiest\r
97 to fix is probably just running \r
98 uncrustify -c devel/uncrustify.cfg --replace sprinter.c \r
99 \r
100 Tomi\r
101 \r
102 > +\r
103 > +struct sprinter *\r
104 > +sprinter_text = NULL;\r
105 > +\r
106 > +/*\r
107 > + * Every below here is the implementation of the JSON printer.\r
108 > + */\r
109 > +\r
110 > +struct sprinter_json\r
111 > +{\r
112 > +    struct sprinter vtable;\r
113 > +    FILE *stream;\r
114 > +    /* Top of the state stack, or NULL if the printer is not currently\r
115 > +     * inside any aggregate types. */\r
116 > +    struct json_state *state;\r
117 > +};\r
118 > +\r
119 > +struct json_state\r
120 > +{\r
121 > +    struct json_state *parent;\r
122 > +    /* True if nothing has been printed in this aggregate yet.\r
123 > +     * Suppresses the comma before a value. */\r
124 > +    notmuch_bool_t first;\r
125 > +    /* The character that closes the current aggregate. */\r
126 > +    char close;\r
127 > +};\r
128 > +\r
129 > +/* Helper function to set up the stream to print a value.  If this\r
130 > + * value follows another value, prints a comma. */\r
131 > +static struct sprinter_json *\r
132 > +json_begin_value(struct sprinter *sp)\r
133 > +{\r
134 > +    struct sprinter_json *spj = (struct sprinter_json*)sp;\r
135 > +    if (spj->state) {\r
136 > +     if (!spj->state->first)\r
137 > +         fputs (", ", spj->stream);\r
138 > +     else\r
139 > +         spj->state->first = false;\r
140 > +    }\r
141 > +    return spj;\r
142 > +}\r
143 > +\r
144 > +/* Helper function to begin an aggregate type.  Prints the open\r
145 > + * character and pushes a new state frame. */\r
146 > +static void\r
147 > +json_begin_aggregate(struct sprinter *sp, char open, char close)\r
148 > +{\r
149 > +    struct sprinter_json *spj = json_begin_value (sp);\r
150 > +    struct json_state *state = talloc (spj, struct json_state);\r
151 > +\r
152 > +    fputc (open, spj->stream);\r
153 > +    state->parent = spj->state;\r
154 > +    state->first = true;\r
155 > +    state->close = close;\r
156 > +    spj->state = state;\r
157 > +}\r
158 > +\r
159 > +static void\r
160 > +json_begin_map(struct sprinter *sp)\r
161 > +{\r
162 > +    json_begin_aggregate (sp, '{', '}');\r
163 > +}\r
164 > +\r
165 > +static void\r
166 > +json_begin_list(struct sprinter *sp)\r
167 > +{\r
168 > +    json_begin_aggregate (sp, '[', ']');\r
169 > +}\r
170 > +\r
171 > +static void\r
172 > +json_end(struct sprinter *sp)\r
173 > +{\r
174 > +    struct sprinter_json *spj = (struct sprinter_json*)sp;\r
175 > +    struct json_state *state = spj->state;\r
176 > +\r
177 > +    fputc (spj->state->close, spj->stream);\r
178 > +    spj->state = state->parent;\r
179 > +    talloc_free (state);\r
180 > +    if(spj->state == NULL)\r
181 > +     fputc ('\n', spj->stream);\r
182 > +}\r
183 > +\r
184 > +static void\r
185 > +json_string(struct sprinter *sp, const char *val)\r
186 > +{\r
187 > +    static const char * const escapes[] = {\r
188 > +     ['\"'] = "\\\"", ['\\'] = "\\\\", ['\b'] = "\\b",\r
189 > +     ['\f'] = "\\f",  ['\n'] = "\\n",  ['\t'] = "\\t"\r
190 > +    };\r
191 > +    struct sprinter_json *spj = json_begin_value (sp);\r
192 > +    fputc ('"', spj->stream);\r
193 > +    for (; *val; ++val) {\r
194 > +     unsigned char ch = *val;\r
195 > +     if (ch < ARRAY_SIZE(escapes) && escapes[ch])\r
196 > +         fputs (escapes[ch], spj->stream);\r
197 > +     else if (ch >= 32)\r
198 > +         fputc (ch, spj->stream);\r
199 > +     else\r
200 > +         fprintf (spj->stream, "\\u%04x", ch);\r
201 > +    }\r
202 > +    fputc ('"', spj->stream);\r
203 > +}\r
204 > +\r
205 > +static void\r
206 > +json_integer(struct sprinter *sp, int val)\r
207 > +{\r
208 > +    struct sprinter_json *spj = json_begin_value (sp);\r
209 > +    fprintf (spj->stream, "%d", val);\r
210 > +}\r
211 > +\r
212 > +static void\r
213 > +json_boolean(struct sprinter *sp, notmuch_bool_t val)\r
214 > +{\r
215 > +    struct sprinter_json *spj = json_begin_value (sp);\r
216 > +    fputs (val ? "true" : "false", spj->stream);\r
217 > +}\r
218 > +\r
219 > +static void\r
220 > +json_null(struct sprinter *sp)\r
221 > +{\r
222 > +    struct sprinter_json *spj = json_begin_value (sp);\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 > +    json_string (sp, key);\r
231 > +    fputs (": ", spj->stream);\r
232 > +    spj->state->first = true;\r
233 > +}\r
234 > +\r
235 > +static void\r
236 > +json_frame(struct sprinter *sp)\r
237 > +{\r
238 > +    struct sprinter_json *spj = (struct sprinter_json*)sp;\r
239 > +    fputc ('\n', spj->stream);\r
240 > +}\r
241 > +\r
242 > +struct sprinter *\r
243 > +sprinter_json_new(const void *ctx, FILE *stream)\r
244 > +{\r
245 > +    static const struct sprinter_json template = {\r
246 > +     .vtable = {\r
247 > +         .begin_map = json_begin_map,\r
248 > +         .begin_list = json_begin_list,\r
249 > +         .end = json_end,\r
250 > +         .string = json_string,\r
251 > +         .integer = json_integer,\r
252 > +         .boolean = json_boolean,\r
253 > +         .null = json_null,\r
254 > +         .map_key = json_map_key,\r
255 > +         .frame = json_frame,\r
256 > +     }\r
257 > +    };\r
258 > +    struct sprinter_json *res;\r
259 > +\r
260 > +    res = talloc (ctx, struct sprinter_json);\r
261 > +    if (!res)\r
262 > +     return NULL;\r
263 > +\r
264 > +    *res = template;\r
265 > +    res->stream = stream;\r
266 > +    return &res->vtable;\r
267 > +}\r
268 > -- \r
269 > 1.7.11.1\r
270 >\r
271 > _______________________________________________\r
272 > notmuch mailing list\r
273 > notmuch@notmuchmail.org\r
274 > http://notmuchmail.org/mailman/listinfo/notmuch\r