fix small comment typo
[lttv.git] / ltt / branches / poly / ltt / parser.c
index 492f766724f9aaa38c5eb3be12194af3d6bbcb95..2a7dd1e532311351baee1c0ed8577c61444283d2 100644 (file)
@@ -1,25 +1,24 @@
 /*
- * parser.c: Generate helper declarations and functions to trace events
- * from an event description file.
- *
- *    Copyright (C) 2005, Mathieu Desnoyers
- *    Copyright (C) 2002, Xianxiu Yang
- *    Copyright (C) 2002, Michel Dagenais 
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License Version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
- */
+
+parser.c: Generate helper declarations and functions to trace events
+  from an event description file.
+
+           Copyright (C) 2005, Mathieu Desnoyers
+      Copyright (C) 2002, Xianxiu Yang
+      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.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+*/
 
 /* This program reads the ".xml" event definitions input files 
    and constructs structure for each event.
@@ -97,12 +96,31 @@ int getSizeindex(unsigned int value)
 
 unsigned long long int getSize(parse_file_t *in)
 {
-  char *token;
+  char *token, *token2;
+  unsigned long long int ret;
 
   token = getToken(in);
-  if(in->type == NUMBER) {
-               return strtoull(token, NULL, 0);
+
+
+  if(in->type == QUOTEDSTRING) {
+    in->type = NUMBER;
+    token2 = token;
+    do {
+       if (!isdigit(*token2)) {
+          in->type = QUOTEDSTRING;
+          break;
+                               }
+    } while (*(++token2) != '\0');
   }
+
+  if(in->type == NUMBER) {
+               ret = strtoull(token, NULL, 0);
+  } else {
+               goto error;
+       }
+               
+       return ret;
+error:
   in->error(in,"incorrect size specification");
   return -1;
 }
@@ -177,13 +195,15 @@ char *allocAndCopy(char *str)
  **************************************************************************/
 
 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->fmt = NULL;
   t->size = 0;
   t->custom_write = 0;
+  t->network = 0;
   
   while(1) {
     token = getToken(in); 
@@ -206,6 +226,24 @@ void getTypeAttributes(parse_file_t *in, type_descriptor_t *t,
       t->size = getSize(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;
+       }
+    } 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;
+       }
     }
   }
 }
@@ -225,11 +263,16 @@ void getTypeAttributes(parse_file_t *in, type_descriptor_t *t,
 void getEventAttributes(parse_file_t *in, event_t *ev)
 {
   char * token;
-  char car;
+  int car;
   
   ev->name = NULL;
   ev->per_trace = 0;
   ev->per_tracefile = 0;
+  ev->param_buffer = 0;
+  ev->no_instrument_function = 0;
+  ev->high_priority = 0;
+  ev->force = 0;
+  ev->compact_data = 0;
 
   while(1) {
     token = getToken(in); 
@@ -244,12 +287,36 @@ void getEventAttributes(parse_file_t *in, event_t *ev)
       if(car == EOF) in->error(in,"name was expected");
       else if(car == '\"') ev->name = allocAndCopy(getQuotedString(in));
       else ev->name = allocAndCopy(getName(in));
-    } else if(!strcmp("per_trace", token)) {
-      ev->per_trace = 1;
-    } else if(!strcmp("per_tracefile", token)) {
-      ev->per_tracefile = 1;
+    } else if(!strcmp("scope", token)) {
+      getEqual(in);
+      car = seekNextChar(in);
+      if(car == EOF) in->error(in,"scope was expected");
+      else if(car == '\"') token = getQuotedString(in);
+      else token = getName(in);
+      if(!strcmp(token, "trace")) ev->per_trace = 1;
+      else if(!strcmp(token, "tracefile")) ev->per_tracefile = 1;
+    } else if(!strcmp("param", token)) {
+      getEqual(in);
+      car = seekNextChar(in);
+      if(car == EOF) in->error(in,"parameter type was expected");
+      else if(car == '\"') token = getQuotedString(in);
+      else token = getName(in);
+      if(!strcmp(token, "buffer")) ev->param_buffer = 1;
+    } else if(!strcmp("attribute", token)) {
+      getEqual(in);
+      car = seekNextChar(in);
+      if(car == EOF) in->error(in,"attribute was expected");
+      else if(car == '\"') token = getQuotedString(in);
+      else token = getName(in);
+      if(!strcmp(token, "no_instrument_function"))
+        ev->no_instrument_function = 1;
+      else if(!strcmp(token, "high_priority"))
+        ev->high_priority = 1;
+      else if(!strcmp(token, "force"))
+        ev->force = 1;
+      else if(!strcmp(token, "compact_data"))
+        ev->compact_data = 1;
     }
-
   }
 }
 
