More constification of arrays.
[obnox/wireshark/wip.git] / ethereal_gen.py
index 25378f3c448aa1b2222f8b145092af06aa204905..2dacf1eaf4b844361ab6f751bb7aa65c7036552e 100644 (file)
@@ -1,7 +1,6 @@
 # -*- python -*-
 #
-# $Id: ethereal_gen.py,v 1.7 2001/07/20 09:50:08 guy Exp $
-#
+# $Id$
 #                           
 # ethereal_gen.py (part of idl2eth)           
 #
@@ -10,7 +9,7 @@
 #    Copyright (C) 2001 Frank Singleton, Ericsson Inc.
 #
 #  This file is a backend to "omniidl", used to generate "Ethereal"
-#  dissectors from IDL descriptions. The output language generated
+#  dissectors from CORBA IDL descriptions. The output language generated
 #  is "C". It will generate code to use the GIOP/IIOP get_CDR_XXX API.
 #
 #  Please see packet-giop.h in Ethereal distro for API description.
@@ -38,7 +37,7 @@
 #   
 #   Omniidl Back-end which parses an IDL list of "Operation" nodes
 #   passed from ethereal_be2.py and generates "C" code for compiling
-#   as a dissector in Ethereal IP protocol anlayser.
+#   as a plugin for the  Ethereal IP Protocol Analyser.
 #
 #
 # Strategy (sneaky but ...)
@@ -84,13 +83,21 @@ import tempfile
 # 6. Handle pragmas.
 # 7. Exception can be common to many operations, so handle them outside the
 #    operation helper functions [done]
-# 8. Automatic variable declaration [done, improve]
+# 8. Automatic variable declaration [done, improve, still get some collisions.add variable delegator function ]
+#    For example, mutlidimensional arrays.
 # 9. wchar and wstring handling [giop API needs improving]
 # 10. Support Fixed [done]
-# 11. Support attributes (get/set)
-# 12. Implement IDL "union" code
-# 13. Implement support for plugins
+# 11. Support attributes (get/set) [started, needs language mapping option, perhaps ethereal GUI option
+#     to set the attribute function prefix or suffix ? ] For now the prefix is "_get" and "_set"
+#     eg: attribute string apple  =>   _get_apple and _set_apple
 #
+# 12. Implement IDL "union" code [done]
+# 13. Implement support for plugins [done]
+# 14. Dont generate code for empty operations (cf: exceptions without members)
+# 15. Generate code to display Enums numerically and symbolically [done]
+# 16. Place structs/unions in subtrees
+# 17. Recursive struct and union handling [done ]
+# 18. Improve variable naming for display (eg: structs, unions etc)
 #
 # Also test, Test, TEST
 #
@@ -99,11 +106,12 @@ import tempfile
 
 #
 #   Strategy:
-#
-#    For return val and all parameters do
+#    For every operation and attribute do
+#       For return val and all parameters do
 #       find basic IDL type for each parameter
 #       output get_CDR_xxx
-#    output exception handling code
+#       output exception handling code
+#       output attribute handling code
 #
 #
 
@@ -133,6 +141,8 @@ class ethereal_gen_C:
     c_seq         = "gchar   *seq = NULL;"          # pointer to buffer of gchars
     c_i           = "guint32   i_";                 # loop index
     c_i_lim       = "guint32   u_octet4_loop_";     # loop limit
+    c_u_disc      = "guint32   disc_u_";            # unsigned int union discriminant variable name (enum)
+    c_s_disc      = "gint32    disc_s_";            # signed int union discriminant variable name (other cases, except Enum)
     
     #
     # Constructor
@@ -161,15 +171,21 @@ class ethereal_gen_C:
     #
     #
         
-    def genCode(self,oplist):
+    def genCode(self,oplist, atlist, enlist, stlist, unlist):   # operation,attribute,enums,struct and union lists
 
-        self.genHelpers(oplist)         # sneaky .. call it now, to populate the fn_hash
+
+        self.genHelpers(oplist,stlist,unlist)  # sneaky .. call it now, to populate the fn_hash
                                         # so when I come to that operation later, I have the variables to
                                         # declare already.
                                         
         self.genExceptionHelpers(oplist) # sneaky .. call it now, to populate the fn_hash
-                                        # so when I come to that exception later, I have the variables to
-                                        # declare already.
+                                         # so when I come to that exception later, I have the variables to
+                                         # declare already.
+                                        
+        self.genAttributeHelpers(atlist) # sneaky .. call it now, to populate the fn_hash
+                                         # so when I come to that exception later, I have the variables to
+                                         # declare already.
+                                        
                                                                                 
         self.fn_hash_built = 1          # DONE, so now I know , see genOperation()
 
@@ -178,19 +194,24 @@ class ethereal_gen_C:
         self.genEthCopyright()          # Ethereal Copyright comments.
         self.genGPL()                   # GPL license
         self.genIncludes()
-        self.genDeclares(oplist)
+        self.genDeclares(oplist,atlist,enlist,stlist,unlist)
         self.genProtocol()
         self.genRegisteredFields()
         self.genOpList(oplist)          # string constant declares for operation names
         self.genExList(oplist)          # string constant declares for user exceptions
