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
)
101 token
= getToken(in
);
102 if(in
->type
== NUMBER
) {
103 return strtoull(token
, NULL
, 0);
105 in
->error(in
,"incorrect size specification");
109 /*****************************************************************************
111 * error_callback : print out error info
113 * in : input file handle
114 * msg : message to be printed
115 ****************************************************************************/
117 void error_callback(parse_file_t
*in
, char *msg
)
120 printf("Error in file %s, line %d: %s\n", in
->name
, in
->lineno
, msg
);
127 /*****************************************************************************
129 * memAlloc : allocate memory
131 * size : required memory size
133 * void * : pointer to allocate memory or NULL
134 ****************************************************************************/
136 void * memAlloc(int size
)
139 if(size
== 0) return NULL
;
142 printf("Failed to allocate memory");
148 /*****************************************************************************
150 * allocAndCopy : allocate memory and initialize it
152 * str : string to be put in memory
154 * char * : pointer to allocate memory or NULL
155 ****************************************************************************/
157 char *allocAndCopy(char *str
)
160 if(str
== NULL
) return NULL
;
161 addr
= (char *)memAlloc(strlen(str
)+1);
166 /**************************************************************************
170 * Read the attribute from the input file.
173 * in , input file handle.
174 * t , the type descriptor to fill.
176 **************************************************************************/
178 void getTypeAttributes(parse_file_t
*in
, type_descriptor_t
*t
,
179 sequence_t
* unnamed_types
, table_t
* named_types
)
188 token
= getToken(in
);
189 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
194 if(!strcmp("format",token
)) {
196 t
->fmt
= allocAndCopy(getQuotedString(in
));
197 //} else if(!strcmp("name",token)) {
199 // car = seekNextChar(in);
200 // if(car == EOF) in->error(in,"name was expected");
201 // else if(car == '\"') t->type_name = allocAndCopy(getQuotedString(in));
202 // else t->type_name = allocAndCopy(getName(in));
203 } else if(!strcmp("size",token
)) {
205 t
->size
= getSize(in
);
206 } else if(!strcmp("custom_write", token
)) {
212 /**************************************************************************
216 * Read the attribute from the input file.
219 * in , input file handle.
220 * ev , the event to fill.
222 **************************************************************************/
224 void getEventAttributes(parse_file_t
*in
, event_t
*ev
)
231 ev
->per_tracefile
= 0;
234 token
= getToken(in
);
235 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
240 if(!strcmp("name",token
)) {
242 car
= seekNextChar(in
);
243 if(car
== EOF
) in
->error(in
,"name was expected");
244 else if(car
== '\"') ev
->name
= allocAndCopy(getQuotedString(in
));
245 else ev
->name
= allocAndCopy(getName(in
));
246 } else if(!strcmp("per_trace", token
)) {
248 } else if(!strcmp("per_tracefile", token
)) {
249 ev
->per_tracefile
= 1;
255 /**************************************************************************
257 * getFacilityAttributes
259 * Read the attribute from the input file.
262 * in , input file handle.
263 * fac , the facility to fill.
265 **************************************************************************/
267 void getFacilityAttributes(parse_file_t
*in
, facility_t
*fac
)
276 token
= getToken(in
);
277 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
282 if(!strcmp("name",token
)) {
284 car
= seekNextChar(in
);
285 if(car
== EOF
) in
->error(in
,"name was expected");
286 else if(car
== '\"') fac
->name
= allocAndCopy(getQuotedString(in
));
287 else fac
->name
= allocAndCopy(getName(in
));
288 } else if(!strcmp("arch", token
)) {
290 car
= seekNextChar(in
);
291 if(car
== '\"') fac
->name
= allocAndCopy(getQuotedString(in
));
292 else fac
->arch
= allocAndCopy(getName(in
));
297 /**************************************************************************
301 * Read the attribute from the input file.
304 * in , input file handle.
305 * f , the field to fill.
307 **************************************************************************/
309 void getFieldAttributes(parse_file_t
*in
, field_t
*f
)
317 token
= getToken(in
);
318 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
323 if(!strcmp("name",token
)) {
325 car
= seekNextChar(in
);
326 if(car
== EOF
) in
->error(in
,"name was expected");
327 else if(car
== '\"') f
->name
= allocAndCopy(getQuotedString(in
));
328 else f
->name
= allocAndCopy(getName(in
));
333 char *getNameAttribute(parse_file_t
*in
)
340 token
= getToken(in
);
341 if(!strcmp("name",token
)) {
343 car
= seekNextChar(in
);
344 if(car
== EOF
) in
->error(in
,"name was expected");
345 else if(car
== '\"') name
= allocAndCopy(getQuotedString(in
));
346 else name
= allocAndCopy(getName(in
));
353 if(name
== NULL
) in
->error(in
, "Name was expected");
359 //for <label name=label_name value=n format="..."/>, value is an option
360 char * getValueStrAttribute(parse_file_t
*in
)
364 token
= getToken(in
);
365 if(strcmp("/",token
) == 0){
370 if(strcmp("value",token
))in
->error(in
,"value was expected");
372 token
= getToken(in
);
373 if(in
->type
!= NUMBER
) in
->error(in
,"number was expected");
377 char * getDescription(parse_file_t
*in
)
380 char * token
, car
, *str
;
384 getLAnglebracket(in
);
386 if(strcmp("description",token
)){
387 fseek(in
->fp
, pos
, SEEK_SET
);
391 getRAnglebracket(in
);
394 while((car
= getc(in
->fp
)) != EOF
) {
395 if(car
== '<') break;
396 if(car
== '\0') continue;
397 in
->buffer
[pos
] = car
;
400 if(car
== EOF
)in
->error(in
,"not a valid description");
401 in
->buffer
[pos
] = '\0';
403 str
= allocAndCopy(in
->buffer
);
407 if(strcmp("description", token
))in
->error(in
,"not a valid description");
408 getRAnglebracket(in
);
413 /*****************************************************************************
415 * parseFacility : generate event list
417 * in : input file handle
418 * fac : empty facility
420 * fac : facility filled with event list
421 ****************************************************************************/
423 void parseFacility(parse_file_t
*in
, facility_t
* fac
)
428 getFacilityAttributes(in
, fac
);
429 if(fac
->name
== NULL
) in
->error(in
, "Attribute not named");
431 fac
->capname
= allocAndCopy(fac
->name
);
432 strupper(fac
->capname
);
433 getRAnglebracket(in
);
435 fac
->description
= getDescription(in
);
438 getLAnglebracket(in
);
440 token
= getToken(in
);
441 if(in
->type
== ENDFILE
)
442 in
->error(in
,"the definition of the facility is not finished");
444 if(strcmp("event",token
) == 0){
445 ev
= (event_t
*) memAlloc(sizeof(event_t
));
446 sequence_push(&(fac
->events
),ev
);
447 parseEvent(in
,ev
, &(fac
->unnamed_types
), &(fac
->named_types
));
448 }else if(strcmp("type",token
) == 0){
449 parseTypeDefinition(in
, &(fac
->unnamed_types
), &(fac
->named_types
));
450 }else if(in
->type
== FORWARDSLASH
){
452 }else in
->error(in
,"event or type token expected\n");
456 if(strcmp("facility",token
)) in
->error(in
,"not the end of the facility");
457 getRAnglebracket(in
); //</facility>
460 /*****************************************************************************
462 * parseEvent : generate event from event definition
464 * in : input file handle
466 * unnamed_types : array of unamed types
467 * named_types : array of named types
469 * ev : new event (parameters are passed to it)
470 ****************************************************************************/
472 void parseEvent(parse_file_t
*in
, event_t
* ev
, sequence_t
* unnamed_types
,
473 table_t
* named_types
)
478 sequence_init(&(ev
->fields
));
479 //<event name=eventtype_name>
480 getEventAttributes(in
, ev
);
481 if(ev
->name
== NULL
) in
->error(in
, "Event not named");
482 getRAnglebracket(in
);
484 //<description>...</description>
485 ev
->description
= getDescription(in
);
488 /* Events can have multiple fields. each field form at least a function
489 * parameter of the logging function. */
491 getLAnglebracket(in
);
492 token
= getToken(in
);
495 case FORWARDSLASH
: /* </event> */
497 if(strcmp("event",token
))in
->error(in
,"not an event definition");
498 getRAnglebracket(in
); //</event>
501 case NAME
: /* a field */
502 if(strcmp("field",token
))in
->error(in
,"expecting a field");
503 f
= (field_t
*)memAlloc(sizeof(field_t
));
504 sequence_push(&(ev
->fields
),f
);
505 parseFields(in
, f
, unnamed_types
, named_types
, 1);
508 in
->error(in
, "expecting </event> or <field >");
513 if(in
->type
== FORWARDSLASH
){ //</event> NOTHING
515 }else if(in
->type
== NAME
){
516 if(strcmp("struct",token
)==0 || strcmp("typeref",token
)==0){
518 ev
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
519 if(ev
->type
->type
!= STRUCT
&& ev
->type
->type
!= NONE
)
520 in
->error(in
,"type must be a struct");
521 }else in
->error(in
, "not a valid type");
523 getLAnglebracket(in
);
525 }else in
->error(in
,"not a struct type");
526 getLAnglebracket(in
);
529 if(strcmp("event",token
))in
->error(in
,"not an event definition");
530 getRAnglebracket(in
); //</event>
534 /*****************************************************************************
536 * parseField : get field infomation from buffer
538 * in : input file handle
540 * unnamed_types : array of unamed types
541 * named_types : array of named types
542 * tag : is field surrounded by a <field> </field> tag ?
543 ****************************************************************************/
545 void parseFields(parse_file_t
*in
, field_t
*f
,
546 sequence_t
* unnamed_types
,
547 table_t
* named_types
,
552 //<field name=field_name> <description> <type> </field>
553 getFieldAttributes(in
, f
);
554 if(f
->name
== NULL
) in
->error(in
, "Field not named");
555 getRAnglebracket(in
);
557 f
->description
= getDescription(in
);
561 getLAnglebracket(in
);
562 f
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
565 getLAnglebracket(in
);
568 if(strcmp("field",token
))in
->error(in
,"not a valid field definition");
569 getRAnglebracket(in
); //</field>
574 /*****************************************************************************
576 * parseType : get type information, type can be :
578 * int(size,fmt); uint(size,fmt); float(size,fmt);
579 * string(fmt); enum(size,fmt,(label1,label2...))
581 * array(arraySize, type); sequence(lengthSize,type)
582 * struct(field(name,type,description)...)
586 * in : input file handle
587 * inType : a type descriptor
588 * unnamed_types : array of unamed types
589 * named_types : array of named types
591 * type_descriptor* : a type descriptor
592 ****************************************************************************/
594 type_descriptor_t
*parseType(parse_file_t
*in
, type_descriptor_t
*inType
,
595 sequence_t
* unnamed_types
, table_t
* named_types
)
598 type_descriptor_t
*t
;
602 t
= (type_descriptor_t
*) memAlloc(sizeof(type_descriptor_t
));
606 sequence_push(unnamed_types
,t
);
612 if(strcmp(token
,"struct") == 0) {
614 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
615 getRAnglebracket(in
); //<struct>
616 getLAnglebracket(in
); //<field name=..>
617 token
= getToken(in
);
618 sequence_init(&(t
->fields
));
619 while(strcmp("field",token
) == 0){
620 f
= (field_t
*)memAlloc(sizeof(field_t
));
621 sequence_push(&(t
->fields
),f
);
623 parseFields(in
, f
, unnamed_types
, named_types
, 1);
626 getLAnglebracket(in
);
627 token
= getToken(in
);
629 if(strcmp("/",token
))in
->error(in
,"not a valid structure definition");
631 if(strcmp("struct",token
)!=0)
632 in
->error(in
,"not a valid structure definition");
633 getRAnglebracket(in
); //</struct>
635 else if(strcmp(token
,"union") == 0) {
637 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
638 getRAnglebracket(in
); //<union>
640 getLAnglebracket(in
); //<field name=..>
641 token
= getToken(in
);
642 sequence_init(&(t
->fields
));
643 while(strcmp("field",token
) == 0){
644 f
= (field_t
*)memAlloc(sizeof(field_t
));
645 sequence_push(&(t
->fields
),f
);
646 parseFields(in
, f
, unnamed_types
, named_types
, 1);
649 getLAnglebracket(in
);
650 token
= getToken(in
);
652 if(strcmp("/",token
))in
->error(in
,"not a valid union definition");
654 if(strcmp("union",token
)!=0)
655 in
->error(in
,"not a valid union definition");
656 getRAnglebracket(in
); //</union>
658 else if(strcmp(token
,"array") == 0) {
660 sequence_init(&(t
->fields
));
661 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
662 if(t
->size
== 0) in
->error(in
, "Array has empty size");
664 getRAnglebracket(in
); //<array size=n>
666 //getLAnglebracket(in); //<subtype>
668 f
= (field_t
*)memAlloc(sizeof(field_t
));
669 sequence_push(&(t
->fields
),f
);
670 parseFields(in
, f
, unnamed_types
, named_types
, 0);
672 //getLAnglebracket(in); //<type struct>
673 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
675 getLAnglebracket(in
); //</array>
678 if(strcmp("array",token
))in
->error(in
,"not a valid array definition");
679 getRAnglebracket(in
); //</array>
681 else if(strcmp(token
,"sequence") == 0) {
683 sequence_init(&(t
->fields
));
684 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
686 getRAnglebracket(in
); //<sequence>
688 //getLAnglebracket(in); //<sequence size type>
690 f
= (field_t
*)memAlloc(sizeof(field_t
));
691 sequence_push(&(t
->fields
),f
);
692 parseFields(in
, f
, unnamed_types
, named_types
, 0);
694 //getLAnglebracket(in); //<subtype>
696 f
= (field_t
*)memAlloc(sizeof(field_t
));
697 sequence_push(&(t
->fields
),f
);
698 parseFields(in
, f
, unnamed_types
, named_types
, 0);
700 //getLAnglebracket(in); //<type sequence>
701 //t->length_type = parseType(in, NULL, unnamed_types, named_types);
703 //getLAnglebracket(in); //<type sequence>
705 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
707 if(t
->fields
.position
< 1) in
->error(in
, "Sequence has no length type");
708 if(t
->fields
.position
< 2) in
->error(in
, "Sequence has no subtype");
709 switch(((field_t
*)t
->fields
.array
[0])->type
->type
) {
719 in
->error(in
, "Wrong length type for sequence");
722 getLAnglebracket(in
); //</sequence>
725 if(strcmp("sequence",token
))in
->error(in
,"not a valid sequence definition");
726 getRAnglebracket(in
); //</sequence>
728 else if(strcmp(token
,"enum") == 0) {
733 sequence_init(&(t
->labels
));
734 sequence_init(&(t
->labels_values
));
735 sequence_init(&(t
->labels_description
));
736 t
->already_printed
= 0;
737 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
738 //if(t->size == 0) in->error(in, "Sequence has empty size");
739 //Mathieu : we fix enum size to target int size. GCC is always like this.
740 //fox copy optimisation.
741 if(t
->size
!= 0) in
->error(in
, "Enum has fixed size of target int.");
743 getRAnglebracket(in
);
745 //<label name=label1 value=n/>
746 getLAnglebracket(in
);
747 token
= getToken(in
); //"label" or "/"
748 while(strcmp("label",token
) == 0){
749 int *label_value
= malloc(sizeof(int));
751 str
= allocAndCopy(getNameAttribute(in
));
752 token
= getValueStrAttribute(in
);
754 sequence_push(&(t
->labels
),str
);
756 if(token
) value
= strtol(token
, NULL
, 0);
759 *label_value
= value
;
760 sequence_push(&(t
->labels_values
), label_value
);
763 getRAnglebracket(in
);
765 //read description if any. May be NULL.
766 str
= allocAndCopy(getDescription(in
));
767 sequence_push(&(t
->labels_description
),str
);
769 //next label definition
770 getLAnglebracket(in
);
771 token
= getToken(in
); //"label" or "/"
773 if(strcmp("/",token
))in
->error(in
, "not a valid enum definition");
775 if(strcmp("enum",token
))in
->error(in
, "not a valid enum definition");
776 getRAnglebracket(in
); //</label>
778 else if(strcmp(token
,"int_fixed") == 0) {
780 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
781 if(t
->size
== 0) in
->error(in
, "int has empty size");
783 getRAnglebracket(in
);
785 else if(strcmp(token
,"uint_fixed") == 0) {
786 t
->type
= UINT_FIXED
;
787 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
788 if(t
->size
== 0) in
->error(in
, "uint has empty size");
790 getRAnglebracket(in
);
792 else if(strcmp(token
,"char") == 0) {
794 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
797 getRAnglebracket(in
);
799 else if(strcmp(token
,"uchar") == 0) {
801 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
804 getRAnglebracket(in
);
806 else if(strcmp(token
,"short") == 0) {
808 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
811 getRAnglebracket(in
);
813 else if(strcmp(token
,"ushort") == 0) {
815 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
818 getRAnglebracket(in
);
820 else if(strcmp(token
,"int") == 0) {
822 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
824 getRAnglebracket(in
);
826 else if(strcmp(token
,"uint") == 0) {
828 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
830 getRAnglebracket(in
);
833 else if(strcmp(token
,"pointer") == 0) {
835 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
837 getRAnglebracket(in
);
839 else if(strcmp(token
,"long") == 0) {
841 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
843 getRAnglebracket(in
);
845 else if(strcmp(token
,"ulong") == 0) {
847 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
849 getRAnglebracket(in
);
851 else if(strcmp(token
,"size_t") == 0) {
853 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
855 getRAnglebracket(in
);
857 else if(strcmp(token
,"ssize_t") == 0) {
859 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
861 getRAnglebracket(in
);
863 else if(strcmp(token
,"off_t") == 0) {
865 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
867 getRAnglebracket(in
);
869 else if(strcmp(token
,"float") == 0) {
871 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
873 getRAnglebracket(in
);
875 else if(strcmp(token
,"string") == 0) {
877 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
879 getRAnglebracket(in
);
881 else if(strcmp(token
,"typeref") == 0){
882 // Must be a named type
884 sequence_pop(unnamed_types
);
885 token
= getNameAttribute(in
);
886 t
= find_named_type(token
, named_types
);
887 if(t
== NULL
) in
->error(in
,"Named referred to must be pre-declared.");
888 getForwardslash(in
); //<typeref name=type_name/>
889 getRAnglebracket(in
);
891 }else in
->error(in
,"not a valid type");
896 /*****************************************************************************
898 * find_named_type : find a named type from hash table
901 * named_types : array of named types
903 * type_descriptor * : a type descriptor
904 *****************************************************************************/
906 type_descriptor_t
* find_named_type(char *name
, table_t
* named_types
)
908 type_descriptor_t
*t
;
910 t
= table_find(named_types
,name
);
915 type_descriptor_t
* create_named_type(char *name
, table_t
* named_types
)
917 type_descriptor_t
*t
;
919 t
= (type_descriptor_t
*)memAlloc(sizeof(type_descriptor_t
));
920 t
->type_name
= allocAndCopy(name
);
923 table_insert(named_types
,t
->type_name
,t
);
924 // table_insert(named_types,allocAndCopy(name),t);
928 /*****************************************************************************
930 * parseTypeDefinition : get type information from type definition
932 * in : input file handle
933 * unnamed_types : array of unamed types
934 * named_types : array of named types
935 *****************************************************************************/
937 void parseTypeDefinition(parse_file_t
* in
, sequence_t
* unnamed_types
,
938 table_t
* named_types
)
941 type_descriptor_t
*t
;
943 token
= getNameAttribute(in
);
944 if(token
== NULL
) in
->error(in
, "Type has empty name");
945 t
= create_named_type(token
, named_types
);
947 if(t
->type
!= NONE
) in
->error(in
,"redefinition of named type");
948 getRAnglebracket(in
); //<type name=type_name>
949 getLAnglebracket(in
); //<
951 //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
953 parseType(in
,t
, unnamed_types
, named_types
);
956 getLAnglebracket(in
);
959 if(strcmp("type",token
))in
->error(in
,"not a valid type definition");
960 getRAnglebracket(in
); //</type>
963 /**************************************************************************
965 * getComa, getName, getNumber, getEqual
967 * Read a token from the input file, check its type, return it scontent.
970 * in , input file handle.
973 * address of token content.
975 **************************************************************************/
977 char *getName(parse_file_t
* in
)
981 token
= getToken(in
);
982 // Optional descriptions
983 // if(in->type != NAME) in->error(in,"Name token was expected");
987 int getNumber(parse_file_t
* in
)
991 token
= getToken(in
);
992 if(in
->type
!= NUMBER
) in
->error(in
, "Number token was expected");
996 char *getForwardslash(parse_file_t
* in
)
1000 token
= getToken(in
);
1001 //if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected");
1002 /* Mathieu : final / is optional now. */
1003 if(in
->type
!= FORWARDSLASH
) ungetToken(in
);
1008 char *getLAnglebracket(parse_file_t
* in
)
1012 token
= getToken(in
);
1013 if(in
->type
!= LANGLEBRACKET
) in
->error(in
, "Left angle bracket was expected");
1017 char *getRAnglebracket(parse_file_t
* in
)
1021 token
= getToken(in
);
1022 if(in
->type
!= RANGLEBRACKET
) in
->error(in
, "Right angle bracket was expected");
1026 char *getQuotedString(parse_file_t
* in
)
1030 token
= getToken(in
);
1031 if(in
->type
!= QUOTEDSTRING
) in
->error(in
, "quoted string was expected");
1035 char * getEqual(parse_file_t
*in
)
1039 token
= getToken(in
);
1040 if(in
->type
!= EQUAL
) in
->error(in
, "equal was expected");
1044 char seekNextChar(parse_file_t
*in
)
1047 while((car
= getc(in
->fp
)) != EOF
) {
1056 /******************************************************************
1058 * getToken, ungetToken
1060 * Read a token from the input file and return its type and content.
1061 * Line numbers are accounted for and whitespace/comments are skipped.
1064 * in, input file handle.
1067 * address of token content.
1069 ******************************************************************/
1071 void ungetToken(parse_file_t
* in
)
1076 char *getToken(parse_file_t
* in
)
1080 int pos
= 0, escaped
;
1082 if(in
->unget
== 1) {
1087 /* skip whitespace and comments */
1089 while((car
= getc(fp
)) != EOF
) {
1092 if(car1
== '*') skipComment(in
);
1093 else if(car1
== '/') skipEOL(in
);
1095 car1
= ungetc(car1
,fp
);
1099 else if(car
== '\n') in
->lineno
++;
1100 else if(!isspace(car
)) break;
1108 in
->type
= FORWARDSLASH
;
1109 in
->buffer
[pos
] = car
;
1113 in
->type
= LANGLEBRACKET
;
1114 in
->buffer
[pos
] = car
;
1118 in
->type
= RANGLEBRACKET
;
1119 in
->buffer
[pos
] = car
;
1124 in
->buffer
[pos
] = car
;
1129 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1130 if(car
== '\\' && escaped
== 0) {
1131 in
->buffer
[pos
] = car
;
1136 if(car
== '"' && escaped
== 0) break;
1137 if(car
== '\n' && escaped
== 0) {
1138 in
->error(in
, "non escaped newline inside quoted string");
1140 if(car
== '\n') in
->lineno
++;
1141 in
->buffer
[pos
] = car
;
1145 if(car
== EOF
) in
->error(in
,"no ending quotemark");
1146 if(pos
== BUFFER_SIZE
) in
->error(in
, "quoted string token too large");
1147 in
->type
= QUOTEDSTRING
;
1151 in
->buffer
[pos
] = car
;
1153 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1158 in
->buffer
[pos
] = car
;
1161 if(car
== EOF
) ungetc(car
,fp
);
1162 if(pos
== BUFFER_SIZE
) in
->error(in
, "number token too large");
1165 else if(isalnum(car
) || car
== '_' || car
== '-') {
1166 in
->buffer
[0] = car
;
1168 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1169 if(!(isalnum(car
) || car
== '_' || car
== '-')) {
1173 in
->buffer
[pos
] = car
;
1176 if(car
== EOF
) ungetc(car
,fp
);
1177 if(pos
== BUFFER_SIZE
) in
->error(in
, "name token too large");
1180 else in
->error(in
, "invalid character, unrecognized token");
1182 in
->buffer
[pos
] = 0;
1186 void skipComment(parse_file_t
* in
)
1189 while((car
= getc(in
->fp
)) != EOF
) {
1190 if(car
== '\n') in
->lineno
++;
1191 else if(car
== '*') {
1193 if(car
==EOF
) break;
1194 if(car
== '/') return;
1198 if(car
== EOF
) in
->error(in
,"comment begining with '/*' has no ending '*/'");
1201 void skipEOL(parse_file_t
* in
)
1204 while((car
= getc(in
->fp
)) != EOF
) {
1210 if(car
== EOF
)ungetc(car
, in
->fp
);
1213 /*****************************************************************************
1215 * checkNamedTypesImplemented : check if all named types have definition
1216 ****************************************************************************/
1218 void checkNamedTypesImplemented(table_t
* named_types
)
1220 type_descriptor_t
*t
;
1224 for(pos
= 0 ; pos
< named_types
->values
.position
; pos
++) {
1225 t
= (type_descriptor_t
*) named_types
->values
.array
[pos
];
1226 if(t
->type
== NONE
){
1227 sprintf(str
,"named type '%s' has no definition",
1228 (char*)named_types
->keys
.array
[pos
]);
1229 error_callback(NULL
,str
);
1235 /*****************************************************************************
1237 * generateChecksum : generate checksum for the facility
1239 * facName : name of facility
1241 * checksum : checksum for the facility
1242 ****************************************************************************/
1244 void generateChecksum(char* facName
,
1245 unsigned int * checksum
, sequence_t
* events
)
1252 crc
= crc32(facName
);
1253 for(pos
= 0; pos
< events
->position
; pos
++){
1254 ev
= (event_t
*)(events
->array
[pos
]);
1255 crc
= partial_crc32(ev
->name
, crc
);
1256 for(i
= 0; i
< ev
->fields
.position
; i
++) {
1257 field_t
*f
= (field_t
*)ev
->fields
.array
[i
];
1258 crc
= partial_crc32(f
->name
, crc
);
1259 crc
= getTypeChecksum(crc
, f
->type
);
1265 /*****************************************************************************
1267 * getTypeChecksum : generate checksum by type info
1269 * crc : checksum generated so far
1270 * type : type descriptor containing type info
1272 * unsigned long : checksum
1273 *****************************************************************************/
1275 unsigned long getTypeChecksum(unsigned long aCrc
, type_descriptor_t
* type
)
1277 unsigned long crc
= aCrc
;
1278 char * str
= NULL
, buf
[16];
1284 str
= intOutputTypes
[getSizeindex(type
->size
)];
1287 str
= uintOutputTypes
[getSizeindex(type
->size
)];
1290 str
= allocAndCopy("void *");
1294 str
= allocAndCopy("signed char");
1298 str
= allocAndCopy("unsigned char");
1302 str
= allocAndCopy("short");
1306 str
= allocAndCopy("unsigned short");
1310 str
= allocAndCopy("int");
1314 str
= allocAndCopy("uint");
1318 str
= allocAndCopy("long");
1322 str
= allocAndCopy("unsigned long");
1326 str
= allocAndCopy("size_t");
1330 str
= allocAndCopy("ssize_t");
1334 str
= allocAndCopy("off_t");
1338 str
= floatOutputTypes
[getSizeindex(type
->size
)];
1341 str
= allocAndCopy("string");
1345 //str = appendString("enum ", uintOutputTypes[getSizeindex(type->size)]);
1346 str
= allocAndCopy("enum");
1350 sprintf(buf
,"%zu", type
->size
);
1351 str
= appendString("array ",buf
);
1355 str
= allocAndCopy("sequence ");
1359 str
= allocAndCopy("struct");
1363 str
= allocAndCopy("union");
1367 error_callback(NULL
, "named type has no definition");
1371 crc
= partial_crc32(str
,crc
);
1374 if(type
->fmt
) crc
= partial_crc32(type
->fmt
,crc
);
1376 if(type
->type
== ARRAY
){
1377 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1378 } else if(type
->type
==SEQUENCE
) {
1379 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1380 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[1])->type
);
1381 } else if(type
->type
== STRUCT
|| type
->type
== UNION
){
1382 for(pos
=0; pos
< type
->fields
.position
; pos
++){
1383 fld
= (field_t
*) type
->fields
.array
[pos
];
1384 crc
= partial_crc32(fld
->name
,crc
);
1385 crc
= getTypeChecksum(crc
, fld
->type
);
1387 }else if(type
->type
== ENUM
){
1388 for(pos
= 0; pos
< type
->labels
.position
; pos
++)
1389 crc
= partial_crc32((char*)type
->labels
.array
[pos
],crc
);
1396 /* Event type descriptors */
1397 void freeType(type_descriptor_t
* tp
)
1402 if(tp
->fmt
!= NULL
) free(tp
->fmt
);
1403 if(tp
->type
== ENUM
) {
1404 for(pos2
= 0; pos2
< tp
->labels
.position
; pos2
++) {
1405 free(tp
->labels
.array
[pos2
]);
1407 sequence_dispose(&(tp
->labels
));
1408 for(pos2
= 0; pos2
< tp
->labels_values
.position
; pos2
++) {
1409 free(tp
->labels_values
.array
[pos2
]);
1411 sequence_dispose(&(tp
->labels_values
));
1413 if(tp
->type
== STRUCT
) {
1414 for(pos2
= 0; pos2
< tp
->fields
.position
; pos2
++) {
1415 f
= (field_t
*) tp
->fields
.array
[pos2
];
1417 free(f
->description
);
1420 sequence_dispose(&(tp
->fields
));
1424 void freeNamedType(table_t
* t
)
1427 type_descriptor_t
* td
;
1429 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1430 free((char *)t
->keys
.array
[pos
]);
1431 td
= (type_descriptor_t
*)t
->values
.array
[pos
];
1437 void freeTypes(sequence_t
*t
)
1440 type_descriptor_t
*tp
;
1442 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1443 tp
= (type_descriptor_t
*)t
->array
[pos
];
1449 void freeEvents(sequence_t
*t
)
1454 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1455 ev
= (event_t
*) t
->array
[pos
];
1457 free(ev
->description
);
1458 sequence_dispose(&ev
->fields
);
1465 /* Extensible array */
1467 void sequence_init(sequence_t
*t
)
1471 t
->array
= (void **)memAlloc(t
->size
* sizeof(void *));
1474 void sequence_dispose(sequence_t
*t
)
1481 void sequence_push(sequence_t
*t
, void *elem
)
1485 if(t
->position
>= t
->size
) {
1487 t
->array
= (void **)memAlloc(t
->size
* 2 * sizeof(void *));
1488 memcpy(t
->array
, tmp
, t
->size
* sizeof(void *));
1489 t
->size
= t
->size
* 2;
1492 t
->array
[t
->position
] = elem
;
1496 void *sequence_pop(sequence_t
*t
)
1498 return t
->array
[t
->position
--];
1502 /* Hash table API, implementation is just linear search for now */
1504 void table_init(table_t
*t
)
1506 sequence_init(&(t
->keys
));
1507 sequence_init(&(t
->values
));
1510 void table_dispose(table_t
*t
)
1512 sequence_dispose(&(t
->keys
));
1513 sequence_dispose(&(t
->values
));
1516 void table_insert(table_t
*t
, char *key
, void *value
)
1518 sequence_push(&(t
->keys
),key
);
1519 sequence_push(&(t
->values
),value
);
1522 void *table_find(table_t
*t
, char *key
)
1525 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1526 if(strcmp((char *)key
,(char *)t
->keys
.array
[pos
]) == 0)
1527 return(t
->values
.array
[pos
]);
1532 void table_insert_int(table_t
*t
, int *key
, void *value
)
1534 sequence_push(&(t
->keys
),key
);
1535 sequence_push(&(t
->values
),value
);
1538 void *table_find_int(table_t
*t
, int *key
)
1541 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1542 if(*key
== *(int *)t
->keys
.array
[pos
])
1543 return(t
->values
.array
[pos
]);
1549 /* Concatenate strings */
1551 char *appendString(char *s
, char *suffix
)
1554 if(suffix
== NULL
) return s
;
1556 tmp
= (char *)memAlloc(strlen(s
) + strlen(suffix
) + 1);
This page took 0.062809 seconds and 4 git commands to generate.