parser.c: Generate helper declarations and functions to trace events
from an event description file.
- Copyright (C) 2005, Mathieu Desnoyers
+ Copyright (C) 2005, Mathieu Desnoyers
Copyright (C) 2002, Xianxiu Yang
- Copyright (C) 2002, Michel Dagenais
+ Copyright (C) 2002, Michel Dagenais
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-/* This program reads the ".xml" event definitions input files
+/* This program reads the ".xml" event definitions input files
and constructs structure for each event.
-
+
The program uses a very simple tokenizer, called from a hand written
recursive descent parser to fill a data structure describing the events.
The result is a sequence of events definitions which refer to type
A table of named types is maintained to allow refering to types by name
when the same type is used at several places. Finally a sequence of
- all types is maintained to facilitate the freeing of all type
+ all types is maintained to facilitate the freeing of all type
information when the processing of an ".xml" file is finished. */
-#include <stdlib.h>
+#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
-#include <linux/errno.h>
+#include <linux/errno.h>
#include <assert.h>
#include <ctype.h>
void strupper(char *string)
{
char *ptr = string;
-
+
while(*ptr != '\0') {
*ptr = toupper(*ptr);
- ptr++;
+ ptr++;
}
}
int getSizeindex(unsigned int value)
-{
+{
switch(value) {
case 1:
return 0;
/*****************************************************************************
*Function name
* getSize : translate from string to integer
- *Input params
+ *Input params
* in : input file handle
- *Return values
- * size
+ *Return values
+ * size
*****************************************************************************/
unsigned long long int getSize(parse_file_t *in)
in->type = NUMBER;
token2 = token;
do {
- if (!isdigit(*token2)) {
- in->type = QUOTEDSTRING;
- break;
- }
+ if (!isdigit(*token2)) {
+ in->type = QUOTEDSTRING;
+ break;
+ }
} while (*(++token2) != '\0');
}
if(in->type == NUMBER) {
- ret = strtoull(token, NULL, 0);
+ ret = strtoull(token, NULL, 0);
} else {
- goto error;
- }
-
- return ret;
+ goto error;
+ }
+
+ return ret;
error:
in->error(in,"incorrect size specification");
return -1;
* error_callback : print out error info
*Input params
* in : input file handle
- * msg : message to be printed
+ * msg : message to be printed
****************************************************************************/
void error_callback(parse_file_t *in, char *msg)
/*****************************************************************************
*Function name
- * memAlloc : allocate memory
- *Input params
- * size : required memory size
- *return value
- * void * : pointer to allocate memory or NULL
+ * memAlloc : allocate memory
+ *Input params
+ * size : required memory size
+ *return value
+ * void * : pointer to allocate memory or NULL
****************************************************************************/
void * memAlloc(int size)
if(size == 0) return NULL;
addr = malloc(size);
if(!addr){
- printf("Failed to allocate memory");
+ printf("Failed to allocate memory");
exit(1);
}
- return addr;
+ return addr;
}
/*****************************************************************************
*Function name
- * allocAndCopy : allocate memory and initialize it
- *Input params
- * str : string to be put in memory
- *return value
+ * allocAndCopy : allocate memory and initialize it
+ *Input params
+ * str : string to be put in memory
+ *return value
* char * : pointer to allocate memory or NULL
****************************************************************************/
**************************************************************************/
void getTypeAttributes(parse_file_t *in, type_descriptor_t *t,
- sequence_t * unnamed_types, table_t * named_types)
+ sequence_t * unnamed_types, table_t * named_types)
{
char * token;
int car;
t->size = 0;
t->custom_write = 0;
t->network = 0;
-
+
while(1) {
- token = getToken(in);
+ token = getToken(in);
if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
ungetToken(in);
break;
}
-
+
if(!strcmp("format",token)) {
getEqual(in);
t->fmt = allocAndCopy(getQuotedString(in));
} else if(!strcmp("custom_write", token)) {
t->custom_write = 1;
} else if(!strcmp("byte_order",token)) {
- getEqual(in);
- car = seekNextChar(in);
- if(car == EOF) in->error(in,"byte order was expected (network?)");
- else if(car == '\"') token = getQuotedString(in);
- else token = getName(in);
- if(!strcmp("network", token)) {
- t->network = 1;
- }
+ getEqual(in);
+ car = seekNextChar(in);
+ if(car == EOF) in->error(in,"byte order was expected (network?)");
+ else if(car == '\"') token = getQuotedString(in);
+ else token = getName(in);
+ if(!strcmp("network", token)) {
+ t->network = 1;
+ }
} else if(!strcmp("write",token)) {
- getEqual(in);
- car = seekNextChar(in);
- if(car == EOF) in->error(in,"write type was expected (custom?)");
- else if(car == '\"') token = getQuotedString(in);
- else token = getName(in);
- if(!strcmp("custom", token)) {
- t->custom_write = 1;
- }
+ getEqual(in);
+ car = seekNextChar(in);
+ if(car == EOF) in->error(in,"write type was expected (custom?)");
+ else if(car == '\"') token = getQuotedString(in);
+ else token = getName(in);
+ if(!strcmp("custom", token)) {
+ t->custom_write = 1;
+ }
}
}
}
{
char * token;
int car;
-
+
ev->name = NULL;
ev->per_trace = 0;
ev->per_tracefile = 0;
ev->compact_data = 0;
while(1) {
- token = getToken(in);
+ token = getToken(in);
if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
ungetToken(in);
break;
{
char * token;
int car;
-
+
fac->name = NULL;
fac->arch = NULL;
fac->align = 1;
fac->user = 0;
while(1) {
- token = getToken(in);
+ token = getToken(in);
if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
ungetToken(in);
break;
if(car == EOF) in->error(in,"name was expected");
else if(car == '\"') fac->name = allocAndCopy(getQuotedString(in));
else fac->name = allocAndCopy(getName(in));
- if(!strncmp(fac->name, "user_", sizeof("user_")-1))
- fac->user = 1;
+ if(!strncmp(fac->name, "user_", sizeof("user_")-1))
+ fac->user = 1;
} else if(!strcmp("arch", token)) {
getEqual(in);
car = seekNextChar(in);
f->name = NULL;
while(1) {
- token = getToken(in);
+ token = getToken(in);
if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
ungetToken(in);
break;
else if(car == '\"') f->name = allocAndCopy(getQuotedString(in));
else f->name = allocAndCopy(getName(in));
}
- }
+ }
}
char *getNameAttribute(parse_file_t *in)
char * token;
char *name = NULL;
int car;
-
+
while(1) {
- token = getToken(in);
+ token = getToken(in);
if(!strcmp("name",token)) {
getEqual(in);
car = seekNextChar(in);
char * token, *token2;
token = getToken(in);
-
+
if(strcmp("/",token) == 0 || strcmp(">", token) == 0){
ungetToken(in);
return 0;
}
if(strcmp("value",token))in->error(in,"value was expected");
-
+
getEqual(in);
token = getToken(in);
in->type = NUMBER;
token2 = token;
do {
- if (!isdigit(*token2)) {
+ if (!isdigit(*token2)) {
in->type = QUOTEDSTRING;
break;
- }
+ }
} while (*(++token2) != '\0');
}
if(in->type == NUMBER)
- *value = strtoll(token, NULL, 0);
- else
- goto error;
+ *value = strtoll(token, NULL, 0);
+ else
+ goto error;
return 1;
error:
in->error(in,"incorrect size specification");
fseek(in->fp, pos, SEEK_SET);
return NULL;
}
-
+
getRAnglebracket(in);
pos = 0;
/*****************************************************************************
*Function name
- * parseFacility : generate event list
- *Input params
- * in : input file handle
+ * parseFacility : generate event list
+ *Input params
+ * in : input file handle
* fac : empty facility
*Output params
* fac : facility filled with event list
{
char * token;
event_t *ev;
-
+
getFacilityAttributes(in, fac);
if(fac->name == NULL) in->error(in, "Attribute not named");
-
+
fac->capname = allocAndCopy(fac->name);
- strupper(fac->capname);
- getRAnglebracket(in);
-
+ strupper(fac->capname);
+ getRAnglebracket(in);
+
fac->description = getDescription(in);
-
+
while(1){
- getLAnglebracket(in);
+ getLAnglebracket(in);
token = getToken(in);
if(in->type == ENDFILE)
if(strcmp("event",token) == 0){
ev = (event_t*) memAlloc(sizeof(event_t));
sequence_push(&(fac->events),ev);
- parseEvent(fac, in, ev, &(fac->unnamed_types), &(fac->named_types));
+ parseEvent(fac, in, ev, &(fac->unnamed_types), &(fac->named_types));
}else if(strcmp("type",token) == 0){
parseTypeDefinition(fac, in, &(fac->unnamed_types), &(fac->named_types));
}else if(in->type == FORWARDSLASH){
/*****************************************************************************
*Function name
- * parseEvent : generate event from event definition
+ * parseEvent : generate event from event definition
*Input params
* fac : facility holding the event
- * in : input file handle
- * ev : new event
+ * in : input file handle
+ * ev : new event
* unnamed_types : array of unamed types
* named_types : array of named types
- *Output params
- * ev : new event (parameters are passed to it)
+ *Output params
+ * ev : new event (parameters are passed to it)
****************************************************************************/
-void parseEvent(facility_t *fac, parse_file_t *in, event_t * ev, sequence_t * unnamed_types,
- table_t * named_types)
+void parseEvent(facility_t *fac, parse_file_t *in, event_t * ev, sequence_t * unnamed_types,
+ table_t * named_types)
{
char *token;
- field_t *f;
+ field_t *f;
ev->fac = fac;
- sequence_init(&(ev->fields));
+ sequence_init(&(ev->fields));
//<event name=eventtype_name>
getEventAttributes(in, ev);
if(ev->name == NULL) in->error(in, "Event not named");
- getRAnglebracket(in);
+ getRAnglebracket(in);
//<description>...</description>
- ev->description = getDescription(in);
-
- int got_end = 0;
- /* Events can have multiple fields. each field form at least a function
- * parameter of the logging function. */
- while(!got_end) {
- getLAnglebracket(in);
- token = getToken(in);
-
- switch(in->type) {
- case FORWARDSLASH: /* </event> */
- token = getName(in);
- if(strcmp("event",token))in->error(in,"not an event definition");
- getRAnglebracket(in); //</event>
- got_end = 1;
- break;
- case NAME: /* a field */
- if(strcmp("field",token))in->error(in,"expecting a field");
- f = (field_t *)memAlloc(sizeof(field_t));
- sequence_push(&(ev->fields),f);
- parseFields(fac, in, f, unnamed_types, named_types, 1);
- break;
- default:
- in->error(in, "expecting </event> or <field >");
- break;
- }
- }
+ ev->description = getDescription(in);
+
+ int got_end = 0;
+ /* Events can have multiple fields. each field form at least a function
+ * parameter of the logging function. */
+ while(!got_end) {
+ getLAnglebracket(in);
+ token = getToken(in);
+
+ switch(in->type) {
+ case FORWARDSLASH: /* </event> */
+ token = getName(in);
+ if(strcmp("event",token))in->error(in,"not an event definition");
+ getRAnglebracket(in); //</event>
+ got_end = 1;
+ break;
+ case NAME: /* a field */
+ if(strcmp("field",token))in->error(in,"expecting a field");
+ f = (field_t *)memAlloc(sizeof(field_t));
+ sequence_push(&(ev->fields),f);
+ parseFields(fac, in, f, unnamed_types, named_types, 1);
+ break;
+ default:
+ in->error(in, "expecting </event> or <field >");
+ break;
+ }
+ }
#if 0
- if(in->type == FORWARDSLASH){ //</event> NOTHING
- ev->type = NULL;
- }else if(in->type == NAME){
- if(strcmp("struct",token)==0 || strcmp("typeref",token)==0){
- ungetToken(in);
- ev->type = parseType(in,NULL, unnamed_types, named_types);
- if(ev->type->type != STRUCT && ev->type->type != NONE)
- in->error(in,"type must be a struct");
- }else in->error(in, "not a valid type");
-
- getLAnglebracket(in);
- getForwardslash(in);
- }else in->error(in,"not a struct type");
- getLAnglebracket(in);
- getForwardslash(in);
- token = getName(in);
- if(strcmp("event",token))in->error(in,"not an event definition");
- getRAnglebracket(in); //</event>
+ if(in->type == FORWARDSLASH){ //</event> NOTHING
+ ev->type = NULL;
+ }else if(in->type == NAME){
+ if(strcmp("struct",token)==0 || strcmp("typeref",token)==0){
+ ungetToken(in);
+ ev->type = parseType(in,NULL, unnamed_types, named_types);
+ if(ev->type->type != STRUCT && ev->type->type != NONE)
+ in->error(in,"type must be a struct");
+ }else in->error(in, "not a valid type");
+
+ getLAnglebracket(in);
+ getForwardslash(in);
+ }else in->error(in,"not a struct type");
+ getLAnglebracket(in);
+ getForwardslash(in);
+ token = getName(in);
+ if(strcmp("event",token))in->error(in,"not an event definition");
+ getRAnglebracket(in); //</event>
#endif //0
}
/*****************************************************************************
*Function name
- * parseField : get field infomation from buffer
- *Input params
+ * parseField : get field infomation from buffer
+ *Input params
* fac : facility holding the field
* in : input file handle
* f : field
* unnamed_types : array of unamed types
* named_types : array of named types
- * tag : is field surrounded by a <field> </field> tag ?
+ * tag : is field surrounded by a <field> </field> tag ?
****************************************************************************/
void parseFields(facility_t *fac, parse_file_t *in, field_t *f,
sequence_t * unnamed_types,
- table_t * named_types,
- int tag)
+ table_t * named_types,
+ int tag)
{
char * token;
f->fac = fac;
- if(tag) {
- //<field name=field_name> <description> <type> </field>
- getFieldAttributes(in, f);
- if(f->name == NULL) in->error(in, "Field not named");
- getRAnglebracket(in);
+ if(tag) {
+ //<field name=field_name> <description> <type> </field>
+ getFieldAttributes(in, f);
+ if(f->name == NULL) in->error(in, "Field not named");
+ getRAnglebracket(in);
- f->description = getDescription(in);
- } else {
- f->description = NULL;
- }
+ f->description = getDescription(in);
+ } else {
+ f->description = NULL;
+ }
//<int size=...>
getLAnglebracket(in);
f->type = parseType(fac, in,NULL, unnamed_types, named_types);
- if(tag) {
- getLAnglebracket(in);
- getForwardslash(in);
- token = getName(in);
- if(strcmp("field",token))in->error(in,"not a valid field definition");
- getRAnglebracket(in); //</field>
- }
+ if(tag) {
+ getLAnglebracket(in);
+ getForwardslash(in);
+ token = getName(in);
+ if(strcmp("field",token))in->error(in,"not a valid field definition");
+ getRAnglebracket(in); //</field>
+ }
}
/*****************************************************************************
*Function name
- * parseType : get type information, type can be :
+ * parseType : get type information, type can be :
* Primitive:
- * int(size,fmt); uint(size,fmt); float(size,fmt);
+ * int(size,fmt); uint(size,fmt); float(size,fmt);
* string(fmt); enum(size,fmt,(label1,label2...))
* Compound:
* array(arraySize, type); sequence(lengthSize,type)
- * struct(field(name,type,description)...)
+ * struct(field(name,type,description)...)
* type name:
* type(name,type)
- *Input params
+ *Input params
* fac : facility
* in : input file handle
- * inType : a type descriptor
+ * inType : a type descriptor
* unnamed_types : array of unamed types
* named_types : array of named types
- *Return values
- * type_descriptor* : a type descriptor
+ *Return values
+ * type_descriptor* : a type descriptor
****************************************************************************/
-type_descriptor_t *parseType(facility_t *fac, parse_file_t *in, type_descriptor_t *inType,
- sequence_t * unnamed_types, table_t * named_types)
+type_descriptor_t *parseType(facility_t *fac, parse_file_t *in, type_descriptor_t *inType,
+ sequence_t * unnamed_types, table_t * named_types)
{
char *token;
type_descriptor_t *t;
- field_t *f;
+ field_t *f;
if(inType == NULL) {
t = (type_descriptor_t *) memAlloc(sizeof(type_descriptor_t));
token = getToken(in);
sequence_init(&(t->fields));
while(strcmp("field",token) == 0){
- f = (field_t *)memAlloc(sizeof(field_t));
- sequence_push(&(t->fields),f);
+ f = (field_t *)memAlloc(sizeof(field_t));
+ sequence_push(&(t->fields),f);
parseFields(fac, in, f, unnamed_types, named_types, 1);
-
+
//next field
getLAnglebracket(in);
- token = getToken(in);
+ token = getToken(in);
}
if(strcmp("/",token))in->error(in,"not a valid structure definition");
token = getName(in);
token = getToken(in);
sequence_init(&(t->fields));
while(strcmp("field",token) == 0){
- f = (field_t *)memAlloc(sizeof(field_t));
- sequence_push(&(t->fields),f);
+ f = (field_t *)memAlloc(sizeof(field_t));
+ sequence_push(&(t->fields),f);
parseFields(fac, in, f, unnamed_types, named_types, 1);
-
+
//next field
getLAnglebracket(in);
- token = getToken(in);
+ token = getToken(in);
}
if(strcmp("/",token))in->error(in,"not a valid union definition");
token = getName(in);
if(strcmp("union",token)!=0)
- in->error(in,"not a valid union definition");
+ in->error(in,"not a valid union definition");
getRAnglebracket(in); //</union>
}
else if(strcmp(token,"array") == 0) {
getForwardslash(in);
getRAnglebracket(in); //<array size=n>
- //getLAnglebracket(in); //<subtype>
- /* subfield */
+ //getLAnglebracket(in); //<subtype>
+ /* subfield */
f = (field_t *)memAlloc(sizeof(field_t));
-
+
f->name = NULL;
sequence_push(&(t->fields),f);
parseFields(fac, in, f, unnamed_types, named_types, 0);
- //getLAnglebracket(in); //<type struct>
+ //getLAnglebracket(in); //<type struct>
//t->nested_type = parseType(in, NULL, unnamed_types, named_types);
getLAnglebracket(in); //</array>
getForwardslash(in);
getRAnglebracket(in); //<sequence>
- //getLAnglebracket(in); //<sequence size type>
- /* subfield */
+ //getLAnglebracket(in); //<sequence size type>
+ /* subfield */
f = (field_t *)memAlloc(sizeof(field_t));
f->name = NULL;
sequence_push(&(t->fields),f);
parseFields(fac, in, f, unnamed_types, named_types, 0);
- //getLAnglebracket(in); //<subtype>
- /* subfield */
+ //getLAnglebracket(in); //<subtype>
+ /* subfield */
f = (field_t *)memAlloc(sizeof(field_t));
f->name = NULL;
sequence_push(&(t->fields),f);
parseFields(fac, in, f, unnamed_types, named_types, 0);
- //getLAnglebracket(in); //<type sequence>
+ //getLAnglebracket(in); //<type sequence>
//t->length_type = parseType(in, NULL, unnamed_types, named_types);
- //getLAnglebracket(in); //<type sequence>
+ //getLAnglebracket(in); //<type sequence>
//t->nested_type = parseType(in, NULL, unnamed_types, named_types);
if(t->fields.position < 1) in->error(in, "Sequence has no length type");
if(t->fields.position < 2) in->error(in, "Sequence has no subtype");
- switch(((field_t*)t->fields.array[0])->type->type) {
- case UINT_FIXED :
- case UCHAR :
- case USHORT :
- case UINT :
- case ULONG :
- case SIZE_T :
- case OFF_T :
- break;
- default:
- in->error(in, "Wrong length type for sequence");
- }
+ switch(((field_t*)t->fields.array[0])->type->type) {
+ case UINT_FIXED :
+ case UCHAR :
+ case USHORT :
+ case UINT :
+ case ULONG :
+ case SIZE_T :
+ case OFF_T :
+ break;
+ default:
+ in->error(in, "Wrong length type for sequence");
+ }
getLAnglebracket(in); //</sequence>
getForwardslash(in);
sequence_init(&(t->labels));
sequence_init(&(t->labels_values));
sequence_init(&(t->labels_description));
- t->already_printed = 0;
+ t->already_printed = 0;
getTypeAttributes(in, t, unnamed_types, named_types);
//if(t->size == 0) in->error(in, "Sequence has empty size");
- //Mathieu : we fix enum size to target int size. GCC is always like this.
- //fox copy optimisation.
+ //Mathieu : we fix enum size to target int size. GCC is always like this.
+ //fox copy optimisation.
if(t->size != 0) in->error(in, "Enum has fixed size of target int.");
- t->size = 0;
+ t->size = 0;
getRAnglebracket(in);
//<label name=label1 value=n/>
token = getToken(in); //"label" or "/"
while(strcmp("label",token) == 0){
int *label_value = malloc(sizeof(int));
- int has_value = 0;
- long long loc_value;
-
+ int has_value = 0;
+ long long loc_value;
+
str = allocAndCopy(getNameAttribute(in));
has_value = getValueAttribute(in, &loc_value);
-
+
sequence_push(&(t->labels),str);
if(has_value) value = loc_value;
getForwardslash(in);
getRAnglebracket(in);
-
+
//read description if any. May be NULL.
str = allocAndCopy(getDescription(in));
- sequence_push(&(t->labels_description),str);
-
+ sequence_push(&(t->labels_description),str);
+
//next label definition
getLAnglebracket(in);
- token = getToken(in); //"label" or "/"
+ token = getToken(in); //"label" or "/"
}
if(strcmp("/",token))in->error(in, "not a valid enum definition");
token = getName(in);
getTypeAttributes(in, t, unnamed_types, named_types);
if(t->size == 0) in->error(in, "int has empty size");
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"uint_fixed") == 0) {
t->type = UINT_FIXED;
getTypeAttributes(in, t, unnamed_types, named_types);
if(t->size == 0) in->error(in, "uint has empty size");
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"char") == 0) {
t->type = CHAR;
getTypeAttributes(in, t, unnamed_types, named_types);
- t->size = 1;
+ t->size = 1;
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"uchar") == 0) {
t->type = UCHAR;
getTypeAttributes(in, t, unnamed_types, named_types);
- t->size = 1;
+ t->size = 1;
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"short") == 0) {
t->type = SHORT;
getTypeAttributes(in, t, unnamed_types, named_types);
- t->size = 2;
+ t->size = 2;
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"ushort") == 0) {
t->type = USHORT;
getTypeAttributes(in, t, unnamed_types, named_types);
- t->size = 2;
+ t->size = 2;
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"int") == 0) {
t->type = INT;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"uint") == 0) {
t->type = UINT;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"pointer") == 0) {
t->type = POINTER;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"long") == 0) {
t->type = LONG;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"ulong") == 0) {
t->type = ULONG;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"size_t") == 0) {
t->type = SIZE_T;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"ssize_t") == 0) {
t->type = SSIZE_T;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"off_t") == 0) {
t->type = OFF_T;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"float") == 0) {
t->type = FLOAT;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"string") == 0) {
t->type = STRING;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"typeref") == 0){
// Must be a named type
- free(t);
- sequence_pop(unnamed_types);
- token = getNameAttribute(in);
- t = find_named_type(token, named_types);
- if(t == NULL) in->error(in,"Named referred to must be pre-declared.");
- getForwardslash(in); //<typeref name=type_name/>
- getRAnglebracket(in);
- return t;
+ free(t);
+ sequence_pop(unnamed_types);
+ token = getNameAttribute(in);
+ t = find_named_type(token, named_types);
+ if(t == NULL) in->error(in,"Named referred to must be pre-declared.");
+ getForwardslash(in); //<typeref name=type_name/>
+ getRAnglebracket(in);
+ return t;
}else in->error(in,"not a valid type");
return t;
-}
+}
/*****************************************************************************
*Function name
- * find_named_type : find a named type from hash table
- *Input params
- * name : type name
+ * find_named_type : find a named type from hash table
+ *Input params
+ * name : type name
* named_types : array of named types
- *Return values
- * type_descriptor * : a type descriptor
+ *Return values
+ * type_descriptor * : a type descriptor
*****************************************************************************/
type_descriptor_t * find_named_type(char *name, table_t * named_types)
-{
+{
type_descriptor_t *t;
t = (type_descriptor_t*)table_find(named_types,name);
{
type_descriptor_t *t;
- t = (type_descriptor_t *)memAlloc(sizeof(type_descriptor_t));
- t->type_name = allocAndCopy(name);
- t->type = NONE;
- t->fmt = NULL;
- table_insert(named_types,t->type_name,t);
- // table_insert(named_types,allocAndCopy(name),t);
- return t;
+ t = (type_descriptor_t *)memAlloc(sizeof(type_descriptor_t));
+ t->type_name = allocAndCopy(name);
+ t->type = NONE;
+ t->fmt = NULL;
+ table_insert(named_types,t->type_name,t);
+ // table_insert(named_types,allocAndCopy(name),t);
+ return t;
}
/*****************************************************************************
*Function name
- * parseTypeDefinition : get type information from type definition
- *Input params
+ * parseTypeDefinition : get type information from type definition
+ *Input params
* fac : facility
- * in : input file handle
+ * in : input file handle
* unnamed_types : array of unamed types
* named_types : array of named types
*****************************************************************************/
void parseTypeDefinition(facility_t *fac, parse_file_t * in, sequence_t * unnamed_types,
- table_t * named_types)
+ table_t * named_types)
{
char *token;
type_descriptor_t *t;
//MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
ungetToken(in);
parseType(fac, in,t, unnamed_types, named_types);
-
+
//</type>
getLAnglebracket(in);
getForwardslash(in);
token = getName(in);
- if(strcmp("type",token))in->error(in,"not a valid type definition");
+ if(strcmp("type",token))in->error(in,"not a valid type definition");
getRAnglebracket(in); //</type>
}
*
**************************************************************************/
-char *getName(parse_file_t * in)
+char *getName(parse_file_t * in)
{
char *token;
token = getToken(in);
// Optional descriptions
- // if(in->type != NAME) in->error(in,"Name token was expected");
+ // if(in->type != NAME) in->error(in,"Name token was expected");
return token;
}
-int getNumber(parse_file_t * in)
+int getNumber(parse_file_t * in)
{
char *token;
return atoi(token);
}
-char *getForwardslash(parse_file_t * in)
+char *getForwardslash(parse_file_t * in)
{
char *token;
token = getToken(in);
//if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected");
- /* Mathieu : final / is optional now. */
+ /* Mathieu : final / is optional now. */
if(in->type != FORWARDSLASH) ungetToken(in);
return token;
}
-char *getLAnglebracket(parse_file_t * in)
+char *getLAnglebracket(parse_file_t * in)
{
char *token;
return token;
}
-char *getRAnglebracket(parse_file_t * in)
+char *getRAnglebracket(parse_file_t * in)
{
char *token;
return token;
}
-char *getQuotedString(parse_file_t * in)
+char *getQuotedString(parse_file_t * in)
{
char *token;
ungetc(car,in->fp);
return car;
}
- }
+ }
return EOF;
}
while((car = getc(fp)) != EOF) {
if(car == '/') {
- car1 = getc(fp);
+ car1 = getc(fp);
if(car1 == '*') skipComment(in);
else if(car1 == '/') skipEOL(in);
- else {
+ else {
car1 = ungetc(car1,fp);
break;
}
escaped = 0;
while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) {
if(car == '\\' && escaped == 0) {
- in->buffer[pos] = car;
- pos++;
+ in->buffer[pos] = car;
+ pos++;
escaped = 1;
continue;
}
in->buffer[pos] = car;
pos++;
}
- if(car == EOF) ungetc(car,fp);
+ if(car == EOF) ungetc(car,fp);
if(pos == BUFFER_SIZE) in->error(in, "number token too large");
in->type = NUMBER;
}
in->buffer[pos] = car;
pos++;
}
- if(car == EOF) ungetc(car,fp);
+ if(car == EOF) ungetc(car,fp);
if(pos == BUFFER_SIZE) in->error(in, "name token too large");
in->type = NAME;
} else if(car == '?') {
- in->buffer[0] = car;
- pos++;
- }
+ in->buffer[0] = car;
+ pos++;
+ }
else in->error(in, "invalid character, unrecognized token");
}
in->buffer[pos] = 0;
if(t->type == NONE){
sprintf(str,"named type '%s' has no definition",
(char*)named_types->keys.array[pos]);
- error_callback(NULL,str);
+ error_callback(NULL,str);
}
}
}
for(pos = 0; pos < events->position; pos++){
ev = (event_t *)(events->array[pos]);
crc = partial_crc32(ev->name, crc);
- for(i = 0; i < ev->fields.position; i++) {
- field_t *f = (field_t*)ev->fields.array[i];
+ for(i = 0; i < ev->fields.position; i++) {
+ field_t *f = (field_t*)ev->fields.array[i];
crc = partial_crc32(f->name, crc);
crc = getTypeChecksum(crc, f->type);
- }
+ }
}
*checksum = crc;
}
*Input Params
* crc : checksum generated so far
* type : type descriptor containing type info
- *Return value
- * unsigned long : checksum
+ *Return value
+ * unsigned long : checksum
*****************************************************************************/
unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor_t * type)
break;
case POINTER:
str = allocAndCopy("void *");
- flag = 1;
+ flag = 1;
break;
case CHAR:
str = allocAndCopy("signed char");
- flag = 1;
+ flag = 1;
break;
case UCHAR:
str = allocAndCopy("unsigned char");
- flag = 1;
+ flag = 1;
break;
case SHORT:
str = allocAndCopy("short");
- flag = 1;
+ flag = 1;
break;
case USHORT:
str = allocAndCopy("unsigned short");
- flag = 1;
+ flag = 1;
break;
case INT:
str = allocAndCopy("int");
- flag = 1;
+ flag = 1;
break;
case UINT:
str = allocAndCopy("uint");
- flag = 1;
+ flag = 1;
break;
case LONG:
str = allocAndCopy("long");
- flag = 1;
+ flag = 1;
break;
case ULONG:
str = allocAndCopy("unsigned long");
- flag = 1;
+ flag = 1;
break;
case SIZE_T:
str = allocAndCopy("size_t");
- flag = 1;
+ flag = 1;
break;
case SSIZE_T:
str = allocAndCopy("ssize_t");
- flag = 1;
+ flag = 1;
break;
case OFF_T:
str = allocAndCopy("off_t");
- flag = 1;
+ flag = 1;
break;
case FLOAT:
str = floatOutputTypes[getSizeindex(type->size)];
if(flag) free(str);
if(type->fmt) crc = partial_crc32(type->fmt,crc);
-
+
if(type->type == ARRAY){
crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type);
} else if(type->type ==SEQUENCE) {
crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type);
crc = getTypeChecksum(crc,((field_t*)type->fields.array[1])->type);
- } else if(type->type == STRUCT || type->type == UNION){
+ } else if(type->type == STRUCT || type->type == UNION){
for(pos =0; pos < type->fields.position; pos++){
fld = (field_t *) type->fields.array[pos];
crc = partial_crc32(fld->name,crc);
crc = getTypeChecksum(crc, fld->type);
- }
+ }
}else if(type->type == ENUM){
for(pos = 0; pos < type->labels.position; pos++)
crc = partial_crc32((char*)type->labels.array[pos],crc);
}
}
-void freeTypes(sequence_t *t)
+void freeTypes(sequence_t *t)
{
int pos;
type_descriptor_t *tp;
}
}
-void freeEvents(sequence_t *t)
+void freeEvents(sequence_t *t)
{
int pos;
event_t *ev;
ev = (event_t *) t->array[pos];
free(ev->name);
free(ev->description);
- sequence_dispose(&ev->fields);
+ sequence_dispose(&ev->fields);
free(ev);
}
/* Extensible array */
-void sequence_init(sequence_t *t)
+void sequence_init(sequence_t *t)
{
t->size = 10;
t->position = 0;
t->array = (void **)memAlloc(t->size * sizeof(void *));
}
-void sequence_dispose(sequence_t *t)
+void sequence_dispose(sequence_t *t)
{
t->size = 0;
free(t->array);
t->array = NULL;
}
-void sequence_push(sequence_t *t, void *elem)
+void sequence_push(sequence_t *t, void *elem)
{
void **tmp;
t->position++;
}
-void *sequence_pop(sequence_t *t)
+void *sequence_pop(sequence_t *t)
{
- if(t->position == 0) printf("Error : trying to pop an empty sequence");
+ if(t->position == 0) printf("Error : trying to pop an empty sequence");
return t->array[--t->position];
}
/* Hash table API, implementation is just linear search for now */
-void table_init(table_t *t)
+void table_init(table_t *t)
{
sequence_init(&(t->keys));
sequence_init(&(t->values));
}
-void table_dispose(table_t *t)
+void table_dispose(table_t *t)
{
sequence_dispose(&(t->keys));
sequence_dispose(&(t->values));
}
-void table_insert(table_t *t, char *key, void *value)
+void table_insert(table_t *t, char *key, void *value)
{
sequence_push(&(t->keys),key);
sequence_push(&(t->values),value);
}
-void *table_find(table_t *t, char *key)
+void *table_find(table_t *t, char *key)
{
int pos;
for(pos = 0 ; pos < t->keys.position; pos++) {
/* Concatenate strings */
-char *appendString(char *s, char *suffix)
+char *appendString(char *s, char *suffix)
{
char *tmp;
if(suffix == NULL) return s;
tmp = (char *)memAlloc(strlen(s) + strlen(suffix) + 1);
strcpy(tmp,s);
- strcat(tmp,suffix);
+ strcat(tmp,suffix);
return tmp;
}
parser.c: Generate helper declarations and functions to trace events
from an event description file.
- Copyright (C) 2005, Mathieu Desnoyers
+ Copyright (C) 2005, Mathieu Desnoyers
Copyright (C) 2002, Xianxiu Yang
- Copyright (C) 2002, Michel Dagenais
+ Copyright (C) 2002, Michel Dagenais
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-/* This program reads the ".xml" event definitions input files
+/* This program reads the ".xml" event definitions input files
and constructs structure for each event.
-
+
The program uses a very simple tokenizer, called from a hand written
recursive descent parser to fill a data structure describing the events.
The result is a sequence of events definitions which refer to type
A table of named types is maintained to allow refering to types by name
when the same type is used at several places. Finally a sequence of
- all types is maintained to facilitate the freeing of all type
+ all types is maintained to facilitate the freeing of all type
information when the processing of an ".xml" file is finished. */
-#include <stdlib.h>
+#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
-#include <linux/errno.h>
+#include <linux/errno.h>
#include <assert.h>
#include <ctype.h>
void strupper(char *string)
{
char *ptr = string;
-
+
while(*ptr != '\0') {
*ptr = toupper(*ptr);
- ptr++;
+ ptr++;
}
}
int getSizeindex(unsigned int value)
-{
+{
switch(value) {
case 1:
return 0;
/*****************************************************************************
*Function name
* getSize : translate from string to integer
- *Input params
+ *Input params
* in : input file handle
- *Return values
- * size
+ *Return values
+ * size
*****************************************************************************/
unsigned long long int getSize(parse_file_t *in)
in->type = NUMBER;
token2 = token;
do {
- if (!isdigit(*token2)) {
- in->type = QUOTEDSTRING;
- break;
- }
+ if (!isdigit(*token2)) {
+ in->type = QUOTEDSTRING;
+ break;
+ }
} while (*(++token2) != '\0');
}
if(in->type == NUMBER) {
- ret = strtoull(token, NULL, 0);
+ ret = strtoull(token, NULL, 0);
} else {
- goto error;
- }
-
- return ret;
+ goto error;
+ }
+
+ return ret;
error:
in->error(in,"incorrect size specification");
return -1;
* error_callback : print out error info
*Input params
* in : input file handle
- * msg : message to be printed
+ * msg : message to be printed
****************************************************************************/
void error_callback(parse_file_t *in, char *msg)
/*****************************************************************************
*Function name
- * memAlloc : allocate memory
- *Input params
- * size : required memory size
- *return value
- * void * : pointer to allocate memory or NULL
+ * memAlloc : allocate memory
+ *Input params
+ * size : required memory size
+ *return value
+ * void * : pointer to allocate memory or NULL
****************************************************************************/
void * memAlloc(int size)
if(size == 0) return NULL;
addr = malloc(size);
if(!addr){
- printf("Failed to allocate memory");
+ printf("Failed to allocate memory");
exit(1);
}
- return addr;
+ return addr;
}
/*****************************************************************************
*Function name
- * allocAndCopy : allocate memory and initialize it
- *Input params
- * str : string to be put in memory
- *return value
+ * allocAndCopy : allocate memory and initialize it
+ *Input params
+ * str : string to be put in memory
+ *return value
* char * : pointer to allocate memory or NULL
****************************************************************************/
**************************************************************************/
void getTypeAttributes(parse_file_t *in, type_descriptor_t *t,
- sequence_t * unnamed_types, table_t * named_types)
+ sequence_t * unnamed_types, table_t * named_types)
{
char * token;
int car;
t->size = 0;
t->custom_write = 0;
t->network = 0;
-
+
while(1) {
- token = getToken(in);
+ token = getToken(in);
if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
ungetToken(in);
break;
}
-
+
if(!strcmp("format",token)) {
getEqual(in);
t->fmt = allocAndCopy(getQuotedString(in));
} else if(!strcmp("custom_write", token)) {
t->custom_write = 1;
} else if(!strcmp("byte_order",token)) {
- getEqual(in);
- car = seekNextChar(in);
- if(car == EOF) in->error(in,"byte order was expected (network?)");
- else if(car == '\"') token = getQuotedString(in);
- else token = getName(in);
- if(!strcmp("network", token)) {
- t->network = 1;
- }
+ getEqual(in);
+ car = seekNextChar(in);
+ if(car == EOF) in->error(in,"byte order was expected (network?)");
+ else if(car == '\"') token = getQuotedString(in);
+ else token = getName(in);
+ if(!strcmp("network", token)) {
+ t->network = 1;
+ }
} else if(!strcmp("write",token)) {
- getEqual(in);
- car = seekNextChar(in);
- if(car == EOF) in->error(in,"write type was expected (custom?)");
- else if(car == '\"') token = getQuotedString(in);
- else token = getName(in);
- if(!strcmp("custom", token)) {
- t->custom_write = 1;
- }
+ getEqual(in);
+ car = seekNextChar(in);
+ if(car == EOF) in->error(in,"write type was expected (custom?)");
+ else if(car == '\"') token = getQuotedString(in);
+ else token = getName(in);
+ if(!strcmp("custom", token)) {
+ t->custom_write = 1;
+ }
}
}
}
{
char * token;
int car;
-
+
ev->name = NULL;
ev->per_trace = 0;
ev->per_tracefile = 0;
ev->compact_data = 0;
while(1) {
- token = getToken(in);
+ token = getToken(in);
if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
ungetToken(in);
break;
{
char * token;
int car;
-
+
fac->name = NULL;
fac->arch = NULL;
fac->align = 1;
fac->user = 0;
while(1) {
- token = getToken(in);
+ token = getToken(in);
if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
ungetToken(in);
break;
if(car == EOF) in->error(in,"name was expected");
else if(car == '\"') fac->name = allocAndCopy(getQuotedString(in));
else fac->name = allocAndCopy(getName(in));
- if(!strncmp(fac->name, "user_", sizeof("user_")-1))
- fac->user = 1;
+ if(!strncmp(fac->name, "user_", sizeof("user_")-1))
+ fac->user = 1;
} else if(!strcmp("arch", token)) {
getEqual(in);
car = seekNextChar(in);
f->name = NULL;
while(1) {
- token = getToken(in);
+ token = getToken(in);
if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
ungetToken(in);
break;
else if(car == '\"') f->name = allocAndCopy(getQuotedString(in));
else f->name = allocAndCopy(getName(in));
}
- }
+ }
}
char *getNameAttribute(parse_file_t *in)
char * token;
char *name = NULL;
int car;
-
+
while(1) {
- token = getToken(in);
+ token = getToken(in);
if(!strcmp("name",token)) {
getEqual(in);
car = seekNextChar(in);
char * token, *token2;
token = getToken(in);
-
+
if(strcmp("/",token) == 0 || strcmp(">", token) == 0){
ungetToken(in);
return 0;
}
if(strcmp("value",token))in->error(in,"value was expected");
-
+
getEqual(in);
token = getToken(in);
in->type = NUMBER;
token2 = token;
do {
- if (!isdigit(*token2)) {
+ if (!isdigit(*token2)) {
in->type = QUOTEDSTRING;
break;
- }
+ }
} while (*(++token2) != '\0');
}
if(in->type == NUMBER)
- *value = strtoll(token, NULL, 0);
- else
- goto error;
+ *value = strtoll(token, NULL, 0);
+ else
+ goto error;
return 1;
error:
in->error(in,"incorrect size specification");
fseek(in->fp, pos, SEEK_SET);
return NULL;
}
-
+
getRAnglebracket(in);
pos = 0;
/*****************************************************************************
*Function name
- * parseFacility : generate event list
- *Input params
- * in : input file handle
+ * parseFacility : generate event list
+ *Input params
+ * in : input file handle
* fac : empty facility
*Output params
* fac : facility filled with event list
{
char * token;
event_t *ev;
-
+
getFacilityAttributes(in, fac);
if(fac->name == NULL) in->error(in, "Attribute not named");
-
+
fac->capname = allocAndCopy(fac->name);
- strupper(fac->capname);
- getRAnglebracket(in);
-
+ strupper(fac->capname);
+ getRAnglebracket(in);
+
fac->description = getDescription(in);
-
+
while(1){
- getLAnglebracket(in);
+ getLAnglebracket(in);
token = getToken(in);
if(in->type == ENDFILE)
if(strcmp("event",token) == 0){
ev = (event_t*) memAlloc(sizeof(event_t));
sequence_push(&(fac->events),ev);
- parseEvent(fac, in, ev, &(fac->unnamed_types), &(fac->named_types));
+ parseEvent(fac, in, ev, &(fac->unnamed_types), &(fac->named_types));
}else if(strcmp("type",token) == 0){
parseTypeDefinition(fac, in, &(fac->unnamed_types), &(fac->named_types));
}else if(in->type == FORWARDSLASH){
/*****************************************************************************
*Function name
- * parseEvent : generate event from event definition
+ * parseEvent : generate event from event definition
*Input params
* fac : facility holding the event
- * in : input file handle
- * ev : new event
+ * in : input file handle
+ * ev : new event
* unnamed_types : array of unamed types
* named_types : array of named types
- *Output params
- * ev : new event (parameters are passed to it)
+ *Output params
+ * ev : new event (parameters are passed to it)
****************************************************************************/
-void parseEvent(facility_t *fac, parse_file_t *in, event_t * ev, sequence_t * unnamed_types,
- table_t * named_types)
+void parseEvent(facility_t *fac, parse_file_t *in, event_t * ev, sequence_t * unnamed_types,
+ table_t * named_types)
{
char *token;
- field_t *f;
+ field_t *f;
ev->fac = fac;
- sequence_init(&(ev->fields));
+ sequence_init(&(ev->fields));
//<event name=eventtype_name>
getEventAttributes(in, ev);
if(ev->name == NULL) in->error(in, "Event not named");
- getRAnglebracket(in);
+ getRAnglebracket(in);
//<description>...</description>
- ev->description = getDescription(in);
-
- int got_end = 0;
- /* Events can have multiple fields. each field form at least a function
- * parameter of the logging function. */
- while(!got_end) {
- getLAnglebracket(in);
- token = getToken(in);
-
- switch(in->type) {
- case FORWARDSLASH: /* </event> */
- token = getName(in);
- if(strcmp("event",token))in->error(in,"not an event definition");
- getRAnglebracket(in); //</event>
- got_end = 1;
- break;
- case NAME: /* a field */
- if(strcmp("field",token))in->error(in,"expecting a field");
- f = (field_t *)memAlloc(sizeof(field_t));
- sequence_push(&(ev->fields),f);
- parseFields(fac, in, f, unnamed_types, named_types, 1);
- break;
- default:
- in->error(in, "expecting </event> or <field >");
- break;
- }
- }
+ ev->description = getDescription(in);
+
+ int got_end = 0;
+ /* Events can have multiple fields. each field form at least a function
+ * parameter of the logging function. */
+ while(!got_end) {
+ getLAnglebracket(in);
+ token = getToken(in);
+
+ switch(in->type) {
+ case FORWARDSLASH: /* </event> */
+ token = getName(in);
+ if(strcmp("event",token))in->error(in,"not an event definition");
+ getRAnglebracket(in); //</event>
+ got_end = 1;
+ break;
+ case NAME: /* a field */
+ if(strcmp("field",token))in->error(in,"expecting a field");
+ f = (field_t *)memAlloc(sizeof(field_t));
+ sequence_push(&(ev->fields),f);
+ parseFields(fac, in, f, unnamed_types, named_types, 1);
+ break;
+ default:
+ in->error(in, "expecting </event> or <field >");
+ break;
+ }
+ }
#if 0
- if(in->type == FORWARDSLASH){ //</event> NOTHING
- ev->type = NULL;
- }else if(in->type == NAME){
- if(strcmp("struct",token)==0 || strcmp("typeref",token)==0){
- ungetToken(in);
- ev->type = parseType(in,NULL, unnamed_types, named_types);
- if(ev->type->type != STRUCT && ev->type->type != NONE)
- in->error(in,"type must be a struct");
- }else in->error(in, "not a valid type");
-
- getLAnglebracket(in);
- getForwardslash(in);
- }else in->error(in,"not a struct type");
- getLAnglebracket(in);
- getForwardslash(in);
- token = getName(in);
- if(strcmp("event",token))in->error(in,"not an event definition");
- getRAnglebracket(in); //</event>
+ if(in->type == FORWARDSLASH){ //</event> NOTHING
+ ev->type = NULL;
+ }else if(in->type == NAME){
+ if(strcmp("struct",token)==0 || strcmp("typeref",token)==0){
+ ungetToken(in);
+ ev->type = parseType(in,NULL, unnamed_types, named_types);
+ if(ev->type->type != STRUCT && ev->type->type != NONE)
+ in->error(in,"type must be a struct");
+ }else in->error(in, "not a valid type");
+
+ getLAnglebracket(in);
+ getForwardslash(in);
+ }else in->error(in,"not a struct type");
+ getLAnglebracket(in);
+ getForwardslash(in);
+ token = getName(in);
+ if(strcmp("event",token))in->error(in,"not an event definition");
+ getRAnglebracket(in); //</event>
#endif //0
}
/*****************************************************************************
*Function name
- * parseField : get field infomation from buffer
- *Input params
+ * parseField : get field infomation from buffer
+ *Input params
* fac : facility holding the field
* in : input file handle
* f : field
* unnamed_types : array of unamed types
* named_types : array of named types
- * tag : is field surrounded by a <field> </field> tag ?
+ * tag : is field surrounded by a <field> </field> tag ?
****************************************************************************/
void parseFields(facility_t *fac, parse_file_t *in, field_t *f,
sequence_t * unnamed_types,
- table_t * named_types,
- int tag)
+ table_t * named_types,
+ int tag)
{
char * token;
f->fac = fac;
- if(tag) {
- //<field name=field_name> <description> <type> </field>
- getFieldAttributes(in, f);
- if(f->name == NULL) in->error(in, "Field not named");
- getRAnglebracket(in);
+ if(tag) {
+ //<field name=field_name> <description> <type> </field>
+ getFieldAttributes(in, f);
+ if(f->name == NULL) in->error(in, "Field not named");
+ getRAnglebracket(in);
- f->description = getDescription(in);
- } else {
- f->description = NULL;
- }
+ f->description = getDescription(in);
+ } else {
+ f->description = NULL;
+ }
//<int size=...>
getLAnglebracket(in);
f->type = parseType(fac, in,NULL, unnamed_types, named_types);
- if(tag) {
- getLAnglebracket(in);
- getForwardslash(in);
- token = getName(in);
- if(strcmp("field",token))in->error(in,"not a valid field definition");
- getRAnglebracket(in); //</field>
- }
+ if(tag) {
+ getLAnglebracket(in);
+ getForwardslash(in);
+ token = getName(in);
+ if(strcmp("field",token))in->error(in,"not a valid field definition");
+ getRAnglebracket(in); //</field>
+ }
}
/*****************************************************************************
*Function name
- * parseType : get type information, type can be :
+ * parseType : get type information, type can be :
* Primitive:
- * int(size,fmt); uint(size,fmt); float(size,fmt);
+ * int(size,fmt); uint(size,fmt); float(size,fmt);
* string(fmt); enum(size,fmt,(label1,label2...))
* Compound:
* array(arraySize, type); sequence(lengthSize,type)
- * struct(field(name,type,description)...)
+ * struct(field(name,type,description)...)
* type name:
* type(name,type)
- *Input params
+ *Input params
* fac : facility
* in : input file handle
- * inType : a type descriptor
+ * inType : a type descriptor
* unnamed_types : array of unamed types
* named_types : array of named types
- *Return values
- * type_descriptor* : a type descriptor
+ *Return values
+ * type_descriptor* : a type descriptor
****************************************************************************/
-type_descriptor_t *parseType(facility_t *fac, parse_file_t *in, type_descriptor_t *inType,
- sequence_t * unnamed_types, table_t * named_types)
+type_descriptor_t *parseType(facility_t *fac, parse_file_t *in, type_descriptor_t *inType,
+ sequence_t * unnamed_types, table_t * named_types)
{
char *token;
type_descriptor_t *t;
- field_t *f;
+ field_t *f;
if(inType == NULL) {
t = (type_descriptor_t *) memAlloc(sizeof(type_descriptor_t));
token = getToken(in);
sequence_init(&(t->fields));
while(strcmp("field",token) == 0){
- f = (field_t *)memAlloc(sizeof(field_t));
- sequence_push(&(t->fields),f);
+ f = (field_t *)memAlloc(sizeof(field_t));
+ sequence_push(&(t->fields),f);
parseFields(fac, in, f, unnamed_types, named_types, 1);
-
+
//next field
getLAnglebracket(in);
- token = getToken(in);
+ token = getToken(in);
}
if(strcmp("/",token))in->error(in,"not a valid structure definition");
token = getName(in);
token = getToken(in);
sequence_init(&(t->fields));
while(strcmp("field",token) == 0){
- f = (field_t *)memAlloc(sizeof(field_t));
- sequence_push(&(t->fields),f);
+ f = (field_t *)memAlloc(sizeof(field_t));
+ sequence_push(&(t->fields),f);
parseFields(fac, in, f, unnamed_types, named_types, 1);
-
+
//next field
getLAnglebracket(in);
- token = getToken(in);
+ token = getToken(in);
}
if(strcmp("/",token))in->error(in,"not a valid union definition");
token = getName(in);
if(strcmp("union",token)!=0)
- in->error(in,"not a valid union definition");
+ in->error(in,"not a valid union definition");
getRAnglebracket(in); //</union>
}
else if(strcmp(token,"array") == 0) {
getForwardslash(in);
getRAnglebracket(in); //<array size=n>
- //getLAnglebracket(in); //<subtype>
- /* subfield */
+ //getLAnglebracket(in); //<subtype>
+ /* subfield */
f = (field_t *)memAlloc(sizeof(field_t));
-
+
f->name = NULL;
sequence_push(&(t->fields),f);
parseFields(fac, in, f, unnamed_types, named_types, 0);
- //getLAnglebracket(in); //<type struct>
+ //getLAnglebracket(in); //<type struct>
//t->nested_type = parseType(in, NULL, unnamed_types, named_types);
getLAnglebracket(in); //</array>
getForwardslash(in);
getRAnglebracket(in); //<sequence>
- //getLAnglebracket(in); //<sequence size type>
- /* subfield */
+ //getLAnglebracket(in); //<sequence size type>
+ /* subfield */
f = (field_t *)memAlloc(sizeof(field_t));
f->name = NULL;
sequence_push(&(t->fields),f);
parseFields(fac, in, f, unnamed_types, named_types, 0);
- //getLAnglebracket(in); //<subtype>
- /* subfield */
+ //getLAnglebracket(in); //<subtype>
+ /* subfield */
f = (field_t *)memAlloc(sizeof(field_t));
f->name = NULL;
sequence_push(&(t->fields),f);
parseFields(fac, in, f, unnamed_types, named_types, 0);
- //getLAnglebracket(in); //<type sequence>
+ //getLAnglebracket(in); //<type sequence>
//t->length_type = parseType(in, NULL, unnamed_types, named_types);
- //getLAnglebracket(in); //<type sequence>
+ //getLAnglebracket(in); //<type sequence>
//t->nested_type = parseType(in, NULL, unnamed_types, named_types);
if(t->fields.position < 1) in->error(in, "Sequence has no length type");
if(t->fields.position < 2) in->error(in, "Sequence has no subtype");
- switch(((field_t*)t->fields.array[0])->type->type) {
- case UINT_FIXED :
- case UCHAR :
- case USHORT :
- case UINT :
- case ULONG :
- case SIZE_T :
- case OFF_T :
- break;
- default:
- in->error(in, "Wrong length type for sequence");
- }
+ switch(((field_t*)t->fields.array[0])->type->type) {
+ case UINT_FIXED :
+ case UCHAR :
+ case USHORT :
+ case UINT :
+ case ULONG :
+ case SIZE_T :
+ case OFF_T :
+ break;
+ default:
+ in->error(in, "Wrong length type for sequence");
+ }
getLAnglebracket(in); //</sequence>
getForwardslash(in);
sequence_init(&(t->labels));
sequence_init(&(t->labels_values));
sequence_init(&(t->labels_description));
- t->already_printed = 0;
+ t->already_printed = 0;
getTypeAttributes(in, t, unnamed_types, named_types);
//if(t->size == 0) in->error(in, "Sequence has empty size");
- //Mathieu : we fix enum size to target int size. GCC is always like this.
- //fox copy optimisation.
+ //Mathieu : we fix enum size to target int size. GCC is always like this.
+ //fox copy optimisation.
if(t->size != 0) in->error(in, "Enum has fixed size of target int.");
- t->size = 0;
+ t->size = 0;
getRAnglebracket(in);
//<label name=label1 value=n/>
token = getToken(in); //"label" or "/"
while(strcmp("label",token) == 0){
int *label_value = malloc(sizeof(int));
- int has_value = 0;
- long long loc_value;
-
+ int has_value = 0;
+ long long loc_value;
+
str = allocAndCopy(getNameAttribute(in));
has_value = getValueAttribute(in, &loc_value);
-
+
sequence_push(&(t->labels),str);
if(has_value) value = loc_value;
getForwardslash(in);
getRAnglebracket(in);
-
+
//read description if any. May be NULL.
str = allocAndCopy(getDescription(in));
- sequence_push(&(t->labels_description),str);
-
+ sequence_push(&(t->labels_description),str);
+
//next label definition
getLAnglebracket(in);
- token = getToken(in); //"label" or "/"
+ token = getToken(in); //"label" or "/"
}
if(strcmp("/",token))in->error(in, "not a valid enum definition");
token = getName(in);
getTypeAttributes(in, t, unnamed_types, named_types);
if(t->size == 0) in->error(in, "int has empty size");
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"uint_fixed") == 0) {
t->type = UINT_FIXED;
getTypeAttributes(in, t, unnamed_types, named_types);
if(t->size == 0) in->error(in, "uint has empty size");
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"char") == 0) {
t->type = CHAR;
getTypeAttributes(in, t, unnamed_types, named_types);
- t->size = 1;
+ t->size = 1;
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"uchar") == 0) {
t->type = UCHAR;
getTypeAttributes(in, t, unnamed_types, named_types);
- t->size = 1;
+ t->size = 1;
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"short") == 0) {
t->type = SHORT;
getTypeAttributes(in, t, unnamed_types, named_types);
- t->size = 2;
+ t->size = 2;
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"ushort") == 0) {
t->type = USHORT;
getTypeAttributes(in, t, unnamed_types, named_types);
- t->size = 2;
+ t->size = 2;
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"int") == 0) {
t->type = INT;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"uint") == 0) {
t->type = UINT;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"pointer") == 0) {
t->type = POINTER;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"long") == 0) {
t->type = LONG;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"ulong") == 0) {
t->type = ULONG;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"size_t") == 0) {
t->type = SIZE_T;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"ssize_t") == 0) {
t->type = SSIZE_T;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"off_t") == 0) {
t->type = OFF_T;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"float") == 0) {
t->type = FLOAT;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"string") == 0) {
t->type = STRING;
getTypeAttributes(in, t, unnamed_types, named_types);
getForwardslash(in);
- getRAnglebracket(in);
+ getRAnglebracket(in);
}
else if(strcmp(token,"typeref") == 0){
// Must be a named type
- free(t);
- sequence_pop(unnamed_types);
- token = getNameAttribute(in);
- t = find_named_type(token, named_types);
- if(t == NULL) in->error(in,"Named referred to must be pre-declared.");
- getForwardslash(in); //<typeref name=type_name/>
- getRAnglebracket(in);
- return t;
+ free(t);
+ sequence_pop(unnamed_types);
+ token = getNameAttribute(in);
+ t = find_named_type(token, named_types);
+ if(t == NULL) in->error(in,"Named referred to must be pre-declared.");
+ getForwardslash(in); //<typeref name=type_name/>
+ getRAnglebracket(in);
+ return t;
}else in->error(in,"not a valid type");
return t;
-}
+}
/*****************************************************************************
*Function name
- * find_named_type : find a named type from hash table
- *Input params
- * name : type name
+ * find_named_type : find a named type from hash table
+ *Input params
+ * name : type name
* named_types : array of named types
- *Return values
- * type_descriptor * : a type descriptor
+ *Return values
+ * type_descriptor * : a type descriptor
*****************************************************************************/
type_descriptor_t * find_named_type(char *name, table_t * named_types)
-{
+{
type_descriptor_t *t;
t = (type_descriptor_t*)table_find(named_types,name);
{
type_descriptor_t *t;
- t = (type_descriptor_t *)memAlloc(sizeof(type_descriptor_t));
- t->type_name = allocAndCopy(name);
- t->type = NONE;
- t->fmt = NULL;
- table_insert(named_types,t->type_name,t);
- // table_insert(named_types,allocAndCopy(name),t);
- return t;
+ t = (type_descriptor_t *)memAlloc(sizeof(type_descriptor_t));
+ t->type_name = allocAndCopy(name);
+ t->type = NONE;
+ t->fmt = NULL;
+ table_insert(named_types,t->type_name,t);
+ // table_insert(named_types,allocAndCopy(name),t);
+ return t;
}
/*****************************************************************************
*Function name
- * parseTypeDefinition : get type information from type definition
- *Input params
+ * parseTypeDefinition : get type information from type definition
+ *Input params
* fac : facility
- * in : input file handle
+ * in : input file handle
* unnamed_types : array of unamed types
* named_types : array of named types
*****************************************************************************/
void parseTypeDefinition(facility_t *fac, parse_file_t * in, sequence_t * unnamed_types,
- table_t * named_types)
+ table_t * named_types)
{
char *token;
type_descriptor_t *t;
//MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
ungetToken(in);
parseType(fac, in,t, unnamed_types, named_types);
-
+
//</type>
getLAnglebracket(in);
getForwardslash(in);
token = getName(in);
- if(strcmp("type",token))in->error(in,"not a valid type definition");
+ if(strcmp("type",token))in->error(in,"not a valid type definition");
getRAnglebracket(in); //</type>
}
*
**************************************************************************/
-char *getName(parse_file_t * in)
+char *getName(parse_file_t * in)
{
char *token;
token = getToken(in);
// Optional descriptions
- // if(in->type != NAME) in->error(in,"Name token was expected");
+ // if(in->type != NAME) in->error(in,"Name token was expected");
return token;
}
-int getNumber(parse_file_t * in)
+int getNumber(parse_file_t * in)
{
char *token;
return atoi(token);
}
-char *getForwardslash(parse_file_t * in)
+char *getForwardslash(parse_file_t * in)
{
char *token;
token = getToken(in);
//if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected");
- /* Mathieu : final / is optional now. */
+ /* Mathieu : final / is optional now. */
if(in->type != FORWARDSLASH) ungetToken(in);
return token;
}
-char *getLAnglebracket(parse_file_t * in)
+char *getLAnglebracket(parse_file_t * in)
{
char *token;
return token;
}
-char *getRAnglebracket(parse_file_t * in)
+char *getRAnglebracket(parse_file_t * in)
{
char *token;
return token;
}
-char *getQuotedString(parse_file_t * in)
+char *getQuotedString(parse_file_t * in)
{
char *token;
ungetc(car,in->fp);
return car;
}
- }
+ }
return EOF;
}
while((car = getc(fp)) != EOF) {
if(car == '/') {
- car1 = getc(fp);
+ car1 = getc(fp);
if(car1 == '*') skipComment(in);
else if(car1 == '/') skipEOL(in);
- else {
+ else {
car1 = ungetc(car1,fp);
break;
}
escaped = 0;
while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) {
if(car == '\\' && escaped == 0) {
- in->buffer[pos] = car;
- pos++;
+ in->buffer[pos] = car;
+ pos++;
escaped = 1;
continue;
}
in->buffer[pos] = car;
pos++;
}
- if(car == EOF) ungetc(car,fp);
+ if(car == EOF) ungetc(car,fp);
if(pos == BUFFER_SIZE) in->error(in, "number token too large");
in->type = NUMBER;
}
in->buffer[pos] = car;
pos++;
}
- if(car == EOF) ungetc(car,fp);
+ if(car == EOF) ungetc(car,fp);
if(pos == BUFFER_SIZE) in->error(in, "name token too large");
in->type = NAME;
} else if(car == '?') {
- in->buffer[0] = car;
- pos++;
- }
+ in->buffer[0] = car;
+ pos++;
+ }
else in->error(in, "invalid character, unrecognized token");
}
in->buffer[pos] = 0;
if(t->type == NONE){
sprintf(str,"named type '%s' has no definition",
(char*)named_types->keys.array[pos]);
- error_callback(NULL,str);
+ error_callback(NULL,str);
}
}
}
for(pos = 0; pos < events->position; pos++){
ev = (event_t *)(events->array[pos]);
crc = partial_crc32(ev->name, crc);
- for(i = 0; i < ev->fields.position; i++) {
- field_t *f = (field_t*)ev->fields.array[i];
+ for(i = 0; i < ev->fields.position; i++) {
+ field_t *f = (field_t*)ev->fields.array[i];
crc = partial_crc32(f->name, crc);
crc = getTypeChecksum(crc, f->type);
- }
+ }
}
*checksum = crc;
}
*Input Params
* crc : checksum generated so far
* type : type descriptor containing type info
- *Return value
- * unsigned long : checksum
+ *Return value
+ * unsigned long : checksum
*****************************************************************************/
unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor_t * type)
break;
case POINTER:
str = allocAndCopy("void *");
- flag = 1;
+ flag = 1;
break;
case CHAR:
str = allocAndCopy("signed char");
- flag = 1;
+ flag = 1;
break;
case UCHAR:
str = allocAndCopy("unsigned char");
- flag = 1;
+ flag = 1;
break;
case SHORT:
str = allocAndCopy("short");
- flag = 1;
+ flag = 1;
break;
case USHORT:
str = allocAndCopy("unsigned short");
- flag = 1;
+ flag = 1;
break;
case INT:
str = allocAndCopy("int");
- flag = 1;
+ flag = 1;
break;
case UINT:
str = allocAndCopy("uint");
- flag = 1;
+ flag = 1;
break;
case LONG:
str = allocAndCopy("long");
- flag = 1;
+ flag = 1;
break;
case ULONG:
str = allocAndCopy("unsigned long");
- flag = 1;
+ flag = 1;
break;
case SIZE_T:
str = allocAndCopy("size_t");
- flag = 1;
+ flag = 1;
break;
case SSIZE_T:
str = allocAndCopy("ssize_t");
- flag = 1;
+ flag = 1;
break;
case OFF_T:
str = allocAndCopy("off_t");
- flag = 1;
+ flag = 1;
break;
case FLOAT:
str = floatOutputTypes[getSizeindex(type->size)];
if(flag) free(str);
if(type->fmt) crc = partial_crc32(type->fmt,crc);
-
+
if(type->type == ARRAY){
crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type);
} else if(type->type ==SEQUENCE) {
crc = getTypeChecksum(crc,((field_t*)type->fields.array[0])->type);
crc = getTypeChecksum(crc,((field_t*)type->fields.array[1])->type);
- } else if(type->type == STRUCT || type->type == UNION){
+ } else if(type->type == STRUCT || type->type == UNION){
for(pos =0; pos < type->fields.position; pos++){
fld = (field_t *) type->fields.array[pos];
crc = partial_crc32(fld->name,crc);
crc = getTypeChecksum(crc, fld->type);
- }
+ }
}else if(type->type == ENUM){
for(pos = 0; pos < type->labels.position; pos++)
crc = partial_crc32((char*)type->labels.array[pos],crc);
}
}
-void freeTypes(sequence_t *t)
+void freeTypes(sequence_t *t)
{
int pos;
type_descriptor_t *tp;
}
}
-void freeEvents(sequence_t *t)
+void freeEvents(sequence_t *t)
{
int pos;
event_t *ev;
ev = (event_t *) t->array[pos];
free(ev->name);
free(ev->description);
- sequence_dispose(&ev->fields);
+ sequence_dispose(&ev->fields);
free(ev);
}
/* Extensible array */
-void sequence_init(sequence_t *t)
+void sequence_init(sequence_t *t)
{
t->size = 10;
t->position = 0;
t->array = (void **)memAlloc(t->size * sizeof(void *));
}
-void sequence_dispose(sequence_t *t)
+void sequence_dispose(sequence_t *t)
{
t->size = 0;
free(t->array);
t->array = NULL;
}
-void sequence_push(sequence_t *t, void *elem)
+void sequence_push(sequence_t *t, void *elem)
{
void **tmp;
t->position++;
}
-void *sequence_pop(sequence_t *t)
+void *sequence_pop(sequence_t *t)
{
- if(t->position == 0) printf("Error : trying to pop an empty sequence");
+ if(t->position == 0) printf("Error : trying to pop an empty sequence");
return t->array[--t->position];
}
/* Hash table API, implementation is just linear search for now */
-void table_init(table_t *t)
+void table_init(table_t *t)
{
sequence_init(&(t->keys));
sequence_init(&(t->values));
}
-void table_dispose(table_t *t)
+void table_dispose(table_t *t)
{
sequence_dispose(&(t->keys));
sequence_dispose(&(t->values));
}
-void table_insert(table_t *t, char *key, void *value)
+void table_insert(table_t *t, char *key, void *value)
{
sequence_push(&(t->keys),key);
sequence_push(&(t->values),value);
}
-void *table_find(table_t *t, char *key)
+void *table_find(table_t *t, char *key)
{
int pos;
for(pos = 0 ; pos < t->keys.position; pos++) {
/* Concatenate strings */
-char *appendString(char *s, char *suffix)
+char *appendString(char *s, char *suffix)
{
char *tmp;
if(suffix == NULL) return s;
tmp = (char *)memAlloc(strlen(s) + strlen(suffix) + 1);
strcpy(tmp,s);
- strcat(tmp,suffix);
+ strcat(tmp,suffix);
return tmp;
}