+        self.genAtList(atlist)          # string constant declares for Attributes
+        self.genEnList(enlist)          # string constant declares for Enums
         
         
         self.genExceptionHelpers(oplist)   # helper function to decode user exceptions that have members
         self.genExceptionDelegator(oplist) # finds the helper function to decode a user exception
-        self.genHelpers(oplist)
+        self.genAttributeHelpers(atlist)   # helper function to decode "attributes"
+
+        self.genHelpers(oplist,stlist,unlist)  # operation, struct and union decode helper functions
 
         self.genMainEntryStart(oplist)
-        self.genDelegator(oplist)
+        self.genOpDelegator(oplist)
+        self.genAtDelegator(atlist)        
         self.genMainEntryEnd()
 
         self.gen_proto_register()
@@ -255,15 +276,39 @@ class ethereal_gen_C:
             
         self.st.out(self.template_Includes)
                                 
+
     #
     # denDeclares
     #
+    # generate function prototypes if required
     #
+    # Currently this is used for struct and union helper function declarations.
+    #
+    
     
-    def genDeclares(self,oplist):
+    def genDeclares(self,oplist,atlist,enlist,stlist,unlist):
         if self.DEBUG:
             print "XXX genDeclares"
 
+        # struct prototypes
+        
+        self.st.out(self.template_prototype_struct_start)        
+        for st in stlist:
+            #print st.repoId()
+            sname = self.namespace(st, "_")   
+
+            self.st.out(self.template_prototype_struct_body, stname=st.repoId(),name=sname)        
+        self.st.out(self.template_prototype_struct_end)        
+
+        # union prototypes
+
+        self.st.out(self.template_prototype_union_start)        
+        for un in unlist:
+            sname = self.namespace(un, "_")   
+            self.st.out(self.template_prototype_union_body, unname=un.repoId(),name=sname)        
+        self.st.out(self.template_prototype_union_end)        
+                        
+
 
                                 
     #
@@ -308,7 +353,6 @@ class ethereal_gen_C:
         self.st.dec_indent()
         self.st.out(self.template_main_dissector_switch_msgtype_all_other_msgtype)             
         self.st.dec_indent()
-        #self.st.out(self.template_main_dissector_switch_msgtype_end)              
         self.st.out(self.template_main_dissector_end)        
         
 
@@ -365,6 +409,90 @@ class ethereal_gen_C:
     
         self.st.out(self.template_comment_user_exceptions_string_declare_end)
 
+    #
+    # genAtList
+    #
+    # in: atlist
+    #
+    # out: C code for IDL attribute decalarations.
+    #
+    # NOTE: Mapping of attributes to  operation(function) names is tricky.
+    #
+    # The actual accessor function names are language-mapping specific. The attribute name
+    # is subject to OMG IDL's name scoping rules; the accessor function names are 
+    # guaranteed not to collide with any legal operation names specifiable in OMG IDL.
+    #
+    # eg:
+    #
+    # static const char get_Penguin_Echo_get_width_at[] = "get_width" ;
+    # static const char set_Penguin_Echo_set_width_at[] = "set_width" ;
+    #
+    # or:
+    #
+    # static const char get_Penguin_Echo_get_width_at[] = "_get_width" ;
+    # static const char set_Penguin_Echo_set_width_at[] = "_set_width" ;
+    #    
+    # TODO: Implement some language dependant templates to handle naming conventions
+    #       language <=> attribute. for C, C++. Java etc
+    #
+    # OR, just add a runtime GUI option to select language binding for attributes -- FS
+    #
+    #
+    #
+    # ie: def genAtlist(self,atlist,language) 
+    #
+    
+    
+
+    def genAtList(self,atlist):
+        self.st.out(self.template_comment_attributes_start)        
+
+        for n in atlist:
+            for i in n.declarators():   # 
+                sname = self.namespace(i, "_")   
+                atname = i.identifier()
+                self.st.out(self.template_attributes_declare_Java_get, sname=sname, atname=atname)
+                if not n.readonly():
+                    self.st.out(self.template_attributes_declare_Java_set, sname=sname, atname=atname)
+    
+        self.st.out(self.template_comment_attributes_end)
+
+
+    #
+    # genEnList
+    #
+    # in: enlist
+    #
+    # out: C code for IDL Enum decalarations using "static const value_string" template
+    #
+
+    
+
+    def genEnList(self,enlist):
+        
+        self.st.out(self.template_comment_enums_start)        
+
+        for enum in enlist:
+            sname = self.namespace(enum, "_")
+            
+            self.st.out(self.template_comment_enum_comment, ename=enum.repoId())
+            self.st.out(self.template_value_string_start, valstringname=sname)
+            for enumerator in enum.enumerators():
+                self.st.out(self.template_value_string_entry, intval=str(self.valFromEnum(enum,enumerator)), description=enumerator.identifier())
+                
+                
+            #atname = n.identifier()
+            self.st.out(self.template_value_string_end, valstringname=sname)
+    
+        self.st.out(self.template_comment_enums_end)
+
+
+
+
+
+
+
+
 
 
     #
@@ -400,6 +528,88 @@ class ethereal_gen_C:
         self.st.out(self.template_main_exception_delegator_end)
 
 
