xml_parse.c 28.1 KB
Newer Older
Cedric Roux's avatar
 
Cedric Roux committed
1
2
3
#include <string.h>
#include <unistd.h>

4
5
#include <gtk/gtk.h>

Cedric Roux's avatar
 
Cedric Roux committed
6
7
8
9
10
11
12
13
#include <libxml/parser.h>
#include <libxml/tree.h>

#include "types.h"
#include "xml_parse.h"
#include "union_type.h"

#include "ui_interface.h"
14
#include "ui_main_screen.h"
Cedric Roux's avatar
Cedric Roux committed
15
#include "ui_notif_dlg.h"
winckel's avatar
winckel committed
16
#include "ui_filters.h"
Cedric Roux's avatar
 
Cedric Roux committed
17
18
19
20
21
22
23
24
25
26
27
28

#include "../libresolver/locate_root.h"
#include "../libresolver/resolvers.h"

extern int debug_parser;

#if (ENABLE_DISPLAY_PARSE_INFO != 0)
# define INDENT_START 30
#else
# define INDENT_START 0
#endif

Cedric Roux's avatar
Cedric Roux committed
29
30
31
32
#define PARSER_DEBUG(fmt, args...)       \
do {                                     \
    if (debug_parser)                    \
        g_debug("WARNING: "fmt, ##args); \
Cedric Roux's avatar
 
Cedric Roux committed
33
34
35
36
} while(0)

#define PARSER_ERROR(fmt, args...)      \
do {                                    \
Cedric Roux's avatar
Cedric Roux committed
37
    g_error("FATAL: "fmt, ##args);      \
Cedric Roux's avatar
 
Cedric Roux committed
38
39
40
41
42
43
44
45
46
47
} while(0)

types_t *root = NULL;

static int xml_parse_doc(xmlDocPtr doc);

static int parse_attribute_name(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "name")) == NULL) {
        if (mandatory) {
48
            PARSER_ERROR("cannot retrieve name attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
49
50
51
52
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            return -1;
        }
        else {
53
            PARSER_DEBUG("cannot retrieve name attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
54
55
56
57
58
59
60
61
62
63
64
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            return 0;
        }
    }
    type->name = strdup ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_id(xmlNode *node, types_t *type) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "id")) == NULL) {
65
        PARSER_ERROR("cannot retrieve id attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
66
67
68
69
70
71
72
73
74
75
76
                     (char *)node->name, node->doc->URL, XML_GET_LINE(node));
        return -1;
    }
    type->id = atoi ((char *) &node_attribute->children->content[1]);
    return 0;
}

static int parse_attribute_size(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "size")) == NULL) {
        if (mandatory) {
77
            PARSER_ERROR("cannot retrieve size attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
78
79
80
81
82
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->size = -1;
            return -1;
        }
        else {
83
            PARSER_DEBUG("cannot retrieve size attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
84
85
86
87
88
89
90
91
92
93
94
95
96
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->size = -1;
            return 0;
        }
    }
    type->size = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_align(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "align")) == NULL) {
        if (mandatory) {
97
            PARSER_ERROR("cannot retrieve align attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
98
99
100
101
102
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->align = -1;
            return -1;
        }
        else {
103
            PARSER_DEBUG("cannot retrieve align attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
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
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->align = -1;
            return 0;
        }
    }
    type->align = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_line(xmlNode *node, types_t *type) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "line")) == NULL) {
        type->line = -1;
        return 0;
    }
    type->line = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_file(xmlNode *node, types_t *type) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "file")) == NULL) {
        type->file = NULL;
        return 0;
    }
    type->file = strdup ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_context(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "context")) == NULL) {
        if (mandatory) {
137
            PARSER_ERROR("cannot retrieve context attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->context = -1;
            return -1;
        }
        else {
            type->context = -1;
            return 0;
        }
    }
    type->context = atoi ((char *) &node_attribute->children->content[1]);
    return 0;
}

static int parse_attribute_artificial(xmlNode *node, types_t *type) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "artificial")) == NULL) {
154
        PARSER_DEBUG("cannot retrieve artificial attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
155
156
157
158
159
160
161
162
163
164
165
                     (char *)node->name, node->doc->URL, XML_GET_LINE(node));
        type->artificial = -1;
        return 0;
    }
    type->artificial = atoi ((char *) &node_attribute->children->content[1]);
    return 0;
}

