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
));
756 sequence_push(&(t
->fields
),f
);
757 parseFields(in
, f
, unnamed_types
, named_types
, 0);
759 //getLAnglebracket(in); //<type struct>
760 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
762 getLAnglebracket(in
); //</array>
765 if(strcmp("array",token
))in
->error(in
,"not a valid array definition");
766 getRAnglebracket(in
); //</array>
768 else if(strcmp(token
,"sequence") == 0) {
770 sequence_init(&(t
->fields
));
771 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
773 getRAnglebracket(in
); //<sequence>
775 //getLAnglebracket(in); //<sequence size type>
777 f
= (field_t
*)memAlloc(sizeof(field_t
));
779 sequence_push(&(t
->fields
),f
);
780 parseFields(in
, f
, unnamed_types
, named_types
, 0);
782 //getLAnglebracket(in); //<subtype>
784 f
= (field_t
*)memAlloc(sizeof(field_t
));
786 sequence_push(&(t
->fields
),f
);
787 parseFields(in
, f
, unnamed_types
, named_types
, 0);
789 //getLAnglebracket(in); //<type sequence>
790 //t->length_type = parseType(in, NULL, unnamed_types, named_types);
792 //getLAnglebracket(in); //<type sequence>
794 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
796 if(t
->fields
.position
< 1) in
->error(in
, "Sequence has no length type");
797 if(t
->fields
.position
< 2) in
->error(in
, "Sequence has no subtype");
798 switch(((field_t
*)t
->fields
.array
[0])->type
->type
) {
808 in
->error(in
, "Wrong length type for sequence");
811 getLAnglebracket(in
); //</sequence>
814 if(strcmp("sequence",token
))in
->error(in
,"not a valid sequence definition");
815 getRAnglebracket(in
); //</sequence>
817 else if(strcmp(token
,"enum") == 0) {
819 long long value
= -1;
822 sequence_init(&(t
->labels
));
823 sequence_init(&(t
->labels_values
));
824 sequence_init(&(t
->labels_description
));
825 t
->already_printed
= 0;
826 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
827 //if(t->size == 0) in->error(in, "Sequence has empty size");
828 //Mathieu : we fix enum size to target int size. GCC is always like this.
829 //fox copy optimisation.
830 if(t
->size
!= 0) in
->error(in
, "Enum has fixed size of target int.");
832 getRAnglebracket(in
);
834 //<label name=label1 value=n/>
835 getLAnglebracket(in
);
836 token
= getToken(in
); //"label" or "/"
837 while(strcmp("label",token
) == 0){
838 int *label_value
= malloc(sizeof(int));
842 str
= allocAndCopy(getNameAttribute(in
));
843 has_value
= getValueAttribute(in
, &loc_value
);
845 sequence_push(&(t
->labels
),str
);
847 if(has_value
) value
= loc_value
;
850 *label_value
= value
;
851 sequence_push(&(t
->labels_values
), label_value
);
854 getRAnglebracket(in
);
856 //read description if any. May be NULL.
857 str
= allocAndCopy(getDescription(in
));
858 sequence_push(&(t
->labels_description
),str
);
860 //next label definition
861 getLAnglebracket(in
);
862 token
= getToken(in
); //"label" or "/"
864 if(strcmp("/",token
))in
->error(in
, "not a valid enum definition");
866 if(strcmp("enum",token
))in
->error(in
, "not a valid enum definition");
867 getRAnglebracket(in
); //</label>
869 else if(strcmp(token
,"int_fixed") == 0) {
871 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
872 if(t
->size
== 0) in
->error(in
, "int has empty size");
874 getRAnglebracket(in
);
876 else if(strcmp(token
,"uint_fixed") == 0) {
877 t
->type
= UINT_FIXED
;
878 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
879 if(t
->size
== 0) in
->error(in
, "uint has empty size");
881 getRAnglebracket(in
);
883 else if(strcmp(token
,"char") == 0) {
885 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
888 getRAnglebracket(in
);
890 else if(strcmp(token
,"uchar") == 0) {
892 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
895 getRAnglebracket(in
);
897 else if(strcmp(token
,"short") == 0) {
899 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
902 getRAnglebracket(in
);
904 else if(strcmp(token
,"ushort") == 0) {
906 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
909 getRAnglebracket(in
);
911 else if(strcmp(token
,"int") == 0) {
913 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
915 getRAnglebracket(in
);
917 else if(strcmp(token
,"uint") == 0) {
919 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
921 getRAnglebracket(in
);
924 else if(strcmp(token
,"pointer") == 0) {
926 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
928 getRAnglebracket(in
);
930 else if(strcmp(token
,"long") == 0) {
932 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
934 getRAnglebracket(in
);
936 else if(strcmp(token
,"ulong") == 0) {
938 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
940 getRAnglebracket(in
);
942 else if(strcmp(token
,"size_t") == 0) {
944 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
946 getRAnglebracket(in
);
948 else if(strcmp(token
,"ssize_t") == 0) {
950 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
952 getRAnglebracket(in
);
954 else if(strcmp(token
,"off_t") == 0) {
956 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
958 getRAnglebracket(in
);
960 else if(strcmp(token
,"float") == 0) {
962 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
964 getRAnglebracket(in
);
966 else if(strcmp(token
,"string") == 0) {
968 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
970 getRAnglebracket(in
);
972 else if(strcmp(token
,"typeref") == 0){
973 // Must be a named type
975 sequence_pop(unnamed_types
);
976 token
= getNameAttribute(in
);
977 t
= find_named_type(token
, named_types
);
978 if(t
== NULL
) in
->error(in
,"Named referred to must be pre-declared.");
979 getForwardslash(in
); //<typeref name=type_name/>
980 getRAnglebracket(in
);
982 }else in
->error(in
,"not a valid type");
987 /*****************************************************************************
989 * find_named_type : find a named type from hash table
992 * named_types : array of named types
994 * type_descriptor * : a type descriptor
995 *****************************************************************************/
997 type_descriptor_t
* find_named_type(char *name
, table_t
* named_types
)
999 type_descriptor_t
*t
;
1001 t
= (type_descriptor_t
*)table_find(named_types
,name
);
1006 type_descriptor_t
* create_named_type(char *name
, table_t
* named_types
)
1008 type_descriptor_t
*t
;
1010 t
= (type_descriptor_t
*)memAlloc(sizeof(type_descriptor_t
));
1011 t
->type_name
= allocAndCopy(name
);
1014 table_insert(named_types
,t
->type_name
,t
);
1015 // table_insert(named_types,allocAndCopy(name),t);
1019 /*****************************************************************************
1021 * parseTypeDefinition : get type information from type definition
1023 * in : input file handle
1024 * unnamed_types : array of unamed types
1025 * named_types : array of named types
1026 *****************************************************************************/
1028 void parseTypeDefinition(parse_file_t
* in
, sequence_t
* unnamed_types
,
1029 table_t
* named_types
)
1032 type_descriptor_t
*t
;
1034 token
= getNameAttribute(in
);
1035 if(token
== NULL
) in
->error(in
, "Type has empty name");
1036 t
= create_named_type(token
, named_types
);
1038 if(t
->type
!= NONE
) in
->error(in
,"redefinition of named type");
1039 getRAnglebracket(in
); //<type name=type_name>
1040 getLAnglebracket(in
); //<
1041 token
= getName(in
);
1042 //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
1044 parseType(in
,t
, unnamed_types
, named_types
);
1047 getLAnglebracket(in
);
1048 getForwardslash(in
);
1049 token
= getName(in
);
1050 if(strcmp("type",token
))in
->error(in
,"not a valid type definition");
1051 getRAnglebracket(in
); //</type>
1054 /**************************************************************************
1056 * getComa, getName, getNumber, getEqual
1058 * Read a token from the input file, check its type, return it scontent.
1061 * in , input file handle.
1064 * address of token content.
1066 **************************************************************************/
1068 char *getName(parse_file_t
* in
)
1072 token
= getToken(in
);
1073 // Optional descriptions
1074 // if(in->type != NAME) in->error(in,"Name token was expected");
1078 int getNumber(parse_file_t
* in
)
1082 token
= getToken(in
);
1083 if(in
->type
!= NUMBER
) in
->error(in
, "Number token was expected");
1087 char *getForwardslash(parse_file_t
* in
)
1091 token
= getToken(in
);
1092 //if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected");
1093 /* Mathieu : final / is optional now. */
1094 if(in
->type
!= FORWARDSLASH
) ungetToken(in
);
1099 char *getLAnglebracket(parse_file_t
* in
)
1103 token
= getToken(in
);
1104 if(in
->type
!= LANGLEBRACKET
) in
->error(in
, "Left angle bracket was expected");
1108 char *getRAnglebracket(parse_file_t
* in
)
1112 token
= getToken(in
);
1113 if(in
->type
!= RANGLEBRACKET
) in
->error(in
, "Right angle bracket was expected");
1117 char *getQuotedString(parse_file_t
* in
)
1121 token
= getToken(in
);
1122 if(in
->type
!= QUOTEDSTRING
) in
->error(in
, "quoted string was expected");
1126 char * getEqual(parse_file_t
*in
)
1130 token
= getToken(in
);
1131 if(in
->type
!= EQUAL
) in
->error(in
, "equal was expected");
1135 int seekNextChar(parse_file_t
*in
)
1138 while((car
= getc(in
->fp
)) != EOF
) {
1147 /******************************************************************
1149 * getToken, ungetToken
1151 * Read a token from the input file and return its type and content.
1152 * Line numbers are accounted for and whitespace/comments are skipped.
1155 * in, input file handle.
1158 * address of token content.
1160 ******************************************************************/
1162 void ungetToken(parse_file_t
* in
)
1167 char *getToken(parse_file_t
* in
)
1171 int pos
= 0, escaped
;
1173 if(in
->unget
== 1) {
1178 /* skip whitespace and comments */
1180 while((car
= getc(fp
)) != EOF
) {
1183 if(car1
== '*') skipComment(in
);
1184 else if(car1
== '/') skipEOL(in
);
1186 car1
= ungetc(car1
,fp
);
1190 else if(car
== '\n') in
->lineno
++;
1191 else if(!isspace(car
)) break;
1199 in
->type
= FORWARDSLASH
;
1200 in
->buffer
[pos
] = car
;
1204 in
->type
= LANGLEBRACKET
;
1205 in
->buffer
[pos
] = car
;
1209 in
->type
= RANGLEBRACKET
;
1210 in
->buffer
[pos
] = car
;
1215 in
->buffer
[pos
] = car
;
1220 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1221 if(car
== '\\' && escaped
== 0) {
1222 in
->buffer
[pos
] = car
;
1227 if(car
== '"' && escaped
== 0) break;
1228 if(car
== '\n' && escaped
== 0) {
1229 in
->error(in
, "non escaped newline inside quoted string");
1231 if(car
== '\n') in
->lineno
++;
1232 in
->buffer
[pos
] = car
;
1236 if(car
== EOF
) in
->error(in
,"no ending quotemark");
1237 if(pos
== BUFFER_SIZE
) in
->error(in
, "quoted string token too large");
1238 in
->type
= QUOTEDSTRING
;
1242 in
->buffer
[pos
] = car
;
1244 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1249 in
->buffer
[pos
] = car
;
1252 if(car
== EOF
) ungetc(car
,fp
);
1253 if(pos
== BUFFER_SIZE
) in
->error(in
, "number token too large");
1256 else if(isalnum(car
) || car
== '_' || car
== '-') {
1257 in
->buffer
[0] = car
;
1259 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1260 if(!(isalnum(car
) || car
== '_' || car
== '-')) {
1264 in
->buffer
[pos
] = car
;
1267 if(car
== EOF
) ungetc(car
,fp
);
1268 if(pos
== BUFFER_SIZE
) in
->error(in
, "name token too large");
1270 } else if(car
== '?') {
1271 in
->buffer
[0] = car
;
1274 else in
->error(in
, "invalid character, unrecognized token");
1276 in
->buffer
[pos
] = 0;
1280 void skipComment(parse_file_t
* in
)
1283 while((car
= getc(in
->fp
)) != EOF
) {
1284 if(car
== '\n') in
->lineno
++;
1285 else if(car
== '*') {
1287 if(car
==EOF
) break;
1288 if(car
== '/') return;
1292 if(car
== EOF
) in
->error(in
,"comment begining with '/*' has no ending '*/'");
1295 void skipEOL(parse_file_t
* in
)
1298 while((car
= getc(in
->fp
)) != EOF
) {
1304 if(car
== EOF
)ungetc(car
, in
->fp
);
1307 /*****************************************************************************
1309 * checkNamedTypesImplemented : check if all named types have definition
1310 ****************************************************************************/
1312 void checkNamedTypesImplemented(table_t
* named_types
)
1314 type_descriptor_t
*t
;
1318 for(pos
= 0 ; pos
< named_types
->values
.position
; pos
++) {
1319 t
= (type_descriptor_t
*) named_types
->values
.array
[pos
];
1320 if(t
->type
== NONE
){
1321 sprintf(str
,"named type '%s' has no definition",
1322 (char*)named_types
->keys
.array
[pos
]);
1323 error_callback(NULL
,str
);
1329 /*****************************************************************************
1331 * generateChecksum : generate checksum for the facility
1333 * facName : name of facility
1335 * checksum : checksum for the facility
1336 ****************************************************************************/
1338 void generateChecksum(char* facName
,
1339 unsigned int * checksum
, sequence_t
* events
)
1346 crc
= crc32(facName
);
1347 for(pos
= 0; pos
< events
->position
; pos
++){
1348 ev
= (event_t
*)(events
->array
[pos
]);
1349 crc
= partial_crc32(ev
->name
, crc
);
1350 for(i
= 0; i
< ev
->fields
.position
; i
++) {
1351 field_t
*f
= (field_t
*)ev
->fields
.array
[i
];
1352 crc
= partial_crc32(f
->name
, crc
);
1353 crc
= getTypeChecksum(crc
, f
->type
);
1359 /*****************************************************************************
1361 * getTypeChecksum : generate checksum by type info
1363 * crc : checksum generated so far
1364 * type : type descriptor containing type info
1366 * unsigned long : checksum
1367 *****************************************************************************/
1369 unsigned long getTypeChecksum(unsigned long aCrc
, type_descriptor_t
* type
)
1371 unsigned long crc
= aCrc
;
1372 char * str
= NULL
, buf
[16];
1378 str
= intOutputTypes
[getSizeindex(type
->size
)];
1381 str
= uintOutputTypes
[getSizeindex(type
->size
)];
1384 str
= allocAndCopy("void *");
1388 str
= allocAndCopy("signed char");
1392 str
= allocAndCopy("unsigned char");
1396 str
= allocAndCopy("short");
1400 str
= allocAndCopy("unsigned short");
1404 str
= allocAndCopy("int");
1408 str
= allocAndCopy("uint");
1412 str
= allocAndCopy("long");
1416 str
= allocAndCopy("unsigned long");
1420 str
= allocAndCopy("size_t");
1424 str
= allocAndCopy("ssize_t");
1428 str
= allocAndCopy("off_t");
1432 str
= floatOutputTypes
[getSizeindex(type
->size
)];
1435 str
= allocAndCopy("string");
1439 //str = appendString("enum ", uintOutputTypes[getSizeindex(type->size)]);
1440 str
= allocAndCopy("enum");
1444 sprintf(buf
,"%zu", type
->size
);
1445 str
= appendString("array ",buf
);
1449 str
= allocAndCopy("sequence ");
1453 str
= allocAndCopy("struct");
1457 str
= allocAndCopy("union");
1461 error_callback(NULL
, "named type has no definition");
1465 crc
= partial_crc32(str
,crc
);
1468 if(type
->fmt
) crc
= partial_crc32(type
->fmt
,crc
);
1470 if(type
->type
== ARRAY
){
1471 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1472 } else if(type
->type
==SEQUENCE
) {
1473 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1474 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[1])->type
);
1475 } else if(type
->type
== STRUCT
|| type
->type
== UNION
){
1476 for(pos
=0; pos
< type
->fields
.position
; pos
++){
1477 fld
= (field_t
*) type
->fields
.array
[pos
];
1478 crc
= partial_crc32(fld
->name
,crc
);
1479 crc
= getTypeChecksum(crc
, fld
->type
);
1481 }else if(type
->type
== ENUM
){
1482 for(pos
= 0; pos
< type
->labels
.position
; pos
++)
1483 crc
= partial_crc32((char*)type
->labels
.array
[pos
],crc
);
1490 /* Event type descriptors */
1491 void freeType(type_descriptor_t
* tp
)
1496 if(tp
->fmt
!= NULL
) free(tp
->fmt
);
1497 if(tp
->type
== ENUM
) {
1498 for(pos2
= 0; pos2
< tp
->labels
.position
; pos2
++) {
1499 free(tp
->labels
.array
[pos2
]);
1501 sequence_dispose(&(tp
->labels
));
1502 for(pos2
= 0; pos2
< tp
->labels_values
.position
; pos2
++) {
1503 free(tp
->labels_values
.array
[pos2
]);
1505 sequence_dispose(&(tp
->labels_values
));
1507 if(tp
->type
== STRUCT
) {
1508 for(pos2
= 0; pos2
< tp
->fields
.position
; pos2
++) {
1509 f
= (field_t
*) tp
->fields
.array
[pos2
];
1511 free(f
->description
);
1514 sequence_dispose(&(tp
->fields
));
1518 void freeNamedType(table_t
* t
)
1521 type_descriptor_t
* td
;
1523 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1524 free((char *)t
->keys
.array
[pos
]);
1525 td
= (type_descriptor_t
*)t
->values
.array
[pos
];
1531 void freeTypes(sequence_t
*t
)
1534 type_descriptor_t
*tp
;
1536 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1537 tp
= (type_descriptor_t
*)t
->array
[pos
];
1543 void freeEvents(sequence_t
*t
)
1548 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1549 ev
= (event_t
*) t
->array
[pos
];
1551 free(ev
->description
);
1552 sequence_dispose(&ev
->fields
);
1559 /* Extensible array */
1561 void sequence_init(sequence_t
*t
)
1565 t
->array
= (void **)memAlloc(t
->size
* sizeof(void *));
1568 void sequence_dispose(sequence_t
*t
)
1575 void sequence_push(sequence_t
*t
, void *elem
)
1579 if(t
->position
>= t
->size
) {
1581 t
->array
= (void **)memAlloc(t
->size
* 2 * sizeof(void *));
1582 memcpy(t
->array
, tmp
, t
->size
* sizeof(void *));
1583 t
->size
= t
->size
* 2;
1586 t
->array
[t
->position
] = elem
;
1590 void *sequence_pop(sequence_t
*t
)
1592 if(t
->position
== 0) printf("Error : trying to pop an empty sequence");
1593 return t
->array
[--t
->position
];
1597 /* Hash table API, implementation is just linear search for now */
1599 void table_init(table_t
*t
)
1601 sequence_init(&(t
->keys
));
1602 sequence_init(&(t
->values
));
1605 void table_dispose(table_t
*t
)
1607 sequence_dispose(&(t
->keys
));
1608 sequence_dispose(&(t
->values
));
1611 void table_insert(table_t
*t
, char *key
, void *value
)
1613 sequence_push(&(t
->keys
),key
);
1614 sequence_push(&(t
->values
),value
);
1617 void *table_find(table_t
*t
, char *key
)
1620 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1621 if(strcmp((char *)key
,(char *)t
->keys
.array
[pos
]) == 0)
1622 return(t
->values
.array
[pos
]);
1627 void table_insert_int(table_t
*t
, int *key
, void *value
)
1629 sequence_push(&(t
->keys
),key
);
1630 sequence_push(&(t
->values
),value
);
1633 void *table_find_int(table_t
*t
, int *key
)
1636 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1637 if(*key
== *(int *)t
->keys
.array
[pos
])
1638 return(t
->values
.array
[pos
]);
1644 /* Concatenate strings */
1646 char *appendString(char *s
, char *suffix
)
1649 if(suffix
== NULL
) return s
;
1651 tmp
= (char *)memAlloc(strlen(s
) + strlen(suffix
) + 1);
This page took 0.063731 seconds and 4 git commands to generate.