summaryrefslogtreecommitdiff
path: root/src/bin/eldbus/parser.c
blob: f695490c46c1d5eada45718e10b56322caa68e5b (plain)
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
#include "codegen.h"

#define OBJECT_TAG "node"
#define OBJECT_TAG_LENGTH sizeof(OBJECT_TAG) - 1
#define INTERFACE_TAG "interface"
#define INTERFACE_TAG_LENGTH sizeof(INTERFACE_TAG) - 1
#define SIGNAL_TAG "signal"
#define SIGNAL_TAG_LENGTH sizeof(SIGNAL_TAG) - 1
#define METHOD_TAG "method"
#define METHOD_TAG_LENGTH sizeof(METHOD_TAG) - 1
#define PROPERTY_TAG "property"
#define PROPERTY_TAG_LENGTH sizeof(PROPERTY_TAG) - 1
#define ARG_TAG "arg"
#define ARG_TAG_LENGTH sizeof(ARG_TAG) - 1
#define ANNOTATION_TAG "annotation"
#define ANNOTATION_TAG_LENGTH sizeof(ANNOTATION_TAG) - 1

//attributes
#define NAME_ATTR "name"
#define TYPE_ATTR "type"
#define DIRECTION_ATTR "direction"
#define ACCESS_ATTR "access"
#define VALUE_ATTR "value"

#define ACCESS_ATTR_VALUE_WRITE "write"
#define ACCESS_ATTR_VALUE_READ "read"

#define DBUS_INTERFACE "org.freedesktop.DBus."

static DBus_Interface *iface;
static DBus_Signal *d_signal;
static DBus_Method *method;
static DBus_Property *property;

static Eina_Bool attributes_parse(const char *content, unsigned length, Eina_Simple_XML_Attribute_Cb func, const void *data);

static Eina_Bool
obj_attributes_parser(void *data, const char *key, const char *value)
{
   DBus_Object *obj = data;

   if (!strcmp(key, NAME_ATTR))
     obj->name = strdup(value);

   return EINA_TRUE;
}

static Eina_Bool
iface_attributes_parser(void *data EINA_UNUSED, const char *key, const char *value)
{
   if (!strcmp(key, NAME_ATTR))
     iface->name = strdup(value);

   return EINA_TRUE;
}

static Eina_Bool
signal_attributes_parser(void *data EINA_UNUSED, const char *key, const char *value)
{
   if (!strcmp(key, NAME_ATTR))
     d_signal->name = strdup(value);

   return EINA_TRUE;
}

static Eina_Bool
arg_attributes_parser(void *data EINA_UNUSED, const char *key, const char *value)
{
   DBus_Arg *arg = data;
   if (!strcmp(key, NAME_ATTR))
     arg->name = strdup(value);
   else if (!strcmp(key, TYPE_ATTR))
     arg->type = strdup(value);
   else if (!strcmp(key, DIRECTION_ATTR))
     arg->direction = value[0];

   return EINA_TRUE;
}

static Eina_Bool
method_attributes_parser(void *data EINA_UNUSED, const char *key, const char *value)
{
   if (!strcmp(key, NAME_ATTR))
     method->name = strdup(value);

   return EINA_TRUE;
}

static Eina_Bool
property_attributes_parser(void *data EINA_UNUSED, const char *key, const char *value)
{
   if (!strcmp(key, NAME_ATTR))
     property->name = strdup(value);
   else if (!strcmp(key, TYPE_ATTR))
     {
        property->type = strdup(value);
        if (value[1] || value[0] == 'v')
          property->complex = EINA_TRUE;
     }
   else if (!strcmp(key, ACCESS_ATTR))
     {
        if (!strcmp(value, ACCESS_ATTR_VALUE_READ))
          property->access = ACCESS_READ;
        else if (!strcmp(value, ACCESS_ATTR_VALUE_WRITE))
          property->access = ACCESS_WRITE;
        else
          property->access = (ACCESS_WRITE | ACCESS_READ);
     }

   return EINA_TRUE;
}