static int parse_attribute_init(xmlNode *node, types_t *type) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "init")) == NULL) {
166
        PARSER_ERROR("cannot retrieve init attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
167
168
169
170
171
172
173
174
175
176
177
178
                     (char *)node->name, node->doc->URL, XML_GET_LINE(node));
        type->init_value = -1;
        return 0;
    }
    type->init_value = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_members(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "members")) == NULL) {
        if (mandatory) {
179
            PARSER_ERROR("cannot retrieve members attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
180
181
182
183
184
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->members = 0;
            return -1;
        }
        else {
185
            PARSER_DEBUG("cannot retrieve members attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
186
187
188
189
190
191
192
193
194
195
196
197
198
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->members = 0;
            return 0;
        }
    }
    type->members = strdup ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_mangled(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "mangled")) == NULL) {
        if (mandatory) {
199
            PARSER_ERROR("cannot retrieve mangled attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
200
201
202
203
204
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->mangled = 0;
            return -1;
        }
        else {
205
            PARSER_DEBUG("cannot retrieve mangled attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
206
207
208
209
210
211
212
213
214
215
216
217
218
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->mangled = 0;
            return 0;
        }
    }
    type->mangled = strdup ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_demangled(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "mangled")) == NULL) {
        if (mandatory) {
219
            PARSER_ERROR("cannot retrieve demangled attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
220
221
222
223
224
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->demangled = 0;
            return -1;
        }
        else {
225
            PARSER_DEBUG("cannot retrieve demangled attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
226
227
228
229
230
231
232
233
234
235
236
237
238
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->demangled = 0;
            return 0;
        }
    }
    type->demangled = strdup ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_offset(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "offset")) == NULL) {
        if (mandatory) {
239
            PARSER_ERROR("cannot retrieve offset attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
240
241
242
243
244
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->offset = -1;
            return -1;
        }
        else {
245
            PARSER_DEBUG("cannot retrieve offset attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
246
247
248
249
250
251
252
253
254
255
256
257
258
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->offset = -1;
            return 0;
        }
    }
    type->offset = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_bits(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "bits")) == NULL) {
        if (mandatory) {
259
            PARSER_ERROR("cannot retrieve bits attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
260
261
262
263
264
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->bits = -1;
            return -1;
        }
        else {
265
            PARSER_DEBUG("cannot retrieve bits attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
266
267
268
269
270
271
272
273
274
275
276
277
278
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->bits = -1;
            return 0;
        }
    }
    type->bits = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_type(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "type")) == NULL) {
        if (mandatory) {
279
            PARSER_ERROR("cannot retrieve type attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
280
281
282
283
284
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->type_xml = 0;
            return -1;
        }
        else {
285
            PARSER_DEBUG("cannot retrieve type attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
286
287
288
289
290
291
292
293
294
295
296
297
298
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->type_xml = 0;
            return 0;
        }
    }
    type->type_xml = atoi ((char *) &node_attribute->children->content[1]);
    return 0;
}

static int parse_attribute_min(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "min")) == NULL) {
        if (mandatory) {
299
            PARSER_ERROR("cannot retrieve min attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
300
301
302
303
304
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->min = 0;
            return -1;
        }
        else {
305
            PARSER_DEBUG("cannot retrieve min attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
306
307
308
309
310
311
312
313
314
315
316
317
318
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->min = 0;
            return 0;
        }
    }
    type->min = atoi ((char *) node_attribute->children->content);
    return 0;
}

static int parse_attribute_max(xmlNode *node, types_t *type, int mandatory) {
    xmlAttrPtr node_attribute;
    if ((node_attribute = xmlHasProp (node, (xmlChar *) "max")) == NULL) {
        if (mandatory) {
319
            PARSER_ERROR("cannot retrieve max attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
320
321
322
323
324
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->max = 0;
            return -1;
        }
        else {
325
            PARSER_DEBUG("cannot retrieve max attribute in node %s, %s:%ld",
Cedric Roux's avatar
 
Cedric Roux committed
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
                         (char *)node->name, node->doc->URL, XML_GET_LINE(node));
            type->max = 0;
            return 0;
        }
    }
    type->max = atoi ((char *) &node_attribute->children->content[1]);
    return 0;
}

static int parse_enum_values(xmlNode *node, types_t *parent) {
    types_t *new;

    if (node == NULL || parent == NULL)
        return -1;

    new = type_new (TYPE_ENUMERATION_VALUE);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_init(node, new));

    CHECK_FCT(types_insert_tail(&parent->child, new));

    return 0;
}

static int parse_enumeration(xmlNode *node, types_t **head) {
    types_t *new;
    xmlNode *enum_value_node;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_ENUMERATION);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_size(node, new, 1));
    CHECK_FCT(parse_attribute_align(node, new, 1));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_line(node, new));
    CHECK_FCT(parse_attribute_file(node, new));
    CHECK_FCT(parse_attribute_artificial(node, new));

    CHECK_FCT(types_insert_tail(head, new));

    /* Parse enum values */
    for (enum_value_node = node->children; enum_value_node; enum_value_node = enum_value_node->next) {
        if (strcmp ((char *) enum_value_node->name, "EnumValue") == 0)
            CHECK_FCT(parse_enum_values(enum_value_node, new));
    }

    return 0;
}

