1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2012 Peter Feigl
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.
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.
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/ .
18 * Author: Peter Feigl <peter.feigl@gmx.at>
28 struct sprinter_sexp {
29 struct sprinter vtable;
31 /* Top of the state stack, or NULL if the printer is not currently
32 * inside any aggregate types. */
33 struct sexp_state *state;
35 /* A flag to signify that a separator should be inserted in the
36 * output as soon as possible. */
37 bool insert_separator;
41 struct sexp_state *parent;
43 /* True if nothing has been printed in this aggregate yet.
44 * Suppresses the space before a value. */
48 /* Helper function to set up the stream to print a value. If this
49 * value follows another value, prints a space. */
50 static struct sprinter_sexp *
51 sexp_begin_value (struct sprinter *sp)
53 struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
56 if (! sps->state->first) {
57 if (sps->insert_separator) {
58 fputc ('\n', sps->stream);
59 sps->insert_separator = false;
61 fputc (' ', sps->stream);
64 sps->state->first = false;
70 /* Helper function to begin an aggregate type. Prints the open
71 * character and pushes a new state frame. */
73 sexp_begin_aggregate (struct sprinter *sp)
75 struct sprinter_sexp *sps = sexp_begin_value (sp);
76 struct sexp_state *state = talloc (sps, struct sexp_state);
78 fputc ('(', sps->stream);
79 state->parent = sps->state;
85 sexp_begin_map (struct sprinter *sp)
87 sexp_begin_aggregate (sp);
91 sexp_begin_list (struct sprinter *sp)
93 sexp_begin_aggregate (sp);
97 sexp_end (struct sprinter *sp)
99 struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
100 struct sexp_state *state = sps->state;
102 fputc (')', sps->stream);
103 sps->state = state->parent;
105 if (sps->state == NULL)
106 fputc ('\n', sps->stream);
110 sexp_string_len (struct sprinter *sp, const char *val, size_t len)
112 /* Some characters need escaping. " and \ work fine in all Lisps,
113 * \n is not supported in CL, but all others work fine.
114 * Characters below 32 are printed as \123o (three-digit
115 * octals), which work fine in most Schemes and Emacs. */
116 static const char *const escapes[] = {
117 ['\"'] = "\\\"", ['\\'] = "\\\\", ['\n'] = "\\n"
119 struct sprinter_sexp *sps = sexp_begin_value (sp);
121 fputc ('"', sps->stream);
122 for (; len; ++val, --len) {
123 unsigned char ch = *val;
124 if (ch < ARRAY_SIZE (escapes) && escapes[ch])
125 fputs (escapes[ch], sps->stream);
127 fputc (ch, sps->stream);
129 fprintf (sps->stream, "\\%03o", ch);
131 fputc ('"', sps->stream);
135 sexp_string (struct sprinter *sp, const char *val)
139 sexp_string_len (sp, val, strlen (val));
142 /* Prints a symbol, i.e. the name preceded by a colon. This should work
143 * in all Lisps, at least as a symbol, if not as a proper keyword */
145 sexp_keyword (struct sprinter *sp, const char *val)
148 struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
152 INTERNAL_ERROR ("illegal symbol NULL");
154 for (i = 0; i < strlen (val); i++) {
156 if (! (isalnum (ch) || (ch == '-') || (ch == '_'))) {
157 INTERNAL_ERROR ("illegal character in symbol %s: %c", val, ch);
160 fputc (':', sps->stream);
161 fputs (val, sps->stream);
165 sexp_integer (struct sprinter *sp, int64_t val)
167 struct sprinter_sexp *sps = sexp_begin_value (sp);
169 fprintf (sps->stream, "%" PRId64, val);
173 sexp_boolean (struct sprinter *sp, bool val)
175 struct sprinter_sexp *sps = sexp_begin_value (sp);
177 fputs (val ? "t" : "nil", sps->stream);
181 sexp_null (struct sprinter *sp)
183 struct sprinter_sexp *sps = sexp_begin_value (sp);
185 fputs ("nil", sps->stream);
189 sexp_map_key (struct sprinter *sp, const char *key)
191 sexp_begin_value (sp);
193 sexp_keyword (sp, key);
197 sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))
202 sexp_separator (struct sprinter *sp)
204 struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
206 sps->insert_separator = true;
210 sprinter_sexp_create (notmuch_database_t *db, FILE *stream)
212 static const struct sprinter_sexp template = {
214 .begin_map = sexp_begin_map,
215 .begin_list = sexp_begin_list,
217 .string = sexp_string,
218 .string_len = sexp_string_len,
219 .integer = sexp_integer,
220 .boolean = sexp_boolean,
222 .map_key = sexp_map_key,
223 .separator = sexp_separator,
224 .set_prefix = sexp_set_prefix,
225 .is_text_printer = false,
228 struct sprinter_sexp *res;
230 res = talloc (db, struct sprinter_sexp);
235 res->vtable.notmuch = db;
236 res->stream = stream;