aboutsummaryrefslogtreecommitdiffstats
path: root/libical/src/libical/icalitipy.y
blob: 07573d62cdaddbf1db55b866a93f2950d99e5ed0 (plain) (blame)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
%{
/* -*- Mode: C -*-
  ======================================================================
  FILE: icalitip.y
  CREATOR: eric 10 June 1999
  
  DESCRIPTION:
  
  $Id: icalitipy.y,v 1.3 2000/05/15 06:18:17 ericb Exp $
  $Locker:  $

  (C) COPYRIGHT 1999 Eric Busboom 
  http://www.softwarestudio.org

  The contents of this file are subject to the Mozilla Public License
  Version 1.0 (the "License"); you may not use this file except in
  compliance with the License. You may obtain a copy of the License at
  http://www.mozilla.org/MPL/
 
  Software distributed under the License is distributed on an "AS IS"
  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  the License for the specific language governing rights and
  limitations under the License.

  The original author is Eric Busboom
  The original code is icalitip.y



  ================================b======================================*/

#include <stdlib.h>
#include <string.h> /* for strdup() */
#include <limits.h> /* for SHRT_MAX*/
#include "icalparser.h"
#include "ical.h"
#include "pvl.h"

#define YYERROR_VERBOSE
#define YYDEBUG 1 

icalvalue *icalparser_yy_value; /* Current Value */

/* Globals for UTCOFFSET values */
int utc; 
int utc_b; 
int utcsign;

/* Globals for DURATION values */
struct icaldurationtype duration;

/* Globals for RECUR values */
struct icalrecurrencetype recur;
short skiplist[367];
short skippos;

void copy_list(short* array, size_t size);
void clear_recur();
void add_prop(icalproperty_kind);
void icalparser_fill_date(struct tm* t, char* dstr);
void icalparser_fill_time(struct tm* t, char* tstr);
void set_value_type(icalvalue_kind kind);
void yyerror(char *s); /* Don't know why I need this.... */
int yylex(void); /* Or this. */
void set_parser_value_state();
struct icaltimetype fill_datetime(char* d, char* t);



/* Set the state of the lexer so it will interpret values ( iCAL
   VALUEs, that is, ) correctly. */

%}

%union {
    float v_float;
    int   v_int;
    char* v_string;

}

%token <v_string> DIGITS
%token <v_int> INTNUMBER
%token <v_float> FLOATNUMBER
%token <v_string> STRING   
%token EOL EQUALS CHARACTER COLON COMMA SEMICOLON TIMESEPERATOR 

%token TRUE FALSE

%token FREQ BYDAY BYHOUR BYMINUTE BYMONTH BYMONTHDAY BYSECOND BYSETPOS BYWEEKNO
%token BYYEARDAY DAILY MINUTELY MONTHLY SECONDLY WEEKLY HOURLY YEARLY
%token INTERVAL COUNT UNTIL WKST MO SA SU TU WE TH FR 

%token BIT8 ACCEPTED ADD AUDIO BASE64 BINARY BOOLEAN BUSY BUSYTENTATIVE
%token BUSYUNAVAILABLE CALADDRESS CANCEL CANCELLED CHAIR CHILD COMPLETED
%token CONFIDENTIAL CONFIRMED COUNTER DATE DATETIME DECLINECOUNTER DECLINED
%token DELEGATED DISPLAY DRAFT DURATION EMAIL END FINAL FLOAT FREE GREGORIAN
%token GROUP INDIVIDUAL INPROCESS INTEGER NEEDSACTION NONPARTICIPANT
%token OPAQUE OPTPARTICIPANT PARENT PERIOD PRIVATE PROCEDURE PUBLIC PUBLISH
%token RECUR REFRESH REPLY REQPARTICIPANT REQUEST RESOURCE ROOM SIBLING
%token START TENTATIVE TEXT THISANDFUTURE THISANDPRIOR TIME TRANSPAENT
%token UNKNOWN UTCOFFSET XNAME

%token ALTREP CN CUTYPE DAYLIGHT DIR ENCODING EVENT FBTYPE FMTTYPE LANGUAGE 
%token MEMBER PARTSTAT RANGE RELATED RELTYPE ROLE RSVP SENTBY STANDARD URI

%token TIME_CHAR UTC_CHAR


%%

value:
       binary_value
    | boolean_value
    | date_value
    | datetime_value
    | duration_value
    | period_value
    | recur_value
        | utcoffset_value
        | error { 
                  icalparser_yy_value = 0;
          icalparser_clear_flex_input();
                  yyclearin;
                  }

binary_value: "unimplemented2"

boolean_value: 
        TRUE
    { icalparser_yy_value = icalvalue_new_boolean(1); }
    | FALSE     
    { icalparser_yy_value = icalvalue_new_boolean(0); }

date_value: DIGITS
        {
        struct icaltimetype stm;

        stm = fill_datetime($1,0);

        stm.hour = -1;
        stm.minute = -1;
        stm.second = -1;
        stm.is_utc = 0;
        stm.is_date = 1;

        icalparser_yy_value = icalvalue_new_date(stm);
    }

