File: parser.c

package info (click to toggle)
librra 0.9.0-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,032 kB
  • ctags: 1,045
  • sloc: ansic: 9,914; sh: 8,954; makefile: 125
file content (679 lines) | stat: -rw-r--r-- 15,699 bytes parent folder | download
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/* $Id: parser.c,v 1.17 2004/08/17 16:13:21 twogood Exp $ */
#define _GNU_SOURCE
#include "parser.h"
#include "dbstream.h"
#include "timezone.h"
#include "environment.h"
#include <synce_log.h>
#include <synce_hash.h>
#include <rapi.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <synce_log.h>
#include <assert.h>

#define VERBOSE 0

#define STR_EQUAL(a,b)  (0 == strcasecmp(a,b))

#define MAX_PROPVAL_COUNT         50

/* for parser_component_new() */
#define parser_component_HASH_SIZE   5
#define parser_property_HASH_SIZE    5

/* for parser_duration_to_seconds() */
#define SECONDS_PER_MINUTE  (60)
#define SECONDS_PER_HOUR    (SECONDS_PER_MINUTE * 60)
#define SECONDS_PER_DAY     (SECONDS_PER_HOUR   * 24)
#define SECONDS_PER_WEEK    (SECONDS_PER_DAY    * 7)

struct _ParserProperty
{
  char* name;
  ParserPropertyFunc func;
  bool used;
};

struct _ParserComponent
{
  char* name;
  SHashTable* parser_components;
  SHashTable* parser_properties;
};

struct _Parser
{
  ParserComponent* base_parser_component;
  RRA_Timezone* tzi;
  int flags;
  void* cookie;
  mdir_line** mimedir;
  mdir_line** iterator;
  CEPROPVAL propvals[MAX_PROPVAL_COUNT];
	size_t propval_count;
};

bool parser_duration_to_seconds(const char* duration, int* result)/*{{{*/
{
  enum { dur_sign, dur_p, dur_any, dur_time, dur_end } state = dur_sign;
  const char *p;
  struct tm time_struct;
  int sign = 1;
  int value = 0;
  int seconds = 0;

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

  for (p = duration; *p; p++)
  {
    switch (state)
    {
      case dur_sign:/*{{{*/
        switch (*p)
        {
          case '+':
            /* default sign */
            state = dur_p;
            break;

          case '-':
            sign = -1;
            state = dur_p;
            break;

          case 'P':
            /* use default sign */
            state = dur_any;
            break;

          default:
            synce_error("Unexpected char '%c' at offset %i in duration '%s'",
                *p, p - duration, duration);
            return false;
        }
        break;/*}}}*/

      case dur_p:
        if ('P' == *p)
          state = dur_any;
        else
        {
          synce_error("Unexpected char '%c' at offset %i in duration '%s'",
              *p, p - duration, duration);
          return false;
        }
        break;

      case dur_any:/*{{{*/
        switch (*p)
        {
          case 'W':
            seconds += value * SECONDS_PER_WEEK;
            state = dur_end;
            break;
          case 'D':
            seconds += value * SECONDS_PER_DAY;
            value = 0;
            break;
          case 'T':
            state = dur_time;
            break;

          default:
            if (isdigit(*p))
            {
              value = (value * 10) + (*p - '0');
            }
            else 
            {
              synce_error("Unexpected char '%c' at offset %i in duration '%s'",
                  *p, p - duration, duration);
              return false;
            }
            break;
        }
        break;/*}}}*/

      case dur_time:/*{{{*/
        switch (*p)
        {
          case 'H':
            seconds += value * SECONDS_PER_HOUR;
            value = 0;
            break;
          case 'M':
            seconds += value * SECONDS_PER_MINUTE;
            value = 0;
            break;
          case 'S':
            seconds += value;
            state = dur_end;
            break;

          default:
            if (isdigit(*p))
            {
              value = (value * 10) + (*p - '0');
            }
            else 
            {
              synce_error("Unexpected char '%c' at offset %i in duration '%s'",
                  *p, p - duration, duration);
              return false;
            }
            break;
        }
        break;/*}}}*/

      case dur_end:
        synce_error("Unexpected char '%c' at offset %i in duration '%s'",
            *p, p - duration, duration);
        return false;
    }
  }

  *result = seconds * sign;
  return true;

}/*}}}*/

