Set the svn:eol-style property on all text files to "native", so that
[obnox/wireshark/wip.git] / ethereal_gen.py
1 # -*- python -*-
2 #
3 # $Id$
4 #                           
5 # ethereal_gen.py (part of idl2eth)           
6 #
7 # Author : Frank Singleton (frank.singleton@ericsson.com)
8 #
9 #    Copyright (C) 2001 Frank Singleton, Ericsson Inc.
10 #
11 #  This file is a backend to "omniidl", used to generate "Ethereal"
12 #  dissectors from CORBA IDL descriptions. The output language generated
13 #  is "C". It will generate code to use the GIOP/IIOP get_CDR_XXX API.
14 #
15 #  Please see packet-giop.h in Ethereal distro for API description.
16 #  Ethereal is available at http://www.ethereal.com/
17 #
18 #  Omniidl is part of the OmniOrb distribution, and is available at
19 #  http://www.uk.research.att.com/omniORB/omniORB.html
20 #
21 #  This program is free software; you can redistribute it and/or modify it
22 #  under the terms of the GNU General Public License as published by
23 #  the Free Software Foundation; either version 2 of the License, or
24 #  (at your option) any later version.
25 #
26 #  This program is distributed in the hope that it will be useful,
27 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
28 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29 #  General Public License for more details.
30 #
31 #  You should have received a copy of the GNU General Public License
32 #  along with this program; if not, write to the Free Software
33 #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
34 #  02111-1307, USA.
35 #
36 # Description:
37 #   
38 #   Omniidl Back-end which parses an IDL list of "Operation" nodes
39 #   passed from ethereal_be2.py and generates "C" code for compiling
40 #   as a plugin for the  Ethereal IP Protocol Analyser.
41 #
42 #
43 # Strategy (sneaky but ...)
44 #
45 # problem: I dont know what variables to declare until AFTER the helper functions
46 # have been built, so ...
47 #
48 # There are 2 passes through genHelpers, the first one is there just to
49 # make sure the fn_hash data struct is populated properly.
50 # The second pass is the real thing, generating code and declaring
51 # variables (from the 1st pass) properly.
52 #
53
54
55 """Ethereal IDL compiler back-end."""
56
57 from omniidl import idlast, idltype, idlutil, output
58 import sys, string
59 import tempfile
60
61 #
62 # Output class, generates "C" src code for the sub-dissector 
63 #
64 # in:
65 #
66 #
67 # self - me
68 # st   - output stream
69 # node - a reference to an Operations object.
70 # name - scoped name (Module::Module::Interface:: .. ::Operation
71 #
72
73
74
75 #
76 # TODO -- FS
77
78 # 1. generate hf[] data for searchable fields (but what is searchable?)
79 # 2. add item instead of add_text()
80 # 3. sequence handling [done]
81 # 4. User Exceptions [done]
82 # 5. Fix arrays, and structs containing arrays [done]
83 # 6. Handle pragmas.
84 # 7. Exception can be common to many operations, so handle them outside the
85 #    operation helper functions [done]
86 # 8. Automatic variable declaration [done, improve, still get some collisions.add variable delegator function ]
87 #    For example, mutlidimensional arrays.
88 # 9. wchar and wstring handling [giop API needs improving]
89 # 10. Support Fixed [done]
90 # 11. Support attributes (get/set) [started, needs language mapping option, perhaps ethereal GUI option
91 #     to set the attribute function prefix or suffix ? ] For now the prefix is "_get" and "_set"
92 #     eg: attribute string apple  =>   _get_apple and _set_apple
93 #
94 # 12. Implement IDL "union" code [done]
95 # 13. Implement support for plugins [done]
96 # 14. Dont generate code for empty operations (cf: exceptions without members)
97 # 15. Generate code to display Enums numerically and symbolically [done]
98 # 16. Place structs/unions in subtrees
99 # 17. Recursive struct and union handling [done ]
100 # 18. Improve variable naming for display (eg: structs, unions etc)
101 #
102 # Also test, Test, TEST
103 #
104
105
106
107 #
108 #   Strategy:
109 #    For every operation and attribute do
110 #       For return val and all parameters do
111 #       find basic IDL type for each parameter
112 #       output get_CDR_xxx
113 #       output exception handling code
114 #       output attribute handling code
115 #
116 #
117
118 class ethereal_gen_C:
119
120
121     #
122     # Turn DEBUG stuff on/off
123     #
124
125     DEBUG = 0
126
127     #
128     # Some string constants for our templates
129     #
130
131     c_u_octet4    = "guint32   u_octet4;"
132     c_s_octet4    = "gint32    s_octet4;"
133     c_u_octet2    = "guint16   u_octet2;"
134     c_s_octet2    = "gint16    s_octet2;"
135     c_u_octet1    = "guint8    u_octet1;"
136     c_s_octet1    = "gint8     s_octet1;"
137     
138     c_float       = "gfloat    my_float;"
139     c_double      = "gdouble   my_double;"
140
141     c_seq         = "gchar   *seq = NULL;"          # pointer to buffer of gchars
142     c_i           = "guint32   i_";                 # loop index
143     c_i_lim       = "guint32   u_octet4_loop_";     # loop limit
144     c_u_disc      = "guint32   disc_u_";            # unsigned int union discriminant variable name (enum)
145     c_s_disc      = "gint32    disc_s_";            # signed int union discriminant variable name (other cases, except Enum)
146     
147     #
148     # Constructor
149     #
150
151     def __init__(self, st, protocol_name, dissector_name ,description):
152         self.st = output.Stream(tempfile.TemporaryFile(),4) # for first pass only
153         
154         self.st_save = st               # where 2nd pass should go
155         self.protoname = protocol_name  # Protocol Name (eg: ECHO)
156         self.dissname = dissector_name  # Dissector name (eg: echo)
157         self.description = description  # Detailed Protocol description (eg: Echo IDL Example)
158         self.exlist = []                # list of exceptions used in operations.
159         #self.curr_sname                # scoped name of current opnode or exnode I am visiting, used for generating "C" var declares
160         self.fn_hash = {}               # top level hash to contain key = function/exception and val = list of variable declarations
161                                         # ie a hash of lists
162         self.fn_hash_built = 0          # flag to indicate the 1st pass is complete, and the fn_hash is correctly
163                                         # populated with operations/vars and exceptions/vars
164
165                                         
166     #
167     # genCode()
168     #
169     # Main entry point, controls sequence of
170     # generated code.
171     #
172     #
173         
174     def genCode(self,oplist, atlist, enlist, stlist, unlist):   # operation,attribute,enums,struct and union lists
175
176
177         self.genHelpers(oplist,stlist,unlist)  # sneaky .. call it now, to populate the fn_hash
178                                         # so when I come to that operation later, I have the variables to
179                                         # declare already.
180                                         
181         self.genExceptionHelpers(oplist) # sneaky .. call it now, to populate the fn_hash
182                                          # so when I come to that exception later, I have the variables to
183                                          # declare already.
184                                         
185         self.genAttributeHelpers(atlist) # sneaky .. call it now, to populate the fn_hash
186                                          # so when I come to that exception later, I have the variables to
187                                          # declare already.
188                                         
189                                                                                 
190         self.fn_hash_built = 1          # DONE, so now I know , see genOperation()
191
192         self.st = self.st_save
193         self.genHeader()                # initial dissector comments
194         self.genEthCopyright()          # Ethereal Copyright comments.
195         self.genGPL()                   # GPL license
196         self.genIncludes()
197         self.genDeclares(oplist,atlist,enlist,stlist,unlist)
198         self.genProtocol()
199         self.genRegisteredFields()
200         self.genOpList(oplist)          # string constant declares for operation names
201         self.genExList(oplist)          # string constant declares for user exceptions
202         self.genAtList(atlist)          # string constant declares for Attributes
203         self.genEnList(enlist)          # string constant declares for Enums
204         
205         
206         self.genExceptionHelpers(oplist)   # helper function to decode user exceptions that have members
207         self.genExceptionDelegator(oplist) # finds the helper function to decode a user exception
208         self.genAttributeHelpers(atlist)   # helper function to decode "attributes"
209
210         self.genHelpers(oplist,stlist,unlist)  # operation, struct and union decode helper functions
211
212         self.genMainEntryStart(oplist)
213         self.genOpDelegator(oplist)
214         self.genAtDelegator(atlist)        
215         self.genMainEntryEnd()
216
217         self.gen_proto_register()
218         self.gen_proto_reg_handoff(oplist)
219         self.gen_plugin_init()
220
221         #self.dumpvars()                 # debug
222         
223
224
225     #
226     # genHeader
227     #
228     # Generate Standard Ethereal Header Comments
229     #
230     #
231     
232     def genHeader(self):
233         self.st.out(self.template_Header,dissector_name=self.dissname)        
234         if self.DEBUG:
235             print "XXX genHeader"
236
237
238
239
240     #
241     # genEthCopyright
242     #
243     # Ethereal Copyright Info
244     #
245     #
246
247     def genEthCopyright(self):        
248         if self.DEBUG:
249             print "XXX genEthCopyright"            
250         self.st.out(self.template_ethereal_copyright)
251
252
253     #
254     # genGPL
255     #
256     # GPL licencse
257     #
258     #
259
260     def genGPL(self):
261         if self.DEBUG:
262             print "XXX genGPL"
263             
264         self.st.out(self.template_GPL)
265
266     #
267     # genIncludes
268     #
269     # GPL licencse
270     #
271     #
272
273     def genIncludes(self):
274         if self.DEBUG:
275             print "XXX genIncludes"
276             
277         self.st.out(self.template_Includes)
278                                 
279
280     #
281     # denDeclares
282     #
283     # generate function prototypes if required
284     #
285     # Currently this is used for struct and union helper function declarations.
286     #
287     
288     
289     def genDeclares(self,oplist,atlist,enlist,stlist,unlist):
290         if self.DEBUG:
291             print "XXX genDeclares"
292
293         # struct prototypes
294         
295         self.st.out(self.template_prototype_struct_start)        
296         for st in stlist:
297             #print st.repoId()
298             sname = self.namespace(st, "_")   
299
300             self.st.out(self.template_prototype_struct_body, stname=st.repoId(),name=sname)        
301         self.st.out(self.template_prototype_struct_end)        
302
303         # union prototypes
304
305         self.st.out(self.template_prototype_union_start)        
306         for un in unlist:
307             sname = self.namespace(un, "_")   
308             self.st.out(self.template_prototype_union_body, unname=un.repoId(),name=sname)        
309         self.st.out(self.template_prototype_union_end)        
310                         
311
312
313                                 
314     #
315     # genProtocol
316     #
317     #
318     
319     def genProtocol(self):
320         self.st.out(self.template_protocol, dissector_name=self.dissname)        
321         self.st.out(self.template_init_boundary)        
322         
323                                 
324     #
325     # genProtoAndRegisteredFields
326     #
327     #
328     
329     def genRegisteredFields(self):
330         self.st.out(self.template_registered_fields )        
331         
332
333
334     #
335     # genMainEntryStart
336     #
337
338     def genMainEntryStart(self,oplist):
339         self.st.out(self.template_main_dissector_start, dissname=self.dissname, disprot=self.protoname)        
340         self.st.inc_indent()
341         self.st.out(self.template_main_dissector_switch_msgtype_start)        
342         self.st.out(self.template_main_dissector_switch_msgtype_start_request_reply)        
343         self.st.inc_indent()
344
345                 
346     #
347     # genMainEntryEnd
348     #
349
350     def genMainEntryEnd(self):
351
352         self.st.out(self.template_main_dissector_switch_msgtype_end_request_reply)        
353         self.st.dec_indent()
354         self.st.out(self.template_main_dissector_switch_msgtype_all_other_msgtype)             
355         self.st.dec_indent()
356         self.st.out(self.template_main_dissector_end)        
357         
358
359     #
360     # genOpList
361     #
362     # in: oplist
363     #
364     # out: C code for IDL operations
365     #
366     # eg:
367     #
368     # static const char Penguin_Echo_echoShort_op[] = "echoShort" ;
369     #
370
371     def genOpList(self,oplist):
372         self.st.out(self.template_comment_operations_start)        
373
374         for n in oplist:
375             sname = self.namespace(n, "_")   
376             opname = n.identifier()
377             self.st.out(self.template_operations_declare, sname=sname, opname=opname)
378     
379         self.st.out(self.template_comment_operations_end)
380
381     #
382     # genExList
383     #
384     # in: oplist
385     #
386     # out: C code for IDL User Exceptions that contain members
387     #
388     # eg:
389     #
390     # static const char user_exception_tux_bad_value[] = "IDL:tux/bad_value:1.0" ;
391     #
392
393     def genExList(self,oplist):
394         
395         self.st.out(self.template_comment_user_exceptions_string_declare_start)
396         
397         exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
398
399         for ex in exlist:
400             if self.DEBUG:
401                 print "XXX Exception " , ex.repoId()
402                 print "XXX Exception Identifier" , ex.identifier()
403                 print "XXX Exception Scoped Name" , ex.scopedName()
404             
405             if (ex.members()):          # only if has members
406                 sname = self.namespace(ex, "_")   
407                 exname = ex.repoId()
408                 self.st.out(self.template_user_exceptions_declare,  sname=sname, exname=ex.repoId())
409     
410         self.st.out(self.template_comment_user_exceptions_string_declare_end)
411
412     #
413     # genAtList
414     #
415     # in: atlist
416     #
417     # out: C code for IDL attribute decalarations.
418     #
419     # NOTE: Mapping of attributes to  operation(function) names is tricky.
420     #
421     # The actual accessor function names are language-mapping specific. The attribute name
422     # is subject to OMG IDL's name scoping rules; the accessor function names are 
423     # guaranteed not to collide with any legal operation names specifiable in OMG IDL.
424     #
425     # eg:
426     #
427     # static const char get_Penguin_Echo_get_width_at[] = "get_width" ;
428     # static const char set_Penguin_Echo_set_width_at[] = "set_width" ;
429     #
430     # or:
431     #
432     # static const char get_Penguin_Echo_get_width_at[] = "_get_width" ;
433     # static const char set_Penguin_Echo_set_width_at[] = "_set_width" ;
434     #    
435     # TODO: Implement some language dependant templates to handle naming conventions
436     #       language <=> attribute. for C, C++. Java etc
437     #
438     # OR, just add a runtime GUI option to select language binding for attributes -- FS
439     #
440     #
441     #
442     # ie: def genAtlist(self,atlist,language) 
443     #
444     
445     
446
447     def genAtList(self,atlist):
448         self.st.out(self.template_comment_attributes_start)        
449
450         for n in atlist:
451             for i in n.declarators():   # 
452                 sname = self.namespace(i, "_")   
453                 atname = i.identifier()
454                 self.st.out(self.template_attributes_declare_Java_get, sname=sname, atname=atname)
455                 if not n.readonly():
456                     self.st.out(self.template_attributes_declare_Java_set, sname=sname, atname=atname)
457     
458         self.st.out(self.template_comment_attributes_end)
459
460
461     #
462     # genEnList
463     #
464     # in: enlist
465     #
466     # out: C code for IDL Enum decalarations using "static const value_string" template
467     #
468
469     
470
471     def genEnList(self,enlist):
472         
473         self.st.out(self.template_comment_enums_start)        
474
475         for enum in enlist:
476             sname = self.namespace(enum, "_")
477             
478             self.st.out(self.template_comment_enum_comment, ename=enum.repoId())
479             self.st.out(self.template_value_string_start, valstringname=sname)
480             for enumerator in enum.enumerators():
481                 self.st.out(self.template_value_string_entry, intval=str(self.valFromEnum(enum,enumerator)), description=enumerator.identifier())
482                 
483                 
484             #atname = n.identifier()
485             self.st.out(self.template_value_string_end, valstringname=sname)
486     
487         self.st.out(self.template_comment_enums_end)
488
489
490
491
492
493
494
495
496
497
498     #
499     # genExceptionDelegator
500     #
501     # in: oplist
502     #
503     # out: C code for User exception delegator
504     #
505     # eg:
506     #
507     #
508
509     def genExceptionDelegator(self,oplist):
510         
511         self.st.out(self.template_main_exception_delegator_start)
512         self.st.inc_indent()
513
514         exlist = self.get_exceptionList(oplist) # grab list of ALL UNIQUE exception nodes
515
516         for ex in exlist:
517             if self.DEBUG:
518                 print "XXX Exception " , ex.repoId()
519                 print "XXX Exception Identifier" , ex.identifier()
520                 print "XXX Exception Scoped Name" , ex.scopedName()
521             
522             if (ex.members()):          # only if has members
523                 sname = self.namespace(ex, "_")   
524                 exname = ex.repoId()
525                 self.st.out(self.template_ex_delegate_code,  sname=sname, exname=ex.repoId())
526
527         self.st.dec_indent()    
528         self.st.out(self.template_main_exception_delegator_end)
529
530
531     #
532     # genAttribueHelpers()
533     #
534     # Generate private helper functions to decode Attributes.
535     #
536     # in: atlist
537     #
538     # For readonly attribute - generate get_xxx()
539     # If NOT readonly attribute - also generate set_xxx()
540     #    
541         
542     def genAttributeHelpers(self,atlist):        
543         if self.DEBUG:
544             print "XXX genAttributeHelpers: atlist = ", atlist
545
546         self.st.out(self.template_attribute_helpers_start)
547         
548         for attrib in atlist:
549             for decl in attrib.declarators():
550                 self.genAtHelper(attrib,decl,"get") # get accessor
551                 if not attrib.readonly():
552                     self.genAtHelper(attrib,decl,"set") # set accessor
553                     
554         self.st.out(self.template_attribute_helpers_end)
555
556     #
557     # genAtHelper() 
558     #
559     # Generate private helper functions to decode an attribute
560     #
561     # in: at - attribute node
562     # in: decl - declarator belonging to this attribute 
563     # in: order - to generate a "get" or "set" helper
564     
565     def genAtHelper(self,attrib,decl,order):
566         if self.DEBUG:
567             print "XXX genAtHelper"
568             
569         sname = order + "_" + self.namespace(decl, "_")  # must use set or get prefix to avoid collision
570         self.curr_sname = sname                    # update current opnode/exnode scoped name
571                                                 
572         if not self.fn_hash_built:
573             self.fn_hash[sname] = []        # init empty list as val for this sname key
574                                             # but only if the fn_hash is not already built
575
576         self.st.out(self.template_attribute_helper_function_start, sname=sname, atname=decl.repoId())
577         self.st.inc_indent()
578
579         self.st.out(self.template_helper_function_vars_start)
580         self.dumpCvars(sname)
581         self.st.out(self.template_helper_function_vars_end )
582         
583         self.st.out(self.template_exception_helper_function_get_endianess)
584
585         #
586         # TODO - attributes are simple types, so remove array handling
587         #
588                 
589         if decl.sizes():        # an array
590             indices = self.get_indices_from_sizes(decl.sizes())
591             string_indices = '%i ' % indices # convert int to string
592             self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)     
593             self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
594             self.addvar(self.c_i + decl.identifier() + ";")
595             
596             self.st.inc_indent()
597
598             self.getCDR3(attrib.attrType(), decl.identifier() )
599                     
600             self.st.dec_indent()
601             self.st.out(self.template_get_CDR_array_end)
602                     
603                     
604         else:
605
606             self.getCDR3(attrib.attrType(), decl.identifier() )
607
608         self.st.dec_indent()
609         self.st.out(self.template_attribute_helper_function_end)
610
611
612
613     #
614     # genExceptionHelpers()
615     #
616     # Generate private helper functions to decode Exceptions used
617     # within operations
618     #
619     # in: oplist
620     #
621     
622         
623     def genExceptionHelpers(self,oplist):        
624         exlist = self.get_exceptionList(oplist) # grab list of exception nodes
625         if self.DEBUG:
626             print "XXX genExceptionHelpers: exlist = ", exlist
627
628         self.st.out(self.template_exception_helpers_start)
629         for ex in exlist:
630             if (ex.members()):          # only if has members
631                 #print "XXX Exception = " + ex.identifier()
632                 self.genExHelper(ex)
633         
634         self.st.out(self.template_exception_helpers_end)
635
636
637     #
638     # genExhelper() 
639     #
640     # Generate private helper functions to decode User Exceptions
641     #
642     # in: exnode ( an exception node)
643     #
644     
645     def genExHelper(self,ex):
646         if self.DEBUG:
647             print "XXX genExHelper"
648             
649         sname = self.namespace(ex, "_")
650         self.curr_sname = sname         # update current opnode/exnode scoped name
651         if not self.fn_hash_built:
652             self.fn_hash[sname] = []        # init empty list as val for this sname key
653                                             # but only if the fn_hash is not already built
654
655         self.st.out(self.template_exception_helper_function_start, sname=sname, exname=ex.repoId())
656         self.st.inc_indent()
657
658         self.st.out(self.template_helper_function_vars_start)
659         self.dumpCvars(sname)
660         self.st.out(self.template_helper_function_vars_end )
661         
662         self.st.out(self.template_exception_helper_function_get_endianess)
663
664         
665         for m in ex.members():
666             #print "XXX genExhelper, member = ", m, "member type = ", m.memberType()
667             
668
669             for decl in m.declarators():
670                 #print "XXX genExhelper, d = ", decl
671                 if decl.sizes():        # an array
672                     indices = self.get_indices_from_sizes(decl.sizes())
673                     string_indices = '%i ' % indices # convert int to string
674                     self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)     
675                     self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
676                     self.addvar(self.c_i + decl.identifier() + ";")
677                     
678                     self.st.inc_indent()       
679                     self.getCDR3(m.memberType(), ex.identifier() + "_" + decl.identifier() )
680                     
681                     self.st.dec_indent()
682                     self.st.out(self.template_get_CDR_array_end)
683                     
684                     
685                 else:    
686                     self.getCDR3(m.memberType(), ex.identifier() + "_" + decl.identifier() )
687
688         self.st.dec_indent()
689         self.st.out(self.template_exception_helper_function_end)
690
691
692     #
693     # genHelpers()
694     #
695     # Generate private helper functions for each IDL operation.
696     # Generate private helper functions for each IDL struct.
697     # Generate private helper functions for each IDL union.
698     #
699     #
700     # in: oplist, stlist, unlist
701     #
702     
703         
704     def genHelpers(self,oplist,stlist,unlist):
705         for op in oplist:
706             self.genOperation(op)
707         for st in stlist:
708             self.genStructHelper(st)
709         for un in unlist:
710             self.genUnionHelper(un)
711
712     #
713     # genOperation()
714     #
715     # Generate private helper functions for a specificIDL operation.
716     #
717     # in: opnode
718     #
719     
720     def genOperation(self,opnode):
721         if self.DEBUG:
722             print "XXX genOperation called"
723             
724         sname = self.namespace(opnode, "_")
725         if not self.fn_hash_built:
726             self.fn_hash[sname] = []        # init empty list as val for this sname key
727                                             # but only if the fn_hash is not already built
728         
729         self.curr_sname = sname         # update current opnode's scoped name
730         opname = opnode.identifier()
731         
732         self.st.out(self.template_helper_function_comment, repoid=opnode.repoId() )
733
734         self.st.out(self.template_helper_function_start, sname=sname)
735         self.st.inc_indent()
736
737         self.st.out(self.template_helper_function_vars_start)
738         self.dumpCvars(sname)
739         self.st.out(self.template_helper_function_vars_end )
740         
741
742         self.st.out(self.template_helper_switch_msgtype_start)
743
744         self.st.out(self.template_helper_switch_msgtype_request_start)
745         self.st.inc_indent()
746         self.genOperationRequest(opnode)       
747         self.st.out(self.template_helper_switch_msgtype_request_end)
748         self.st.dec_indent()
749
750         self.st.out(self.template_helper_switch_msgtype_reply_start)
751         self.st.inc_indent()
752         
753         self.st.out(self.template_helper_switch_rep_status_start)
754
755         
756         self.st.out(self.template_helper_switch_msgtype_reply_no_exception_start)
757         self.st.inc_indent()
758         self.genOperationReply(opnode)
759         self.st.out(self.template_helper_switch_msgtype_reply_no_exception_end)
760         self.st.dec_indent()
761         
762         self.st.out(self.template_helper_switch_msgtype_reply_user_exception_start)
763         self.st.inc_indent()       
764         self.genOpExceptions(opnode)
765         self.st.out(self.template_helper_switch_msgtype_reply_user_exception_end)
766         self.st.dec_indent()       
767         
768         self.st.out(self.template_helper_switch_msgtype_reply_default_start)
769         self.st.out(self.template_helper_switch_msgtype_reply_default_end)
770
771         self.st.out(self.template_helper_switch_rep_status_end)
772         
773         self.st.dec_indent()
774         
775         self.st.out(self.template_helper_switch_msgtype_default_start)
776         self.st.out(self.template_helper_switch_msgtype_default_end)
777         
778         self.st.out(self.template_helper_switch_msgtype_end)        
779         self.st.dec_indent()
780
781
782         self.st.out(self.template_helper_function_end, sname=sname)
783
784
785
786
787     #
788     # Decode function parameters for a GIOP request message
789     #
790     #
791     
792     def genOperationRequest(self,opnode):
793         for p in opnode.parameters():
794             if p.is_in():
795                 if self.DEBUG:
796                     print "XXX parameter = " ,p
797                     print "XXX parameter type = " ,p.paramType()
798                     print "XXX parameter type kind = " ,p.paramType().kind()
799
800                 self.getCDR3(p.paramType(),p.identifier())
801                 
802
803     #
804     # Decode function parameters for a GIOP reply message
805     #
806
807     
808     def genOperationReply(self,opnode):
809
810         rt = opnode.returnType()        # get return type
811         if self.DEBUG:
812             print "XXX opnode  = " , opnode
813             print "XXX return type  = " , rt
814             print "XXX return type.unalias  = " , rt.unalias()        
815             print "XXX return type.kind()  = " , rt.kind();
816             
817
818         if (rt.kind() == idltype.tk_alias): # a typdef return val possibly ?
819             #self.getCDR3(rt.decl().alias().aliasType(),"dummy")    # return value maybe a typedef
820             #self.get_CDR_alias(rt, "Operation_Return_Value" )
821             self.get_CDR_alias(rt, rt.name() )
822                                
823         else:            
824             self.getCDR3(rt, "Operation_Return_Value")    # return value is NOT an alias
825               
826         for p in opnode.parameters():
827             if p.is_out():              # out or inout
828                 self.getCDR3(p.paramType(),p.identifier())
829
830         #self.st.dec_indent()
831                
832     def genOpExceptions(self,opnode):
833         for ex in opnode.raises():
834             if ex.members():
835                 #print ex.members()
836                 for m in ex.members():
837                     t=0
838                     #print m.memberType(), m.memberType().kind()
839     #
840     # Delegator for Operations
841     #
842     
843     def genOpDelegator(self,oplist):
844         for op in oplist:
845             opname = op.identifier()
846             sname = self.namespace(op, "_")
847             self.st.out(self.template_op_delegate_code, sname=sname)
848
849     #
850     # Delegator for Attributes
851     #
852
853     def genAtDelegator(self,atlist):            
854         for a in atlist:
855             for i in a.declarators():
856                 atname = i.identifier()
857                 sname = self.namespace(i, "_")
858                 self.st.out(self.template_at_delegate_code_get, sname=sname)
859                 if not a.readonly():
860                     self.st.out(self.template_at_delegate_code_set, sname=sname)
861  
862
863     #
864     # Add a variable declaration to the hash of list
865     #
866     
867     def addvar(self, var):
868         if not ( var in self.fn_hash[self.curr_sname] ):
869             self.fn_hash[self.curr_sname].append(var)
870
871     #
872     # Print the variable declaration from  the hash of list
873     #
874                 
875
876     def dumpvars(self):
877         for fn in self.fn_hash.keys():
878             print "FN = " + fn
879             for v in self.fn_hash[fn]:
880                 print "-> " + v
881     #
882     # Print the "C" variable declaration from  the hash of list
883     # for a given scoped operation name (eg: tux_penguin_eat)
884     #
885                 
886
887     def dumpCvars(self, sname):
888             for v in self.fn_hash[sname]:
889                 self.st.out(v)
890
891
892     #
893     # Given an enum node, and a enumerator node, return 
894     # the enumerator's numerical value.
895     #
896     # eg: enum Color {red,green,blue} should return
897     # val = 1 for green
898     #
899                 
900     def valFromEnum(self,enumNode, enumeratorNode):
901         if self.DEBUG:
902             print "XXX valFromEnum, enumNode = ", enumNode, " from ", enumNode.repoId()
903             print "XXX valFromEnum, enumeratorNode = ", enumeratorNode, " from ", enumeratorNode.repoId()
904
905         if isinstance(enumeratorNode,idlast.Enumerator):
906             value = enumNode.enumerators().index(enumeratorNode)
907             return value
908                 
909
910 ## tk_null               = 0
911 ## tk_void               = 1
912 ## tk_short              = 2
913 ## tk_long               = 3
914 ## tk_ushort             = 4
915 ## tk_ulong              = 5
916 ## tk_float              = 6
917 ## tk_double             = 7
918 ## tk_boolean            = 8
919 ## tk_char               = 9
920 ## tk_octet              = 10
921 ## tk_any                = 11
922 ## tk_TypeCode           = 12
923 ## tk_Principal          = 13
924 ## tk_objref             = 14
925 ## tk_struct             = 15
926 ## tk_union              = 16
927 ## tk_enum               = 17
928 ## tk_string             = 18
929 ## tk_sequence           = 19
930 ## tk_array              = 20
931 ## tk_alias              = 21
932 ## tk_except             = 22
933 ## tk_longlong           = 23
934 ## tk_ulonglong          = 24
935 ## tk_longdouble         = 25
936 ## tk_wchar              = 26
937 ## tk_wstring            = 27
938 ## tk_fixed              = 28
939 ## tk_value              = 29
940 ## tk_value_box          = 30
941 ## tk_native             = 31
942 ## tk_abstract_interface = 32
943
944
945     #
946     # getCDR()
947     #
948     # This is the main "iterator" function. It takes a node, and tries to output
949     # a get_CDR_XXX accessor method(s). It can call itself multiple times
950     # if I find nested structures etc.
951     #
952
953     def getCDR3(self,type,name="fred"):
954
955         pt = type.unalias().kind()      # param CDR type
956         pn = name                       # param name
957
958         if self.DEBUG:
959             print "XXX getCDR3: kind = " , pt
960             
961         if pt == idltype.tk_ulong:
962             self.get_CDR_ulong(pn)
963         elif pt ==  idltype.tk_void:
964             self.get_CDR_void(pn)
965         elif pt ==  idltype.tk_short:
966             self.get_CDR_short(pn)          
967         elif pt ==  idltype.tk_long:
968             self.get_CDR_long(pn)
969         elif pt ==  idltype.tk_ushort:
970             self.get_CDR_ushort(pn)
971         elif pt ==  idltype.tk_float:
972             self.get_CDR_float(pn)
973         elif pt ==  idltype.tk_double:
974             self.get_CDR_double(pn)
975         elif pt == idltype.tk_fixed:
976             self.get_CDR_fixed(type.unalias(),pn)
977         elif pt ==  idltype.tk_boolean:
978             self.get_CDR_boolean(pn)
979         elif pt ==  idltype.tk_char:
980             self.get_CDR_char(pn)
981         elif pt ==  idltype.tk_octet:
982             self.get_CDR_octet(pn)
983         elif pt ==  idltype.tk_any:
984             self.get_CDR_any(pn)
985         elif pt ==  idltype.tk_string:
986             self.get_CDR_string(pn)
987         elif pt ==  idltype.tk_wstring:
988             self.get_CDR_wstring(pn)
989         elif pt ==  idltype.tk_wchar:
990             self.get_CDR_wchar(pn)            
991         elif pt ==  idltype.tk_enum:
992             #print type.decl()
993             self.get_CDR_enum(pn,type)
994             #self.get_CDR_enum(pn)
995             
996         elif pt ==  idltype.tk_struct:
997             self.get_CDR_struct(type,pn)
998         elif pt ==  idltype.tk_TypeCode: # will I ever get here ?
999             self.get_CDR_TypeCode(pn)
1000         elif pt == idltype.tk_sequence:
1001             self.get_CDR_sequence(type,pn)
1002         elif pt == idltype.tk_objref:
1003             self.get_CDR_objref(type,pn)
1004         elif pt == idltype.tk_array:
1005             self.get_CDR_array(type,pn)
1006         elif pt == idltype.tk_union:
1007             self.get_CDR_union(type,pn)            
1008         elif pt == idltype.tk_alias:
1009             if self.DEBUG:
1010                 print "XXXXX Alias type XXXXX " , type
1011             self.get_CDR_alias(type,pn)            
1012         else:
1013             self.genWARNING("Unknown typecode = " + '%i ' % pt) # put comment in source code
1014
1015
1016     #
1017     # get_CDR_XXX methods are here ..
1018     #
1019     #
1020     
1021             
1022     def get_CDR_ulong(self,pn):
1023         self.st.out(self.template_get_CDR_ulong, varname=pn)
1024         self.addvar(self.c_u_octet4)
1025
1026     def get_CDR_short(self,pn):
1027         self.st.out(self.template_get_CDR_short, varname=pn)
1028         self.addvar(self.c_s_octet2)
1029
1030     def get_CDR_void(self,pn):
1031         self.st.out(self.template_get_CDR_void, varname=pn)
1032
1033     def get_CDR_long(self,pn):
1034         self.st.out(self.template_get_CDR_long, varname=pn)
1035         self.addvar(self.c_s_octet4)
1036
1037     def get_CDR_ushort(self,pn):
1038         self.st.out(self.template_get_CDR_ushort, varname=pn)
1039         self.addvar(self.c_u_octet2)
1040
1041     def get_CDR_float(self,pn):
1042         self.st.out(self.template_get_CDR_float, varname=pn)
1043         self.addvar(self.c_float)
1044
1045     def get_CDR_double(self,pn):
1046         self.st.out(self.template_get_CDR_double, varname=pn)
1047         self.addvar(self.c_double)
1048
1049     def get_CDR_boolean(self,pn):
1050         self.st.out(self.template_get_CDR_boolean, varname=pn)
1051         self.addvar(self.c_u_octet1)
1052         
1053     def get_CDR_fixed(self,type,pn):
1054         if self.DEBUG:
1055             print "XXXX calling get_CDR_fixed, type = ", type
1056             print "XXXX calling get_CDR_fixed, type.digits() = ", type.digits()
1057             print "XXXX calling get_CDR_fixed, type.scale() = ", type.scale()
1058
1059         string_digits = '%i ' % type.digits() # convert int to string
1060         string_scale  = '%i ' % type.scale()  # convert int to string
1061         string_length  = '%i ' % self.dig_to_len(type.digits())  # how many octets to hilight for a number of digits
1062     
1063         self.st.out(self.template_get_CDR_fixed, varname=pn, digits=string_digits, scale=string_scale, length=string_length )
1064         self.addvar(self.c_seq)
1065                 
1066                 
1067     def get_CDR_char(self,pn):
1068         self.st.out(self.template_get_CDR_char, varname=pn)
1069         self.addvar(self.c_u_octet1)        
1070
1071     def get_CDR_octet(self,pn):
1072         self.st.out(self.template_get_CDR_octet, varname=pn)
1073         self.addvar(self.c_u_octet1)
1074
1075     def get_CDR_any(self,pn):
1076         self.st.out(self.template_get_CDR_any, varname=pn)
1077
1078     def get_CDR_enum(self,pn,type):
1079         #self.st.out(self.template_get_CDR_enum, varname=pn)
1080         sname = self.namespace(type.unalias(), "_")
1081         self.st.out(self.template_get_CDR_enum_symbolic, valstringarray=sname)
1082
1083
1084         self.addvar(self.c_u_octet4)
1085
1086     def get_CDR_string(self,pn):
1087         self.st.out(self.template_get_CDR_string, varname=pn)
1088         self.addvar(self.c_u_octet4)
1089         self.addvar(self.c_seq)
1090
1091     def get_CDR_wstring(self,pn):
1092         self.st.out(self.template_get_CDR_wstring, varname=pn)
1093         self.addvar(self.c_u_octet4)
1094         self.addvar(self.c_seq)
1095         
1096     def get_CDR_wchar(self,pn):
1097         self.st.out(self.template_get_CDR_wchar, varname=pn)
1098         self.addvar(self.c_s_octet1)
1099         self.addvar(self.c_seq)
1100                 
1101     def get_CDR_TypeCode(self,pn):
1102         self.st.out(self.template_get_CDR_TypeCode, varname=pn)
1103         self.addvar(self.c_u_octet4)
1104
1105     def get_CDR_objref(self,type,pn):
1106         self.st.out(self.template_get_CDR_object)
1107
1108     def get_CDR_sequence_len(self,pn):
1109         self.st.out(self.template_get_CDR_sequence_length, seqname=pn)
1110         self.addvar(self.c_u_octet4)
1111
1112
1113             
1114     def get_CDR_union(self,type,pn):
1115         if self.DEBUG:
1116             print "XXX Union type =" , type, " pn = ",pn
1117             print "XXX Union type.decl()" , type.decl()
1118             print "XXX Union Scoped Name" , type.scopedName()
1119
1120        #  If I am a typedef union {..}; node then find the union node
1121         
1122         if isinstance(type.decl(), idlast.Declarator):
1123             ntype = type.decl().alias().aliasType().decl()           
1124         else:
1125             ntype = type.decl()         # I am a union node
1126
1127         if self.DEBUG:    
1128             print "XXX Union ntype =" , ntype
1129
1130         sname = self.namespace(ntype, "_")
1131         self.st.out(self.template_union_start, name=sname )
1132
1133         # Output a call to the union helper function so I can handle recursive union also.
1134         
1135         self.st.out(self.template_decode_union,name=sname)    
1136
1137         self.st.out(self.template_union_end, name=sname )
1138             
1139
1140     #
1141     # Code to generate Union Helper functions
1142     #
1143     # in: un - a union node
1144     #
1145     #
1146
1147
1148     def genUnionHelper(self,un):
1149         if self.DEBUG:
1150             print "XXX Union type =" , un
1151             print "XXX Union type.decl()" , un.decl()
1152             print "XXX Union Scoped Name" , un.scopedName()
1153
1154         sname = self.namespace(un, "_")
1155         self.curr_sname = sname         # update current opnode/exnode/stnode/unnode scoped name
1156         if not self.fn_hash_built:
1157             self.fn_hash[sname] = []        # init empty list as val for this sname key
1158                                             # but only if the fn_hash is not already built
1159
1160         self.st.out(self.template_union_helper_function_start, sname=sname, unname=un.repoId())
1161         self.st.inc_indent()
1162
1163         self.st.out(self.template_helper_function_vars_start)
1164         self.dumpCvars(sname)
1165         self.st.out(self.template_helper_function_vars_end )
1166         
1167         self.st.out(self.template_union_helper_function_get_endianess)
1168
1169         st = un.switchType().unalias() # may be typedef switch type, so find real type
1170
1171         self.st.out(self.template_comment_union_code_start, uname=un.repoId() )
1172
1173         self.getCDR3(st,un.identifier());
1174
1175         # Depending on what kind of discriminant I come accross (enum,integer,char,
1176         # short, boolean), make sure I cast the return value of the get_XXX accessor
1177         # to an appropriate value. Omniidl idlast.CaseLabel.value() accessor will
1178         # return an integer, or an Enumerator object that is then converted to its
1179         # integer equivalent.
1180         #
1181         #
1182         # NOTE - May be able to skip some of this stuff, but leave it in for now -- FS
1183         #
1184
1185         if (st.kind() == idltype.tk_enum):
1186             std = st.decl()            
1187             self.st.out(self.template_comment_union_code_discriminant, uname=std.repoId() )
1188             self.st.out(self.template_union_code_save_discriminant_enum, discname=un.identifier() )
1189             self.addvar(self.c_s_disc + un.identifier() + ";")            
1190
1191         elif (st.kind() == idltype.tk_long):
1192             self.st.out(self.template_union_code_save_discriminant_long, discname=un.identifier() )
1193             self.addvar(self.c_s_disc + un.identifier() + ";")            
1194
1195         elif (st.kind() == idltype.tk_ulong):
1196             self.st.out(self.template_union_code_save_discriminant_ulong, discname=un.identifier() )
1197             self.addvar(self.c_s_disc + un.identifier() + ";")            
1198
1199         elif (st.kind() == idltype.tk_short):
1200             self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
1201             self.addvar(self.c_s_disc + un.identifier() + ";")            
1202
1203         elif (st.kind() == idltype.tk_ushort):
1204             self.st.out(self.template_union_code_save_discriminant_ushort, discname=un.identifier() )
1205             self.addvar(self.c_s_disc + un.identifier() + ";")            
1206
1207         elif (st.kind() == idltype.tk_boolean):
1208             self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier()  )
1209             self.addvar(self.c_s_disc + un.identifier() + ";")            
1210             
1211         elif (st.kind() == idltype.tk_char):
1212             self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
1213             self.addvar(self.c_s_disc + un.identifier() + ";")            
1214
1215         else:
1216             print "XXX Unknown st.kind() = ", st.kind()
1217             
1218         #
1219         # Loop over all cases in this union
1220         #
1221         
1222         for uc in un.cases():           # for all UnionCase objects in this union
1223             for cl in uc.labels():      # for all Caselabel objects in this UnionCase
1224
1225                 # get integer value, even if discriminant is
1226                 # an Enumerator node
1227                 
1228                 if isinstance(cl.value(),idlast.Enumerator):
1229                     if self.DEBUG:
1230                         print "XXX clv.identifier()", cl.value().identifier()
1231                         print "XXX clv.repoId()", cl.value().repoId()
1232                         print "XXX clv.scopedName()", cl.value().scopedName()
1233                                             
1234                     # find index of enumerator in enum declaration
1235                     # eg: RED is index 0 in enum Colors { RED, BLUE, GREEN }
1236                     
1237                     clv = self.valFromEnum(std,cl.value())
1238                     
1239                 else:
1240                     clv = cl.value()
1241                     
1242                 #print "XXX clv = ",clv
1243
1244                 #    
1245                 # if char, dont convert to int, but put inside single quotes so that it is understood by C.
1246                 # eg: if (disc == 'b')..
1247                 #
1248                 # TODO : handle \xxx chars generically from a function or table lookup rather than
1249                 #        a whole bunch of "if" statements. -- FS
1250                 
1251                                           
1252                 if (st.kind() == idltype.tk_char):
1253                     if (clv == '\n'):          # newline
1254                         string_clv = "'\\n'" 
1255                     elif (clv == '\t'):        # tab
1256                         string_clv = "'\\t'"
1257                     else:
1258                         string_clv = "'" + clv + "'"
1259                 else:                    
1260                     string_clv = '%i ' % clv
1261
1262                 #
1263                 # If default case, then skp comparison with discriminator
1264                 #
1265                 
1266                 if not cl.default():
1267                     self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
1268                     self.st.inc_indent()       
1269                 else:
1270                     self.st.out(self.template_comment_union_code_label_default_start  )
1271                     
1272
1273                 self.getCDR3(uc.caseType(),uc.declarator().identifier())
1274
1275                 if not cl.default():
1276                     self.st.dec_indent()       
1277                     self.st.out(self.template_comment_union_code_label_compare_end )
1278                 else:
1279                     self.st.out(self.template_comment_union_code_label_default_end  )
1280
1281         self.st.dec_indent()
1282         self.st.out(self.template_union_helper_function_end)
1283
1284
1285
1286     #
1287     # Currently, get_CDR_alias is geared to finding typdef 
1288     #
1289
1290     def get_CDR_alias(self,type,pn):
1291         if self.DEBUG:
1292             print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1293             print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1294             print "XXX get_CDR_alias, type.decl().alias() = " ,type.decl().alias()
1295
1296         decl = type.decl()              # get declarator object
1297         
1298         if (decl.sizes()):        # a typedef array 
1299             indices = self.get_indices_from_sizes(decl.sizes())
1300             string_indices = '%i ' % indices # convert int to string
1301             self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1302             
1303             self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)                        
1304             self.addvar(self.c_i + pn + ";")            
1305             self.st.inc_indent()       
1306             self.getCDR3(type.decl().alias().aliasType(),  pn )
1307             
1308             self.st.dec_indent()
1309             self.st.out(self.template_get_CDR_array_end)
1310             
1311             
1312         else:                           # a simple typdef
1313             if self.DEBUG:
1314                 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1315                 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1316
1317             self.getCDR3(type, decl.identifier() )
1318             
1319             
1320             
1321         
1322         
1323
1324     #
1325     # Handle structs, including recursive
1326     #
1327     
1328     def get_CDR_struct(self,type,pn):       
1329
1330         #  If I am a typedef struct {..}; node then find the struct node
1331         
1332         if isinstance(type.decl(), idlast.Declarator):
1333             ntype = type.decl().alias().aliasType().decl()           
1334         else:
1335             ntype = type.decl()         # I am a struct node
1336
1337         sname = self.namespace(ntype, "_")
1338         self.st.out(self.template_structure_start, name=sname )
1339
1340         # Output a call to the struct helper function so I can handle recursive structs also.
1341         
1342         self.st.out(self.template_decode_struct,name=sname)    
1343
1344         self.st.out(self.template_structure_end, name=sname )
1345
1346     #
1347     # genStructhelper() 
1348     #
1349     # Generate private helper functions to decode a struct
1350     #
1351     # in: stnode ( a struct node)
1352     #
1353     
1354     def genStructHelper(self,st):
1355         if self.DEBUG:
1356             print "XXX genStructHelper"
1357             
1358         sname = self.namespace(st, "_")
1359         self.curr_sname = sname         # update current opnode/exnode/stnode scoped name
1360         if not self.fn_hash_built:
1361             self.fn_hash[sname] = []        # init empty list as val for this sname key
1362                                             # but only if the fn_hash is not already built
1363
1364         self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
1365         self.st.inc_indent()
1366
1367         self.st.out(self.template_helper_function_vars_start)
1368         self.dumpCvars(sname)
1369         self.st.out(self.template_helper_function_vars_end )
1370         
1371         self.st.out(self.template_struct_helper_function_get_endianess)
1372
1373         for m in st.members():
1374             for decl in m.declarators():
1375                 if decl.sizes():        # an array
1376                     indices = self.get_indices_from_sizes(decl.sizes())
1377                     string_indices = '%i ' % indices # convert int to string
1378                     self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)     
1379                     self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
1380                     self.addvar(self.c_i + decl.identifier() + ";")
1381                     
1382                     self.st.inc_indent()       
1383                     self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )                    
1384                     self.st.dec_indent()
1385                     self.st.out(self.template_get_CDR_array_end)
1386                     
1387                     
1388                 else:                            
1389                     self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
1390                             
1391         self.st.dec_indent()
1392         self.st.out(self.template_struct_helper_function_end)
1393
1394
1395
1396
1397
1398     #
1399     # Generate code to access a sequence of a type
1400     #
1401
1402     
1403     def get_CDR_sequence(self,type, pn):
1404         self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
1405         self.st.out(self.template_get_CDR_sequence_loop_start, seqname=pn )
1406         self.addvar(self.c_i_lim + pn + ";" )
1407         self.addvar(self.c_i + pn + ";")
1408
1409         self.st.inc_indent()       
1410         self.getCDR3(type.unalias().seqType(), pn ) # and start all over with the type
1411         self.st.dec_indent()     
1412         
1413         self.st.out(self.template_get_CDR_sequence_loop_end)
1414
1415
1416         
1417     #
1418     # Generate code to access arrays, 
1419     #
1420     # This is handled elsewhere. Arrays are either typedefs or in
1421     # structs
1422     #
1423     # TODO - Remove this
1424     #
1425     
1426     def get_CDR_array(self,type, decl):
1427         if self.DEBUG:
1428             print "XXX get_CDR_array called "
1429             print "XXX array size = " ,decl.sizes()
1430         
1431
1432    #
1433    # namespace()
1434    #
1435    # in - op node
1436    #
1437    # out - scoped operation name, using sep character instead of "::"
1438    #
1439    # eg: Penguin::Echo::echoWString => Penguin_Echo_echoWString if sep = "_"
1440    #
1441    #
1442    
1443     def namespace(self,node,sep):    
1444         sname = string.replace(idlutil.ccolonName(node.scopedName()), '::', sep)
1445         #print "XXX namespace: sname = " + sname
1446         return sname
1447
1448
1449     #
1450     # generate code for plugin initialisation
1451     #
1452
1453     def gen_plugin_init(self):
1454         self.st.out(self.template_plugin_init, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1455
1456     #
1457     # generate  register_giop_user_module code, and register only
1458     # unique interfaces that contain operations. Also output
1459     # a heuristic register in case we want to use that.
1460     #
1461     # TODO - make this a command line option
1462     #
1463     # -e explicit
1464     # -h heuristic
1465     #
1466     
1467     
1468
1469     def gen_proto_reg_handoff(self, oplist):
1470
1471         self.st.out(self.template_proto_reg_handoff_start, dissector_name=self.dissname)
1472         self.st.inc_indent()
1473
1474         for iname in self.get_intlist(oplist):
1475             self.st.out(self.template_proto_reg_handoff_body, dissector_name=self.dissname, protocol_name=self.protoname, interface=iname )
1476             
1477         self.st.out(self.template_proto_reg_handoff_heuristic, dissector_name=self.dissname,  protocol_name=self.protoname)
1478         self.st.dec_indent()
1479         
1480         self.st.out(self.template_proto_reg_handoff_end)
1481
1482
1483
1484     #
1485     # generate  proto_register_<protoname> code,
1486     #
1487     
1488
1489     def gen_proto_register(self):
1490         self.st.out(self.template_proto_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1491     
1492
1493     #
1494     # in - oplist[]
1495     #
1496     # out - a list of unique interface names. This will be used in
1497     # register_giop_user_module(dissect_giop_auto, "TEST IDL", "Penguin/Echo" );   so the operation
1498     # name must be removed from the scope. And we also only want unique interfaces.
1499     #
1500
1501     def get_intlist(self,oplist):
1502         int_hash = {}                   # holds a hash of unique interfaces
1503         for op in oplist:
1504             sc = op.scopedName()        # eg: penguin,tux,bite
1505             sc1 = sc[:-1]               # drop last entry
1506             sn = idlutil.slashName(sc1)         # penguin/tux
1507             if not int_hash.has_key(sn):
1508                 int_hash[sn] = 0;       # dummy val, but at least key is unique
1509                 
1510         return int_hash.keys()
1511
1512
1513
1514     #
1515     # in - oplist[]
1516     #
1517     # out - a list of exception nodes (unique). This will be used in
1518     # to generate dissect_exception_XXX functions.
1519     #
1520     
1521
1522
1523     def get_exceptionList(self,oplist):
1524         ex_hash = {}                   # holds a hash of unique exceptions.
1525         for op in oplist:
1526             for ex in op.raises():
1527                 if not ex_hash.has_key(ex):
1528                     ex_hash[ex] = 0; # dummy val, but at least key is unique
1529                     if self.DEBUG:
1530                         print "XXX Exception = " + ex.identifier()
1531
1532         return ex_hash.keys()
1533
1534
1535
1536     #
1537     # Simple function to take a list of array sizes and find the
1538     # total number of elements
1539     #
1540     #
1541     # eg: temp[4][3] = 12 elements
1542     #
1543
1544     def get_indices_from_sizes(self,sizelist):
1545         val = 1;
1546         for i in sizelist:
1547             val = val * i
1548             
1549         return val
1550
1551     #
1552     # Determine how many octets contain requested number
1553     # of digits for an "fixed" IDL type  "on the wire"
1554     #
1555
1556     def dig_to_len(self,dignum):
1557         return (dignum/2) + 1
1558
1559             
1560
1561     #
1562     # Output some TODO comment
1563     #
1564     
1565
1566     def genTODO(self,message):
1567         self.st.out(self.template_debug_TODO, message=message)
1568
1569     #
1570     # Output some WARNING comment
1571     #
1572     
1573
1574     def genWARNING(self,message):
1575         self.st.out(self.template_debug_WARNING, message=message)
1576             
1577     #
1578     # Templates for C code
1579     #
1580
1581
1582
1583     template_comment_operations_start = """\
1584 /*
1585  * IDL Operations Start
1586  */
1587  
1588  """
1589
1590
1591     template_operations_declare = """static const char @sname@_op[] = \"@opname@\" ;"""
1592     
1593
1594     template_comment_operations_end = """
1595 /*
1596  * IDL Operations End
1597  */
1598  
1599 """
1600
1601     template_helper_function_comment = """\
1602
1603 /*
1604  * @repoid@
1605  */
1606  
1607 """
1608     
1609     template_helper_function_vars_start = """
1610 /* Operation specific Variable declarations Begin */
1611 """
1612     
1613     template_helper_function_vars_end = """
1614 /* Operation specific Variable declarations End */
1615 """
1616
1617
1618
1619     template_helper_function_start = """\
1620 static void decode_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header, gchar *operation _U_) {
1621
1622     gboolean stream_is_big_endian;          /* big endianess */
1623 """
1624     
1625     template_helper_function_end = """\
1626 }
1627 """
1628     
1629     #
1630     # proto_reg_handoff() templates
1631     #
1632
1633
1634     template_proto_reg_handoff_start = """
1635 /* register me as handler for these interfaces */
1636
1637 void proto_register_handoff_giop_@dissector_name@(void) {
1638
1639 """
1640
1641     template_proto_reg_handoff_body = """
1642 #if 0
1643
1644 /* Register for Explicit Dissection */
1645
1646 register_giop_user_module(dissect_@dissector_name@, \"@protocol_name@\", \"@interface@\", proto_@dissector_name@ );     /* explicit dissector */
1647
1648 #endif
1649
1650 """
1651
1652     template_proto_reg_handoff_heuristic = """
1653
1654 /* Register for Heuristic Dissection */
1655
1656 register_giop_user(dissect_@dissector_name@, \"@protocol_name@\" ,proto_@dissector_name@);     /* heuristic dissector */ 
1657
1658 """
1659
1660     template_proto_reg_handoff_end = """
1661 }
1662 """
1663
1664
1665     #
1666     # Initialize the protocol
1667     #
1668
1669     template_protocol = """
1670 /* Initialise the protocol and subtree pointers */
1671
1672 static int proto_@dissector_name@ = -1;
1673
1674 static gint ett_@dissector_name@ = -1;
1675
1676 """
1677
1678
1679
1680     #
1681     # Initialize the boundary Alignment
1682     #
1683
1684     template_init_boundary = """
1685 /* Initialise the initial Alignment */
1686
1687 static guint32  boundary = GIOP_HEADER_SIZE;  /* initial value */
1688
1689 """
1690
1691
1692
1693
1694
1695     #
1696     # Initialize the Registered fields
1697     #
1698
1699     template_registered_fields = """
1700
1701 /* Initialise the Registered fields */
1702
1703 /* TODO - Use registered fields */
1704
1705 """
1706
1707     #
1708     # plugin_init and plugin_reg_handoff templates
1709     #
1710
1711     template_plugin_init = """
1712
1713 #ifndef __ETHEREAL_STATIC__
1714
1715 G_MODULE_EXPORT void
1716 plugin_reg_handoff(void){
1717    proto_register_handoff_giop_@dissector_name@();
1718 }
1719
1720 G_MODULE_EXPORT void
1721 plugin_init(plugin_address_table_t *pat
1722 #ifndef PLUGINS_NEED_ADDRESS_TABLE
1723 _U_
1724 #endif
1725 ){
1726    /* initialise the table of pointers needed in Win32 DLLs */
1727    plugin_address_table_init(pat);
1728    if (proto_@dissector_name@ == -1) {
1729      proto_register_giop_@dissector_name@();
1730    }
1731 }
1732
1733 #endif
1734
1735 """
1736
1737     #
1738     # proto_register_<dissector name>(void) templates
1739     #
1740
1741
1742     template_proto_register = """
1743
1744 /* Register the protocol with Ethereal */
1745
1746 void proto_register_giop_@dissector_name@(void) {
1747
1748    /* setup list of header fields */
1749
1750 #if 0
1751    static hf_register_info hf[] = {
1752
1753       /* no fields yet */
1754       
1755    };
1756 #endif
1757
1758    /* setup protocol subtree array */
1759
1760    static gint *ett[] = {
1761       &ett_@dissector_name@,
1762    };
1763
1764    /* Register the protocol name and description */
1765    
1766    proto_@dissector_name@ = proto_register_protocol(\"@description@\" , \"@protocol_name@\", \"giop-@dissector_name@\" );
1767
1768 #if 0
1769    proto_register_field_array(proto_@dissector_name@, hf, array_length(hf));
1770 #endif
1771    proto_register_subtree_array(ett,array_length(ett));
1772    
1773 }
1774
1775
1776 """
1777
1778     #
1779     # template for delegation code
1780     #
1781
1782     template_op_delegate_code = """\
1783 if (!strcmp(operation, @sname@_op )) {
1784    decode_@sname@(tvb, pinfo, tree, offset, header, operation);
1785    return TRUE;
1786 }
1787 """
1788
1789     #
1790     # Templates for the helper functions
1791     #
1792     #
1793     #
1794
1795
1796     template_helper_switch_msgtype_start = """\
1797
1798 stream_is_big_endian = is_big_endian(header);
1799
1800 switch(header->message_type) {
1801 """
1802
1803     template_helper_switch_msgtype_default_start = """\
1804 default:
1805
1806     /* Unknown GIOP Exception */
1807
1808     g_warning("Unknown GIOP Message");
1809     
1810 """
1811     template_helper_switch_msgtype_default_end = """\
1812 break;
1813 """
1814
1815     
1816     
1817     template_helper_switch_msgtype_end = """\
1818     
1819 } /* switch(header->message_type) */ 
1820 """
1821
1822     template_helper_switch_msgtype_request_start = """\
1823 case Request:
1824 """
1825     
1826     template_helper_switch_msgtype_request_end = """\
1827 break;
1828 """
1829
1830     template_helper_switch_msgtype_reply_start = """\
1831 case Reply:
1832 """
1833     
1834     template_helper_switch_msgtype_reply_no_exception_start = """\
1835 case NO_EXCEPTION:
1836 """
1837     
1838     template_helper_switch_msgtype_reply_no_exception_end = """\
1839 break;
1840 """
1841
1842     
1843     template_helper_switch_msgtype_reply_user_exception_start = """\
1844 case USER_EXCEPTION:
1845 """
1846     
1847     template_helper_switch_msgtype_reply_user_exception_end = """\
1848 break;
1849 """
1850
1851     template_helper_switch_msgtype_reply_default_start = """\
1852 default:
1853
1854     /* Unknown Exception */
1855
1856     g_warning("Unknown Exception ");
1857
1858     
1859 """
1860     
1861     template_helper_switch_msgtype_reply_default_end = """\
1862     break;
1863 """
1864
1865
1866     template_helper_switch_msgtype_reply_end = """\
1867 break;
1868 """
1869
1870     template_helper_switch_msgtype_default_start = """\
1871 default:
1872
1873     /* Unknown GIOP Message */
1874
1875     g_warning("Unknown GIOP Message");
1876     
1877 """
1878
1879     template_helper_switch_msgtype_default_end = """\
1880     break;
1881 """
1882
1883
1884     
1885     template_helper_switch_rep_status_start = """\
1886 switch(header->rep_status) {
1887 """
1888
1889     template_helper_switch_rep_status_default_start = """\
1890 default:
1891
1892     /* Unknown Reply Status */
1893
1894     g_warning("Unknown Reply Status");
1895     
1896 """
1897     
1898     template_helper_switch_rep_status_default_end = """\
1899     break;  
1900 """
1901     
1902     template_helper_switch_rep_status_end = """\
1903
1904 }   /* switch(header->message_type) */
1905
1906 break;   
1907 """
1908
1909             
1910
1911
1912
1913     #
1914     # Templates for get_CDR_xxx accessors
1915     #
1916     
1917     template_get_CDR_ulong = """\
1918 u_octet4 = get_CDR_ulong(tvb,offset,stream_is_big_endian, boundary);
1919 if (tree) {
1920    proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %u",u_octet4);
1921 }
1922 """
1923
1924     template_get_CDR_short = """\
1925 s_octet2 = get_CDR_short(tvb,offset,stream_is_big_endian, boundary);
1926 if (tree) {
1927    proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %i",s_octet2);
1928 }
1929 """
1930     
1931     template_get_CDR_void = """\
1932
1933 /* Function returns void */
1934
1935 """
1936
1937     template_get_CDR_long = """\
1938 s_octet4 = get_CDR_long(tvb,offset,stream_is_big_endian, boundary);
1939 if (tree) {
1940    proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %i",s_octet4);
1941 }
1942 """
1943
1944     template_get_CDR_ushort = """\
1945 u_octet2 = get_CDR_ushort(tvb,offset,stream_is_big_endian, boundary);
1946 if (tree) {
1947    proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %u",u_octet2);
1948 }
1949 """
1950     template_get_CDR_float = """\
1951 my_float = get_CDR_float(tvb,offset,stream_is_big_endian, boundary);
1952 if (tree) {
1953    proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %.6e",my_float);
1954 }
1955 """
1956     
1957     template_get_CDR_double = """\
1958 my_double = get_CDR_double(tvb,offset,stream_is_big_endian, boundary);
1959 if (tree) {
1960    proto_tree_add_text(tree,tvb,*offset-8,8,"@varname@ = %.15e",my_double);
1961 }
1962 """
1963     
1964     template_get_CDR_boolean = """\
1965 u_octet1 = get_CDR_boolean(tvb,offset);
1966 if (tree) {
1967    proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1968 }
1969 """
1970     
1971     template_get_CDR_char = """\
1972 u_octet1 = get_CDR_char(tvb,offset);
1973 if (tree) {
1974    proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1975 }
1976 """
1977     
1978     template_get_CDR_octet = """\
1979 u_octet1 = get_CDR_octet(tvb,offset);
1980 if (tree) {
1981    proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1982 }
1983 """
1984
1985
1986
1987     template_get_CDR_any = """\
1988 get_CDR_any(tvb,tree,offset,stream_is_big_endian, boundary, header);
1989
1990 """
1991
1992     template_get_CDR_fixed = """\
1993 get_CDR_fixed(tvb, &seq, offset, @digits@, @scale@);
1994 if (tree) {
1995    proto_tree_add_text(tree,tvb,*offset-@length@, @length@, "@varname@ < @digits@, @scale@> = %s",seq);   
1996 }
1997
1998 g_free(seq);          /*  free buffer  */
1999 seq = NULL;
2000
2001 """
2002
2003             
2004     template_get_CDR_enum_symbolic = """\
2005     
2006 u_octet4 = get_CDR_enum(tvb,offset,stream_is_big_endian, boundary);
2007 if (tree) {
2008    proto_tree_add_text(tree,tvb,*offset-4,4,"Enum value = %u (%s)",u_octet4,val_to_str(u_octet4,@valstringarray@,"Unknown Enum Value"));
2009 }
2010 """
2011
2012
2013
2014     template_get_CDR_string = """\
2015 u_octet4 = get_CDR_string(tvb, &seq, offset, stream_is_big_endian, boundary);
2016 if (tree) {
2017    proto_tree_add_text(tree,tvb,*offset-4-u_octet4,4,"length = %u",u_octet4);
2018    if (u_octet4 > 0)
2019       proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ = %s",seq);
2020    
2021 }
2022
2023 g_free(seq);          /*  free buffer  */
2024 seq = NULL;
2025 """
2026
2027    
2028     template_get_CDR_wstring = """\
2029 u_octet4 = get_CDR_wstring(tvb, &seq, offset, stream_is_big_endian, boundary, header);
2030 if (tree) {
2031    proto_tree_add_text(tree,tvb,*offset-4-u_octet4,4,"length = %u",u_octet4);
2032    if (u_octet4 > 0)
2033       proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ = %s",seq);
2034    
2035 }
2036
2037 g_free(seq);          /*  free buffer  */
2038 seq = NULL;
2039 """
2040
2041    
2042     template_get_CDR_wchar = """\
2043 s_octet1 = get_CDR_wchar(tvb, &seq, offset, header);
2044 if (tree) {
2045     if (s_octet1 > 0)
2046         proto_tree_add_text(tree,tvb,*offset-1-s_octet1,1,"length = %u",s_octet1);
2047
2048     if (s_octet1 < 0)
2049         s_octet1 = -s_octet1;
2050
2051     if (s_octet1 > 0)
2052         proto_tree_add_text(tree,tvb,*offset-s_octet1,s_octet1,"@varname@ = %s",seq);
2053            
2054 }
2055         
2056 g_free(seq);          /*  free buffer  */
2057 seq = NULL;
2058 """
2059
2060
2061
2062
2063     template_get_CDR_TypeCode = """\
2064 u_octet4 = get_CDR_typeCode(tvb, tree, offset, stream_is_big_endian, boundary, header);
2065
2066 """
2067     
2068     template_get_CDR_object = """\
2069 get_CDR_object(tvb, pinfo, tree, offset, stream_is_big_endian, boundary);
2070
2071 """
2072     
2073
2074     template_get_CDR_sequence_length = """\
2075 u_octet4_loop_@seqname@ = get_CDR_ulong(tvb, offset, stream_is_big_endian, boundary);
2076 if (tree) {
2077    proto_tree_add_text(tree,tvb,*offset-4, 4 ,"Seq length of @seqname@ = %u",u_octet4_loop_@seqname@);   
2078 }
2079 """
2080
2081     template_get_CDR_sequence_loop_start = """\
2082 for (i_@seqname@=0; i_@seqname@ < u_octet4_loop_@seqname@; i_@seqname@++) {
2083 """
2084     template_get_CDR_sequence_loop_end = """\
2085 }
2086 """
2087     
2088
2089
2090     template_get_CDR_array_start = """\
2091 for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
2092 """
2093     template_get_CDR_array_end = """\
2094 }
2095 """
2096
2097     template_get_CDR_array_comment = """\
2098 /* Array: @aname@[ @asize@]  */
2099 """
2100             
2101
2102
2103
2104     template_structure_start = """\
2105 /*  Begin struct \"@name@\"  */
2106 """
2107
2108
2109     template_structure_end = """\
2110 /*  End struct \"@name@\"  */
2111 """
2112
2113     template_union_start = """\
2114 /*  Begin union \"@name@\"  */
2115 """
2116
2117
2118     template_union_end = """\
2119 /*  End union \"@name@\"  */
2120 """
2121
2122
2123
2124
2125 #
2126 # Program Header Template
2127 #
2128
2129     template_Header = """\
2130 /* packet-@dissector_name@.c
2131  * Routines for IDL dissection
2132  *
2133  * Autogenerated from idl2eth
2134  * Copyright 2001 Frank Singleton <frank.singleton@@ericsson.com>
2135  */
2136
2137 """
2138
2139     template_ethereal_copyright = """\
2140 /*
2141  * Ethereal - Network traffic analyzer
2142  * By Gerald Combs
2143  * Copyright 1999 Gerald Combs
2144  */
2145  
2146 """
2147     
2148
2149
2150 #
2151 # GPL Template
2152 #
2153
2154
2155     template_GPL = """\
2156 /*
2157  * This program is free software; you can redistribute it and/or
2158  * modify it under the terms of the GNU General Public License
2159  * as published by the Free Software Foundation; either version 2
2160  * of the License, or (at your option) any later version.
2161  * 
2162  * This program is distributed in the hope that it will be useful,
2163  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2164  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2165  * GNU General Public License for more details.
2166  * 
2167  * You should have received a copy of the GNU General Public License
2168  * along with this program; if not, write to the Free Software
2169  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
2170  *
2171  */
2172 """
2173
2174 #
2175 # Includes template
2176 #
2177
2178     template_Includes = """\
2179
2180 #ifdef HAVE_CONFIG_H
2181 # include "config.h"
2182 #endif
2183
2184 #include "plugins/plugin_api.h"
2185
2186 #include <stdio.h>
2187 #include <stdlib.h>
2188 #include <gmodule.h>
2189
2190 #ifdef NEED_SNPRINTF_H
2191 # include "snprintf.h"
2192 #endif
2193
2194 #include <string.h>
2195 #include <glib.h>
2196 #include <epan/packet.h>
2197 #include <epan/proto.h>
2198 #include "packet-giop.h"
2199
2200 #include "plugins/plugin_api_defs.h"
2201
2202 #ifndef __ETHEREAL_STATIC__
2203 G_MODULE_EXPORT const gchar version[] = "0.0.1";
2204 #endif
2205
2206 """
2207
2208     
2209 #
2210 # Main dissector entry templates
2211 #
2212
2213     template_main_dissector_start = """\
2214 static gboolean dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, gchar *operation, gchar *idlname _U_) {
2215
2216     proto_item *ti = NULL;
2217     proto_tree *tree = NULL;            /* init later, inside if(tree) */
2218     
2219     gboolean be;                        /* big endianess */
2220
2221     if (check_col(pinfo->cinfo, COL_PROTOCOL))
2222        col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
2223
2224 /* 
2225  * Do not clear COL_INFO, as nothing is being written there by 
2226  * this dissector yet. So leave it as is from the GIOP dissector.
2227  * TODO: add something useful to COL_INFO 
2228  *  if (check_col(pinfo->cinfo, COL_INFO))
2229  *     col_clear(pinfo->cinfo, COL_INFO);
2230  */
2231
2232     if (ptree) {
2233        ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, tvb_length(tvb) - *offset, FALSE);
2234        tree = proto_item_add_subtree(ti, ett_@dissname@);
2235     }  
2236
2237
2238     be = is_big_endian(header);         /* get endianess - TODO use passed in stream_is_big_endian instead ? */
2239
2240     /* If we have a USER Exception, then decode it and return */
2241
2242     if ((header->message_type == Reply) && (header->rep_status == USER_EXCEPTION)) {
2243
2244        return decode_user_exception(tvb, pinfo, tree, offset, header, operation);
2245
2246     }
2247
2248     
2249 """
2250
2251
2252        
2253
2254     template_main_dissector_switch_msgtype_start = """\
2255 switch(header->message_type) {
2256 """
2257     
2258     template_main_dissector_switch_msgtype_start_request_reply = """\
2259 case Request:
2260 case Reply:
2261
2262 """
2263
2264     template_main_dissector_switch_msgtype_end_request_reply = """\
2265
2266 break;
2267 """
2268
2269
2270     template_main_dissector_switch_msgtype_all_other_msgtype = """\
2271 case CancelRequest:
2272 case LocateRequest:
2273 case LocateReply:
2274 case CloseConnection:
2275 case MessageError:
2276 case Fragment:
2277    return FALSE;      /* not handled yet */
2278
2279 default:
2280    return FALSE;      /* not handled yet */
2281
2282 }   /* switch */
2283
2284 """
2285
2286
2287     template_main_dissector_end = """\
2288
2289     return FALSE;
2290
2291 }  /* End of main dissector  */
2292
2293 """
2294
2295
2296     
2297
2298
2299
2300
2301 #-------------------------------------------------------------#
2302 #             Exception handling templates                    #
2303 #-------------------------------------------------------------#
2304
2305
2306
2307
2308
2309
2310
2311     template_exception_helpers_start = """\
2312 /*  Begin Exception Helper Functions  */
2313
2314 """
2315     
2316     template_exception_helpers_end = """\
2317     
2318 /*  End Exception Helper Functions  */
2319
2320 """
2321
2322
2323
2324 #
2325 # Templates for declaration of string constants for user exceptions.
2326 #
2327     
2328     template_comment_user_exceptions_string_declare_start = """\
2329 /*  Begin Exception (containing members) String  Declare  */
2330
2331 """
2332     
2333     template_user_exceptions_declare = """static const char user_exception_@sname@[] = \"@exname@\" ; """
2334
2335     
2336     template_comment_user_exceptions_string_declare_end = """\
2337     
2338 /*  End Exception (containing members) String Declare  */
2339
2340 """
2341
2342
2343     
2344
2345 #
2346 # template for Main delegator for exception handling
2347 #
2348
2349     template_main_exception_delegator_start = """\
2350
2351 /*
2352  * Main delegator for exception handling
2353  *
2354  */
2355  
2356 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_ ) {
2357     
2358     gboolean be _U_;                        /* big endianess */
2359
2360     
2361 """
2362
2363
2364 #
2365 # template for exception delegation code body
2366 #
2367     template_ex_delegate_code = """\
2368 if (!strcmp(header->exception_id, user_exception_@sname@ )) {
2369    decode_ex_@sname@(tvb, pinfo, tree, offset, header, operation);   /*  @exname@  */
2370    return TRUE;
2371 }
2372
2373 """
2374
2375
2376 #
2377 # End of Main delegator for exception handling
2378 #
2379
2380     template_main_exception_delegator_end = """\
2381
2382
2383     return FALSE;    /* user exception not found */
2384
2385 }
2386     
2387 """
2388     
2389 #
2390 # template for exception helper code
2391 #
2392
2393
2394     template_exception_helper_function_start = """\
2395
2396 /* Exception = @exname@ */
2397
2398 static void decode_ex_@sname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2399
2400     gboolean stream_is_big_endian;          /* big endianess */
2401 """
2402
2403
2404
2405     #
2406     # Template for the helper function
2407     # to get stream endianess from header
2408     #
2409
2410     template_exception_helper_function_get_endianess = """\
2411
2412 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2413
2414 """
2415
2416     
2417     template_exception_helper_function_end = """\
2418 }
2419 """
2420
2421     
2422 #
2423 # template for struct helper code
2424 #
2425
2426
2427     template_struct_helper_function_start = """\
2428
2429 /* Struct = @stname@ */
2430
2431 static void decode_@sname@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2432
2433     gboolean stream_is_big_endian;          /* big endianess */
2434 """
2435
2436
2437
2438
2439     #
2440     # Template for the helper function
2441     # to get stream endianess from header
2442     #
2443
2444     template_struct_helper_function_get_endianess = """\
2445
2446 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2447
2448 """
2449
2450     
2451     template_struct_helper_function_end = """\
2452 }
2453 """
2454
2455 #
2456 # template for union helper code
2457 #
2458
2459
2460     template_union_helper_function_start = """\
2461
2462 /* Union = @unname@ */
2463
2464 static void decode_@sname@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2465
2466     gboolean stream_is_big_endian;          /* big endianess */
2467 """
2468
2469
2470
2471
2472     #
2473     # Template for the helper function
2474     # to get stream endianess from header
2475     #
2476
2477     template_union_helper_function_get_endianess = """\
2478
2479 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2480
2481 """
2482
2483     
2484     template_union_helper_function_end = """\
2485 }
2486 """
2487
2488
2489
2490 #-------------------------------------------------------------#
2491 #             Value string  templates                         #
2492 #-------------------------------------------------------------#
2493
2494     template_value_string_start = """\
2495 static const value_string @valstringname@[] = {
2496 """
2497
2498     template_value_string_entry = """\
2499    { @intval@, \"@description@\" }, """    
2500     
2501     template_value_string_end = """\
2502    { 0,       NULL },
2503 };
2504     
2505 """
2506
2507     
2508
2509 #-------------------------------------------------------------#
2510 #             Enum   handling templates                       #
2511 #-------------------------------------------------------------#
2512
2513     template_comment_enums_start = """\
2514 /*
2515  * IDL Enums Start
2516  */
2517  
2518  """
2519     
2520     template_comment_enums_end = """\
2521 /*
2522  * IDL Enums End
2523  */
2524  
2525  """
2526     
2527     template_comment_enum_comment = """\
2528 /*
2529  * Enum = @ename@
2530  */
2531  
2532  """
2533
2534
2535
2536 #-------------------------------------------------------------#
2537 #             Attribute handling templates                    #
2538 #-------------------------------------------------------------#
2539
2540
2541     template_comment_attributes_start = """\
2542 /*
2543  * IDL Attributes Start
2544  */
2545  
2546  """
2547
2548     #
2549     # get/set accessor method names are language mapping dependant.
2550     #
2551
2552     template_attributes_declare_Java_get = """static const char get_@sname@_at[] = \"_get_@atname@\" ;"""
2553     template_attributes_declare_Java_set = """static const char set_@sname@_at[] = \"_set_@atname@\" ;"""
2554  
2555
2556     template_comment_attributes_end = """
2557 /*
2558  * IDL Attributes End
2559  */
2560  
2561 """
2562
2563
2564     #
2565     # template for Attribute delegation code
2566     #
2567     # Note: _get_xxx() should only be called for Reply with NO_EXCEPTION
2568     # Note: _set_xxx() should only be called for Request
2569     #
2570     #
2571
2572     template_at_delegate_code_get = """\
2573 if (!strcmp(operation, get_@sname@_at ) && (header->message_type == Reply) && (header->rep_status == NO_EXCEPTION) ) {
2574    decode_get_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2575    return TRUE;
2576 }
2577 """
2578     
2579     template_at_delegate_code_set = """\
2580 if (!strcmp(operation, set_@sname@_at ) && (header->message_type == Request) ) {
2581    decode_set_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2582    return TRUE;
2583 }
2584 """
2585         
2586
2587
2588     template_attribute_helpers_start = """\
2589 /*  Begin Attribute Helper Functions  */
2590
2591 """
2592     
2593     template_attribute_helpers_end = """\
2594     
2595 /*  End Attribute Helper Functions  */
2596
2597 """
2598
2599 #
2600 # template for attribute helper code
2601 #
2602
2603
2604     template_attribute_helper_function_start = """\
2605
2606 /* Attribute = @atname@ */
2607
2608 static void decode_@sname@_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2609
2610     gboolean stream_is_big_endian;          /* big endianess */
2611 """
2612
2613
2614
2615     #
2616     # Template for the helper function
2617     # to get stream endianess from header
2618     #
2619
2620     template_attribute_helper_function_get_endianess = """\
2621
2622 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2623
2624 """
2625
2626     
2627     template_attribute_helper_function_end = """\
2628 }
2629 """
2630
2631     
2632 #-------------------------------------------------------------#
2633 #                     Debugging  templates                    #
2634 #-------------------------------------------------------------#
2635
2636
2637     #
2638     # Template for outputting TODO "C" comments
2639     # so user know I need ti improve something.
2640     #
2641
2642     template_debug_TODO = """\
2643
2644 /* TODO - @message@ */
2645 """
2646
2647     #
2648     # Template for outputting WARNING "C" comments
2649     # so user know if I have found a problem.
2650     #
2651
2652     template_debug_WARNING = """\
2653
2654 /* WARNING - @message@ */
2655 """
2656
2657
2658     
2659 #-------------------------------------------------------------#
2660 #                     IDL Union  templates                    #
2661 #-------------------------------------------------------------#
2662
2663
2664
2665     template_comment_union_code_start = """\
2666 /*
2667  * IDL Union Start - @uname@
2668  */
2669  
2670  """
2671
2672
2673     template_comment_union_code_end = """
2674 /*
2675  * IDL union End - @uname@
2676  */
2677  
2678 """
2679
2680     template_comment_union_code_discriminant = """\
2681 /*
2682  * IDL Union - Discriminant - @uname@
2683  */
2684  
2685  """
2686
2687     #
2688     # Cast Unions types to something appropriate
2689     # Enum value cast to guint32, all others cast to gint32 
2690     # as omniidl accessor returns integer or Enum.
2691     #
2692
2693     template_union_code_save_discriminant_enum = """\
2694 disc_s_@discname@ = (gint32) u_octet4;     /* save Enum Value  discriminant and cast to gint32 */
2695 """
2696     
2697     template_union_code_save_discriminant_long = """\
2698 disc_s_@discname@ = (gint32) s_octet4;     /* save gint32 discriminant and cast to gint32 */
2699 """
2700
2701     template_union_code_save_discriminant_ulong = """\
2702 disc_s_@discname@ = (gint32) u_octet4;     /* save guint32 discriminant and cast to gint32 */
2703 """    
2704
2705     template_union_code_save_discriminant_short = """\
2706 disc_s_@discname@ = (gint32) s_octet2;     /* save gint16 discriminant and cast to gint32 */
2707 """    
2708
2709     template_union_code_save_discriminant_ushort = """\
2710 disc_s_@discname@ = (gint32) u_octet2;     /* save guint16 discriminant and cast to gint32 */
2711 """    
2712     
2713     template_union_code_save_discriminant_char = """\
2714 disc_s_@discname@ = (gint32) u_octet1;     /* save guint1 discriminant and cast to gint32 */
2715 """
2716     
2717     template_union_code_save_discriminant_boolean = """\
2718 disc_s_@discname@ = (gint32) u_octet1;     /* save guint1 discriminant and cast to gint32 */
2719 """
2720
2721
2722     template_comment_union_code_label_compare_start = """\
2723 if (disc_s_@discname@ == @labelval@) {
2724
2725  """
2726     template_comment_union_code_label_compare_end = """\
2727     return;     /* End Compare for this discriminant type */
2728 }
2729
2730  """
2731
2732
2733     template_comment_union_code_label_default_start = """
2734 /* Default Union Case Start */
2735  
2736 """
2737
2738     template_comment_union_code_label_default_end = """\
2739 /* Default Union Case End */
2740  
2741  """
2742
2743     #
2744     # Templates for function prototypes.
2745     # This is used in genDeclares() for declaring function prototypes
2746     # for structs and union helper functions.
2747     #
2748
2749     template_prototype_struct_start = """
2750 /* Struct prototype declaration Start */
2751  
2752 """
2753     
2754     template_prototype_struct_end = """
2755 /* Struct prototype declaration End */
2756  
2757 """
2758     
2759     template_prototype_struct_body = """
2760         
2761 /* Struct = @stname@ */
2762
2763 static void decode_@name@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
2764
2765 """
2766
2767     template_decode_struct = """
2768         
2769 decode_@name@_st(tvb, pinfo, tree, offset, header, operation);
2770
2771 """
2772
2773
2774     
2775     
2776     template_prototype_union_start = """
2777 /* Union prototype declaration Start */
2778  
2779 """
2780     
2781     template_prototype_union_end = """
2782 /* Union prototype declaration End */
2783  
2784 """
2785     
2786     template_prototype_union_body = """
2787         
2788 /* Union = @unname@ */
2789
2790 static void decode_@name@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
2791
2792 """
2793
2794     template_decode_union = """
2795         
2796 decode_@name@_un(tvb, pinfo, tree, offset, header, operation);
2797
2798 """
2799     
2800