3583026d |
1 | /* |
2 | |
3 | parser.c: Generate helper declarations and functions to trace events |
4 | from an event description file. |
5 | |
a3e6ce64 |
6 | Copyright (C) 2005, Mathieu Desnoyers |
7 | Copyright (C) 2002, Xianxiu Yang |
8 | Copyright (C) 2002, Michel Dagenais |
9 | This program is free software; you can redistribute it and/or modify |
3583026d |
10 | it under the terms of the GNU General Public License as published by |
a3e6ce64 |
11 | the Free Software Foundation; version 2 of the License. |
3583026d |
12 | |
a3e6ce64 |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
3583026d |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | */ |
22 | |
23 | /* This program reads the ".xml" event definitions input files |
24 | and constructs structure for each event. |
25 | |
26 | The program uses a very simple tokenizer, called from a hand written |
27 | recursive descent parser to fill a data structure describing the events. |
28 | The result is a sequence of events definitions which refer to type |
29 | definitions. |
30 | |
31 | A table of named types is maintained to allow refering to types by name |
32 | when the same type is used at several places. Finally a sequence of |
33 | all types is maintained to facilitate the freeing of all type |
34 | information when the processing of an ".xml" file is finished. */ |
35 | |
36 | #include <stdlib.h> |
37 | #include <string.h> |
38 | #include <stdio.h> |
39 | #include <stdarg.h> |
40 | #include <linux/errno.h> |
41 | #include <assert.h> |
42 | #include <ctype.h> |
43 | |
44 | #include "parser.h" |
45 | |
46 | |
2d2d14a7 |
47 | char *intOutputTypes[] = { |
48 | "int8_t", "int16_t", "int32_t", "int64_t" }; |
3583026d |
49 | |
2d2d14a7 |
50 | char *uintOutputTypes[] = { |
51 | "uint8_t", "uint16_t", "uint32_t", "uint64_t" }; |
3583026d |
52 | |
2d2d14a7 |
53 | char *floatOutputTypes[] = { |
54 | "undef", "undef", "float", "double" }; |
3583026d |
55 | |
56 | |
57 | |
58 | |
59 | /* helper function */ |
60 | void strupper(char *string) |
61 | { |
62 | char *ptr = string; |
63 | |
64 | while(*ptr != '\0') { |
65 | *ptr = toupper(*ptr); |
66 | ptr++; |
67 | } |
68 | } |
69 | |
70 | |
2d2d14a7 |
71 | int getSizeindex(unsigned int value) |
3583026d |
72 | { |
73 | switch(value) { |
74 | case 1: |
75 | return 0; |
76 | case 2: |
77 | return 1; |
78 | case 4: |
79 | return 2; |
80 | case 8: |
81 | return 3; |
82 | default: |
83 | printf("Error : unknown value size %d\n", value); |
84 | exit(-1); |
85 | } |
86 | } |
87 | |
88 | /***************************************************************************** |
89 | *Function name |
90 | * getSize : translate from string to integer |
91 | *Input params |
92 | * in : input file handle |
93 | *Return values |
94 | * size |
95 | *****************************************************************************/ |
96 | |
2d2d14a7 |
97 | unsigned long long int getSize(parse_file_t *in) |
3583026d |
98 | { |
99 | char *token; |
100 | |
101 | token = getToken(in); |
102 | if(in->type == NUMBER) { |
2d2d14a7 |
103 | return strtoull(token, NULL, 0); |
3583026d |
104 | } |
105 | in->error(in,"incorrect size specification"); |
106 | return -1; |
107 | } |
108 | |
109 | /***************************************************************************** |
110 | *Function name |
111 | * error_callback : print out error info |
112 | *Input params |
113 | * in : input file handle |
114 | * msg : message to be printed |
115 | ****************************************************************************/ |
116 | |
117 | void error_callback(parse_file_t *in, char *msg) |
118 | { |
119 | if(in) |
120 | printf("Error in file %s, line %d: %s\n", in->name, in->lineno, msg); |
121 | else |
122 | printf("%s\n",msg); |
123 | assert(0); |
124 | exit(1); |
125 | } |
126 | |
127 | /***************************************************************************** |
128 | *Function name |
129 | * memAlloc : allocate memory |
130 | *Input params |
131 | * size : required memory size |
132 | *return value |
133 | * void * : pointer to allocate memory or NULL |
134 | ****************************************************************************/ |
135 | |
136 | void * memAlloc(int size) |
137 | { |
138 | void * addr; |
139 | if(size == 0) return NULL; |
140 | addr = malloc(size); |
141 | if(!addr){ |
142 | printf("Failed to allocate memory"); |
143 | exit(1); |
144 | } |
145 | return addr; |
146 | } |
147 | |
148 | /***************************************************************************** |
149 | *Function name |
150 | * allocAndCopy : allocate memory and initialize it |
151 | *Input params |
152 | * str : string to be put in memory |
153 | *return value |
154 | * char * : pointer to allocate memory or NULL |
155 | ****************************************************************************/ |
156 | |
157 | char *allocAndCopy(char *str) |
158 | { |
159 | char * addr; |
160 | if(str == NULL) return NULL; |
161 | addr = (char *)memAlloc(strlen(str)+1); |
162 | strcpy(addr,str); |
163 | return addr; |
164 | } |
165 | |
166 | /************************************************************************** |
167 | * Function : |
168 | * getTypeAttributes |
169 | * Description : |
170 | * Read the attribute from the input file. |
171 | * |
172 | * Parameters : |
173 | * in , input file handle. |
174 | * t , the type descriptor to fill. |
175 | * |
176 | **************************************************************************/ |
177 | |
be97b953 |
178 | void getTypeAttributes(parse_file_t *in, type_descriptor_t *t, |
179 | sequence_t * unnamed_types, table_t * named_types) |
3583026d |
180 | { |
181 | char * token; |
182 | |
183 | t->fmt = NULL; |
2d2d14a7 |
184 | t->size = 0; |
64a6ab10 |
185 | t->custom_write = 0; |
3583026d |
186 | |
187 | while(1) { |
188 | token = getToken(in); |
189 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
190 | ungetToken(in); |
191 | break; |
192 | } |
193 | |
194 | if(!strcmp("format",token)) { |
195 | getEqual(in); |
196 | t->fmt = allocAndCopy(getQuotedString(in)); |
197 | //} else if(!strcmp("name",token)) { |
198 | // getEqual(in); |
199 | // car = seekNextChar(in); |
200 | // if(car == EOF) in->error(in,"name was expected"); |
201 | // else if(car == '\"') t->type_name = allocAndCopy(getQuotedString(in)); |
202 | // else t->type_name = allocAndCopy(getName(in)); |
be97b953 |
203 | } else if(!strcmp("size",token)) { |
3583026d |
204 | getEqual(in); |
205 | t->size = getSize(in); |
64a6ab10 |
206 | } else if(!strcmp("custom_write", token)) { |
207 | t->custom_write = 1; |
3583026d |
208 | } |
209 | } |
210 | } |
211 | |
212 | /************************************************************************** |
213 | * Function : |
214 | * getEventAttributes |
215 | * Description : |
216 | * Read the attribute from the input file. |
217 | * |
218 | * Parameters : |
219 | * in , input file handle. |
220 | * ev , the event to fill. |
221 | * |
222 | **************************************************************************/ |
223 | |
224 | void getEventAttributes(parse_file_t *in, event_t *ev) |
225 | { |
226 | char * token; |
227 | char car; |
228 | |
229 | ev->name = NULL; |
230 | ev->per_trace = 0; |
231 | ev->per_tracefile = 0; |
232 | |
233 | while(1) { |
234 | token = getToken(in); |
235 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
236 | ungetToken(in); |
237 | break; |
238 | } |
239 | |
240 | if(!strcmp("name",token)) { |
241 | getEqual(in); |
242 | car = seekNextChar(in); |
243 | if(car == EOF) in->error(in,"name was expected"); |
244 | else if(car == '\"') ev->name = allocAndCopy(getQuotedString(in)); |
245 | else ev->name = allocAndCopy(getName(in)); |
246 | } else if(!strcmp("per_trace", token)) { |
247 | ev->per_trace = 1; |
248 | } else if(!strcmp("per_tracefile", token)) { |
249 | ev->per_tracefile = 1; |
250 | } |
251 | |
252 | } |
253 | } |
254 | |
255 | /************************************************************************** |
256 | * Function : |
257 | * getFacilityAttributes |
258 | * Description : |
259 | * Read the attribute from the input file. |
260 | * |
261 | * Parameters : |
262 | * in , input file handle. |
263 | * fac , the facility to fill. |
264 | * |
265 | **************************************************************************/ |
266 | |
267 | void getFacilityAttributes(parse_file_t *in, facility_t *fac) |
268 | { |
269 | char * token; |
270 | char car; |
271 | |
272 | fac->name = NULL; |
ffaf5031 |
273 | fac->arch = NULL; |
3583026d |
274 | |
275 | while(1) { |
276 | token = getToken(in); |
277 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
278 | ungetToken(in); |
279 | break; |
280 | } |
281 | |
282 | if(!strcmp("name",token)) { |
283 | getEqual(in); |
284 | car = seekNextChar(in); |
285 | if(car == EOF) in->error(in,"name was expected"); |
286 | else if(car == '\"') fac->name = allocAndCopy(getQuotedString(in)); |
287 | else fac->name = allocAndCopy(getName(in)); |
ffaf5031 |
288 | } else if(!strcmp("arch", token)) { |
289 | getEqual(in); |
290 | car = seekNextChar(in); |
291 | if(car == '\"') fac->name = allocAndCopy(getQuotedString(in)); |
292 | else fac->arch = allocAndCopy(getName(in)); |
293 | } |
3583026d |
294 | } |
295 | } |
296 | |
297 | /************************************************************************** |
298 | * Function : |
299 | * getFieldAttributes |
300 | * Description : |
301 | * Read the attribute from the input file. |
302 | * |
303 | * Parameters : |
304 | * in , input file handle. |
305 | * f , the field to fill. |
306 | * |
307 | **************************************************************************/ |
308 | |
309 | void getFieldAttributes(parse_file_t *in, field_t *f) |
310 | { |
311 | char * token; |
312 | char car; |
313 | |
314 | f->name = NULL; |
315 | |
316 | while(1) { |
317 | token = getToken(in); |
318 | if(strcmp("/",token) == 0 || strcmp(">",token) == 0){ |
319 | ungetToken(in); |
320 | break; |
321 | } |
322 | |
323 | if(!strcmp("name",token)) { |
324 | getEqual(in); |
325 | car = seekNextChar(in); |
326 | if(car == EOF) in->error(in,"name was expected"); |
327 | else if(car == '\"') f->name = allocAndCopy(getQuotedString(in)); |
328 | else f->name = allocAndCopy(getName(in)); |
329 | } |
330 | } |
331 | } |
332 | |
333 | char *getNameAttribute(parse_file_t *in) |
334 | { |
335 | char * token; |
336 | char *name = NULL; |
337 | char car; |
338 | |
339 | while(1) { |
340 | token = getToken(in); |
3583026d |
341 | if(!strcmp("name",token)) { |
342 | getEqual(in); |
343 | car = seekNextChar(in); |
344 | if(car == EOF) in->error(in,"name was expected"); |
345 | else if(car == '\"') name = allocAndCopy(getQuotedString(in)); |
346 | else name = allocAndCopy(getName(in)); |
64a6ab10 |
347 | } else { |
348 | ungetToken(in); |
349 | break; |
3583026d |
350 | } |
64a6ab10 |
351 | |
3583026d |
352 | } |
353 | if(name == NULL) in->error(in, "Name was expected"); |
354 | return name; |
3583026d |
355 | } |
356 | |
357 | |
358 | |
359 | //for <label name=label_name value=n format="..."/>, value is an option |
360 | char * getValueStrAttribute(parse_file_t *in) |
361 | { |
362 | char * token; |
363 | |
364 | token = getToken(in); |
365 | if(strcmp("/",token) == 0){ |
366 | ungetToken(in); |
367 | return NULL; |
368 | } |
369 | |
370 | if(strcmp("value",token))in->error(in,"value was expected"); |
371 | getEqual(in); |
372 | token = getToken(in); |
373 | if(in->type != NUMBER) in->error(in,"number was expected"); |
374 | return token; |
375 | } |
376 | |
377 | char * getDescription(parse_file_t *in) |
378 | { |
379 | long int pos; |
380 | char * token, car, *str; |
381 | |
382 | pos = ftell(in->fp); |
383 | |
384 | getLAnglebracket(in); |
385 | token = getName(in); |
386 | if(strcmp("description",token)){ |
387 | fseek(in->fp, pos, SEEK_SET); |
388 | return NULL; |
389 | } |
390 | |
391 | getRAnglebracket(in); |
392 | |
393 | pos = 0; |
394 | while((car = getc(in->fp)) != EOF) { |
395 | if(car == '<') break; |
396 | if(car == '\0') continue; |
397 | in->buffer[pos] = car; |
398 | pos++; |
399 | } |
400 | if(car == EOF)in->error(in,"not a valid description"); |
401 | in->buffer[pos] = '\0'; |
402 | |
403 | str = allocAndCopy(in->buffer); |
404 | |
405 | getForwardslash(in); |
406 | token = getName(in); |
407 | if(strcmp("description", token))in->error(in,"not a valid description"); |
408 | getRAnglebracket(in); |
409 | |
410 | return str; |
411 | } |
412 | |
413 | /***************************************************************************** |
414 | *Function name |
415 | * parseFacility : generate event list |
416 | *Input params |
417 | * in : input file handle |
418 | * fac : empty facility |
419 | *Output params |
420 | * fac : facility filled with event list |
421 | ****************************************************************************/ |
422 | |
423 | void parseFacility(parse_file_t *in, facility_t * fac) |
424 | { |
425 | char * token; |
426 | event_t *ev; |
427 | |
428 | getFacilityAttributes(in, fac); |
429 | if(fac->name == NULL) in->error(in, "Attribute not named"); |
ffaf5031 |
430 | |
3583026d |
431 | fac->capname = allocAndCopy(fac->name); |
432 | strupper(fac->capname); |
433 | getRAnglebracket(in); |
434 | |
435 | fac->description = getDescription(in); |
436 | |
437 | while(1){ |
438 | getLAnglebracket(in); |
439 | |
440 | token = getToken(in); |
441 | if(in->type == ENDFILE) |
442 | in->error(in,"the definition of the facility is not finished"); |
443 | |
444 | if(strcmp("event",token) == 0){ |
445 | ev = (event_t*) memAlloc(sizeof(event_t)); |
446 | sequence_push(&(fac->events),ev); |
447 | parseEvent(in,ev, &(fac->unnamed_types), &(fac->named_types)); |
448 | }else if(strcmp("type",token) == 0){ |
449 | parseTypeDefinition(in, &(fac->unnamed_types), &(fac->named_types)); |
450 | }else if(in->type == FORWARDSLASH){ |
451 | break; |
452 | }else in->error(in,"event or type token expected\n"); |
453 | } |
454 | |
455 | token = getName(in); |
456 | if(strcmp("facility",token)) in->error(in,"not the end of the facility"); |
457 | getRAnglebracket(in); //</facility> |
458 | } |
459 | |
460 | /***************************************************************************** |
461 | *Function name |
462 | * parseEvent : generate event from event definition |
463 | *Input params |
464 | * in : input file handle |
465 | * ev : new event |
466 | * unnamed_types : array of unamed types |
467 | * named_types : array of named types |
468 | *Output params |
469 | * ev : new event (parameters are passed to it) |
470 | ****************************************************************************/ |
471 | |
472 | void parseEvent(parse_file_t *in, event_t * ev, sequence_t * unnamed_types, |
473 | table_t * named_types) |
474 | { |
475 | char *token; |
47299663 |
476 | field_t *f; |
3583026d |
477 | |
47299663 |
478 | sequence_init(&(ev->fields)); |
3583026d |
479 | //<event name=eventtype_name> |
480 | getEventAttributes(in, ev); |
481 | if(ev->name == NULL) in->error(in, "Event not named"); |
482 | getRAnglebracket(in); |
483 | |
47299663 |
484 | //<description>...</description> |
3583026d |
485 | ev->description = getDescription(in); |
486 | |
47299663 |
487 | int got_end = 0; |
488 | /* Events can have multiple fields. each field form at least a function |
489 | * parameter of the logging function. */ |
490 | while(!got_end) { |
491 | getLAnglebracket(in); |
492 | token = getToken(in); |
493 | |
494 | switch(in->type) { |
495 | case FORWARDSLASH: /* </event> */ |
496 | token = getName(in); |
497 | if(strcmp("event",token))in->error(in,"not an event definition"); |
498 | getRAnglebracket(in); //</event> |
499 | got_end = 1; |
500 | break; |
501 | case NAME: /* a field */ |
502 | if(strcmp("field",token))in->error(in,"expecting a field"); |
503 | f = (field_t *)memAlloc(sizeof(field_t)); |
504 | sequence_push(&(ev->fields),f); |
a67cd958 |
505 | parseFields(in, f, unnamed_types, named_types, 1); |
47299663 |
506 | break; |
507 | default: |
508 | in->error(in, "expecting </event> or <field >"); |
509 | break; |
510 | } |
511 | } |
512 | #if 0 |
513 | if(in->type == FORWARDSLASH){ //</event> NOTHING |
514 | ev->type = NULL; |
515 | }else if(in->type == NAME){ |
516 | if(strcmp("struct",token)==0 || strcmp("typeref",token)==0){ |
517 | ungetToken(in); |
518 | ev->type = parseType(in,NULL, unnamed_types, named_types); |
519 | if(ev->type->type != STRUCT && ev->type->type != NONE) |
520 | in->error(in,"type must be a struct"); |
521 | }else in->error(in, "not a valid type"); |
522 | |
523 | getLAnglebracket(in); |
524 | getForwardslash(in); |
525 | }else in->error(in,"not a struct type"); |
526 | getLAnglebracket(in); |
527 | getForwardslash(in); |
528 | token = getName(in); |
529 | if(strcmp("event",token))in->error(in,"not an event definition"); |
530 | getRAnglebracket(in); //</event> |
531 | #endif //0 |
3583026d |
532 | } |
533 | |
534 | /***************************************************************************** |
535 | *Function name |
536 | * parseField : get field infomation from buffer |
537 | *Input params |
538 | * in : input file handle |
47299663 |
539 | * f : field |
3583026d |
540 | * unnamed_types : array of unamed types |
541 | * named_types : array of named types |
a67cd958 |
542 | * tag : is field surrounded by a <field> </field> tag ? |
3583026d |
543 | ****************************************************************************/ |
544 | |
47299663 |
545 | void parseFields(parse_file_t *in, field_t *f, |
3583026d |
546 | sequence_t * unnamed_types, |
a67cd958 |
547 | table_t * named_types, |
548 | int tag) |
3583026d |
549 | { |
550 | char * token; |
a67cd958 |
551 | if(tag) { |
552 | //<field name=field_name> <description> <type> </field> |
553 | getFieldAttributes(in, f); |
554 | if(f->name == NULL) in->error(in, "Field not named"); |
555 | getRAnglebracket(in); |
3583026d |
556 | |
a67cd958 |
557 | f->description = getDescription(in); |
558 | } |
3583026d |
559 | |
560 | //<int size=...> |
561 | getLAnglebracket(in); |
562 | f->type = parseType(in,NULL, unnamed_types, named_types); |
563 | |
a67cd958 |
564 | if(tag) { |
565 | getLAnglebracket(in); |
566 | getForwardslash(in); |
567 | token = getName(in); |
568 | if(strcmp("field",token))in->error(in,"not a valid field definition"); |
569 | getRAnglebracket(in); //</field> |
570 | } |
3583026d |
571 | } |
572 | |
573 | |
574 | /***************************************************************************** |
575 | *Function name |
576 | * parseType : get type information, type can be : |
577 | * Primitive: |
578 | * int(size,fmt); uint(size,fmt); float(size,fmt); |
579 | * string(fmt); enum(size,fmt,(label1,label2...)) |
580 | * Compound: |
581 | * array(arraySize, type); sequence(lengthSize,type) |
582 | * struct(field(name,type,description)...) |
583 | * type name: |
584 | * type(name,type) |
585 | *Input params |
586 | * in : input file handle |
587 | * inType : a type descriptor |
588 | * unnamed_types : array of unamed types |
589 | * named_types : array of named types |
590 | *Return values |
591 | * type_descriptor* : a type descriptor |
592 | ****************************************************************************/ |
593 | |
594 | type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType, |
595 | sequence_t * unnamed_types, table_t * named_types) |
596 | { |
597 | char *token; |
598 | type_descriptor_t *t; |
47299663 |
599 | field_t *f; |
3583026d |
600 | |
601 | if(inType == NULL) { |
602 | t = (type_descriptor_t *) memAlloc(sizeof(type_descriptor_t)); |
603 | t->type_name = NULL; |
604 | t->type = NONE; |
605 | t->fmt = NULL; |
606 | sequence_push(unnamed_types,t); |
607 | } |
608 | else t = inType; |
609 | |
610 | token = getName(in); |
611 | |
612 | if(strcmp(token,"struct") == 0) { |
613 | t->type = STRUCT; |
be97b953 |
614 | getTypeAttributes(in, t, unnamed_types, named_types); |
3583026d |
615 | getRAnglebracket(in); //<struct> |
616 | getLAnglebracket(in); //<field name=..> |
617 | token = getToken(in); |
618 | sequence_init(&(t->fields)); |
619 | while(strcmp("field",token) == 0){ |
47299663 |
620 | f = (field_t *)memAlloc(sizeof(field_t)); |
621 | sequence_push(&(t->fields),f); |
622 | |
a67cd958 |
623 | parseFields(in, f, unnamed_types, named_types, 1); |
3583026d |
624 | |
625 | //next field |
626 | getLAnglebracket(in); |
627 | token = getToken(in); |
628 | } |
629 | if(strcmp("/",token))in->error(in,"not a valid structure definition"); |
630 | token = getName(in); |
631 | if(strcmp("struct",token)!=0) |
632 | in->error(in,"not a valid structure definition"); |
633 | getRAnglebracket(in); //</struct> |
634 | } |
635 | else if(strcmp(token,"union") == 0) { |
636 | t->type = UNION; |
be97b953 |
637 | getTypeAttributes(in, t, unnamed_types, named_types); |
2d2d14a7 |
638 | getRAnglebracket(in); //<union> |
3583026d |
639 | |
640 | getLAnglebracket(in); //<field name=..> |
641 | token = getToken(in); |
642 | sequence_init(&(t->fields)); |
643 | while(strcmp("field",token) == 0){ |
47299663 |
644 | f = (field_t *)memAlloc(sizeof(field_t)); |
645 | sequence_push(&(t->fields),f); |
a67cd958 |
646 | parseFields(in, f, unnamed_types, named_types, 1); |
3583026d |
647 | |
648 | //next field |
649 | getLAnglebracket(in); |
650 | token = getToken(in); |
651 | } |
652 | if(strcmp("/",token))in->error(in,"not a valid union definition"); |
653 | token = getName(in); |
654 | if(strcmp("union",token)!=0) |
655 | in->error(in,"not a valid union definition"); |
656 | getRAnglebracket(in); //</union> |
657 | } |
658 | else if(strcmp(token,"array") == 0) { |
659 | t->type = ARRAY; |
a67cd958 |
660 | sequence_init(&(t->fields)); |
be97b953 |
661 | getTypeAttributes(in, t, unnamed_types, named_types); |
2d2d14a7 |
662 | if(t->size == 0) in->error(in, "Array has empty size"); |
663 | getForwardslash(in); |
3583026d |
664 | getRAnglebracket(in); //<array size=n> |
665 | |
2e415130 |
666 | //getLAnglebracket(in); //<subtype> |
a67cd958 |
667 | /* subfield */ |
668 | f = (field_t *)memAlloc(sizeof(field_t)); |
669 | sequence_push(&(t->fields),f); |
670 | parseFields(in, f, unnamed_types, named_types, 0); |
671 | |
672 | //getLAnglebracket(in); //<type struct> |
673 | //t->nested_type = parseType(in, NULL, unnamed_types, named_types); |
3583026d |
674 | |
675 | getLAnglebracket(in); //</array> |
676 | getForwardslash(in); |
677 | token = getName(in); |
678 | if(strcmp("array",token))in->error(in,"not a valid array definition"); |
679 | getRAnglebracket(in); //</array> |
680 | } |
681 | else if(strcmp(token,"sequence") == 0) { |
682 | t->type = SEQUENCE; |
a67cd958 |
683 | sequence_init(&(t->fields)); |
64a6ab10 |
684 | getTypeAttributes(in, t, unnamed_types, named_types); |
685 | getForwardslash(in); |
be97b953 |
686 | getRAnglebracket(in); //<sequence> |
3583026d |
687 | |
2e415130 |
688 | //getLAnglebracket(in); //<sequence size type> |
a67cd958 |
689 | /* subfield */ |
690 | f = (field_t *)memAlloc(sizeof(field_t)); |
691 | sequence_push(&(t->fields),f); |
692 | parseFields(in, f, unnamed_types, named_types, 0); |
693 | |
2e415130 |
694 | //getLAnglebracket(in); //<subtype> |
a67cd958 |
695 | /* subfield */ |
696 | f = (field_t *)memAlloc(sizeof(field_t)); |
697 | sequence_push(&(t->fields),f); |
698 | parseFields(in, f, unnamed_types, named_types, 0); |
3583026d |
699 | |
a67cd958 |
700 | //getLAnglebracket(in); //<type sequence> |
701 | //t->length_type = parseType(in, NULL, unnamed_types, named_types); |
be97b953 |
702 | |
a67cd958 |
703 | //getLAnglebracket(in); //<type sequence> |
be97b953 |
704 | |
a67cd958 |
705 | //t->nested_type = parseType(in, NULL, unnamed_types, named_types); |
706 | |
707 | if(t->fields.position < 1) in->error(in, "Sequence has no length type"); |
708 | if(t->fields.position < 2) in->error(in, "Sequence has no subtype"); |
709 | switch(((field_t*)t->fields.array[0])->type->type) { |
be97b953 |
710 | case UINT_FIXED : |
711 | case UCHAR : |
712 | case USHORT : |
713 | case UINT : |
714 | case ULONG : |
715 | case SIZE_T : |
716 | case OFF_T : |
717 | break; |
718 | default: |
719 | in->error(in, "Wrong length type for sequence"); |
720 | } |
721 | |
3583026d |
722 | getLAnglebracket(in); //</sequence> |
723 | getForwardslash(in); |
724 | token = getName(in); |
725 | if(strcmp("sequence",token))in->error(in,"not a valid sequence definition"); |
726 | getRAnglebracket(in); //</sequence> |
727 | } |
728 | else if(strcmp(token,"enum") == 0) { |
70f46ac3 |
729 | char * str; |
730 | int value = -1; |
731 | |
3583026d |
732 | t->type = ENUM; |
733 | sequence_init(&(t->labels)); |
70f46ac3 |
734 | sequence_init(&(t->labels_values)); |
3583026d |
735 | sequence_init(&(t->labels_description)); |
736 | t->already_printed = 0; |
be97b953 |
737 | getTypeAttributes(in, t, unnamed_types, named_types); |
2d2d14a7 |
738 | //if(t->size == 0) in->error(in, "Sequence has empty size"); |
bf6349fa |
739 | //Mathieu : we fix enum size to target int size. GCC is always like this. |
2d2d14a7 |
740 | //fox copy optimisation. |
bf6349fa |
741 | if(t->size != 0) in->error(in, "Enum has fixed size of target int."); |
742 | t->size = 0; |
3583026d |
743 | getRAnglebracket(in); |
744 | |
745 | //<label name=label1 value=n/> |
746 | getLAnglebracket(in); |
747 | token = getToken(in); //"label" or "/" |
748 | while(strcmp("label",token) == 0){ |
70f46ac3 |
749 | int *label_value = malloc(sizeof(int)); |
750 | |
9dae5ec2 |
751 | str = allocAndCopy(getNameAttribute(in)); |
3583026d |
752 | token = getValueStrAttribute(in); |
70f46ac3 |
753 | |
754 | sequence_push(&(t->labels),str); |
755 | |
756 | if(token) value = strtol(token, NULL, 0); |
757 | else value++; |
758 | |
759 | *label_value = value; |
760 | sequence_push(&(t->labels_values), label_value); |
3583026d |
761 | |
762 | getForwardslash(in); |
763 | getRAnglebracket(in); |
764 | |
765 | //read description if any. May be NULL. |
766 | str = allocAndCopy(getDescription(in)); |
767 | sequence_push(&(t->labels_description),str); |
768 | |
769 | //next label definition |
770 | getLAnglebracket(in); |
771 | token = getToken(in); //"label" or "/" |
772 | } |
773 | if(strcmp("/",token))in->error(in, "not a valid enum definition"); |
774 | token = getName(in); |
775 | if(strcmp("enum",token))in->error(in, "not a valid enum definition"); |
776 | getRAnglebracket(in); //</label> |
777 | } |
2d2d14a7 |
778 | else if(strcmp(token,"int_fixed") == 0) { |
779 | t->type = INT_FIXED; |
be97b953 |
780 | getTypeAttributes(in, t, unnamed_types, named_types); |
2d2d14a7 |
781 | if(t->size == 0) in->error(in, "int has empty size"); |
782 | getForwardslash(in); |
783 | getRAnglebracket(in); |
784 | } |
785 | else if(strcmp(token,"uint_fixed") == 0) { |
786 | t->type = UINT_FIXED; |
be97b953 |
787 | getTypeAttributes(in, t, unnamed_types, named_types); |
2d2d14a7 |
788 | if(t->size == 0) in->error(in, "uint has empty size"); |
789 | getForwardslash(in); |
790 | getRAnglebracket(in); |
791 | } |
792 | else if(strcmp(token,"char") == 0) { |
793 | t->type = CHAR; |
be97b953 |
794 | getTypeAttributes(in, t, unnamed_types, named_types); |
294f0059 |
795 | t->size = 1; |
2d2d14a7 |
796 | getForwardslash(in); |
797 | getRAnglebracket(in); |
798 | } |
799 | else if(strcmp(token,"uchar") == 0) { |
800 | t->type = UCHAR; |
be97b953 |
801 | getTypeAttributes(in, t, unnamed_types, named_types); |
294f0059 |
802 | t->size = 1; |
2d2d14a7 |
803 | getForwardslash(in); |
804 | getRAnglebracket(in); |
805 | } |
806 | else if(strcmp(token,"short") == 0) { |
807 | t->type = SHORT; |
be97b953 |
808 | getTypeAttributes(in, t, unnamed_types, named_types); |
294f0059 |
809 | t->size = 2; |
2d2d14a7 |
810 | getForwardslash(in); |
811 | getRAnglebracket(in); |
812 | } |
813 | else if(strcmp(token,"ushort") == 0) { |
814 | t->type = USHORT; |
be97b953 |
815 | getTypeAttributes(in, t, unnamed_types, named_types); |
294f0059 |
816 | t->size = 2; |
2d2d14a7 |
817 | getForwardslash(in); |
818 | getRAnglebracket(in); |
819 | } |
3583026d |
820 | else if(strcmp(token,"int") == 0) { |
821 | t->type = INT; |
be97b953 |
822 | getTypeAttributes(in, t, unnamed_types, named_types); |
3583026d |
823 | getForwardslash(in); |
824 | getRAnglebracket(in); |
825 | } |
826 | else if(strcmp(token,"uint") == 0) { |
827 | t->type = UINT; |
be97b953 |
828 | getTypeAttributes(in, t, unnamed_types, named_types); |
3583026d |
829 | getForwardslash(in); |
830 | getRAnglebracket(in); |
831 | } |
2d2d14a7 |
832 | |
3583026d |
833 | else if(strcmp(token,"pointer") == 0) { |
834 | t->type = POINTER; |
be97b953 |
835 | getTypeAttributes(in, t, unnamed_types, named_types); |
3583026d |
836 | getForwardslash(in); |
837 | getRAnglebracket(in); |
838 | } |
839 | else if(strcmp(token,"long") == 0) { |
840 | t->type = LONG; |
be97b953 |
841 | getTypeAttributes(in, t, unnamed_types, named_types); |
3583026d |
842 | getForwardslash(in); |
843 | getRAnglebracket(in); |
844 | } |
845 | else if(strcmp(token,"ulong") == 0) { |
846 | t->type = ULONG; |
be97b953 |
847 | getTypeAttributes(in, t, unnamed_types, named_types); |
3583026d |
848 | getForwardslash(in); |
849 | getRAnglebracket(in); |
850 | } |
851 | else if(strcmp(token,"size_t") == 0) { |
852 | t->type = SIZE_T; |
be97b953 |
853 | getTypeAttributes(in, t, unnamed_types, named_types); |
3583026d |
854 | getForwardslash(in); |
855 | getRAnglebracket(in); |
856 | } |
857 | else if(strcmp(token,"ssize_t") == 0) { |
858 | t->type = SSIZE_T; |
be97b953 |
859 | getTypeAttributes(in, t, unnamed_types, named_types); |
3583026d |
860 | getForwardslash(in); |
861 | getRAnglebracket(in); |
862 | } |
863 | else if(strcmp(token,"off_t") == 0) { |
864 | t->type = OFF_T; |
be97b953 |
865 | getTypeAttributes(in, t, unnamed_types, named_types); |
3583026d |
866 | getForwardslash(in); |
867 | getRAnglebracket(in); |
868 | } |
869 | else if(strcmp(token,"float") == 0) { |
870 | t->type = FLOAT; |
be97b953 |
871 | getTypeAttributes(in, t, unnamed_types, named_types); |
3583026d |
872 | getForwardslash(in); |
873 | getRAnglebracket(in); |
874 | } |
875 | else if(strcmp(token,"string") == 0) { |
876 | t->type = STRING; |
be97b953 |
877 | getTypeAttributes(in, t, unnamed_types, named_types); |
3583026d |
878 | getForwardslash(in); |
879 | getRAnglebracket(in); |
880 | } |
881 | else if(strcmp(token,"typeref") == 0){ |
882 | // Must be a named type |
a67cd958 |
883 | free(t); |
884 | sequence_pop(unnamed_types); |
885 | token = getNameAttribute(in); |
886 | t = find_named_type(token, named_types); |
887 | if(t == NULL) in->error(in,"Named referred to must be pre-declared."); |
888 | getForwardslash(in); //<typeref name=type_name/> |
889 | getRAnglebracket(in); |
890 | return t; |
3583026d |
891 | }else in->error(in,"not a valid type"); |
892 | |
893 | return t; |
894 | } |
895 | |
896 | /***************************************************************************** |
897 | *Function name |
898 | * find_named_type : find a named type from hash table |
899 | *Input params |
900 | * name : type name |
901 | * named_types : array of named types |
902 | *Return values |
903 | * type_descriptor * : a type descriptor |
904 | *****************************************************************************/ |
905 | |
906 | type_descriptor_t * find_named_type(char *name, table_t * named_types) |
907 | { |
908 | type_descriptor_t *t; |
909 | |
910 | t = table_find(named_types,name); |
34c1d1b5 |
911 | |
3583026d |
912 | return t; |
34c1d1b5 |
913 | } |
914 | |
915 | type_descriptor_t * create_named_type(char *name, table_t * named_types) |
916 | { |
917 | type_descriptor_t *t; |
918 | |
919 | t = (type_descriptor_t *)memAlloc(sizeof(type_descriptor_t)); |
920 | t->type_name = allocAndCopy(name); |
921 | t->type = NONE; |
922 | t->fmt = NULL; |
923 | table_insert(named_types,t->type_name,t); |
924 | // table_insert(named_types,allocAndCopy(name),t); |
925 | return t; |
926 | } |
3583026d |
927 | |
928 | /***************************************************************************** |
929 | *Function name |
930 | * parseTypeDefinition : get type information from type definition |
931 | *Input params |
932 | * in : input file handle |
933 | * unnamed_types : array of unamed types |
934 | * named_types : array of named types |
935 | *****************************************************************************/ |
936 | |
937 | void parseTypeDefinition(parse_file_t * in, sequence_t * unnamed_types, |
938 | table_t * named_types) |
939 | { |
940 | char *token; |
941 | type_descriptor_t *t; |
942 | |
943 | token = getNameAttribute(in); |
944 | if(token == NULL) in->error(in, "Type has empty name"); |
34c1d1b5 |
945 | t = create_named_type(token, named_types); |
3583026d |
946 | |
947 | if(t->type != NONE) in->error(in,"redefinition of named type"); |
948 | getRAnglebracket(in); //<type name=type_name> |
949 | getLAnglebracket(in); //< |
950 | token = getName(in); |
951 | //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition"); |
952 | ungetToken(in); |
953 | parseType(in,t, unnamed_types, named_types); |
954 | |
955 | //</type> |
956 | getLAnglebracket(in); |
957 | getForwardslash(in); |
958 | token = getName(in); |
959 | if(strcmp("type",token))in->error(in,"not a valid type definition"); |
960 | getRAnglebracket(in); //</type> |
961 | } |
962 | |
963 | /************************************************************************** |
964 | * Function : |
965 | * getComa, getName, getNumber, getEqual |
966 | * Description : |
967 | * Read a token from the input file, check its type, return it scontent. |
968 | * |
969 | * Parameters : |
970 | * in , input file handle. |
971 | * |
972 | * Return values : |
973 | * address of token content. |
974 | * |
975 | **************************************************************************/ |
976 | |
977 | char *getName(parse_file_t * in) |
978 | { |
979 | char *token; |
980 | |
981 | token = getToken(in); |
9dae5ec2 |
982 | // Optional descriptions |
983 | // if(in->type != NAME) in->error(in,"Name token was expected"); |
3583026d |
984 | return token; |
985 | } |
986 | |
987 | int getNumber(parse_file_t * in) |
988 | { |
989 | char *token; |
990 | |
991 | token = getToken(in); |
992 | if(in->type != NUMBER) in->error(in, "Number token was expected"); |
993 | return atoi(token); |
994 | } |
995 | |
996 | char *getForwardslash(parse_file_t * in) |
997 | { |
998 | char *token; |
999 | |
1000 | token = getToken(in); |
2d2d14a7 |
1001 | //if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected"); |
1002 | /* Mathieu : final / is optional now. */ |
1003 | if(in->type != FORWARDSLASH) ungetToken(in); |
1004 | |
3583026d |
1005 | return token; |
1006 | } |
1007 | |
1008 | char *getLAnglebracket(parse_file_t * in) |
1009 | { |
1010 | char *token; |
1011 | |
1012 | token = getToken(in); |
1013 | if(in->type != LANGLEBRACKET) in->error(in, "Left angle bracket was expected"); |
1014 | return token; |
1015 | } |
1016 | |
1017 | char *getRAnglebracket(parse_file_t * in) |
1018 | { |
1019 | char *token; |
1020 | |
1021 | token = getToken(in); |
1022 | if(in->type != RANGLEBRACKET) in->error(in, "Right angle bracket was expected"); |
1023 | return token; |
1024 | } |
1025 | |
1026 | char *getQuotedString(parse_file_t * in) |
1027 | { |
1028 | char *token; |
1029 | |
1030 | token = getToken(in); |
1031 | if(in->type != QUOTEDSTRING) in->error(in, "quoted string was expected"); |
1032 | return token; |
1033 | } |
1034 | |
1035 | char * getEqual(parse_file_t *in) |
1036 | { |
1037 | char *token; |
1038 | |
1039 | token = getToken(in); |
1040 | if(in->type != EQUAL) in->error(in, "equal was expected"); |
1041 | return token; |
1042 | } |
1043 | |
1044 | char seekNextChar(parse_file_t *in) |
1045 | { |
1046 | char car; |
1047 | while((car = getc(in->fp)) != EOF) { |
1048 | if(!isspace(car)){ |
1049 | ungetc(car,in->fp); |
1050 | return car; |
1051 | } |
1052 | } |
1053 | return EOF; |
1054 | } |
1055 | |
1056 | /****************************************************************** |
1057 | * Function : |
1058 | * getToken, ungetToken |
1059 | * Description : |
1060 | * Read a token from the input file and return its type and content. |
1061 | * Line numbers are accounted for and whitespace/comments are skipped. |
1062 | * |
1063 | * Parameters : |
1064 | * in, input file handle. |
1065 | * |
1066 | * Return values : |
1067 | * address of token content. |
1068 | * |
1069 | ******************************************************************/ |
1070 | |
1071 | void ungetToken(parse_file_t * in) |
1072 | { |
1073 | in->unget = 1; |
1074 | } |
1075 | |
1076 | char *getToken(parse_file_t * in) |
1077 | { |
1078 | FILE *fp = in->fp; |
1079 | char car, car1; |
1080 | int pos = 0, escaped; |
1081 | |
1082 | if(in->unget == 1) { |
1083 | in->unget = 0; |
1084 | return in->buffer; |
1085 | } |
1086 | |
1087 | /* skip whitespace and comments */ |
1088 | |
1089 | while((car = getc(fp)) != EOF) { |
1090 | if(car == '/') { |
1091 | car1 = getc(fp); |
1092 | if(car1 == '*') skipComment(in); |
1093 | else if(car1 == '/') skipEOL(in); |
1094 | else { |
1095 | car1 = ungetc(car1,fp); |
1096 | break; |
1097 | } |
1098 | } |
1099 | else if(car == '\n') in->lineno++; |
1100 | else if(!isspace(car)) break; |
1101 | } |
1102 | |
1103 | switch(car) { |
1104 | case EOF: |
1105 | in->type = ENDFILE; |
1106 | break; |
1107 | case '/': |
1108 | in->type = FORWARDSLASH; |
1109 | in->buffer[pos] = car; |
1110 | pos++; |
1111 | break; |
1112 | case '<': |
1113 | in->type = LANGLEBRACKET; |
1114 | in->buffer[pos] = car; |
1115 | pos++; |
1116 | break; |
1117 | case '>': |
1118 | in->type = RANGLEBRACKET; |
1119 | in->buffer[pos] = car; |
1120 | pos++; |
1121 | break; |
1122 | case '=': |
1123 | in->type = EQUAL; |
1124 | in->buffer[pos] = car; |
1125 | pos++; |
1126 | break; |
1127 | case '"': |
1128 | escaped = 0; |
1129 | while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) { |
1130 | if(car == '\\' && escaped == 0) { |
1131 | in->buffer[pos] = car; |
1132 | pos++; |
1133 | escaped = 1; |
1134 | continue; |
1135 | } |
1136 | if(car == '"' && escaped == 0) break; |
1137 | if(car == '\n' && escaped == 0) { |
1138 | in->error(in, "non escaped newline inside quoted string"); |
1139 | } |
1140 | if(car == '\n') in->lineno++; |
1141 | in->buffer[pos] = car; |
1142 | pos++; |
1143 | escaped = 0; |
1144 | } |
1145 | if(car == EOF) in->error(in,"no ending quotemark"); |
1146 | if(pos == BUFFER_SIZE) in->error(in, "quoted string token too large"); |
1147 | in->type = QUOTEDSTRING; |
1148 | break; |
1149 | default: |
1150 | if(isdigit(car)) { |
1151 | in->buffer[pos] = car; |
1152 | pos++; |
1153 | while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) { |
1154 | if(!isdigit(car)) { |
1155 | ungetc(car,fp); |
1156 | break; |
1157 | } |
1158 | in->buffer[pos] = car; |
1159 | pos++; |
1160 | } |
1161 | if(car == EOF) ungetc(car,fp); |
1162 | if(pos == BUFFER_SIZE) in->error(in, "number token too large"); |
1163 | in->type = NUMBER; |
1164 | } |
9dae5ec2 |
1165 | else if(isalnum(car) || car == '_' || car == '-') { |
3583026d |
1166 | in->buffer[0] = car; |
1167 | pos = 1; |
1168 | while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) { |
9dae5ec2 |
1169 | if(!(isalnum(car) || car == '_' || car == '-')) { |
3583026d |
1170 | ungetc(car,fp); |
1171 | break; |
1172 | } |
1173 | in->buffer[pos] = car; |
1174 | pos++; |
1175 | } |
1176 | if(car == EOF) ungetc(car,fp); |
1177 | if(pos == BUFFER_SIZE) in->error(in, "name token too large"); |
1178 | in->type = NAME; |
1179 | } |
1180 | else in->error(in, "invalid character, unrecognized token"); |
1181 | } |
1182 | in->buffer[pos] = 0; |
1183 | return in->buffer; |
1184 | } |
1185 | |
1186 | void skipComment(parse_file_t * in) |
1187 | { |
1188 | char car; |
1189 | while((car = getc(in->fp)) != EOF) { |
1190 | if(car == '\n') in->lineno++; |
1191 | else if(car == '*') { |
1192 | car = getc(in->fp); |
1193 | if(car ==EOF) break; |
1194 | if(car == '/') return; |
1195 | ungetc(car,in->fp); |
1196 | } |
1197 | } |
1198 | if(car == EOF) in->error(in,"comment begining with '/*' has no ending '*/'"); |
1199 | } |
1200 | |
1201 | void skipEOL(parse_file_t * in) |
1202 | { |
1203 | char car; |
1204 | while((car = getc(in->fp)) != EOF) { |
1205 | if(car == '\n') { |
1206 | ungetc(car,in->fp); |
1207 | break; |
1208 | } |
1209 | } |
1210 | if(car == EOF)ungetc(car, in->fp); |
1211 | } |
1212 | |
1213 | /***************************************************************************** |
1214 | *Function name |
1215 | * checkNamedTypesImplemented : check if all named types have definition |
1216 | ****************************************************************************/ |
1217 | |
1218 | void checkNamedTypesImplemented(table_t * named_types) |
1219 | { |
1220 | type_descriptor_t *t; |
1221 | int pos; |
1222 | char str[256]; |
1223 | |
1224 | for(pos = 0 ; pos < named_types->values.position; pos++) { |
1225 | t = (type_descriptor_t *) named_types->values.array[pos]; |
1226 | if(t->type == NONE){ |
1227 | sprintf(str,"named type '%s' has no definition", |
1228 | (char*)named_types->keys.array[pos]); |
1229 | error_callback(NULL,str); |
1230 | } |
1231 | } |
1232 | } |
1233 | |
1234 | |
1235 | /***************************************************************************** |
1236 | *Function name |
1237 | * generateChecksum : generate checksum for the facility |
1238 | *Input Params |
1239 | * facName : name of facility |
1240 | *Output Params |
1241 | * checksum : checksum for the facility |
1242 | ****************************************************************************/ |
1243 | |
1244 | void generateChecksum(char* facName, |
30d72138 |
1245 | unsigned int * checksum, sequence_t * events) |
3583026d |
1246 | { |
1247 | unsigned long crc ; |
1248 | int pos; |
1249 | event_t * ev; |
bf6349fa |
1250 | unsigned int i; |
3583026d |
1251 | |
1252 | crc = crc32(facName); |
1253 | for(pos = 0; pos < events->position; pos++){ |
1254 | ev = (event_t *)(events->array[pos]); |
47299663 |
1255 | crc = partial_crc32(ev->name, crc); |
bf6349fa |
1256 | for(i = 0; i < ev->fields.position; i++) { |
47299663 |
1257 | field_t *f = (field_t*)ev->fields.array[i]; |
1258 | crc = partial_crc32(f->name, crc); |
1259 | crc = getTypeChecksum(crc, f->type); |
1260 | } |
3583026d |
1261 | } |
1262 | *checksum = crc; |
1263 | } |
1264 | |
1265 | /***************************************************************************** |
1266 | *Function name |
1267 | * getTypeChecksum : generate checksum by type info |
1268 | *Input Params |
1269 | * crc : checksum generated so far |
1270 | * type : type descriptor containing type info |
1271 | *Return value |
1272 | * unsigned long : checksum |
1273 | *****************************************************************************/ |
1274 | |
1275 | unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor_t * type) |
1276 | { |
1277 | unsigned long crc = aCrc; |
1278 | char * str = NULL, buf[16]; |
1279 | int flag = 0, pos; |
1280 | field_t * fld; |
1281 | |
1282 | switch(type->type){ |
2d2d14a7 |
1283 | case INT_FIXED: |
1284 | str = intOutputTypes[getSizeindex(type->size)]; |
3583026d |
1285 | break; |
2d2d14a7 |
1286 | case UINT_FIXED: |
1287 | str = uintOutputTypes[getSizeindex(type->size)]; |
3583026d |
1288 | break; |
1289 | case POINTER: |
1290 | str = allocAndCopy("void *"); |
1291 | flag = 1; |
1292 | break; |
2d2d14a7 |
1293 | case CHAR: |
1294 | str = allocAndCopy("signed char"); |
1295 | flag = 1; |
1296 | break; |
1297 | case UCHAR: |
1298 | str = allocAndCopy("unsigned char"); |
1299 | flag = 1; |
1300 | break; |
1301 | case SHORT: |
1302 | str = allocAndCopy("short"); |
1303 | flag = 1; |
1304 | break; |
1305 | case USHORT: |
1306 | str = allocAndCopy("unsigned short"); |
1307 | flag = 1; |
1308 | break; |
1309 | case INT: |
1310 | str = allocAndCopy("int"); |
1311 | flag = 1; |
1312 | break; |
1313 | case UINT: |
1314 | str = allocAndCopy("uint"); |
1315 | flag = 1; |
1316 | break; |
3583026d |
1317 | case LONG: |
1318 | str = allocAndCopy("long"); |
1319 | flag = 1; |
1320 | break; |
1321 | case ULONG: |
1322 | str = allocAndCopy("unsigned long"); |
1323 | flag = 1; |
1324 | break; |
1325 | case SIZE_T: |
1326 | str = allocAndCopy("size_t"); |
1327 | flag = 1; |
1328 | break; |
1329 | case SSIZE_T: |
1330 | str = allocAndCopy("ssize_t"); |
1331 | flag = 1; |
1332 | break; |
1333 | case OFF_T: |
1334 | str = allocAndCopy("off_t"); |
1335 | flag = 1; |
1336 | break; |
1337 | case FLOAT: |
2d2d14a7 |
1338 | str = floatOutputTypes[getSizeindex(type->size)]; |
3583026d |
1339 | break; |
1340 | case STRING: |
1341 | str = allocAndCopy("string"); |
1342 | flag = 1; |
1343 | break; |
1344 | case ENUM: |
2d2d14a7 |
1345 | //str = appendString("enum ", uintOutputTypes[getSizeindex(type->size)]); |
1346 | str = allocAndCopy("enum"); |
3583026d |
1347 | flag = 1; |
1348 | break; |
1349 | case ARRAY: |
2e415130 |
1350 | sprintf(buf,"%zu", type->size); |
3583026d |
1351 | str = appendString("array ",buf); |
1352 | flag = 1; |
1353 | break; |
1354 | case SEQUENCE: |
294f0059 |
1355 | str = allocAndCopy("sequence "); |
3583026d |
1356 | flag = 1; |
1357 | break; |
1358 | case STRUCT: |
1359 | str = allocAndCopy("struct"); |
1360 | flag = 1; |
1361 | break; |
1362 | case UNION: |
1363 | str = allocAndCopy("union"); |
1364 | flag = 1; |
1365 | break; |
1366 | default: |
1367 | error_callback(NULL, "named type has no definition"); |
1368 | break; |
1369 | } |
1370 | |
1371 | crc = partial_crc32(str,crc); |
1372 | if(flag) free(str); |
1373 | |
1374 | if(type->fmt) crc = partial_crc32(type->fmt,crc); |
1375 | |
2e415130 |
1376 | if(type->type == ARRAY){ |
1377 | crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type); |
1378 | } else if(type->type ==SEQUENCE) { |
1379 | crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type); |
1380 | crc = getTypeChecksum(crc,((field_t*)type->fields.array[1])->type); |
1381 | } else if(type->type == STRUCT || type->type == UNION){ |
3583026d |
1382 | for(pos =0; pos < type->fields.position; pos++){ |
1383 | fld = (field_t *) type->fields.array[pos]; |
1384 | crc = partial_crc32(fld->name,crc); |
1385 | crc = getTypeChecksum(crc, fld->type); |
1386 | } |
1387 | }else if(type->type == ENUM){ |
1388 | for(pos = 0; pos < type->labels.position; pos++) |
1389 | crc = partial_crc32((char*)type->labels.array[pos],crc); |
1390 | } |
1391 | |
1392 | return crc; |
1393 | } |
1394 | |
1395 | |
1396 | /* Event type descriptors */ |
1397 | void freeType(type_descriptor_t * tp) |
1398 | { |
1399 | int pos2; |
1400 | field_t *f; |
1401 | |
1402 | if(tp->fmt != NULL) free(tp->fmt); |
1403 | if(tp->type == ENUM) { |
1404 | for(pos2 = 0; pos2 < tp->labels.position; pos2++) { |
1405 | free(tp->labels.array[pos2]); |
1406 | } |
1407 | sequence_dispose(&(tp->labels)); |
70f46ac3 |
1408 | for(pos2 = 0; pos2 < tp->labels_values.position; pos2++) { |
1409 | free(tp->labels_values.array[pos2]); |
1410 | } |
1411 | sequence_dispose(&(tp->labels_values)); |
3583026d |
1412 | } |
1413 | if(tp->type == STRUCT) { |
1414 | for(pos2 = 0; pos2 < tp->fields.position; pos2++) { |
1415 | f = (field_t *) tp->fields.array[pos2]; |
1416 | free(f->name); |
1417 | free(f->description); |
1418 | free(f); |
1419 | } |
1420 | sequence_dispose(&(tp->fields)); |
1421 | } |
1422 | } |
1423 | |
1424 | void freeNamedType(table_t * t) |
1425 | { |
1426 | int pos; |
1427 | type_descriptor_t * td; |
1428 | |
1429 | for(pos = 0 ; pos < t->keys.position; pos++) { |
1430 | free((char *)t->keys.array[pos]); |
1431 | td = (type_descriptor_t*)t->values.array[pos]; |
1432 | freeType(td); |
1433 | free(td); |
1434 | } |
1435 | } |
1436 | |
1437 | void freeTypes(sequence_t *t) |
1438 | { |
1439 | int pos; |
1440 | type_descriptor_t *tp; |
1441 | |
1442 | for(pos = 0 ; pos < t->position; pos++) { |
1443 | tp = (type_descriptor_t *)t->array[pos]; |
1444 | freeType(tp); |
1445 | free(tp); |
1446 | } |
1447 | } |
1448 | |
1449 | void freeEvents(sequence_t *t) |
1450 | { |
1451 | int pos; |
1452 | event_t *ev; |
1453 | |
1454 | for(pos = 0 ; pos < t->position; pos++) { |
1455 | ev = (event_t *) t->array[pos]; |
1456 | free(ev->name); |
1457 | free(ev->description); |
47299663 |
1458 | sequence_dispose(&ev->fields); |
3583026d |
1459 | free(ev); |
1460 | } |
1461 | |
1462 | } |
1463 | |
1464 | |
1465 | /* Extensible array */ |
1466 | |
1467 | void sequence_init(sequence_t *t) |
1468 | { |
1469 | t->size = 10; |
1470 | t->position = 0; |
1471 | t->array = (void **)memAlloc(t->size * sizeof(void *)); |
1472 | } |
1473 | |
1474 | void sequence_dispose(sequence_t *t) |
1475 | { |
1476 | t->size = 0; |
1477 | free(t->array); |
1478 | t->array = NULL; |
1479 | } |
1480 | |
1481 | void sequence_push(sequence_t *t, void *elem) |
1482 | { |
1483 | void **tmp; |
1484 | |
1485 | if(t->position >= t->size) { |
1486 | tmp = t->array; |
1487 | t->array = (void **)memAlloc(t->size * 2 * sizeof(void *)); |
1488 | memcpy(t->array, tmp, t->size * sizeof(void *)); |
1489 | t->size = t->size * 2; |
1490 | free(tmp); |
1491 | } |
1492 | t->array[t->position] = elem; |
1493 | t->position++; |
1494 | } |
1495 | |
1496 | void *sequence_pop(sequence_t *t) |
1497 | { |
1498 | return t->array[t->position--]; |
1499 | } |
1500 | |
1501 | |
1502 | /* Hash table API, implementation is just linear search for now */ |
1503 | |
1504 | void table_init(table_t *t) |
1505 | { |
1506 | sequence_init(&(t->keys)); |
1507 | sequence_init(&(t->values)); |
1508 | } |
1509 | |
1510 | void table_dispose(table_t *t) |
1511 | { |
1512 | sequence_dispose(&(t->keys)); |
1513 | sequence_dispose(&(t->values)); |
1514 | } |
1515 | |
1516 | void table_insert(table_t *t, char *key, void *value) |
1517 | { |
1518 | sequence_push(&(t->keys),key); |
1519 | sequence_push(&(t->values),value); |
1520 | } |
1521 | |
1522 | void *table_find(table_t *t, char *key) |
1523 | { |
1524 | int pos; |
1525 | for(pos = 0 ; pos < t->keys.position; pos++) { |
1526 | if(strcmp((char *)key,(char *)t->keys.array[pos]) == 0) |
1527 | return(t->values.array[pos]); |
1528 | } |
1529 | return NULL; |
1530 | } |
1531 | |
1532 | void table_insert_int(table_t *t, int *key, void *value) |
1533 | { |
1534 | sequence_push(&(t->keys),key); |
1535 | sequence_push(&(t->values),value); |
1536 | } |
1537 | |
1538 | void *table_find_int(table_t *t, int *key) |
1539 | { |
1540 | int pos; |
1541 | for(pos = 0 ; pos < t->keys.position; pos++) { |
1542 | if(*key == *(int *)t->keys.array[pos]) |
1543 | return(t->values.array[pos]); |
1544 | } |
1545 | return NULL; |
1546 | } |
1547 | |
1548 | |
1549 | /* Concatenate strings */ |
1550 | |
1551 | char *appendString(char *s, char *suffix) |
1552 | { |
1553 | char *tmp; |
1554 | if(suffix == NULL) return s; |
1555 | |
1556 | tmp = (char *)memAlloc(strlen(s) + strlen(suffix) + 1); |
1557 | strcpy(tmp,s); |
1558 | strcat(tmp,suffix); |
1559 | return tmp; |
1560 | } |