bool parser_datetime_to_struct(const char* datetime, struct tm* time_struct, bool* is_utc)/*{{{*/
{
  int count;
  char suffix = 0;
  memset(time_struct, 0, sizeof(struct tm));

  count = sscanf(datetime, "%4d%2d%2dT%2d%2d%2d%1c", 
      &time_struct->tm_year,
      &time_struct->tm_mon,
      &time_struct->tm_mday,
      &time_struct->tm_hour,
      &time_struct->tm_min,
      &time_struct->tm_sec,
      &suffix);

  if (count != 3 && count != 6 && count != 7)
  {
    synce_error("Bad date-time: '%s'", datetime);
    return false;
  }

  if (count >= 7)
  {
    if ('Z' != suffix)
      synce_warning("Unknown date-time suffix: '%c'", suffix);
  }

  if (is_utc)
    *is_utc = ('Z' == suffix);

  time_struct->tm_year -= 1900;
  time_struct->tm_mon--;
  time_struct->tm_isdst = -1;

  return true;
}/*}}}*/

bool parser_datetime_to_unix_time(const char* datetime, time_t* unix_time, bool* is_utc)/*{{{*/
{
  void* handle = NULL;
  struct tm time_struct;
  bool local_is_utc;

  if (!parser_datetime_to_struct(datetime, &time_struct, &local_is_utc))
  {
    synce_error("Failed to parse DATE or DATE-TIME to struct tm");
    return false;
  }
  
  if (local_is_utc)
    handle = environment_push_timezone("UTC");
#if VERBOSE
  else
    synce_debug("Using system timezone configuration for appointment.");
#endif
  
  *unix_time = mktime(&time_struct);
  
  if (local_is_utc)
    environment_pop_timezone(handle);

  if (is_utc)
    *is_utc = local_is_utc;

  return -1 != *unix_time;
}/*}}}*/

static CEPROPVAL* parser_get_next_propval(Parser* self)/*{{{*/
{
  if (MAX_PROPVAL_COUNT == self->propval_count)
  {
    synce_error("Too many propvals.");
    return NULL;
  }
  else
    return &self->propvals[self->propval_count++];
}/*}}}*/

bool parser_add_blob(Parser* self, uint16_t id, const uint8_t* data, size_t data_size)/*{{{*/
{
  CEPROPVAL* propval = parser_get_next_propval(self);
  if (!propval)
    return false;

  assert(data);

  propval->propid = (id << 16) | CEVT_BLOB;
  propval->val.blob.dwCount = data_size;
  propval->val.blob.lpb     = malloc(data_size);
  assert(propval->val.blob.lpb);
  memcpy(propval->val.blob.lpb, data, data_size);

  return true;
}/*}}}*/

bool parser_add_int16(Parser* self, uint16_t id, int16_t value)/*{{{*/
{
  CEPROPVAL* propval = parser_get_next_propval(self);
  if (!propval)
    return false;

  propval->propid = (id << 16) | CEVT_I2;
  propval->val.iVal = value;

  return true;
}/*}}}*/

bool parser_add_int32 (Parser* self, uint16_t id, int32_t value)/*{{{*/
{
  CEPROPVAL* propval = parser_get_next_propval(self);
  if (!propval)
    return false;

  propval->propid = (id << 16) | CEVT_I4;
  propval->val.lVal = value;

  return true;
}/*}}}*/

bool parser_add_string(Parser* self, uint16_t id, const char* str)/*{{{*/
{
  WCHAR* wstr = NULL;

  if (self->flags & PARSER_UTF8)
    wstr = wstr_from_utf8(str);
  else
    wstr = wstr_from_ascii(str);

  if (wstr)
  {
   CEPROPVAL* propval = parser_get_next_propval(self);
    if (!propval)
      return false;

    propval->propid = (id << 16) | CEVT_LPWSTR;
    propval->val.lpwstr = wstr;

    return true;
  }
  else
  {
    synce_error("Failed to convert string '%s' to wide string. UTF8 = %s",
        str, (self->flags & PARSER_UTF8) ? "true" : "false");
    return false;
  }
}/*}}}*/

bool parser_add_time  (Parser* self, uint16_t id, time_t value)/*{{{*/
{
  CEPROPVAL* propval = parser_get_next_propval(self);
  if (!propval)
    return false;

  propval->propid = (id << 16) | CEVT_FILETIME;

  filetime_from_unix_time(value, &propval->val.filetime);

  return true;
}/*}}}*/