utc_char: 
    /*empty*/  {utc = 0;}
    | UTC_CHAR {utc = 1;}

/* This is used in the period_value, where there may be two utc characters per rule. */
utc_char_b: 
    /*empty*/  {utc_b = 0;}
    | UTC_CHAR {utc_b = 1;}

datetime_value: 
    DIGITS TIME_CHAR DIGITS utc_char
        {
        struct  icaltimetype stm;
        stm = fill_datetime($1, $3);
        stm.is_utc = utc;
        stm.is_date = 0;

        icalparser_yy_value = 
        icalvalue_new_datetime(stm);
    }


/* Duration */


dur_date: dur_day
    | dur_day dur_time

dur_week: DIGITS 'W'
    {
        duration.weeks = atoi($1);
    }

dur_time: TIME_CHAR dur_hour 
    {
    }
    | TIME_CHAR dur_minute
    {
    }
    | TIME_CHAR dur_second
    {
    }

dur_hour: DIGITS 'H'
    {
        duration.hours = atoi($1);
    }
    | DIGITS 'H' dur_minute
    {
        duration.hours = atoi($1);
    }

dur_minute: DIGITS 'M'
    {
        duration.minutes = atoi($1);
    }
    | DIGITS 'M' dur_second
    {
        duration.minutes = atoi($1);
    }

dur_second: DIGITS 'S'
    {
        duration.seconds = atoi($1);
    }

dur_day: DIGITS 'D'
    {
        duration.days = atoi($1);
    }

dur_prefix: /* empty */
    {
    } 
    | '+'
    {
    }
    | '-'
    {
    }

duration_value: dur_prefix 'P' dur_date
    { 
        icalparser_yy_value = icalvalue_new_duration(duration); 
        memset(&duration,0, sizeof(duration));
    }
    | dur_prefix 'P' dur_time
    { 
        icalparser_yy_value = icalvalue_new_duration(duration); 
        memset(&duration,0, sizeof(duration));
    }
    | dur_prefix 'P' dur_week
    { 
        icalparser_yy_value = icalvalue_new_duration(duration); 
        memset(&duration,0, sizeof(duration));
    }


/* Period */

period_value:  DIGITS TIME_CHAR DIGITS utc_char '/'  DIGITS TIME_CHAR DIGITS utc_char_b
    {
            struct icalperiodtype p;
        
        p.start = fill_datetime($1,$3);
        p.start.is_utc = utc;
        p.start.is_date = 0;


        p.end = fill_datetime($6,$8);
        p.end.is_utc = utc_b;
        p.end.is_date = 0;
        
        p.duration.days = -1;
        p.duration.weeks = -1;
        p.duration.hours = -1;
        p.duration.minutes = -1;
        p.duration.seconds = -1;

        icalparser_yy_value = icalvalue_new_period(p);
    }
    | DIGITS TIME_CHAR DIGITS utc_char '/'  duration_value
    {
            struct icalperiodtype p;
        
        p.start = fill_datetime($1,$3);
        p.start.is_utc = utc;
        p.start.is_date = 0;

        p.end.year = -1;
        p.end.month = -1;
        p.end.day = -1;
        p.end.hour = -1;
        p.end.minute = -1;
        p.end.second = -1;
           
        /* The duration_value rule setes the global 'duration'
               variable, but it also creates a new value in
               icalparser_yy_value. So, free that, then copy
               'duration' into the icalperiodtype struct. */

        p.duration = icalvalue_get_duration(icalparser_yy_value);
        icalvalue_free(icalparser_yy_value);
        icalparser_yy_value = 0;

        icalparser_yy_value = icalvalue_new_period(p);

    }



/* Recur */

recur_start:
    FREQ EQUALS SECONDLY {clear_recur();recur.freq = ICAL_SECONDLY_RECURRENCE;}
        | FREQ EQUALS MINUTELY {clear_recur();recur.freq = ICAL_MINUTELY_RECURRENCE;}
    | FREQ EQUALS HOURLY {clear_recur();recur.freq = ICAL_HOURLY_RECURRENCE;}
    | FREQ EQUALS DAILY {clear_recur();recur.freq = ICAL_DAILY_RECURRENCE;}
    | FREQ EQUALS WEEKLY {clear_recur();recur.freq = ICAL_WEEKLY_RECURRENCE;}
    | FREQ EQUALS MONTHLY {clear_recur();recur.freq = ICAL_MONTHLY_RECURRENCE;}
    | FREQ EQUALS YEARLY {clear_recur();recur.freq = ICAL_YEARLY_RECURRENCE;}
    ;


