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