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
)
467 getLAnglebracket(in
);
469 if(strcmp("description",token
)){
470 fseek(in
->fp
, pos
, SEEK_SET
);
474 getRAnglebracket(in
);
477 while((car
= getc(in
->fp
)) != EOF
) {
478 if(car
== '<') break;
479 if(car
== '\0') continue;
480 in
->buffer
[pos
] = car
;
483 if(car
== EOF
)in
->error(in
,"not a valid description");
484 in
->buffer
[pos
] = '\0';
486 str
= allocAndCopy(in
->buffer
);
490 if(strcmp("description", token
))in
->error(in
,"not a valid description");
491 getRAnglebracket(in
);
496 /*****************************************************************************
498 * parseFacility : generate event list
500 * in : input file handle
501 * fac : empty facility
503 * fac : facility filled with event list
504 ****************************************************************************/
506 void parseFacility(parse_file_t
*in
, facility_t
* fac
)
511 getFacilityAttributes(in
, fac
);
512 if(fac
->name
== NULL
) in
->error(in
, "Attribute not named");
514 fac
->capname
= allocAndCopy(fac
->name
);
515 strupper(fac
->capname
);
516 getRAnglebracket(in
);
518 fac
->description
= getDescription(in
);
521 getLAnglebracket(in
);
523 token
= getToken(in
);
524 if(in
->type
== ENDFILE
)
525 in
->error(in
,"the definition of the facility is not finished");
527 if(strcmp("event",token
) == 0){
528 ev
= (event_t
*) memAlloc(sizeof(event_t
));
529 sequence_push(&(fac
->events
),ev
);
530 parseEvent(in
,ev
, &(fac
->unnamed_types
), &(fac
->named_types
));
531 }else if(strcmp("type",token
) == 0){
532 parseTypeDefinition(in
, &(fac
->unnamed_types
), &(fac
->named_types
));
533 }else if(in
->type
== FORWARDSLASH
){
535 }else in
->error(in
,"event or type token expected\n");
539 if(strcmp("facility",token
)) in
->error(in
,"not the end of the facility");
540 getRAnglebracket(in
); //</facility>
543 /*****************************************************************************
545 * parseEvent : generate event from event definition
547 * in : input file handle
549 * unnamed_types : array of unamed types
550 * named_types : array of named types
552 * ev : new event (parameters are passed to it)
553 ****************************************************************************/
555 void parseEvent(parse_file_t
*in
, event_t
* ev
, sequence_t
* unnamed_types
,
556 table_t
* named_types
)
561 sequence_init(&(ev
->fields
));
562 //<event name=eventtype_name>
563 getEventAttributes(in
, ev
);
564 if(ev
->name
== NULL
) in
->error(in
, "Event not named");
565 getRAnglebracket(in
);
567 //<description>...</description>
568 ev
->description
= getDescription(in
);
571 /* Events can have multiple fields. each field form at least a function
572 * parameter of the logging function. */
574 getLAnglebracket(in
);
575 token
= getToken(in
);
578 case FORWARDSLASH
: /* </event> */
580 if(strcmp("event",token
))in
->error(in
,"not an event definition");
581 getRAnglebracket(in
); //</event>
584 case NAME
: /* a field */
585 if(strcmp("field",token
))in
->error(in
,"expecting a field");
586 f
= (field_t
*)memAlloc(sizeof(field_t
));
587 sequence_push(&(ev
->fields
),f
);
588 parseFields(in
, f
, unnamed_types
, named_types
, 1);
591 in
->error(in
, "expecting </event> or <field >");
596 if(in
->type
== FORWARDSLASH
){ //</event> NOTHING
598 }else if(in
->type
== NAME
){
599 if(strcmp("struct",token
)==0 || strcmp("typeref",token
)==0){
601 ev
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
602 if(ev
->type
->type
!= STRUCT
&& ev
->type
->type
!= NONE
)
603 in
->error(in
,"type must be a struct");
604 }else in
->error(in
, "not a valid type");
606 getLAnglebracket(in
);
608 }else in
->error(in
,"not a struct type");
609 getLAnglebracket(in
);
612 if(strcmp("event",token
))in
->error(in
,"not an event definition");
613 getRAnglebracket(in
); //</event>
617 /*****************************************************************************
619 * parseField : get field infomation from buffer
621 * in : input file handle
623 * unnamed_types : array of unamed types
624 * named_types : array of named types
625 * tag : is field surrounded by a <field> </field> tag ?
626 ****************************************************************************/
628 void parseFields(parse_file_t
*in
, field_t
*f
,
629 sequence_t
* unnamed_types
,
630 table_t
* named_types
,
635 //<field name=field_name> <description> <type> </field>
636 getFieldAttributes(in
, f
);
637 if(f
->name
== NULL
) in
->error(in
, "Field not named");
638 getRAnglebracket(in
);
640 f
->description
= getDescription(in
);
642 f
->description
= NULL
;
646 getLAnglebracket(in
);
647 f
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
650 getLAnglebracket(in
);
653 if(strcmp("field",token
))in
->error(in
,"not a valid field definition");
654 getRAnglebracket(in
); //</field>
659 /*****************************************************************************
661 * parseType : get type information, type can be :
663 * int(size,fmt); uint(size,fmt); float(size,fmt);
664 * string(fmt); enum(size,fmt,(label1,label2...))
666 * array(arraySize, type); sequence(lengthSize,type)
667 * struct(field(name,type,description)...)
671 * in : input file handle
672 * inType : a type descriptor
673 * unnamed_types : array of unamed types
674 * named_types : array of named types
676 * type_descriptor* : a type descriptor
677 ****************************************************************************/
679 type_descriptor_t
*parseType(parse_file_t
*in
, type_descriptor_t
*inType
,
680 sequence_t
* unnamed_types
, table_t
* named_types
)
683 type_descriptor_t
*t
;
687 t
= (type_descriptor_t
*) memAlloc(sizeof(type_descriptor_t
));
691 sequence_push(unnamed_types
,t
);
697 if(strcmp(token
,"struct") == 0) {
699 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
700 getRAnglebracket(in
); //<struct>
701 getLAnglebracket(in
); //<field name=..>
702 token
= getToken(in
);
703 sequence_init(&(t
->fields
));
704 while(strcmp("field",token
) == 0){
705 f
= (field_t
*)memAlloc(sizeof(field_t
));
706 sequence_push(&(t
->fields
),f
);
708 parseFields(in
, f
, unnamed_types
, named_types
, 1);
711 getLAnglebracket(in
);
712 token
= getToken(in
);
714 if(strcmp("/",token
))in
->error(in
,"not a valid structure definition");
716 if(strcmp("struct",token
)!=0)
717 in
->error(in
,"not a valid structure definition");
718 getRAnglebracket(in
); //</struct>
720 else if(strcmp(token
,"union") == 0) {
722 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
723 getRAnglebracket(in
); //<union>
725 getLAnglebracket(in
); //<field name=..>
726 token
= getToken(in
);
727 sequence_init(&(t
->fields
));
728 while(strcmp("field",token
) == 0){
729 f
= (field_t
*)memAlloc(sizeof(field_t
));
730 sequence_push(&(t
->fields
),f
);
731 parseFields(in
, f
, unnamed_types
, named_types
, 1);
734 getLAnglebracket(in
);
735 token
= getToken(in
);
737 if(strcmp("/",token
))in
->error(in
,"not a valid union definition");
739 if(strcmp("union",token
)!=0)
740 in
->error(in
,"not a valid union definition");
741 getRAnglebracket(in
); //</union>
743 else if(strcmp(token
,"array") == 0) {
745 sequence_init(&(t
->fields
));
746 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
747 if(t
->size
== 0) in
->error(in
, "Array has empty size");
749 getRAnglebracket(in
); //<array size=n>
751 //getLAnglebracket(in); //<subtype>
753 f
= (field_t
*)memAlloc(sizeof(field_t
));
754 sequence_push(&(t
->fields
),f
);
755 parseFields(in
, f
, unnamed_types
, named_types
, 0);
757 //getLAnglebracket(in); //<type struct>
758 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
760 getLAnglebracket(in
); //</array>
763 if(strcmp("array",token
))in
->error(in
,"not a valid array definition");
764 getRAnglebracket(in
); //</array>
766 else if(strcmp(token
,"sequence") == 0) {
768 sequence_init(&(t
->fields
));
769 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
771 getRAnglebracket(in
); //<sequence>
773 //getLAnglebracket(in); //<sequence size type>
775 f
= (field_t
*)memAlloc(sizeof(field_t
));
776 sequence_push(&(t
->fields
),f
);
777 parseFields(in
, f
, unnamed_types
, named_types
, 0);
779 //getLAnglebracket(in); //<subtype>
781 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); //<type sequence>
786 //t->length_type = parseType(in, NULL, unnamed_types, named_types);
788 //getLAnglebracket(in); //<type sequence>
790 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
792 if(t
->fields
.position
< 1) in
->error(in
, "Sequence has no length type");
793 if(t
->fields
.position
< 2) in
->error(in
, "Sequence has no subtype");
794 switch(((field_t
*)t
->fields
.array
[0])->type
->type
) {
804 in
->error(in
, "Wrong length type for sequence");
807 getLAnglebracket(in
); //</sequence>
810 if(strcmp("sequence",token
))in
->error(in
,"not a valid sequence definition");
811 getRAnglebracket(in
); //</sequence>
813 else if(strcmp(token
,"enum") == 0) {
815 long long value
= -1;
818 sequence_init(&(t
->labels
));
819 sequence_init(&(t
->labels_values
));
820 sequence_init(&(t
->labels_description
));
821 t
->already_printed
= 0;
822 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
823 //if(t->size == 0) in->error(in, "Sequence has empty size");
824 //Mathieu : we fix enum size to target int size. GCC is always like this.
825 //fox copy optimisation.
826 if(t
->size
!= 0) in
->error(in
, "Enum has fixed size of target int.");
828 getRAnglebracket(in
);
830 //<label name=label1 value=n/>
831 getLAnglebracket(in
);
832 token
= getToken(in
); //"label" or "/"
833 while(strcmp("label",token
) == 0){
834 int *label_value
= malloc(sizeof(int));
838 str
= allocAndCopy(getNameAttribute(in
));
839 has_value
= getValueAttribute(in
, &loc_value
);
841 sequence_push(&(t
->labels
),str
);
843 if(has_value
) value
= loc_value
;
846 *label_value
= value
;
847 sequence_push(&(t
->labels_values
), label_value
);
850 getRAnglebracket(in
);
852 //read description if any. May be NULL.
853 str
= allocAndCopy(getDescription(in
));
854 sequence_push(&(t
->labels_description
),str
);
856 //next label definition
857 getLAnglebracket(in
);
858 token
= getToken(in
); //"label" or "/"
860 if(strcmp("/",token
))in
->error(in
, "not a valid enum definition");
862 if(strcmp("enum",token
))in
->error(in
, "not a valid enum definition");
863 getRAnglebracket(in
); //</label>
865 else if(strcmp(token
,"int_fixed") == 0) {
867 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
868 if(t
->size
== 0) in
->error(in
, "int has empty size");
870 getRAnglebracket(in
);
872 else if(strcmp(token
,"uint_fixed") == 0) {
873 t
->type
= UINT_FIXED
;
874 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
875 if(t
->size
== 0) in
->error(in
, "uint has empty size");
877 getRAnglebracket(in
);
879 else if(strcmp(token
,"char") == 0) {
881 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
884 getRAnglebracket(in
);
886 else if(strcmp(token
,"uchar") == 0) {
888 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
891 getRAnglebracket(in
);
893 else if(strcmp(token
,"short") == 0) {
895 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
898 getRAnglebracket(in
);
900 else if(strcmp(token
,"ushort") == 0) {
902 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
905 getRAnglebracket(in
);
907 else if(strcmp(token
,"int") == 0) {
909 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
911 getRAnglebracket(in
);
913 else if(strcmp(token
,"uint") == 0) {
915 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
917 getRAnglebracket(in
);
920 else if(strcmp(token
,"pointer") == 0) {
922 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
924 getRAnglebracket(in
);
926 else if(strcmp(token
,"long") == 0) {
928 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
930 getRAnglebracket(in
);
932 else if(strcmp(token
,"ulong") == 0) {
934 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
936 getRAnglebracket(in
);
938 else if(strcmp(token
,"size_t") == 0) {
940 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
942 getRAnglebracket(in
);
944 else if(strcmp(token
,"ssize_t") == 0) {
946 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
948 getRAnglebracket(in
);
950 else if(strcmp(token
,"off_t") == 0) {
952 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
954 getRAnglebracket(in
);
956 else if(strcmp(token
,"float") == 0) {
958 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
960 getRAnglebracket(in
);
962 else if(strcmp(token
,"string") == 0) {
964 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
966 getRAnglebracket(in
);
968 else if(strcmp(token
,"typeref") == 0){
969 // Must be a named type
971 sequence_pop(unnamed_types
);
972 token
= getNameAttribute(in
);
973 t
= find_named_type(token
, named_types
);
974 if(t
== NULL
) in
->error(in
,"Named referred to must be pre-declared.");
975 getForwardslash(in
); //<typeref name=type_name/>
976 getRAnglebracket(in
);
978 }else in
->error(in
,"not a valid type");
983 /*****************************************************************************
985 * find_named_type : find a named type from hash table
988 * named_types : array of named types
990 * type_descriptor * : a type descriptor
991 *****************************************************************************/
993 type_descriptor_t
* find_named_type(char *name
, table_t
* named_types
)
995 type_descriptor_t
*t
;
997 t
= table_find(named_types
,name
);
1002 type_descriptor_t
* create_named_type(char *name
, table_t
* named_types
)
1004 type_descriptor_t
*t
;
1006 t
= (type_descriptor_t
*)memAlloc(sizeof(type_descriptor_t
));
1007 t
->type_name
= allocAndCopy(name
);
1010 table_insert(named_types
,t
->type_name
,t
);
1011 // table_insert(named_types,allocAndCopy(name),t);
1015 /*****************************************************************************
1017 * parseTypeDefinition : get type information from type definition
1019 * in : input file handle
1020 * unnamed_types : array of unamed types
1021 * named_types : array of named types
1022 *****************************************************************************/
1024 void parseTypeDefinition(parse_file_t
* in
, sequence_t
* unnamed_types
,
1025 table_t
* named_types
)
1028 type_descriptor_t
*t
;
1030 token
= getNameAttribute(in
);
1031 if(token
== NULL
) in
->error(in
, "Type has empty name");
1032 t
= create_named_type(token
, named_types
);
1034 if(t
->type
!= NONE
) in
->error(in
,"redefinition of named type");
1035 getRAnglebracket(in
); //<type name=type_name>
1036 getLAnglebracket(in
); //<
1037 token
= getName(in
);
1038 //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
1040 parseType(in
,t
, unnamed_types
, named_types
);
1043 getLAnglebracket(in
);
1044 getForwardslash(in
);
1045 token
= getName(in
);
1046 if(strcmp("type",token
))in
->error(in
,"not a valid type definition");
1047 getRAnglebracket(in
); //</type>
1050 /**************************************************************************
1052 * getComa, getName, getNumber, getEqual
1054 * Read a token from the input file, check its type, return it scontent.
1057 * in , input file handle.
1060 * address of token content.
1062 **************************************************************************/
1064 char *getName(parse_file_t
* in
)
1068 token
= getToken(in
);
1069 // Optional descriptions
1070 // if(in->type != NAME) in->error(in,"Name token was expected");
1074 int getNumber(parse_file_t
* in
)
1078 token
= getToken(in
);
1079 if(in
->type
!= NUMBER
) in
->error(in
, "Number token was expected");
1083 char *getForwardslash(parse_file_t
* in
)
1087 token
= getToken(in
);
1088 //if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected");
1089 /* Mathieu : final / is optional now. */
1090 if(in
->type
!= FORWARDSLASH
) ungetToken(in
);
1095 char *getLAnglebracket(parse_file_t
* in
)
1099 token
= getToken(in
);
1100 if(in
->type
!= LANGLEBRACKET
) in
->error(in
, "Left angle bracket was expected");
1104 char *getRAnglebracket(parse_file_t
* in
)
1108 token
= getToken(in
);
1109 if(in
->type
!= RANGLEBRACKET
) in
->error(in
, "Right angle bracket was expected");
1113 char *getQuotedString(parse_file_t
* in
)
1117 token
= getToken(in
);
1118 if(in
->type
!= QUOTEDSTRING
) in
->error(in
, "quoted string was expected");
1122 char * getEqual(parse_file_t
*in
)
1126 token
= getToken(in
);
1127 if(in
->type
!= EQUAL
) in
->error(in
, "equal was expected");
1131 int seekNextChar(parse_file_t
*in
)
1134 while((car
= getc(in
->fp
)) != EOF
) {
1143 /******************************************************************
1145 * getToken, ungetToken
1147 * Read a token from the input file and return its type and content.
1148 * Line numbers are accounted for and whitespace/comments are skipped.
1151 * in, input file handle.
1154 * address of token content.
1156 ******************************************************************/
1158 void ungetToken(parse_file_t
* in
)
1163 char *getToken(parse_file_t
* in
)
1167 int pos
= 0, escaped
;
1169 if(in
->unget
== 1) {
1174 /* skip whitespace and comments */
1176 while((car
= getc(fp
)) != EOF
) {
1179 if(car1
== '*') skipComment(in
);
1180 else if(car1
== '/') skipEOL(in
);
1182 car1
= ungetc(car1
,fp
);
1186 else if(car
== '\n') in
->lineno
++;
1187 else if(!isspace(car
)) break;
1195 in
->type
= FORWARDSLASH
;
1196 in
->buffer
[pos
] = car
;
1200 in
->type
= LANGLEBRACKET
;
1201 in
->buffer
[pos
] = car
;
1205 in
->type
= RANGLEBRACKET
;
1206 in
->buffer
[pos
] = car
;
1211 in
->buffer
[pos
] = car
;
1216 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1217 if(car
== '\\' && escaped
== 0) {
1218 in
->buffer
[pos
] = car
;
1223 if(car
== '"' && escaped
== 0) break;
1224 if(car
== '\n' && escaped
== 0) {
1225 in
->error(in
, "non escaped newline inside quoted string");
1227 if(car
== '\n') in
->lineno
++;
1228 in
->buffer
[pos
] = car
;
1232 if(car
== EOF
) in
->error(in
,"no ending quotemark");
1233 if(pos
== BUFFER_SIZE
) in
->error(in
, "quoted string token too large");
1234 in
->type
= QUOTEDSTRING
;
1238 in
->buffer
[pos
] = car
;
1240 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1245 in
->buffer
[pos
] = car
;
1248 if(car
== EOF
) ungetc(car
,fp
);
1249 if(pos
== BUFFER_SIZE
) in
->error(in
, "number token too large");
1252 else if(isalnum(car
) || car
== '_' || car
== '-') {
1253 in
->buffer
[0] = car
;
1255 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1256 if(!(isalnum(car
) || car
== '_' || car
== '-')) {
1260 in
->buffer
[pos
] = car
;
1263 if(car
== EOF
) ungetc(car
,fp
);
1264 if(pos
== BUFFER_SIZE
) in
->error(in
, "name token too large");
1266 } else if(car
== '?') {
1267 in
->buffer
[0] = car
;
1270 else in
->error(in
, "invalid character, unrecognized token");
1272 in
->buffer
[pos
] = 0;
1276 void skipComment(parse_file_t
* in
)
1279 while((car
= getc(in
->fp
)) != EOF
) {
1280 if(car
== '\n') in
->lineno
++;
1281 else if(car
== '*') {
1283 if(car
==EOF
) break;
1284 if(car
== '/') return;
1288 if(car
== EOF
) in
->error(in
,"comment begining with '/*' has no ending '*/'");
1291 void skipEOL(parse_file_t
* in
)
1294 while((car
= getc(in
->fp
)) != EOF
) {
1300 if(car
== EOF
)ungetc(car
, in
->fp
);
1303 /*****************************************************************************
1305 * checkNamedTypesImplemented : check if all named types have definition
1306 ****************************************************************************/
1308 void checkNamedTypesImplemented(table_t
* named_types
)
1310 type_descriptor_t
*t
;
1314 for(pos
= 0 ; pos
< named_types
->values
.position
; pos
++) {
1315 t
= (type_descriptor_t
*) named_types
->values
.array
[pos
];
1316 if(t
->type
== NONE
){
1317 sprintf(str
,"named type '%s' has no definition",
1318 (char*)named_types
->keys
.array
[pos
]);
1319 error_callback(NULL
,str
);
1325 /*****************************************************************************
1327 * generateChecksum : generate checksum for the facility
1329 * facName : name of facility
1331 * checksum : checksum for the facility
1332 ****************************************************************************/
1334 void generateChecksum(char* facName
,
1335 unsigned int * checksum
, sequence_t
* events
)
1342 crc
= crc32(facName
);
1343 for(pos
= 0; pos
< events
->position
; pos
++){
1344 ev
= (event_t
*)(events
->array
[pos
]);
1345 crc
= partial_crc32(ev
->name
, crc
);
1346 for(i
= 0; i
< ev
->fields
.position
; i
++) {
1347 field_t
*f
= (field_t
*)ev
->fields
.array
[i
];
1348 crc
= partial_crc32(f
->name
, crc
);
1349 crc
= getTypeChecksum(crc
, f
->type
);
1355 /*****************************************************************************
1357 * getTypeChecksum : generate checksum by type info
1359 * crc : checksum generated so far
1360 * type : type descriptor containing type info
1362 * unsigned long : checksum
1363 *****************************************************************************/
1365 unsigned long getTypeChecksum(unsigned long aCrc
, type_descriptor_t
* type
)
1367 unsigned long crc
= aCrc
;
1368 char * str
= NULL
, buf
[16];
1374 str
= intOutputTypes
[getSizeindex(type
->size
)];
1377 str
= uintOutputTypes
[getSizeindex(type
->size
)];
1380 str
= allocAndCopy("void *");
1384 str
= allocAndCopy("signed char");
1388 str
= allocAndCopy("unsigned char");
1392 str
= allocAndCopy("short");
1396 str
= allocAndCopy("unsigned short");
1400 str
= allocAndCopy("int");
1404 str
= allocAndCopy("uint");
1408 str
= allocAndCopy("long");
1412 str
= allocAndCopy("unsigned long");
1416 str
= allocAndCopy("size_t");
1420 str
= allocAndCopy("ssize_t");
1424 str
= allocAndCopy("off_t");
1428 str
= floatOutputTypes
[getSizeindex(type
->size
)];
1431 str
= allocAndCopy("string");
1435 //str = appendString("enum ", uintOutputTypes[getSizeindex(type->size)]);
1436 str
= allocAndCopy("enum");
1440 sprintf(buf
,"%zu", type
->size
);
1441 str
= appendString("array ",buf
);
1445 str
= allocAndCopy("sequence ");
1449 str
= allocAndCopy("struct");
1453 str
= allocAndCopy("union");
1457 error_callback(NULL
, "named type has no definition");
1461 crc
= partial_crc32(str
,crc
);
1464 if(type
->fmt
) crc
= partial_crc32(type
->fmt
,crc
);
1466 if(type
->type
== ARRAY
){
1467 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1468 } else if(type
->type
==SEQUENCE
) {
1469 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1470 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[1])->type
);
1471 } else if(type
->type
== STRUCT
|| type
->type
== UNION
){
1472 for(pos
=0; pos
< type
->fields
.position
; pos
++){
1473 fld
= (field_t
*) type
->fields
.array
[pos
];
1474 crc
= partial_crc32(fld
->name
,crc
);
1475 crc
= getTypeChecksum(crc
, fld
->type
);
1477 }else if(type
->type
== ENUM
){
1478 for(pos
= 0; pos
< type
->labels
.position
; pos
++)
1479 crc
= partial_crc32((char*)type
->labels
.array
[pos
],crc
);
1486 /* Event type descriptors */
1487 void freeType(type_descriptor_t
* tp
)
1492 if(tp
->fmt
!= NULL
) free(tp
->fmt
);
1493 if(tp
->type
== ENUM
) {
1494 for(pos2
= 0; pos2
< tp
->labels
.position
; pos2
++) {
1495 free(tp
->labels
.array
[pos2
]);
1497 sequence_dispose(&(tp
->labels
));
1498 for(pos2
= 0; pos2
< tp
->labels_values
.position
; pos2
++) {
1499 free(tp
->labels_values
.array
[pos2
]);
1501 sequence_dispose(&(tp
->labels_values
));
1503 if(tp
->type
== STRUCT
) {
1504 for(pos2
= 0; pos2
< tp
->fields
.position
; pos2
++) {
1505 f
= (field_t
*) tp
->fields
.array
[pos2
];
1507 free(f
->description
);
1510 sequence_dispose(&(tp
->fields
));
1514 void freeNamedType(table_t
* t
)
1517 type_descriptor_t
* td
;
1519 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1520 free((char *)t
->keys
.array
[pos
]);
1521 td
= (type_descriptor_t
*)t
->values
.array
[pos
];
1527 void freeTypes(sequence_t
*t
)
1530 type_descriptor_t
*tp
;
1532 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1533 tp
= (type_descriptor_t
*)t
->array
[pos
];
1539 void freeEvents(sequence_t
*t
)
1544 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1545 ev
= (event_t
*) t
->array
[pos
];
1547 free(ev
->description
);
1548 sequence_dispose(&ev
->fields
);
1555 /* Extensible array */
1557 void sequence_init(sequence_t
*t
)
1561 t
->array
= (void **)memAlloc(t
->size
* sizeof(void *));
1564 void sequence_dispose(sequence_t
*t
)
1571 void sequence_push(sequence_t
*t
, void *elem
)
1575 if(t
->position
>= t
->size
) {
1577 t
->array
= (void **)memAlloc(t
->size
* 2 * sizeof(void *));
1578 memcpy(t
->array
, tmp
, t
->size
* sizeof(void *));
1579 t
->size
= t
->size
* 2;
1582 t
->array
[t
->position
] = elem
;
1586 void *sequence_pop(sequence_t
*t
)
1588 if(t
->position
== 0) printf("Error : trying to pop an empty sequence");
1589 return t
->array
[--t
->position
];
1593 /* Hash table API, implementation is just linear search for now */
1595 void table_init(table_t
*t
)
1597 sequence_init(&(t
->keys
));
1598 sequence_init(&(t
->values
));
1601 void table_dispose(table_t
*t
)
1603 sequence_dispose(&(t
->keys
));
1604 sequence_dispose(&(t
->values
));
1607 void table_insert(table_t
*t
, char *key
, void *value
)
1609 sequence_push(&(t
->keys
),key
);
1610 sequence_push(&(t
->values
),value
);
1613 void *table_find(table_t
*t
, char *key
)
1616 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1617 if(strcmp((char *)key
,(char *)t
->keys
.array
[pos
]) == 0)
1618 return(t
->values
.array
[pos
]);
1623 void table_insert_int(table_t
*t
, int *key
, void *value
)
1625 sequence_push(&(t
->keys
),key
);
1626 sequence_push(&(t
->values
),value
);
1629 void *table_find_int(table_t
*t
, int *key
)
1632 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1633 if(*key
== *(int *)t
->keys
.array
[pos
])
1634 return(t
->values
.array
[pos
]);
1640 /* Concatenate strings */
1642 char *appendString(char *s
, char *suffix
)
1645 if(suffix
== NULL
) return s
;
1647 tmp
= (char *)memAlloc(strlen(s
) + strlen(suffix
) + 1);
This page took 0.063174 seconds and 4 git commands to generate.