weekday:
    SU { skiplist[skippos]=ICAL_SUNDAY_WEEKDAY; if( skippos<8) skippos++;}
    | MO { skiplist[skippos]=ICAL_MONDAY_WEEKDAY;if( skippos<8) skippos++;}
    | TU { skiplist[skippos]=ICAL_TUESDAY_WEEKDAY;if( skippos<8) skippos++;}
    | WE { skiplist[skippos]=ICAL_WEDNESDAY_WEEKDAY;if( skippos<8) skippos++;}
    | TH { skiplist[skippos]=ICAL_THURSDAY_WEEKDAY;if( skippos<8) skippos++;}
    | FR { skiplist[skippos]=ICAL_FRIDAY_WEEKDAY;if( skippos<8) skippos++;}
    | SA { skiplist[skippos]=ICAL_SATURDAY_WEEKDAY;if( skippos<8) skippos++;}
    ;


weekday_list:
    weekday
    | DIGITS weekday { } /* HACK Incorectly handles int in BYDAY */
    | weekday_list COMMA weekday
    

recur_list: 
    DIGITS { skiplist[skippos] = atoi($1); skippos++;}
        | recur_list COMMA DIGITS { skiplist[skippos] = atoi($3); if (skippos<367) skippos++;}
    ;

recur_skip:
    INTERVAL EQUALS DIGITS {recur.interval = atoi($3);}
    | WKST EQUALS SU {recur.week_start = ICAL_SUNDAY_WEEKDAY;}
    | WKST EQUALS MO {recur.week_start = ICAL_MONDAY_WEEKDAY;}
    | WKST EQUALS TU {recur.week_start = ICAL_TUESDAY_WEEKDAY;}
    | WKST EQUALS WE {recur.week_start = ICAL_WEDNESDAY_WEEKDAY;}
    | WKST EQUALS TH {recur.week_start = ICAL_THURSDAY_WEEKDAY;}
    | WKST EQUALS FR {recur.week_start = ICAL_FRIDAY_WEEKDAY;}
    | WKST EQUALS SA {recur.week_start = ICAL_SATURDAY_WEEKDAY;}
    | BYSECOND EQUALS recur_list{copy_list(recur.by_second,60);}
    | BYMINUTE EQUALS recur_list{copy_list(recur.by_minute,60);}
    | BYHOUR EQUALS recur_list{copy_list(recur.by_hour,24);}
    | BYDAY EQUALS weekday_list{copy_list(recur.by_day,7);}
    | BYMONTH EQUALS recur_list{copy_list(recur.by_month,12);}
    | BYMONTHDAY EQUALS recur_list{copy_list(recur.by_month_day,31);}
    | BYYEARDAY EQUALS recur_list{copy_list(recur.by_year_day,366);}
    | BYWEEKNO EQUALS recur_list{copy_list(recur.by_week_no,53);}
    | BYSETPOS EQUALS recur_list{copy_list(recur.by_set_pos,366);}
    | UNTIL EQUALS datetime_value
           { recur.until = icalvalue_get_datetime(icalparser_yy_value);
       icalvalue_free(icalparser_yy_value); icalparser_yy_value=0;}
    | UNTIL EQUALS date_value 
           { recur.until = icalvalue_get_date(icalparser_yy_value);
       icalvalue_free(icalparser_yy_value); icalparser_yy_value=0;}
    | COUNT EQUALS DIGITS
           { recur.count = atoi($3); }
    ;

recur_skip_list:
    /* empty */
    | recur_skip_list SEMICOLON recur_skip

recur_value: 
    recur_start recur_skip_list
      { icalparser_yy_value = icalvalue_new_recur(recur); }



/* UTC Offset */

plusminus: '+' { utcsign = 1; }
    | '-' { utcsign = -1; }

utcoffset_value: 
    plusminus INTNUMBER INTNUMBER
    {
        icalparser_yy_value = icalvalue_new_utcoffset( utcsign * ($2*3600) + ($3*60) );
    }

    | plusminus INTNUMBER INTNUMBER INTNUMBER
    {
        icalparser_yy_value = icalvalue_new_utcoffset(utcsign * ($2*3600) + ($3*60) +($4));
    }



%%


void clear_recur()
{   
    memset(&skiplist, ICAL_RECURRENCE_ARRAY_MAX_BYTE, sizeof(skiplist));
    skippos = 0;

    icalrecurrencetype_clear(&recur);
}

void copy_list(short* array, size_t size)
{ 
    memcpy(array, skiplist, size*sizeof(short));
    memset(&skiplist,ICAL_RECURRENCE_ARRAY_MAX_BYTE, sizeof(skiplist));
    skippos = 0;
}

struct icaltimetype fill_datetime(char* datestr, char* timestr)
{
        struct icaltimetype stm;

        memset(&stm,0,sizeof(stm));

        if (datestr != 0){
        sscanf(datestr,"%4d%2d%2d",&(stm.year), &(stm.month), 
               &(stm.day));
        }

        if (timestr != 0){
        sscanf(timestr,"%2d%2d%2d", &(stm.hour), &(stm.minute), 
               &(stm.second));
        }

        return stm;

}

void yyerror(char* s)
{
    /*fprintf(stderr,"Parse error \'%s\'\n", s);*/
}