static int parse_union(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_UNION);

    CHECK_FCT(parse_attribute_name(node, new, 0));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_size(node, new, 1));
    CHECK_FCT(parse_attribute_align(node, new, 1));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_members(node, new, 0));
    CHECK_FCT(parse_attribute_line(node, new));
    CHECK_FCT(parse_attribute_file(node, new));
    CHECK_FCT(parse_attribute_artificial(node, new));
    CHECK_FCT(parse_attribute_mangled(node, new, 0));
    CHECK_FCT(parse_attribute_demangled(node, new, 0));

    if (new->name)
        if (strcmp (new->name, "msg_s") == 0)
            new->type_dissect_from_buffer = union_msg_dissect_from_buffer;

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_fundamental(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_FUNDAMENTAL);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_size(node, new, 0));
    CHECK_FCT(parse_attribute_align(node, new, 0));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_struct(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_STRUCT);

    CHECK_FCT(parse_attribute_name(node, new, 0));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_size(node, new, 0));
    CHECK_FCT(parse_attribute_align(node, new, 0));
    CHECK_FCT(parse_attribute_file(node, new));
    CHECK_FCT(parse_attribute_line(node, new));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_artificial(node, new));
    CHECK_FCT(parse_attribute_members(node, new, 0));
    CHECK_FCT(parse_attribute_mangled(node, new, 0));
    CHECK_FCT(parse_attribute_demangled(node, new, 0));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_typedef(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_TYPEDEF);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_file(node, new));
    CHECK_FCT(parse_attribute_line(node, new));
    CHECK_FCT(parse_attribute_type(node, new, 1));
    CHECK_FCT(parse_attribute_size(node, new, 0));
    CHECK_FCT(parse_attribute_align(node, new, 0));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_artificial(node, new));
    CHECK_FCT(parse_attribute_members(node, new, 0));
    CHECK_FCT(parse_attribute_mangled(node, new, 0));
    CHECK_FCT(parse_attribute_demangled(node, new, 0));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_field(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_FIELD);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_type(node, new, 1));
    CHECK_FCT(parse_attribute_size(node, new, 0));
    CHECK_FCT(parse_attribute_bits(node, new, 0));
    CHECK_FCT(parse_attribute_offset(node, new, 0));
    CHECK_FCT(parse_attribute_align(node, new, 0));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_artificial(node, new));
    CHECK_FCT(parse_attribute_members(node, new, 0));
    CHECK_FCT(parse_attribute_mangled(node, new, 0));
    CHECK_FCT(parse_attribute_demangled(node, new, 0));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_file(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_FILE);

    CHECK_FCT(parse_attribute_name(node, new, 1));
    CHECK_FCT(parse_attribute_id(node, new));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_reference_type(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_REFERENCE);

    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_type(node, new, 1));
    CHECK_FCT(parse_attribute_size(node, new, 1));
    CHECK_FCT(parse_attribute_align(node, new, 1));
    CHECK_FCT(parse_attribute_context(node, new, 0));
    CHECK_FCT(parse_attribute_offset(node, new, 0));
    CHECK_FCT(parse_attribute_file(node, new));
    CHECK_FCT(parse_attribute_line(node, new));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_array_type(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_ARRAY);

    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_type(node, new, 1));
    CHECK_FCT(parse_attribute_size(node, new, 0));
    CHECK_FCT(parse_attribute_align(node, new, 1));
    CHECK_FCT(parse_attribute_min(node, new, 1));
    CHECK_FCT(parse_attribute_max(node, new, 1));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

static int parse_pointer_type(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_POINTER);

    CHECK_FCT(parse_attribute_id(node, new));
    CHECK_FCT(parse_attribute_type(node, new, 1));
    CHECK_FCT(parse_attribute_size(node, new, 1));
    CHECK_FCT(parse_attribute_align(node, new, 1));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
