Back to index

lightning-sunbird  0.9+nobinonly
Typedefs | Enumerations | Functions
icalparser.h File Reference

Line-oriented parsing. More...

#include "icalenums.h"
#include "icaltypes.h"
#include "icalcomponent.h"
#include <stdio.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct icalparser_impl
typedef enum icalparser_state icalparser_state

Enumerations

enum  icalparser_state {
  ICALPARSER_ERROR, ICALPARSER_SUCCESS, ICALPARSER_BEGIN_COMP, ICALPARSER_END_COMP,
  ICALPARSER_IN_PROGRESS, ICALPARSER_ERROR, ICALPARSER_SUCCESS, ICALPARSER_BEGIN_COMP,
  ICALPARSER_END_COMP, ICALPARSER_IN_PROGRESS
}

Functions

icalparser * icalparser_new (void)
icalcomponent * icalparser_add_line (icalparser *parser, char *str)
icalcomponent * icalparser_clean (icalparser *parser)
icalparser_state icalparser_get_state (icalparser *parser)
void icalparser_free (icalparser *parser)
icalcomponent * icalparser_parse (icalparser *parser, char *(*line_gen_func)(char *s, size_t size, void *d))
 Message oriented parsing.
void icalparser_set_gen_data (icalparser *parser, void *data)
 Set the data that icalparser_parse will give to the line_gen_func as the parameter 'd'.
icalcomponent * icalparser_parse_string (const char *str)
icalvalue * icalparser_parse_value (icalvalue_kind kind, const char *str, icalcomponent **errors)
 Use the flex/bison parser to turn a string into a value type.
char * icalparser_get_line (icalparser *parser, char *(*line_gen_func)(char *s, size_t size, void *d))
 Given a line generator function, return a single iCal content line.
char * icalparser_string_line_generator (char *out, size_t buf_size, void *d)

Detailed Description

Line-oriented parsing.

Create a new parser via icalparse_new_parser, then add lines one at a time with icalparse_add_line(). icalparser_add_line() will return non-zero when it has finished with a component.

Definition in file icalparser.h.


Typedef Documentation

typedef struct icalparser_impl

Definition at line 36 of file icalparser.h.


Enumeration Type Documentation

Enumerator:
ICALPARSER_ERROR 
ICALPARSER_SUCCESS 
ICALPARSER_BEGIN_COMP 
ICALPARSER_END_COMP 
ICALPARSER_IN_PROGRESS 
ICALPARSER_ERROR 
ICALPARSER_SUCCESS 
ICALPARSER_BEGIN_COMP 
ICALPARSER_END_COMP 
ICALPARSER_IN_PROGRESS 

Definition at line 48 of file icalparser.h.


Function Documentation

icalcomponent* icalparser_add_line ( icalparser *  parser,
char *  str 
)

Definition at line 607 of file icalparser.c.