bool parser_add_filetime(Parser* self, uint16_t id, FILETIME* filetime)
{
  CEPROPVAL* propval = parser_get_next_propval(self);
  if (!propval)
    return false;

  propval->propid = (id << 16) | CEVT_FILETIME;

  memcpy(&propval->val.filetime, filetime, sizeof(FILETIME));

  return true;
}

bool parser_add_string_from_line(Parser* self, uint16_t id, mdir_line* line)/*{{{*/
{
  return parser_add_string(self, id, line->values[0]);
}/*}}}*/

ParserTimeFormat parser_get_time_format(mdir_line* line)/*{{{*/
{
  ParserTimeFormat format = PARSER_TIME_FORMAT_DATE_AND_TIME; /* default */
  char** types = mdir_get_param_values(line, "VALUE");
  
  if (types && types[0])
  {
    if (STR_EQUAL(types[0], "DATE"))
    {
      format = PARSER_TIME_FORMAT_ONLY_DATE;
    }
    else if (!STR_EQUAL(types[0], "DATE-TIME"))
    {
      synce_warning("Unknown data type: '%s'", types[0]);
      format = PARSER_TIME_FORMAT_UNKNOWN;
    }
  }

  return format;
}/*}}}*/

bool parser_add_time_from_line  (Parser* self, uint16_t id, mdir_line* line)/*{{{*/
{
  bool success = false;
  time_t some_time;

  ParserTimeFormat format = parser_get_time_format(line);

  if (!line)
    return false;

  if (format == PARSER_TIME_FORMAT_DATE_AND_TIME ||
      format == PARSER_TIME_FORMAT_ONLY_DATE)
  {
    bool is_utc = false;
    
    success = parser_datetime_to_unix_time(line->values[0], &some_time, &is_utc);
    if (!success)
    {
      synce_error("Failed to convert DATE or DATE-TIME to UNIX time: '%s'",
          line->values[0]);
    }
  }

  return success && parser_add_time(self, id, some_time);
}/*}}}*/

ParserProperty* parser_property_new(const char* name, ParserPropertyFunc func)/*{{{*/
{
  ParserProperty* self = (ParserProperty*)calloc(1, sizeof(ParserProperty));
  
  if (self)
  {
    self->name = name ? strdup(name) : NULL;
    self->func = func;
    self->used = false;
  }
  
  return self;
}/*}}}*/

void parser_property_destroy(ParserProperty* self)/*{{{*/
{
  if (self)
  {
    free(self->name);
    free(self);
  }
}/*}}}*/

ParserComponent* parser_component_new(const char* name)/*{{{*/
{
  ParserComponent* self = (ParserComponent*)calloc(1, sizeof(ParserComponent));
  
  if (self)
  {
    self->name = name ? strdup(name) : NULL;
    
    self->parser_components = s_hash_table_new(s_str_hash, s_str_equal_no_case,
        parser_component_HASH_SIZE);
    self->parser_properties  = s_hash_table_new(s_str_hash, s_str_equal_no_case, 
        parser_property_HASH_SIZE);
  }

  return self;
}/*}}}*/

void parser_component_destroy(ParserComponent* self)/*{{{*/
{
  if (self)
  {
    s_hash_table_destroy(self->parser_components, NULL); /*(void (*)(void *))parser_component_destroy); */
    s_hash_table_destroy(self->parser_properties,  (void (*)(void *))parser_property_destroy);
    if (self->name)
      free(self->name);
    free(self);
  }
}/*}}}*/

void parser_component_add_parser_component(ParserComponent* self, ParserComponent* ct)/*{{{*/
{
  s_hash_table_insert(self->parser_components, ct->name, ct);
}/*}}}*/

void parser_component_add_parser_property (ParserComponent* self, ParserProperty* pt)/*{{{*/
{
  s_hash_table_insert(self->parser_properties, pt->name, pt);
}/*}}}*/

ParserComponent* parser_component_get_parser_component(ParserComponent* self, const char* name)/*{{{*/
{
  if (self && name)
    return (ParserComponent*)s_hash_table_lookup(self->parser_components, name);
  else
    return NULL;
}/*}}}*/

ParserProperty* parser_component_get_parser_property(ParserComponent* self, const char* name)/*{{{*/
{
  if (self && name)
    return (ParserProperty*)s_hash_table_lookup(self->parser_properties, name);
  else
    return NULL;
}/*}}}*/