static int parse_function_type(xmlNode *node, types_t **head) {
    types_t *new;

    if (node == NULL)
        return -1;

    new = type_new (TYPE_FUNCTION);

    CHECK_FCT(parse_attribute_id(node, new));

    CHECK_FCT(types_insert_tail(head, new));

    return 0;
}

Cedric Roux's avatar
 
Cedric Roux committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/**
 * print_element_names:
 * @a_node: the initial xml node to consider.
 *
 * Prints the names of the all the xml elements
 * that are siblings or children of a given xml node.
 */
static int parse_elements(xmlNode * a_node, types_t **head) {
    xmlNode *cur_node = NULL;
    xmlNode *child_node = NULL;

    for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
        for (child_node = cur_node->children; child_node; child_node = child_node->next) {
            if (child_node->type == XML_ELEMENT_NODE) {
                if (strcmp ((char *) child_node->name, "Enumeration") == 0) {
Cedric Roux's avatar
Cedric Roux committed
610
                    CHECK_FCT_DO(parse_enumeration(child_node, head), return RC_FAIL);
Cedric Roux's avatar
 
Cedric Roux committed
611
612
613
                }
                else
                    if (strcmp ((char *) child_node->name, "FundamentalType") == 0) {
Cedric Roux's avatar
Cedric Roux committed
614
                        CHECK_FCT_DO(parse_fundamental(child_node, head), return RC_FAIL);
Cedric Roux's avatar
 
Cedric Roux committed
615
616
617
                    }
                    else
                        if (strcmp ((char *) child_node->name, "Struct") == 0) {
Cedric Roux's avatar
Cedric Roux committed
618
                            CHECK_FCT_DO(parse_struct(child_node, head), return RC_FAIL);
Cedric Roux's avatar
 
Cedric Roux committed
619
620
621
                        }
                        else
                            if (strcmp ((char *) child_node->name, "Union") == 0) {
Cedric Roux's avatar
Cedric Roux committed
622
                                CHECK_FCT_DO(parse_union(child_node, head), return RC_FAIL);
Cedric Roux's avatar
 
Cedric Roux committed
623
624
625
                            }
                            else
                                if (strcmp ((char *) child_node->name, "Typedef") == 0) {
Cedric Roux's avatar
Cedric Roux committed
626
                                    CHECK_FCT_DO(parse_typedef(child_node, head), return RC_FAIL);
Cedric Roux's avatar
 
Cedric Roux committed
627
628
629
                                }
                                else
                                    if (strcmp ((char *) child_node->name, "File") == 0) {
Cedric Roux's avatar
Cedric Roux committed
630
                                        CHECK_FCT_DO(parse_file(child_node, head), return RC_FAIL);
Cedric Roux's avatar
 
Cedric Roux committed
631
632
633
                                    }
                                    else
                                        if (strcmp ((char *) child_node->name, "Field") == 0) {
Cedric Roux's avatar
Cedric Roux committed
634
                                            CHECK_FCT_DO(parse_field(child_node, head), return RC_FAIL);
Cedric Roux's avatar
 
Cedric Roux committed
635
636
637
                                        }
                                        else
                                            if (strcmp ((char *) child_node->name, "ReferenceType") == 0) {
Cedric Roux's avatar
Cedric Roux committed
638
                                                CHECK_FCT_DO(parse_reference_type(child_node, head), return RC_FAIL);
Cedric Roux's avatar
 
Cedric Roux committed
639
640
641
                                            }
                                            else
                                                if (strcmp ((char *) child_node->name, "ArrayType") == 0) {
642
                                                    CHECK_FCT_DO(parse_array_type(child_node, head), return RC_FAIL);
Cedric Roux's avatar
 
Cedric Roux committed
643
644
645
                                                }
                                                else
                                                    if (strcmp ((char *) child_node->name, "PointerType") == 0) {
646
                                                        CHECK_FCT_DO(parse_pointer_type(child_node, head), return RC_FAIL);
Cedric Roux's avatar
 
Cedric Roux committed
647
                                                    }
648
649
650
651
                                                    else
                                                        if (strcmp ((char *) child_node->name, "FunctionType") == 0) {
                                                            CHECK_FCT_DO(parse_function_type(child_node, head), return RC_FAIL);
                                                        }
Cedric Roux's avatar
 
Cedric Roux committed
652
653
654
655
656
657
658
659
660
661
662
663
664
665
            }
        }
    }

    return RC_OK;
}