{ 
    char *str;
    char *end;
    int vcount = 0;
    icalproperty *prop;
    icalproperty_kind prop_kind;
    icalvalue *value;
    icalvalue_kind value_kind = ICAL_NO_VALUE;


    icalerror_check_arg_rz((parser != 0),"parser");


    if (line == 0)
    {
       parser->state = ICALPARSER_ERROR;
       return 0;
    }

    if(line_is_blank(line) == 1){
       return 0;
    }

    /* Begin by getting the property name at the start of the line. The
       property name may end up being "BEGIN" or "END" in which case it
       is not really a property, but the marker for the start or end of
       a component */

    end = 0;
    str = icalparser_get_prop_name(line, &end);

    if (str == 0 || strlen(str) == 0 ){
       /* Could not get a property name */
       icalcomponent *tail = pvl_data(pvl_tail(parser->components));

       if (tail){
           insert_error(tail,line,
                      "Got a data line, but could not find a property name or component begin tag",
                      ICAL_XLICERRORTYPE_COMPONENTPARSEERROR);
       }
       tail = 0;
       parser->state = ICALPARSER_ERROR;
       return 0; 
    }

    /**********************************************************************
     * Handle begin and end of components
     **********************************************************************/                                                         
    /* If the property name is BEGIN or END, we are actually
       starting or ending a new component */


    if(strcmp(str,"BEGIN") == 0){
       icalcomponent *c;
        icalcomponent_kind comp_kind;

       icalmemory_free_buffer(str);
       parser->level++;
       str = icalparser_get_next_value(end,&end, value_kind);
           

        comp_kind = icalenum_string_to_component_kind(str);


        if (comp_kind == ICAL_NO_COMPONENT){


           c = icalcomponent_new(ICAL_XLICINVALID_COMPONENT);
           insert_error(c,str,"Parse error in component name",
                      ICAL_XLICERRORTYPE_COMPONENTPARSEERROR);
        }


       if (comp_kind != ICAL_X_COMPONENT) {
           c  =  icalcomponent_new(comp_kind);
       } else {
           c  =  icalcomponent_new_x(str);
       }

       if (c == 0){
           c = icalcomponent_new(ICAL_XLICINVALID_COMPONENT);
           insert_error(c,str,"Parse error in component name",
                      ICAL_XLICERRORTYPE_COMPONENTPARSEERROR);
       }
           
       pvl_push(parser->components,c);

       parser->state = ICALPARSER_BEGIN_COMP;
       if (str)
           icalmemory_free_buffer(str);
       return 0;

    } else if (strcmp(str,"END") == 0 ) {
       icalcomponent* tail;

       icalmemory_free_buffer(str);
       parser->level--;
       str = icalparser_get_next_value(end,&end, value_kind);
       if (str)
           icalmemory_free_buffer(str);

       /* Pop last component off of list and add it to the second-to-last*/
       parser->root_component = pvl_pop(parser->components);

       tail = pvl_data(pvl_tail(parser->components));

       if(tail != 0){
           icalcomponent_add_component(tail,parser->root_component);
       } 

       tail = 0;

       /* Return the component if we are back to the 0th level */
       if (parser->level == 0){
           icalcomponent *rtrn; 

           if(pvl_count(parser->components) != 0){
           /* There are still components on the stack -- this means
               that one of them did not have a proper "END" */
              pvl_push(parser->components,parser->root_component);
              icalparser_clean(parser); /* may reset parser->root_component*/
           }

           assert(pvl_count(parser->components) == 0);

           parser->state = ICALPARSER_SUCCESS;
           rtrn = parser->root_component;
           parser->root_component = 0;
           return rtrn;

       } else {
           parser->state = ICALPARSER_END_COMP;
           return 0;
       }
    }


    /* There is no point in continuing if we have not seen a
       component yet */

    if(pvl_data(pvl_tail(parser->components)) == 0){
       parser->state = ICALPARSER_ERROR;
       icalmemory_free_buffer(str);
       return 0;
    }


    /**********************************************************************
     * Handle property names
     **********************************************************************/
                                                               
    /* At this point, the property name really is a property name,
       (Not a component name) so make a new property and add it to
       the component */

    
    prop_kind = icalproperty_string_to_kind(str);

    prop = icalproperty_new(prop_kind);

    if (prop != 0){
       icalcomponent *tail = pvl_data(pvl_tail(parser->components));

        if(prop_kind==ICAL_X_PROPERTY){
            icalproperty_set_x_name(prop,str);
        }
       icalmemory_free_buffer(str);

       icalcomponent_add_property(tail, prop);

       /* Set the value kind for the default for this type of
          property. This may be re-set by a VALUE parameter */
       value_kind = icalproperty_kind_to_value_kind(icalproperty_isa(prop));

    } else {
       icalcomponent* tail = pvl_data(pvl_tail(parser->components));

       insert_error(tail,str,"Parse error in property name",
                   ICAL_XLICERRORTYPE_PROPERTYPARSEERROR);
           
       tail = 0;
       parser->state = ICALPARSER_ERROR;
       icalmemory_free_buffer(str);
       return 0;
    }

    /**********************************************************************
     * Handle parameter values
     **********************************************************************/                                                         

    /* Now, add any parameters to the last property */

    while(1) {

       if (*(end-1) == ':'){
           /* if the last seperator was a ":" and the value is a
              URL, icalparser_get_next_parameter will find the
              ':' in the URL, so better break now. */
           break;
       }

       str = icalparser_get_next_parameter(end,&end);

       if (str != 0){
           char* name;
           char* pvalue;
        
           icalparameter *param = 0;
           icalparameter_kind kind;
           icalcomponent *tail = pvl_data(pvl_tail(parser->components));

           name = icalparser_get_param_name_and_value(str,&pvalue);

           if (name == 0){
              /* 'tail' defined above */
              insert_error(tail, str, "Cant parse parameter name",
                          ICAL_XLICERRORTYPE_PARAMETERNAMEPARSEERROR);
              tail = 0;
              icalmemory_free_buffer(str);
              if (pvalue)
                  icalmemory_free_buffer(pvalue);
              break;
           }

           kind = icalparameter_string_to_kind(name);

           if(kind == ICAL_X_PARAMETER){
              param = icalparameter_new(ICAL_X_PARAMETER);
              
              if(param != 0){
                  icalparameter_set_xname(param,name);
                  icalparameter_set_xvalue(param,pvalue);
              }


           } else if (kind != ICAL_NO_PARAMETER){
              param = icalparameter_new_from_value_string(kind,pvalue);
           } else {
              /* Error. Failed to parse the parameter*/
              /* 'tail' defined above */

                /* Change for mozilla */
                /* have the option of being flexible towards unsupported parameters */
                #ifndef ICAL_ERRORS_ARE_FATAL
                continue;
                #endif

              insert_error(tail, str, "Cant parse parameter name",
                          ICAL_XLICERRORTYPE_PARAMETERNAMEPARSEERROR);
              tail = 0;
              parser->state = ICALPARSER_ERROR;
              icalmemory_free_buffer(str);
              icalmemory_free_buffer(name);
              if (pvalue)
                  icalmemory_free_buffer(pvalue);
              return 0;
           }
           icalmemory_free_buffer(name);
           if (pvalue)
              icalmemory_free_buffer(pvalue);


           if (param == 0){
              /* 'tail' defined above */
              insert_error(tail,str,"Cant parse parameter value",
                          ICAL_XLICERRORTYPE_PARAMETERVALUEPARSEERROR);
                  
              tail = 0;
              parser->state = ICALPARSER_ERROR;
              icalmemory_free_buffer(str);
              continue;
           }

           /* If it is a VALUE parameter, set the kind of value*/
           if (icalparameter_isa(param)==ICAL_VALUE_PARAMETER){

              value_kind = (icalvalue_kind)
                    icalparameter_value_to_value_kind(
                                icalparameter_get_value(param)
                                );

              if (value_kind == ICAL_NO_VALUE){

                  /* Ooops, could not parse the value of the
                     parameter ( it was not one of the defined
                     values ), so reset the value_kind */
                     
                  insert_error(
                     tail, str, 
                     "Got a VALUE parameter with an unknown type",
                     ICAL_XLICERRORTYPE_PARAMETERVALUEPARSEERROR);
                  icalparameter_free(param);
                     
                  value_kind = 
                     icalproperty_kind_to_value_kind(
                         icalproperty_isa(prop));
                     
                  icalparameter_free(param);
                  tail = 0;
                  parser->state = ICALPARSER_ERROR;
                  icalmemory_free_buffer(str);
                  return 0;
              } 
           }

           /* Everything is OK, so add the parameter */
           icalproperty_add_parameter(prop,param);
           tail = 0;
           icalmemory_free_buffer(str);

       } else { /* if ( str != 0)  */
           /* If we did not get a param string, go on to looking
              for a value */
           break;
       } /* if ( str != 0)  */

    } /* while(1) */     
       
    /**********************************************************************
     * Handle values
     **********************************************************************/                                                         

    /* Look for values. If there are ',' characters in the values,
       then there are multiple values, so clone the current
       parameter and add one part of the value to each clone */

    vcount=0;
    while(1) {
        /* Only some properies can have multiple values. This list was taken
           from rfc2445. Also added the x-properties, because the spec actually
           says that commas should be escaped. For x-properties, other apps may
           depend on that behaviour
        */
        switch (prop_kind) {
            case ICAL_X_PROPERTY:
            case ICAL_CATEGORIES_PROPERTY:
            case ICAL_RESOURCES_PROPERTY:
            /* Referring to RFC 2445, section 4.8.5.3 and section 4.8.5.1:
               RDATE and EXDATE can specify a list of dates/date-times/periods.
            */
            case ICAL_RDATE_PROPERTY:
            case ICAL_EXDATE_PROPERTY:
            /* Referring to RFC 2445, section 4.8.2.6 Free/Busy Time:
               The "FREEBUSY" property can specify more than one value, separated by
               the COMMA character (US-ASCII decimal 44).
            */
            case ICAL_FREEBUSY_PROPERTY:
                 str = icalparser_get_next_value(end,&end, value_kind);
                 break;
            default:
                 str = icalparser_get_value(end, &end, value_kind);
                 break;
        }

       if (str != 0){
              
           if (vcount > 0){
              /* Actually, only clone after the second value */
              icalproperty* clone = icalproperty_new_clone(prop);
              icalcomponent* tail = pvl_data(pvl_tail(parser->components));
                  
              icalcomponent_add_property(tail, clone);
              prop = clone;            
              tail = 0;
           }
              
           value = icalvalue_new_from_string(value_kind, str);
              
           /* Don't add properties without value */
           if (value == 0){
              char temp[200]; /* HACK */

              icalproperty_kind prop_kind = icalproperty_isa(prop);
              icalcomponent* tail = pvl_data(pvl_tail(parser->components));

              snprintf(temp,sizeof(temp),"Cant parse as %s value in %s property. Removing entire property",
                     icalvalue_kind_to_string(value_kind),
                     icalproperty_kind_to_string(prop_kind));

              insert_error(tail, str, temp,
                          ICAL_XLICERRORTYPE_VALUEPARSEERROR);

              /* Remove the troublesome property */
              icalcomponent_remove_property(tail,prop);
              icalproperty_free(prop);
              prop = 0;
              tail = 0;
              parser->state = ICALPARSER_ERROR;
              icalmemory_free_buffer(str);
              return 0;
                  
           } else {
              vcount++;
              icalproperty_set_value(prop, value);
              icalmemory_free_buffer(str);
           }


       } else {
           if (vcount == 0){
              char temp[200]; /* HACK */
              
              icalproperty_kind prop_kind = icalproperty_isa(prop);
              icalcomponent *tail = pvl_data(pvl_tail(parser->components));
              
              snprintf(temp,sizeof(temp),"No value for %s property. Removing entire property",
                     icalproperty_kind_to_string(prop_kind));

              insert_error(tail, str, temp,
                          ICAL_XLICERRORTYPE_VALUEPARSEERROR);

              /* Remove the troublesome property */
              icalcomponent_remove_property(tail,prop);
              icalproperty_free(prop);
              prop = 0;
              tail = 0;
              parser->state = ICALPARSER_ERROR;
              return 0;
           } else {

              break;
           }
       }
    }
       
    /****************************************************************
     * End of component parsing. 
     *****************************************************************/

    if (pvl_data(pvl_tail(parser->components)) == 0 &&
       parser->level == 0){
       /* HACK. Does this clause ever get executed? */
       parser->state = ICALPARSER_SUCCESS;
       assert(0);
       return parser->root_component;
    } else {
       parser->state = ICALPARSER_IN_PROGRESS;
       return 0;
    }

}
icalcomponent* icalparser_clean ( icalparser *  parser)

