File: icalparse.y

package info (click to toggle)
nmh 1.8-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,860 kB
  • sloc: ansic: 50,445; sh: 22,697; makefile: 1,138; lex: 740; perl: 509; yacc: 265
file content (370 lines) | stat: -rw-r--r-- 10,047 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* icalparse.y -- icalendar (RFC 5545) parser
 *
 * This code is Copyright (c) 2014, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

%{
    /*
     * Use these yy* #defines, instead of the -p command line
     * option, to allow multiple parsers in a program.  yyval
     * is generated by Solaris yacc and is of type YYSTYPE.
     * All other yy* symbols are data of a built-in type and
     * are initialized by yyparse(), so they can be shared
     * between different parsers.
     */
#define yydebug icaldebug
#define yyerror icalerror
#define yylex   icallex
#define yylval  icallval
#define yyval   icalval
#define yyparse icalparse
#define YYDEBUG 1
#define YYERROR_DEBUG
#define YYERROR_VERBOSE
#define YY_NO_LEAKS

    /*
     * To quiet compile warnings with Solaris yacc:
#ifdef sun
# define lint 1
# undef YYDEBUG
#endif
    */

#include "h/mh.h"
#include "sbr/charstring.h"
#include "sbr/error.h"
#include "h/icalendar.h"
#include "sbr/utils.h"

static char *append (contentline *, const char *, const size_t);
static void new_content_line (contentline **);
static void new_vevent (vevent *);
static void free_param_names (param_list *);
static void free_param_values (value_list *);
static int icalerror (const char *);
%}

%token ICAL_NAME ICAL_COLON ICAL_VALUE ICAL_CRLF ICAL_SEMICOLON
%token ICAL_PARAM_NAME ICAL_EQUAL ICAL_PARAM_VALUE ICAL_COMMA

%start contentline_list

%%

    /* Instead of rigorous definition, cheat based on fact that every
       icalbody line looks like a contentline.  And we don't need to
       parse values. */
contentline_list
    : contentline
    | contentline_list contentline

    /* contentline = name *(";" param ) ":" value CRLF */
contentline
    : ICAL_NAME {
          new_content_line (&vevents.last->contentlines);
          append (vevents.last->contentlines->last, $1, strlen ($1));
          vevents.last->contentlines->last->name = $1;
      } ICAL_COLON {
          append (vevents.last->contentlines->last, $3, strlen ($3));
      } ICAL_VALUE {
          append (vevents.last->contentlines->last, $5, strlen ($5));
          vevents.last->contentlines->last->value = $5;
      } ICAL_CRLF {
          append (vevents.last->contentlines->last, $7, strlen ($7));
          if (vevents.last->contentlines->cr_before_lf == CR_UNSET) {
              vevents.last->contentlines->cr_before_lf =
                  $7[0] == '\r'  ?  CR_BEFORE_LF  :  LF_ONLY;
          }
          /* END:VEVENT doesn't have a param_list so we don't need
             to check for it below. */
          if (vevents.last->contentlines->last->name  &&
              vevents.last->contentlines->last->value  &&
              ! strcasecmp (vevents.last->contentlines->last->name, "END")  &&
              ! strcasecmp (vevents.last->contentlines->last->value,
                            "VEVENT")) {
              new_vevent (&vevents);
          }
      }
    | ICAL_NAME {
          new_content_line (&vevents.last->contentlines);
          append (vevents.last->contentlines->last, $1, strlen ($1));
          vevents.last->contentlines->last->name = $1;
      } param_list ICAL_COLON {
          append (vevents.last->contentlines->last, $4, strlen ($4));
      } ICAL_VALUE {
          append (vevents.last->contentlines->last, $6, strlen ($6));
          vevents.last->contentlines->last->value = $6;
      } ICAL_CRLF {
          append (vevents.last->contentlines->last, $8, strlen ($8));
          if (vevents.last->contentlines->cr_before_lf == CR_UNSET) {
              vevents.last->contentlines->cr_before_lf =
                  $8[0] == '\r'  ?  CR_BEFORE_LF  :  LF_ONLY;
          }
      }

param_list
    : ICAL_SEMICOLON {
          append (vevents.last->contentlines->last, $1, strlen ($1));
      } param
    | param_list ICAL_SEMICOLON {
          append (vevents.last->contentlines->last, $2, strlen ($2));
      } param

    /* param = param-name "=" param-value *("," param-value) */
param
    : ICAL_PARAM_NAME {
          append (vevents.last->contentlines->last, $1, strlen ($1));
          add_param_name (vevents.last->contentlines->last, $1);
      } ICAL_EQUAL {
          append (vevents.last->contentlines->last, $3, strlen ($3));
      } param_value_list

param_value_list
    : ICAL_PARAM_VALUE {
          append (vevents.last->contentlines->last, $1, strlen ($1));
          add_param_value (vevents.last->contentlines->last, $1);
      }
    | param_value_list ICAL_COMMA {
          append (vevents.last->contentlines->last, $2, strlen ($2));
      } ICAL_PARAM_VALUE {
          append (vevents.last->contentlines->last, $4, strlen ($4));
          add_param_value (vevents.last->contentlines->last, $4);
      }

