1 /* icalparse.y -- icalendar (RFC 5545) parser
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.
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.
17 #define yydebug icaldebug
18 #define yyerror icalerror
20 #define yylval icallval
22 #define yyparse icalparse
25 #define YYERROR_VERBOSE
29 * To quiet compile warnings with Solaris yacc:
38 #include "h/icalendar.h"
41 static char *append (contentline *, const char *, const size_t);
42 static void new_content_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 *);
49 %token ICAL_NAME ICAL_COLON ICAL_VALUE ICAL_CRLF ICAL_SEMICOLON
50 %token ICAL_PARAM_NAME ICAL_EQUAL ICAL_PARAM_VALUE ICAL_COMMA
52 %start contentline_list
56 /* Instead of rigorous definition, cheat based on fact that every
57 icalbody line looks like a contentline. And we don't need to
61 | contentline_list contentline
63 /* contentline = name *(";" param ) ":" value CRLF */
66 new_content_line (&vevents.last->contentlines);
67 append (vevents.last->contentlines->last, $1, strlen ($1));
68 vevents.last->contentlines->last->name = $1;
70 append (vevents.last->contentlines->last, $3, strlen ($3));
72 append (vevents.last->contentlines->last, $5, strlen ($5));
73 vevents.last->contentlines->last->value = $5;
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;
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,
87 new_vevent (&vevents);
91 new_content_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));
97 append (vevents.last->contentlines->last, $6, strlen ($6));
98 vevents.last->contentlines->last->value = $6;
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;
109 append (vevents.last->contentlines->last, $1, strlen ($1));
111 | param_list ICAL_SEMICOLON {
112 append (vevents.last->contentlines->last, $2, strlen ($2));
115 /* param = param-name "=" param-value *("," param-value) */
118 append (vevents.last->contentlines->last, $1, strlen ($1));
119 add_param_name (vevents.last->contentlines->last, $1);
121 append (vevents.last->contentlines->last, $3, strlen ($3));
126 append (vevents.last->contentlines->last, $1, strlen ($1));
127 add_param_value (vevents.last->contentlines->last, $1);
129 | param_value_list ICAL_COMMA {
130 append (vevents.last->contentlines->last, $2, strlen ($2));
132 append (vevents.last->contentlines->last, $4, strlen ($4));
133 add_param_value (vevents.last->contentlines->last, $4);
139 * Remove the contentline node (by setting its name to NULL).
142 remove_contentline (contentline *node)
149 add_contentline (contentline *node, const char *name)
151 contentline *new_node;
154 new_node->name = mh_xstrdup (name);
155 new_node->next = node->next;
156 node->next = new_node;
162 * Remove the value from a value_list.
165 remove_value (value_list *node)
172 * Find the contentline with the specified name, and optionally,
173 * the specified value and/or parameter name.
176 find_contentline (contentline *contentlines, const char *name,
181 for (node = contentlines; node; node = node->next) {
182 /* node->name will be NULL if the line was "deleted". */
183 if (node->name && ! strcasecmp (name, node->name)) {
184 if (!val || !node->value || !strcasecmp(val, node->value))
193 append (contentline *cline, const char *src, const size_t src_len)
196 const size_t len = cline->input_line_len + src_len;
198 while (len >= cline->input_line_size) {
199 cline->input_line_size = cline->input_line_size == 0
201 : 2 * cline->input_line_size;
203 mh_xrealloc (cline->input_line, cline->input_line_size);
206 memcpy (cline->input_line + cline->input_line_len, src, src_len);
207 cline->input_line[len] = '\0';
208 cline->input_line_len = len;
211 return cline->input_line;
215 new_content_line (contentline **cline)
217 contentline *new_node;
221 /* Append the new node to the end of the list. */
222 (*cline)->last->next = new_node;
224 /* First line: save the root node in *cline. */
228 /* Only maintain the pointer to the last node in the root node. */
229 (*cline)->last = new_node;
233 new_vevent (vevent *event)
235 vevent *new_node, *node;
239 /* Append the new node to the end of the list. */
240 for (node = event; node->next; node = node->next) { continue; }
241 event->last = node->next = new_node;
245 add_param_name (contentline *cline, char *name)
247 param_list *new_node;
251 new_node->param_name = name;
254 for (p = cline->params; p->next; p = p->next) { continue; }
255 /* The loop terminated at, not after, the last node. */
258 cline->params = new_node;
263 * Add a value to the last parameter seen.
266 add_param_value (contentline *cline, char *value)
268 value_list *new_node;
273 new_node->value = value;
276 for (p = cline->params; p->next; p = p->next) { continue; }
277 /* The loop terminated at, not after, the last param_list node. */
280 for (v = p->values; v->next; v = v->next) { continue; }
281 /* The loop terminated at, not after, the last value_list node. */
284 p->values = new_node;
287 /* Never should get here because a param value is always
288 preceded by a param name. */
294 free_contentlines (contentline *root)
296 contentline *i, *next;
298 for (i = root; i; i = next) {
301 free_param_names (i->params);
304 free (i->input_line);
305 charstring_free (i->unexpected);
312 free_param_names (param_list *p)
316 for ( ; p; p = next) {
317 free (p->param_name);
318 free_param_values (p->values);
325 free_param_values (value_list *v)
329 for ( ; v; v = next) {
337 icalerror (const char *error)
340 charstring_t context = NULL;
342 /* Find last chunk of unexpected text. */
343 for (c = vevents.last->contentlines; c; c = c->next) {
345 context = c->unexpected;
349 if (! strcmp ("syntax error, unexpected $end, expecting ICAL_NAME",
351 /* Empty input: produce no output. */
354 inform ("%s after \"%s\"", error, charstring_buffer (context));
356 inform ("%s", error);
362 return 0; /* The return value isn't used anyway. */
366 * In case YYDEBUG is disabled above; mhical refers to icaldebug.
368 #if ! defined YYDEBUG || ! YYDEBUG
370 #endif /* ! YYDEBUG */