3 parser.c: Generate helper declarations and functions to trace events
4 from an event description file.
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.
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.
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
23 /* This program reads the ".xml" event definitions input files
24 and constructs structure for each event.
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
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. */
40 #include <linux/errno.h>
47 char *intOutputTypes
[] = {
48 "int8_t", "int16_t", "int32_t", "int64_t" };
50 char *uintOutputTypes
[] = {
51 "uint8_t", "uint16_t", "uint32_t", "uint64_t" };
53 char *floatOutputTypes
[] = {
54 "undef", "undef", "float", "double" };
60 void strupper(char *string
)
71 int getSizeindex(unsigned int value
)
83 printf("Error : unknown value size %d\n", value
);
88 /*****************************************************************************
90 * getSize : translate from string to integer
92 * in : input file handle
95 *****************************************************************************/
97 unsigned long long int getSize(parse_file_t
*in
)
100 unsigned long long int ret
;
102 token
= getToken(in
);
105 if(in
->type
== QUOTEDSTRING
) {
109 if (!isdigit(*token2
)) {
110 in
->type
= QUOTEDSTRING
;
113 } while (*(++token2
) != '\0');
116 if(in
->type
== NUMBER
) {
117 ret
= strtoull(token
, NULL
, 0);
124 in
->error(in
,"incorrect size specification");
128 /*****************************************************************************
130 * error_callback : print out error info
132 * in : input file handle
133 * msg : message to be printed
134 ****************************************************************************/
136 void error_callback(parse_file_t
*in
, char *msg
)
139 printf("Error in file %s, line %d: %s\n", in
->name
, in
->lineno
, msg
);
146 /*****************************************************************************
148 * memAlloc : allocate memory
150 * size : required memory size
152 * void * : pointer to allocate memory or NULL
153 ****************************************************************************/
155 void * memAlloc(int size
)
158 if(size
== 0) return NULL
;
161 printf("Failed to allocate memory");
167 /*****************************************************************************
169 * allocAndCopy : allocate memory and initialize it
171 * str : string to be put in memory
173 * char * : pointer to allocate memory or NULL
174 ****************************************************************************/
176 char *allocAndCopy(char *str
)
179 if(str
== NULL
) return NULL
;
180 addr
= (char *)memAlloc(strlen(str
)+1);
185 /**************************************************************************
189 * Read the attribute from the input file.
192 * in , input file handle.
193 * t , the type descriptor to fill.
195 **************************************************************************/
197 void getTypeAttributes(parse_file_t
*in
, type_descriptor_t
*t
,
198 sequence_t
* unnamed_types
, table_t
* named_types
)
209 token
= getToken(in
);
210 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
215 if(!strcmp("format",token
)) {
217 t
->fmt
= allocAndCopy(getQuotedString(in
));
218 //} else if(!strcmp("name",token)) {
220 // car = seekNextChar(in);
221 // if(car == EOF) in->error(in,"name was expected");
222 // else if(car == '\"') t->type_name = allocAndCopy(getQuotedString(in));
223 // else t->type_name = allocAndCopy(getName(in));
224 } else if(!strcmp("size",token
)) {
226 t
->size
= getSize(in
);
227 } else if(!strcmp("custom_write", token
)) {
229 } else if(!strcmp("byte_order",token
)) {
231 car
= seekNextChar(in
);
232 if(car
== EOF
) in
->error(in
,"byte order was expected (network?)");
233 else if(car
== '\"') token
= getQuotedString(in
);
234 else token
= getName(in
);
235 if(!strcmp("network", token
)) {
238 } else if(!strcmp("write",token
)) {
240 car
= seekNextChar(in
);
241 if(car
== EOF
) in
->error(in
,"write type was expected (custom?)");
242 else if(car
== '\"') token
= getQuotedString(in
);
243 else token
= getName(in
);
244 if(!strcmp("custom", token
)) {
251 /**************************************************************************
255 * Read the attribute from the input file.
258 * in , input file handle.
259 * ev , the event to fill.
261 **************************************************************************/
263 void getEventAttributes(parse_file_t
*in
, event_t
*ev
)
270 ev
->per_tracefile
= 0;
271 ev
->param_buffer
= 0;
272 ev
->no_instrument_function
= 0;
273 ev
->high_priority
= 0;
276 token
= getToken(in
);
277 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
282 if(!strcmp("name",token
)) {
284 car
= seekNextChar(in
);
285 if(car
== EOF
) in
->error(in
,"name was expected");
286 else if(car
== '\"') ev
->name
= allocAndCopy(getQuotedString(in
));
287 else ev
->name
= allocAndCopy(getName(in
));
288 } else if(!strcmp("scope", token
)) {
290 car
= seekNextChar(in
);
291 if(car
== EOF
) in
->error(in
,"scope was expected");
292 else if(car
== '\"') token
= getQuotedString(in
);
293 else token
= getName(in
);
294 if(!strcmp(token
, "trace")) ev
->per_trace
= 1;
295 else if(!strcmp(token
, "tracefile")) ev
->per_tracefile
= 1;
296 } else if(!strcmp("param", token
)) {
298 car
= seekNextChar(in
);
299 if(car
== EOF
) in
->error(in
,"parameter type was expected");
300 else if(car
== '\"') token
= getQuotedString(in
);
301 else token
= getName(in
);
302 if(!strcmp(token
, "buffer")) ev
->param_buffer
= 1;
303 } else if(!strcmp("attribute", token
)) {
305 car
= seekNextChar(in
);
306 if(car
== EOF
) in
->error(in
,"attribute was expected");
307 else if(car
== '\"') token
= getQuotedString(in
);
308 else token
= getName(in
);
309 if(!strcmp(token
, "no_instrument_function"))
310 ev
->no_instrument_function
= 1;
311 else if(!strcmp(token
, "high_priority"))
312 ev
->high_priority
= 1;
317 /**************************************************************************
319 * getFacilityAttributes
321 * Read the attribute from the input file.
324 * in , input file handle.
325 * fac , the facility to fill.
327 **************************************************************************/
329 void getFacilityAttributes(parse_file_t
*in
, facility_t
*fac
)
339 token
= getToken(in
);
340 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
345 if(!strcmp("name",token
)) {
347 car
= seekNextChar(in
);
348 if(car
== EOF
) in
->error(in
,"name was expected");
349 else if(car
== '\"') fac
->name
= allocAndCopy(getQuotedString(in
));
350 else fac
->name
= allocAndCopy(getName(in
));
351 if(!strncmp(fac
->name
, "user_", sizeof("user_")-1))
353 } else if(!strcmp("arch", token
)) {
355 car
= seekNextChar(in
);
356 if(car
== '\"') fac
->arch
= allocAndCopy(getQuotedString(in
));
357 else fac
->arch
= allocAndCopy(getName(in
));
362 /**************************************************************************
366 * Read the attribute from the input file.
369 * in , input file handle.
370 * f , the field to fill.
372 **************************************************************************/
374 void getFieldAttributes(parse_file_t
*in
, field_t
*f
)
382 token
= getToken(in
);
383 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
388 if(!strcmp("name",token
)) {
390 car
= seekNextChar(in
);
391 if(car
== EOF
) in
->error(in
,"name was expected");
392 else if(car
== '\"') f
->name
= allocAndCopy(getQuotedString(in
));
393 else f
->name
= allocAndCopy(getName(in
));
398 char *getNameAttribute(parse_file_t
*in
)
405 token
= getToken(in
);
406 if(!strcmp("name",token
)) {
408 car
= seekNextChar(in
);
409 if(car
== EOF
) in
->error(in
,"name was expected");
410 else if(car
== '\"') name
= allocAndCopy(getQuotedString(in
));
411 else name
= allocAndCopy(getName(in
));
418 if(name
== NULL
) in
->error(in
, "Name was expected");
424 //for <label name=label_name value=n format="...">, value is an option
425 //Return value : 0 : no value, 1 : has a value
426 int getValueAttribute(parse_file_t
*in
, long long *value
)
428 char * token
, *token2
;
430 token
= getToken(in
);
432 if(strcmp("/",token
) == 0 || strcmp(">", token
) == 0){
436 if(strcmp("value",token
))in
->error(in
,"value was expected");
439 token
= getToken(in
);
441 if(in
->type
== QUOTEDSTRING
) {
445 if (!isdigit(*token2
)) {
446 in
->type
= QUOTEDSTRING
;
449 } while (*(++token2
) != '\0');
452 if(in
->type
== NUMBER
)
453 *value
= strtoll(token
, NULL
, 0);
458 in
->error(in
,"incorrect size specification");
462 char * getDescription(parse_file_t
*in
)
470 getLAnglebracket(in
);
472 if(strcmp("description",token
)){
473 fseek(in
->fp
, pos
, SEEK_SET
);
477 getRAnglebracket(in
);
480 while((car
= getc(in
->fp
)) != EOF
) {
481 if(car
== '<') break;
482 if(car
== '\0') continue;
483 in
->buffer
[pos
] = car
;
486 if(car
== EOF
)in
->error(in
,"not a valid description");
487 in
->buffer
[pos
] = '\0';
489 str
= allocAndCopy(in
->buffer
);
493 if(strcmp("description", token
))in
->error(in
,"not a valid description");
494 getRAnglebracket(in
);
499 /*****************************************************************************
501 * parseFacility : generate event list
503 * in : input file handle
504 * fac : empty facility
506 * fac : facility filled with event list
507 ****************************************************************************/
509 void parseFacility(parse_file_t
*in
, facility_t
* fac
)
514 getFacilityAttributes(in
, fac
);
515 if(fac
->name
== NULL
) in
->error(in
, "Attribute not named");
517 fac
->capname
= allocAndCopy(fac
->name
);
518 strupper(fac
->capname
);
519 getRAnglebracket(in
);
521 fac
->description
= getDescription(in
);
524 getLAnglebracket(in
);
526 token
= getToken(in
);
527 if(in
->type
== ENDFILE
)
528 in
->error(in
,"the definition of the facility is not finished");
530 if(strcmp("event",token
) == 0){
531 ev
= (event_t
*) memAlloc(sizeof(event_t
));
532 sequence_push(&(fac
->events
),ev
);
533 parseEvent(in
,ev
, &(fac
->unnamed_types
), &(fac
->named_types
));
534 }else if(strcmp("type",token
) == 0){
535 parseTypeDefinition(in
, &(fac
->unnamed_types
), &(fac
->named_types
));
536 }else if(in
->type
== FORWARDSLASH
){
538 }else in
->error(in
,"event or type token expected\n");
542 if(strcmp("facility",token
)) in
->error(in
,"not the end of the facility");
543 getRAnglebracket(in
); //</facility>
546 /*****************************************************************************
548 * parseEvent : generate event from event definition
550 * in : input file handle
552 * unnamed_types : array of unamed types
553 * named_types : array of named types
555 * ev : new event (parameters are passed to it)
556 ****************************************************************************/
558 void parseEvent(parse_file_t
*in
, event_t
* ev
, sequence_t
* unnamed_types
,
559 table_t
* named_types
)
564 sequence_init(&(ev
->fields
));
565 //<event name=eventtype_name>
566 getEventAttributes(in
, ev
);
567 if(ev
->name
== NULL
) in
->error(in
, "Event not named");
568 getRAnglebracket(in
);
570 //<description>...</description>
571 ev
->description
= getDescription(in
);
574 /* Events can have multiple fields. each field form at least a function
575 * parameter of the logging function. */
577 getLAnglebracket(in
);
578 token
= getToken(in
);
581 case FORWARDSLASH
: /* </event> */
583 if(strcmp("event",token
))in
->error(in
,"not an event definition");
584 getRAnglebracket(in
); //</event>
587 case NAME
: /* a field */
588 if(strcmp("field",token
))in
->error(in
,"expecting a field");
589 f
= (field_t
*)memAlloc(sizeof(field_t
));
590 sequence_push(&(ev
->fields
),f
);
591 parseFields(in
, f
, unnamed_types
, named_types
, 1);
594 in
->error(in
, "expecting </event> or <field >");
599 if(in
->type
== FORWARDSLASH
){ //</event> NOTHING
601 }else if(in
->type
== NAME
){
602 if(strcmp("struct",token
)==0 || strcmp("typeref",token
)==0){
604 ev
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
605 if(ev
->type
->type
!= STRUCT
&& ev
->type
->type
!= NONE
)
606 in
->error(in
,"type must be a struct");
607 }else in
->error(in
, "not a valid type");
609 getLAnglebracket(in
);
611 }else in
->error(in
,"not a struct type");
612 getLAnglebracket(in
);
615 if(strcmp("event",token
))in
->error(in
,"not an event definition");
616 getRAnglebracket(in
); //</event>
620 /*****************************************************************************
622 * parseField : get field infomation from buffer
624 * in : input file handle
626 * unnamed_types : array of unamed types
627 * named_types : array of named types
628 * tag : is field surrounded by a <field> </field> tag ?
629 ****************************************************************************/
631 void parseFields(parse_file_t
*in
, field_t
*f
,
632 sequence_t
* unnamed_types
,
633 table_t
* named_types
,
638 //<field name=field_name> <description> <type> </field>
639 getFieldAttributes(in
, f
);
640 if(f
->name
== NULL
) in
->error(in
, "Field not named");
641 getRAnglebracket(in
);
643 f
->description
= getDescription(in
);
645 f
->description
= NULL
;
649 getLAnglebracket(in
);
650 f
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
653 getLAnglebracket(in
);
656 if(strcmp("field",token
))in
->error(in
,"not a valid field definition");
657 getRAnglebracket(in
); //</field>
662 /*****************************************************************************
664 * parseType : get type information, type can be :
666 * int(size,fmt); uint(size,fmt); float(size,fmt);
667 * string(fmt); enum(size,fmt,(label1,label2...))
669 * array(arraySize, type); sequence(lengthSize,type)
670 * struct(field(name,type,description)...)
674 * in : input file handle
675 * inType : a type descriptor
676 * unnamed_types : array of unamed types
677 * named_types : array of named types
679 * type_descriptor* : a type descriptor
680 ****************************************************************************/
682 type_descriptor_t
*parseType(parse_file_t
*in
, type_descriptor_t
*inType
,
683 sequence_t
* unnamed_types
, table_t
* named_types
)
686 type_descriptor_t
*t
;
690 t
= (type_descriptor_t
*) memAlloc(sizeof(type_descriptor_t
));
694 sequence_push(unnamed_types
,t
);
700 if(strcmp(token
,"struct") == 0) {
702 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
703 getRAnglebracket(in
); //<struct>
704 getLAnglebracket(in
); //<field name=..>
705 token
= getToken(in
);
706 sequence_init(&(t
->fields
));
707 while(strcmp("field",token
) == 0){
708 f
= (field_t
*)memAlloc(sizeof(field_t
));
709 sequence_push(&(t
->fields
),f
);
711 parseFields(in
, f
, unnamed_types
, named_types
, 1);
714 getLAnglebracket(in
);
715 token
= getToken(in
);
717 if(strcmp("/",token
))in
->error(in
,"not a valid structure definition");
719 if(strcmp("struct",token
)!=0)
720 in
->error(in
,"not a valid structure definition");
721 getRAnglebracket(in
); //</struct>
723 else if(strcmp(token
,"union") == 0) {
725 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
726 getRAnglebracket(in
); //<union>
728 getLAnglebracket(in
); //<field name=..>
729 token
= getToken(in
);
730 sequence_init(&(t
->fields
));
731 while(strcmp("field",token
) == 0){
732 f
= (field_t
*)memAlloc(sizeof(field_t
));
733 sequence_push(&(t
->fields
),f
);
734 parseFields(in
, f
, unnamed_types
, named_types
, 1);
737 getLAnglebracket(in
);
738 token
= getToken(in
);
740 if(strcmp("/",token
))in
->error(in
,"not a valid union definition");
742 if(strcmp("union",token
)!=0)
743 in
->error(in
,"not a valid union definition");
744 getRAnglebracket(in
); //</union>
746 else if(strcmp(token
,"array") == 0) {
748 sequence_init(&(t
->fields
));
749 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
750 if(t
->size
== 0) in
->error(in
, "Array has empty size");
752 getRAnglebracket(in
); //<array size=n>
754 //getLAnglebracket(in); //<subtype>
756 f
= (field_t
*)memAlloc(sizeof(field_t
));
759 sequence_push(&(t
->fields
),f
);
760 parseFields(in
, f
, unnamed_types
, named_types
, 0);
762 //getLAnglebracket(in); //<type struct>
763 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
765 getLAnglebracket(in
); //</array>
768 if(strcmp("array",token
))in
->error(in
,"not a valid array definition");
769 getRAnglebracket(in
); //</array>
771 else if(strcmp(token
,"sequence") == 0) {
773 sequence_init(&(t
->fields
));
774 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
776 getRAnglebracket(in
); //<sequence>
778 //getLAnglebracket(in); //<sequence size type>
780 f
= (field_t
*)memAlloc(sizeof(field_t
));
782 sequence_push(&(t
->fields
),f
);
783 parseFields(in
, f
, unnamed_types
, named_types
, 0);
785 //getLAnglebracket(in); //<subtype>
787 f
= (field_t
*)memAlloc(sizeof(field_t
));
789 sequence_push(&(t
->fields
),f
);
790 parseFields(in
, f
, unnamed_types
, named_types
, 0);
792 //getLAnglebracket(in); //<type sequence>
793 //t->length_type = parseType(in, NULL, unnamed_types, named_types);
795 //getLAnglebracket(in); //<type sequence>
797 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
799 if(t
->fields
.position
< 1) in
->error(in
, "Sequence has no length type");
800 if(t
->fields
.position
< 2) in
->error(in
, "Sequence has no subtype");
801 switch(((field_t
*)t
->fields
.array
[0])->type
->type
) {
811 in
->error(in
, "Wrong length type for sequence");
814 getLAnglebracket(in
); //</sequence>
817 if(strcmp("sequence",token
))in
->error(in
,"not a valid sequence definition");
818 getRAnglebracket(in
); //</sequence>
820 else if(strcmp(token
,"enum") == 0) {
822 long long value
= -1;
825 sequence_init(&(t
->labels
));
826 sequence_init(&(t
->labels_values
));
827 sequence_init(&(t
->labels_description
));
828 t
->already_printed
= 0;
829 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
830 //if(t->size == 0) in->error(in, "Sequence has empty size");
831 //Mathieu : we fix enum size to target int size. GCC is always like this.
832 //fox copy optimisation.
833 if(t
->size
!= 0) in
->error(in
, "Enum has fixed size of target int.");
835 getRAnglebracket(in
);
837 //<label name=label1 value=n/>
838 getLAnglebracket(in
);
839 token
= getToken(in
); //"label" or "/"
840 while(strcmp("label",token
) == 0){
841 int *label_value
= malloc(sizeof(int));
845 str
= allocAndCopy(getNameAttribute(in
));
846 has_value
= getValueAttribute(in
, &loc_value
);
848 sequence_push(&(t
->labels
),str
);
850 if(has_value
) value
= loc_value
;
853 *label_value
= value
;
854 sequence_push(&(t
->labels_values
), label_value
);
857 getRAnglebracket(in
);
859 //read description if any. May be NULL.
860 str
= allocAndCopy(getDescription(in
));
861 sequence_push(&(t
->labels_description
),str
);
863 //next label definition
864 getLAnglebracket(in
);
865 token
= getToken(in
); //"label" or "/"
867 if(strcmp("/",token
))in
->error(in
, "not a valid enum definition");
869 if(strcmp("enum",token
))in
->error(in
, "not a valid enum definition");
870 getRAnglebracket(in
); //</label>
872 else if(strcmp(token
,"int_fixed") == 0) {
874 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
875 if(t
->size
== 0) in
->error(in
, "int has empty size");
877 getRAnglebracket(in
);
879 else if(strcmp(token
,"uint_fixed") == 0) {
880 t
->type
= UINT_FIXED
;
881 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
882 if(t
->size
== 0) in
->error(in
, "uint has empty size");
884 getRAnglebracket(in
);
886 else if(strcmp(token
,"char") == 0) {
888 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
891 getRAnglebracket(in
);
893 else if(strcmp(token
,"uchar") == 0) {
895 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
898 getRAnglebracket(in
);
900 else if(strcmp(token
,"short") == 0) {
902 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
905 getRAnglebracket(in
);
907 else if(strcmp(token
,"ushort") == 0) {
909 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
912 getRAnglebracket(in
);
914 else if(strcmp(token
,"int") == 0) {
916 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
918 getRAnglebracket(in
);
920 else if(strcmp(token
,"uint") == 0) {
922 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
924 getRAnglebracket(in
);
927 else if(strcmp(token
,"pointer") == 0) {
929 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
931 getRAnglebracket(in
);
933 else if(strcmp(token
,"long") == 0) {
935 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
937 getRAnglebracket(in
);
939 else if(strcmp(token
,"ulong") == 0) {
941 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
943 getRAnglebracket(in
);
945 else if(strcmp(token
,"size_t") == 0) {
947 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
949 getRAnglebracket(in
);
951 else if(strcmp(token
,"ssize_t") == 0) {
953 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
955 getRAnglebracket(in
);
957 else if(strcmp(token
,"off_t") == 0) {
959 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
961 getRAnglebracket(in
);
963 else if(strcmp(token
,"float") == 0) {
965 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
967 getRAnglebracket(in
);
969 else if(strcmp(token
,"string") == 0) {
971 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
973 getRAnglebracket(in
);
975 else if(strcmp(token
,"typeref") == 0){
976 // Must be a named type
978 sequence_pop(unnamed_types
);
979 token
= getNameAttribute(in
);
980 t
= find_named_type(token
, named_types
);
981 if(t
== NULL
) in
->error(in
,"Named referred to must be pre-declared.");
982 getForwardslash(in
); //<typeref name=type_name/>
983 getRAnglebracket(in
);
985 }else in
->error(in
,"not a valid type");
990 /*****************************************************************************
992 * find_named_type : find a named type from hash table
995 * named_types : array of named types
997 * type_descriptor * : a type descriptor
998 *****************************************************************************/
1000 type_descriptor_t
* find_named_type(char *name
, table_t
* named_types
)
1002 type_descriptor_t
*t
;
1004 t
= (type_descriptor_t
*)table_find(named_types
,name
);
1009 type_descriptor_t
* create_named_type(char *name
, table_t
* named_types
)
1011 type_descriptor_t
*t
;
1013 t
= (type_descriptor_t
*)memAlloc(sizeof(type_descriptor_t
));
1014 t
->type_name
= allocAndCopy(name
);
1017 table_insert(named_types
,t
->type_name
,t
);
1018 // table_insert(named_types,allocAndCopy(name),t);
1022 /*****************************************************************************
1024 * parseTypeDefinition : get type information from type definition
1026 * in : input file handle
1027 * unnamed_types : array of unamed types
1028 * named_types : array of named types
1029 *****************************************************************************/
1031 void parseTypeDefinition(parse_file_t
* in
, sequence_t
* unnamed_types
,
1032 table_t
* named_types
)
1035 type_descriptor_t
*t
;
1037 token
= getNameAttribute(in
);
1038 if(token
== NULL
) in
->error(in
, "Type has empty name");
1039 t
= create_named_type(token
, named_types
);
1041 if(t
->type
!= NONE
) in
->error(in
,"redefinition of named type");
1042 getRAnglebracket(in
); //<type name=type_name>
1043 getLAnglebracket(in
); //<
1044 token
= getName(in
);
1045 //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
1047 parseType(in
,t
, unnamed_types
, named_types
);
1050 getLAnglebracket(in
);
1051 getForwardslash(in
);
1052 token
= getName(in
);
1053 if(strcmp("type",token
))in
->error(in
,"not a valid type definition");
1054 getRAnglebracket(in
); //</type>
1057 /**************************************************************************
1059 * getComa, getName, getNumber, getEqual
1061 * Read a token from the input file, check its type, return it scontent.
1064 * in , input file handle.
1067 * address of token content.
1069 **************************************************************************/
1071 char *getName(parse_file_t
* in
)
1075 token
= getToken(in
);
1076 // Optional descriptions
1077 // if(in->type != NAME) in->error(in,"Name token was expected");
1081 int getNumber(parse_file_t
* in
)
1085 token
= getToken(in
);
1086 if(in
->type
!= NUMBER
) in
->error(in
, "Number token was expected");
1090 char *getForwardslash(parse_file_t
* in
)
1094 token
= getToken(in
);
1095 //if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected");
1096 /* Mathieu : final / is optional now. */
1097 if(in
->type
!= FORWARDSLASH
) ungetToken(in
);
1102 char *getLAnglebracket(parse_file_t
* in
)
1106 token
= getToken(in
);
1107 if(in
->type
!= LANGLEBRACKET
) in
->error(in
, "Left angle bracket was expected");
1111 char *getRAnglebracket(parse_file_t
* in
)
1115 token
= getToken(in
);
1116 if(in
->type
!= RANGLEBRACKET
) in
->error(in
, "Right angle bracket was expected");
1120 char *getQuotedString(parse_file_t
* in
)
1124 token
= getToken(in
);
1125 if(in
->type
!= QUOTEDSTRING
) in
->error(in
, "quoted string was expected");
1129 char * getEqual(parse_file_t
*in
)
1133 token
= getToken(in
);
1134 if(in
->type
!= EQUAL
) in
->error(in
, "equal was expected");
1138 int seekNextChar(parse_file_t
*in
)
1141 while((car
= getc(in
->fp
)) != EOF
) {
1150 /******************************************************************
1152 * getToken, ungetToken
1154 * Read a token from the input file and return its type and content.
1155 * Line numbers are accounted for and whitespace/comments are skipped.
1158 * in, input file handle.
1161 * address of token content.
1163 ******************************************************************/
1165 void ungetToken(parse_file_t
* in
)
1170 char *getToken(parse_file_t
* in
)
1174 int pos
= 0, escaped
;
1176 if(in
->unget
== 1) {
1181 /* skip whitespace and comments */
1183 while((car
= getc(fp
)) != EOF
) {
1186 if(car1
== '*') skipComment(in
);
1187 else if(car1
== '/') skipEOL(in
);
1189 car1
= ungetc(car1
,fp
);
1193 else if(car
== '\n') in
->lineno
++;
1194 else if(!isspace(car
)) break;
1202 in
->type
= FORWARDSLASH
;
1203 in
->buffer
[pos
] = car
;
1207 in
->type
= LANGLEBRACKET
;
1208 in
->buffer
[pos
] = car
;
1212 in
->type
= RANGLEBRACKET
;
1213 in
->buffer
[pos
] = car
;
1218 in
->buffer
[pos
] = car
;
1223 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1224 if(car
== '\\' && escaped
== 0) {
1225 in
->buffer
[pos
] = car
;
1230 if(car
== '"' && escaped
== 0) break;
1231 if(car
== '\n' && escaped
== 0) {
1232 in
->error(in
, "non escaped newline inside quoted string");
1234 if(car
== '\n') in
->lineno
++;
1235 in
->buffer
[pos
] = car
;
1239 if(car
== EOF
) in
->error(in
,"no ending quotemark");
1240 if(pos
== BUFFER_SIZE
) in
->error(in
, "quoted string token too large");
1241 in
->type
= QUOTEDSTRING
;
1245 in
->buffer
[pos
] = car
;
1247 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1252 in
->buffer
[pos
] = car
;
1255 if(car
== EOF
) ungetc(car
,fp
);
1256 if(pos
== BUFFER_SIZE
) in
->error(in
, "number token too large");
1259 else if(isalnum(car
) || car
== '_' || car
== '-') {
1260 in
->buffer
[0] = car
;
1262 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1263 if(!(isalnum(car
) || car
== '_' || car
== '-')) {
1267 in
->buffer
[pos
] = car
;
1270 if(car
== EOF
) ungetc(car
,fp
);
1271 if(pos
== BUFFER_SIZE
) in
->error(in
, "name token too large");
1273 } else if(car
== '?') {
1274 in
->buffer
[0] = car
;
1277 else in
->error(in
, "invalid character, unrecognized token");
1279 in
->buffer
[pos
] = 0;
1283 void skipComment(parse_file_t
* in
)
1286 while((car
= getc(in
->fp
)) != EOF
) {
1287 if(car
== '\n') in
->lineno
++;
1288 else if(car
== '*') {
1290 if(car
==EOF
) break;
1291 if(car
== '/') return;
1295 if(car
== EOF
) in
->error(in
,"comment begining with '/*' has no ending '*/'");
1298 void skipEOL(parse_file_t
* in
)
1301 while((car
= getc(in
->fp
)) != EOF
) {
1307 if(car
== EOF
)ungetc(car
, in
->fp
);
1310 /*****************************************************************************
1312 * checkNamedTypesImplemented : check if all named types have definition
1313 ****************************************************************************/
1315 void checkNamedTypesImplemented(table_t
* named_types
)
1317 type_descriptor_t
*t
;
1321 for(pos
= 0 ; pos
< named_types
->values
.position
; pos
++) {
1322 t
= (type_descriptor_t
*) named_types
->values
.array
[pos
];
1323 if(t
->type
== NONE
){
1324 sprintf(str
,"named type '%s' has no definition",
1325 (char*)named_types
->keys
.array
[pos
]);
1326 error_callback(NULL
,str
);
1332 /*****************************************************************************
1334 * generateChecksum : generate checksum for the facility
1336 * facName : name of facility
1338 * checksum : checksum for the facility
1339 ****************************************************************************/
1341 void generateChecksum(char* facName
,
1342 unsigned int * checksum
, sequence_t
* events
)
1349 crc
= crc32(facName
);
1350 for(pos
= 0; pos
< events
->position
; pos
++){
1351 ev
= (event_t
*)(events
->array
[pos
]);
1352 crc
= partial_crc32(ev
->name
, crc
);
1353 for(i
= 0; i
< ev
->fields
.position
; i
++) {
1354 field_t
*f
= (field_t
*)ev
->fields
.array
[i
];
1355 crc
= partial_crc32(f
->name
, crc
);
1356 crc
= getTypeChecksum(crc
, f
->type
);
1362 /*****************************************************************************
1364 * getTypeChecksum : generate checksum by type info
1366 * crc : checksum generated so far
1367 * type : type descriptor containing type info
1369 * unsigned long : checksum
1370 *****************************************************************************/
1372 unsigned long getTypeChecksum(unsigned long aCrc
, type_descriptor_t
* type
)
1374 unsigned long crc
= aCrc
;
1375 char * str
= NULL
, buf
[16];
1381 str
= intOutputTypes
[getSizeindex(type
->size
)];
1384 str
= uintOutputTypes
[getSizeindex(type
->size
)];
1387 str
= allocAndCopy("void *");
1391 str
= allocAndCopy("signed char");
1395 str
= allocAndCopy("unsigned char");
1399 str
= allocAndCopy("short");
1403 str
= allocAndCopy("unsigned short");
1407 str
= allocAndCopy("int");
1411 str
= allocAndCopy("uint");
1415 str
= allocAndCopy("long");
1419 str
= allocAndCopy("unsigned long");
1423 str
= allocAndCopy("size_t");
1427 str
= allocAndCopy("ssize_t");
1431 str
= allocAndCopy("off_t");
1435 str
= floatOutputTypes
[getSizeindex(type
->size
)];
1438 str
= allocAndCopy("string");
1442 //str = appendString("enum ", uintOutputTypes[getSizeindex(type->size)]);
1443 str
= allocAndCopy("enum");
1447 sprintf(buf
,"%zu", type
->size
);
1448 str
= appendString("array ",buf
);
1452 str
= allocAndCopy("sequence ");
1456 str
= allocAndCopy("struct");
1460 str
= allocAndCopy("union");
1464 error_callback(NULL
, "named type has no definition");
1468 crc
= partial_crc32(str
,crc
);
1471 if(type
->fmt
) crc
= partial_crc32(type
->fmt
,crc
);
1473 if(type
->type
== ARRAY
){
1474 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1475 } else if(type
->type
==SEQUENCE
) {
1476 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1477 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[1])->type
);
1478 } else if(type
->type
== STRUCT
|| type
->type
== UNION
){
1479 for(pos
=0; pos
< type
->fields
.position
; pos
++){
1480 fld
= (field_t
*) type
->fields
.array
[pos
];
1481 crc
= partial_crc32(fld
->name
,crc
);
1482 crc
= getTypeChecksum(crc
, fld
->type
);
1484 }else if(type
->type
== ENUM
){
1485 for(pos
= 0; pos
< type
->labels
.position
; pos
++)
1486 crc
= partial_crc32((char*)type
->labels
.array
[pos
],crc
);
1493 /* Event type descriptors */
1494 void freeType(type_descriptor_t
* tp
)
1499 if(tp
->fmt
!= NULL
) free(tp
->fmt
);
1500 if(tp
->type
== ENUM
) {
1501 for(pos2
= 0; pos2
< tp
->labels
.position
; pos2
++) {
1502 free(tp
->labels
.array
[pos2
]);
1504 sequence_dispose(&(tp
->labels
));
1505 for(pos2
= 0; pos2
< tp
->labels_values
.position
; pos2
++) {
1506 free(tp
->labels_values
.array
[pos2
]);
1508 sequence_dispose(&(tp
->labels_values
));
1510 if(tp
->type
== STRUCT
) {
1511 for(pos2
= 0; pos2
< tp
->fields
.position
; pos2
++) {
1512 f
= (field_t
*) tp
->fields
.array
[pos2
];
1514 free(f
->description
);
1517 sequence_dispose(&(tp
->fields
));
1521 void freeNamedType(table_t
* t
)
1524 type_descriptor_t
* td
;
1526 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1527 free((char *)t
->keys
.array
[pos
]);
1528 td
= (type_descriptor_t
*)t
->values
.array
[pos
];
1534 void freeTypes(sequence_t
*t
)
1537 type_descriptor_t
*tp
;
1539 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1540 tp
= (type_descriptor_t
*)t
->array
[pos
];
1546 void freeEvents(sequence_t
*t
)
1551 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1552 ev
= (event_t
*) t
->array
[pos
];
1554 free(ev
->description
);
1555 sequence_dispose(&ev
->fields
);
1562 /* Extensible array */
1564 void sequence_init(sequence_t
*t
)
1568 t
->array
= (void **)memAlloc(t
->size
* sizeof(void *));
1571 void sequence_dispose(sequence_t
*t
)
1578 void sequence_push(sequence_t
*t
, void *elem
)
1582 if(t
->position
>= t
->size
) {
1584 t
->array
= (void **)memAlloc(t
->size
* 2 * sizeof(void *));
1585 memcpy(t
->array
, tmp
, t
->size
* sizeof(void *));
1586 t
->size
= t
->size
* 2;
1589 t
->array
[t
->position
] = elem
;
1593 void *sequence_pop(sequence_t
*t
)
1595 if(t
->position
== 0) printf("Error : trying to pop an empty sequence");
1596 return t
->array
[--t
->position
];
1600 /* Hash table API, implementation is just linear search for now */
1602 void table_init(table_t
*t
)
1604 sequence_init(&(t
->keys
));
1605 sequence_init(&(t
->values
));
1608 void table_dispose(table_t
*t
)
1610 sequence_dispose(&(t
->keys
));
1611 sequence_dispose(&(t
->values
));
1614 void table_insert(table_t
*t
, char *key
, void *value
)
1616 sequence_push(&(t
->keys
),key
);
1617 sequence_push(&(t
->values
),value
);
1620 void *table_find(table_t
*t
, char *key
)
1623 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1624 if(strcmp((char *)key
,(char *)t
->keys
.array
[pos
]) == 0)
1625 return(t
->values
.array
[pos
]);
1630 void table_insert_int(table_t
*t
, int *key
, void *value
)
1632 sequence_push(&(t
->keys
),key
);
1633 sequence_push(&(t
->values
),value
);
1636 void *table_find_int(table_t
*t
, int *key
)
1639 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1640 if(*key
== *(int *)t
->keys
.array
[pos
])
1641 return(t
->values
.array
[pos
]);
1647 /* Concatenate strings */
1649 char *appendString(char *s
, char *suffix
)
1652 if(suffix
== NULL
) return s
;
1654 tmp
= (char *)memAlloc(strlen(s
) + strlen(suffix
) + 1);
This page took 0.064382 seconds and 4 git commands to generate.