3 parser.c: Generate helper declarations and functions to trace events
4 from an event description file.
6 Copyright (C) 2002, Xianxiu Yang
7 Copyright (C) 2002, Michel Dagenais
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* This program reads the ".xml" event definitions input files
23 and constructs structure for each event.
25 The program uses a very simple tokenizer, called from a hand written
26 recursive descent parser to fill a data structure describing the events.
27 The result is a sequence of events definitions which refer to type
30 A table of named types is maintained to allow refering to types by name
31 when the same type is used at several places. Finally a sequence of
32 all types is maintained to facilitate the freeing of all type
33 information when the processing of an ".xml" file is finished. */
39 #include <linux/errno.h>
46 char *intOutputTypes
[] = {
47 "int8_t", "int16_t", "int32_t", "int64_t" };
49 char *uintOutputTypes
[] = {
50 "uint8_t", "uint16_t", "uint32_t", "uint64_t" };
52 char *floatOutputTypes
[] = {
53 "undef", "undef", "float", "double" };
59 void strupper(char *string
)
70 int getSizeindex(unsigned int value
)
82 printf("Error : unknown value size %d\n", value
);
87 /*****************************************************************************
89 * getSize : translate from string to integer
91 * in : input file handle
94 *****************************************************************************/
96 unsigned long long int getSize(parse_file_t
*in
)
100 token
= getToken(in
);
101 if(in
->type
== NUMBER
) {
102 return strtoull(token
, NULL
, 0);
104 in
->error(in
,"incorrect size specification");
108 /*****************************************************************************
110 * error_callback : print out error info
112 * in : input file handle
113 * msg : message to be printed
114 ****************************************************************************/
116 void error_callback(parse_file_t
*in
, char *msg
)
119 printf("Error in file %s, line %d: %s\n", in
->name
, in
->lineno
, msg
);
126 /*****************************************************************************
128 * memAlloc : allocate memory
130 * size : required memory size
132 * void * : pointer to allocate memory or NULL
133 ****************************************************************************/
135 void * memAlloc(int size
)
138 if(size
== 0) return NULL
;
141 printf("Failed to allocate memory");
147 /*****************************************************************************
149 * allocAndCopy : allocate memory and initialize it
151 * str : string to be put in memory
153 * char * : pointer to allocate memory or NULL
154 ****************************************************************************/
156 char *allocAndCopy(char *str
)
159 if(str
== NULL
) return NULL
;
160 addr
= (char *)memAlloc(strlen(str
)+1);
165 /**************************************************************************
169 * Read the attribute from the input file.
172 * in , input file handle.
173 * t , the type descriptor to fill.
175 **************************************************************************/
177 void getTypeAttributes(parse_file_t
*in
, type_descriptor_t
*t
,
178 sequence_t
* unnamed_types
, table_t
* named_types
)
187 token
= getToken(in
);
188 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
193 if(!strcmp("format",token
)) {
195 t
->fmt
= allocAndCopy(getQuotedString(in
));
196 //} else if(!strcmp("name",token)) {
198 // car = seekNextChar(in);
199 // if(car == EOF) in->error(in,"name was expected");
200 // else if(car == '\"') t->type_name = allocAndCopy(getQuotedString(in));
201 // else t->type_name = allocAndCopy(getName(in));
202 } else if(!strcmp("size",token
)) {
204 t
->size
= getSize(in
);
205 } else if(!strcmp("align",token
)) {
207 t
->alignment
= getNumber(in
);
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
)
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
== '\"') fac
->name
= allocAndCopy(getQuotedString(in
));
286 else fac
->name
= allocAndCopy(getName(in
));
291 /**************************************************************************
295 * Read the attribute from the input file.
298 * in , input file handle.
299 * f , the field to fill.
301 **************************************************************************/
303 void getFieldAttributes(parse_file_t
*in
, field_t
*f
)
311 token
= getToken(in
);
312 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
317 if(!strcmp("name",token
)) {
319 car
= seekNextChar(in
);
320 if(car
== EOF
) in
->error(in
,"name was expected");
321 else if(car
== '\"') f
->name
= allocAndCopy(getQuotedString(in
));
322 else f
->name
= allocAndCopy(getName(in
));
327 char *getNameAttribute(parse_file_t
*in
)
334 token
= getToken(in
);
335 if(strcmp("/",token
) == 0 || strcmp(">",token
) == 0){
340 if(!strcmp("name",token
)) {
342 car
= seekNextChar(in
);
343 if(car
== EOF
) in
->error(in
,"name was expected");
344 else if(car
== '\"') name
= allocAndCopy(getQuotedString(in
));
345 else name
= allocAndCopy(getName(in
));
348 if(name
== NULL
) in
->error(in
, "Name was expected");
355 //for <label name=label_name value=n format="..."/>, value is an option
356 char * getValueStrAttribute(parse_file_t
*in
)
360 token
= getToken(in
);
361 if(strcmp("/",token
) == 0){
366 if(strcmp("value",token
))in
->error(in
,"value was expected");
368 token
= getToken(in
);
369 if(in
->type
!= NUMBER
) in
->error(in
,"number was expected");
373 char * getDescription(parse_file_t
*in
)
376 char * token
, car
, *str
;
380 getLAnglebracket(in
);
382 if(strcmp("description",token
)){
383 fseek(in
->fp
, pos
, SEEK_SET
);
387 getRAnglebracket(in
);
390 while((car
= getc(in
->fp
)) != EOF
) {
391 if(car
== '<') break;
392 if(car
== '\0') continue;
393 in
->buffer
[pos
] = car
;
396 if(car
== EOF
)in
->error(in
,"not a valid description");
397 in
->buffer
[pos
] = '\0';
399 str
= allocAndCopy(in
->buffer
);
403 if(strcmp("description", token
))in
->error(in
,"not a valid description");
404 getRAnglebracket(in
);
409 /*****************************************************************************
411 * parseFacility : generate event list
413 * in : input file handle
414 * fac : empty facility
416 * fac : facility filled with event list
417 ****************************************************************************/
419 void parseFacility(parse_file_t
*in
, facility_t
* fac
)
424 getFacilityAttributes(in
, fac
);
425 if(fac
->name
== NULL
) in
->error(in
, "Attribute not named");
427 fac
->capname
= allocAndCopy(fac
->name
);
428 strupper(fac
->capname
);
429 getRAnglebracket(in
);
431 fac
->description
= getDescription(in
);
434 getLAnglebracket(in
);
436 token
= getToken(in
);
437 if(in
->type
== ENDFILE
)
438 in
->error(in
,"the definition of the facility is not finished");
440 if(strcmp("event",token
) == 0){
441 ev
= (event_t
*) memAlloc(sizeof(event_t
));
442 sequence_push(&(fac
->events
),ev
);
443 parseEvent(in
,ev
, &(fac
->unnamed_types
), &(fac
->named_types
));
444 }else if(strcmp("type",token
) == 0){
445 parseTypeDefinition(in
, &(fac
->unnamed_types
), &(fac
->named_types
));
446 }else if(in
->type
== FORWARDSLASH
){
448 }else in
->error(in
,"event or type token expected\n");
452 if(strcmp("facility",token
)) in
->error(in
,"not the end of the facility");
453 getRAnglebracket(in
); //</facility>
456 /*****************************************************************************
458 * parseEvent : generate event from event definition
460 * in : input file handle
462 * unnamed_types : array of unamed types
463 * named_types : array of named types
465 * ev : new event (parameters are passed to it)
466 ****************************************************************************/
468 void parseEvent(parse_file_t
*in
, event_t
* ev
, sequence_t
* unnamed_types
,
469 table_t
* named_types
)
474 sequence_init(&(ev
->fields
));
475 //<event name=eventtype_name>
476 getEventAttributes(in
, ev
);
477 if(ev
->name
== NULL
) in
->error(in
, "Event not named");
478 getRAnglebracket(in
);
480 //<description>...</description>
481 ev
->description
= getDescription(in
);
484 /* Events can have multiple fields. each field form at least a function
485 * parameter of the logging function. */
487 getLAnglebracket(in
);
488 token
= getToken(in
);
491 case FORWARDSLASH
: /* </event> */
493 if(strcmp("event",token
))in
->error(in
,"not an event definition");
494 getRAnglebracket(in
); //</event>
497 case NAME
: /* a field */
498 if(strcmp("field",token
))in
->error(in
,"expecting a field");
499 f
= (field_t
*)memAlloc(sizeof(field_t
));
500 sequence_push(&(ev
->fields
),f
);
501 parseFields(in
, f
, unnamed_types
, named_types
, 1);
504 in
->error(in
, "expecting </event> or <field >");
509 if(in
->type
== FORWARDSLASH
){ //</event> NOTHING
511 }else if(in
->type
== NAME
){
512 if(strcmp("struct",token
)==0 || strcmp("typeref",token
)==0){
514 ev
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
515 if(ev
->type
->type
!= STRUCT
&& ev
->type
->type
!= NONE
)
516 in
->error(in
,"type must be a struct");
517 }else in
->error(in
, "not a valid type");
519 getLAnglebracket(in
);
521 }else in
->error(in
,"not a struct type");
522 getLAnglebracket(in
);
525 if(strcmp("event",token
))in
->error(in
,"not an event definition");
526 getRAnglebracket(in
); //</event>
530 /*****************************************************************************
532 * parseField : get field infomation from buffer
534 * in : input file handle
536 * unnamed_types : array of unamed types
537 * named_types : array of named types
538 * tag : is field surrounded by a <field> </field> tag ?
539 ****************************************************************************/
541 void parseFields(parse_file_t
*in
, field_t
*f
,
542 sequence_t
* unnamed_types
,
543 table_t
* named_types
,
548 //<field name=field_name> <description> <type> </field>
549 getFieldAttributes(in
, f
);
550 if(f
->name
== NULL
) in
->error(in
, "Field not named");
551 getRAnglebracket(in
);
553 f
->description
= getDescription(in
);
557 getLAnglebracket(in
);
558 f
->type
= parseType(in
,NULL
, unnamed_types
, named_types
);
560 /* Those will be set later by preset_field_type_size */
561 f
->fixed_root
= FIELD_UNKNOWN
;
562 f
->fixed_parent
= FIELD_UNKNOWN
;
563 f
->fixed_size
= FIELD_UNKNOWN
;
566 getLAnglebracket(in
);
569 if(strcmp("field",token
))in
->error(in
,"not a valid field definition");
570 getRAnglebracket(in
); //</field>
575 /*****************************************************************************
577 * parseType : get type information, type can be :
579 * int(size,fmt); uint(size,fmt); float(size,fmt);
580 * string(fmt); enum(size,fmt,(label1,label2...))
582 * array(arraySize, type); sequence(lengthSize,type)
583 * struct(field(name,type,description)...)
587 * in : input file handle
588 * inType : a type descriptor
589 * unnamed_types : array of unamed types
590 * named_types : array of named types
592 * type_descriptor* : a type descriptor
593 ****************************************************************************/
595 type_descriptor_t
*parseType(parse_file_t
*in
, type_descriptor_t
*inType
,
596 sequence_t
* unnamed_types
, table_t
* named_types
)
599 type_descriptor_t
*t
;
603 t
= (type_descriptor_t
*) memAlloc(sizeof(type_descriptor_t
));
607 sequence_push(unnamed_types
,t
);
613 if(strcmp(token
,"struct") == 0) {
615 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
616 getRAnglebracket(in
); //<struct>
617 getLAnglebracket(in
); //<field name=..>
618 token
= getToken(in
);
619 sequence_init(&(t
->fields
));
620 while(strcmp("field",token
) == 0){
621 f
= (field_t
*)memAlloc(sizeof(field_t
));
622 sequence_push(&(t
->fields
),f
);
624 parseFields(in
, f
, unnamed_types
, named_types
, 1);
627 getLAnglebracket(in
);
628 token
= getToken(in
);
630 if(strcmp("/",token
))in
->error(in
,"not a valid structure definition");
632 if(strcmp("struct",token
)!=0)
633 in
->error(in
,"not a valid structure definition");
634 getRAnglebracket(in
); //</struct>
636 else if(strcmp(token
,"union") == 0) {
638 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
639 getRAnglebracket(in
); //<union>
641 getLAnglebracket(in
); //<field name=..>
642 token
= getToken(in
);
643 sequence_init(&(t
->fields
));
644 while(strcmp("field",token
) == 0){
645 f
= (field_t
*)memAlloc(sizeof(field_t
));
646 sequence_push(&(t
->fields
),f
);
647 parseFields(in
, f
, unnamed_types
, named_types
, 1);
650 getLAnglebracket(in
);
651 token
= getToken(in
);
653 if(strcmp("/",token
))in
->error(in
,"not a valid union definition");
655 if(strcmp("union",token
)!=0)
656 in
->error(in
,"not a valid union definition");
657 getRAnglebracket(in
); //</union>
659 else if(strcmp(token
,"array") == 0) {
661 sequence_init(&(t
->fields
));
662 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
663 if(t
->size
== 0) in
->error(in
, "Array has empty size");
665 getRAnglebracket(in
); //<array size=n>
667 getLAnglebracket(in
); //<subtype>
669 f
= (field_t
*)memAlloc(sizeof(field_t
));
670 sequence_push(&(t
->fields
),f
);
671 parseFields(in
, f
, unnamed_types
, named_types
, 0);
673 //getLAnglebracket(in); //<type struct>
674 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
676 getLAnglebracket(in
); //</array>
679 if(strcmp("array",token
))in
->error(in
,"not a valid array definition");
680 getRAnglebracket(in
); //</array>
682 else if(strcmp(token
,"sequence") == 0) {
684 sequence_init(&(t
->fields
));
685 //getTypeAttributes(in, t, unnamed_types, named_types);
686 //getForwardslash(in);
687 getRAnglebracket(in
); //<sequence>
689 getLAnglebracket(in
); //<sequence size type>
691 f
= (field_t
*)memAlloc(sizeof(field_t
));
692 sequence_push(&(t
->fields
),f
);
693 parseFields(in
, f
, unnamed_types
, named_types
, 0);
695 getLAnglebracket(in
); //<subtype>
697 f
= (field_t
*)memAlloc(sizeof(field_t
));
698 sequence_push(&(t
->fields
),f
);
699 parseFields(in
, f
, unnamed_types
, named_types
, 0);
701 //getLAnglebracket(in); //<type sequence>
702 //t->length_type = parseType(in, NULL, unnamed_types, named_types);
704 //getLAnglebracket(in); //<type sequence>
706 //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
708 if(t
->fields
.position
< 1) in
->error(in
, "Sequence has no length type");
709 if(t
->fields
.position
< 2) in
->error(in
, "Sequence has no subtype");
710 switch(((field_t
*)t
->fields
.array
[0])->type
->type
) {
720 in
->error(in
, "Wrong length type for sequence");
723 getLAnglebracket(in
); //</sequence>
726 if(strcmp("sequence",token
))in
->error(in
,"not a valid sequence definition");
727 getRAnglebracket(in
); //</sequence>
729 else if(strcmp(token
,"enum") == 0) {
732 sequence_init(&(t
->labels
));
733 sequence_init(&(t
->labels_description
));
734 t
->already_printed
= 0;
735 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
736 //if(t->size == 0) in->error(in, "Sequence has empty size");
737 //Mathieu : we fix enum size to 4 bytes. GCC is always like this.
738 //fox copy optimisation.
739 if(t
->size
!= 0) in
->error(in
, "Enum has fixed size of 4.");
741 getRAnglebracket(in
);
743 //<label name=label1 value=n/>
744 getLAnglebracket(in
);
745 token
= getToken(in
); //"label" or "/"
746 while(strcmp("label",token
) == 0){
747 str
= allocAndCopy(getNameAttribute(in
));
748 token
= getValueStrAttribute(in
);
750 str1
= appendString(str
,"=");
752 str
= appendString(str1
,token
);
754 sequence_push(&(t
->labels
),str
);
757 sequence_push(&(t
->labels
),str
);
760 getRAnglebracket(in
);
762 //read description if any. May be NULL.
763 str
= allocAndCopy(getDescription(in
));
764 sequence_push(&(t
->labels_description
),str
);
766 //next label definition
767 getLAnglebracket(in
);
768 token
= getToken(in
); //"label" or "/"
770 if(strcmp("/",token
))in
->error(in
, "not a valid enum definition");
772 if(strcmp("enum",token
))in
->error(in
, "not a valid enum definition");
773 getRAnglebracket(in
); //</label>
775 else if(strcmp(token
,"int_fixed") == 0) {
777 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
778 if(t
->size
== 0) in
->error(in
, "int has empty size");
780 getRAnglebracket(in
);
782 else if(strcmp(token
,"uint_fixed") == 0) {
783 t
->type
= UINT_FIXED
;
784 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
785 if(t
->size
== 0) in
->error(in
, "uint has empty size");
787 getRAnglebracket(in
);
789 else if(strcmp(token
,"char") == 0) {
792 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
794 getRAnglebracket(in
);
796 else if(strcmp(token
,"uchar") == 0) {
799 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
801 getRAnglebracket(in
);
803 else if(strcmp(token
,"short") == 0) {
806 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
808 getRAnglebracket(in
);
810 else if(strcmp(token
,"ushort") == 0) {
813 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
815 getRAnglebracket(in
);
817 else if(strcmp(token
,"int") == 0) {
819 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
821 getRAnglebracket(in
);
823 else if(strcmp(token
,"uint") == 0) {
825 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
827 getRAnglebracket(in
);
830 else if(strcmp(token
,"pointer") == 0) {
832 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
834 getRAnglebracket(in
);
836 else if(strcmp(token
,"long") == 0) {
838 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
840 getRAnglebracket(in
);
842 else if(strcmp(token
,"ulong") == 0) {
844 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
846 getRAnglebracket(in
);
848 else if(strcmp(token
,"size_t") == 0) {
850 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
852 getRAnglebracket(in
);
854 else if(strcmp(token
,"ssize_t") == 0) {
856 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
858 getRAnglebracket(in
);
860 else if(strcmp(token
,"off_t") == 0) {
862 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
864 getRAnglebracket(in
);
866 else if(strcmp(token
,"float") == 0) {
868 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
870 getRAnglebracket(in
);
872 else if(strcmp(token
,"string") == 0) {
874 getTypeAttributes(in
, t
, unnamed_types
, named_types
);
876 getRAnglebracket(in
);
878 else if(strcmp(token
,"typeref") == 0){
879 // Must be a named type
881 sequence_pop(unnamed_types
);
882 token
= getNameAttribute(in
);
883 t
= find_named_type(token
, named_types
);
884 if(t
== NULL
) in
->error(in
,"Named referred to must be pre-declared.");
885 getForwardslash(in
); //<typeref name=type_name/>
886 getRAnglebracket(in
);
888 }else in
->error(in
,"not a valid type");
893 /*****************************************************************************
895 * find_named_type : find a named type from hash table
898 * named_types : array of named types
900 * type_descriptor * : a type descriptor
901 *****************************************************************************/
903 type_descriptor_t
* find_named_type(char *name
, table_t
* named_types
)
905 type_descriptor_t
*t
;
907 t
= table_find(named_types
,name
);
909 t
= (type_descriptor_t
*)memAlloc(sizeof(type_descriptor_t
));
910 t
->type_name
= allocAndCopy(name
);
913 table_insert(named_types
,t
->type_name
,t
);
914 // table_insert(named_types,allocAndCopy(name),t);
919 /*****************************************************************************
921 * parseTypeDefinition : get type information from type definition
923 * in : input file handle
924 * unnamed_types : array of unamed types
925 * named_types : array of named types
926 *****************************************************************************/
928 void parseTypeDefinition(parse_file_t
* in
, sequence_t
* unnamed_types
,
929 table_t
* named_types
)
932 type_descriptor_t
*t
;
934 token
= getNameAttribute(in
);
935 if(token
== NULL
) in
->error(in
, "Type has empty name");
936 t
= find_named_type(token
, named_types
);
938 if(t
->type
!= NONE
) in
->error(in
,"redefinition of named type");
939 getRAnglebracket(in
); //<type name=type_name>
940 getLAnglebracket(in
); //<
942 //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
944 parseType(in
,t
, unnamed_types
, named_types
);
947 getLAnglebracket(in
);
950 if(strcmp("type",token
))in
->error(in
,"not a valid type definition");
951 getRAnglebracket(in
); //</type>
954 /**************************************************************************
956 * getComa, getName, getNumber, getEqual
958 * Read a token from the input file, check its type, return it scontent.
961 * in , input file handle.
964 * address of token content.
966 **************************************************************************/
968 char *getName(parse_file_t
* in
)
972 token
= getToken(in
);
973 if(in
->type
!= NAME
) in
->error(in
,"Name token was expected");
977 int getNumber(parse_file_t
* in
)
981 token
= getToken(in
);
982 if(in
->type
!= NUMBER
) in
->error(in
, "Number token was expected");
986 char *getForwardslash(parse_file_t
* in
)
990 token
= getToken(in
);
991 //if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected");
992 /* Mathieu : final / is optional now. */
993 if(in
->type
!= FORWARDSLASH
) ungetToken(in
);
998 char *getLAnglebracket(parse_file_t
* in
)
1002 token
= getToken(in
);
1003 if(in
->type
!= LANGLEBRACKET
) in
->error(in
, "Left angle bracket was expected");
1007 char *getRAnglebracket(parse_file_t
* in
)
1011 token
= getToken(in
);
1012 if(in
->type
!= RANGLEBRACKET
) in
->error(in
, "Right angle bracket was expected");
1016 char *getQuotedString(parse_file_t
* in
)
1020 token
= getToken(in
);
1021 if(in
->type
!= QUOTEDSTRING
) in
->error(in
, "quoted string was expected");
1025 char * getEqual(parse_file_t
*in
)
1029 token
= getToken(in
);
1030 if(in
->type
!= EQUAL
) in
->error(in
, "equal was expected");
1034 char seekNextChar(parse_file_t
*in
)
1037 while((car
= getc(in
->fp
)) != EOF
) {
1046 /******************************************************************
1048 * getToken, ungetToken
1050 * Read a token from the input file and return its type and content.
1051 * Line numbers are accounted for and whitespace/comments are skipped.
1054 * in, input file handle.
1057 * address of token content.
1059 ******************************************************************/
1061 void ungetToken(parse_file_t
* in
)
1066 char *getToken(parse_file_t
* in
)
1070 int pos
= 0, escaped
;
1072 if(in
->unget
== 1) {
1077 /* skip whitespace and comments */
1079 while((car
= getc(fp
)) != EOF
) {
1082 if(car1
== '*') skipComment(in
);
1083 else if(car1
== '/') skipEOL(in
);
1085 car1
= ungetc(car1
,fp
);
1089 else if(car
== '\n') in
->lineno
++;
1090 else if(!isspace(car
)) break;
1098 in
->type
= FORWARDSLASH
;
1099 in
->buffer
[pos
] = car
;
1103 in
->type
= LANGLEBRACKET
;
1104 in
->buffer
[pos
] = car
;
1108 in
->type
= RANGLEBRACKET
;
1109 in
->buffer
[pos
] = car
;
1114 in
->buffer
[pos
] = car
;
1119 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1120 if(car
== '\\' && escaped
== 0) {
1121 in
->buffer
[pos
] = car
;
1126 if(car
== '"' && escaped
== 0) break;
1127 if(car
== '\n' && escaped
== 0) {
1128 in
->error(in
, "non escaped newline inside quoted string");
1130 if(car
== '\n') in
->lineno
++;
1131 in
->buffer
[pos
] = car
;
1135 if(car
== EOF
) in
->error(in
,"no ending quotemark");
1136 if(pos
== BUFFER_SIZE
) in
->error(in
, "quoted string token too large");
1137 in
->type
= QUOTEDSTRING
;
1141 in
->buffer
[pos
] = car
;
1143 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1148 in
->buffer
[pos
] = car
;
1151 if(car
== EOF
) ungetc(car
,fp
);
1152 if(pos
== BUFFER_SIZE
) in
->error(in
, "number token too large");
1155 else if(isalpha(car
)) {
1156 in
->buffer
[0] = car
;
1158 while((car
= getc(fp
)) != EOF
&& pos
< BUFFER_SIZE
) {
1159 if(!(isalnum(car
) || car
== '_')) {
1163 in
->buffer
[pos
] = car
;
1166 if(car
== EOF
) ungetc(car
,fp
);
1167 if(pos
== BUFFER_SIZE
) in
->error(in
, "name token too large");
1170 else in
->error(in
, "invalid character, unrecognized token");
1172 in
->buffer
[pos
] = 0;
1176 void skipComment(parse_file_t
* in
)
1179 while((car
= getc(in
->fp
)) != EOF
) {
1180 if(car
== '\n') in
->lineno
++;
1181 else if(car
== '*') {
1183 if(car
==EOF
) break;
1184 if(car
== '/') return;
1188 if(car
== EOF
) in
->error(in
,"comment begining with '/*' has no ending '*/'");
1191 void skipEOL(parse_file_t
* in
)
1194 while((car
= getc(in
->fp
)) != EOF
) {
1200 if(car
== EOF
)ungetc(car
, in
->fp
);
1203 /*****************************************************************************
1205 * checkNamedTypesImplemented : check if all named types have definition
1206 ****************************************************************************/
1208 void checkNamedTypesImplemented(table_t
* named_types
)
1210 type_descriptor_t
*t
;
1214 for(pos
= 0 ; pos
< named_types
->values
.position
; pos
++) {
1215 t
= (type_descriptor_t
*) named_types
->values
.array
[pos
];
1216 if(t
->type
== NONE
){
1217 sprintf(str
,"named type '%s' has no definition",
1218 (char*)named_types
->keys
.array
[pos
]);
1219 error_callback(NULL
,str
);
1225 /*****************************************************************************
1227 * generateChecksum : generate checksum for the facility
1229 * facName : name of facility
1231 * checksum : checksum for the facility
1232 ****************************************************************************/
1234 void generateChecksum(char* facName
,
1235 unsigned long * checksum
, sequence_t
* events
)
1241 crc
= crc32(facName
);
1242 for(pos
= 0; pos
< events
->position
; pos
++){
1243 ev
= (event_t
*)(events
->array
[pos
]);
1244 crc
= partial_crc32(ev
->name
, crc
);
1245 for(unsigned int i
= 0; i
< ev
->fields
.position
; i
++) {
1246 field_t
*f
= (field_t
*)ev
->fields
.array
[i
];
1247 crc
= partial_crc32(f
->name
, crc
);
1248 crc
= getTypeChecksum(crc
, f
->type
);
1254 /*****************************************************************************
1256 * getTypeChecksum : generate checksum by type info
1258 * crc : checksum generated so far
1259 * type : type descriptor containing type info
1261 * unsigned long : checksum
1262 *****************************************************************************/
1264 unsigned long getTypeChecksum(unsigned long aCrc
, type_descriptor_t
* type
)
1266 unsigned long crc
= aCrc
;
1267 char * str
= NULL
, buf
[16];
1273 str
= intOutputTypes
[getSizeindex(type
->size
)];
1276 str
= uintOutputTypes
[getSizeindex(type
->size
)];
1279 str
= allocAndCopy("void *");
1283 str
= allocAndCopy("signed char");
1287 str
= allocAndCopy("unsigned char");
1291 str
= allocAndCopy("short");
1295 str
= allocAndCopy("unsigned short");
1299 str
= allocAndCopy("int");
1303 str
= allocAndCopy("uint");
1307 str
= allocAndCopy("long");
1311 str
= allocAndCopy("unsigned long");
1315 str
= allocAndCopy("size_t");
1319 str
= allocAndCopy("ssize_t");
1323 str
= allocAndCopy("off_t");
1327 str
= floatOutputTypes
[getSizeindex(type
->size
)];
1330 str
= allocAndCopy("string");
1334 //str = appendString("enum ", uintOutputTypes[getSizeindex(type->size)]);
1335 str
= allocAndCopy("enum");
1339 sprintf(buf
,"%llu", type
->size
);
1340 str
= appendString("array ",buf
);
1344 sprintf(buf
,"%llu", type
->size
);
1345 str
= appendString("sequence ",buf
);
1349 str
= allocAndCopy("struct");
1353 str
= allocAndCopy("union");
1357 error_callback(NULL
, "named type has no definition");
1361 crc
= partial_crc32(str
,crc
);
1364 if(type
->fmt
) crc
= partial_crc32(type
->fmt
,crc
);
1366 if(type
->type
== ARRAY
|| type
->type
== SEQUENCE
){
1367 crc
= getTypeChecksum(crc
,type
->nested_type
);
1368 }else if(type
->type
== STRUCT
|| type
->type
== UNION
){
1369 for(pos
=0; pos
< type
->fields
.position
; pos
++){
1370 fld
= (field_t
*) type
->fields
.array
[pos
];
1371 crc
= partial_crc32(fld
->name
,crc
);
1372 crc
= getTypeChecksum(crc
, fld
->type
);
1374 }else if(type
->type
== ENUM
){
1375 for(pos
= 0; pos
< type
->labels
.position
; pos
++)
1376 crc
= partial_crc32((char*)type
->labels
.array
[pos
],crc
);
1383 /* Event type descriptors */
1384 void freeType(type_descriptor_t
* tp
)
1389 if(tp
->fmt
!= NULL
) free(tp
->fmt
);
1390 if(tp
->type
== ENUM
) {
1391 for(pos2
= 0; pos2
< tp
->labels
.position
; pos2
++) {
1392 free(tp
->labels
.array
[pos2
]);
1394 sequence_dispose(&(tp
->labels
));
1396 if(tp
->type
== STRUCT
) {
1397 for(pos2
= 0; pos2
< tp
->fields
.position
; pos2
++) {
1398 f
= (field_t
*) tp
->fields
.array
[pos2
];
1400 free(f
->description
);
1403 sequence_dispose(&(tp
->fields
));
1407 void freeNamedType(table_t
* t
)
1410 type_descriptor_t
* td
;
1412 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1413 free((char *)t
->keys
.array
[pos
]);
1414 td
= (type_descriptor_t
*)t
->values
.array
[pos
];
1420 void freeTypes(sequence_t
*t
)
1423 type_descriptor_t
*tp
;
1425 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1426 tp
= (type_descriptor_t
*)t
->array
[pos
];
1432 void freeEvents(sequence_t
*t
)
1437 for(pos
= 0 ; pos
< t
->position
; pos
++) {
1438 ev
= (event_t
*) t
->array
[pos
];
1440 free(ev
->description
);
1441 sequence_dispose(&ev
->fields
);
1448 /* Extensible array */
1450 void sequence_init(sequence_t
*t
)
1454 t
->array
= (void **)memAlloc(t
->size
* sizeof(void *));
1457 void sequence_dispose(sequence_t
*t
)
1464 void sequence_push(sequence_t
*t
, void *elem
)
1468 if(t
->position
>= t
->size
) {
1470 t
->array
= (void **)memAlloc(t
->size
* 2 * sizeof(void *));
1471 memcpy(t
->array
, tmp
, t
->size
* sizeof(void *));
1472 t
->size
= t
->size
* 2;
1475 t
->array
[t
->position
] = elem
;
1479 void *sequence_pop(sequence_t
*t
)
1481 return t
->array
[t
->position
--];
1485 /* Hash table API, implementation is just linear search for now */
1487 void table_init(table_t
*t
)
1489 sequence_init(&(t
->keys
));
1490 sequence_init(&(t
->values
));
1493 void table_dispose(table_t
*t
)
1495 sequence_dispose(&(t
->keys
));
1496 sequence_dispose(&(t
->values
));
1499 void table_insert(table_t
*t
, char *key
, void *value
)
1501 sequence_push(&(t
->keys
),key
);
1502 sequence_push(&(t
->values
),value
);
1505 void *table_find(table_t
*t
, char *key
)
1508 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1509 if(strcmp((char *)key
,(char *)t
->keys
.array
[pos
]) == 0)
1510 return(t
->values
.array
[pos
]);
1515 void table_insert_int(table_t
*t
, int *key
, void *value
)
1517 sequence_push(&(t
->keys
),key
);
1518 sequence_push(&(t
->values
),value
);
1521 void *table_find_int(table_t
*t
, int *key
)
1524 for(pos
= 0 ; pos
< t
->keys
.position
; pos
++) {
1525 if(*key
== *(int *)t
->keys
.array
[pos
])
1526 return(t
->values
.array
[pos
]);
1532 /* Concatenate strings */
1534 char *appendString(char *s
, char *suffix
)
1537 if(suffix
== NULL
) return s
;
1539 tmp
= (char *)memAlloc(strlen(s
) + strlen(suffix
) + 1);
This page took 0.076246 seconds and 4 git commands to generate.