%%

/*
 * Remove the contentline node (by setting its name to NULL).
 */
void
remove_contentline (contentline *node)
{
    free (node->name);
    node->name = NULL;
}

contentline *
add_contentline (contentline *node, const char *name)
{
    contentline *new_node;

    NEW0(new_node);
    new_node->name  = mh_xstrdup (name);
    new_node->next = node->next;
    node->next = new_node;

    return new_node;
}

/*
 * Remove the value from a value_list.
 */
void
remove_value (value_list *node)
{
    free (node->value);
    node->value = NULL;
}

/*
 * Find the contentline with the specified name, and optionally,
 * the specified value and/or parameter name.
 */
contentline *
find_contentline (contentline *contentlines, const char *name,
                  const char *val)
{
    contentline *node;

    for (node = contentlines; node; node = node->next) {
        /* node->name will be NULL if the line was "deleted". */
        if (node->name  &&  ! strcasecmp (name, node->name)) {
            if (!val || !node->value || !strcasecmp(val, node->value))
                return node;
        }
    }

    return NULL;
}

static char *
append (contentline *cline, const char *src, const size_t src_len)
{
    if (src_len > 0) {
        const size_t len = cline->input_line_len + src_len;

        while (len >= cline->input_line_size) {
            cline->input_line_size = cline->input_line_size == 0
                ?  NMH_BUFSIZ
                :  2 * cline->input_line_size;
            cline->input_line =
                mh_xrealloc (cline->input_line, cline->input_line_size);
        }

        memcpy (cline->input_line + cline->input_line_len, src, src_len);
        cline->input_line[len] = '\0';
        cline->input_line_len = len;
    }

    return cline->input_line;
}

static void
new_content_line (contentline **cline)
{
    contentline *new_node;

    NEW0(new_node);
    if (*cline) {
        /* Append the new node to the end of the list. */
        (*cline)->last->next = new_node;
    } else {
        /* First line:  save the root node in *cline. */
        *cline = new_node;
    }

    /* Only maintain the pointer to the last node in the root node. */
    (*cline)->last = new_node;
}

static void
new_vevent (vevent *event)
{
    vevent *new_node, *node;

    NEW0(new_node);

    /* Append the new node to the end of the list. */
    for (node = event; node->next; node = node->next) { continue; }
    event->last =  node->next = new_node;
}

void
add_param_name (contentline *cline, char *name)
{
    param_list *new_node;
    param_list *p;

    NEW0(new_node);
    new_node->param_name = name;

    if (cline->params) {
        for (p = cline->params; p->next; p = p->next) { continue; }
        /* The loop terminated at, not after, the last node. */
        p->next = new_node;
    } else {
        cline->params = new_node;
    }
}

/*
 * Add a value to the last parameter seen.
 */
void
add_param_value (contentline *cline, char *value)
{
    value_list *new_node;
    param_list *p;
    value_list *v;

    NEW0(new_node);
    new_node->value = value;

    if (cline->params) {
        for (p = cline->params; p->next; p = p->next) { continue; }
        /* The loop terminated at, not after, the last param_list node. */

        if (p->values) {
            for (v = p->values; v->next; v = v->next) { continue; }
            /* The loop terminated at, not after, the last value_list node. */
            v->next = new_node;
        } else {
            p->values = new_node;
        }
    } else {
        /* Never should get here because a param value is always
           preceded by a param name. */
        free (new_node);
    }
}

void
free_contentlines (contentline *root)
{
    contentline *i, *next;

    for (i = root; i; i = next) {
        free (i->name);
        if (i->params) {
            free_param_names (i->params);
        }
        free (i->value);
        free (i->input_line);
        charstring_free (i->unexpected);
        next = i->next;
        free (i);
    }
}

static void
free_param_names (param_list *p)
{
    param_list *next;

    for ( ; p; p = next) {
        free (p->param_name);
        free_param_values (p->values);
        next = p->next;
        free (p);
    }
}

static void
free_param_values (value_list *v)
{
    value_list *next;

    for ( ; v; v = next) {
        free (v->value);
        next = v->next;
        free (v);
    }
}

static int
icalerror (const char *error)
{
    contentline *c;
    charstring_t context = NULL;

    /* Find last chunk of unexpected text. */
    for (c = vevents.last->contentlines; c; c = c->next) {
        if (c->unexpected) {
            context = c->unexpected;
        }
    }

    if (! strcmp ("syntax error, unexpected $end, expecting ICAL_NAME",
                  error)) {
        /* Empty input: produce no output. */
    } else {
        if (context) {
            inform ("%s after \"%s\"", error, charstring_buffer (context));
        } else {
            inform ("%s", error);
        }
        return 1;
    }

    return 0;  /* The return value isn't used anyway. */
}

/*
 * In case YYDEBUG is disabled above; mhical refers to icaldebug.
 */
#if ! defined YYDEBUG  ||  ! YYDEBUG
int icaldebug;
#endif /* ! YYDEBUG */