+    #
+    # genAttribueHelpers()
+    #
+    # Generate private helper functions to decode Attributes.
+    #
+    # in: atlist
+    #
+    # For readonly attribute - generate get_xxx()
+    # If NOT readonly attribute - also generate set_xxx()
+    #    
+        
+    def genAttributeHelpers(self,atlist):        
+        if self.DEBUG:
+            print "XXX genAttributeHelpers: atlist = ", atlist
+
+        self.st.out(self.template_attribute_helpers_start)
+        
+        for attrib in atlist:
+            for decl in attrib.declarators():
+                self.genAtHelper(attrib,decl,"get") # get accessor
+                if not attrib.readonly():
+                    self.genAtHelper(attrib,decl,"set") # set accessor
+                    
+        self.st.out(self.template_attribute_helpers_end)
+
+    #
+    # genAtHelper() 
+    #
+    # Generate private helper functions to decode an attribute
+    #
+    # in: at - attribute node
+    # in: decl - declarator belonging to this attribute 
+    # in: order - to generate a "get" or "set" helper
+    
+    def genAtHelper(self,attrib,decl,order):
+        if self.DEBUG:
+            print "XXX genAtHelper"
+            
+        sname = order + "_" + self.namespace(decl, "_")  # must use set or get prefix to avoid collision
+        self.curr_sname = sname                    # update current opnode/exnode scoped name
+                                                
+        if not self.fn_hash_built:
+            self.fn_hash[sname] = []        # init empty list as val for this sname key
+                                            # but only if the fn_hash is not already built
+
+        self.st.out(self.template_attribute_helper_function_start, sname=sname, atname=decl.repoId())
+        self.st.inc_indent()
+
+        self.st.out(self.template_helper_function_vars_start)
+        self.dumpCvars(sname)
+        self.st.out(self.template_helper_function_vars_end )
+        
+        self.st.out(self.template_exception_helper_function_get_endianess)
+
+        #
+        # TODO - attributes are simple types, so remove array handling
+        #
+                
+        if decl.sizes():        # an array
+            indices = self.get_indices_from_sizes(decl.sizes())
+            string_indices = '%i ' % indices # convert int to string
+            self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)     
+            self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
+            self.addvar(self.c_i + decl.identifier() + ";")
+            
+            self.st.inc_indent()
+
+            self.getCDR3(attrib.attrType(), decl.identifier() )
+                    
+            self.st.dec_indent()
+            self.st.out(self.template_get_CDR_array_end)
+                    
+                    
+        else:
+
+            self.getCDR3(attrib.attrType(), decl.identifier() )
+
+        self.st.dec_indent()
+        self.st.out(self.template_attribute_helper_function_end)
+
+
+
     #
     # genExceptionHelpers()
     #
@@ -483,15 +693,21 @@ class ethereal_gen_C:
     # genHelpers()
     #
     # Generate private helper functions for each IDL operation.
+    # Generate private helper functions for each IDL struct.
+    # Generate private helper functions for each IDL union.
     #
-    # in: oplist
+    #
+    # in: oplist, stlist, unlist
     #
     
         
-    def genHelpers(self,oplist):
+    def genHelpers(self,oplist,stlist,unlist):
         for op in oplist:
             self.genOperation(op)
-
+        for st in stlist:
+            self.genStructHelper(st)
+        for un in unlist:
+            self.genUnionHelper(un)
 
     #
     # genOperation()
@@ -571,7 +787,6 @@ class ethereal_gen_C:
     #
     # Decode function parameters for a GIOP request message
     #
-    # TODO check for enum
     #
     
     def genOperationRequest(self,opnode):
@@ -588,7 +803,6 @@ class ethereal_gen_C:
     #
     # Decode function parameters for a GIOP reply message
     #
-    # TODO check for enum
 
     
     def genOperationReply(self,opnode):
@@ -603,10 +817,11 @@ class ethereal_gen_C:
 
         if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
             #self.getCDR3(rt.decl().alias().aliasType(),"dummy")    # return value maybe a typedef
-            self.get_CDR_alias(rt, "Operation Return Value" )
+            #self.get_CDR_alias(rt, "Operation_Return_Value" )
+            self.get_CDR_alias(rt, rt.name() )
                                
         else:            
-            self.getCDR3(rt, "Operation Return Value")    # return value is NOT an alias
+            self.getCDR3(rt, "Operation_Return_Value")    # return value is NOT an alias
               
         for p in opnode.parameters():
             if p.is_out():              # out or inout
@@ -621,12 +836,29 @@ class ethereal_gen_C:
                 for m in ex.members():
                     t=0
                     #print m.memberType(), m.memberType().kind()
-               
-    def genDelegator(self,oplist):
+    #
+    # Delegator for Operations
+    #
+    
+    def genOpDelegator(self,oplist):
         for op in oplist:
             opname = op.identifier()
             sname = self.namespace(op, "_")
-            self.st.out(self.template_delegate_code, opname=opname, sname=sname)
+            self.st.out(self.template_op_delegate_code, sname=sname)
+
+    #
+    # Delegator for Attributes
+    #
+
+    def genAtDelegator(self,atlist):            
+        for a in atlist:
+            for i in a.declarators():
+                atname = i.identifier()
+                sname = self.namespace(i, "_")
+                self.st.out(self.template_at_delegate_code_get, sname=sname)
+                if not a.readonly():
+                    self.st.out(self.template_at_delegate_code_set, sname=sname)
 
     #
     # Add a variable declaration to the hash of list