static bool parser_handle_component(Parser* p, ParserComponent* ct)/*{{{*/
{ 
  bool success = false;
  mdir_line* line = NULL;

  while ( (line = *p->iterator++) )
  {
    if (STR_EQUAL(line->name, "BEGIN"))
    {
      bool result;
      ParserComponent* other =
        parser_component_get_parser_component(ct, line->values[0]);

      if (other)
      {
        result = parser_handle_component(p, other);
      }
      else
      {
        /* create and use temporary component type */
        /*synce_trace("Handling unknown component '%s'", line->values[0]);*/
        other = parser_component_new(line->values[0]);
        result = parser_handle_component(p, other);
        parser_component_destroy(other);
      }

      if (!result)
      {
        synce_error("Failed to handle component '%s'", line->values[0]);
        break;
      }
    }
    else if (STR_EQUAL(line->name, "END"))
    {
      if (STR_EQUAL(line->values[0], ct->name))
      {
        success = true;
        break;
      }
      else
      {
        synce_error("Unexpected END: '%s'", line->values[0]);
        break;
      }
    }
    else
    {
      ParserProperty* pt = 
        parser_component_get_parser_property(ct, line->name);
      
      if (pt)
      {
        if (!pt->func(p, line, p->cookie))
        {
          synce_error("Failed to handle property '%s'", line->name);
          break;
        }

        pt->used = true;
      }
      /*else
      {
        synce_trace("Property '%s' not handled", line->name);
      }*/
    }
  }

  /* no more lines, success! */
  if (!line)
    success = true;

  return success;
}/*}}}*/

Parser* parser_new(/*{{{*/
    ParserComponent* base_parser_component, 
    int flags,
    RRA_Timezone* tzi, 
    void* cookie)
{
  Parser* self = (Parser*)calloc(1, sizeof(Parser));

  if (self)
  {
    self->base_parser_component = base_parser_component;
    self->tzi                   = tzi;
    self->flags                 = flags;
    self->cookie                = cookie;
  }

  return self;
}/*}}}*/

bool parser_set_mimedir(Parser* self, const char* mimedir)/*{{{*/
{
  if (self->mimedir)
    return false;

  self->iterator = self->mimedir = mdir_parse((char*)mimedir);

  return NULL != self->mimedir;
}/*}}}*/

void parser_destroy(Parser* self)/*{{{*/
{
  if (self)
  {
    size_t i;

    /* free strings in propvals array */
    for (i = 0; i < self->propval_count; i++)
    {
      switch (self->propvals[i].propid & 0xffff)
      {
        case CEVT_BLOB:
          assert(self->propvals[i].val.blob.lpb);
          free(self->propvals[i].val.blob.lpb);
          break;

        case CEVT_LPWSTR:
          wstr_free_string(self->propvals[i].val.lpwstr);
          break;
      }
    }
    
    mdir_free(self->mimedir);
    free(self);
  }
}/*}}}*/

bool parser_utf8(Parser* self)
{
  if (self)
    return self->flags & PARSER_UTF8;
  else
    return false;
}

bool parser_run(Parser* self)/*{{{*/
{
  bool success = false;
  
  if (!self || !self->mimedir || self->propval_count)
  {
    synce_error("Invalid parser state");
    goto exit;
  }

  if (!parser_handle_component(self, self->base_parser_component))
  {
    synce_error("Failed to parse components");
    goto exit;
  }

  success = true;

exit:
  return success;
}/*}}}*/

static void parser_on_property(const void* key, const void* data, void* cookie)/*{{{*/
{
  ParserProperty* property = (ParserProperty*)data;
  if (!property->used)
  {
    Parser* p = (Parser*)cookie;

    if (property->func(p, NULL, p->cookie))
      property->used = true;
  }
}/*}}}*/

static void parser_on_component(const void* key, const void* data, void* cookie)/*{{{*/
{
  const ParserComponent* component = (const ParserComponent*)data;
  s_hash_table_foreach(component->parser_components, parser_on_component, cookie);
  s_hash_table_foreach(component->parser_properties, parser_on_property,  cookie);
}/*}}}*/

void parser_call_unused_properties(Parser* self)
{
  parser_on_component(NULL, self->base_parser_component, self);
}

bool parser_get_result(Parser* self, uint8_t** result, size_t* result_size)/*{{{*/
{
  if (self && self->propval_count && result && result_size)
    return dbstream_from_propvals(
        self->propvals,
        self->propval_count,
        result,
        result_size);
  else
    return false;
}/*}}}*/