]> git.notmuchmail.org Git - notmuch/blob - sprinter-sexp.c
test-lib.sh: add "atexit" functionality
[notmuch] / sprinter-sexp.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2012 Peter Feigl
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see https://www.gnu.org/licenses/ .
17  *
18  * Author: Peter Feigl <peter.feigl@gmx.at>
19  */
20
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <talloc.h>
24 #include "sprinter.h"
25 #include <ctype.h>
26
27 struct sprinter_sexp {
28     struct sprinter vtable;
29     FILE *stream;
30     /* Top of the state stack, or NULL if the printer is not currently
31      * inside any aggregate types. */
32     struct sexp_state *state;
33
34     /* A flag to signify that a separator should be inserted in the
35      * output as soon as possible. */
36     notmuch_bool_t insert_separator;
37 };
38
39 struct sexp_state {
40     struct sexp_state *parent;
41
42     /* True if nothing has been printed in this aggregate yet.
43      * Suppresses the space before a value. */
44     notmuch_bool_t first;
45 };
46
47 /* Helper function to set up the stream to print a value.  If this
48  * value follows another value, prints a space. */
49 static struct sprinter_sexp *
50 sexp_begin_value (struct sprinter *sp)
51 {
52     struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
53
54     if (sps->state) {
55         if (! sps->state->first) {
56             if (sps->insert_separator) {
57                 fputc ('\n', sps->stream);
58                 sps->insert_separator = FALSE;
59             } else {
60                 fputc (' ', sps->stream);
61             }
62         } else {
63             sps->state->first = FALSE;
64         }
65     }
66     return sps;
67 }
68
69 /* Helper function to begin an aggregate type.  Prints the open
70  * character and pushes a new state frame. */
71 static void
72 sexp_begin_aggregate (struct sprinter *sp)
73 {
74     struct sprinter_sexp *sps = sexp_begin_value (sp);
75     struct sexp_state *state = talloc (sps, struct sexp_state);
76
77     fputc ('(', sps->stream);
78     state->parent = sps->state;
79     state->first = TRUE;
80     sps->state = state;
81 }
82
83 static void
84 sexp_begin_map (struct sprinter *sp)
85 {
86     sexp_begin_aggregate (sp);
87 }
88
89 static void
90 sexp_begin_list (struct sprinter *sp)
91 {
92     sexp_begin_aggregate (sp);
93 }
94
95 static void
96 sexp_end (struct sprinter *sp)
97 {
98     struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
99     struct sexp_state *state = sps->state;
100
101     fputc (')', sps->stream);
102     sps->state = state->parent;
103     talloc_free (state);
104     if (sps->state == NULL)
105         fputc ('\n', sps->stream);
106 }
107
108 static void
109 sexp_string_len (struct sprinter *sp, const char *val, size_t len)
110 {
111     /* Some characters need escaping. " and \ work fine in all Lisps,
112      * \n is not supported in CL, but all others work fine.
113      * Characters below 32 are printed as \123o (three-digit
114      * octals), which work fine in most Schemes and Emacs. */
115     static const char *const escapes[] = {
116         ['\"'] = "\\\"", ['\\'] = "\\\\",  ['\n'] = "\\n"
117     };
118     struct sprinter_sexp *sps = sexp_begin_value (sp);
119
120     fputc ('"', sps->stream);
121     for (; len; ++val, --len) {
122         unsigned char ch = *val;
123         if (ch < ARRAY_SIZE (escapes) && escapes[ch])
124             fputs (escapes[ch], sps->stream);
125         else if (ch >= 32)
126             fputc (ch, sps->stream);
127         else
128             fprintf (sps->stream, "\\%03o", ch);
129     }
130     fputc ('"', sps->stream);
131 }
132
133 static void
134 sexp_string (struct sprinter *sp, const char *val)
135 {
136     if (val == NULL)
137         val = "";
138     sexp_string_len (sp, val, strlen (val));
139 }
140
141 /* Prints a symbol, i.e. the name preceded by a colon. This should work
142  * in all Lisps, at least as a symbol, if not as a proper keyword */
143 static void
144 sexp_keyword (struct sprinter *sp, const char *val)
145 {
146     unsigned int i = 0;
147     struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
148     char ch;
149
150     if (val == NULL)
151         INTERNAL_ERROR ("illegal symbol NULL");
152
153     for (i = 0; i < strlen (val); i++) {
154         ch = val[i];
155         if (! (isalnum (ch) || (ch == '-') || (ch == '_'))) {
156             INTERNAL_ERROR ("illegal character in symbol %s: %c", val, ch);
157         }
158     }
159     fputc (':', sps->stream);
160     fputs (val, sps->stream);
161 }
162
163 static void
164 sexp_integer (struct sprinter *sp, int val)
165 {
166     struct sprinter_sexp *sps = sexp_begin_value (sp);
167
168     fprintf (sps->stream, "%d", val);
169 }
170
171 static void
172 sexp_boolean (struct sprinter *sp, notmuch_bool_t val)
173 {
174     struct sprinter_sexp *sps = sexp_begin_value (sp);
175
176     fputs (val ? "t" : "nil", sps->stream);
177 }
178
179 static void
180 sexp_null (struct sprinter *sp)
181 {
182     struct sprinter_sexp *sps = sexp_begin_value (sp);
183
184     fputs ("nil", sps->stream);
185 }
186
187 static void
188 sexp_map_key (struct sprinter *sp, const char *key)
189 {
190     sexp_begin_value (sp);
191
192     sexp_keyword (sp, key);
193 }
194
195 static void
196 sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))
197 {
198 }
199
200 static void
201 sexp_separator (struct sprinter *sp)
202 {
203     struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
204
205     sps->insert_separator = TRUE;
206 }
207
208 struct sprinter *
209 sprinter_sexp_create (const void *ctx, FILE *stream)
210 {
211     static const struct sprinter_sexp template = {
212         .vtable = {
213             .begin_map = sexp_begin_map,
214             .begin_list = sexp_begin_list,
215             .end = sexp_end,
216             .string = sexp_string,
217             .string_len = sexp_string_len,
218             .integer = sexp_integer,
219             .boolean = sexp_boolean,
220             .null = sexp_null,
221             .map_key = sexp_map_key,
222             .separator = sexp_separator,
223             .set_prefix = sexp_set_prefix,
224             .is_text_printer = FALSE,
225         }
226     };
227     struct sprinter_sexp *res;
228
229     res = talloc (ctx, struct sprinter_sexp);
230     if (! res)
231         return NULL;
232
233     *res = template;
234     res->stream = stream;
235     return &res->vtable;
236 }