@@ -657,7 +889,22 @@ class ethereal_gen_C:
                 self.st.out(v)
 
 
-            
+    #
+    # Given an enum node, and a enumerator node, return 
+    # the enumerator's numerical value.
+    #
+    # eg: enum Color {red,green,blue} should return
+    # val = 1 for green
+    #
+                
+    def valFromEnum(self,enumNode, enumeratorNode):
+        if self.DEBUG:
+            print "XXX valFromEnum, enumNode = ", enumNode, " from ", enumNode.repoId()
+            print "XXX valFromEnum, enumeratorNode = ", enumeratorNode, " from ", enumeratorNode.repoId()
+
+        if isinstance(enumeratorNode,idlast.Enumerator):
+            value = enumNode.enumerators().index(enumeratorNode)
+            return value
                 
 
 ## tk_null               = 0
@@ -743,24 +990,27 @@ class ethereal_gen_C:
             self.get_CDR_wchar(pn)            
         elif pt ==  idltype.tk_enum:
             #print type.decl()
-            self.get_CDR_enum(pn)
+            self.get_CDR_enum(pn,type)
+            #self.get_CDR_enum(pn)
+            
         elif pt ==  idltype.tk_struct:
             self.get_CDR_struct(type,pn)
         elif pt ==  idltype.tk_TypeCode: # will I ever get here ?
-            self.get_CDR_TypeCode(type,pn)
+            self.get_CDR_TypeCode(pn)
         elif pt == idltype.tk_sequence:
             self.get_CDR_sequence(type,pn)
         elif pt == idltype.tk_objref:
             self.get_CDR_objref(type,pn)
         elif pt == idltype.tk_array:
             self.get_CDR_array(type,pn)
+        elif pt == idltype.tk_union:
+            self.get_CDR_union(type,pn)            
         elif pt == idltype.tk_alias:
             if self.DEBUG:
                 print "XXXXX Alias type XXXXX " , type
             self.get_CDR_alias(type,pn)            
         else:
-            if self.DEBUG:
-                print "XXXXX Unknown type XXXXX " , pt
+            self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
 
 
     #
@@ -825,8 +1075,12 @@ class ethereal_gen_C:
     def get_CDR_any(self,pn):
         self.st.out(self.template_get_CDR_any, varname=pn)
 
-    def get_CDR_enum(self,pn):
-        self.st.out(self.template_get_CDR_enum, varname=pn)
+    def get_CDR_enum(self,pn,type):
+        #self.st.out(self.template_get_CDR_enum, varname=pn)
+        sname = self.namespace(type.unalias(), "_")
+        self.st.out(self.template_get_CDR_enum_symbolic, valstringarray=sname)
+
+
         self.addvar(self.c_u_octet4)
 
     def get_CDR_string(self,pn):
@@ -856,6 +1110,179 @@ class ethereal_gen_C:
         self.addvar(self.c_u_octet4)
 
 
