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("align",token
)) {
208 t
->alignment
= getNumber(in
);
213 /**************************************************************************
217 * Read the attribute from the input file.
220 * in , input file handle.
221 * ev , the event to fill.
223 **************************************************************************/
225 void getEventAttributes(parse_file_t
*in
, event_t
*ev
)
232 ev
->per_tracefile
= 0;
235 token
= getToken(in
);
236 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
241 if(!strcmp("name",token
)) {
243 car
= seekNextChar(in
);
244 if(car
== EOF
) in
->error(in
,"name was expected");
245 else if(car
== '\"') ev
->name
= allocAndCopy(getQuotedString(in
));
246 else ev
->name
= allocAndCopy(getName(in
));
247 } else if(!strcmp("per_trace", token
)) {
249 } else if(!strcmp("per_tracefile", token
)) {
250 ev
->per_tracefile
= 1;
256 /**************************************************************************
258 * getFacilityAttributes
260 * Read the attribute from the input file.
263 * in , input file handle.
264 * fac , the facility to fill.
266 **************************************************************************/
268 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
));
292 /**************************************************************************
296 * Read the attribute from the input file.
299 * in , input file handle.
300 * f , the field to fill.
302 **************************************************************************/
304 void getFieldAttributes(parse_file_t
*in
, field_t
*f
)
312 token
= getToken(in
);
313 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
318 if(!strcmp("name",token
)) {
320 car
= seekNextChar(in
);
321 if(car
== EOF
) in
->error(in
,"name was expected");
322 else if(car
== '\"') f
->name
= allocAndCopy(getQuotedString(in
));
323 else f
->name
= allocAndCopy(getName(in
));
328 char *getNameAttribute(parse_file_t
*in
)
335 token
= getToken(in
);
336 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
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
));
349 if(name
== NULL
) in
->error(in
, "Name was expected");
356 //for <label name=label_name value=n format="..."/>, value is an option
357 char * getValueStrAttribute(parse_file_t
*in
)
361 token
= getToken(in
);
362 if(strcmp("/",token
) == 0){
367 if(strcmp("value",token
))in
->error(in
,"value was expected");
369 token
= getToken(in
);
370 if(in
->type
!= NUMBER
) in
->error(in
,"number was expected");
374 char * getDescription(parse_file_t
*in
)
377 char * token
, car
, *str
;
381 getLAnglebracket(in
);
383 if(strcmp("description",token
)){
384 fseek(in
->fp
, pos
, SEEK_SET
);
388 getRAnglebracket(in
);
391 while((car
= getc(in
->fp
)) != EOF
) {
392 if(car
== '<') break;
393 if(car
== '\0') continue;
394 in
->buffer
[pos
] = car
;
397 if(car
== EOF
)in
->error(in
,"not a valid description");
398 in
->buffer
[pos
] = '\0';
400 str
= allocAndCopy(in
->buffer
);
404 if(strcmp("description", token
))in
->error(in
,"not a valid description");
405 getRAnglebracket(in
);
410 /*****************************************************************************
412 * parseFacility : generate event list
414 * in : input file handle
415 * fac : empty facility
417 * fac : facility filled with event list
418 ****************************************************************************/
420 void parseFacility(parse_file_t
*in
, facility_t
* fac
)
425 getFacilityAttributes(in
, fac
);
426 if(fac
->name
== NULL
) in
->error(in
, "Attribute not named");
428 fac
->capname
= allocAndCopy(fac
->name
);
429 strupper(fac
->capname
);
430 getRAnglebracket(in
);
432 fac
->description
= getDescription(in
);
435 getLAnglebracket(in
);
437 token
= getToken(in
);
438 if(in
->type
== ENDFILE
)
439 in
->error(in
,"the definition of the facility is not finished");
441 if(strcmp("event",token
) == 0){
442 ev
= (event_t
*) memAlloc(sizeof(event_t
));
443 sequence_push(&(fac
->events
),ev
);
444 parseEvent(in
,ev
, &(fac
->unnamed_types
), &(fac
->named_types
));
445 }else if(strcmp("type",token
) == 0){
446 parseTypeDefinition(in
, &(fac
->unnamed_types
), &(fac
->named_types
));
447 }else if(in
->type
== FORWARDSLASH
){
449 }else in
->error(in
,"event or type token expected\n");
453 if(strcmp("facility",token
)) in
->error(in
,"not the end of the facility");
454 getRAnglebracket(in
); //</facility>
457 /*****************************************************************************
459 * parseEvent : generate event from event definition
461 * in : input file handle
463 * unnamed_types : array of unamed types
464 * named_types : array of named types
466 * ev : new event (parameters are passed to it)
467 ****************************************************************************/
469 void parseEvent(parse_file_t
*in
, event_t
* ev
, sequence_t
* unnamed_types
,
470 table_t
* named_types
)
475 sequence_init(&(ev
->fields
));
476 //<event name=eventtype_name>
477 getEventAttributes(in
, ev
);
478 if(ev
->name
== NULL
) in
->error(in
, "Event not named");
479 getRAnglebracket(in
);
481 //<description>...</description>
482 ev
->description
= getDescription(in
);
485 /* Events can have multiple fields. each field form at least a function
486 * parameter of the logging function. */
488 getLAnglebracket(in
);
489 token
= getToken(in
);
492 case FORWARDSLASH
: /* </event> */
494 if(strcmp("event",token
))in
->error(in
,"not an event definition");
495 getRAnglebracket(in
); //</event>
498 case NAME
: /* a field */
499 if(strcmp("field",token
))in
->error(in
,"expecting a field");
500 f
= (field_t
*)memAlloc(sizeof(field_t
));
501 sequence_push(&(ev
->fields
),f
);
502 parseFields(in
, f
, unnamed_types
, named_types
, 1);
505 in
->error(in
, "expecting </event> or <field >");
510 if(in
->type
== FORWARDSLASH
){ //</event> NOTHING
512 }else if(in
->type
== NAME
){
513 if(strcmp("struct",token
)==0 || strcmp("typeref",token
)==0){
515 ev
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
516 if(ev
->type
->type
!= STRUCT
&& ev
->type
->type
!= NONE
)
517 in
->error(in
,"type must be a struct");
518 }else in
->error(in
, "not a valid type");
520 getLAnglebracket(in
);
522 }else in
->error(in
,"not a struct type");
523 getLAnglebracket(in
);
526 if(strcmp("event",token
))in
->error(in
,"not an event definition");
527 getRAnglebracket(in
); //</event>
531 /*****************************************************************************
533 * parseField : get field infomation from buffer
535 * in : input file handle
537 * unnamed_types : array of unamed types
538 * named_types : array of named types
539 * tag : is field surrounded by a <field> </field> tag ?
540 ****************************************************************************/
542 void parseFields(parse_file_t
*in
, field_t
*f
,
543 sequence_t
* unnamed_types
,
544 table_t
* named_types
,
549 //<field name=field_name> <description> <type> </field>
550 getFieldAttributes(in
, f
);
551 if(f
->name
== NULL
) in
->error(in
, "Field not named");
552 getRAnglebracket(in
);
554 f
->description
= getDescription(in
);
558 getLAnglebracket(in
);
559 f
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
562 getLAnglebracket(in
);
565 if(strcmp("field",token
))in
->error(in
,"not a valid field definition");
566 getRAnglebracket(in
); //</field>
571 /*****************************************************************************
573 * parseType : get type information, type can be :
575 * int(size,fmt); uint(size,fmt); float(size,fmt);
576 * string(fmt); enum(size,fmt,(label1,label2...))
578 * array(arraySize, type); sequence(lengthSize,type)
579 * struct(field(name,type,description)...)
583 * in : input file handle
584 * inType : a type descriptor
585 * unnamed_types : array of unamed types
586 * named_types : array of named types
588 * type_descriptor* : a type descriptor
589 ****************************************************************************/
591 type_descriptor_t
*parseType(parse_file_t
*in
, type_descriptor_t
*inType
,
592 sequence_t
* unnamed_types
, table_t
* named_types
)
595 type_descriptor_t
*t
;
599 t
= (type_descriptor_t
*) memAlloc(sizeof(type_descriptor_t
));
603 sequence_push(unnamed_types
,t
);
609 if(strcmp(token
,"struct") == 0) {
611 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
612 getRAnglebracket(in
); //<struct>
613 getLAnglebracket(in
); //<field name=..>
614 token
= getToken(in
);
615 sequence_init(&(t
->fields
));
616 while(strcmp("field",token
) == 0){
617 f
= (field_t
*)memAlloc(sizeof(field_t
));
618 sequence_push(&(t
->fields
),f
);
620 parseFields(in
, f
, unnamed_types
, named_types
, 1);
623 getLAnglebracket(in
);
624 token
= getToken(in
);
626 if(strcmp("/",token
))in
->error(in
,"not a valid structure definition");
628 if(strcmp("struct",token
)!=0)
629 in
->error(in
,"not a valid structure definition");
630 getRAnglebracket(in
); //</struct>
632 else if(strcmp(token
,"union") == 0) {
634 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
635 getRAnglebracket(in
); //<union>
637 getLAnglebracket(in
); //<field name=..>
638 token
= getToken(in
);
639 sequence_init(&(t
->fields
));
640 while(strcmp("field",token
) == 0){
641 f
= (field_t
*)memAlloc(sizeof(field_t
));
642 sequence_push(&(t
->fields
),f
);
643 parseFields(in
, f
, unnamed_types
, named_types
, 1);
646 getLAnglebracket(in
);
647 token
= getToken(in
);
649 if(strcmp("/",token
))in
->error(in
,"not a valid union definition");
651 if(strcmp("union",token
)!=0)
652 in
->error(in
,"not a valid union definition");
653 getRAnglebracket(in
); //</union>
655 else if(strcmp(token
,"array") == 0) {
657 sequence_init(&(t
->fields
));
658 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
659 if(t
->size
== 0) in
->error(in
, "Array has empty size");
661 getRAnglebracket(in
); //<array size=n>
663 //getLAnglebracket(in); //<subtype>
665 f
= (field_t
*)memAlloc(sizeof(field_t
));
666 sequence_push(&(t
->fields
),f
);
667 parseFields(in
, f
, unnamed_types
, named_types
, 0);
669 //getLAnglebracket(in); //<type struct>
670 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
672 getLAnglebracket(in
); //</array>
675 if(strcmp("array",token
))in
->error(in
,"not a valid array definition");
676 getRAnglebracket(in
); //</array>
678 else if(strcmp(token
,"sequence") == 0) {
680 sequence_init(&(t
->fields
));
681 //getTypeAttributes(in, t, unnamed_types, named_types);
682 //getForwardslash(in);
683 getRAnglebracket(in
); //<sequence>
685 //getLAnglebracket(in); //<sequence size type>
687 f
= (field_t
*)memAlloc(sizeof(field_t
));
688 sequence_push(&(t
->fields
),f
);
689 parseFields(in
, f
, unnamed_types
, named_types
, 0);
691 //getLAnglebracket(in); //<subtype>
693 f
= (field_t
*)memAlloc(sizeof(field_t
));
694 sequence_push(&(t
->fields
),f
);
695 parseFields(in
, f
, unnamed_types
, named_types
, 0);
697 //getLAnglebracket(in); //<type sequence>
698 //t->length_type = parseType(in, NULL, unnamed_types, named_types);
700 //getLAnglebracket(in); //<type sequence>
702 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
704 if(t
->fields
.position
< 1) in
->error(in
, "Sequence has no length type");
705 if(t
->fields
.position
< 2) in
->error(in
, "Sequence has no subtype");
706 switch(((field_t
*)t
->fields
.array
[0])->type
->type
) {
716 in
->error(in
, "Wrong length type for sequence");
719 getLAnglebracket(in
); //</sequence>
722 if(strcmp("sequence",token
))in
->error(in
,"not a valid sequence definition");
723 getRAnglebracket(in
); //</sequence>
725 else if(strcmp(token
,"enum") == 0) {
728 sequence_init(&(t
->labels
));
729 sequence_init(&(t
->labels_description
));
730 t
->already_printed
= 0;
731 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
732 //if(t->size == 0) in->error(in, "Sequence has empty size");
733 //Mathieu : we fix enum size to 4 bytes. GCC is always like this.
734 //fox copy optimisation.
735 if(t
->size
!= 0) in
->error(in
, "Enum has fixed size of 4.");
737 getRAnglebracket(in
);
739 //<label name=label1 value=n/>
740 getLAnglebracket(in
);
741 token
= getToken(in
); //"label" or "/"
742 while(strcmp("label",token
) == 0){
743 str
= allocAndCopy(getNameAttribute(in
));
744 token
= getValueStrAttribute(in
);
746 str1
= appendString(str
,"=");
748 str
= appendString(str1
,token
);
750 sequence_push(&(t
->labels
),str
);
753 sequence_push(&(t
->labels
),str
);
756 getRAnglebracket(in
);
758 //read description if any. May be NULL.
759 str
= allocAndCopy(getDescription(in
));
760 sequence_push(&(t
->labels_description
),str
);
762 //next label definition
763 getLAnglebracket(in
);
764 token
= getToken(in
); //"label" or "/"
766 if(strcmp("/",token
))in
->error(in
, "not a valid enum definition");
768 if(strcmp("enum",token
))in
->error(in
, "not a valid enum definition");
769 getRAnglebracket(in
); //</label>
771 else if(strcmp(token
,"int_fixed") == 0) {
773 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
774 if(t
->size
== 0) in
->error(in
, "int has empty size");
776 getRAnglebracket(in
);
778 else if(strcmp(token
,"uint_fixed") == 0) {
779 t
->type
= UINT_FIXED
;
780 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
781 if(t
->size
== 0) in
->error(in
, "uint has empty size");
783 getRAnglebracket(in
);
785 else if(strcmp(token
,"char") == 0) {
788 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
790 getRAnglebracket(in
);
792 else if(strcmp(token
,"uchar") == 0) {
795 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
797 getRAnglebracket(in
);
799 else if(strcmp(token
,"short") == 0) {
802 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
804 getRAnglebracket(in
);
806 else if(strcmp(token
,"ushort") == 0) {
809 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
811 getRAnglebracket(in
);
813 else if(strcmp(token
,"int") == 0) {
815 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
817 getRAnglebracket(in
);
819 else if(strcmp(token
,"uint") == 0) {
821 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
823 getRAnglebracket(in
);
826 else if(strcmp(token
,"pointer") == 0) {
828 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
830 getRAnglebracket(in
);
832 else if(strcmp(token
,"long") == 0) {
834 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
836 getRAnglebracket(in
);
838 else if(strcmp(token
,"ulong") == 0) {
840 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
842 getRAnglebracket(in
);
844 else if(strcmp(token
,"size_t") == 0) {
846 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
848 getRAnglebracket(in
);
850 else if(strcmp(token
,"ssize_t") == 0) {
852 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
854 getRAnglebracket(in
);
856 else if(strcmp(token
,"off_t") == 0) {
858 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
860 getRAnglebracket(in
);
862 else if(strcmp(token
,"float") == 0) {
864 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
866 getRAnglebracket(in
);
868 else if(strcmp(token
,"string") == 0) {
870 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
872 getRAnglebracket(in
);
874 else if(strcmp(token
,"typeref") == 0){
875 // Must be a named type
877 sequence_pop(unnamed_types
);
878 token
= getNameAttribute(in
);
879 t
= find_named_type(token
, named_types
);
880 if(t
== NULL
) in
->error(in
,"Named referred to must be pre-declared.");
881 getForwardslash(in
); //<typeref name=type_name/>
882 getRAnglebracket(in
);
884 }else in
->error(in
,"not a valid type");
889 /*****************************************************************************
891 * find_named_type : find a named type from hash table
894 * named_types : array of named types
896 * type_descriptor * : a type descriptor
897 *****************************************************************************/
899 type_descriptor_t
* find_named_type(char *name
, table_t
* named_types
)
901 type_descriptor_t
*t
;
903 t
= table_find(named_types
,name
);
908 type_descriptor_t
* create_named_type(char *name
, table_t
* named_types
)
910 type_descriptor_t
*t
;
912 t
= (type_descriptor_t
*)memAlloc(sizeof(type_descriptor_t
));
913 t
->type_name
= allocAndCopy(name
);
916 table_insert(named_types
,t
->type_name
,t
);
917 // table_insert(named_types,allocAndCopy(name),t);
921 /*****************************************************************************
923 * parseTypeDefinition : get type information from type definition
925 * in : input file handle
926 * unnamed_types : array of unamed types
927 * named_types : array of named types
928 *****************************************************************************/
930 void parseTypeDefinition(parse_file_t
* in
, sequence_t
* unnamed_types
,
931 table_t
* named_types
)
934 type_descriptor_t
*t
;
936 token
= getNameAttribute(in
);
937 if(token
== NULL
) in
->error(in
, "Type has empty name");
938 t
= create_named_type(token
, named_types
);
940 if(t
->type
!= NONE
) in
->error(in
,"redefinition of named type");
941 getRAnglebracket(in
); //<type name=type_name>
942 getLAnglebracket(in
); //<
944 //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
946 parseType(in
,t
, unnamed_types
, named_types
);
949 getLAnglebracket(in
);
952 if(strcmp("type",token
))in
->error(in
,"not a valid type definition");
953 getRAnglebracket(in
); //</type>
956 /**************************************************************************
958 * getComa, getName, getNumber, getEqual
960 * Read a token from the input file, check its type, return it scontent.
963 * in , input file handle.
966 * address of token content.
968 **************************************************************************/
970 char *getName(parse_file_t
* in
)
974 token
= getToken(in
);
975 if(in
->type
!= NAME
) in
->error(in
,"Name token was expected");
979 int getNumber(parse_file_t
* in
)
983 token
= getToken(in
);
984 if(in
->type
!= NUMBER
) in
->error(in
, "Number token was expected");
988 char *getForwardslash(parse_file_t
* in
)
992 token
= getToken(in
);
993 //if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected");
994 /* Mathieu : final / is optional now. */
995 if(in
->type
!= FORWARDSLASH
) ungetToken(in
);
1000 char *getLAnglebracket(parse_file_t
* in
)
1004 token
= getToken(in
);
1005 if(in
->type
!= LANGLEBRACKET
) in
->error(in
, "Left angle bracket was expected");
1009 char *getRAnglebracket(parse_file_t
* in
)
1013 token
= getToken(in
);
1014 if(in
->type
!= RANGLEBRACKET
) in
->error(in
, "Right angle bracket was expected");
1018 char *getQuotedString(parse_file_t
* in
)
1022 token
= getToken(in
);
1023 if(in
->type
!= QUOTEDSTRING
) in
->error(in
, "quoted string was expected");
1027 char * getEqual(parse_file_t
*in
)
1031 token
= getToken(in
);
1032 if(in
->type
!= EQUAL
) in
->error(in
, "equal was expected");
1036 char seekNextChar(parse_file_t
*in
)
1039 while((car
= getc(in
->fp
)) != EOF
) {
1048 /******************************************************************
1050 * getToken, ungetToken
1052 * Read a token from the input file and return its type and content.
1053 * Line numbers are accounted for and whitespace/comments are skipped.
1056 * in, input file handle.
1059 * address of token content.
1061 ******************************************************************/
1063 void ungetToken(parse_file_t
* in
)
1068 char *getToken(parse_file_t
* in
)
1072 int pos
= 0, escaped
;
1074 if(in
->unget
== 1) {
1079 /* skip whitespace and comments */
1081 while((car
= getc(fp
)) != EOF
) {
1084 if(car1
== '*') skipComment(in
);
1085 else if(car1
== '/') skipEOL(in
);
1087 car1
= ungetc(car1
,fp
);
1091 else if(car
== '\n') in
->lineno
++;
1092 else if(!isspace(car
)) break;
1100 in
->type
= FORWARDSLASH
;
1101 in
->buffer
[pos
] = car
;
1105 in
->type
= LANGLEBRACKET
;
1106 in
->buffer
[pos
] = car
;
1110 in
->type
= RANGLEBRACKET
;
1111 in
->buffer
[pos
] = car
;
1116 in
->buffer
[pos
] = car
;
1121 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1122 if(car
== '\\' && escaped
== 0) {
1123 in
->buffer
[pos
] = car
;
1128 if(car
== '"' && escaped
== 0) break;
1129 if(car
== '\n' && escaped
== 0) {
1130 in
->error(in
, "non escaped newline inside quoted string");
1132 if(car
== '\n') in
->lineno
++;
1133 in
->buffer
[pos
] = car
;
1137 if(car
== EOF
) in
->error(in
,"no ending quotemark");
1138 if(pos
== BUFFER_SIZE
) in
->error(in
, "quoted string token too large");
1139 in
->type
= QUOTEDSTRING
;
1143 in
->buffer
[pos
] = car
;
1145 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1150 in
->buffer
[pos
] = car
;
1153 if(car
== EOF
) ungetc(car
,fp
);
1154 if(pos
== BUFFER_SIZE
) in
->error(in
, "number token too large");
1157 else if(isalpha(car
)) {
1158 in
->buffer
[0] = car
;
1160 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1161 if(!(isalnum(car
) || car
== '_')) {
1165 in
->buffer
[pos
] = car
;
1168 if(car
== EOF
) ungetc(car
,fp
);
1169 if(pos
== BUFFER_SIZE
) in
->error(in
, "name token too large");
1172 else in
->error(in
, "invalid character, unrecognized token");
1174 in
->buffer
[pos
] = 0;
1178 void skipComment(parse_file_t
* in
)
1181 while((car
= getc(in
->fp
)) != EOF
) {
1182 if(car
== '\n') in
->lineno
++;
1183 else if(car
== '*') {
1185 if(car
==EOF
) break;
1186 if(car
== '/') return;
1190 if(car
== EOF
) in
->error(in
,"comment begining with '/*' has no ending '*/'");
1193 void skipEOL(parse_file_t
* in
)
1196 while((car
= getc(in
->fp
)) != EOF
) {
1202 if(car
== EOF
)ungetc(car
, in
->fp
);
1205 /*****************************************************************************
1207 * checkNamedTypesImplemented : check if all named types have definition
1208 ****************************************************************************/
1210 void checkNamedTypesImplemented(table_t
* named_types
)
1212 type_descriptor_t
*t
;
1216 for(pos
= 0 ; pos
< named_types
->values
.position
; pos
++) {
1217 t
= (type_descriptor_t
*) named_types
->values
.array
[pos
];
1218 if(t
->type
== NONE
){
1219 sprintf(str
,"named type '%s' has no definition",
1220 (char*)named_types
->keys
.array
[pos
]);
1221 error_callback(NULL
,str
);
1227 /*****************************************************************************
1229 * generateChecksum : generate checksum for the facility
1231 * facName : name of facility
1233 * checksum : checksum for the facility
1234 ****************************************************************************/
1236 void generateChecksum(char* facName
,
1237 unsigned int * checksum
, sequence_t
* events
)
1243 crc
= crc32(facName
);
1244 for(pos
= 0; pos
< events
->position
; pos
++){
1245 ev
= (event_t
*)(events
->array
[pos
]);
1246 crc
= partial_crc32(ev
->name
, crc
);
1247 for(unsigned int i
= 0; i
< ev
->fields
.position
; i
++) {
1248 field_t
*f
= (field_t
*)ev
->fields
.array
[i
];
1249 crc
= partial_crc32(f
->name
, crc
);
1250 crc
= getTypeChecksum(crc
, f
->type
);
1256 /*****************************************************************************
1258 * getTypeChecksum : generate checksum by type info
1260 * crc : checksum generated so far
1261 * type : type descriptor containing type info
1263 * unsigned long : checksum
1264 *****************************************************************************/
1266 unsigned long getTypeChecksum(unsigned long aCrc
, type_descriptor_t
* type
)
1268 unsigned long crc
= aCrc
;
1269 char * str
= NULL
, buf
[16];
1275 str
= intOutputTypes
[getSizeindex(type
->size
)];
1278 str
= uintOutputTypes
[getSizeindex(type
->size
)];
1281 str
= allocAndCopy("void *");
1285 str
= allocAndCopy("signed char");
1289 str
= allocAndCopy("unsigned char");
1293 str
= allocAndCopy("short");
1297 str
= allocAndCopy("unsigned short");
1301 str
= allocAndCopy("int");
1305 str
= allocAndCopy("uint");
1309 str
= allocAndCopy("long");
1313 str
= allocAndCopy("unsigned long");
1317 str
= allocAndCopy("size_t");
1321 str
= allocAndCopy("ssize_t");
1325 str
= allocAndCopy("off_t");
1329 str
= floatOutputTypes
[getSizeindex(type
->size
)];
1332 str
= allocAndCopy("string");
1336 //str = appendString("enum ", uintOutputTypes[getSizeindex(type->size)]);
1337 str
= allocAndCopy("enum");
1341 sprintf(buf
,"%zu", type
->size
);
1342 str
= appendString("array ",buf
);
1346 sprintf(buf
,"%zu", type
->size
);
1347 str
= appendString("sequence ",buf
);
1351 str
= allocAndCopy("struct");
1355 str
= allocAndCopy("union");
1359 error_callback(NULL
, "named type has no definition");
1363 crc
= partial_crc32(str
,crc
);
1366 if(type
->fmt
) crc
= partial_crc32(type
->fmt
,crc
);
1368 if(type
->type
== ARRAY
){
1369 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1370 } else if(type
->type
==SEQUENCE
) {
1371 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[0])->type
);
1372 crc
= getTypeChecksum(crc
,((field_t
*)type
->fields
.array
[1])->type
);
1373 } else if(type
->type
== STRUCT
|| type
->type
== UNION
){
1374 for(pos
=0; pos
< type
->fields
.position
; pos
++){
1375 fld
= (field_t
*) type
->fields
.array
[pos
];
1376 crc
= partial_crc32(fld
->name
,crc
);
1377 crc
= getTypeChecksum(crc
, fld
->type
);
1379 }else if(type
->type
== ENUM
){
1380 for(pos
= 0; pos
< type
->labels
.position
; pos
++)
1381 crc
= partial_crc32((char*)type
->labels
.array
[pos
],crc
);
1388 /* Event type descriptors */
1389 void freeType(type_descriptor_t
* tp
)
1394 if(tp
->fmt
!= NULL
) free(tp
->fmt
);
1395 if(tp
->type
== ENUM
) {
1396 for(pos2
= 0; pos2
< tp
->labels
.position
; pos2
++) {
1397 free(tp
->labels
.array
[pos2
]);
1399 sequence_dispose(&(tp
->labels
));
1401 if(tp
->type
== STRUCT
) {
1402 for(pos2
= 0; pos2
< tp
->fields
.position
; pos2
++) {
1403 f
= (field_t
*) tp
->fields
.array
[pos2
];
1405 free(f
->description
);
1408 sequence_dispose(&(tp
->fields
));
1412 void freeNamedType(table_t
* t
)
1415 type_descriptor_t
* td
;
1417 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1418 free((char *)t
->keys
.array
[pos
]);
1419 td
= (type_descriptor_t
*)t
->values
.array
[pos
];
1425 void freeTypes(sequence_t
*t
)
1428 type_descriptor_t
*tp
;
1430 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1431 tp
= (type_descriptor_t
*)t
->array
[pos
];
1437 void freeEvents(sequence_t
*t
)
1442 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1443 ev
= (event_t
*) t
->array
[pos
];
1445 free(ev
->description
);
1446 sequence_dispose(&ev
->fields
);
1453 /* Extensible array */
1455 void sequence_init(sequence_t
*t
)
1459 t
->array
= (void **)memAlloc(t
->size
* sizeof(void *));
1462 void sequence_dispose(sequence_t
*t
)
1469 void sequence_push(sequence_t
*t
, void *elem
)
1473 if(t
->position
>= t
->size
) {
1475 t
->array
= (void **)memAlloc(t
->size
* 2 * sizeof(void *));
1476 memcpy(t
->array
, tmp
, t
->size
* sizeof(void *));
1477 t
->size
= t
->size
* 2;
1480 t
->array
[t
->position
] = elem
;
1484 void *sequence_pop(sequence_t
*t
)
1486 return t
->array
[t
->position
--];
1490 /* Hash table API, implementation is just linear search for now */
1492 void table_init(table_t
*t
)
1494 sequence_init(&(t
->keys
));
1495 sequence_init(&(t
->values
));
1498 void table_dispose(table_t
*t
)
1500 sequence_dispose(&(t
->keys
));
1501 sequence_dispose(&(t
->values
));
1504 void table_insert(table_t
*t
, char *key
, void *value
)
1506 sequence_push(&(t
->keys
),key
);
1507 sequence_push(&(t
->values
),value
);
1510 void *table_find(table_t
*t
, char *key
)
1513 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1514 if(strcmp((char *)key
,(char *)t
->keys
.array
[pos
]) == 0)
1515 return(t
->values
.array
[pos
]);
1520 void table_insert_int(table_t
*t
, int *key
, void *value
)
1522 sequence_push(&(t
->keys
),key
);
1523 sequence_push(&(t
->values
),value
);
1526 void *table_find_int(table_t
*t
, int *key
)
1529 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1530 if(*key
== *(int *)t
->keys
.array
[pos
])
1531 return(t
->values
.array
[pos
]);
1537 /* Concatenate strings */
1539 char *appendString(char *s
, char *suffix
)
1542 if(suffix
== NULL
) return s
;
1544 tmp
= (char *)memAlloc(strlen(s
) + strlen(suffix
) + 1);
This page took 0.062889 seconds and 4 git commands to generate.