Definition at line 1057 of file icalparser.c.

{
    icalcomponent *tail; 

    icalerror_check_arg_rz((parser != 0 ),"parser");

    /* We won't get a clean exit if some components did not have an
       "END" tag. Clear off any component that may be left in the list */
    
    while((tail=pvl_data(pvl_tail(parser->components))) != 0){

       insert_error(tail," ",
                   "Missing END tag for this component. Closing component at end of input.",
                   ICAL_XLICERRORTYPE_COMPONENTPARSEERROR);
       

       parser->root_component = pvl_pop(parser->components);
       tail=pvl_data(pvl_tail(parser->components));

       if(tail != 0){
           if(icalcomponent_get_parent(parser->root_component)!=0){
              icalerror_warn("icalparser_clean is trying to attach a component for the second time");
           } else {
              icalcomponent_add_component(tail,parser->root_component);
           }
       }
           
    }

    return parser->root_component;

}

Here is the call graph for this function:

void icalparser_free ( icalparser *  parser)

Definition at line 113 of file icalparser.c.

{
    icalcomponent *c;

    if (parser->root_component != 0){
       icalcomponent_free(parser->root_component);
    }

    while( (c=pvl_pop(parser->components)) != 0){
       icalcomponent_free(c);
    }
    
    pvl_free(parser->components);
    
    free(parser);
}