@@ -268,10 +335,12 @@ void getEventAttributes(parse_file_t *in, event_t *ev)
 void getFacilityAttributes(parse_file_t *in, facility_t *fac)
 {
   char * token;
-  char car;
+  int car;
   
   fac->name = NULL;
   fac->arch = NULL;
+  fac->align = 1;
+  fac->user = 0;
 
   while(1) {
     token = getToken(in); 
@@ -286,12 +355,18 @@ void getFacilityAttributes(parse_file_t *in, facility_t *fac)
       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;
     } else if(!strcmp("arch", token)) {
       getEqual(in);
       car = seekNextChar(in);
-      if(car == '\"') fac->name = allocAndCopy(getQuotedString(in));
-                       else fac->arch = allocAndCopy(getName(in));
-               }
+      if(car == '\"') fac->arch = allocAndCopy(getQuotedString(in));
+      else fac->arch = allocAndCopy(getName(in));
+    } else if(!strcmp("align", token)) {
+      getEqual(in);
+      fac->align = getSize(in);
+    }
+
   }
 }
 
@@ -310,7 +385,7 @@ void getFacilityAttributes(parse_file_t *in, facility_t *fac)
 void getFieldAttributes(parse_file_t *in, field_t *f)
 {
   char * token;
-  char car;
+  int car;
 
   f->name = NULL;
 
@@ -328,59 +403,78 @@ void getFieldAttributes(parse_file_t *in, field_t *f)
       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;
-  char car;
+  int car;
   
   while(1) {
     token = getToken(in); 
-    if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
-      ungetToken(in);
-      break;
-    }
-
     if(!strcmp("name",token)) {
       getEqual(in);
       car = seekNextChar(in);
       if(car == EOF) in->error(in,"name was expected");
       else if(car == '\"') name = allocAndCopy(getQuotedString(in));
       else name = allocAndCopy(getName(in));
+    } else {
+      ungetToken(in);
+      break;
     }
+
   }
   if(name == NULL) in->error(in, "Name was expected");
   return name;
-  
 }
 
 
 