static Eina_Bool
open_object(const char *content, unsigned length, Eina_Bool is_open_empty, DBus_Object **ptr_obj)
{
   Eina_Bool r;
   DBus_Object *obj = *ptr_obj;

   if (is_open_empty) return EINA_TRUE;

   if (obj)
     {
        printf("Only one object is supported per file.");
        return EINA_FALSE;
     }
   obj = calloc(1, sizeof(DBus_Object));
   EINA_SAFETY_ON_NULL_RETURN_VAL(obj, EINA_FALSE);

   r = attributes_parse(content, length, obj_attributes_parser, obj);
   if (!obj->name) obj->name = strdup("/");

   obj->c_name = dbus_name_to_c(obj->name);

   *ptr_obj = obj;
   return r;
}

static void
interface_close(void)
{
   //its not necessary generate code to FreeDesktop interfaces
   if (!strncmp(iface->name, DBUS_INTERFACE, strlen(DBUS_INTERFACE)))
     {
        printf("Refusing to generate code for FDO interface; see eldbus_freedesktop.h\n");
        interface_free(iface);
     }
   iface = NULL;
}

static Eina_Bool
open_interface(const char *content, unsigned length, Eina_Bool is_open_empty, DBus_Object *obj)
{
   Eina_Bool r;
   char *tmp_name;

   iface = interface_new(obj);
   EINA_SAFETY_ON_NULL_RETURN_VAL(iface, EINA_FALSE);

   r = attributes_parse(content, length, iface_attributes_parser, NULL);
   if (!iface->name)
     {
        interface_free(iface);
        printf("Error interface without name.\n");
        return EINA_FALSE;
     }

   tmp_name = get_pieces(iface->name, '.', 2);
   iface->c_name = dbus_name_to_c(tmp_name);
   free(tmp_name);

   if (is_open_empty)
     interface_close();

   return r;
}

static void
signal_close(void)
{
   DBus_Arg *arg;
   EINA_INLIST_FOREACH(d_signal->args, arg)
     {
        if ((arg->type[1]) || (arg->type[0] == 'v'))
          {
             d_signal->complex = EINA_TRUE;
             break;
          }
     }
   d_signal = NULL;
}

static Eina_Bool
open_signal(const char *content, unsigned length, Eina_Bool is_open_empty)
{
   Eina_Bool r;
   char *tmp;
   int i;
   Eina_Strbuf *buf = eina_strbuf_new();

   d_signal = signal_new(iface);
   EINA_SAFETY_ON_NULL_RETURN_VAL(d_signal, EINA_FALSE);

   r = attributes_parse(content, length, signal_attributes_parser, NULL);
   if (!d_signal->name)
     {
        signal_free(d_signal);
        d_signal = NULL;
        printf("Error signal without name.\n");
        return EINA_FALSE;
     }

   tmp = dbus_name_to_c(d_signal->name);
   d_signal->c_name = string_build("%s_%s", iface->c_name, tmp);
   free(tmp);
   d_signal->cb_name = string_build("on_%s", d_signal->c_name);
   d_signal->free_function = string_build("%s_data_free", d_signal->c_name);
   d_signal->struct_name = string_build("%s_%s_Data", iface->c_name, d_signal->name);
   d_signal->struct_name[0] = toupper(d_signal->struct_name[0]);
   for (i = 0; d_signal->struct_name[i]; i++)
     {
        if (d_signal->struct_name[i] == '_' && d_signal->struct_name[i+1])
          d_signal->struct_name[i+1] = toupper(d_signal->struct_name[i+1]);
     }
   for (i = 0; iface->c_name[i]; i++)
     eina_strbuf_append_char(buf, toupper(iface->c_name[i]));
   eina_strbuf_append_char(buf, '_');
   for (i = 0; d_signal->name[i]; i++)
     {
        if (i && isupper(d_signal->name[i]) && !isupper(d_signal->name[i-1]))
          eina_strbuf_append_char(buf, '_');
        eina_strbuf_append_char(buf, toupper(d_signal->name[i]));
     }
   eina_strbuf_append(buf, "_EVENT");
   d_signal->signal_event = eina_strbuf_string_steal(buf);
   eina_strbuf_free(buf);

   if (is_open_empty)
     signal_close();

   return r;
}

