]> diplodocus.org Git - nmh/blobdiff - sbr/icalparse.y
mhbuildsbr.c: Flip logic, moving goto to then-block; no need for else.
[nmh] / sbr / icalparse.y
index f2e7b37405ec771324946bbebc1ecef0a62ebbe9..88edb0c83eb66a05f996b511be45abb89d9fb1f1 100644 (file)
@@ -1,5 +1,4 @@
-/*
- * icalparse.y -- icalendar (RFC 5545) parser
+/* 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
@@ -39,7 +38,7 @@
 #include "h/utils.h"
 
 static char *append (contentline *, const char *, const size_t);
-static void new_input_line (contentline **);
+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 *);
@@ -63,7 +62,7 @@ contentline_list
     /* contentline = name *(";" param ) ":" value CRLF */
 contentline
     : ICAL_NAME {
-          new_input_line (&vevents.last->contentlines);
+          new_content_line (&vevents.last->contentlines);
           append (vevents.last->contentlines->last, $1, strlen ($1));
           vevents.last->contentlines->last->name = $1;
       } ICAL_COLON {
@@ -88,7 +87,7 @@ contentline
           }
       }
     | ICAL_NAME {
-          new_input_line (&vevents.last->contentlines);
+          new_content_line (&vevents.last->contentlines);
           append (vevents.last->contentlines->last, $1, strlen ($1));
           vevents.last->contentlines->last->name = $1;
       } param_list ICAL_COLON {
@@ -146,9 +145,10 @@ remove_contentline (contentline *node) {
 
 contentline *
 add_contentline (contentline *node, const char *name) {
-    contentline *new_node = mh_xcalloc (1, sizeof (contentline));
+    contentline *new_node;
 
-    new_node->name  = strdup (name);
+    NEW0(new_node);
+    new_node->name  = mh_xstrdup (name);
     new_node->next = node->next;
     node->next = new_node;
 
@@ -196,7 +196,7 @@ append (contentline *cline, const char *src, const size_t src_len) {
 
         while (len >= cline->input_line_size) {
             cline->input_line_size = cline->input_line_size == 0
-                ?  (BUFSIZ>=8192 ? BUFSIZ : 8192)
+                ?  NMH_BUFSIZ
                 :  2 * cline->input_line_size;
             cline->input_line =
                 mh_xrealloc (cline->input_line, cline->input_line_size);
@@ -211,9 +211,10 @@ append (contentline *cline, const char *src, const size_t src_len) {
 }
 
 static void
-new_input_line (contentline **cline) {
-    contentline *new_node = mh_xcalloc (1, sizeof (contentline));
+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;
@@ -228,7 +229,9 @@ new_input_line (contentline **cline) {
 
 static void
 new_vevent (vevent *event) {
-    vevent *new_node = mh_xcalloc (1, sizeof (vevent)), *node;
+    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; }
@@ -237,9 +240,10 @@ new_vevent (vevent *event) {
 
 void
 add_param_name (contentline *cline, char *name) {
-    param_list *new_node = mh_xcalloc (1, sizeof (param_list));
+    param_list *new_node;
     param_list *p;
 
+    NEW0(new_node);
     new_node->param_name = name;
 
     if (cline->params) {
@@ -256,10 +260,11 @@ add_param_name (contentline *cline, char *name) {
  */
 void
 add_param_value (contentline *cline, char *value) {
-    value_list *new_node = mh_xcalloc (1, sizeof (value_list));
+    value_list *new_node;
     param_list *p;
     value_list *v;
 
+    NEW0(new_node);
     new_node->value = value;
 
     if (cline->params) {
@@ -291,6 +296,7 @@ free_contentlines (contentline *root) {
         }
         free (i->value);
         free (i->input_line);
+        charstring_free (i->unexpected);
         next = i->next;
         free (i);
     }
@@ -321,11 +327,27 @@ free_param_values (value_list *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 {
-        adios (NULL, "%s", error);
+        if (context) {
+            inform ("%s after \"%s\"", error, charstring_buffer (context));
+        } else {
+            inform ("%s", error);
+        }
+        parser_status = -1;
+        return -1;
     }
 
     return 0;  /* The return value isn't used anyway. */