-//for <label name=label_name value=n format="..."/>, value is an option
-char * getValueStrAttribute(parse_file_t *in)
+//for <label name=label_name value=n format="...">, value is an option
+//Return value : 0 : no value,   1 : has a value
+int getValueAttribute(parse_file_t *in, long long *value)
 {
-  char * token;
+  char * token, *token2;
 
-  token = getToken(in); 
-  if(strcmp("/",token) == 0){
+  token = getToken(in);
+       
+  if(strcmp("/",token) == 0 || strcmp(">", token) == 0){
     ungetToken(in);
-    return NULL;
+    return 0;
   }
-  
   if(strcmp("value",token))in->error(in,"value was expected");
+       
   getEqual(in);
   token = getToken(in);
-  if(in->type != NUMBER) in->error(in,"number was expected");
-  return token;  
+
+  if(in->type == QUOTEDSTRING) {
+    in->type = NUMBER;
+    token2 = token;
+    do {
+       if (!isdigit(*token2)) {
+          in->type = QUOTEDSTRING;
+          break;
+                        }
+    } while (*(++token2) != '\0');
+  }
+
+  if(in->type == NUMBER)
+               *value = strtoll(token, NULL, 0);
+       else
+               goto error;
+  return 1;
+error:
+  in->error(in,"incorrect size specification");
+  return 0;
 }
 
 char * getDescription(parse_file_t *in)
 {
   long int pos;
-  char * token, car, *str;
+  char * token, *str;
+  int car;
 
   pos = ftell(in->fp);
 
@@ -447,9 +541,9 @@ void parseFacility(parse_file_t *in, facility_t * fac)
     if(strcmp("event",token) == 0){
       ev = (event_t*) memAlloc(sizeof(event_t));
       sequence_push(&(fac->events),ev);
-      parseEvent(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(in, &(fac->unnamed_types), &(fac->named_types));
+      parseTypeDefinition(fac, in, &(fac->unnamed_types), &(fac->named_types));
     }else if(in->type == FORWARDSLASH){
       break;
     }else in->error(in,"event or type token expected\n");
@@ -463,7 +557,8 @@ void parseFacility(parse_file_t *in, facility_t * fac)
 /*****************************************************************************
  *Function name
  *    parseEvent    : generate event from event definition 
- *Input params 
+ *Input params
+ *    fac           : facility holding the event
  *    in            : input file handle          
  *    ev            : new event                              
  *    unnamed_types : array of unamed types
@@ -472,12 +567,13 @@ void parseFacility(parse_file_t *in, facility_t * fac)
  *    ev            : new event (parameters are passed to it)   
  ****************************************************************************/
 
-void parseEvent(parse_file_t *in, event_t * ev, sequence_t * unnamed_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;
 
+  ev->fac = fac;
        sequence_init(&(ev->fields));
   //<event name=eventtype_name>
   getEventAttributes(in, ev);
@@ -505,7 +601,7 @@ void parseEvent(parse_file_t *in, event_t * ev, sequence_t * unnamed_types,
                        if(strcmp("field",token))in->error(in,"expecting a field");
                        f = (field_t *)memAlloc(sizeof(field_t));
                        sequence_push(&(ev->fields),f);
-                       parseFields(in, f, unnamed_types, named_types, 1);
+                       parseFields(fac, in, f, unnamed_types, named_types, 1);
                        break;
                default:
                        in->error(in, "expecting </event> or <field >");
@@ -538,6 +634,7 @@ void parseEvent(parse_file_t *in, event_t * ev, sequence_t * unnamed_types,
  *Function name
  *    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
@@ -545,12 +642,13 @@ void parseEvent(parse_file_t *in, event_t * ev, sequence_t * unnamed_types,
  *    tag                                              : is field surrounded by a <field> </field> tag ?
  ****************************************************************************/
 
-void parseFields(parse_file_t *in, field_t *f,
+void parseFields(facility_t *fac, parse_file_t *in, field_t *f,
     sequence_t * unnamed_types,
                table_t * named_types,
                int tag) 
 {
   char * token;
+  f->fac = fac;
        if(tag) {
                //<field name=field_name> <description> <type> </field>
                getFieldAttributes(in, f);
@@ -558,11 +656,13 @@ void parseFields(parse_file_t *in, field_t *f,
                getRAnglebracket(in);
 
                f->description = getDescription(in);
+       } else {
+               f->description = NULL;
        }
 
   //<int size=...>
   getLAnglebracket(in);
-  f->type = parseType(in,NULL, unnamed_types, named_types);
+  f->type = parseType(fac, in,NULL, unnamed_types, named_types);
 
        if(tag) {
                getLAnglebracket(in);
@@ -586,6 +686,7 @@ void parseFields(parse_file_t *in, field_t *f,
  *                     type name:
  *                        type(name,type)
  *Input params 
+ *    fac              : facility
  *    in               : input file handle
  *    inType           : a type descriptor          
  *    unnamed_types    : array of unamed types
@@ -594,7 +695,7 @@ void parseFields(parse_file_t *in, field_t *f,
  *    type_descriptor* : a type descriptor             
  ****************************************************************************/
 
-type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType, 
+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;
@@ -609,6 +710,7 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
     sequence_push(unnamed_types,t);
   }
   else t = inType;
+  t->fac = fac;
 
   token = getName(in);
 
@@ -623,7 +725,7 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
                        f = (field_t *)memAlloc(sizeof(field_t));
                        sequence_push(&(t->fields),f);
 
-      parseFields(in, f, unnamed_types, named_types, 1);
+      parseFields(fac, in, f, unnamed_types, named_types, 1);
       
       //next field
       getLAnglebracket(in);
@@ -646,7 +748,7 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
     while(strcmp("field",token) == 0){
                        f = (field_t *)memAlloc(sizeof(field_t));
                        sequence_push(&(t->fields),f);
-      parseFields(in, f, unnamed_types, named_types, 1);
+      parseFields(fac, in, f, unnamed_types, named_types, 1);
       
       //next field
       getLAnglebracket(in);
@@ -668,9 +770,11 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
 
     //getLAnglebracket(in); //<subtype> 
                /* subfield */
-               f = (field_t *)memAlloc(sizeof(field_t));
-               sequence_push(&(t->fields),f);
-    parseFields(in, f, unnamed_types, named_types, 0);
+    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> 
     //t->nested_type = parseType(in, NULL, unnamed_types, named_types);
@@ -690,15 +794,17 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
 
     //getLAnglebracket(in); //<sequence size type> 
                /* subfield */
-               f = (field_t *)memAlloc(sizeof(field_t));
-               sequence_push(&(t->fields),f);
-    parseFields(in, f, unnamed_types, named_types, 0);
+    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 */
-               f = (field_t *)memAlloc(sizeof(field_t));
-               sequence_push(&(t->fields),f);
-    parseFields(in, f, unnamed_types, named_types, 0);
+    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> 
     //t->length_type = parseType(in, NULL, unnamed_types, named_types);
@@ -730,7 +836,7 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
   }
   else if(strcmp(token,"enum") == 0) {
     char * str;
-    int value = -1;
+    long long value = -1;
 
     t->type = ENUM;
     sequence_init(&(t->labels));
@@ -750,13 +856,15 @@ type_descriptor_t *parseType(parse_file_t *in, type_descriptor_t *inType,
     token = getToken(in); //"label" or "/"
     while(strcmp("label",token) == 0){
       int *label_value = malloc(sizeof(int));
+                       int has_value = 0;
+                       long long loc_value;
       
       str   = allocAndCopy(getNameAttribute(in));
-      token = getValueStrAttribute(in);
+      has_value = getValueAttribute(in, &loc_value);
       
-       sequence_push(&(t->labels),str);
+      sequence_push(&(t->labels),str);
 
-      if(token) value = strtol(token, NULL, 0);
+      if(has_value) value = loc_value;
       else value++;
 
       *label_value = value;
@@ -910,7 +1018,7 @@ type_descriptor_t * find_named_type(char *name, table_t * named_types)
 { 
   type_descriptor_t *t;
 
-  t = table_find(named_types,name);
+  t = (type_descriptor_t*)table_find(named_types,name);
 
   return t;
 }
@@ -932,12 +1040,13 @@ type_descriptor_t * create_named_type(char *name, table_t * named_types)
  *Function name
  *    parseTypeDefinition : get type information from type definition 
  *Input params 
+ *    fac                 : facility
  *    in                  : input file handle          
  *    unnamed_types       : array of unamed types
  *    named_types         : array of named types
  *****************************************************************************/
 
-void parseTypeDefinition(parse_file_t * in, sequence_t * unnamed_types,
+void parseTypeDefinition(facility_t *fac, parse_file_t * in, sequence_t * unnamed_types,
                         table_t * named_types)
 {
   char *token;
@@ -953,7 +1062,7 @@ void parseTypeDefinition(parse_file_t * in, sequence_t * unnamed_types,
   token = getName(in);
   //MD ??if(strcmp("struct",token))in->error(in,"not a valid type definition");
   ungetToken(in);
-  parseType(in,t, unnamed_types, named_types);
+  parseType(fac, in,t, unnamed_types, named_types);
   
   //</type>
   getLAnglebracket(in);
@@ -1044,9 +1153,9 @@ char * getEqual(parse_file_t *in)
   return token;
 }
 
-char seekNextChar(parse_file_t *in)
+int seekNextChar(parse_file_t *in)
 {
-  char car;
+  int car;
   while((car = getc(in->fp)) != EOF) {
     if(!isspace(car)){
       ungetc(car,in->fp);
@@ -1079,7 +1188,7 @@ void ungetToken(parse_file_t * in)
 char *getToken(parse_file_t * in)
 {
   FILE *fp = in->fp;
-  char car, car1;
+  int car, car1;
   int pos = 0, escaped;
 
   if(in->unget == 1) {
@@ -1131,8 +1240,8 @@ char *getToken(parse_file_t * in)
       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;
         }
@@ -1161,10 +1270,10 @@ char *getToken(parse_file_t * in)
           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;
-      }    
+      }
       else if(isalnum(car) || car == '_' || car == '-') {
         in->buffer[0] = car;
         pos = 1;
@@ -1176,10 +1285,13 @@ char *getToken(parse_file_t * in)
           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++;
+                       }
       else in->error(in, "invalid character, unrecognized token");
   }
   in->buffer[pos] = 0;
@@ -1188,7 +1300,7 @@ char *getToken(parse_file_t * in)
 
 void skipComment(parse_file_t * in)
 {
-  char car;
+  int car;
   while((car = getc(in->fp)) != EOF) {
     if(car == '\n') in->lineno++;
     else if(car == '*') {
@@ -1203,7 +1315,7 @@ void skipComment(parse_file_t * in)
 
 void skipEOL(parse_file_t * in)
 {
-  char car;
+  int car;
   while((car = getc(in->fp)) != EOF) {
     if(car == '\n') {
       ungetc(car,in->fp);
@@ -1498,7 +1610,8 @@ void sequence_push(sequence_t *t, void *elem)
 
 void *sequence_pop(sequence_t *t) 
 {
-  return t->array[t->position--];
+       if(t->position == 0) printf("Error : trying to pop an empty sequence");
+  return t->array[--t->position];
 }
 
 
This page took 0.031318 seconds and 4 git commands to generate.