#define ANNOTATION_NO_REPLY "org.freedesktop.DBus.Method.NoReply"

static Eina_Bool
annotation_attributes_parser(void *data, const char *key, const char *value)
{
   DBus_Annotation *annotation = data;
   if (!strcmp(key, NAME_ATTR))
     {
        if (!strcmp(value, ANNOTATION_NO_REPLY))
          annotation->type = NO_REPLY;
     }
   else if (!strcmp(key, VALUE_ATTR))
     {
        unsigned i;
        annotation->value = strdup(value);
        for (i = 0; annotation->value[i]; i++)
          annotation->value[i] = tolower(annotation->value[i]);
     }

   return EINA_TRUE;
}

static Eina_Bool
open_annotation(const char *content, unsigned length)
{
   DBus_Annotation annotation;
   Eina_Bool r;

   annotation.type = INVALID;
   r = attributes_parse(content, length, annotation_attributes_parser, &annotation);

   if (annotation.type == NO_REPLY)
     {
        Eina_Bool value = EINA_FALSE;
        if (!strcmp(annotation.value, "true"))
          value = EINA_TRUE;
        free(annotation.value);

        if (method)
          method->no_reply = value;
     }

   return r;
}

static Eina_Bool
open_arg(const char *content, unsigned length)
{
   Eina_Bool r;
   unsigned int *without_name;
   DBus_Arg *arg = calloc(1, sizeof(DBus_Arg));
   EINA_SAFETY_ON_NULL_RETURN_VAL(arg, EINA_FALSE);

   r = attributes_parse(content, length, arg_attributes_parser, arg);
   if (d_signal)
     {
        d_signal->args = eina_inlist_append(d_signal->args, EINA_INLIST_GET(arg));
        without_name = &d_signal->arg_without_name;
     }
   else if (method)
     {
        method->args = eina_inlist_append(method->args, EINA_INLIST_GET(arg));
        without_name = &method->arg_without_name;
     }
   else
     {
        printf("Error find an argument without any valid parent.\n");
        return EINA_FALSE;
     }

   if (!arg->name)
     {
        arg->c_name = string_build("arg%d", *without_name);
        (*without_name)++;
     }
   else
     arg->c_name = dbus_name_to_c(arg->name);

   return r;
}

static void
method_close(void)
{
   DBus_Arg *arg;
   EINA_INLIST_FOREACH(method->args, arg)
     {
        if ((arg->type[1]) || (arg->type[0] == 'v'))
          {
             if (arg->direction == 'o')
               method->out_complex = EINA_TRUE;
             else
               method->in_complex = EINA_TRUE;
          }
     }
   if (method->no_reply)
     {
        free(method->cb_name);
        method->cb_name = strdup("NULL");
     }
   method = NULL;
}

static Eina_Bool
open_method(const char *content, unsigned lenght, Eina_Bool is_open_empty)
{
   Eina_Bool r;
   char *tmp;
   int i;

   method = method_new(iface);
   EINA_SAFETY_ON_NULL_RETURN_VAL(method, EINA_FALSE);

   r = attributes_parse(content, lenght, method_attributes_parser, NULL);
   if (!method->name)
     {
        method_free(method);
        method = NULL;
        printf("Error method without name.\n");
        return EINA_FALSE;
     }

   tmp = dbus_name_to_c(method->name);
   method->c_name = string_build("%s_%s", iface->c_name, tmp);
   free(tmp);
   method->cb_name = string_build("cb_%s", method->c_name);
   method->function_cb = string_build("%s_Cb", method->c_name);
   method->function_cb[0] = toupper(method->function_cb[0]);
   for (i = 0; method->function_cb[i]; i++)
     {
        if (method->function_cb[i] == '_' && method->function_cb[i+1])
           method->function_cb[i+1] = toupper(method->function_cb[i+1]);
     }

   if (is_open_empty)
     method_close();

   return r;
}