+            
+    def get_CDR_union(self,type,pn):
+        if self.DEBUG:
+            print "XXX Union type =" , type, " pn = ",pn
+            print "XXX Union type.decl()" , type.decl()
+            print "XXX Union Scoped Name" , type.scopedName()
+
+       #  If I am a typedef union {..}; node then find the union node
+        
+        if isinstance(type.decl(), idlast.Declarator):
+            ntype = type.decl().alias().aliasType().decl()           
+        else:
+            ntype = type.decl()         # I am a union node
+
+        if self.DEBUG:    
+            print "XXX Union ntype =" , ntype
+
+        sname = self.namespace(ntype, "_")
+        self.st.out(self.template_union_start, name=sname )
+
+        # Output a call to the union helper function so I can handle recursive union also.
+        
+        self.st.out(self.template_decode_union,name=sname)    
+
+        self.st.out(self.template_union_end, name=sname )
+            
+
+    #
+    # Code to generate Union Helper functions
+    #
+    # in: un - a union node
+    #
+    #
+
+
+    def genUnionHelper(self,un):
+        if self.DEBUG:
+            print "XXX Union type =" , un
+            print "XXX Union type.decl()" , un.decl()
+            print "XXX Union Scoped Name" , un.scopedName()
+
+        sname = self.namespace(un, "_")
+        self.curr_sname = sname         # update current opnode/exnode/stnode/unnode scoped name
+        if not self.fn_hash_built:
+            self.fn_hash[sname] = []        # init empty list as val for this sname key
+                                            # but only if the fn_hash is not already built
+
+        self.st.out(self.template_union_helper_function_start, sname=sname, unname=un.repoId())
+        self.st.inc_indent()
+
+        self.st.out(self.template_helper_function_vars_start)
+        self.dumpCvars(sname)
+        self.st.out(self.template_helper_function_vars_end )
+        
+        self.st.out(self.template_union_helper_function_get_endianess)
+
+        st = un.switchType().unalias() # may be typedef switch type, so find real type
+
+        self.st.out(self.template_comment_union_code_start, uname=un.repoId() )
+
+        self.getCDR3(st,un.identifier());
+
+        # Depending on what kind of discriminant I come accross (enum,integer,char,
+        # short, boolean), make sure I cast the return value of the get_XXX accessor
+        # to an appropriate value. Omniidl idlast.CaseLabel.value() accessor will
+        # return an integer, or an Enumerator object that is then converted to its
+        # integer equivalent.
+        #
+        #
+        # NOTE - May be able to skip some of this stuff, but leave it in for now -- FS
+        #
+
+        if (st.kind() == idltype.tk_enum):
+            std = st.decl()            
+            self.st.out(self.template_comment_union_code_discriminant, uname=std.repoId() )
+            self.st.out(self.template_union_code_save_discriminant_enum, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
+
+        elif (st.kind() == idltype.tk_long):
+            self.st.out(self.template_union_code_save_discriminant_long, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
+
+        elif (st.kind() == idltype.tk_ulong):
+            self.st.out(self.template_union_code_save_discriminant_ulong, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
+
+        elif (st.kind() == idltype.tk_short):
+            self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
+
+        elif (st.kind() == idltype.tk_ushort):
+            self.st.out(self.template_union_code_save_discriminant_ushort, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
+
+        elif (st.kind() == idltype.tk_boolean):
+            self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier()  )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
+            
+        elif (st.kind() == idltype.tk_char):
+            self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
+            self.addvar(self.c_s_disc + un.identifier() + ";")            
+
+        else:
+            print "XXX Unknown st.kind() = ", st.kind()
+            
+        #
+        # Loop over all cases in this union
+        #
+        
+        for uc in un.cases():           # for all UnionCase objects in this union
+            for cl in uc.labels():      # for all Caselabel objects in this UnionCase
+
+                # get integer value, even if discriminant is
+                # an Enumerator node
+                
+                if isinstance(cl.value(),idlast.Enumerator):
+                    if self.DEBUG:
+                        print "XXX clv.identifier()", cl.value().identifier()
+                        print "XXX clv.repoId()", cl.value().repoId()
+                        print "XXX clv.scopedName()", cl.value().scopedName()
+                                            
+                    # find index of enumerator in enum declaration
+                    # eg: RED is index 0 in enum Colors { RED, BLUE, GREEN }
+                    
+                    clv = self.valFromEnum(std,cl.value())
+                    
+                else:
+                    clv = cl.value()
+                    
+                #print "XXX clv = ",clv
+
+                #    
+                # if char, dont convert to int, but put inside single quotes so that it is understood by C.
+                # eg: if (disc == 'b')..
+                #
+                # TODO : handle \xxx chars generically from a function or table lookup rather than
+                #        a whole bunch of "if" statements. -- FS
+                
+                                          
+                if (st.kind() == idltype.tk_char):
+                    if (clv == '\n'):          # newline
+                        string_clv = "'\\n'" 
+                    elif (clv == '\t'):        # tab
+                        string_clv = "'\\t'"
+                    else:
+                        string_clv = "'" + clv + "'"
+                else:                    
+                    string_clv = '%i ' % clv
+
+                #
+                # If default case, then skp comparison with discriminator
+                #
+                
+                if not cl.default():
+                    self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
+                    self.st.inc_indent()       
+                else:
+                    self.st.out(self.template_comment_union_code_label_default_start  )
+                    
+
+                self.getCDR3(uc.caseType(),uc.declarator().identifier())
+
+                if not cl.default():
+                    self.st.dec_indent()       
+                    self.st.out(self.template_comment_union_code_label_compare_end )
+                else:
+                    self.st.out(self.template_comment_union_code_label_default_end  )
+
+        self.st.dec_indent()
+        self.st.out(self.template_union_helper_function_end)
+
+
+
     #
     # Currently, get_CDR_alias is geared to finding typdef 
     #
@@ -894,8 +1321,11 @@ class ethereal_gen_C:
         
         
 
+    #
+    # Handle structs, including recursive
+    #
+    
     def get_CDR_struct(self,type,pn):       
-        self.st.out(self.template_structure_start, name=type.name() )
 
         #  If I am a typedef struct {..}; node then find the struct node
         
@@ -903,8 +1333,44 @@ class ethereal_gen_C:
             ntype = type.decl().alias().aliasType().decl()           
         else:
             ntype = type.decl()         # I am a struct node
-                        
-        for m in ntype.members():
+
+        sname = self.namespace(ntype, "_")
+        self.st.out(self.template_structure_start, name=sname )
+
+        # Output a call to the struct helper function so I can handle recursive structs also.
+        
+        self.st.out(self.template_decode_struct,name=sname)    
+
+        self.st.out(self.template_structure_end, name=sname )
+
+    #
+    # genStructhelper() 
+    #
+    # Generate private helper functions to decode a struct
+    #
+    # in: stnode ( a struct node)
+    #
+    
+    def genStructHelper(self,st):
+        if self.DEBUG:
+            print "XXX genStructHelper"
+            
+        sname = self.namespace(st, "_")
+        self.curr_sname = sname         # update current opnode/exnode/stnode scoped name
+        if not self.fn_hash_built:
+            self.fn_hash[sname] = []        # init empty list as val for this sname key
+                                            # but only if the fn_hash is not already built
+
+        self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
+        self.st.inc_indent()
+
+        self.st.out(self.template_helper_function_vars_start)
+        self.dumpCvars(sname)
+        self.st.out(self.template_helper_function_vars_end )
+        
+        self.st.out(self.template_struct_helper_function_get_endianess)
+
+        for m in st.members():
             for decl in m.declarators():
                 if decl.sizes():        # an array
                     indices = self.get_indices_from_sizes(decl.sizes())
@@ -914,21 +1380,25 @@ class ethereal_gen_C:
                     self.addvar(self.c_i + decl.identifier() + ";")
                     
                     self.st.inc_indent()       
-                    self.getCDR3(m.memberType(), type.name() + "_" + decl.identifier() )                    
+                    self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )                    
                     self.st.dec_indent()
                     self.st.out(self.template_get_CDR_array_end)
                     
                     
-                else:    
-                    self.getCDR3(m.memberType(), type.name() + "_" + decl.identifier() )
+                else:                            
+                    self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
+                            
+        self.st.dec_indent()
+        self.st.out(self.template_struct_helper_function_end)
+
 
-        self.st.out(self.template_structure_end, name=type.name())
 
 
 
     #
     # Generate code to access a sequence of a type
     #
+
     
     def get_CDR_sequence(self,type, pn):
         self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
@@ -937,7 +1407,7 @@ class ethereal_gen_C:
         self.addvar(self.c_i + pn + ";")
 
         self.st.inc_indent()       
-        self.getCDR3(type.unalias().seqType() ) # and start all over with the type
+        self.getCDR3(type.unalias().seqType(), pn ) # and start all over with the type
         self.st.dec_indent()     
         
         self.st.out(self.template_get_CDR_sequence_loop_end)
@@ -1087,7 +1557,23 @@ class ethereal_gen_C:
         return (dignum/2) + 1
 
             
+
+    #
+    # Output some TODO comment
+    #
+    
+
+    def genTODO(self,message):
+        self.st.out(self.template_debug_TODO, message=message)
+
+    #
+    # Output some WARNING comment
+    #
     
+
+    def genWARNING(self,message):
+        self.st.out(self.template_debug_WARNING, message=message)
+            
     #
     # Templates for C code
     #
@@ -1131,7 +1617,7 @@ class ethereal_gen_C:
 
 
     template_helper_function_start = """\
-static void decode_@sname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
+static void decode_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header, gchar *operation _U_) {
 
     gboolean stream_is_big_endian;          /* big endianess */
 """
@@ -1232,7 +1718,11 @@ plugin_reg_handoff(void){
 }
 
 G_MODULE_EXPORT void
-plugin_init(plugin_address_table_t *pat){
+plugin_init(plugin_address_table_t *pat
+#ifndef PLUGINS_NEED_ADDRESS_TABLE
+_U_
+#endif
+){
    /* initialise the table of pointers needed in Win32 DLLs */
    plugin_address_table_init(pat);
    if (proto_@dissector_name@ == -1) {
@@ -1289,7 +1779,7 @@ void proto_register_giop_@dissector_name@(void) {
     # template for delegation code
     #
 
-    template_delegate_code = """\
+    template_op_delegate_code = """\
 if (!strcmp(operation, @sname@_op )) {
    decode_@sname@(tvb, pinfo, tree, offset, header, operation);
    return TRUE;
@@ -1509,18 +1999,18 @@ g_free(seq);          /*  free buffer  */
 seq = NULL;
 
 """
-    
-    
-    template_get_CDR_enum = """\
 
-/* TODO - translate Enum val into symbolic value */
+            
+    template_get_CDR_enum_symbolic = """\
     
 u_octet4 = get_CDR_enum(tvb,offset,stream_is_big_endian, boundary);
 if (tree) {
-   proto_tree_add_text(tree,tvb,*offset-4,4,"Enum value = %u ",u_octet4);
+   proto_tree_add_text(tree,tvb,*offset-4,4,"Enum value = %u (%s)",u_octet4,val_to_str(u_octet4,@valstringarray@,"Unknown Enum Value"));
 }
 """
 
+
+
     template_get_CDR_string = """\
 u_octet4 = get_CDR_string(tvb, &seq, offset, stream_is_big_endian, boundary);
 if (tree) {
@@ -1620,6 +2110,15 @@ for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
 /*  End struct \"@name@\"  */
 """
 
+    template_union_start = """\
+/*  Begin union \"@name@\"  */
+"""
+
+
+    template_union_end = """\
+/*  End union \"@name@\"  */
+"""
+
 
 
 
@@ -1628,8 +2127,7 @@ for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
 #
 
     template_Header = """\
-/*    
- * packet-@dissector_name@-idl.c
+/* packet-@dissector_name@.c
  * Routines for IDL dissection
  *
  * Autogenerated from idl2eth
@@ -1689,29 +2187,18 @@ for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
 #include <stdlib.h>
 #include <gmodule.h>
 
-#ifdef HAVE_SYS_TYPES_H
-# include <sys/types.h>
-#endif
-
-#ifdef HAVE_NETINET_IN_H
-# include <netinet/in.h>
-#endif
-
 #ifdef NEED_SNPRINTF_H
-# ifdef HAVE_STDARG_H
-#  include <stdarg.h>
-# else
-#  include <varargs.h>
-# endif
 # include "snprintf.h"
 #endif
 
 #include <string.h>
 #include <glib.h>
-#include "packet.h"
-#include "proto.h"
+#include <epan/packet.h>
+#include <epan/proto.h>
 #include "packet-giop.h"
 
+#include "plugins/plugin_api_defs.h"
+
 #ifndef __ETHEREAL_STATIC__
 G_MODULE_EXPORT const gchar version[] = "0.0.1";
 #endif
@@ -1724,18 +2211,23 @@ G_MODULE_EXPORT const gchar version[] = "0.0.1";
 #
 
     template_main_dissector_start = """\
-static gboolean dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, gchar *operation, gchar *idlname) {
+static gboolean dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, gchar *operation, gchar *idlname _U_) {
 
     proto_item *ti = NULL;
     proto_tree *tree = NULL;            /* init later, inside if(tree) */
     
     gboolean be;                        /* big endianess */
-    guint32  offset_saved = (*offset);  /* save in case we must back out */
 
-    pinfo->current_proto = \"@disprot@\";
+    if (check_col(pinfo->cinfo, COL_PROTOCOL))
+       col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
 
-    if (check_col(pinfo->fd, COL_PROTOCOL))
-       col_add_str(pinfo->fd, COL_PROTOCOL, \"@disprot@\");
+/* 
+ * Do not clear COL_INFO, as nothing is being written there by 
+ * this dissector yet. So leave it as is from the GIOP dissector.
+ * TODO: add something useful to COL_INFO 
+ *  if (check_col(pinfo->cinfo, COL_INFO))
+ *     col_clear(pinfo->cinfo, COL_INFO);
+ */
 
     if (ptree) {
        ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, tvb_length(tvb) - *offset, FALSE);
@@ -1775,23 +2267,6 @@ break;
 """
 
 
-    template_main_dissector_switch_msgtype_end = """\
-
-
-/*
- * We failed to match ANY operations, so perhaps this is not for us !
- */
-
-(*offset) = offset_saved;       /* be nice */
-
-return FALSE;
-
-
-"""
-
-
-
-    
     template_main_dissector_switch_msgtype_all_other_msgtype = """\
 case CancelRequest:
 case LocateRequest:
@@ -1809,14 +2284,6 @@ default:
 """
 
 
-    template_main_dissector_switch_msgtype_end = """\
-
-   return TRUE;
-
-} /* switch */
-
-"""
-
     template_main_dissector_end = """\
 
     return FALSE;
@@ -1886,9 +2353,9 @@ default:
  *
  */
  
-static gboolean decode_user_exception(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation ) {
+static gboolean decode_user_exception(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header _U_, gchar *operation _U_ ) {
     
-    gboolean be;                        /* big endianess */
+    gboolean be _U_;                        /* big endianess */
 
     
 """
@@ -1951,7 +2418,383 @@ stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
 }
 """
 
+    
+#
+# template for struct helper code
+#
+
+
+    template_struct_helper_function_start = """\
+
+/* Struct = @stname@ */
+
+static void decode_@sname@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
+
+    gboolean stream_is_big_endian;          /* big endianess */
+"""
+
+
+
+
+    #
+    # Template for the helper function
+    # to get stream endianess from header
+    #
+
+    template_struct_helper_function_get_endianess = """\
+
+stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
+
+"""
+
+    
+    template_struct_helper_function_end = """\
+}
+"""
+
+#
+# template for union helper code
+#
+
+
+    template_union_helper_function_start = """\
+
+/* Union = @unname@ */
+
+static void decode_@sname@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
+
+    gboolean stream_is_big_endian;          /* big endianess */
+"""
+
+
+
+
+    #
+    # Template for the helper function
+    # to get stream endianess from header
+    #
+
+    template_union_helper_function_get_endianess = """\
+
+stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
+
+"""
+
+    
+    template_union_helper_function_end = """\
+}
+"""
+
+
+
+#-------------------------------------------------------------#
+#             Value string  templates                         #
+#-------------------------------------------------------------#
+
+    template_value_string_start = """\
+static const value_string @valstringname@[] = {
+"""
+
+    template_value_string_entry = """\
+   { @intval@, \"@description@\" }, """    
+    
+    template_value_string_end = """\
+   { 0,       NULL },
+};
+    
+"""
+
+    
+
+#-------------------------------------------------------------#
+#             Enum   handling templates                       #
+#-------------------------------------------------------------#
+
+    template_comment_enums_start = """\
+/*
+ * IDL Enums Start
+ */
+ """
+    
+    template_comment_enums_end = """\
+/*
+ * IDL Enums End
+ */
+ """
+    
+    template_comment_enum_comment = """\
+/*
+ * Enum = @ename@
+ */
+ """
+
+
+
+#-------------------------------------------------------------#
+#             Attribute handling templates                    #
+#-------------------------------------------------------------#
+
+
+    template_comment_attributes_start = """\
+/*
+ * IDL Attributes Start
+ */
+ """
+
+    #
+    # get/set accessor method names are language mapping dependant.
+    #
+
+    template_attributes_declare_Java_get = """static const char get_@sname@_at[] = \"_get_@atname@\" ;"""
+    template_attributes_declare_Java_set = """static const char set_@sname@_at[] = \"_set_@atname@\" ;"""
+
+    template_comment_attributes_end = """
+/*
+ * IDL Attributes End
+ */
+"""
+
+
+    #
+    # template for Attribute delegation code
+    #
+    # Note: _get_xxx() should only be called for Reply with NO_EXCEPTION
+    # Note: _set_xxx() should only be called for Request
+    #
+    #
+
+    template_at_delegate_code_get = """\
+if (!strcmp(operation, get_@sname@_at ) && (header->message_type == Reply) && (header->rep_status == NO_EXCEPTION) ) {
+   decode_get_@sname@_at(tvb, pinfo, tree, offset, header, operation);
+   return TRUE;
+}
+"""
+    
+    template_at_delegate_code_set = """\
+if (!strcmp(operation, set_@sname@_at ) && (header->message_type == Request) ) {
+   decode_set_@sname@_at(tvb, pinfo, tree, offset, header, operation);
+   return TRUE;
+}
+"""
+        
+
+
+    template_attribute_helpers_start = """\
+/*  Begin Attribute Helper Functions  */
+
+"""
+    
+    template_attribute_helpers_end = """\
+    
+/*  End Attribute Helper Functions  */
+
+"""
+
+#
+# template for attribute helper code
+#
+
+
+    template_attribute_helper_function_start = """\
+
+/* Attribute = @atname@ */
+
+static void decode_@sname@_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
+
+    gboolean stream_is_big_endian;          /* big endianess */
+"""
+
+
+
+    #
+    # Template for the helper function
+    # to get stream endianess from header
+    #
+
+    template_attribute_helper_function_get_endianess = """\
+
+stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
+
+"""
+
+    
+    template_attribute_helper_function_end = """\
+}
+"""
+
+    
+#-------------------------------------------------------------#
+#                     Debugging  templates                    #
+#-------------------------------------------------------------#
+
+
+    #
+    # Template for outputting TODO "C" comments
+    # so user know I need ti improve something.
+    #
+
+    template_debug_TODO = """\
+
+/* TODO - @message@ */
+"""
+
+    #
+    # Template for outputting WARNING "C" comments
+    # so user know if I have found a problem.
+    #
+
+    template_debug_WARNING = """\
+
+/* WARNING - @message@ */
+"""
+
+
+    
+#-------------------------------------------------------------#
+#                     IDL Union  templates                    #
+#-------------------------------------------------------------#
+
 
 
+    template_comment_union_code_start = """\
+/*
+ * IDL Union Start - @uname@
+ */
+ """
 
+
+    template_comment_union_code_end = """
+/*
+ * IDL union End - @uname@
+ */
+"""
+
+    template_comment_union_code_discriminant = """\
+/*
+ * IDL Union - Discriminant - @uname@
+ */
+ """
+
+    #
+    # Cast Unions types to something appropriate
+    # Enum value cast to guint32, all others cast to gint32 
+    # as omniidl accessor returns integer or Enum.
+    #
+
+    template_union_code_save_discriminant_enum = """\
+disc_s_@discname@ = (gint32) u_octet4;     /* save Enum Value  discriminant and cast to gint32 */
+"""
+    
+    template_union_code_save_discriminant_long = """\
+disc_s_@discname@ = (gint32) s_octet4;     /* save gint32 discriminant and cast to gint32 */
+"""
+
+    template_union_code_save_discriminant_ulong = """\
+disc_s_@discname@ = (gint32) u_octet4;     /* save guint32 discriminant and cast to gint32 */
+"""    
+
+    template_union_code_save_discriminant_short = """\
+disc_s_@discname@ = (gint32) s_octet2;     /* save gint16 discriminant and cast to gint32 */
+"""    
+
+    template_union_code_save_discriminant_ushort = """\
+disc_s_@discname@ = (gint32) u_octet2;     /* save guint16 discriminant and cast to gint32 */
+"""    
+    
+    template_union_code_save_discriminant_char = """\
+disc_s_@discname@ = (gint32) u_octet1;     /* save guint1 discriminant and cast to gint32 */
+"""
+    
+    template_union_code_save_discriminant_boolean = """\
+disc_s_@discname@ = (gint32) u_octet1;     /* save guint1 discriminant and cast to gint32 */
+"""
+
+
+    template_comment_union_code_label_compare_start = """\
+if (disc_s_@discname@ == @labelval@) {
+
+ """
+    template_comment_union_code_label_compare_end = """\
+    return;     /* End Compare for this discriminant type */
+}
+
+ """
+
+
+    template_comment_union_code_label_default_start = """
+/* Default Union Case Start */
+"""
+
+    template_comment_union_code_label_default_end = """\
+/* Default Union Case End */
+ """
+
+    #
+    # Templates for function prototypes.
+    # This is used in genDeclares() for declaring function prototypes
+    # for structs and union helper functions.
+    #
+
+    template_prototype_struct_start = """
+/* Struct prototype declaration Start */
+"""
+    
+    template_prototype_struct_end = """
+/* Struct prototype declaration End */
+"""
+    
+    template_prototype_struct_body = """
+        
+/* Struct = @stname@ */
+
+static void decode_@name@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
+
+"""
+
+    template_decode_struct = """
+        
+decode_@name@_st(tvb, pinfo, tree, offset, header, operation);
+
+"""
+
+
+    
+    
+    template_prototype_union_start = """
+/* Union prototype declaration Start */
+"""
+    
+    template_prototype_union_end = """
+/* Union prototype declaration End */
+"""
+    
+    template_prototype_union_body = """
+        
+/* Union = @unname@ */
+
+static void decode_@name@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
+
+"""
+
+    template_decode_union = """
+        
+decode_@name@_un(tvb, pinfo, tree, offset, header, operation);
+
+"""
+