aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/lexer.c
blob: e03cb666fbe7d40582f2c55d80abdeccaa98f772 (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
435
436
437
438
439
440
441
/*
 * lexer.c: Reads in the .calendar files
 */
#include <stdio.h>
#include <glib.h>
#include "cal_struct.h"


#define opener "["
#define closer "]"
#define VersionMajor 2

GSList *eventlist;

void print_glist(gpointer data, gpointer user_data)
{
    struct event *myevent = (struct event*)data;

    if (data == NULL)
        return;
    printf ("===============================================\nNew event\n");
    printf ("Start: %s %02d:%02d  End: %s %02d:%02d\n", myevent->start.date, myevent->start.time / 60, myevent->start.time % 60, myevent->end.date, myevent->end.time / 60, myevent->end.time % 60);
    printf ("Contents: %s\n", myevent->description);
    printf ("Repeat = %d (%d)", (int)(myevent->repeat), myevent->repeatcount);
}


int skip_chars(FILE *fp, char *terminator)
{
    int c;
    int cnt;

    cnt = 0;
    while( (c = fgetc(fp)) != EOF) {
        if (c == terminator[cnt]) {
            cnt++;
            if (terminator[cnt] == '\0')
                return TRUE;
        } else
            cnt = 0;
    }
    return FALSE;
}

int peek_char(FILE *fp, char *c)
{
    if ( ((*c) = fgetc(fp)) != EOF) {
        ungetc((*c), fp);
        return TRUE;
    } else  
        return FALSE;
}

int skip_whitespace(FILE *fp)
{
    int c;

    while( (c = fgetc(fp)) != EOF)
        if (!isspace(c)) {
            ungetc(c, fp);
            return TRUE;
        }
    return FALSE;
}

int get_until(FILE *fp, char terminator, char *buf) 
{
    int c;

    while( (c = fgetc(fp)) != EOF) {
        if (c == terminator) {
                *buf = '\0';
            return TRUE;
            }
        *buf = (char)c;
        buf++;
    }
    *buf = '\0';
    return FALSE;
}

int get_number(FILE *fp, int *x) 
{
    char buf[50];
    int c;
    int cnt;

    cnt = 0;
    buf[cnt] = '\0';
    while( (c= fgetc(fp)) != EOF) {
       if (!isdigit(c)) {
        ungetc(c, fp);
        *x = atoi(buf);
        return TRUE;
       }
       buf[cnt++] = (char)c;
       buf[cnt] = '\0';
    }
    *x = atoi(buf);
    return FALSE;
}   

/* Get string until EOF or closer_char */
int get_string(FILE *fp, char *string)
{
    int c;
    int cnt;

    cnt = 0;
    while ( (c = fgetc(fp)) != EOF) {
        if (c == closer[0]) {
            string[cnt] = '\0';
            ungetc((char)c, fp);
            return TRUE;
        }
        string[cnt++] = (char)c;
    }   
    return FALSE;
}

int get_dates(FILE *fp, char *keyword, struct event *ptr)
{
    char *c;
    int x;

    if (strncmp("Single", keyword, 6) == 0) {
        ptr->repeat = Single;
        /* It's a single date */
        if (! skip_whitespace(fp) || !get_until(fp, ' ', ptr->start.date))
            return FALSE;
        if (! skip_chars(fp, "End"))
            return FALSE;
        return TRUE;
    } else if (strncmp("Days", keyword, 4) == 0) {
        ptr->repeat = Days;
        if (! skip_whitespace(fp) || !get_until(fp, ' ', ptr->start.date))
            return FALSE;
        if (! skip_whitespace(fp) || !get_number(fp, &(ptr->repeatcount)))
            return FALSE;
        if (! skip_chars(fp, "End"))
            return FALSE;
        return TRUE;
    }

    return FALSE;
}

int getid(FILE *fp, char *string)
{
    int c;
    int cnt;

    cnt = 0;
    while( (c =fgetc(fp)) != EOF) {
        if (isalnum(c)) 
            string[cnt++] = (char)c;
        else {
            string[cnt] = '\0';
            return TRUE;
        }
    }
    string[cnt] = '\0';
    return FALSE;
}

int parse_appointment(FILE *fp, struct event *ptr, char keyword[])
{
    char buf[50];
    int x,y,c;

    if (strcmp(keyword, "Start") == 0) {
        if ( ! skip_whitespace(fp) || ! get_number(fp, &x) ) {
            g_error("Unable to get start time");
            return FALSE;
        }
        g_print ("Appointment start = %02d:%02d\n", x/60, x % 60);
        ptr->start.time = x;
        return TRUE;
    }

    if (strcmp(keyword, "Length") == 0) {
        if ( ! skip_whitespace(fp) || ! get_number(fp, &x) ) {
            g_error("Unable to get length");
            return FALSE;
        }
        g_print ("Appointment length = %d\n", x);
        ptr->end.time = ptr->start.time + x;
        return TRUE;
    }

    if (strcmp(keyword, "Alarms") == 0) {
        while(TRUE) {
            skip_whitespace(fp);
            if (!peek_char(fp, (char*)&c)) {
                g_error("Cannot read alarm list");
                return FALSE;
            }
            if (!isdigit(c))
                break;

            if (! get_number(fp, &x))
                return FALSE;

            g_print("New alarm %d\n", x);
        }
        return TRUE;
    }

    g_print("Unknown keyword %s\n", keyword);
    return FALSE;
}
            
int parse_item(FILE *fp, struct event *ptr, char keyword[]) 
{
    char buf[50];
    int x, y, c;

    if (strcmp(keyword, "Remind") == 0) {
        if (! skip_whitespace(fp) || ! get_number(fp, &x)) {
            g_error("Cannot get remind level");
            return FALSE;
        }
        g_print("Remind level = %d\n", x);
        return TRUE;
    }

    if (strcmp(keyword, "Owner") == 0) {
        if (!get_string(fp, buf)) {
            g_error("Cannot get owner information");
            return FALSE;
        }
        g_print("Owner = %s\n", buf);
        return TRUE;
    }

    if (strcmp(keyword, "Uid") == 0) {
        if (!skip_whitespace(fp) || !get_until(fp, *closer, buf)) {
            g_error("Cannot get unique ID");
            return FALSE;
        }
        g_print("UID = %s\n", buf);
        return TRUE;
    }

    if (strcmp(keyword, "Contents") == 0) {
        if (!get_string(fp, buf)) {
            g_error("Cannot get item text");
            return FALSE;
        }
        g_print("Contents = %s\n", buf);
        strcpy(ptr->description,buf);
        return TRUE;
    }

    if (strcmp(keyword, "Text") == 0) {
        if (! skip_whitespace(fp) || ! get_number(fp, &x) ||
            (x < 0) || ! skip_whitespace(fp) || ! skip_chars(fp, opener) ) {
            g_error("Cannot get item text");
            return FALSE;
        }
        y = 0;
        while(y < x) {
            if ( (c = fgetc(fp)) == EOF) {
                g_error("Short item text");
                return FALSE;
            }
            buf[y++] = (char)c;
        }
        buf[y] = '\0';
        g_print("Text = %s\n", buf);
        return TRUE;
    }
    
    if (strcmp(keyword, "Dates") == 0) {
        if ( ! getid(fp, buf)) {
            g_error("Cannot get date");
            return FALSE;
        }
        return get_dates(fp, buf,ptr);
    }

    if (strcmp(keyword, "Deleted") == 0) {
        if (! skip_whitespace(fp) || ! get_number(fp, &x)) {
            g_error("Cannot get deleted day");
            return FALSE;
        }
        g_print("%d/", x);
        if (! skip_whitespace(fp) || ! get_number(fp, &x)) {
            g_error("Cannot get deleted month");
            return FALSE;
        }
        g_print("%d/", x);
        if (! skip_whitespace(fp) || ! get_number(fp, &x)) {
            g_error("Cannot get deleted year");
            return FALSE;
        }
        g_print("%d\n", x);
        return TRUE;
    }

    if (strcmp(keyword, "Hilite") == 0) {
        if (! get_string(fp, buf) ) {
            g_error("Cannot get hilite data");
            return FALSE;
        }
        g_print("Hilite = %s\n", buf);
        return  TRUE;
    }

    if (strcmp(keyword, "Todo") == 0) {
        g_print("Todo\n");
        return TRUE;
    }


    if (strcmp(keyword, "Done") == 0) {
        g_print("Done\n");
        return TRUE;
    }

    return FALSE;
}

void parse_ical_file(char const *file)
{
    FILE *fp;
    int finished;
    char keyword[50];
    int file_major, file_minor;
    char c;
    int item_type;
    int incomplete_item;
    struct event *myevent;

    if ( (fp = fopen(file, "r")) == NULL) {
        g_error("couldn't open file");
        return;
    }

    finished = FALSE;

    if (!skip_whitespace(fp))
        return;

    if (! skip_chars(fp, "Calendar") || ! skip_whitespace(fp) ) {
        g_error("unable to find calendar file");
        fclose(fp);
        return;
    }

    if (! skip_chars(fp, opener) || ! skip_chars(fp, "v") ) {
        g_error("Unable to get version line");
        fclose(fp);
        return;
    }
    if (! get_number(fp, &file_major) || ! (file_major >=0) || (file_major > VersionMajor)) {
        g_error("Missing/bad major version");
        fclose(fp);
        return;
    }

    if (! skip_chars(fp, ".") || ! get_number(fp, &file_minor) ||
        ! skip_chars(fp, "]") || ! skip_whitespace(fp) ) {
        g_error("Missing  minor version");
        fclose(fp);
        return;
    }
    if (file_minor > 0) {
        g_error("Bad  minor version");
        fclose(fp);
        return;
    }

    while(TRUE) {
        g_print("----------------------------------------\n");
        item_type= 0;
        skip_whitespace(fp);
        if (! getid(fp,keyword) || ! skip_whitespace(fp) ||
            ! skip_chars(fp, opener) || ! skip_whitespace(fp) ) {
            fclose(fp);
            return;
        }

        if (strcmp(keyword, "Appt") == 0) {
            g_print("New Appointment\n");
            item_type = 1;

        } else if (strcmp(keyword, "Note") == 0) {
            g_print("New Note\n");
            item_type = 2;
        } else 
            g_print("New ??? (%s)\n", keyword);

        incomplete_item = TRUE;
        myevent = g_malloc0(sizeof(struct event));
        while(incomplete_item) {
            if (! skip_whitespace(fp) || ! peek_char(fp, &c)) {
                g_warning("Incomplete item\n");
                fclose(fp);
                return;
            }
            if (c == closer[0]) {
                (void)fgetc(fp);
                g_print("done!\n");
                incomplete_item = FALSE;
                g_slist_append(eventlist, myevent);
                break;
            }
        
            if (! getid(fp,keyword) || ! skip_whitespace(fp) ||
                ! skip_chars(fp, opener) ) {
                g_error("Error reading item property name");
                fclose(fp);
                return;
            }
            if ( ! parse_item(fp, myevent, keyword) && ! parse_appointment(fp, myevent, keyword) ) {
                g_warning("Unable to parse line\n");
                fclose(fp);
                return;
            }
            if ( ! skip_whitespace(fp) || ! skip_chars(fp, closer)) {
                g_error("Error reading item property");
                fclose(fp);
                return;
            }
        } /* while */
    } /* while */
}
        
    


int main(int argc, char *argv[])
{

    eventlist = g_slist_alloc();
    parse_ical_file("/home/csmall/.calendar");
    g_slist_foreach(eventlist, print_glist, NULL);
    return 0;
}