static Eina_Bool
open_property(const char *content, unsigned length)
{
   Eina_Bool r;
   char *tmp;

   property = property_new(iface);
   EINA_SAFETY_ON_NULL_RETURN_VAL(property, EINA_FALSE);

   r = attributes_parse(content, length, property_attributes_parser, NULL);
   if (!property->name)
     {
        property_free(property);
        property = NULL;
        printf("Error property without name.\n");
        return EINA_FALSE;
     }

   tmp = dbus_name_to_c(property->name);
   property->c_name = string_build("%s_%s", iface->c_name, tmp);
   free(tmp);
   property->cb_name = string_build("cb_%s", property->c_name);

   return r;
}

static Eina_Bool
open_tag(const char *content, unsigned length, Eina_Bool is_open_empty, DBus_Object **obj)
{
   unsigned int i;
   if (!strncmp(content, OBJECT_TAG, OBJECT_TAG_LENGTH))
     return open_object(content, length, is_open_empty, obj);
   else if (!strncmp(content, INTERFACE_TAG, INTERFACE_TAG_LENGTH) && *obj)
     return open_interface(content, length, is_open_empty, *obj);
   else if (!strncmp(content, SIGNAL_TAG, SIGNAL_TAG_LENGTH) && iface)
     return open_signal(content, length, is_open_empty);
   else if (!strncmp(content, ARG_TAG, ARG_TAG_LENGTH) && iface)
     return open_arg(content, length);
   else if (!strncmp(content, ANNOTATION_TAG, ANNOTATION_TAG_LENGTH) && iface)
     return open_annotation(content, length);
   else if (!strncmp(content, METHOD_TAG, METHOD_TAG_LENGTH) && iface)
     return open_method(content, length, is_open_empty);
   else if (!strncmp(content, PROPERTY_TAG, PROPERTY_TAG_LENGTH) && iface)
     return open_property(content, length);
   else if (!strncmp(content, ANNOTATION_TAG, ANNOTATION_TAG_LENGTH) && iface)
     return EINA_TRUE;

   printf("Warning: Tag not handled:\n");
   for (i = 0; i < length; i++)
     printf("%c", content[i]);
   printf("\n\n");

   return EINA_TRUE;
}

static Eina_Bool
close_tag(const char *content)
{
   if (!strncmp(content, INTERFACE_TAG, INTERFACE_TAG_LENGTH))
     interface_close();
   if (!strncmp(content, SIGNAL_TAG, SIGNAL_TAG_LENGTH))
     signal_close();
   else if (!strncmp(content, METHOD_TAG, METHOD_TAG_LENGTH))
     method_close();
   else if (!strncmp(content, PROPERTY_TAG, PROPERTY_TAG_LENGTH))
     property = NULL;

   return EINA_TRUE;
}

Eina_Bool
parser(void *data, Eina_Simple_XML_Type type, const char *content, unsigned offset EINA_UNUSED, unsigned length)
{
   Eina_Bool r = EINA_TRUE;
   DBus_Object **obj = data;

   switch (type)
     {
      case EINA_SIMPLE_XML_OPEN:
      case EINA_SIMPLE_XML_OPEN_EMPTY:
        {
           r = open_tag(content, length, type == EINA_SIMPLE_XML_OPEN_EMPTY,
                        obj);
           break;
        }
      case EINA_SIMPLE_XML_CLOSE:
        {
           r = close_tag(content);
           break;
        }
      default:
        break;
     }
   return r;
}

static Eina_Bool
attributes_parse(const char *content, unsigned length, Eina_Simple_XML_Attribute_Cb func, const void *data)
{
   const char *attrs = eina_simple_xml_tag_attributes_find(content, length);
   unsigned attrslen = 0;
   if (attrs)
     {
        attrslen = length - (attrs - content);
        if (!eina_simple_xml_attributes_parse(attrs, attrslen, func, data))
          {
             printf("Parser error - attrs=%s | content=%s\n", attrs, content);
             return EINA_FALSE;
          }
     }
   return EINA_TRUE;
}