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 <inttypes.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <talloc.h>
25 #include "sprinter.h"
26 #include <ctype.h>
27 
28 struct sprinter_sexp {
29     struct sprinter vtable;
30     FILE *stream;
31     /* Top of the state stack, or NULL if the printer is not currently
32      * inside any aggregate types. */
33     struct sexp_state *state;
34 
35     /* A flag to signify that a separator should be inserted in the
36      * output as soon as possible. */
37     bool insert_separator;
38 };
39 
40 struct sexp_state {
41     struct sexp_state *parent;
42 
43     /* True if nothing has been printed in this aggregate yet.
44      * Suppresses the space before a value. */
45     bool first;
46 };
47 
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 *
sexp_begin_value(struct sprinter * sp)51 sexp_begin_value (struct sprinter *sp)
52 {
53     struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
54 
55     if (sps->state) {
56 	if (! sps->state->first) {
57 	    if (sps->insert_separator) {
58 		fputc ('\n', sps->stream);
59 		sps->insert_separator = false;
60 	    } else {
61 		fputc (' ', sps->stream);
62 	    }
63 	} else {
64 	    sps->state->first = false;
65 	}
66     }
67     return sps;
68 }
69 
70 /* Helper function to begin an aggregate type.  Prints the open
71  * character and pushes a new state frame. */
72 static void
sexp_begin_aggregate(struct sprinter * sp)73 sexp_begin_aggregate (struct sprinter *sp)
74 {
75     struct sprinter_sexp *sps = sexp_begin_value (sp);
76     struct sexp_state *state = talloc (sps, struct sexp_state);
77 
78     fputc ('(', sps->stream);
79     state->parent = sps->state;
80     state->first = true;
81     sps->state = state;
82 }
83 
84 static void
sexp_begin_map(struct sprinter * sp)85 sexp_begin_map (struct sprinter *sp)
86 {
87     sexp_begin_aggregate (sp);
88 }
89 
90 static void
sexp_begin_list(struct sprinter * sp)91 sexp_begin_list (struct sprinter *sp)
92 {
93     sexp_begin_aggregate (sp);
94 }
95 
96 static void
sexp_end(struct sprinter * sp)97 sexp_end (struct sprinter *sp)
98 {
99     struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
100     struct sexp_state *state = sps->state;
101 
102     fputc (')', sps->stream);
103     sps->state = state->parent;
104     talloc_free (state);
105     if (sps->state == NULL)
106 	fputc ('\n', sps->stream);
107 }
108 
109 static void
sexp_string_len(struct sprinter * sp,const char * val,size_t len)110 sexp_string_len (struct sprinter *sp, const char *val, size_t len)
111 {
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"
118     };
119     struct sprinter_sexp *sps = sexp_begin_value (sp);
120 
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);
126 	else if (ch >= 32)
127 	    fputc (ch, sps->stream);
128 	else
129 	    fprintf (sps->stream, "\\%03o", ch);
130     }
131     fputc ('"', sps->stream);
132 }
133 
134 static void
sexp_string(struct sprinter * sp,const char * val)135 sexp_string (struct sprinter *sp, const char *val)
136 {
137     if (val == NULL)
138 	val = "";
139     sexp_string_len (sp, val, strlen (val));
140 }
141 
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 */
144 static void
sexp_keyword(struct sprinter * sp,const char * val)145 sexp_keyword (struct sprinter *sp, const char *val)
146 {
147     unsigned int i = 0;
148     struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
149     char ch;
150 
151     if (val == NULL)
152 	INTERNAL_ERROR ("illegal symbol NULL");
153 
154     for (i = 0; i < strlen (val); i++) {
155 	ch = val[i];
156 	if (! (isalnum (ch) || (ch == '-') || (ch == '_'))) {
157 	    INTERNAL_ERROR ("illegal character in symbol %s: %c", val, ch);
158 	}
159     }
160     fputc (':', sps->stream);
161     fputs (val, sps->stream);
162 }
163 
164 static void
sexp_integer(struct sprinter * sp,int64_t val)165 sexp_integer (struct sprinter *sp, int64_t val)
166 {
167     struct sprinter_sexp *sps = sexp_begin_value (sp);
168 
169     fprintf (sps->stream, "%" PRId64, val);
170 }
171 
172 static void
sexp_boolean(struct sprinter * sp,bool val)173 sexp_boolean (struct sprinter *sp, bool val)
174 {
175     struct sprinter_sexp *sps = sexp_begin_value (sp);
176 
177     fputs (val ? "t" : "nil", sps->stream);
178 }
179 
180 static void
sexp_null(struct sprinter * sp)181 sexp_null (struct sprinter *sp)
182 {
183     struct sprinter_sexp *sps = sexp_begin_value (sp);
184 
185     fputs ("nil", sps->stream);
186 }
187 
188 static void
sexp_map_key(struct sprinter * sp,const char * key)189 sexp_map_key (struct sprinter *sp, const char *key)
190 {
191     sexp_begin_value (sp);
192 
193     sexp_keyword (sp, key);
194 }
195 
196 static void
sexp_set_prefix(unused (struct sprinter * sp),unused (const char * name))197 sexp_set_prefix (unused (struct sprinter *sp), unused (const char *name))
198 {
199 }
200 
201 static void
sexp_separator(struct sprinter * sp)202 sexp_separator (struct sprinter *sp)
203 {
204     struct sprinter_sexp *sps = (struct sprinter_sexp *) sp;
205 
206     sps->insert_separator = true;
207 }
208 
209 struct sprinter *
sprinter_sexp_create(const void * ctx,FILE * stream)210 sprinter_sexp_create (const void *ctx, FILE *stream)
211 {
212     static const struct sprinter_sexp template = {
213 	.vtable = {
214 	    .begin_map = sexp_begin_map,
215 	    .begin_list = sexp_begin_list,
216 	    .end = sexp_end,
217 	    .string = sexp_string,
218 	    .string_len = sexp_string_len,
219 	    .integer = sexp_integer,
220 	    .boolean = sexp_boolean,
221 	    .null = sexp_null,
222 	    .map_key = sexp_map_key,
223 	    .separator = sexp_separator,
224 	    .set_prefix = sexp_set_prefix,
225 	    .is_text_printer = false,
226 	}
227     };
228     struct sprinter_sexp *res;
229 
230     res = talloc (ctx, struct sprinter_sexp);
231     if (! res)
232 	return NULL;
233 
234     *res = template;
235     res->stream = stream;
236     return &res->vtable;
237 }
238