Here is the call graph for this function:

char* icalparser_get_line ( icalparser *  parser,
char *(*)(char *s, size_t size, void *d line_gen_func 
)

Given a line generator function, return a single iCal content line.

Definition at line 390 of file icalparser.c.

{
    char *line;
    char *line_p;
    size_t buf_size = parser->tmp_buf_size;
    
    line_p = line = icalmemory_new_buffer(buf_size);
    line[0] = '\0';
   
    /* Read lines by calling line_gen_func and putting the data into
       parser->temp. If the line is a continuation line ( begins with a
       space after a newline ) then append the data onto line and read
       again. Otherwise, exit the loop. */

    while(1) {

        /* The first part of the loop deals with the temp buffer,
           which was read on he last pass through the loop. The
           routine is split like this because it has to read lone line
           ahead to determine if a line is a continuation line. */


       /* The tmp buffer is not clear, so transfer the data in it to the
          output. This may be left over from a previous call */
       if (parser->temp[0] != '\0' ) {

           /* If the last position in the temp buffer is occupied,
               mark the buffer as full. The means we will do another
               read later, because the line is not finished */
           if (parser->temp[parser->tmp_buf_size-1] == 0 &&
              parser->temp[parser->tmp_buf_size-2] != '\n'&& 
                parser->temp[parser->tmp_buf_size-2] != 0 ){
              parser->buffer_full = 1;
           } else {
              parser->buffer_full = 0;
           }

           /* Copy the temp to the output and clear the temp buffer. */
            if(parser->continuation_line==1){
                /* back up the pointer to erase the continuation characters */
                parser->continuation_line = 0;
                line_p--;
                
                if ( *(line_p-1) == '\r'){
                    line_p--;
                }
                
                /* copy one space up to eliminate the leading space*/
                icalmemory_append_string(&line,&line_p,&buf_size,
                                         parser->temp+1);

            } else {
                icalmemory_append_string(&line,&line_p,&buf_size,parser->temp);
            }

            parser->temp[0] = '\0' ;
       }
       
       parser->temp[parser->tmp_buf_size-1] = 1; /* Mark end of buffer */

        /****** Here is where the routine gets string data ******************/
       if ((*line_gen_func)(parser->temp,parser->tmp_buf_size,parser->line_gen_data)
           ==0){/* Get more data */

           /* If the first position is clear, it means we didn't get
               any more data from the last call to line_ge_func*/
           if (parser->temp[0] == '\0'){

              if(line[0] != '\0'){
                  /* There is data in the output, so fall trhough and process it*/
                  break;
              } else {
                  /* No data in output; return and signal that there
                       is no more input*/
                  free(line);
                  return 0;
              }
           }
       }
       
 
       /* If the output line ends in a '\n' and the temp buffer
          begins with a ' ', then the buffer holds a continuation
          line, so keep reading.  */

       if ( line_p > line+1 && *(line_p-1) == '\n'
         && (parser->temp[0] == ' ' || parser->temp[0] == '\t') ) {

            parser->continuation_line = 1;

       } else if ( parser->buffer_full == 1 ) {
           
           /* The buffer was filled on the last read, so read again */

       } else {

           /* Looks like the end of this content line, so break */
           break;
       }

       
    }

    /* Erase the final newline and/or carriage return*/
    if ( line_p > line+1 && *(line_p-1) == '\n') {      
       *(line_p-1) = '\0';
       if ( *(line_p-2) == '\r'){
           *(line_p-2) = '\0';
       }

    } else {
       *(line_p) = '\0';
    }

    return line;

}

Here is the call graph for this function:

icalparser_state icalparser_get_state ( icalparser *  parser)

Definition at line 1051 of file icalparser.c.

{
    return parser->state;

}
icalparser* icalparser_new ( void  )

Definition at line 88 of file icalparser.c.

{
    struct icalparser_impl* impl = 0;
    if ( ( impl = (struct icalparser_impl*)
          malloc(sizeof(struct icalparser_impl))) == 0) {
       icalerror_set_errno(ICAL_NEWFAILED_ERROR);
       return 0;
    }
    
    impl->buffer_full = 0;
    impl->continuation_line = 0;
    impl->tmp_buf_size = sizeof(impl->temp);
    memset(impl->temp, 0, sizeof(impl->temp));
    impl->root_component = 0;
    impl->version = 0;
    impl->level = 0;
    impl->lineno = 0;
    impl->state = ICALPARSER_SUCCESS;
    impl->components  = pvl_newlist();  
    impl->line_gen_data = 0;

    return (icalparser*)impl;
}

Here is the call graph for this function:

icalcomponent* icalparser_parse ( icalparser *  parser,
char *(*)(char *s, size_t size, void *d line_gen_func 
)

Message oriented parsing.

icalparser_parse takes a string that holds the text ( in RFC 2445 format ) and returns a pointer to an icalcomponent. The caller owns the memory. line_gen_func is a pointer to a function that returns one content line per invocation

Definition at line 543 of file icalparser.c.

{

    char* line; 
    icalcomponent *c=0; 
    icalcomponent *root=0;
    icalerrorstate es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR);
       int cont;

    icalerror_check_arg_rz((parser !=0),"parser");

    icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_NONFATAL);

    do{
           line = icalparser_get_line(parser, line_gen_func);

       if ((c = icalparser_add_line(parser,line)) != 0){

           if(icalcomponent_get_parent(c) !=0){
              /* This is bad news... assert? */
           }      
           
           assert(parser->root_component == 0);
           assert(pvl_count(parser->components) ==0);

           if (root == 0){
              /* Just one component */
              root = c;
           } else if(icalcomponent_isa(root) != ICAL_XROOT_COMPONENT) {
              /*Got a second component, so move the two components under
                an XROOT container */
              icalcomponent *tempc = icalcomponent_new(ICAL_XROOT_COMPONENT);
              icalcomponent_add_component(tempc, root);
              icalcomponent_add_component(tempc, c);
              root = tempc;
           } else if(icalcomponent_isa(root) == ICAL_XROOT_COMPONENT) {
              /* Already have an XROOT container, so add the component
                 to it*/
              icalcomponent_add_component(root, c);
              
           } else {
              /* Badness */
              assert(0);
           }

           c = 0;

        }
       cont = 0;
       if(line != 0){
           icalmemory_free_buffer(line);
              cont = 1;
       }
    } while ( cont );

    icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es);

    return root;

}

