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