git-svn-id: http://ltt.polymtl.ca/svn@421 04897980-b3bd-0310-b5e0-8ef037075253
authoryangxx <yangxx@04897980-b3bd-0310-b5e0-8ef037075253>
Tue, 27 Jan 2004 22:19:25 +0000 (22:19 +0000)
committeryangxx <yangxx@04897980-b3bd-0310-b5e0-8ef037075253>
Tue, 27 Jan 2004 22:19:25 +0000 (22:19 +0000)
genevent/Makefile [new file with mode: 0644]
genevent/README [new file with mode: 0644]
genevent/core.xml [new file with mode: 0644]
genevent/crc32.tab [new file with mode: 0644]
genevent/genevent [new file with mode: 0755]
genevent/genevent.c [new file with mode: 0644]
genevent/genevent.h [new file with mode: 0644]
genevent/parser.c [new file with mode: 0644]
genevent/parser.h [new file with mode: 0644]

diff --git a/genevent/Makefile b/genevent/Makefile
new file mode 100644 (file)
index 0000000..7a0d8dc
--- /dev/null
@@ -0,0 +1,21 @@
+SHELL = /bin/sh
+
+CC = gcc
+
+CFLAGS = -g -o
+
+all: genevent
+
+genevent:genevent.o parser.o 
+       $(CC) $(CFLAGS) $@ genevent.o parser.o
+
+genevent.o: genevent.c genevent.h parser.h
+       $(CC) -g -c genevent.c
+
+parser.o: parser.c parser.h
+       $(CC) -g -c parser.c
+
+clean:
+       rm -rf *.o *~ *#
+
+
diff --git a/genevent/README b/genevent/README
new file mode 100644 (file)
index 0000000..9b34bf4
--- /dev/null
@@ -0,0 +1,14 @@
+The 'genevent' program parses event descriptions and generates 
+the inline functions to record events in the kernel.
+
+Right now, the program can only parse simple structure, if a 
+structure has a nested structure, it will not work.
+
+There are several files in the directory:
+ genevent.c, genevent.h, crc32.tab, parser.c and parser.h
+
+In fact, crc32.tab, parser.c and parser.h are the same files as  
+those in LTT library.
+
+'core.xml' is an example event description file.
+
diff --git a/genevent/core.xml b/genevent/core.xml
new file mode 100644 (file)
index 0000000..b2e6e00
--- /dev/null
@@ -0,0 +1,246 @@
+<facility name=core>
+  <description>The core facility contains the basic events</description>
+
+  <event name=facility_load>
+    <description>Facility used in the trace</description>
+    <struct>
+      <field name="name"><string/></field>
+      <field name="checksum"><uint size=4/></field>
+      <field name="base_code"><uint size=4/></field>
+    </struct>
+  </event>
+
+  <event name=syscall_entry>
+    <description>Entry in a given system call</description>
+    <struct>
+      <field name="syscall_id"> <description>Syscall entry number in entry.S</description> <uint size=1/> </field>
+      <field name="address"> <description>Address from which call was made</description> <uint size=4/> </field>
+     </struct>
+  </event>
+
+  <event name=syscall_exit>
+    <description>Exit from a given system call</description>
+  </event>
+
+  <event name=trap_entry>
+    <description>Entry in a trap</description>
+    <struct>
+      <field name="trap_id"> <description>Trap number</description> <uint size=2/> </field>
+      <field name="address"> <description>Address where trap occured</description> <uint size=4/> </field>
+     </struct>
+  </event>
+
+  <event name=trap_exit>
+    <description>Exit from a trap</description>
+  </event>
+
+  <event name=irq_entry>
+    <description>Entry in an irq</description>
+    <struct>
+      <field name="irq_id"> <description>IRQ number</description> <uint size=1/> </field>
+      <field name="kernel"> <description>Are we executing kernel code</description> <uint size=1/> </field>
+     </struct>
+  </event>
+
+  <event name=irq_exit>
+    <description>Exit from an IRQ</description>
+  </event>
+
+  <event name=schedchange>
+    <description>Scheduling change</description>
+    <struct>
+      <field name="out"> <description>Outgoing process</description> <uint size=4/> </field>
+      <field name="in"> <description>Incoming process</description> <uint size=4/> </field>
+      <field name="out_state"> <description>Outgoing process' state</description> <uint size=4/> </field>
+     </struct>
+  </event>
+
+  <event name=kernel_timer>
+    <description>The kernel timer routine has been called</description>
+  </event>
+
+  <event name=soft_irq>
+    <description>Hit key part of soft-irq management</description>
+    <struct>
+      <field name="event_sub_id"> <description>Soft-irq event Id</description> 
+        <enum size=1>
+          <label name=TRACE_EV_SOFT_IRQ_BOTTOM_HALF value=1/>
+          <label name=TRACE_EV_SOFT_IRQ_SOFT_IRQ/>
+          <label name=TRACE_EV_SOFT_IRQ_TASKLET_ACTION/>
+          <label name=TRACE_EV_SOFT_IRQ_TASKLET_HI_ACTION/>
+        </enum> 
+      </field>
+       
+      <field name="event_data"> <description>Data associated with event</description> <uint size=4/> </field>
+     </struct>
+  </event>
+
+  <event name=process>
+    <description>Hit key part of process management</description>
+    <struct>
+      <field name="event_sub_id"> <description>Process event ID</description> 
+        <enum size=1>
+          <label name=TRACE_EV_PROCESS_KTHREAD value=1/>
+          <label name=TRACE_EV_PROCESS_FORK/>
+          <label name=TRACE_EV_PROCESS_EXIT/>
+          <label name=TRACE_EV_PROCESS_WAIT/>
+          <label name=TRACE_EV_PROCESS_SIGNAL/>
+          <label name=TRACE_EV_PROCESS_WAKEUP/>
+        </enum> 
+      </field>
+       
+      <field name="event_data1"> <description>Data associated with event</description> <uint size=4/> </field>
+      <field name="event_data2"> <description>Data associated with event</description> <uint size=4/> </field>
+     </struct>
+  </event>
+
+  <event name=file_system>
+    <description>Hit key part of file system</description>
+    <struct>
+      <field name="event_sub_id"> <description>File system event ID</description> 
+        <enum size=1>
+          <label name=TRACE_EV_FILE_SYSTEM_BUF_WAIT_START value=1/>
+          <label name=TRACE_EV_FILE_SYSTEM_BUF_WAIT_END/>
+          <label name=TRACE_EV_FILE_SYSTEM_EXEC/>
+          <label name=TRACE_EV_FILE_SYSTEM_OPEN/>
+          <label name=TRACE_EV_FILE_SYSTEM_CLOSE/>
+          <label name=TRACE_EV_FILE_SYSTEM_READ/>
+          <label name=TRACE_EV_FILE_SYSTEM_WRITE/>
+          <label name=TRACE_EV_FILE_SYSTEM_SEEK/>
+          <label name=TRACE_EV_FILE_SYSTEM_IOCTL/>
+          <label name=TRACE_EV_FILE_SYSTEM_SELECT/>
+          <label name=TRACE_EV_FILE_SYSTEM_POLL/>
+        </enum> 
+      </field>
+       
+      <field name="event_data1"> <description>Event data </description> <uint size=4/> </field>
+      <field name="event_data2"> <description>Event data 2</description> <uint size=4/> </field>
+      <field name="file_name"> <description>Name of file operated on </description> <string/> </field>
+     </struct>
+  </event>
+
+  <event name=timer>
+    <description>Hit key part of timer management</description>
+    <struct>
+      <field name="event_sub_id"> <description>Timer event ID</description> 
+        <enum size=1>
+          <label name=TRACE_EV_TIMER_EXPIRED value=1/>
+          <label name=TRACE_EV_TIMER_SETITIMER/>
+          <label name=TRACE_EV_TIMER_SETTIMEOUT/>
+        </enum> 
+      </field>
+       
+      <field name="event_sdata"> <description>Short data</description> <uint size=1/> </field>
+      <field name="event_data1"> <description>Data associated with event</description> <uint size=4/> </field>
+      <field name="event_data2"> <description>Data associated with event</description> <uint size=4/> </field>
+     </struct>
+  </event>
+
+  <event name=memory>
+    <description>Hit key part of memory management</description>
+    <struct>
+      <field name="event_sub_id"> <description>Memory event ID</description> 
+        <enum size=1>
+          <label name=TRACE_EV_MEMORY_PAGE_ALLOC value=1/>
+          <label name=TRACE_EV_MEMORY_PAGE_FREE/>
+          <label name=TRACE_EV_MEMORY_SWAP_IN/>
+          <label name=TRACE_EV_MEMORY_SWAP_OUT/>
+          <label name=TRACE_EV_MEMORY_PAGE_WAIT_START/>
+          <label name=TRACE_EV_MEMORY_PAGE_WAIT_END/>
+        </enum> 
+      </field>
+       
+      <field name="event_data"> <description>Data associated with event</description> <uint size=4/> </field>
+     </struct>
+  </event>
+
+  <event name=socket>
+    <description>Hit key part of socket communication</description>
+    <struct>
+      <field name="event_sub_id"> <description>Memory event ID</description> 
+        <enum size=1>
+          <label name=TRACE_EV_SOCKET_CALL value=1/>
+          <label name=TRACE_EV_SOCKET_CREATE/>
+          <label name=TRACE_EV_SOCKET_SEND/>
+          <label name=TRACE_EV_SOCKET_RECEIVE/>
+        </enum> 
+      </field>
+       
+      <field name="event_data1"> <description>Data associated with event</description> <uint size=4/> </field>
+      <field name="event_data2"> <description>Data associated with event</description> <uint size=4/> </field>
+     </struct>
+  </event>
+
+  <event name=ipc>
+    <description>Hit key part of System V IPC</description>
+    <struct>
+      <field name="event_sub_id"> <description>Memory event ID</description> 
+        <enum size=1>
+          <label name=TRACE_EV_IPC_CALL value=1/>
+          <label name=TRACE_EV_IPC_MSG_CREATE/>
+          <label name=TRACE_EV_IPC_SEM_CREATE/>
+          <label name=TRACE_EV_IPC_SHM_CREATE/>
+        </enum> 
+      </field>
+       
+      <field name="event_data1"> <description>Data associated with event</description> <uint size=4/> </field>
+      <field name="event_data2"> <description>Data associated with event</description> <uint size=4/> </field>
+     </struct>
+  </event>
+
+  <event name=network>
+    <description>Hit key part of network communication</description>
+    <struct>
+      <field name="event_sub_id"> <description>Memory event ID</description> 
+        <enum size=1>
+          <label name=TRACE_EV_NETWORK_PACKET_IN value=1/>
+          <label name=TRACE_EV_NETWORK_PACKET_OUT/>
+        </enum> 
+      </field>
+       
+      <field name="event_data"> <description>Data associated with event</description> <uint size=4/> </field>
+     </struct>
+  </event>
+/*
+  <event name=block_start>
+    <description>Block start timestamp</description>
+    <typeref name=block_timestamp/>
+  </event>
+
+  <event name=block_end>
+    <description>Block end timestamp</description>
+    <typeref name=block_timestamp/>
+  </event>
+
+  <event name=time_heartbeat>
+    <description>System time values sent periodically to minimize cycle counter
+                 drift with respect to real time clock and to detect cycle counter roolovers
+    </description>
+    <typeref name=timestamp/>
+  </event>
+
+  <type name=block_timestamp>
+    <struct>
+      <field name=timestamp><typeref name=timestamp/></field>
+      <field name=block_id><uint size=4/></field>
+    </struct>
+  </type>
+
+  <type name=timestamp>
+    <struct>
+      <field name=time><typeref name=timespec/></field>
+      <field name="cycle_count"><uint size=8/></field>
+    </struct>
+  </type>
+
+  <type name=timespec>
+    <struct>
+      <field name="seconds"><uint size=4/></field>
+      <field name="nanoseconds"><uint size=4/></field>
+    </struct>
+  </type>
+*/
+
+</facility>
+
+
diff --git a/genevent/crc32.tab b/genevent/crc32.tab
new file mode 100644 (file)
index 0000000..d0174ad
--- /dev/null
@@ -0,0 +1,52 @@
+  0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
+  0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U,
+  0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U,
+  0x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU,
+  0x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U,
+  0x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U,
+  0xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U,
+  0xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU,
+  0x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U,
+  0x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU,
+  0xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U,
+  0xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U,
+  0x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U,
+  0x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU,
+  0x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU,
+  0xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U,
+  0x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU,
+  0x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U,
+  0x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U,
+  0xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U,
+  0x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU,
+  0x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U,
+  0xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U,
+  0xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU,
+  0x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U,
+  0x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U,
+  0x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U,
+  0x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U,
+  0xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U,
+  0x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU,
+  0x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU,
+  0x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U,
+  0xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U,
+  0xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU,
+  0x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU,
+  0x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U,
+  0xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU,
+  0xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U,
+  0x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU,
+  0x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U,
+  0x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU,
+  0xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U,
+  0x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U,
+  0x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU,
+  0x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U,
+  0xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U,
+  0x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U,
+  0x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U,
+  0xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U,
+  0xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U,
+  0x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU,
+  0x2d02ef8dU
diff --git a/genevent/genevent b/genevent/genevent
new file mode 100755 (executable)
index 0000000..a187096
Binary files /dev/null and b/genevent/genevent differ
diff --git a/genevent/genevent.c b/genevent/genevent.c
new file mode 100644 (file)
index 0000000..073ba9c
--- /dev/null
@@ -0,0 +1,498 @@
+/*
+
+genevent.c: Generate helper declarations and functions to trace events
+  from an event description file.
+
+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 ".event" event definitions input files 
+   specified as command line arguments and generates corresponding
+   ".c" and ".h" files required to trace such events in the kernel.
+   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
+   definitions.
+
+   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 
+   information when the processing of an ".event" file is finished. */
+
+#include <stdlib.h> 
+#include <string.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <linux/errno.h>  
+
+
+#include "parser.h"
+#include "genevent.h"
+
+/* Named types may be referenced from anywhere */
+
+facility * fac;
+
+int main(int argc, char** argv)
+{
+  char *token;
+  parse_file in;
+  char buffer[BUFFER_SIZE];
+  int i;
+
+  if(argc < 2){
+    printf("At least one event definition file is needed\n");
+    exit(1);
+  }
+
+  in.buffer = buffer;
+  in.error = error_callback;
+
+  for(i = 1 ; i < argc ; i++) {
+    in.lineno = 0;
+    in.name = allocAndCopy(argv[i]);
+
+    in.fp = fopen(in.name, "r");
+    if(!in.fp ){
+      in.error(&in,"cannot open facility input file");
+    }
+
+    while(1){
+      token = getToken(&in);
+      if(in.type == ENDFILE) break;
+
+      if(strcmp(token, "<")) in.error(&in,"not a facility file");
+      token = getName(&in);
+    
+      if(strcmp("facility",token) == 0) {
+       fac = memAlloc(sizeof(facility));
+       fac->name = NULL;
+       fac->description = NULL;
+       sequence_init(&(fac->events));
+       table_init(&(fac->named_types));
+       sequence_init(&(fac->unnamed_types));
+       
+       parseFacility(&in, fac);
+       
+       //check if any namedType is not defined
+       checkNamedTypesImplemented(&fac->named_types);
+      }else in.error(&in,"facility token was expected");
+
+      generateFile(argv[i]);
+
+      free(fac->name);
+      free(fac->description);
+      freeEvents(&fac->events);
+      sequence_dispose(&fac->events);
+      freeNamedType(&fac->named_types);
+      table_dispose(&fac->named_types);
+      freeTypes(&fac->unnamed_types);
+      sequence_dispose(&fac->unnamed_types);      
+      free(fac);      
+    }
+
+    free(in.name);
+    fclose(in.fp);
+
+  }
+  return 0;
+}
+
+
+/*****************************************************************************
+ *Function name
+ *    generateFile : generate .c and .h file 
+ *Input Params
+ *    name         : name of event definition file
+ ****************************************************************************/
+void generateFile(char *name){
+  char *cName, *hName, *tmp;
+  FILE * cFp, *hFp;
+  int nbEvent;
+  unsigned long checksum=0;
+
+  //remove .xml if it exists
+  tmp = &name[strlen(name)-4];
+  if(strcmp(tmp, ".xml") == 0){
+    *tmp = '\0';
+  }
+
+  tmp = strrchr(name,'/');
+  if(tmp){
+    tmp++;
+  }else{
+    tmp = name;
+  }
+  
+  cName = appendString(tmp,".c");
+  hName = appendString(tmp,".h");
+  cFp = fopen(cName,"w");
+  if(!cFp){
+    printf("Cannot open the file : %s\n",cName);
+    exit(1);
+  }
+
+  hFp = fopen(hName,"w");
+  if(!hFp){
+     printf("Cannot open the file : %s\n",hName);
+     exit(1);
+  }
+  
+  free(cName);
+  free(hName);
+
+  generateChecksum(fac->name, &checksum, &(fac->events));
+
+  /* generate .h file, event enumeration then structures and functions */
+  generateEnumEvent(hFp, fac->name, &nbEvent);
+  generateStructFunc(hFp, fac->name);
+
+  /* generate .c file, calls to register the facility at init time */
+  generateCfile(cFp,fac->name,nbEvent,checksum);
+
+  fclose(hFp);
+  fclose(cFp);
+}
+
+
+/*****************************************************************************
+ *Function name
+ *    generateEnumEvent : output event enum to .h file 
+ *Input Params
+ *    fp                : file to be written to
+ *    facName           : name of facility
+ *Output Params
+ *    nbEvent           : number of events in the facility
+ ****************************************************************************/
+void generateEnumEvent(FILE *fp, char *facName, int * nbEvent) {
+  int pos = 0;
+
+  //will be removed later
+  fprintf(fp,"typedef unsigned int trace_facility_t;\n\n");
+  fprintf(fp,"extern int trace_new(trace_facility_t fID, u8 eID, int length, char * buf);\n\n");
+
+
+  fprintf(fp,"/****  facility handle  ****/\n\n");
+  fprintf(fp,"extern trace_facility_t facility_%s;\n\n\n",facName);
+
+  fprintf(fp,"/****  event type  ****/\n\n");
+  fprintf(fp,"enum %s_event {\n",facName);
+
+  for(pos = 0; pos < fac->events.position;pos++) {
+    fprintf(fp,"  %s", ((event *)(fac->events.array[pos]))->name);
+    if(pos != fac->events.position-1) fprintf(fp,",\n");
+  }
+  fprintf(fp,"\n};\n\n\n");
+
+  //  fprintf(fp,"/****  number of events in the facility  ****/\n\n");
+  //  fprintf(fp,"int nbEvents_%s = %d;\n\n\n",facName, fac->events.position);
+  *nbEvent = fac->events.position;
+}
+
+
+/*****************************************************************************
+ *Function name
+ *    generateEnumDefinition: generate enum definition if it exists 
+ *Input Params
+ *    fp                    : file to be written to
+ *    fHead                 : enum type
+ ****************************************************************************/
+void generateEnumDefinition(FILE * fp, type_descriptor * type){
+  int pos;
+
+  fprintf(fp,"enum {\n");
+  for(pos = 0; pos < type->labels.position; pos++){
+    fprintf(fp,"  %s", type->labels.array[pos]);
+    if (pos != type->labels.position - 1) fprintf(fp,",\n");
+  }
+  fprintf(fp,"\n};\n\n\n");
+}
+
+/*****************************************************************************
+ *Function name
+ *    generateStrucTFunc: output structure and function to .h file 
+ *Input Params
+ *    fp                : file to be written to
+ *    facName           : name of facility
+ ****************************************************************************/
+void generateStructFunc(FILE * fp, char * facName){
+  event * ev;
+  field * fld;
+  type_descriptor * td;
+  int pos, pos1;
+  int hasStrSeq, flag, structCount, seqCount,strCount, whichTypeFirst=0;
+
+  for(pos = 0; pos < fac->events.position; pos++){
+    ev = (event *) fac->events.array[pos];
+    //yxx    if(ev->nested)continue;
+    fprintf(fp,"/****  structure and trace function for event: %s  ****/\n\n",ev->name);
+    if(ev->type == 0){ // event without type
+      fprintf(fp,"static inline void trace_%s_%s(void){\n",facName,ev->name);
+      fprintf(fp,"  trace_new(facility_%s, %s, 0, NULL);\n",facName,ev->name);
+      fprintf(fp,"};\n\n\n");
+      continue;
+    }
+
+    //if fields contain enum, print out enum definition
+    for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
+      fld = (field *)ev->type->fields.array[pos1];
+      if(fld->type->type == ENUM) generateEnumDefinition(fp, fld->type);      
+    }
+      
+    //default: no string, array or sequence in the event
+    hasStrSeq = 0;
+    whichTypeFirst = 0;
+    
+    //structure for kernel
+    flag = 0;
+    structCount = 0;
+    for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
+      fld  = (field *)ev->type->fields.array[pos1];
+      td = fld->type;
+      if( td->type != STRING && td->type != SEQUENCE && td->type != ARRAY){
+       if(whichTypeFirst == 0) whichTypeFirst = 1; //struct first
+       if(flag == 0){
+         flag = 1;         
+         fprintf(fp,"struct %s_%s_%d{\n",ev->name,facName,++structCount);
+       }
+       fprintf(fp, "  %s %s; /* %s */\n",getTypeStr(td),fld->name,fld->description );
+      }else{
+       if(whichTypeFirst == 0) whichTypeFirst = 2; //string or sequence or array first
+       hasStrSeq++;
+       if(flag)
+         fprintf(fp,"} __attribute__ ((packed));\n\n");
+       flag = 0;
+      }       
+    }
+    if(flag)
+      fprintf(fp,"} __attribute__ ((packed));\n\n");
+
+    //trace function : function name and parameters
+    seqCount = 0;
+    strCount = 0;
+    fprintf(fp,"static inline void trace_%s_%s(",facName,ev->name);
+    for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
+      fld  = (field *)ev->type->fields.array[pos1];
+      td = fld->type;
+      if(td->type == ARRAY ){
+       fprintf(fp,"%s * %s",getTypeStr(td), fld->name);
+      }else if(td->type == STRING){
+       fprintf(fp,"short int strLength_%d, %s * %s",++strCount, getTypeStr(td), fld->name);
+     }else if(td->type == SEQUENCE){
+         fprintf(fp,"%s seqLength_%d, %s * %s",uintOutputTypes[td->size], ++seqCount,getTypeStr(td), fld->name);
+      }else  fprintf(fp,"%s %s",getTypeStr(td), fld->name);     
+      if(pos1 != ev->type->fields.position -1)fprintf(fp,", ");
+    }
+    fprintf(fp,"){\n");
+
+    //length of buffer : length of all structures
+    fprintf(fp,"  int bufLength = ");
+    for(pos1=0;pos1<structCount;pos1++){
+      fprintf(fp,"sizeof(struct %s_%s_%d)",ev->name, facName,pos1+1);
+      if(pos1 != structCount-1) fprintf(fp," + ");
+    }
+
+    //length of buffer : length of all arrays, sequences and strings
+    seqCount = 0;
+    strCount = 0;
+    flag = 0;
+    for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
+      fld  = (field *)ev->type->fields.array[pos1];
+      td = fld->type;
+      if(td->type == SEQUENCE || td->type==STRING ||td->type==ARRAY){
+       if(structCount || flag > 0) fprintf(fp," + ");    
+       if(td->type == SEQUENCE) fprintf(fp,"sizeof(%s) + sizeof(%s) * seqLength_%d",uintOutputTypes[td->size], getTypeStr(td), ++seqCount);
+       else if(td->type==STRING) fprintf(fp,"strLength_%d + 1", ++strCount);
+       else if(td->type==ARRAY) fprintf(fp,"sizeof(%s) * %d", getTypeStr(td),td->size);
+       if(structCount == 0) flag = 1;
+      }
+    }
+    fprintf(fp,";\n");
+
+    //allocate buffer
+    fprintf(fp,"  char buff[bufLength];\n");
+
+    //declare a char pointer if needed
+    if(structCount + hasStrSeq > 1) fprintf(fp,"  char * ptr = buff;\n");
+    
+    //allocate memory for new struct and initialize it
+    if(whichTypeFirst == 1){ //struct first
+      for(pos1=0;pos1<structCount;pos1++){     
+       if(pos1==0) fprintf(fp,"  struct %s_%s_1 * __1 = (struct %s_%s_1 *)buff;\n",ev->name, facName,ev->name, facName);
+       else fprintf(fp,"  struct %s_%s_%d  __%d;\n",ev->name, facName,pos1+1,pos1+1);  
+      }      
+    }else if(whichTypeFirst == 2){
+      for(pos1=0;pos1<structCount;pos1++)
+       fprintf(fp,"  struct %s_%s_%d  __%d;\n",ev->name, facName,pos1+1,pos1+1);
+    }
+    fprintf(fp,"\n");
+
+    if(structCount) fprintf(fp,"  //initialize structs\n");
+    flag = 0;
+    structCount = 0;
+    for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
+      fld  = (field *)ev->type->fields.array[pos1];
+      td = fld->type;
+      if(td->type != ARRAY && td->type != SEQUENCE && td->type != STRING){
+       if(flag == 0){
+         flag = 1;     
+         structCount++;
+         if(structCount > 1) fprintf(fp,"\n");
+       }
+       if(structCount == 1 && whichTypeFirst == 1) fprintf(fp, "  __1->%s =  %s;\n",fld->name,fld->name );
+       else fprintf(fp, "  __%d.%s =  %s;\n",structCount ,fld->name,fld->name);        
+      }else flag = 0;
+    }
+    if(structCount) fprintf(fp,"\n");
+
+    //set ptr to the end of first struct if needed;
+    if(whichTypeFirst == 1 && structCount + hasStrSeq > 1){
+      fprintf(fp,"\n  //set ptr to the end of the first struct\n");
+      fprintf(fp,"  ptr +=  sizeof(struct %s_%s_1);\n\n",ev->name, facName); 
+    }
+
+    //copy struct, sequence and string to buffer
+    seqCount = 0;
+    strCount = 0;
+    flag = 0;
+    structCount = 0;
+    for(pos1 = 0; pos1 < ev->type->fields.position; pos1++){
+      fld  = (field *)ev->type->fields.array[pos1];
+      td = fld->type;
+      if(td->type != STRING && td->type != SEQUENCE && td->type != ARRAY){
+       if(flag == 0) structCount++;    
+       flag++; 
+       if((structCount > 1 || whichTypeFirst == 2) && flag == 1){
+         fprintf(fp,"  //copy struct to buffer\n");
+         fprintf(fp,"  memcpy(ptr, &__%d, sizeof(struct %s_%s_%d));\n",structCount, ev->name, facName,structCount);
+         fprintf(fp,"  ptr +=  sizeof(struct %s_%s_%d);\n\n",ev->name, facName,structCount); 
+       }
+      }else if(td->type == SEQUENCE){
+       flag = 0;
+       fprintf(fp,"  //copy sequence length and sequence to buffer\n");
+       fprintf(fp,"  *ptr = seqLength_%d;\n",++seqCount);
+       fprintf(fp,"  ptr += sizeof(%s);\n",uintOutputTypes[td->size]);
+       fprintf(fp,"  memcpy(ptr, %s, sizeof(%s) * seqLength_%d);\n",fld->name, getTypeStr(td), seqCount);
+       fprintf(fp,"  ptr += sizeof(%s) * seqLength_%d;\n\n",getTypeStr(td), seqCount );
+      }else if(td->type==STRING){
+       flag = 0;
+       fprintf(fp,"  //copy string to buffer\n");
+       fprintf(fp,"  if(strLength_%d > 0){\n",++strCount);
+       fprintf(fp,"    memcpy(ptr, %s, strLength_%d + 1);\n", fld->name, strCount);
+       fprintf(fp,"    ptr += strLength_%d + 1;\n",strCount);  
+       fprintf(fp,"  }else{\n");
+       fprintf(fp,"    *ptr = '\\0';\n");
+       fprintf(fp,"    ptr += 1;\n");
+       fprintf(fp,"  }\n\n");
+      }else if(td->type==ARRAY){
+       flag = 0;
+       fprintf(fp,"  //copy array to buffer\n");
+       fprintf(fp,"  memcpy(ptr, %s, sizeof(%s) * %d);\n", fld->name, getTypeStr(td), td->size);
+       fprintf(fp,"  ptr += sizeof(%s) * %d;\n\n",getTypeStr(td), td->size);
+      }      
+    }    
+    if(structCount + seqCount > 1) fprintf(fp,"\n");
+
+    //call trace function
+    fprintf(fp,"\n  //call trace function\n");
+    fprintf(fp,"  trace_new(facility_%s, %s, bufLength, buff);\n",facName,ev->name);
+    fprintf(fp,"};\n\n\n");
+  }
+
+}
+
+/*****************************************************************************
+ *Function name
+ *    getTypeStr        : generate type string 
+ *Input Params
+ *    td                : a type descriptor
+ *Return Values
+ *    char *            : type string
+ ****************************************************************************/
+char * getTypeStr(type_descriptor * td){
+  type_descriptor * t ;
+
+  switch(td->type){
+    case INT:
+      return intOutputTypes[td->size];
+    case UINT:
+      return uintOutputTypes[td->size];
+    case FLOAT:
+      return floatOutputTypes[td->size];
+    case STRING:
+      return "char";
+    case ENUM:
+      return uintOutputTypes[td->size];
+    case ARRAY:
+    case SEQUENCE:
+      t = td->nested_type;
+      switch(t->type){
+        case INT:
+         return intOutputTypes[t->size];
+        case UINT:
+         return uintOutputTypes[t->size];
+        case FLOAT:
+         return floatOutputTypes[t->size];
+        case STRING:
+         return "char";
+        case ENUM:
+         return uintOutputTypes[t->size];
+        default :
+         error_callback(NULL,"Nested struct is not supportted");
+         break;        
+      }
+      break;
+    case STRUCT: //for now we do not support nested struct
+      error_callback(NULL,"Nested struct is not supportted");
+      break;
+    default:
+      error_callback(NULL,"No type information");
+      break;
+  }
+  return NULL;
+}
+
+/*****************************************************************************
+ *Function name
+ *    generateCfile     : generate a .c file 
+ *Input Params
+ *    fp                : file to be written to
+ *    facName           : name of facility
+ *    nbEvent           : number of events in the facility
+ *    checksum          : checksum for the facility
+ ****************************************************************************/
+void generateCfile(FILE * fp, char * facName, int nbEvent, unsigned long checksum){
+  //will be removed later
+  fprintf(fp,"typedef unsigned int trace_facility_t;\n\n");
+
+  fprintf(fp,"static unsigned long checksum = %lu;\n\n",checksum);
+  fprintf(fp,"/* facility handle */\n");
+  fprintf(fp,"trace_facility_t facility_%s;\n\n",facName);
+
+  fprintf(fp,"static void __init facility_%s_init(){\n",facName);  
+  fprintf(fp,"  facility_%s =  trace_register_facility_by_checksum(\"%s\", checksum,%d);\n",facName,facName,nbEvent);
+  fprintf(fp,"}\n\n");
+
+  fprintf(fp,"static void __exit facility_%s_exit(){\n",facName);  
+  fprintf(fp,"}\n\n");
+
+  fprintf(fp,"module_init(facility_%s_init);\n",facName);
+  fprintf(fp,"module_exit(facility_%s_exit);\n",facName);  
+}
+
+
diff --git a/genevent/genevent.h b/genevent/genevent.h
new file mode 100644 (file)
index 0000000..2e2050c
--- /dev/null
@@ -0,0 +1,9 @@
+
+void generateFile(char * file);
+void generateEnumEvent(FILE * fp,char * facName, int * nbEvent);
+void generateStructFunc(FILE * fp, char * facName);
+void generateCfile(FILE * fp, char * facName, int nbEvent, unsigned long checksum);
+void generateEnumDefinition(FILE * fp, type_descriptor * fHead);
+char * getTypeStr(type_descriptor * td);
+
+
diff --git a/genevent/parser.c b/genevent/parser.c
new file mode 100644 (file)
index 0000000..b6561ca
--- /dev/null
@@ -0,0 +1,1175 @@
+/*
+
+parser.c: Generate helper declarations and functions to trace events
+  from an event description file.
+
+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.
+   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
+   definitions.
+
+   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 
+   information when the processing of an ".xml" file is finished. */
+
+#include <stdlib.h> 
+#include <string.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <linux/errno.h>  
+
+
+#include "parser.h"
+
+
+/*****************************************************************************
+ *Function name
+ *    getSize    : translate from string to integer
+ *Input params 
+ *    in         : input file handle
+ *Return values  
+ *    size                           
+ *****************************************************************************/
+
+int getSize(parse_file *in)
+{
+  char *token;
+
+  token = getToken(in);
+  if(in->type == NUMBER) {
+    if(strcmp(token,"1") == 0) return 0;
+    else if(strcmp(token,"2") == 0) return 1;
+    else if(strcmp(token,"4") == 0) return 2;
+    else if(strcmp(token,"8") == 0) return 3;
+  }
+  else if(in->type == NAME) {
+    if(strcmp(token,"short") == 0) return 4;
+    else if(strcmp(token,"medium") == 0) return 5;
+    else if(strcmp(token,"long") == 0) return 6;
+  }
+  in->error(in,"incorrect size specification");
+  return -1;
+}
+
+/*****************************************************************************
+ *Function name
+ *    error_callback  : print out error info
+ *Input params
+ *    in              : input file handle
+ *    msg             : message to be printed                  
+ ****************************************************************************/
+
+void error_callback(parse_file *in, char *msg)
+{
+  if(in)
+    printf("Error in file %s, line %d: %s\n", in->name, in->lineno, msg);
+  else
+    printf("%s\n",msg);
+  exit(1);
+}
+
+/*****************************************************************************
+ *Function name
+ *    memAlloc  : allocate memory                    
+ *Input params 
+ *    size      : required memory size               
+ *return value 
+ *    void *    : pointer to allocate memory or NULL 
+ ****************************************************************************/
+
+void * memAlloc(int size)
+{
+  void * addr;
+  if(size == 0) return NULL;
+  addr = malloc(size);
+  if(!addr){
+    printf("Failed to allocate memory");    
+    exit(1);
+  }
+  return addr;   
+}
+
+/*****************************************************************************
+ *Function name
+ *    allocAndCopy : allocate memory and initialize it  
+ *Input params 
+ *    str          : string to be put in memory         
+ *return value 
+ *    char *       : pointer to allocate memory or NULL
+ ****************************************************************************/
+
+char *allocAndCopy(char *str)
+{
+  char * addr;
+  if(str == NULL) return NULL;
+  addr = (char *)memAlloc(strlen(str)+1);
+  strcpy(addr,str);
+  return addr;
+}
+
+/**************************************************************************
+ * Function :
+ *    getNameAttribute,getFormatAttribute,getSizeAttribute,getValueAttribute 
+ *    getValueStrAttribute
+ * Description :
+ *    Read the attribute from the input file.
+ *
+ * Parameters :
+ *    in , input file handle.
+ *
+ * Return values :
+ *    address of the attribute.
+ *
+ **************************************************************************/
+
+char * getNameAttribute(parse_file *in)
+{
+  char * token, car;
+  token = getName(in);
+  if(strcmp("name",token))in->error(in,"name was expected");
+  getEqual(in);
+  
+  car = seekNextChar(in);
+  if(car == EOF)in->error(in,"name was expected");
+  else if(car == '\"')token = getQuotedString(in);
+  else token = getName(in);
+  return token;
+}
+
+char * getFormatAttribute(parse_file *in)
+{
+  char * token;
+
+  //format is an option
+  token = getToken(in); 
+  if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
+    ungetToken(in);
+    return NULL;
+  }
+
+  if(strcmp("format",token))in->error(in,"format was expected");
+  getEqual(in);
+  token = getQuotedString(in);
+  return token;
+}
+
+int getSizeAttribute(parse_file *in)
+{
+  char * token;
+  getName(in);
+  getEqual(in);
+
+  return getSize(in);
+}
+
+int getValueAttribute(parse_file *in)
+{
+  char * token;
+  getName(in);
+  getEqual(in);
+  
+  return getNumber(in);
+}
+
+//for <label name=label_name value=n/>, value is an option
+char * getValueStrAttribute(parse_file *in)
+{
+  char * token;
+
+  token = getToken(in); 
+  if(strcmp("/",token) == 0){
+    ungetToken(in);
+    return NULL;
+  }
+  
+  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;  
+}
+
+char * getDescription(parse_file *in)
+{
+  long int pos;
+  char * token, car, *str;
+
+  pos = ftell(in->fp);
+
+  getLAnglebracket(in);
+  token = getName(in);
+  if(strcmp("description",token)){
+    fseek(in->fp, pos, SEEK_SET);
+    return NULL;
+  }
+  
+  getRAnglebracket(in);
+
+  pos = 0;
+  while((car = getc(in->fp)) != EOF) {
+    if(car == '<') break;
+    if(car == '\0') continue;
+    in->buffer[pos] = car;
+    pos++;
+  }
+  if(car == EOF)in->error(in,"not a valid description");
+  in->buffer[pos] = '\0';
+
+  str = allocAndCopy(in->buffer);
+
+  getForwardslash(in);
+  token = getName(in);
+  if(strcmp("description", token))in->error(in,"not a valid description");
+  getRAnglebracket(in);
+
+  return str;
+}
+
+/*****************************************************************************
+ *Function name
+ *    parseFacility : generate event list  
+ *Input params 
+ *    in            : input file handle          
+ *    fac           : empty facility
+ *Output params
+ *    fac           : facility filled with event list
+ ****************************************************************************/
+
+void parseFacility(parse_file *in, facility * fac)
+{
+  char * token;
+  event *ev;
+  
+  fac->name = allocAndCopy(getNameAttribute(in));    
+  getRAnglebracket(in);    
+  
+  fac->description = getDescription(in);
+  
+  while(1){
+    getLAnglebracket(in);    
+
+    token = getToken(in);
+    if(in->type == ENDFILE)
+      in->error(in,"the definition of the facility is not finished");
+
+    if(strcmp("event",token) == 0){
+      ev = (event*) memAlloc(sizeof(event));
+      sequence_push(&(fac->events),ev);
+      parseEvent(in,ev, &(fac->unnamed_types), &(fac->named_types));    
+    }else if(strcmp("type",token) == 0){
+      parseTypeDefinition(in, &(fac->unnamed_types), &(fac->named_types));
+    }else if(in->type == FORWARDSLASH){
+      break;
+    }else in->error(in,"event or type token expected\n");
+  }
+
+  token = getName(in);
+  if(strcmp("facility",token)) in->error(in,"not the end of the facility");
+  getRAnglebracket(in); //</facility>
+}
+
+/*****************************************************************************
+ *Function name
+ *    parseEvent    : generate event from event definition 
+ *Input params 
+ *    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)   
+ ****************************************************************************/
+
+void parseEvent(parse_file *in, event * ev, sequence * unnamed_types, 
+               table * named_types) 
+{
+  char *token;
+  type_descriptor *t;
+
+  //<event name=eventtype_name>
+  ev->name = allocAndCopy(getNameAttribute(in));
+  getRAnglebracket(in);  
+
+  //<description>...</descriptio>
+  ev->description = getDescription(in); 
+  
+  //event can have STRUCT, TYPEREF or NOTHING
+  getLAnglebracket(in);
+
+  token = getToken(in);
+  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");
+
+  token = getName(in);
+  if(strcmp("event",token))in->error(in,"not an event definition");
+  getRAnglebracket(in);  //</event>
+}
+
+/*****************************************************************************
+ *Function name
+ *    parseField    : get field infomation from buffer 
+ *Input params 
+ *    in            : input file handle
+ *    t             : type descriptor
+ *    unnamed_types : array of unamed types
+ *    named_types   : array of named types
+ ****************************************************************************/
+
+void parseFields(parse_file *in, type_descriptor *t, sequence * unnamed_types,
+                table * named_types) 
+{
+  char * token;
+  field *f;
+
+  f = (field *)memAlloc(sizeof(field));
+  sequence_push(&(t->fields),f);
+
+  //<field name=field_name> <description> <type> </field>
+  f->name = allocAndCopy(getNameAttribute(in)); 
+  getRAnglebracket(in);
+
+  f->description = getDescription(in);
+
+  //<int size=...>
+  getLAnglebracket(in);
+  f->type = parseType(in,NULL, unnamed_types, named_types);
+
+  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 : 
+ *                     Primitive:
+ *                        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)...)
+ *                     type name:
+ *                        type(name,type)
+ *Input params 
+ *    in               : input file handle
+ *    inType           : a type descriptor          
+ *    unnamed_types    : array of unamed types
+ *    named_types      : array of named types
+ *Return values  
+ *    type_descriptor* : a type descriptor             
+ ****************************************************************************/
+
+type_descriptor *parseType(parse_file *in, type_descriptor *inType, 
+                          sequence * unnamed_types, table * named_types) 
+{
+  char *token;
+  type_descriptor *t;
+
+  if(inType == NULL) {
+    t = (type_descriptor *) memAlloc(sizeof(type_descriptor));
+    t->type_name = NULL;
+    t->type = NONE;
+    t->fmt = NULL;
+    sequence_push(unnamed_types,t);
+  }
+  else t = inType;
+
+  token = getName(in);
+
+  if(strcmp(token,"struct") == 0) {
+    t->type = STRUCT;
+    getRAnglebracket(in); //<struct>
+    getLAnglebracket(in); //<field name=..>
+    token = getToken(in);
+    sequence_init(&(t->fields));
+    while(strcmp("field",token) == 0){
+      parseFields(in,t, unnamed_types, named_types);
+      
+      //next field
+      getLAnglebracket(in);
+      token = getToken(in);    
+    }
+    if(strcmp("/",token))in->error(in,"not a valid structure definition");
+    token = getName(in);
+    if(strcmp("struct",token)!=0)
+      in->error(in,"not a valid structure definition");
+    getRAnglebracket(in); //</struct>
+  }
+  else if(strcmp(token,"union") == 0) {
+    t->type = UNION;
+    t->size = getSizeAttribute(in);
+    getRAnglebracket(in); //<union typecodesize=isize>
+
+    getLAnglebracket(in); //<field name=..>
+    token = getToken(in);
+    sequence_init(&(t->fields));
+    while(strcmp("field",token) == 0){
+      parseFields(in,t, unnamed_types, named_types);
+      
+      //next field
+      getLAnglebracket(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");        
+    getRAnglebracket(in); //</union>
+  }
+  else if(strcmp(token,"array") == 0) {
+    t->type = ARRAY;
+    t->size = getValueAttribute(in);
+    getRAnglebracket(in); //<array size=n>
+
+    getLAnglebracket(in); //<type struct> 
+    t->nested_type = parseType(in,NULL, unnamed_types, named_types);
+
+    getLAnglebracket(in); //</array>
+    getForwardslash(in);
+    token = getName(in);
+    if(strcmp("array",token))in->error(in,"not a valid array definition");
+    getRAnglebracket(in);  //</array>
+  }
+  else if(strcmp(token,"sequence") == 0) {
+    t->type = SEQUENCE;
+    t->size = getSizeAttribute(in);
+    getRAnglebracket(in); //<array lengthsize=isize>
+
+    getLAnglebracket(in); //<type struct> 
+    t->nested_type = parseType(in,NULL, unnamed_types, named_types);
+
+    getLAnglebracket(in); //</sequence>
+    getForwardslash(in);
+    token = getName(in);
+    if(strcmp("sequence",token))in->error(in,"not a valid sequence definition");
+    getRAnglebracket(in); //</sequence>
+  }
+  else if(strcmp(token,"enum") == 0) {
+    char * str, *str1;
+    t->type = ENUM;
+    sequence_init(&(t->labels));
+    t->size = getSizeAttribute(in);
+    t->fmt = allocAndCopy(getFormatAttribute(in));
+    getRAnglebracket(in);
+
+    //<label name=label1 value=n/>
+    getLAnglebracket(in);
+    token = getToken(in); //"label" or "/"
+    while(strcmp("label",token) == 0){
+      str   = allocAndCopy(getNameAttribute(in));      
+      token = getValueStrAttribute(in);
+      if(token){
+       str1 = appendString(str,"=");
+       free(str);
+       str = appendString(str1,token);
+       free(str1);
+       sequence_push(&(t->labels),str);
+      }else
+       sequence_push(&(t->labels),str);
+
+      getForwardslash(in);
+      getRAnglebracket(in);
+      
+      //next label definition
+      getLAnglebracket(in);
+      token = getToken(in); //"label" or "/"      
+    }
+    if(strcmp("/",token))in->error(in, "not a valid enum definition");
+    token = getName(in);
+    if(strcmp("enum",token))in->error(in, "not a valid enum definition");
+    getRAnglebracket(in); //</label>
+  }
+  else if(strcmp(token,"int") == 0) {
+    t->type = INT;
+    t->size = getSizeAttribute(in);
+    t->fmt  = allocAndCopy(getFormatAttribute(in));
+    getForwardslash(in);
+    getRAnglebracket(in); 
+  }
+  else if(strcmp(token,"uint") == 0) {
+    t->type = UINT;
+    t->size = getSizeAttribute(in);
+    t->fmt  = allocAndCopy(getFormatAttribute(in));
+    getForwardslash(in);
+    getRAnglebracket(in); 
+  }
+  else if(strcmp(token,"float") == 0) {
+    t->type = FLOAT;
+    t->size = getSizeAttribute(in);
+    t->fmt  = allocAndCopy(getFormatAttribute(in));
+    getForwardslash(in);
+    getRAnglebracket(in); 
+  }
+  else if(strcmp(token,"string") == 0) {
+    t->type = STRING;
+    t->fmt  = allocAndCopy(getFormatAttribute(in));
+    getForwardslash(in);
+    getRAnglebracket(in); 
+  }
+  else if(strcmp(token,"typeref") == 0){
+    // Must be a named type
+    if(inType != NULL) 
+      in->error(in,"Named type cannot refer to a named type");
+    else {
+      free(t);
+      sequence_pop(unnamed_types);
+      token = getNameAttribute(in);
+      t = find_named_type(token, named_types);
+      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          
+ *    named_types         : array of named types
+ *Return values  
+ *    type_descriptor *   : a type descriptor                       
+ *****************************************************************************/
+
+type_descriptor * find_named_type(char *name, table * named_types)
+{ 
+  type_descriptor *t;
+
+  t = table_find(named_types,name);
+  if(t == NULL) {
+    t = (type_descriptor *)memAlloc(sizeof(type_descriptor));
+    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 
+ *    in                  : input file handle          
+ *    unnamed_types       : array of unamed types
+ *    named_types         : array of named types
+ *****************************************************************************/
+
+void parseTypeDefinition(parse_file * in, sequence * unnamed_types,
+                        table * named_types)
+{
+  char *token;
+  type_descriptor *t;
+
+  token = getNameAttribute(in);
+  t = find_named_type(token, named_types);
+
+  if(t->type != NONE) in->error(in,"redefinition of named type");
+  getRAnglebracket(in); //<type name=type_name>
+  getLAnglebracket(in); //<struct>
+  token = getName(in);
+  if(strcmp("struct",token))in->error(in,"not a valid type definition");
+  ungetToken(in);
+  parseType(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");  
+  getRAnglebracket(in); //</type>
+}
+
+/**************************************************************************
+ * Function :
+ *    getComa, getName, getNumber, getEqual
+ * Description :
+ *    Read a token from the input file, check its type, return it scontent.
+ *
+ * Parameters :
+ *    in , input file handle.
+ *
+ * Return values :
+ *    address of token content.
+ *
+ **************************************************************************/
+
+char *getName(parse_file * in) 
+{
+  char *token;
+
+  token = getToken(in);
+  if(in->type != NAME) in->error(in,"Name token was expected");
+  return token;
+}
+
+int getNumber(parse_file * in) 
+{
+  char *token;
+
+  token = getToken(in);
+  if(in->type != NUMBER) in->error(in, "Number token was expected");
+  return atoi(token);
+}
+
+char *getForwardslash(parse_file * in) 
+{
+  char *token;
+
+  token = getToken(in);
+  if(in->type != FORWARDSLASH) in->error(in, "forward slash token was expected");
+  return token;
+}
+
+char *getLAnglebracket(parse_file * in) 
+{
+  char *token;
+
+  token = getToken(in);
+  if(in->type != LANGLEBRACKET) in->error(in, "Left angle bracket was expected");
+  return token;
+}
+
+char *getRAnglebracket(parse_file * in) 
+{
+  char *token;
+
+  token = getToken(in);
+  if(in->type != RANGLEBRACKET) in->error(in, "Right angle bracket was expected");
+  return token;
+}
+
+char *getQuotedString(parse_file * in) 
+{
+  char *token;
+
+  token = getToken(in);
+  if(in->type != QUOTEDSTRING) in->error(in, "quoted string was expected");
+  return token;
+}
+
+char * getEqual(parse_file *in)
+{
+  char *token;
+
+  token = getToken(in);
+  if(in->type != EQUAL) in->error(in, "equal was expected");
+  return token;
+}
+
+char seekNextChar(parse_file *in)
+{
+  char car;
+  while((car = getc(in->fp)) != EOF) {
+    if(!isspace(car)){
+      ungetc(car,in->fp);
+      return car;
+    }
+  }  
+  return EOF;
+}
+
+/******************************************************************
+ * Function :
+ *    getToken, ungetToken
+ * Description :
+ *    Read a token from the input file and return its type and content.
+ *    Line numbers are accounted for and whitespace/comments are skipped.
+ *
+ * Parameters :
+ *    in, input file handle.
+ *
+ * Return values :
+ *    address of token content.
+ *
+ ******************************************************************/
+
+void ungetToken(parse_file * in)
+{
+  in->unget = 1;
+}
+
+char *getToken(parse_file * in)
+{
+  FILE *fp = in->fp;
+  char car, car1;
+  int pos = 0, escaped;
+
+  if(in->unget == 1) {
+    in->unget = 0;
+    return in->buffer;
+  }
+
+  /* skip whitespace and comments */
+
+  while((car = getc(fp)) != EOF) {
+    if(car == '/') {
+      car1 = getc(fp); 
+      if(car1 == '*') skipComment(in);
+      else if(car1 == '/') skipEOL(in);
+      else { 
+        car1 = ungetc(car1,fp);
+        break;
+      }
+    }
+    else if(car == '\n') in->lineno++;
+    else if(!isspace(car)) break;
+  }
+
+  switch(car) {
+    case EOF:
+      in->type = ENDFILE;
+      break;
+    case '/':
+      in->type = FORWARDSLASH;
+      in->buffer[pos] = car;
+      pos++;
+      break;
+    case '<':
+      in->type = LANGLEBRACKET;
+      in->buffer[pos] = car;
+      pos++;
+      break;
+    case '>':
+      in->type = RANGLEBRACKET;
+      in->buffer[pos] = car;
+      pos++;
+      break;
+    case '=':
+      in->type = EQUAL;
+      in->buffer[pos] = car;
+      pos++;
+      break;
+    case '"':
+      escaped = 0;
+      while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) {
+        if(car == '\\' && escaped == 0) {
+         in->buffer[pos] = car;
+         pos++;
+          escaped = 1;
+          continue;
+        }
+        if(car == '"' && escaped == 0) break;
+        if(car == '\n' && escaped == 0) {
+          in->error(in, "non escaped newline inside quoted string");
+        }
+        if(car == '\n') in->lineno++;
+        in->buffer[pos] = car;
+        pos++;
+        escaped = 0;
+      }
+      if(car == EOF) in->error(in,"no ending quotemark");
+      if(pos == BUFFER_SIZE) in->error(in, "quoted string token too large");
+      in->type = QUOTEDSTRING;
+      break;
+    default:
+      if(isdigit(car)) {
+        in->buffer[pos] = car;
+        pos++;
+        while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) {
+          if(!isdigit(car)) {
+            ungetc(car,fp);
+            break;
+          }
+          in->buffer[pos] = car;
+          pos++;
+        }
+       if(car == EOF) ungetc(car,fp);
+        if(pos == BUFFER_SIZE) in->error(in, "number token too large");
+        in->type = NUMBER;
+      }    
+      else if(isalpha(car)) {
+        in->buffer[0] = car;
+        pos = 1;
+        while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) {
+          if(!isalnum(car)) {
+            ungetc(car,fp);
+            break;
+          }
+          in->buffer[pos] = car;
+          pos++;
+        }
+       if(car == EOF) ungetc(car,fp);
+        if(pos == BUFFER_SIZE) in->error(in, "name token too large");
+        in->type = NAME;
+      }
+      else in->error(in, "invalid character, unrecognized token");
+  }
+  in->buffer[pos] = 0;
+  return in->buffer;
+}
+
+void skipComment(parse_file * in)
+{
+  char car;
+  while((car = getc(in->fp)) != EOF) {
+    if(car == '\n') in->lineno++;
+    else if(car == '*') {
+      car = getc(in->fp);
+      if(car ==EOF) break;
+      if(car == '/') return;
+      ungetc(car,in->fp);
+    }
+  }
+  if(car == EOF) in->error(in,"comment begining with '/*' has no ending '*/'");
+}
+
+void skipEOL(parse_file * in)
+{
+  char car;
+  while((car = getc(in->fp)) != EOF) {
+    if(car == '\n') {
+      ungetc(car,in->fp);
+      break;
+    }
+  }
+  if(car == EOF)ungetc(car, in->fp);
+}
+
+int isalpha(char c)
+{
+  int i,j;
+  if(c == '_')return 1;
+  i = c - 'a';
+  j = c - 'A';
+  if((i>=0 && i<26) || (j>=0 && j<26)) return 1;
+  return 0;
+}
+
+int isalnum(char c)
+{
+  return (isalpha(c) || isdigit(c));
+}
+
+/*****************************************************************************
+ *Function name
+ *    checkNamedTypesImplemented : check if all named types have definition
+ ****************************************************************************/
+
+void checkNamedTypesImplemented(table * named_types)
+{
+  type_descriptor *t;
+  int pos;
+  char str[256];
+
+  for(pos = 0 ; pos < named_types->values.position; pos++) {
+    t = (type_descriptor *) named_types->values.array[pos];
+    if(t->type == NONE){
+      sprintf(str,"named type '%s' has no definition",(char*)named_types->keys.array[pos]);
+      error_callback(NULL,str);   
+    }
+  }
+}
+
+
+/*****************************************************************************
+ *Function name
+ *    generateChecksum  : generate checksum for the facility
+ *Input Params
+ *    facName           : name of facility
+ *Output Params
+ *    checksum          : checksum for the facility
+ ****************************************************************************/
+
+void generateChecksum( char* facName, unsigned long * checksum, sequence * events)
+{
+  unsigned long crc ;
+  int pos;
+  event * ev;
+  char str[256];
+
+  crc = crc32(facName);
+  for(pos = 0; pos < events->position; pos++){
+    ev = (event *)(events->array[pos]);
+    crc = partial_crc32(ev->name,crc);    
+    if(!ev->type) continue; //event without type
+    if(ev->type->type != STRUCT){
+      sprintf(str,"event '%s' has a type other than STRUCT",ev->name);
+      error_callback(NULL, str);
+    }
+    crc = getTypeChecksum(crc, ev->type);
+  }
+  *checksum = crc;
+}
+
+/*****************************************************************************
+ *Function name
+ *   getTypeChecksum    : generate checksum by type info
+ *Input Params
+ *    crc               : checksum generated so far
+ *    type              : type descriptor containing type info
+ *Return value          
+ *    unsigned long     : checksum 
+ *****************************************************************************/
+
+unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor * type)
+{
+  unsigned long crc = aCrc;
+  char * str = NULL, buf[16];
+  int flag = 0, pos;
+  field * fld;
+
+  switch(type->type){
+    case INT:
+      str = intOutputTypes[type->size];
+      break;
+    case UINT:
+      str = uintOutputTypes[type->size];
+      break;
+    case FLOAT:
+      str = floatOutputTypes[type->size];
+      break;
+    case STRING:
+      str = allocAndCopy("string");
+      flag = 1;
+      break;
+    case ENUM:
+      str = appendString("enum ", uintOutputTypes[type->size]);
+      flag = 1;
+      break;
+    case ARRAY:
+      sprintf(buf,"%d\0",type->size);
+      str = appendString("array ",buf);
+      flag = 1;
+      break;
+    case SEQUENCE:
+      sprintf(buf,"%d\0",type->size);
+      str = appendString("sequence ",buf);
+      flag = 1;
+      break;
+    case STRUCT:
+      str = allocAndCopy("struct");
+      flag = 1;
+      break;
+    case UNION:
+      str = allocAndCopy("union");
+      flag = 1;
+      break;
+    default:
+      error_callback(NULL, "named type has no definition");
+      break;
+  }
+
+  crc = partial_crc32(str,crc);
+  if(flag) free(str);
+
+  if(type->fmt) crc = partial_crc32(type->fmt,crc);
+    
+  if(type->type == ARRAY || type->type == SEQUENCE){
+    crc = getTypeChecksum(crc,type->nested_type);
+  }else if(type->type == STRUCT || type->type == UNION){
+    for(pos =0; pos < type->fields.position; pos++){
+      fld = (field *) 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);
+  }
+
+  return crc;
+}
+
+
+/* Event type descriptors */
+void freeType(type_descriptor * tp)
+{
+  int pos2;
+  field *f;
+
+  if(tp->fmt != NULL) free(tp->fmt);
+  if(tp->type == ENUM) {
+    for(pos2 = 0; pos2 < tp->labels.position; pos2++) {
+      free(tp->labels.array[pos2]);
+    }
+    sequence_dispose(&(tp->labels));
+  }
+  if(tp->type == STRUCT) {
+    for(pos2 = 0; pos2 < tp->fields.position; pos2++) {
+      f = (field *) tp->fields.array[pos2];
+      free(f->name);
+      free(f->description);
+      free(f);
+    }
+    sequence_dispose(&(tp->fields));
+  }
+}
+
+void freeNamedType(table * t)
+{
+  int pos;
+  type_descriptor * td;
+
+  for(pos = 0 ; pos < t->keys.position; pos++) {
+    free((char *)t->keys.array[pos]);
+    td = (type_descriptor*)t->values.array[pos];
+    freeType(td);
+    free(td);
+  }
+}
+
+void freeTypes(sequence *t) 
+{
+  int pos, pos2;
+  type_descriptor *tp;
+  field *f;
+
+  for(pos = 0 ; pos < t->position; pos++) {
+    tp = (type_descriptor *)t->array[pos];
+    freeType(tp);
+    free(tp);
+  }
+}
+
+void freeEvents(sequence *t) 
+{
+  int pos;
+  event *ev;
+
+  for(pos = 0 ; pos < t->position; pos++) {
+    ev = (event *) t->array[pos];
+    free(ev->name);
+    free(ev->description);
+    free(ev);
+  }
+
+}
+
+
+/* Extensible array */
+
+void sequence_init(sequence *t) 
+{
+  t->size = 10;
+  t->position = 0;
+  t->array = (void **)memAlloc(t->size * sizeof(void *));
+}
+
+void sequence_dispose(sequence *t) 
+{
+  t->size = 0;
+  free(t->array);
+  t->array = NULL;
+}
+
+void sequence_push(sequence *t, void *elem) 
+{
+  void **tmp;
+
+  if(t->position >= t->size) {
+    tmp = t->array;
+    t->array = (void **)memAlloc(t->size * 2 * sizeof(void *));
+    memcpy(t->array, tmp, t->size * sizeof(void *));
+    t->size = t->size * 2;
+    free(tmp);
+  }
+  t->array[t->position] = elem;
+  t->position++;
+}
+
+void *sequence_pop(sequence *t) 
+{
+  return t->array[t->position--];
+}
+
+
+/* Hash table API, implementation is just linear search for now */
+
+void table_init(table *t) 
+{
+  sequence_init(&(t->keys));
+  sequence_init(&(t->values));
+}
+
+void table_dispose(table *t) 
+{
+  sequence_dispose(&(t->keys));
+  sequence_dispose(&(t->values));
+}
+
+void table_insert(table *t, char *key, void *value) 
+{
+  sequence_push(&(t->keys),key);
+  sequence_push(&(t->values),value);
+}
+
+void *table_find(table *t, char *key) 
+{
+  int pos;
+  for(pos = 0 ; pos < t->keys.position; pos++) {
+    if(strcmp((char *)key,(char *)t->keys.array[pos]) == 0)
+      return(t->values.array[pos]);
+  }
+  return NULL;
+}
+
+void table_insert_int(table *t, int *key, void *value)
+{
+  sequence_push(&(t->keys),key);
+  sequence_push(&(t->values),value);
+}
+
+void *table_find_int(table *t, int *key)
+{
+  int pos;
+  for(pos = 0 ; pos < t->keys.position; pos++) {
+    if(*key == *(int *)t->keys.array[pos])
+      return(t->values.array[pos]);
+  }
+  return NULL;
+}
+
+
+/* Concatenate strings */
+
+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);  
+  return tmp;
+}
+
+
diff --git a/genevent/parser.h b/genevent/parser.h
new file mode 100644 (file)
index 0000000..6612077
--- /dev/null
@@ -0,0 +1,206 @@
+#ifndef PARSER_H
+#define PARSER_H
+
+/* Extensible array container */
+
+typedef struct _sequence {
+  int size;
+  int position;
+  void **array;
+} sequence;
+
+void sequence_init(sequence *t);
+void sequence_dispose(sequence *t);
+void sequence_push(sequence *t, void *elem);
+void *sequence_pop(sequence *t);
+
+
+/* Hash table */
+
+typedef struct _table {
+  sequence keys;
+  sequence values;
+} table;
+
+void table_init(table *t);
+void table_dispose(table *t);
+void table_insert(table *t, char *key, void *value);
+void *table_find(table *t, char *key);
+void table_insert_int(table *t, int *key, void *value);
+void *table_find_int(table *t, int *key);
+
+
+/* Token types */
+
+typedef enum _token_type {
+  ENDFILE,
+  FORWARDSLASH,
+  LANGLEBRACKET,
+  RANGLEBRACKET,
+  EQUAL,
+  QUOTEDSTRING,
+  NUMBER,
+  NAME
+} token_type;
+
+
+/* State associated with a file being parsed */
+typedef struct _parse_file {
+  char *name;
+  FILE * fp;
+  int lineno;
+  char *buffer;
+  token_type type; 
+  int unget;
+  void (*error) (struct _parse_file *, char *);
+} parse_file;
+
+void ungetToken(parse_file * in);
+char *getToken(parse_file *in);
+char *getForwardslash(parse_file *in);
+char *getLAnglebracket(parse_file *in);
+char *getRAnglebracket(parse_file *in);
+char *getQuotedString(parse_file *in);
+char *getName(parse_file *in);
+int   getNumber(parse_file *in);
+char *getEqual(parse_file *in);
+char  seekNextChar(parse_file *in);
+
+void skipComment(parse_file * in);
+void skipEOL(parse_file * in);
+int isalpha(char car);
+int isalnum(char car);
+
+/* Some constants */
+
+static const int BUFFER_SIZE = 1024;
+
+
+/* Events data types */
+
+typedef enum _data_type {
+  INT,
+  UINT,
+  FLOAT,
+  STRING,
+  ENUM,
+  ARRAY,
+  SEQUENCE,
+  STRUCT,
+  UNION,
+  NONE
+} data_type;
+
+
+/* Event type descriptors */
+
+typedef struct _type_descriptor {
+  char * type_name; //used for named type
+  data_type type;
+  char *fmt;
+  int size;
+  sequence labels; // for enumeration
+  sequence fields; // for structure
+  struct _type_descriptor *nested_type; // for array and sequence 
+} type_descriptor;
+
+
+/* Fields within types */
+
+typedef struct _field{
+  char *name;
+  char *description;
+  type_descriptor *type;
+} field;
+
+
+/* Events definitions */
+
+typedef struct _event {  
+  char *name;
+  char *description;
+  type_descriptor *type; 
+} event;
+
+typedef struct _facility {
+  char * name;
+  char * description;
+  sequence events;
+  sequence unnamed_types;
+  table named_types;
+} facility;
+
+int getSize(parse_file *in);
+unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor * type);
+
+void parseFacility(parse_file *in, facility * fac);
+void parseEvent(parse_file *in, event *ev, sequence * unnamed_types, table * named_types);
+void parseTypeDefinition(parse_file *in, sequence * unnamed_types, table * named_types);
+type_descriptor *parseType(parse_file *in, type_descriptor *t, sequence * unnamed_types, table * named_types);
+void parseFields(parse_file *in, type_descriptor *t, sequence * unnamed_types, table * named_types);
+void checkNamedTypesImplemented(table * namedTypes);
+type_descriptor * find_named_type(char *name, table * named_types);
+void generateChecksum(char * facName, unsigned long * checksum, sequence * events);
+
+
+/* get attributes */
+char * getNameAttribute(parse_file *in);
+char * getFormatAttribute(parse_file *in);
+int    getSizeAttribute(parse_file *in);
+int    getValueAttribute(parse_file *in);
+char * getValueStrAttribute(parse_file *in);
+
+char * getDescription(parse_file *in);
+
+
+static char *intOutputTypes[] = {
+  "int8_t", "int16_t", "int32_t", "int64_t", "short int", "int", "long int" };
+
+static char *uintOutputTypes[] = {
+  "uint8_t", "uint16_t", "uint32_t", "uint64_t", "unsigned short int", 
+  "unsigned int", "unsigned long int" };
+
+static char *floatOutputTypes[] = {
+  "undef", "undef", "float", "double", "undef", "float", "double" };
+
+
+/* Dynamic memory allocation and freeing */
+
+void * memAlloc(int size);
+char *allocAndCopy(char * str);
+char *appendString(char *s, char *suffix);
+void freeTypes(sequence *t);
+void freeType(type_descriptor * td);
+void freeEvents(sequence *t);
+void freeNamedType(table * t);
+void error_callback(parse_file *in, char *msg);
+
+
+//checksum part
+static const unsigned int crctab32[] =
+{
+#include "crc32.tab"
+};
+
+static inline unsigned long
+partial_crc32_one(unsigned char c, unsigned long crc)
+{
+  return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8);
+}
+
+static inline unsigned long
+partial_crc32(const char *s, unsigned long crc)
+{
+  while (*s)
+    crc = partial_crc32_one(*s++, crc);
+  return crc;
+}
+
+static inline unsigned long
+crc32(const char *s)
+{
+  return partial_crc32(s, 0xffffffff) ^ 0xffffffff;
+}
+
+
+#endif // PARSER_H
This page took 0.048005 seconds and 4 git commands to generate.