Here is the call graph for this function:

icalcomponent* icalparser_parse_string ( const char *  str)
icalvalue* icalparser_parse_value ( icalvalue_kind  kind,
const char *  str,
icalcomponent **  errors 
)

Use the flex/bison parser to turn a string into a value type.

void icalparser_set_gen_data ( icalparser *  parser,
void data 
)

Set the data that icalparser_parse will give to the line_gen_func as the parameter 'd'.

Definition at line 130 of file icalparser.c.

{
              parser->line_gen_data  = data;
}
char* icalparser_string_line_generator ( char *  out,
size_t  buf_size,
void d 
)

Definition at line 1096 of file icalparser.c.

{
    char *n;
    size_t size;
    struct slg_data* data = (struct slg_data*)d;

    if(data->pos==0){
       data->pos=data->str;
    }

    /* If the pointer is at the end of the string, we are done */
    if (*(data->pos)==0){
       return 0;
    }

    n = strchr(data->pos,'\n');
    
    if (n == 0){
       size = strlen(data->pos);
    } else {
       n++; /* include newline in output */
       size = (n-data->pos);       
    }

    if (size > buf_size-1){
       size = buf_size-1;
    }
    

    strncpy(out,data->pos,size);
    
    *(out+size) = '\0';
    
    data->pos += size;

    return out;    
}

Here is the call graph for this function: