]> diplodocus.org Git - nmh/blob - sbr/icalparse.y
Another pass at cleaning up (some of) the manpages.
[nmh] / sbr / icalparse.y
1 /*
2 * icalparse.y -- icalendar (RFC 5545) parser
3 *
4 * This code is Copyright (c) 2014, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 %{
10 /*
11 * Use these yy* #defines, instead of the -p command line
12 * option, to allow multiple parsers in a program. yyval
13 * is generated by Solaris yacc and is of type YYSTYPE.
14 * All other yy* symbols are data of a built-in type and
15 * are initialized by yyparse(), so they can be shared
16 * between different parsers.
17 */
18 #define yydebug icaldebug
19 #define yyerror icalerror
20 #define yylex icallex
21 #define yylval icallval
22 #define yyval icalval
23 #define yyparse icalparse
24 #define YYDEBUG 1
25 #define YYERROR_DEBUG
26 #define YYERROR_VERBOSE
27 #define YY_NO_LEAKS
28
29 /*
30 * To quiet compile warnings with Solaris yacc:
31 #ifdef sun
32 # define lint 1
33 # undef YYDEBUG
34 #endif
35 */
36
37 #include "h/mh.h"
38 #include "h/icalendar.h"
39 #include "h/utils.h"
40
41 static char *append (contentline *, const char *, const size_t);
42 static void new_input_line (contentline **);
43 static void new_vevent (vevent *);
44 static void free_param_names (param_list *);
45 static void free_param_values (value_list *);
46 static int icalerror (const char *);
47 %}
48
49 %token ICAL_NAME ICAL_COLON ICAL_VALUE ICAL_CRLF ICAL_SEMICOLON
50 %token ICAL_PARAM_NAME ICAL_EQUAL ICAL_PARAM_VALUE ICAL_COMMA
51
52 %start contentline_list
53
54 %%
55
56 /* Instead of rigorous definition, cheat based on fact that every
57 icalbody line looks like a contentline. And we don't need to
58 parse values. */
59 contentline_list
60 : contentline
61 | contentline_list contentline
62
63 /* contentline = name *(";" param ) ":" value CRLF */
64 contentline
65 : ICAL_NAME {
66 new_input_line (&vevents.last->contentlines);
67 append (vevents.last->contentlines->last, $1, strlen ($1));
68 vevents.last->contentlines->last->name = $1;
69 } ICAL_COLON {
70 append (vevents.last->contentlines->last, $3, strlen ($3));
71 } ICAL_VALUE {
72 append (vevents.last->contentlines->last, $5, strlen ($5));
73 vevents.last->contentlines->last->value = $5;
74 } ICAL_CRLF {
75 append (vevents.last->contentlines->last, $7, strlen ($7));
76 if (vevents.last->contentlines->cr_before_lf == CR_UNSET) {
77 vevents.last->contentlines->cr_before_lf =
78 $7[0] == '\r' ? CR_BEFORE_LF : LF_ONLY;
79 }
80 /* END:VEVENT doesn't have a param_list so we don't need
81 to check for it below. */
82 if (vevents.last->contentlines->last->name &&
83 vevents.last->contentlines->last->value &&
84 ! strcasecmp (vevents.last->contentlines->last->name, "END") &&
85 ! strcasecmp (vevents.last->contentlines->last->value,
86 "VEVENT")) {
87 new_vevent (&vevents);
88 }
89 }
90 | ICAL_NAME {
91 new_input_line (&vevents.last->contentlines);
92 append (vevents.last->contentlines->last, $1, strlen ($1));
93 vevents.last->contentlines->last->name = $1;
94 } param_list ICAL_COLON {
95 append (vevents.last->contentlines->last, $4, strlen ($4));
96 } ICAL_VALUE {
97 append (vevents.last->contentlines->last, $6, strlen ($6));
98 vevents.last->contentlines->last->value = $6;
99 } ICAL_CRLF {
100 append (vevents.last->contentlines->last, $8, strlen ($8));
101 if (vevents.last->contentlines->cr_before_lf == CR_UNSET) {
102 vevents.last->contentlines->cr_before_lf =
103 $8[0] == '\r' ? CR_BEFORE_LF : LF_ONLY;
104 }
105 }
106
107 param_list
108 : ICAL_SEMICOLON {
109 append (vevents.last->contentlines->last, $1, strlen ($1));
110 } param
111 | param_list ICAL_SEMICOLON {
112 append (vevents.last->contentlines->last, $2, strlen ($2));
113 } param
114
115 /* param = param-name "=" param-value *("," param-value) */
116 param
117 : ICAL_PARAM_NAME {
118 append (vevents.last->contentlines->last, $1, strlen ($1));
119 add_param_name (vevents.last->contentlines->last, $1);
120 } ICAL_EQUAL {
121 append (vevents.last->contentlines->last, $3, strlen ($3));
122 } param_value_list
123
124 param_value_list
125 : ICAL_PARAM_VALUE {
126 append (vevents.last->contentlines->last, $1, strlen ($1));
127 add_param_value (vevents.last->contentlines->last, $1);
128 }
129 | param_value_list ICAL_COMMA {
130 append (vevents.last->contentlines->last, $2, strlen ($2));
131 } ICAL_PARAM_VALUE {
132 append (vevents.last->contentlines->last, $4, strlen ($4));
133 add_param_value (vevents.last->contentlines->last, $4);
134 }
135
136 %%
137
138 /*
139 * Remove the contentline node (by setting its name to NULL).
140 */
141 void
142 remove_contentline (contentline *node) {
143 free (node->name);
144 node->name = NULL;
145 }
146
147 contentline *
148 add_contentline (contentline *node, const char *name) {
149 contentline *new_node = mh_xcalloc (1, sizeof (contentline));
150
151 new_node->name = strdup (name);
152 new_node->next = node->next;
153 node->next = new_node;
154
155 return new_node;
156 }
157
158 /*
159 * Remove the value from a value_list.
160 */
161 void
162 remove_value (value_list *node) {
163 free (node->value);
164 node->value = NULL;
165 }
166
167 /*
168 * Find the contentline with the specified name, and optionally,
169 * the specified value and/or parameter name.
170 */
171 contentline *
172 find_contentline (contentline *contentlines, const char *name,
173 const char *val) {
174 contentline *node;
175
176 for (node = contentlines; node; node = node->next) {
177 /* node->name will be NULL if the line was "deleted". */
178 if (node->name && ! strcasecmp (name, node->name)) {
179 if (val && node->value) {
180 if (! strcasecmp (val, node->value)) {
181 return node;
182 }
183 } else {
184 return node;
185 }
186 }
187 }
188
189 return NULL;
190 }
191
192 static char *
193 append (contentline *cline, const char *src, const size_t src_len) {
194 if (src_len > 0) {
195 const size_t len = cline->input_line_len + src_len;
196
197 while (len >= cline->input_line_size) {
198 cline->input_line_size = cline->input_line_size == 0
199 ? (BUFSIZ>=8192 ? BUFSIZ : 8192)
200 : 2 * cline->input_line_size;
201 cline->input_line =
202 mh_xrealloc (cline->input_line, cline->input_line_size);
203 }
204
205 memcpy (cline->input_line + cline->input_line_len, src, src_len);
206 cline->input_line[len] = '\0';
207 cline->input_line_len = len;
208 }
209
210 return cline->input_line;
211 }
212
213 static void
214 new_input_line (contentline **cline) {
215 contentline *new_node = mh_xcalloc (1, sizeof (contentline));
216
217 if (*cline) {
218 /* Append the new node to the end of the list. */
219 (*cline)->last->next = new_node;
220 } else {
221 /* First line: save the root node in *cline. */
222 *cline = new_node;
223 }
224
225 /* Only maintain the pointer to the last node in the root node. */
226 (*cline)->last = new_node;
227 }
228
229 static void
230 new_vevent (vevent *event) {
231 vevent *new_node = mh_xcalloc (1, sizeof (vevent)), *node;
232
233 /* Append the new node to the end of the list. */
234 for (node = event; node->next; node = node->next) { continue; }
235 event->last = node->next = new_node;
236 }
237
238 void
239 add_param_name (contentline *cline, char *name) {
240 param_list *new_node = mh_xcalloc (1, sizeof (param_list));
241 param_list *p;
242
243 new_node->param_name = name;
244
245 if (cline->params) {
246 for (p = cline->params; p->next; p = p->next) { continue; }
247 /* The loop terminated at, not after, the last node. */
248 p->next = new_node;
249 } else {
250 cline->params = new_node;
251 }
252 }
253
254 /*
255 * Add a value to the last parameter seen.
256 */
257 void
258 add_param_value (contentline *cline, char *value) {
259 value_list *new_node = mh_xcalloc (1, sizeof (value_list));
260 param_list *p;
261 value_list *v;
262
263 new_node->value = value;
264
265 if (cline->params) {
266 for (p = cline->params; p->next; p = p->next) { continue; }
267 /* The loop terminated at, not after, the last param_list node. */
268
269 if (p->values) {
270 for (v = p->values; v->next; v = v->next) { continue; }
271 /* The loop terminated at, not after, the last value_list node. */
272 v->next = new_node;
273 } else {
274 p->values = new_node;
275 }
276 } else {
277 /* Never should get here because a param value is always
278 preceded by a param name. */
279 free (new_node);
280 }
281 }
282
283 void
284 free_contentlines (contentline *root) {
285 contentline *i, *next;
286
287 for (i = root; i; i = next) {
288 free (i->name);
289 if (i->params) {
290 free_param_names (i->params);
291 }
292 free (i->value);
293 free (i->input_line);
294 next = i->next;
295 free (i);
296 }
297 }
298
299 static void
300 free_param_names (param_list *p) {
301 param_list *next;
302
303 for ( ; p; p = next) {
304 free (p->param_name);
305 free_param_values (p->values);
306 next = p->next;
307 free (p);
308 }
309 }
310
311 static void
312 free_param_values (value_list *v) {
313 value_list *next;
314
315 for ( ; v; v = next) {
316 free (v->value);
317 next = v->next;
318 free (v);
319 }
320 }
321
322 static int
323 icalerror (const char *error) {
324 if (! strcmp ("syntax error, unexpected $end, expecting ICAL_NAME",
325 error)) {
326 /* Empty input: produce no output. */
327 } else {
328 adios (NULL, "%s", error);
329 }
330
331 return 0; /* The return value isn't used anyway. */
332 }
333
334 /*
335 * In case YYDEBUG is disabled above; mhical refers to icaldebug.
336 */
337 #if ! defined YYDEBUG || ! YYDEBUG
338 int icaldebug;
339 #endif /* ! YYDEBUG */