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;
275 token
= getToken(in
);
276 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
281 if(!strcmp("name",token
)) {
283 car
= seekNextChar(in
);
284 if(car
== EOF
) in
->error(in
,"name was expected");
285 else if(car
== '\"') ev
->name
= allocAndCopy(getQuotedString(in
));
286 else ev
->name
= allocAndCopy(getName(in
));
287 } else if(!strcmp("scope", token
)) {
289 car
= seekNextChar(in
);
290 if(car
== EOF
) in
->error(in
,"scope was expected");
291 else if(car
== '\"') token
= getQuotedString(in
);
292 else token
= getName(in
);
293 if(!strcmp(token
, "trace")) ev
->per_trace
= 1;
294 else if(!strcmp(token
, "tracefile")) ev
->per_tracefile
= 1;
295 } else if(!strcmp("param", token
)) {
297 car
= seekNextChar(in
);
298 if(car
== EOF
) in
->error(in
,"parameter type was expected");
299 else if(car
== '\"') token
= getQuotedString(in
);
300 else token
= getName(in
);
301 if(!strcmp(token
, "buffer")) ev
->param_buffer
= 1;
302 } else if(!strcmp("attribute", token
)) {
304 car
= seekNextChar(in
);
305 if(car
== EOF
) in
->error(in
,"attribute was expected");
306 else if(car
== '\"') token
= getQuotedString(in
);
307 else token
= getName(in
);
308 if(!strcmp(token
, "no_instrument_function"))
309 ev
->no_instrument_function
= 1;
314 /**************************************************************************
316 * getFacilityAttributes
318 * Read the attribute from the input file.
321 * in , input file handle.
322 * fac , the facility to fill.
324 **************************************************************************/
326 void getFacilityAttributes(parse_file_t
*in
, facility_t
*fac
)
336 token
= getToken(in
);
337 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
342 if(!strcmp("name",token
)) {
344 car
= seekNextChar(in
);
345 if(car
== EOF
) in
->error(in
,"name was expected");
346 else if(car
== '\"') fac
->name
= allocAndCopy(getQuotedString(in
));
347 else fac
->name
= allocAndCopy(getName(in
));
348 if(!strncmp(fac
->name
, "user_", sizeof("user_")-1))
350 } else if(!strcmp("arch", token
)) {
352 car
= seekNextChar(in
);
353 if(car
== '\"') fac
->arch
= allocAndCopy(getQuotedString(in
));
354 else fac
->arch
= allocAndCopy(getName(in
));
359 /**************************************************************************
363 * Read the attribute from the input file.
366 * in , input file handle.
367 * f , the field to fill.
369 **************************************************************************/
371 void getFieldAttributes(parse_file_t
*in
, field_t
*f
)
379 token
= getToken(in
);
380 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
385 if(!strcmp("name",token
)) {
387 car
= seekNextChar(in
);
388 if(car
== EOF
) in
->error(in
,"name was expected");
389 else if(car
== '\"') f
->name
= allocAndCopy(getQuotedString(in
));
390 else f
->name
= allocAndCopy(getName(in
));
395 char *getNameAttribute(parse_file_t
*in
)
402 token
= getToken(in
);
403 if(!strcmp("name",token
)) {
405 car
= seekNextChar(in
);
406 if(car
== EOF
) in
->error(in
,"name was expected");
407 else if(car
== '\"') name
= allocAndCopy(getQuotedString(in
));
408 else name
= allocAndCopy(getName(in
));
415 if(name
== NULL
) in
->error(in
, "Name was expected");
421 //for <label name=label_name value=n format="...">, value is an option
422 //Return value : 0 : no value, 1 : has a value
423 int getValueAttribute(parse_file_t
*in
, long long *value
)
425 char * token
, *token2
;
427 token
= getToken(in
);
429 if(strcmp("/",token
) == 0 || strcmp(">", token
) == 0){
433 if(strcmp("value",token
))in
->error(in
,"value was expected");
436 token
= getToken(in
);
438 if(in
->type
== QUOTEDSTRING
) {
442 if (!isdigit(*token2
)) {
443 in
->type
= QUOTEDSTRING
;
446 } while (*(++token2
) != '\0');
449 if(in
->type
== NUMBER
)
450 *value
= strtoll(token
, NULL
, 0);
455 in
->error(in
,"incorrect size specification");
459 char * getDescription(parse_file_t
*in
)
462 char * token
, car
, *str
;
466 getLAnglebracket(in
);
468 if(strcmp("description",token
)){
469 fseek(in
->fp
, pos
, SEEK_SET
);
473 getRAnglebracket(in
);
476 while((car
= getc(in
->fp
)) != EOF
) {
477 if(car
== '<') break;
478 if(car
== '\0') continue;
479 in
->buffer
[pos
] = car
;
482 if(car
== EOF
)in
->error(in
,"not a valid description");
483 in
->buffer
[pos
] = '\0';
485 str
= allocAndCopy(in
->buffer
);
489 if(strcmp("description", token
))in
->error(in
,"not a valid description");
490 getRAnglebracket(in
);
495 /*****************************************************************************
497 * parseFacility : generate event list
499 * in : input file handle
500 * fac : empty facility
502 * fac : facility filled with event list
503 ****************************************************************************/
505 void parseFacility(parse_file_t
*in
, facility_t
* fac
)
510 getFacilityAttributes(in
, fac
);
511 if(fac
->name
== NULL
) in
->error(in
, "Attribute not named");
513 fac
->capname
= allocAndCopy(fac
->name
);
514 strupper(fac
->capname
);
515 getRAnglebracket(in
);
517 fac
->description
= getDescription(in
);
520 getLAnglebracket(in
);
522 token
= getToken(in
);
523 if(in
->type
== ENDFILE
)
524 in
->error(in
,"the definition of the facility is not finished");
526 if(strcmp("event",token
) == 0){
527 ev
= (event_t
*) memAlloc(sizeof(event_t
));
528 sequence_push(&(fac
->events
),ev
);
529 parseEvent(in
,ev
, &(fac
->unnamed_types
), &(fac
->named_types
));
530 }else if(strcmp("type",token
) == 0){
531 parseTypeDefinition(in
, &(fac
->unnamed_types
), &(fac
->named_types
));
532 }else if(in
->type
== FORWARDSLASH
){
534 }else in
->error(in
,"event or type token expected\n");
538 if(strcmp("facility",token
)) in
->error(in
,"not the end of the facility");
539 getRAnglebracket(in
); //</facility>
542 /*****************************************************************************
544 * parseEvent : generate event from event definition
546 * in : input file handle
548 * unnamed_types : array of unamed types
549 * named_types : array of named types
551 * ev : new event (parameters are passed to it)
552 ****************************************************************************/
554 void parseEvent(parse_file_t
*in
, event_t
* ev
, sequence_t
* unnamed_types
,
555 table_t
* named_types
)
560 sequence_init(&(ev
->fields
));
561 //<event name=eventtype_name>
562 getEventAttributes(in
, ev
);
563 if(ev
->name
== NULL
) in
->error(in
, "Event not named");
564 getRAnglebracket(in
);
566 //<description>...</description>
567 ev
->description
= getDescription(in
);
570 /* Events can have multiple fields. each field form at least a function
571 * parameter of the logging function. */
573 getLAnglebracket(in
);
574 token
= getToken(in
);
577 case FORWARDSLASH
: /* </event> */
579 if(strcmp("event",token
))in
->error(in
,"not an event definition");
580 getRAnglebracket(in
); //</event>
583 case NAME
: /* a field */
584 if(strcmp("field",token
))in
->error(in
,"expecting a field");
585 f
= (field_t
*)memAlloc(sizeof(field_t
));
586 sequence_push(&(ev
->fields
),f
);
587 parseFields(in
, f
, unnamed_types
, named_types
, 1);
590 in
->error(in
, "expecting </event> or <field >");
595 if(in
->type
== FORWARDSLASH
){ //</event> NOTHING
597 }else if(in
->type
== NAME
){
598 if(strcmp("struct",token
)==0 || strcmp("typeref",token
)==0){
600 ev
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
601 if(ev
->type
->type
!= STRUCT
&& ev
->type
->type
!= NONE
)
602 in
->error(in
,"type must be a struct");
603 }else in
->error(in
, "not a valid type");
605 getLAnglebracket(in
);
607 }else in
->error(in
,"not a struct type");
608 getLAnglebracket(in
);
611 if(strcmp("event",token
))in
->error(in
,"not an event definition");
612 getRAnglebracket(in
); //</event>
616 /*****************************************************************************
618 * parseField : get field infomation from buffer
620 * in : input file handle
622 * unnamed_types : array of unamed types
623 * named_types : array of named types
624 * tag : is field surrounded by a <field> </field> tag ?
625 ****************************************************************************/
627 void parseFields(parse_file_t
*in
, field_t
*f
,
628 sequence_t
* unnamed_types
,
629 table_t
* named_types
,
634 //<field name=field_name> <description> <type> </field>
635 getFieldAttributes(in
, f
);
636 if(f
->name
== NULL
) in
->error(in
, "Field not named");
637 getRAnglebracket(in
);
639 f
->description
= getDescription(in
);
641 f
->description
= NULL
;
645 getLAnglebracket(in
);
646 f
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
649 getLAnglebracket(in
);
652 if(strcmp("field",token
))in
->error(in
,"not a valid field definition");
653 getRAnglebracket(in
); //</field>
658 /*****************************************************************************
660 * parseType : get type information, type can be :
662 * int(size,fmt); uint(size,fmt); float(size,fmt);
663 * string(fmt); enum(size,fmt,(label1,label2...))
665 * array(arraySize, type); sequence(lengthSize,type)
666 * struct(field(name,type,description)...)
670 * in : input file handle
671 * inType : a type descriptor
672 * unnamed_types : array of unamed types
673 * named_types : array of named types
675 * type_descriptor* : a type descriptor
676 ****************************************************************************/
678 type_descriptor_t
*parseType(parse_file_t
*in
, type_descriptor_t
*inType
,
679 sequence_t
* unnamed_types
, table_t
* named_types
)
682 type_descriptor_t
*t
;
686 t
= (type_descriptor_t
*) memAlloc(sizeof(type_descriptor_t
));
690 sequence_push(unnamed_types
,t
);
696 if(strcmp(token
,"struct") == 0) {
698 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
699 getRAnglebracket(in
); //<struct>
700 getLAnglebracket(in
); //<field name=..>
701 token
= getToken(in
);
702 sequence_init(&(t
->fields
));
703 while(strcmp("field",token
) == 0){
704 f
= (field_t
*)memAlloc(sizeof(field_t
));
705 sequence_push(&(t
->fields
),f
);
707 parseFields(in
, f
, unnamed_types
, named_types
, 1);
710 getLAnglebracket(in
);
711 token
= getToken(in
);
713 if(strcmp("/",token
))in
->error(in
,"not a valid structure definition");
715 if(strcmp("struct",token
)!=0)
716 in
->error(in
,"not a valid structure definition");
717 getRAnglebracket(in
); //</struct>
719 else if(strcmp(token
,"union") == 0) {
721 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
722 getRAnglebracket(in
); //<union>
724 getLAnglebracket(in
); //<field name=..>
725 token
= getToken(in
);
726 sequence_init(&(t
->fields
));
727 while(strcmp("field",token
) == 0){
728 f
= (field_t
*)memAlloc(sizeof(field_t
));
729 sequence_push(&(t
->fields
),f
);
730 parseFields(in
, f
, unnamed_types
, named_types
, 1);
733 getLAnglebracket(in
);
734 token
= getToken(in
);
736 if(strcmp("/",token
))in
->error(in
,"not a valid union definition");
738 if(strcmp("union",token
)!=0)
739 in
->error(in
,"not a valid union definition");
740 getRAnglebracket(in
); //</union>
742 else if(strcmp(token
,"array") == 0) {
744 sequence_init(&(t
->fields
));
745 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
746 if(t
->size
== 0) in
->error(in
, "Array has empty size");
748 getRAnglebracket(in
); //<array size=n>
750 //getLAnglebracket(in); //<subtype>
752 f
= (field_t
*)memAlloc(sizeof(field_t
));
753 sequence_push(&(t
->fields
),f
);
754 parseFields(in
, f
, unnamed_types
, named_types
, 0);
756 //getLAnglebracket(in); //<type struct>
757 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
759 getLAnglebracket(in
); //</array>
762 if(strcmp("array",token
))in
->error(in
,"not a valid array definition");
763 getRAnglebracket(in
); //</array>
765 else if(strcmp(token
,"sequence") == 0) {
767 sequence_init(&(t
->fields
));
768 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
770 getRAnglebracket(in
); //<sequence>
772 //getLAnglebracket(in); //<sequence size type>
774 f
= (field_t
*)memAlloc(sizeof(field_t
));
775 sequence_push(&(t
->fields
),f
);
776 parseFields(in
, f
, unnamed_types
, named_types
, 0);
778 //getLAnglebracket(in); //<subtype>
780 f
= (field_t
*)memAlloc(sizeof(field_t
));
781 sequence_push(&(t
->fields
),f
);
782 parseFields(in
, f
, unnamed_types
, named_types
, 0);
784 //getLAnglebracket(in); //<type sequence>
785 //t->length_type = parseType(in, NULL, unnamed_types, named_types);
787 //getLAnglebracket(in); //<type sequence>
789 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
791 if(t
->fields
.position
< 1) in
->error(in
, "Sequence has no length type");
792 if(t
->fields
.position
< 2) in
->error(in
, "Sequence has no subtype");
793 switch(((field_t
*)t
->fields
.array
[0])->type
->type
) {
803 in
->error(in
, "Wrong length type for sequence");
806 getLAnglebracket(in
); //</sequence>
809 if(strcmp("sequence",token
))in
->error(in
,"not a valid sequence definition");
810 getRAnglebracket(in
); //</sequence>
812 else if(strcmp(token
,"enum") == 0) {
814 long long value
= -1;
817 sequence_init(&(t
->labels
));
818 sequence_init(&(t
->labels_values
));
819 sequence_init(&(t
->labels_description
));
820 t
->already_printed
= 0;
821 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
822 //if(t->size == 0) in->error(in, "Sequence has empty size");
823 //Mathieu : we fix enum size to target int size. GCC is always like this.
824 //fox copy optimisation.
825 if(t
->size
!= 0) in
->error(in
, "Enum has fixed size of target int.");
827 getRAnglebracket(in
);
829 //<label name=label1 value=n/>
830 getLAnglebracket(in
);
831 token
= getToken(in
); //"label" or "/"
832 while(strcmp("label",token
) == 0){
833 int *label_value
= malloc(sizeof(int));
837 str
= allocAndCopy(getNameAttribute(in
));
838 has_value
= getValueAttribute(in
, &loc_value
);
840 sequence_push(&(t
->labels
),str
);
842 if(has_value
) value
= loc_value
;
845 *label_value
= value
;
846 sequence_push(&(t
->labels_values
), label_value
);
849 getRAnglebracket(in
);
851 //read description if any. May be NULL.
852 str
= allocAndCopy(getDescription(in
));
853 sequence_push(&(t
->labels_description
),str
);
855 //next label definition
856 getLAnglebracket(in
);
857 token
= getToken(in
); //"label" or "/"
859 if(strcmp("/",token
))in
->error(in
, "not a valid enum definition");
861 if(strcmp("enum",token
))in
->error(in
, "not a valid enum definition");
862 getRAnglebracket(in
); //</label>
864 else if(strcmp(token
,"int_fixed") == 0) {
866 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
867 if(t
->size
== 0) in
->error(in
, "int has empty size");
869 getRAnglebracket(in
);
871 else if(strcmp(token
,"uint_fixed") == 0) {
872 t
->type
= UINT_FIXED
;
873 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
874 if(t
->size
== 0) in
->error(in
, "uint has empty size");
876 getRAnglebracket(in
);
878 else if(strcmp(token
,"char") == 0) {
880 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
883 getRAnglebracket(in
);
885 else if(strcmp(token
,"uchar") == 0) {
887 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
890 getRAnglebracket(in
);
892 else if(strcmp(token
,"short") == 0) {
894 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
897 getRAnglebracket(in
);
899 else if(strcmp(token
,"ushort") == 0) {
901 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
904 getRAnglebracket(in
);
906 else if(strcmp(token
,"int") == 0) {
908 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
910 getRAnglebracket(in
);
912 else if(strcmp(token
,"uint") == 0) {
914 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
916 getRAnglebracket(in
);
919 else if(strcmp(token
,"pointer") == 0) {
921 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
923 getRAnglebracket(in
);
925 else if(strcmp(token
,"long") == 0) {
927 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
929 getRAnglebracket(in
);
931 else if(strcmp(token
,"ulong") == 0) {
933 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
935 getRAnglebracket(in
);
937 else if(strcmp(token
,"size_t") == 0) {
939 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
941 getRAnglebracket(in
);
943 else if(strcmp(token
,"ssize_t") == 0) {
945 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
947 getRAnglebracket(in
);
949 else if(strcmp(token
,"off_t") == 0) {
951 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
953 getRAnglebracket(in
);
955 else if(strcmp(token
,"float") == 0) {
957 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
959 getRAnglebracket(in
);
961 else if(strcmp(token
,"string") == 0) {
963 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
965 getRAnglebracket(in
);
967 else if(strcmp(token
,"typeref") == 0){
968 // Must be a named type
970 sequence_pop(unnamed_types
);
971 token
= getNameAttribute(in
);
972 t
= find_named_type(token
, named_types
);
973 if(t
== NULL
) in
->error(in
,"Named referred to must be pre-declared.");
974 getForwardslash(in
); //<typeref name=type_name/>
975 getRAnglebracket(in
);
977 }else in
->error(in
,"not a valid type");
982 /*****************************************************************************
984 * find_named_type : find a named type from hash table
987 * named_types : array of named types
989 * type_descriptor * : a type descriptor
990 *****************************************************************************/
992 type_descriptor_t
* find_named_type(char *name
, table_t
* named_types
)
994 type_descriptor_t
*t
;
996 t
= table_find(named_types
,name
);
1001 type_descriptor_t
* create_named_type(char *name
, table_t
* named_types
)
1003 type_descriptor_t
*t
;
1005 t
= (type_descriptor_t
*)memAlloc(sizeof(type_descriptor_t
));
1006 t
->type_name
= allocAndCopy(name
);
1009 table_insert(named_types
,t
->type_name
,t
);
1010 // table_insert(named_types,allocAndCopy(name),t);
1014 /*****************************************************************************
1016 * parseTypeDefinition : get type information from type definition
1018 * in : input file handle
1019 * unnamed_types : array of unamed types
1020 * named_types : array of named types
1021 *****************************************************************************/
1023 void parseTypeDefinition(parse_file_t
* in
, sequence_t
* unnamed_types
,
1024 table_t
* named_types
)
1027 type_descriptor_t
*t
;
1029 token
= getNameAttribute(in
);
1030 if(token
== NULL
) in
->error(in
, "Type has empty name");
1031 t
= create_named_type(token
, named_types
);
1033 if(t
->type
!= NONE
) in
->error(in
,"redefinition of named type");
1034 getRAnglebracket(in
); //<type name=type_name>
1035 getLAnglebracket(in
); //<
1036 token
= getName(in
);
1037 //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
1039 parseType(in
,t
, unnamed_types
, named_types
);
1042 getLAnglebracket(in
);
1043 getForwardslash(in
);
1044 token
= getName(in
);
1045 if(strcmp("type",token
))in
->error(in
,"not a valid type definition");
1046 getRAnglebracket(in
); //</type>
1049 /**************************************************************************
1051 * getComa, getName, getNumber, getEqual
1053 * Read a token from the input file, check its type, return it scontent.
1056 * in , input file handle.
1059 * address of token content.
1061 **************************************************************************/
1063 char *getName(parse_file_t
* in
)
1067 token
= getToken(in
);
1068 // Optional descriptions
1069 // if(in->type != NAME) in->error(in,"Name token was expected");
1073 int getNumber(parse_file_t
* in
)
1077 token
= getToken(in
);
1078 if(in
->type
!= NUMBER
) in
->error(in
, "Number token was expected");
1082 char *getForwardslash(parse_file_t
* in
)
1086 token
= getToken(in
);
1087 //if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected");
1088 /* Mathieu : final / is optional now. */
1089 if(in
->type
!= FORWARDSLASH
) ungetToken(in
);
1094 char *getLAnglebracket(parse_file_t
* in
)
1098 token
= getToken(in
);
1099 if(in
->type
!= LANGLEBRACKET
) in
->error(in
, "Left angle bracket was expected");
1103 char *getRAnglebracket(parse_file_t
* in
)
1107 token
= getToken(in
);
1108 if(in
->type
!= RANGLEBRACKET
) in
->error(in
, "Right angle bracket was expected");
1112 char *getQuotedString(parse_file_t
* in
)
1116 token
= getToken(in
);
1117 if(in
->type
!= QUOTEDSTRING
) in
->error(in
, "quoted string was expected");
1121 char * getEqual(parse_file_t
*in
)
1125 token
= getToken(in
);
1126 if(in
->type
!= EQUAL
) in
->error(in
, "equal was expected");
1130 char seekNextChar(parse_file_t
*in
)
1133 while((car
= getc(in
->fp
)) != EOF
) {
1142 /******************************************************************
1144 * getToken, ungetToken
1146 * Read a token from the input file and return its type and content.
1147 * Line numbers are accounted for and whitespace/comments are skipped.
1150 * in, input file handle.
1153 * address of token content.
1155 ******************************************************************/
1157 void ungetToken(parse_file_t
* in
)
1162 char *getToken(parse_file_t
* in
)
1166 int pos
= 0, escaped
;
1168 if(in
->unget
== 1) {
1173 /* skip whitespace and comments */
1175 while((car
= getc(fp
)) != EOF
) {
1178 if(car1
== '*') skipComment(in
);
1179 else if(car1
== '/') skipEOL(in
);
1181 car1
= ungetc(car1
,fp
);
1185 else if(car
== '\n') in
->lineno
++;
1186 else if(!isspace(car
)) break;
1194 in
->type
= FORWARDSLASH
;
1195 in
->buffer
[pos
] = car
;
1199 in
->type
= LANGLEBRACKET
;
1200 in
->buffer
[pos
] = car
;
1204 in
->type
= RANGLEBRACKET
;
1205 in
->buffer
[pos
] = car
;
1210 in
->buffer
[pos
] = car
;
1215 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1216 if(car
== '\\' && escaped
== 0) {
1217 in
->buffer
[pos
] = car
;
1222 if(car
== '"' && escaped
== 0) break;
1223 if(car
== '\n' && escaped
== 0) {
1224 in
->error(in
, "non escaped newline inside quoted string");
1226 if(car
== '\n') in
->lineno
++;
1227 in
->buffer
[pos
] = car
;
1231 if(car
== EOF
) in
->error(in
,"no ending quotemark");
1232 if(pos
== BUFFER_SIZE
) in
->error(in
, "quoted string token too large");
1233 in
->type
= QUOTEDSTRING
;
1237 in
->buffer
[pos
] = car
;
1239 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1244 in
->buffer
[pos
] = car
;
1247 if(car
== EOF
) ungetc(car
,fp
);
1248 if(pos
== BUFFER_SIZE
) in
->error(in
, "number token too large");
1251 else if(isalnum(car
) || car
== '_' || car
== '-') {
1252 in
->buffer
[0] = car
;
1254 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1255 if(!(isalnum(car
) || car
== '_' || car
== '-')) {
1259 in
->buffer
[pos
] = car
;
1262 if(car
== EOF
) ungetc(car
,fp
);
1263 if(pos
== BUFFER_SIZE
) in
->error(in
, "name token too large");
1265 } else if(car
== '?') {
1266 in
->buffer
[0] = car
;
1269 else in
->error(in
, "invalid character, unrecognized token");
1271 in
->buffer
[pos
] = 0;
1275 void skipComment(parse_file_t
* in
)
1278 while((car
= getc(in
->fp
)) != EOF
) {
1279 if(car
== '\n') in
->lineno
++;
1280 else if(car
== '*') {
1282 if(car
==EOF
) break;
1283 if(car
== '/') return;
1287 if(car
== EOF
) in
->error(in
,"comment begining with '/*' has no ending '*/'");
1290 void skipEOL(parse_file_t
* in
)
1293 while((car
= getc(in
->fp
)) != EOF
) {
1299 if(car
== EOF
)ungetc(car
, in
->fp
);
1302 /*****************************************************************************
1304 * checkNamedTypesImplemented : check if all named types have definition
1305 ****************************************************************************/
1307 void checkNamedTypesImplemented(table_t
* named_types
)
1309 type_descriptor_t
*t
;
1313 for(pos
= 0 ; pos
< named_types
->values
.position
; pos
++) {
1314 t
= (type_descriptor_t
*) named_types
->values
.array
[pos
];
1315 if(t
->type
== NONE
){
1316 sprintf(str
,"named type '%s' has no definition",
1317 (char*)named_types
->keys
.array
[pos
]);
1318 error_callback(NULL
,str
);
1324 /*****************************************************************************
1326 * generateChecksum : generate checksum for the facility
1328 * facName : name of facility
1330 * checksum : checksum for the facility
1331 ****************************************************************************/
1333 void generateChecksum(char* facName
,
1334 unsigned int * checksum
, sequence_t
* events
)
1341 crc
= crc32(facName
);
1342 for(pos
= 0; pos
< events
->position
; pos
++){
1343 ev
= (event_t
*)(events
->array
[pos
]);
1344 crc
= partial_crc32(ev
->name
, crc
);
1345 for(i
= 0; i
< ev
->fields
.position
; i
++) {
1346 field_t
*f
= (field_t
*)ev
->fields
.array
[i
];
1347 crc
= partial_crc32(f
->name
, crc
);
1348 crc
= getTypeChecksum(crc
, f
->type
);
1354 /*****************************************************************************
1356 * getTypeChecksum : generate checksum by type info
1358 * crc : checksum generated so far
1359 * type : type descriptor containing type info
1361 * unsigned long : checksum
1362 *****************************************************************************/
1364 unsigned long getTypeChecksum(unsigned long aCrc
, type_descriptor_t
* type
)
1366 unsigned long crc
= aCrc
;
1367 char * str
= NULL
, buf
[16];
1373 str
= intOutputTypes
[getSizeindex(type
->size
)];
1376 str
= uintOutputTypes
[getSizeindex(type
->size
)];
1379 str
= allocAndCopy("void *");
1383 str
= allocAndCopy("signed char");
1387 str
= allocAndCopy("unsigned char");
1391 str
= allocAndCopy("short");
1395 str
= allocAndCopy("unsigned short");
1399 str
= allocAndCopy("int");
1403 str
= allocAndCopy("uint");
1407 str
= allocAndCopy("long");
1411 str
= allocAndCopy("unsigned long");
1415 str
= allocAndCopy("size_t");
1419 str
= allocAndCopy("ssize_t");
1423 str
= allocAndCopy("off_t");
1427 str
= floatOutputTypes
[getSizeindex(type
->size
)];
1430 str
= allocAndCopy("string");
1434 //str = appendString("enum ", uintOutputTypes[getSizeindex(type->size)]);
1435 str
= allocAndCopy("enum");
1439 sprintf(buf
,"%zu", type
->size
);
1440 str
= appendString("array ",buf
);
1444 str
= allocAndCopy("sequence ");
1448 str
= allocAndCopy("struct");
1452 str
= allocAndCopy("union");
1456 error_callback(NULL
, "named type has no definition");
1460 crc
= partial_crc32(str
,crc
);
1463 if(type
->fmt
) crc
= partial_crc32(type
->fmt
,crc
);
1465 if(type
->type
== ARRAY
){
1466 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1467 } else if(type
->type
==SEQUENCE
) {
1468 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1469 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[1])->type
);
1470 } else if(type
->type
== STRUCT
|| type
->type
== UNION
){
1471 for(pos
=0; pos
< type
->fields
.position
; pos
++){
1472 fld
= (field_t
*) type
->fields
.array
[pos
];
1473 crc
= partial_crc32(fld
->name
,crc
);
1474 crc
= getTypeChecksum(crc
, fld
->type
);
1476 }else if(type
->type
== ENUM
){
1477 for(pos
= 0; pos
< type
->labels
.position
; pos
++)
1478 crc
= partial_crc32((char*)type
->labels
.array
[pos
],crc
);
1485 /* Event type descriptors */
1486 void freeType(type_descriptor_t
* tp
)
1491 if(tp
->fmt
!= NULL
) free(tp
->fmt
);
1492 if(tp
->type
== ENUM
) {
1493 for(pos2
= 0; pos2
< tp
->labels
.position
; pos2
++) {
1494 free(tp
->labels
.array
[pos2
]);
1496 sequence_dispose(&(tp
->labels
));
1497 for(pos2
= 0; pos2
< tp
->labels_values
.position
; pos2
++) {
1498 free(tp
->labels_values
.array
[pos2
]);
1500 sequence_dispose(&(tp
->labels_values
));
1502 if(tp
->type
== STRUCT
) {
1503 for(pos2
= 0; pos2
< tp
->fields
.position
; pos2
++) {
1504 f
= (field_t
*) tp
->fields
.array
[pos2
];
1506 free(f
->description
);
1509 sequence_dispose(&(tp
->fields
));
1513 void freeNamedType(table_t
* t
)
1516 type_descriptor_t
* td
;
1518 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1519 free((char *)t
->keys
.array
[pos
]);
1520 td
= (type_descriptor_t
*)t
->values
.array
[pos
];
1526 void freeTypes(sequence_t
*t
)
1529 type_descriptor_t
*tp
;
1531 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1532 tp
= (type_descriptor_t
*)t
->array
[pos
];
1538 void freeEvents(sequence_t
*t
)
1543 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1544 ev
= (event_t
*) t
->array
[pos
];
1546 free(ev
->description
);
1547 sequence_dispose(&ev
->fields
);
1554 /* Extensible array */
1556 void sequence_init(sequence_t
*t
)
1560 t
->array
= (void **)memAlloc(t
->size
* sizeof(void *));
1563 void sequence_dispose(sequence_t
*t
)
1570 void sequence_push(sequence_t
*t
, void *elem
)
1574 if(t
->position
>= t
->size
) {
1576 t
->array
= (void **)memAlloc(t
->size
* 2 * sizeof(void *));
1577 memcpy(t
->array
, tmp
, t
->size
* sizeof(void *));
1578 t
->size
= t
->size
* 2;
1581 t
->array
[t
->position
] = elem
;
1585 void *sequence_pop(sequence_t
*t
)
1587 if(t
->position
== 0) printf("Error : trying to pop an empty sequence");
1588 return t
->array
[--t
->position
];
1592 /* Hash table API, implementation is just linear search for now */
1594 void table_init(table_t
*t
)
1596 sequence_init(&(t
->keys
));
1597 sequence_init(&(t
->values
));
1600 void table_dispose(table_t
*t
)
1602 sequence_dispose(&(t
->keys
));
1603 sequence_dispose(&(t
->values
));
1606 void table_insert(table_t
*t
, char *key
, void *value
)
1608 sequence_push(&(t
->keys
),key
);
1609 sequence_push(&(t
->values
),value
);
1612 void *table_find(table_t
*t
, char *key
)
1615 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1616 if(strcmp((char *)key
,(char *)t
->keys
.array
[pos
]) == 0)
1617 return(t
->values
.array
[pos
]);
1622 void table_insert_int(table_t
*t
, int *key
, void *value
)
1624 sequence_push(&(t
->keys
),key
);
1625 sequence_push(&(t
->values
),value
);
1628 void *table_find_int(table_t
*t
, int *key
)
1631 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1632 if(*key
== *(int *)t
->keys
.array
[pos
])
1633 return(t
->values
.array
[pos
]);
1639 /* Concatenate strings */
1641 char *appendString(char *s
, char *suffix
)
1644 if(suffix
== NULL
) return s
;
1646 tmp
= (char *)memAlloc(strlen(s
) + strlen(suffix
) + 1);
This page took 0.063893 seconds and 4 git commands to generate.