int xml_parse_buffer(const char *xml_buffer, const int size) {
    xmlDocPtr doc; /* the resulting document tree */

    if (xml_buffer == NULL) {
        return -1;
    }

666
    g_message("Parsing XML definition from buffer ...");
Cedric Roux's avatar
 
Cedric Roux committed
667
668
669
670

    doc = xmlReadMemory(xml_buffer, size, NULL, NULL, 0);

    if (doc == NULL) {
671
672
        g_warning("Failed to parse XML buffer: %s", xml_buffer);
        ui_notification_dialog(GTK_MESSAGE_ERROR, "parse messages format definition", "Fail to parse XML buffer");
Cedric Roux's avatar
 
Cedric Roux committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
        return RC_FAIL;
    }

    return xml_parse_doc(doc);
}

int xml_parse_file(const char *filename) {
    xmlDocPtr doc; /* the resulting document tree */

    if (filename == NULL) {
        return -1;
    }

    doc = xmlReadFile (filename, NULL, 0);

    if (doc == NULL) {
689
        ui_notification_dialog(GTK_MESSAGE_ERROR, "parse messages format definition", "Failed to parse file \"%s\"", filename);
Cedric Roux's avatar
 
Cedric Roux committed
690
691
692
693
694
695
        return RC_FAIL;
    }

    return xml_parse_doc(doc);
}

winckel's avatar
winckel committed
696
697
698
699
700
701
702
703
704
705
static int update_filters() {
    types_t *types;

    ui_init_filters(FALSE, TRUE);

    types = messages_id_enum;
    if (types != NULL)
    {
        types = types->child;

706
707
708
709
        while (types != NULL)
        {
            if (strcmp (types->name, "MESSAGES_ID_MAX") != 0)
            {
710
                ui_filters_add (FILTER_MESSAGES, types->init_value, types->name, ENTRY_ENABLED_UNDEFINED, NULL, NULL);
711
            }
winckel's avatar
winckel committed
712
713
714
715
716
717
718
719
720
721
            types = types->next;
        }
    }

    types = origin_task_id_type;
    if (types != NULL)
    {
        types = types->child->child;

        while (types != NULL) {
722
723
            if ((strcmp (types->name, "TASK_FIRST") != 0) && (strcmp (types->name, "TASK_MAX") != 0))
            {
724
            	ui_filters_add(FILTER_ORIGIN_TASKS, types->init_value, types->name, ENTRY_ENABLED_UNDEFINED, NULL, NULL);
725
            }
winckel's avatar
winckel committed
726
727
728
729
730
731
732
733
734
735
            types = types->next;
        }
    }

    types = destination_task_id_type;
    if (types != NULL)
    {
        types = types->child->child;

        while (types != NULL) {
736
737
            if ((strcmp (types->name, "TASK_FIRST") != 0) && (strcmp (types->name, "TASK_MAX") != 0))
            {
738
            	ui_filters_add(FILTER_DESTINATION_TASKS, types->init_value, types->name, ENTRY_ENABLED_UNDEFINED, NULL, NULL);
739
            }
winckel's avatar
winckel committed
740
741
742
743
744
745
746
            types = types->next;
        }
    }

    return RC_OK;
}

Cedric Roux's avatar
 
