3 parser.c: Generate helper declarations and functions to trace events
4 from an event description file.
6 Copyright (C) 2002, Xianxiu Yang
7 Copyright (C) 2002, Michel Dagenais
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* This program reads the ".xml" event definitions input files
23 and constructs structure for each event.
25 The program uses a very simple tokenizer, called from a hand written
26 recursive descent parser to fill a data structure describing the events.
27 The result is a sequence of events definitions which refer to type
30 A table of named types is maintained to allow refering to types by name
31 when the same type is used at several places. Finally a sequence of
32 all types is maintained to facilitate the freeing of all type
33 information when the processing of an ".xml" file is finished. */
39 #include <linux/errno.h>
47 void strupper(char *string
)
58 int getSizeindex(int value
)
70 printf("Error : unknown value size %d\n", value
);
75 /*****************************************************************************
77 * getSize : translate from string to integer
79 * in : input file handle
82 *****************************************************************************/
84 int getSize(parse_file
*in
)
89 if(in
->type
== NUMBER
) {
90 if(strcmp(token
,"1") == 0) return 0;
91 else if(strcmp(token
,"2") == 0) return 1;
92 else if(strcmp(token
,"4") == 0) return 2;
93 else if(strcmp(token
,"8") == 0) return 3;
95 else if(in
->type
== NAME
) {
96 if(strcmp(token
,"short") == 0) return 4;
97 else if(strcmp(token
,"medium") == 0) return 5;
98 else if(strcmp(token
,"long") == 0) return 6;
100 in
->error(in
,"incorrect size specification");
104 /*****************************************************************************
106 * error_callback : print out error info
108 * in : input file handle
109 * msg : message to be printed
110 ****************************************************************************/
112 void error_callback(parse_file
*in
, char *msg
)
115 printf("Error in file %s, line %d: %s\n", in
->name
, in
->lineno
, msg
);
122 /*****************************************************************************
124 * memAlloc : allocate memory
126 * size : required memory size
128 * void * : pointer to allocate memory or NULL
129 ****************************************************************************/
131 void * memAlloc(int size
)
134 if(size
== 0) return NULL
;
137 printf("Failed to allocate memory");
143 /*****************************************************************************
145 * allocAndCopy : allocate memory and initialize it
147 * str : string to be put in memory
149 * char * : pointer to allocate memory or NULL
150 ****************************************************************************/
152 char *allocAndCopy(char *str
)
155 if(str
== NULL
) return NULL
;
156 addr
= (char *)memAlloc(strlen(str
)+1);
161 /**************************************************************************
165 * Read the attribute from the input file.
168 * in , input file handle.
169 * t , the type descriptor to fill.
171 **************************************************************************/
173 void getTypeAttributes(parse_file
*in
, type_descriptor
*t
)
183 token
= getToken(in
);
184 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
189 if(!strcmp("format",token
)) {
191 t
->fmt
= allocAndCopy(getQuotedString(in
));
192 //} else if(!strcmp("name",token)) {
194 // car = seekNextChar(in);
195 // if(car == EOF) in->error(in,"name was expected");
196 // else if(car == '\"') t->type_name = allocAndCopy(getQuotedString(in));
197 // else t->type_name = allocAndCopy(getName(in));
198 } else if(!strcmp("size",token
)) {
200 t
->size
= getSize(in
);
201 } else if(!strcmp("align",token
)) {
203 t
->alignment
= getNumber(in
);
208 /**************************************************************************
212 * Read the attribute from the input file.
215 * in , input file handle.
216 * ev , the event to fill.
218 **************************************************************************/
220 void getEventAttributes(parse_file
*in
, event
*ev
)
227 ev
->per_tracefile
= 0;
230 token
= getToken(in
);
231 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
236 if(!strcmp("name",token
)) {
238 car
= seekNextChar(in
);
239 if(car
== EOF
) in
->error(in
,"name was expected");
240 else if(car
== '\"') ev
->name
= allocAndCopy(getQuotedString(in
));
241 else ev
->name
= allocAndCopy(getName(in
));
242 } else if(!strcmp("per_trace", token
)) {
244 } else if(!strcmp("per_tracefile", token
)) {
245 ev
->per_tracefile
= 1;
251 /**************************************************************************
253 * getFacilityAttributes
255 * Read the attribute from the input file.
258 * in , input file handle.
259 * fac , the facility to fill.
261 **************************************************************************/
263 void getFacilityAttributes(parse_file
*in
, facility
*fac
)
271 token
= getToken(in
);
272 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
277 if(!strcmp("name",token
)) {
279 car
= seekNextChar(in
);
280 if(car
== EOF
) in
->error(in
,"name was expected");
281 else if(car
== '\"') fac
->name
= allocAndCopy(getQuotedString(in
));
282 else fac
->name
= allocAndCopy(getName(in
));
287 /**************************************************************************
291 * Read the attribute from the input file.
294 * in , input file handle.
295 * f , the field to fill.
297 **************************************************************************/
299 void getFieldAttributes(parse_file
*in
, field
*f
)
307 token
= getToken(in
);
308 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
313 if(!strcmp("name",token
)) {
315 car
= seekNextChar(in
);
316 if(car
== EOF
) in
->error(in
,"name was expected");
317 else if(car
== '\"') f
->name
= allocAndCopy(getQuotedString(in
));
318 else f
->name
= allocAndCopy(getName(in
));
323 char *getNameAttribute(parse_file
*in
)
330 token
= getToken(in
);
331 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
336 if(!strcmp("name",token
)) {
338 car
= seekNextChar(in
);
339 if(car
== EOF
) in
->error(in
,"name was expected");
340 else if(car
== '\"') name
= allocAndCopy(getQuotedString(in
));
341 else name
= allocAndCopy(getName(in
));
344 if(name
== NULL
) in
->error(in
, "Name was expected");
351 //for <label name=label_name value=n format="..."/>, value is an option
352 char * getValueStrAttribute(parse_file
*in
)
356 token
= getToken(in
);
357 if(strcmp("/",token
) == 0){
362 if(strcmp("value",token
))in
->error(in
,"value was expected");
364 token
= getToken(in
);
365 if(in
->type
!= NUMBER
) in
->error(in
,"number was expected");
369 char * getDescription(parse_file
*in
)
372 char * token
, car
, *str
;
376 getLAnglebracket(in
);
378 if(strcmp("description",token
)){
379 fseek(in
->fp
, pos
, SEEK_SET
);
383 getRAnglebracket(in
);
386 while((car
= getc(in
->fp
)) != EOF
) {
387 if(car
== '<') break;
388 if(car
== '\0') continue;
389 in
->buffer
[pos
] = car
;
392 if(car
== EOF
)in
->error(in
,"not a valid description");
393 in
->buffer
[pos
] = '\0';
395 str
= allocAndCopy(in
->buffer
);
399 if(strcmp("description", token
))in
->error(in
,"not a valid description");
400 getRAnglebracket(in
);
405 /*****************************************************************************
407 * parseFacility : generate event list
409 * in : input file handle
410 * fac : empty facility
412 * fac : facility filled with event list
413 ****************************************************************************/
415 void parseFacility(parse_file
*in
, facility
* fac
)
420 getFacilityAttributes(in
, fac
);
421 if(fac
->name
== NULL
) in
->error(in
, "Attribute not named");
423 fac
->capname
= allocAndCopy(fac
->name
);
424 strupper(fac
->capname
);
425 getRAnglebracket(in
);
427 fac
->description
= getDescription(in
);
430 getLAnglebracket(in
);
432 token
= getToken(in
);
433 if(in
->type
== ENDFILE
)
434 in
->error(in
,"the definition of the facility is not finished");
436 if(strcmp("event",token
) == 0){
437 ev
= (event
*) memAlloc(sizeof(event
));
438 sequence_push(&(fac
->events
),ev
);
439 parseEvent(in
,ev
, &(fac
->unnamed_types
), &(fac
->named_types
));
440 }else if(strcmp("type",token
) == 0){
441 parseTypeDefinition(in
, &(fac
->unnamed_types
), &(fac
->named_types
));
442 }else if(in
->type
== FORWARDSLASH
){
444 }else in
->error(in
,"event or type token expected\n");
448 if(strcmp("facility",token
)) in
->error(in
,"not the end of the facility");
449 getRAnglebracket(in
); //</facility>
452 /*****************************************************************************
454 * parseEvent : generate event from event definition
456 * in : input file handle
458 * unnamed_types : array of unamed types
459 * named_types : array of named types
461 * ev : new event (parameters are passed to it)
462 ****************************************************************************/
464 void parseEvent(parse_file
*in
, event
* ev
, sequence
* unnamed_types
,
470 //<event name=eventtype_name>
471 getEventAttributes(in
, ev
);
472 if(ev
->name
== NULL
) in
->error(in
, "Event not named");
473 getRAnglebracket(in
);
475 //<description>...</descriptio>
476 ev
->description
= getDescription(in
);
478 //event can have STRUCT, TYPEREF or NOTHING
479 getLAnglebracket(in
);
481 token
= getToken(in
);
482 if(in
->type
== FORWARDSLASH
){ //</event> NOTHING
484 }else if(in
->type
== NAME
){
485 if(strcmp("struct",token
)==0 || strcmp("typeref",token
)==0){
487 ev
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
488 if(ev
->type
->type
!= STRUCT
&& ev
->type
->type
!= NONE
)
489 in
->error(in
,"type must be a struct");
490 }else in
->error(in
, "not a valid type");
492 getLAnglebracket(in
);
494 }else in
->error(in
,"not a struct type");
497 if(strcmp("event",token
))in
->error(in
,"not an event definition");
498 getRAnglebracket(in
); //</event>
501 /*****************************************************************************
503 * parseField : get field infomation from buffer
505 * in : input file handle
506 * t : type descriptor
507 * unnamed_types : array of unamed types
508 * named_types : array of named types
509 ****************************************************************************/
511 void parseFields(parse_file
*in
, type_descriptor
*t
, sequence
* unnamed_types
,
517 f
= (field
*)memAlloc(sizeof(field
));
518 sequence_push(&(t
->fields
),f
);
520 //<field name=field_name> <description> <type> </field>
521 getFieldAttributes(in
, f
);
522 if(f
->name
== NULL
) in
->error(in
, "Field not named");
523 getRAnglebracket(in
);
525 f
->description
= getDescription(in
);
528 getLAnglebracket(in
);
529 f
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
531 getLAnglebracket(in
);
534 if(strcmp("field",token
))in
->error(in
,"not a valid field definition");
535 getRAnglebracket(in
); //</field>
539 /*****************************************************************************
541 * parseType : get type information, type can be :
543 * int(size,fmt); uint(size,fmt); float(size,fmt);
544 * string(fmt); enum(size,fmt,(label1,label2...))
546 * array(arraySize, type); sequence(lengthSize,type)
547 * struct(field(name,type,description)...)
551 * in : input file handle
552 * inType : a type descriptor
553 * unnamed_types : array of unamed types
554 * named_types : array of named types
556 * type_descriptor* : a type descriptor
557 ****************************************************************************/
559 type_descriptor
*parseType(parse_file
*in
, type_descriptor
*inType
,
560 sequence
* unnamed_types
, table
* named_types
)
566 t
= (type_descriptor
*) memAlloc(sizeof(type_descriptor
));
570 sequence_push(unnamed_types
,t
);
576 if(strcmp(token
,"struct") == 0) {
578 getTypeAttributes(in
, t
);
579 getRAnglebracket(in
); //<struct>
580 getLAnglebracket(in
); //<field name=..>
581 token
= getToken(in
);
582 sequence_init(&(t
->fields
));
583 while(strcmp("field",token
) == 0){
584 parseFields(in
,t
, unnamed_types
, named_types
);
587 getLAnglebracket(in
);
588 token
= getToken(in
);
590 if(strcmp("/",token
))in
->error(in
,"not a valid structure definition");
592 if(strcmp("struct",token
)!=0)
593 in
->error(in
,"not a valid structure definition");
594 getRAnglebracket(in
); //</struct>
596 else if(strcmp(token
,"union") == 0) {
598 getTypeAttributes(in
, t
);
599 if(t
->size
== -1) in
->error(in
, "Union has empty size");
600 getRAnglebracket(in
); //<union typecodesize=isize>
602 getLAnglebracket(in
); //<field name=..>
603 token
= getToken(in
);
604 sequence_init(&(t
->fields
));
605 while(strcmp("field",token
) == 0){
606 parseFields(in
,t
, unnamed_types
, named_types
);
609 getLAnglebracket(in
);
610 token
= getToken(in
);
612 if(strcmp("/",token
))in
->error(in
,"not a valid union definition");
614 if(strcmp("union",token
)!=0)
615 in
->error(in
,"not a valid union definition");
616 getRAnglebracket(in
); //</union>
618 else if(strcmp(token
,"array") == 0) {
620 getTypeAttributes(in
, t
);
621 if(t
->size
== -1) in
->error(in
, "Array has empty size");
622 getRAnglebracket(in
); //<array size=n>
624 getLAnglebracket(in
); //<type struct>
625 t
->nested_type
= parseType(in
, NULL
, unnamed_types
, named_types
);
627 getLAnglebracket(in
); //</array>
630 if(strcmp("array",token
))in
->error(in
,"not a valid array definition");
631 getRAnglebracket(in
); //</array>
633 else if(strcmp(token
,"sequence") == 0) {
635 getTypeAttributes(in
, t
);
636 if(t
->size
== -1) in
->error(in
, "Sequence has empty size");
637 getRAnglebracket(in
); //<array lengthsize=isize>
639 getLAnglebracket(in
); //<type struct>
640 t
->nested_type
= parseType(in
,NULL
, unnamed_types
, named_types
);
642 getLAnglebracket(in
); //</sequence>
645 if(strcmp("sequence",token
))in
->error(in
,"not a valid sequence definition");
646 getRAnglebracket(in
); //</sequence>
648 else if(strcmp(token
,"enum") == 0) {
651 sequence_init(&(t
->labels
));
652 sequence_init(&(t
->labels_description
));
653 t
->already_printed
= 0;
654 getTypeAttributes(in
, t
);
655 if(t
->size
== -1) in
->error(in
, "Sequence has empty size");
656 getRAnglebracket(in
);
658 //<label name=label1 value=n/>
659 getLAnglebracket(in
);
660 token
= getToken(in
); //"label" or "/"
661 while(strcmp("label",token
) == 0){
662 str
= allocAndCopy(getNameAttribute(in
));
663 token
= getValueStrAttribute(in
);
665 str1
= appendString(str
,"=");
667 str
= appendString(str1
,token
);
669 sequence_push(&(t
->labels
),str
);
672 sequence_push(&(t
->labels
),str
);
675 getRAnglebracket(in
);
677 //read description if any. May be NULL.
678 str
= allocAndCopy(getDescription(in
));
679 sequence_push(&(t
->labels_description
),str
);
681 //next label definition
682 getLAnglebracket(in
);
683 token
= getToken(in
); //"label" or "/"
685 if(strcmp("/",token
))in
->error(in
, "not a valid enum definition");
687 if(strcmp("enum",token
))in
->error(in
, "not a valid enum definition");
688 getRAnglebracket(in
); //</label>
690 else if(strcmp(token
,"int") == 0) {
692 getTypeAttributes(in
, t
);
693 if(t
->size
== -1) in
->error(in
, "int has empty size");
695 getRAnglebracket(in
);
697 else if(strcmp(token
,"uint") == 0) {
699 getTypeAttributes(in
, t
);
700 if(t
->size
== -1) in
->error(in
, "uint has empty size");
702 getRAnglebracket(in
);
704 else if(strcmp(token
,"pointer") == 0) {
706 getTypeAttributes(in
, t
);
708 getRAnglebracket(in
);
710 else if(strcmp(token
,"long") == 0) {
712 getTypeAttributes(in
, t
);
714 getRAnglebracket(in
);
716 else if(strcmp(token
,"ulong") == 0) {
718 getTypeAttributes(in
, t
);
720 getRAnglebracket(in
);
722 else if(strcmp(token
,"size_t") == 0) {
724 getTypeAttributes(in
, t
);
726 getRAnglebracket(in
);
728 else if(strcmp(token
,"ssize_t") == 0) {
730 getTypeAttributes(in
, t
);
732 getRAnglebracket(in
);
734 else if(strcmp(token
,"off_t") == 0) {
736 getTypeAttributes(in
, t
);
738 getRAnglebracket(in
);
740 else if(strcmp(token
,"float") == 0) {
742 getTypeAttributes(in
, t
);
744 getRAnglebracket(in
);
746 else if(strcmp(token
,"string") == 0) {
748 getTypeAttributes(in
, t
);
750 getRAnglebracket(in
);
752 else if(strcmp(token
,"typeref") == 0){
753 // Must be a named type
755 in
->error(in
,"Named type cannot refer to a named type");
758 sequence_pop(unnamed_types
);
759 token
= getNameAttribute(in
);
760 t
= find_named_type(token
, named_types
);
761 getForwardslash(in
); //<typeref name=type_name/>
762 getRAnglebracket(in
);
765 }else in
->error(in
,"not a valid type");
770 /*****************************************************************************
772 * find_named_type : find a named type from hash table
775 * named_types : array of named types
777 * type_descriptor * : a type descriptor
778 *****************************************************************************/
780 type_descriptor
* find_named_type(char *name
, table
* named_types
)
784 t
= table_find(named_types
,name
);
786 t
= (type_descriptor
*)memAlloc(sizeof(type_descriptor
));
787 t
->type_name
= allocAndCopy(name
);
790 table_insert(named_types
,t
->type_name
,t
);
791 // table_insert(named_types,allocAndCopy(name),t);
796 /*****************************************************************************
798 * parseTypeDefinition : get type information from type definition
800 * in : input file handle
801 * unnamed_types : array of unamed types
802 * named_types : array of named types
803 *****************************************************************************/
805 void parseTypeDefinition(parse_file
* in
, sequence
* unnamed_types
,
811 token
= getNameAttribute(in
);
812 if(token
== NULL
) in
->error(in
, "Type has empty name");
813 t
= find_named_type(token
, named_types
);
815 if(t
->type
!= NONE
) in
->error(in
,"redefinition of named type");
816 getRAnglebracket(in
); //<type name=type_name>
817 getLAnglebracket(in
); //<
819 //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
821 parseType(in
,t
, unnamed_types
, named_types
);
824 getLAnglebracket(in
);
827 if(strcmp("type",token
))in
->error(in
,"not a valid type definition");
828 getRAnglebracket(in
); //</type>
831 /**************************************************************************
833 * getComa, getName, getNumber, getEqual
835 * Read a token from the input file, check its type, return it scontent.
838 * in , input file handle.
841 * address of token content.
843 **************************************************************************/
845 char *getName(parse_file
* in
)
849 token
= getToken(in
);
850 if(in
->type
!= NAME
) in
->error(in
,"Name token was expected");
854 int getNumber(parse_file
* in
)
858 token
= getToken(in
);
859 if(in
->type
!= NUMBER
) in
->error(in
, "Number token was expected");
863 char *getForwardslash(parse_file
* in
)
867 token
= getToken(in
);
868 if(in
->type
!= FORWARDSLASH
) in
->error(in
, "forward slash token was expected");
872 char *getLAnglebracket(parse_file
* in
)
876 token
= getToken(in
);
877 if(in
->type
!= LANGLEBRACKET
) in
->error(in
, "Left angle bracket was expected");
881 char *getRAnglebracket(parse_file
* in
)
885 token
= getToken(in
);
886 if(in
->type
!= RANGLEBRACKET
) in
->error(in
, "Right angle bracket was expected");
890 char *getQuotedString(parse_file
* in
)
894 token
= getToken(in
);
895 if(in
->type
!= QUOTEDSTRING
) in
->error(in
, "quoted string was expected");
899 char * getEqual(parse_file
*in
)
903 token
= getToken(in
);
904 if(in
->type
!= EQUAL
) in
->error(in
, "equal was expected");
908 char seekNextChar(parse_file
*in
)
911 while((car
= getc(in
->fp
)) != EOF
) {
920 /******************************************************************
922 * getToken, ungetToken
924 * Read a token from the input file and return its type and content.
925 * Line numbers are accounted for and whitespace/comments are skipped.
928 * in, input file handle.
931 * address of token content.
933 ******************************************************************/
935 void ungetToken(parse_file
* in
)
940 char *getToken(parse_file
* in
)
944 int pos
= 0, escaped
;
951 /* skip whitespace and comments */
953 while((car
= getc(fp
)) != EOF
) {
956 if(car1
== '*') skipComment(in
);
957 else if(car1
== '/') skipEOL(in
);
959 car1
= ungetc(car1
,fp
);
963 else if(car
== '\n') in
->lineno
++;
964 else if(!isspace(car
)) break;
972 in
->type
= FORWARDSLASH
;
973 in
->buffer
[pos
] = car
;
977 in
->type
= LANGLEBRACKET
;
978 in
->buffer
[pos
] = car
;
982 in
->type
= RANGLEBRACKET
;
983 in
->buffer
[pos
] = car
;
988 in
->buffer
[pos
] = car
;
993 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
994 if(car
== '\\' && escaped
== 0) {
995 in
->buffer
[pos
] = car
;
1000 if(car
== '"' && escaped
== 0) break;
1001 if(car
== '\n' && escaped
== 0) {
1002 in
->error(in
, "non escaped newline inside quoted string");
1004 if(car
== '\n') in
->lineno
++;
1005 in
->buffer
[pos
] = car
;
1009 if(car
== EOF
) in
->error(in
,"no ending quotemark");
1010 if(pos
== BUFFER_SIZE
) in
->error(in
, "quoted string token too large");
1011 in
->type
= QUOTEDSTRING
;
1015 in
->buffer
[pos
] = car
;
1017 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1022 in
->buffer
[pos
] = car
;
1025 if(car
== EOF
) ungetc(car
,fp
);
1026 if(pos
== BUFFER_SIZE
) in
->error(in
, "number token too large");
1029 else if(isalpha(car
)) {
1030 in
->buffer
[0] = car
;
1032 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1033 if(!(isalnum(car
) || car
== '_')) {
1037 in
->buffer
[pos
] = car
;
1040 if(car
== EOF
) ungetc(car
,fp
);
1041 if(pos
== BUFFER_SIZE
) in
->error(in
, "name token too large");
1044 else in
->error(in
, "invalid character, unrecognized token");
1046 in
->buffer
[pos
] = 0;
1050 void skipComment(parse_file
* in
)
1053 while((car
= getc(in
->fp
)) != EOF
) {
1054 if(car
== '\n') in
->lineno
++;
1055 else if(car
== '*') {
1057 if(car
==EOF
) break;
1058 if(car
== '/') return;
1062 if(car
== EOF
) in
->error(in
,"comment begining with '/*' has no ending '*/'");
1065 void skipEOL(parse_file
* in
)
1068 while((car
= getc(in
->fp
)) != EOF
) {
1074 if(car
== EOF
)ungetc(car
, in
->fp
);
1077 /*****************************************************************************
1079 * checkNamedTypesImplemented : check if all named types have definition
1080 ****************************************************************************/
1082 void checkNamedTypesImplemented(table
* named_types
)
1088 for(pos
= 0 ; pos
< named_types
->values
.position
; pos
++) {
1089 t
= (type_descriptor
*) named_types
->values
.array
[pos
];
1090 if(t
->type
== NONE
){
1091 sprintf(str
,"named type '%s' has no definition",(char*)named_types
->keys
.array
[pos
]);
1092 error_callback(NULL
,str
);
1098 /*****************************************************************************
1100 * generateChecksum : generate checksum for the facility
1102 * facName : name of facility
1104 * checksum : checksum for the facility
1105 ****************************************************************************/
1107 void generateChecksum( char* facName
, unsigned long * checksum
, sequence
* events
)
1114 crc
= crc32(facName
);
1115 for(pos
= 0; pos
< events
->position
; pos
++){
1116 ev
= (event
*)(events
->array
[pos
]);
1117 crc
= partial_crc32(ev
->name
,crc
);
1118 if(!ev
->type
) continue; //event without type
1119 if(ev
->type
->type
!= STRUCT
){
1120 sprintf(str
,"event '%s' has a type other than STRUCT",ev
->name
);
1121 error_callback(NULL
, str
);
1123 crc
= getTypeChecksum(crc
, ev
->type
);
1128 /*****************************************************************************
1130 * getTypeChecksum : generate checksum by type info
1132 * crc : checksum generated so far
1133 * type : type descriptor containing type info
1135 * unsigned long : checksum
1136 *****************************************************************************/
1138 unsigned long getTypeChecksum(unsigned long aCrc
, type_descriptor
* type
)
1140 unsigned long crc
= aCrc
;
1141 char * str
= NULL
, buf
[16];
1147 str
= intOutputTypes
[type
->size
];
1150 str
= uintOutputTypes
[type
->size
];
1153 str
= allocAndCopy("void *");
1157 str
= allocAndCopy("long");
1161 str
= allocAndCopy("unsigned long");
1165 str
= allocAndCopy("size_t");
1169 str
= allocAndCopy("ssize_t");
1173 str
= allocAndCopy("off_t");
1177 str
= floatOutputTypes
[type
->size
];
1180 str
= allocAndCopy("string");
1184 str
= appendString("enum ", uintOutputTypes
[type
->size
]);
1188 sprintf(buf
,"%d\0",type
->size
);
1189 str
= appendString("array ",buf
);
1193 sprintf(buf
,"%d\0",type
->size
);
1194 str
= appendString("sequence ",buf
);
1198 str
= allocAndCopy("struct");
1202 str
= allocAndCopy("union");
1206 error_callback(NULL
, "named type has no definition");
1210 crc
= partial_crc32(str
,crc
);
1213 if(type
->fmt
) crc
= partial_crc32(type
->fmt
,crc
);
1215 if(type
->type
== ARRAY
|| type
->type
== SEQUENCE
){
1216 crc
= getTypeChecksum(crc
,type
->nested_type
);
1217 }else if(type
->type
== STRUCT
|| type
->type
== UNION
){
1218 for(pos
=0; pos
< type
->fields
.position
; pos
++){
1219 fld
= (field
*) type
->fields
.array
[pos
];
1220 crc
= partial_crc32(fld
->name
,crc
);
1221 crc
= getTypeChecksum(crc
, fld
->type
);
1223 }else if(type
->type
== ENUM
){
1224 for(pos
= 0; pos
< type
->labels
.position
; pos
++)
1225 crc
= partial_crc32((char*)type
->labels
.array
[pos
],crc
);
1232 /* Event type descriptors */
1233 void freeType(type_descriptor
* tp
)
1238 if(tp
->fmt
!= NULL
) free(tp
->fmt
);
1239 if(tp
->type
== ENUM
) {
1240 for(pos2
= 0; pos2
< tp
->labels
.position
; pos2
++) {
1241 free(tp
->labels
.array
[pos2
]);
1243 sequence_dispose(&(tp
->labels
));
1245 if(tp
->type
== STRUCT
) {
1246 for(pos2
= 0; pos2
< tp
->fields
.position
; pos2
++) {
1247 f
= (field
*) tp
->fields
.array
[pos2
];
1249 free(f
->description
);
1252 sequence_dispose(&(tp
->fields
));
1256 void freeNamedType(table
* t
)
1259 type_descriptor
* td
;
1261 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1262 free((char *)t
->keys
.array
[pos
]);
1263 td
= (type_descriptor
*)t
->values
.array
[pos
];
1269 void freeTypes(sequence
*t
)
1272 type_descriptor
*tp
;
1275 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1276 tp
= (type_descriptor
*)t
->array
[pos
];
1282 void freeEvents(sequence
*t
)
1287 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1288 ev
= (event
*) t
->array
[pos
];
1290 free(ev
->description
);
1297 /* Extensible array */
1299 void sequence_init(sequence
*t
)
1303 t
->array
= (void **)memAlloc(t
->size
* sizeof(void *));
1306 void sequence_dispose(sequence
*t
)
1313 void sequence_push(sequence
*t
, void *elem
)
1317 if(t
->position
>= t
->size
) {
1319 t
->array
= (void **)memAlloc(t
->size
* 2 * sizeof(void *));
1320 memcpy(t
->array
, tmp
, t
->size
* sizeof(void *));
1321 t
->size
= t
->size
* 2;
1324 t
->array
[t
->position
] = elem
;
1328 void *sequence_pop(sequence
*t
)
1330 return t
->array
[t
->position
--];
1334 /* Hash table API, implementation is just linear search for now */
1336 void table_init(table
*t
)
1338 sequence_init(&(t
->keys
));
1339 sequence_init(&(t
->values
));
1342 void table_dispose(table
*t
)
1344 sequence_dispose(&(t
->keys
));
1345 sequence_dispose(&(t
->values
));
1348 void table_insert(table
*t
, char *key
, void *value
)
1350 sequence_push(&(t
->keys
),key
);
1351 sequence_push(&(t
->values
),value
);
1354 void *table_find(table
*t
, char *key
)
1357 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1358 if(strcmp((char *)key
,(char *)t
->keys
.array
[pos
]) == 0)
1359 return(t
->values
.array
[pos
]);
1364 void table_insert_int(table
*t
, int *key
, void *value
)
1366 sequence_push(&(t
->keys
),key
);
1367 sequence_push(&(t
->values
),value
);
1370 void *table_find_int(table
*t
, int *key
)
1373 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1374 if(*key
== *(int *)t
->keys
.array
[pos
])
1375 return(t
->values
.array
[pos
]);
1381 /* Concatenate strings */
1383 char *appendString(char *s
, char *suffix
)
1386 if(suffix
== NULL
) return s
;
1388 tmp
= (char *)memAlloc(strlen(s
) + strlen(suffix
) + 1);
This page took 0.062741 seconds and 4 git commands to generate.