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_t
*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_t
*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_t
*in
, type_descriptor_t
*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_t
*in
, event_t
*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_t
*in
, facility_t
*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_t
*in
, field_t
*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_t
*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_t
*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_t
*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_t
*in
, facility_t
* 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_t
*) memAlloc(sizeof(event_t
));
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_t
*in
, event_t
* ev
, sequence_t
* unnamed_types
,
465 table_t
* named_types
)
468 type_descriptor_t
*t
;
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_t
*in
, type_descriptor_t
*t
,
512 sequence_t
* unnamed_types
,
513 table_t
* named_types
)
518 f
= (field_t
*)memAlloc(sizeof(field_t
));
519 sequence_push(&(t
->fields
),f
);
521 //<field name=field_name> <description> <type> </field>
522 getFieldAttributes(in
, f
);
523 if(f
->name
== NULL
) in
->error(in
, "Field not named");
524 getRAnglebracket(in
);
526 f
->description
= getDescription(in
);
529 getLAnglebracket(in
);
530 f
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
532 getLAnglebracket(in
);
535 if(strcmp("field",token
))in
->error(in
,"not a valid field definition");
536 getRAnglebracket(in
); //</field>
540 /*****************************************************************************
542 * parseType : get type information, type can be :
544 * int(size,fmt); uint(size,fmt); float(size,fmt);
545 * string(fmt); enum(size,fmt,(label1,label2...))
547 * array(arraySize, type); sequence(lengthSize,type)
548 * struct(field(name,type,description)...)
552 * in : input file handle
553 * inType : a type descriptor
554 * unnamed_types : array of unamed types
555 * named_types : array of named types
557 * type_descriptor* : a type descriptor
558 ****************************************************************************/
560 type_descriptor_t
*parseType(parse_file_t
*in
, type_descriptor_t
*inType
,
561 sequence_t
* unnamed_types
, table_t
* named_types
)
564 type_descriptor_t
*t
;
567 t
= (type_descriptor_t
*) memAlloc(sizeof(type_descriptor_t
));
571 sequence_push(unnamed_types
,t
);
577 if(strcmp(token
,"struct") == 0) {
579 getTypeAttributes(in
, t
);
580 getRAnglebracket(in
); //<struct>
581 getLAnglebracket(in
); //<field name=..>
582 token
= getToken(in
);
583 sequence_init(&(t
->fields
));
584 while(strcmp("field",token
) == 0){
585 parseFields(in
,t
, unnamed_types
, named_types
);
588 getLAnglebracket(in
);
589 token
= getToken(in
);
591 if(strcmp("/",token
))in
->error(in
,"not a valid structure definition");
593 if(strcmp("struct",token
)!=0)
594 in
->error(in
,"not a valid structure definition");
595 getRAnglebracket(in
); //</struct>
597 else if(strcmp(token
,"union") == 0) {
599 getTypeAttributes(in
, t
);
600 if(t
->size
== -1) in
->error(in
, "Union has empty size");
601 getRAnglebracket(in
); //<union typecodesize=isize>
603 getLAnglebracket(in
); //<field name=..>
604 token
= getToken(in
);
605 sequence_init(&(t
->fields
));
606 while(strcmp("field",token
) == 0){
607 parseFields(in
,t
, unnamed_types
, named_types
);
610 getLAnglebracket(in
);
611 token
= getToken(in
);
613 if(strcmp("/",token
))in
->error(in
,"not a valid union definition");
615 if(strcmp("union",token
)!=0)
616 in
->error(in
,"not a valid union definition");
617 getRAnglebracket(in
); //</union>
619 else if(strcmp(token
,"array") == 0) {
621 getTypeAttributes(in
, t
);
622 if(t
->size
== -1) in
->error(in
, "Array has empty size");
623 getRAnglebracket(in
); //<array size=n>
625 getLAnglebracket(in
); //<type struct>
626 t
->nested_type
= parseType(in
, NULL
, unnamed_types
, named_types
);
628 getLAnglebracket(in
); //</array>
631 if(strcmp("array",token
))in
->error(in
,"not a valid array definition");
632 getRAnglebracket(in
); //</array>
634 else if(strcmp(token
,"sequence") == 0) {
636 getTypeAttributes(in
, t
);
637 if(t
->size
== -1) in
->error(in
, "Sequence has empty size");
638 getRAnglebracket(in
); //<array lengthsize=isize>
640 getLAnglebracket(in
); //<type struct>
641 t
->nested_type
= parseType(in
,NULL
, unnamed_types
, named_types
);
643 getLAnglebracket(in
); //</sequence>
646 if(strcmp("sequence",token
))in
->error(in
,"not a valid sequence definition");
647 getRAnglebracket(in
); //</sequence>
649 else if(strcmp(token
,"enum") == 0) {
652 sequence_init(&(t
->labels
));
653 sequence_init(&(t
->labels_description
));
654 t
->already_printed
= 0;
655 getTypeAttributes(in
, t
);
656 if(t
->size
== -1) in
->error(in
, "Sequence has empty size");
657 getRAnglebracket(in
);
659 //<label name=label1 value=n/>
660 getLAnglebracket(in
);
661 token
= getToken(in
); //"label" or "/"
662 while(strcmp("label",token
) == 0){
663 str
= allocAndCopy(getNameAttribute(in
));
664 token
= getValueStrAttribute(in
);
666 str1
= appendString(str
,"=");
668 str
= appendString(str1
,token
);
670 sequence_push(&(t
->labels
),str
);
673 sequence_push(&(t
->labels
),str
);
676 getRAnglebracket(in
);
678 //read description if any. May be NULL.
679 str
= allocAndCopy(getDescription(in
));
680 sequence_push(&(t
->labels_description
),str
);
682 //next label definition
683 getLAnglebracket(in
);
684 token
= getToken(in
); //"label" or "/"
686 if(strcmp("/",token
))in
->error(in
, "not a valid enum definition");
688 if(strcmp("enum",token
))in
->error(in
, "not a valid enum definition");
689 getRAnglebracket(in
); //</label>
691 else if(strcmp(token
,"int") == 0) {
693 getTypeAttributes(in
, t
);
694 if(t
->size
== -1) in
->error(in
, "int has empty size");
696 getRAnglebracket(in
);
698 else if(strcmp(token
,"uint") == 0) {
700 getTypeAttributes(in
, t
);
701 if(t
->size
== -1) in
->error(in
, "uint has empty size");
703 getRAnglebracket(in
);
705 else if(strcmp(token
,"pointer") == 0) {
707 getTypeAttributes(in
, t
);
709 getRAnglebracket(in
);
711 else if(strcmp(token
,"long") == 0) {
713 getTypeAttributes(in
, t
);
715 getRAnglebracket(in
);
717 else if(strcmp(token
,"ulong") == 0) {
719 getTypeAttributes(in
, t
);
721 getRAnglebracket(in
);
723 else if(strcmp(token
,"size_t") == 0) {
725 getTypeAttributes(in
, t
);
727 getRAnglebracket(in
);
729 else if(strcmp(token
,"ssize_t") == 0) {
731 getTypeAttributes(in
, t
);
733 getRAnglebracket(in
);
735 else if(strcmp(token
,"off_t") == 0) {
737 getTypeAttributes(in
, t
);
739 getRAnglebracket(in
);
741 else if(strcmp(token
,"float") == 0) {
743 getTypeAttributes(in
, t
);
745 getRAnglebracket(in
);
747 else if(strcmp(token
,"string") == 0) {
749 getTypeAttributes(in
, t
);
751 getRAnglebracket(in
);
753 else if(strcmp(token
,"typeref") == 0){
754 // Must be a named type
756 in
->error(in
,"Named type cannot refer to a named type");
759 sequence_pop(unnamed_types
);
760 token
= getNameAttribute(in
);
761 t
= find_named_type(token
, named_types
);
762 getForwardslash(in
); //<typeref name=type_name/>
763 getRAnglebracket(in
);
766 }else in
->error(in
,"not a valid type");
771 /*****************************************************************************
773 * find_named_type : find a named type from hash table
776 * named_types : array of named types
778 * type_descriptor * : a type descriptor
779 *****************************************************************************/
781 type_descriptor_t
* find_named_type(char *name
, table_t
* named_types
)
783 type_descriptor_t
*t
;
785 t
= table_find(named_types
,name
);
787 t
= (type_descriptor_t
*)memAlloc(sizeof(type_descriptor_t
));
788 t
->type_name
= allocAndCopy(name
);
791 table_insert(named_types
,t
->type_name
,t
);
792 // table_insert(named_types,allocAndCopy(name),t);
797 /*****************************************************************************
799 * parseTypeDefinition : get type information from type definition
801 * in : input file handle
802 * unnamed_types : array of unamed types
803 * named_types : array of named types
804 *****************************************************************************/
806 void parseTypeDefinition(parse_file_t
* in
, sequence_t
* unnamed_types
,
807 table_t
* named_types
)
810 type_descriptor_t
*t
;
812 token
= getNameAttribute(in
);
813 if(token
== NULL
) in
->error(in
, "Type has empty name");
814 t
= find_named_type(token
, named_types
);
816 if(t
->type
!= NONE
) in
->error(in
,"redefinition of named type");
817 getRAnglebracket(in
); //<type name=type_name>
818 getLAnglebracket(in
); //<
820 //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
822 parseType(in
,t
, unnamed_types
, named_types
);
825 getLAnglebracket(in
);
828 if(strcmp("type",token
))in
->error(in
,"not a valid type definition");
829 getRAnglebracket(in
); //</type>
832 /**************************************************************************
834 * getComa, getName, getNumber, getEqual
836 * Read a token from the input file, check its type, return it scontent.
839 * in , input file handle.
842 * address of token content.
844 **************************************************************************/
846 char *getName(parse_file_t
* in
)
850 token
= getToken(in
);
851 if(in
->type
!= NAME
) in
->error(in
,"Name token was expected");
855 int getNumber(parse_file_t
* in
)
859 token
= getToken(in
);
860 if(in
->type
!= NUMBER
) in
->error(in
, "Number token was expected");
864 char *getForwardslash(parse_file_t
* in
)
868 token
= getToken(in
);
869 if(in
->type
!= FORWARDSLASH
) in
->error(in
, "forward slash token was expected");
873 char *getLAnglebracket(parse_file_t
* in
)
877 token
= getToken(in
);
878 if(in
->type
!= LANGLEBRACKET
) in
->error(in
, "Left angle bracket was expected");
882 char *getRAnglebracket(parse_file_t
* in
)
886 token
= getToken(in
);
887 if(in
->type
!= RANGLEBRACKET
) in
->error(in
, "Right angle bracket was expected");
891 char *getQuotedString(parse_file_t
* in
)
895 token
= getToken(in
);
896 if(in
->type
!= QUOTEDSTRING
) in
->error(in
, "quoted string was expected");
900 char * getEqual(parse_file_t
*in
)
904 token
= getToken(in
);
905 if(in
->type
!= EQUAL
) in
->error(in
, "equal was expected");
909 char seekNextChar(parse_file_t
*in
)
912 while((car
= getc(in
->fp
)) != EOF
) {
921 /******************************************************************
923 * getToken, ungetToken
925 * Read a token from the input file and return its type and content.
926 * Line numbers are accounted for and whitespace/comments are skipped.
929 * in, input file handle.
932 * address of token content.
934 ******************************************************************/
936 void ungetToken(parse_file_t
* in
)
941 char *getToken(parse_file_t
* in
)
945 int pos
= 0, escaped
;
952 /* skip whitespace and comments */
954 while((car
= getc(fp
)) != EOF
) {
957 if(car1
== '*') skipComment(in
);
958 else if(car1
== '/') skipEOL(in
);
960 car1
= ungetc(car1
,fp
);
964 else if(car
== '\n') in
->lineno
++;
965 else if(!isspace(car
)) break;
973 in
->type
= FORWARDSLASH
;
974 in
->buffer
[pos
] = car
;
978 in
->type
= LANGLEBRACKET
;
979 in
->buffer
[pos
] = car
;
983 in
->type
= RANGLEBRACKET
;
984 in
->buffer
[pos
] = car
;
989 in
->buffer
[pos
] = car
;
994 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
995 if(car
== '\\' && escaped
== 0) {
996 in
->buffer
[pos
] = car
;
1001 if(car
== '"' && escaped
== 0) break;
1002 if(car
== '\n' && escaped
== 0) {
1003 in
->error(in
, "non escaped newline inside quoted string");
1005 if(car
== '\n') in
->lineno
++;
1006 in
->buffer
[pos
] = car
;
1010 if(car
== EOF
) in
->error(in
,"no ending quotemark");
1011 if(pos
== BUFFER_SIZE
) in
->error(in
, "quoted string token too large");
1012 in
->type
= QUOTEDSTRING
;
1016 in
->buffer
[pos
] = car
;
1018 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1023 in
->buffer
[pos
] = car
;
1026 if(car
== EOF
) ungetc(car
,fp
);
1027 if(pos
== BUFFER_SIZE
) in
->error(in
, "number token too large");
1030 else if(isalpha(car
)) {
1031 in
->buffer
[0] = car
;
1033 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1034 if(!(isalnum(car
) || car
== '_')) {
1038 in
->buffer
[pos
] = car
;
1041 if(car
== EOF
) ungetc(car
,fp
);
1042 if(pos
== BUFFER_SIZE
) in
->error(in
, "name token too large");
1045 else in
->error(in
, "invalid character, unrecognized token");
1047 in
->buffer
[pos
] = 0;
1051 void skipComment(parse_file_t
* in
)
1054 while((car
= getc(in
->fp
)) != EOF
) {
1055 if(car
== '\n') in
->lineno
++;
1056 else if(car
== '*') {
1058 if(car
==EOF
) break;
1059 if(car
== '/') return;
1063 if(car
== EOF
) in
->error(in
,"comment begining with '/*' has no ending '*/'");
1066 void skipEOL(parse_file_t
* in
)
1069 while((car
= getc(in
->fp
)) != EOF
) {
1075 if(car
== EOF
)ungetc(car
, in
->fp
);
1078 /*****************************************************************************
1080 * checkNamedTypesImplemented : check if all named types have definition
1081 ****************************************************************************/
1083 void checkNamedTypesImplemented(table_t
* named_types
)
1085 type_descriptor_t
*t
;
1089 for(pos
= 0 ; pos
< named_types
->values
.position
; pos
++) {
1090 t
= (type_descriptor_t
*) named_types
->values
.array
[pos
];
1091 if(t
->type
== NONE
){
1092 sprintf(str
,"named type '%s' has no definition",
1093 (char*)named_types
->keys
.array
[pos
]);
1094 error_callback(NULL
,str
);
1100 /*****************************************************************************
1102 * generateChecksum : generate checksum for the facility
1104 * facName : name of facility
1106 * checksum : checksum for the facility
1107 ****************************************************************************/
1109 void generateChecksum(char* facName
,
1110 unsigned long * checksum
, sequence_t
* events
)
1117 crc
= crc32(facName
);
1118 for(pos
= 0; pos
< events
->position
; pos
++){
1119 ev
= (event_t
*)(events
->array
[pos
]);
1120 crc
= partial_crc32(ev
->name
,crc
);
1121 if(!ev
->type
) continue; //event without type
1122 if(ev
->type
->type
!= STRUCT
){
1123 sprintf(str
,"event '%s' has a type other than STRUCT",ev
->name
);
1124 error_callback(NULL
, str
);
1126 crc
= getTypeChecksum(crc
, ev
->type
);
1131 /*****************************************************************************
1133 * getTypeChecksum : generate checksum by type info
1135 * crc : checksum generated so far
1136 * type : type descriptor containing type info
1138 * unsigned long : checksum
1139 *****************************************************************************/
1141 unsigned long getTypeChecksum(unsigned long aCrc
, type_descriptor_t
* type
)
1143 unsigned long crc
= aCrc
;
1144 char * str
= NULL
, buf
[16];
1150 str
= intOutputTypes
[type
->size
];
1153 str
= uintOutputTypes
[type
->size
];
1156 str
= allocAndCopy("void *");
1160 str
= allocAndCopy("long");
1164 str
= allocAndCopy("unsigned long");
1168 str
= allocAndCopy("size_t");
1172 str
= allocAndCopy("ssize_t");
1176 str
= allocAndCopy("off_t");
1180 str
= floatOutputTypes
[type
->size
];
1183 str
= allocAndCopy("string");
1187 str
= appendString("enum ", uintOutputTypes
[type
->size
]);
1191 sprintf(buf
,"%d\0",type
->size
);
1192 str
= appendString("array ",buf
);
1196 sprintf(buf
,"%d\0",type
->size
);
1197 str
= appendString("sequence ",buf
);
1201 str
= allocAndCopy("struct");
1205 str
= allocAndCopy("union");
1209 error_callback(NULL
, "named type has no definition");
1213 crc
= partial_crc32(str
,crc
);
1216 if(type
->fmt
) crc
= partial_crc32(type
->fmt
,crc
);
1218 if(type
->type
== ARRAY
|| type
->type
== SEQUENCE
){
1219 crc
= getTypeChecksum(crc
,type
->nested_type
);
1220 }else if(type
->type
== STRUCT
|| type
->type
== UNION
){
1221 for(pos
=0; pos
< type
->fields
.position
; pos
++){
1222 fld
= (field_t
*) type
->fields
.array
[pos
];
1223 crc
= partial_crc32(fld
->name
,crc
);
1224 crc
= getTypeChecksum(crc
, fld
->type
);
1226 }else if(type
->type
== ENUM
){
1227 for(pos
= 0; pos
< type
->labels
.position
; pos
++)
1228 crc
= partial_crc32((char*)type
->labels
.array
[pos
],crc
);
1235 /* Event type descriptors */
1236 void freeType(type_descriptor_t
* tp
)
1241 if(tp
->fmt
!= NULL
) free(tp
->fmt
);
1242 if(tp
->type
== ENUM
) {
1243 for(pos2
= 0; pos2
< tp
->labels
.position
; pos2
++) {
1244 free(tp
->labels
.array
[pos2
]);
1246 sequence_dispose(&(tp
->labels
));
1248 if(tp
->type
== STRUCT
) {
1249 for(pos2
= 0; pos2
< tp
->fields
.position
; pos2
++) {
1250 f
= (field_t
*) tp
->fields
.array
[pos2
];
1252 free(f
->description
);
1255 sequence_dispose(&(tp
->fields
));
1259 void freeNamedType(table_t
* t
)
1262 type_descriptor_t
* td
;
1264 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1265 free((char *)t
->keys
.array
[pos
]);
1266 td
= (type_descriptor_t
*)t
->values
.array
[pos
];
1272 void freeTypes(sequence_t
*t
)
1275 type_descriptor_t
*tp
;
1278 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1279 tp
= (type_descriptor_t
*)t
->array
[pos
];
1285 void freeEvents(sequence_t
*t
)
1290 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1291 ev
= (event_t
*) t
->array
[pos
];
1293 free(ev
->description
);
1300 /* Extensible array */
1302 void sequence_init(sequence_t
*t
)
1306 t
->array
= (void **)memAlloc(t
->size
* sizeof(void *));
1309 void sequence_dispose(sequence_t
*t
)
1316 void sequence_push(sequence_t
*t
, void *elem
)
1320 if(t
->position
>= t
->size
) {
1322 t
->array
= (void **)memAlloc(t
->size
* 2 * sizeof(void *));
1323 memcpy(t
->array
, tmp
, t
->size
* sizeof(void *));
1324 t
->size
= t
->size
* 2;
1327 t
->array
[t
->position
] = elem
;
1331 void *sequence_pop(sequence_t
*t
)
1333 return t
->array
[t
->position
--];
1337 /* Hash table API, implementation is just linear search for now */
1339 void table_init(table_t
*t
)
1341 sequence_init(&(t
->keys
));
1342 sequence_init(&(t
->values
));
1345 void table_dispose(table_t
*t
)
1347 sequence_dispose(&(t
->keys
));
1348 sequence_dispose(&(t
->values
));
1351 void table_insert(table_t
*t
, char *key
, void *value
)
1353 sequence_push(&(t
->keys
),key
);
1354 sequence_push(&(t
->values
),value
);
1357 void *table_find(table_t
*t
, char *key
)
1360 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1361 if(strcmp((char *)key
,(char *)t
->keys
.array
[pos
]) == 0)
1362 return(t
->values
.array
[pos
]);
1367 void table_insert_int(table_t
*t
, int *key
, void *value
)
1369 sequence_push(&(t
->keys
),key
);
1370 sequence_push(&(t
->values
),value
);
1373 void *table_find_int(table_t
*t
, int *key
)
1376 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1377 if(*key
== *(int *)t
->keys
.array
[pos
])
1378 return(t
->values
.array
[pos
]);
1384 /* Concatenate strings */
1386 char *appendString(char *s
, char *suffix
)
1389 if(suffix
== NULL
) return s
;
1391 tmp
= (char *)memAlloc(strlen(s
) + strlen(suffix
) + 1);
This page took 0.059221 seconds and 4 git commands to generate.