Cedric Roux committed
747
748
749
750
static int xml_parse_doc(xmlDocPtr doc) {
    xmlNode *root_element = NULL;
    types_t *head = NULL;
    int ret = 0;
751
    FILE *dissect_file = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
752

753
754
755
    if (ui_main_data.dissect_file_name != NULL) {
        dissect_file = fopen (ui_main_data.dissect_file_name, "w");
    }
Cedric Roux's avatar
 
Cedric Roux committed
756
757

    /* Get the root element node */
Cedric Roux's avatar
Cedric Roux committed
758
    root_element = xmlDocGetRootElement(doc);
Cedric Roux's avatar
 
Cedric Roux committed
759

Cedric Roux's avatar
Cedric Roux committed
760
    ret = parse_elements(root_element, &head);
Cedric Roux's avatar
 
Cedric Roux committed
761
762

    /* Free the document */
Cedric Roux's avatar
Cedric Roux committed
763
    xmlFreeDoc(doc);
Cedric Roux's avatar
 
Cedric Roux committed
764
765
766
767
768
769
770
771
772
773

    if (ret == RC_OK) {
        resolve_typedefs (&head);
        resolve_pointer_type (&head);
        resolve_field (&head);
        resolve_array (&head);
        resolve_reference (&head);
        resolve_struct (&head);
        resolve_file (&head);
        resolve_union (&head);
774
        resolve_function (&head);
775

Cedric Roux's avatar
Cedric Roux committed
776
        /* Locate the root element which corresponds to the MessageDef struct */
Cedric Roux's avatar
 
Cedric Roux committed
777
        CHECK_FCT(locate_root("MessageDef", head, &root));
778
779
780
781
782
783
784
785
786

        /* Locate the LTE time fields */
        if (locate_type("lte_time", head, &lte_time_type) == RC_OK)
        {
            CHECK_FCT(locate_type("frame", lte_time_type->child->child, &lte_time_frame_type));
            CHECK_FCT(locate_type("slot", lte_time_type->child->child, &lte_time_slot_type));
        }

        /* Locate the message id field */
Cedric Roux's avatar
Cedric Roux committed
787
        CHECK_FCT(locate_type("MessagesIds", head, &messages_id_enum));
Cedric Roux's avatar
Cedric Roux committed
788
789
790

        /* Locate the header part of a message */
        CHECK_FCT(locate_type("ittiMsgHeader", head, &message_header_type));
winckel's avatar
winckel committed
791
792
793
        /* Locate the main message part */
        CHECK_FCT(locate_type("msg", head, &message_type));

794
        /* Locate the origin task id field */
Cedric Roux's avatar
Cedric Roux committed
795
        CHECK_FCT(locate_type("originTaskId", message_header_type, &origin_task_id_type));
796
        /* Locate the destination task id field */
Cedric Roux's avatar
Cedric Roux committed
797
        CHECK_FCT(locate_type("destinationTaskId", message_header_type, &destination_task_id_type));
798
        /* Locate the instance field */
Cedric Roux's avatar
Cedric Roux committed
799
800
801
        CHECK_FCT(locate_type("instance", message_header_type, &instance_type));
        /* Locate the message size field */
        CHECK_FCT(locate_type("ittiMsgSize", message_header_type, &message_size_type));
802

Cedric Roux's avatar
 
Cedric Roux committed
803
        // root->type_hr_display(root, 0);
804

winckel's avatar
winckel committed
805
        update_filters();
Cedric Roux's avatar
 
Cedric Roux committed
806
        if (dissect_file != NULL) {
807
            g_debug("generating dissected types file \"%s\" ...", ui_main_data.dissect_file_name);
Cedric Roux's avatar
 
Cedric Roux committed
808
809
810
811
            root->type_file_print (root, 0, dissect_file);
        }
    }

812
813
814
    if (dissect_file != NULL) {
        fclose (dissect_file);
    }
815
816
817

    g_message("Parsed XML definition");

Cedric Roux's avatar
 
Cedric Roux committed
818
819
820
    return ret;
}

Cedric Roux's avatar
Cedric Roux committed
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
int dissect_signal_header(buffer_t *buffer, ui_set_signal_text_cb_t ui_set_signal_text_cb,
                          gpointer cb_user_data)
{
    if (message_header_type == NULL) {
        g_error("No messages format definition provided");
        return RC_FAIL;
    }

    if (buffer == NULL) {
        g_error("Failed buffer is NULL");
        return RC_FAIL;
    }

    message_header_type->type_dissect_from_buffer(
        message_header_type, ui_set_signal_text_cb, cb_user_data,
winckel's avatar
winckel committed
836
        buffer, 0, 0, INDENT_START, TRUE);
Cedric Roux's avatar
Cedric Roux committed
837
838
839
840

    return RC_OK;
}

Cedric Roux's avatar
Cedric Roux committed
841
int dissect_signal(buffer_t *buffer, ui_set_signal_text_cb_t ui_set_signal_text_cb,
Cedric Roux's avatar
Cedric Roux committed
842
                   gpointer cb_user_data)
Cedric Roux's avatar
Cedric Roux committed
843
{
winckel's avatar
winckel committed
844
    if (message_type == NULL) {
845
        g_error("No messages format definition provided");
Cedric Roux's avatar
 
Cedric Roux committed
846
847
848
849
        return RC_FAIL;
    }

    if (buffer == NULL) {
850
        g_error("Failed buffer is NULL");
Cedric Roux's avatar
 
Cedric Roux committed
851
852
853
        return RC_FAIL;
    }

winckel's avatar
winckel committed
854
855
    message_type->type_dissect_from_buffer(message_type, ui_set_signal_text_cb, cb_user_data,
                                           buffer, 0, 0, INDENT_START, TRUE);
Cedric Roux's avatar
 
Cedric Roux committed
856
857
858

    return RC_OK;
}