From Paolo Frigo :
[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_register()
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_register(self):
1454         self.st.out(self.template_plugin_register, 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_register and plugin_reg_handoff templates
1709     #
1710
1711     template_plugin_register = """
1712
1713 #ifndef ENABLE_STATIC
1714
1715 G_MODULE_EXPORT void
1716 plugin_register(void)
1717 {
1718    if (proto_@dissector_name@ == -1) {
1719      proto_register_giop_@dissector_name@();
1720    }
1721 }
1722
1723 G_MODULE_EXPORT void
1724 plugin_reg_handoff(void){
1725    proto_register_handoff_giop_@dissector_name@();
1726 }
1727
1728 #endif
1729
1730 """
1731
1732     #
1733     # proto_register_<dissector name>(void) templates
1734     #
1735
1736
1737     template_proto_register = """
1738
1739 /* Register the protocol with Ethereal */
1740
1741 void proto_register_giop_@dissector_name@(void) {
1742
1743    /* setup list of header fields */
1744
1745 #if 0
1746    static hf_register_info hf[] = {
1747
1748       /* no fields yet */
1749       
1750    };
1751 #endif
1752
1753    /* setup protocol subtree array */
1754
1755    static gint *ett[] = {
1756       &ett_@dissector_name@,
1757    };
1758
1759    /* Register the protocol name and description */
1760    
1761    proto_@dissector_name@ = proto_register_protocol(\"@description@\" , \"@protocol_name@\", \"giop-@dissector_name@\" );
1762
1763 #if 0
1764    proto_register_field_array(proto_@dissector_name@, hf, array_length(hf));
1765 #endif
1766    proto_register_subtree_array(ett,array_length(ett));
1767    
1768 }
1769
1770
1771 """
1772
1773     #
1774     # template for delegation code
1775     #
1776
1777     template_op_delegate_code = """\
1778 if (!strcmp(operation, @sname@_op )) {
1779    decode_@sname@(tvb, pinfo, tree, offset, header, operation);
1780    return TRUE;
1781 }
1782 """
1783
1784     #
1785     # Templates for the helper functions
1786     #
1787     #
1788     #
1789
1790
1791     template_helper_switch_msgtype_start = """\
1792
1793 stream_is_big_endian = is_big_endian(header);
1794
1795 switch(header->message_type) {
1796 """
1797
1798     template_helper_switch_msgtype_default_start = """\
1799 default:
1800
1801     /* Unknown GIOP Exception */
1802
1803     g_warning("Unknown GIOP Message");
1804     
1805 """
1806     template_helper_switch_msgtype_default_end = """\
1807 break;
1808 """
1809
1810     
1811     
1812     template_helper_switch_msgtype_end = """\
1813     
1814 } /* switch(header->message_type) */ 
1815 """
1816
1817     template_helper_switch_msgtype_request_start = """\
1818 case Request:
1819 """
1820     
1821     template_helper_switch_msgtype_request_end = """\
1822 break;
1823 """
1824
1825     template_helper_switch_msgtype_reply_start = """\
1826 case Reply:
1827 """
1828     
1829     template_helper_switch_msgtype_reply_no_exception_start = """\
1830 case NO_EXCEPTION:
1831 """
1832     
1833     template_helper_switch_msgtype_reply_no_exception_end = """\
1834 break;
1835 """
1836
1837     
1838     template_helper_switch_msgtype_reply_user_exception_start = """\
1839 case USER_EXCEPTION:
1840 """
1841     
1842     template_helper_switch_msgtype_reply_user_exception_end = """\
1843 break;
1844 """
1845
1846     template_helper_switch_msgtype_reply_default_start = """\
1847 default:
1848
1849     /* Unknown Exception */
1850
1851     g_warning("Unknown Exception ");
1852
1853     
1854 """
1855     
1856     template_helper_switch_msgtype_reply_default_end = """\
1857     break;
1858 """
1859
1860
1861     template_helper_switch_msgtype_reply_end = """\
1862 break;
1863 """
1864
1865     template_helper_switch_msgtype_default_start = """\
1866 default:
1867
1868     /* Unknown GIOP Message */
1869
1870     g_warning("Unknown GIOP Message");
1871     
1872 """
1873
1874     template_helper_switch_msgtype_default_end = """\
1875     break;
1876 """
1877
1878
1879     
1880     template_helper_switch_rep_status_start = """\
1881 switch(header->rep_status) {
1882 """
1883
1884     template_helper_switch_rep_status_default_start = """\
1885 default:
1886
1887     /* Unknown Reply Status */
1888
1889     g_warning("Unknown Reply Status");
1890     
1891 """
1892     
1893     template_helper_switch_rep_status_default_end = """\
1894     break;  
1895 """
1896     
1897     template_helper_switch_rep_status_end = """\
1898
1899 }   /* switch(header->message_type) */
1900
1901 break;   
1902 """
1903
1904             
1905
1906
1907
1908     #
1909     # Templates for get_CDR_xxx accessors
1910     #
1911     
1912     template_get_CDR_ulong = """\
1913 u_octet4 = get_CDR_ulong(tvb,offset,stream_is_big_endian, boundary);
1914 if (tree) {
1915    proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %u",u_octet4);
1916 }
1917 """
1918
1919     template_get_CDR_short = """\
1920 s_octet2 = get_CDR_short(tvb,offset,stream_is_big_endian, boundary);
1921 if (tree) {
1922    proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %i",s_octet2);
1923 }
1924 """
1925     
1926     template_get_CDR_void = """\
1927
1928 /* Function returns void */
1929
1930 """
1931
1932     template_get_CDR_long = """\
1933 s_octet4 = get_CDR_long(tvb,offset,stream_is_big_endian, boundary);
1934 if (tree) {
1935    proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %i",s_octet4);
1936 }
1937 """
1938
1939     template_get_CDR_ushort = """\
1940 u_octet2 = get_CDR_ushort(tvb,offset,stream_is_big_endian, boundary);
1941 if (tree) {
1942    proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %u",u_octet2);
1943 }
1944 """
1945     template_get_CDR_float = """\
1946 my_float = get_CDR_float(tvb,offset,stream_is_big_endian, boundary);
1947 if (tree) {
1948    proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %.6e",my_float);
1949 }
1950 """
1951     
1952     template_get_CDR_double = """\
1953 my_double = get_CDR_double(tvb,offset,stream_is_big_endian, boundary);
1954 if (tree) {
1955    proto_tree_add_text(tree,tvb,*offset-8,8,"@varname@ = %.15e",my_double);
1956 }
1957 """
1958     
1959     template_get_CDR_boolean = """\
1960 u_octet1 = get_CDR_boolean(tvb,offset);
1961 if (tree) {
1962    proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1963 }
1964 """
1965     
1966     template_get_CDR_char = """\
1967 u_octet1 = get_CDR_char(tvb,offset);
1968 if (tree) {
1969    proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1970 }
1971 """
1972     
1973     template_get_CDR_octet = """\
1974 u_octet1 = get_CDR_octet(tvb,offset);
1975 if (tree) {
1976    proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1977 }
1978 """
1979
1980
1981
1982     template_get_CDR_any = """\
1983 get_CDR_any(tvb,tree,offset,stream_is_big_endian, boundary, header);
1984
1985 """
1986
1987     template_get_CDR_fixed = """\
1988 get_CDR_fixed(tvb, &seq, offset, @digits@, @scale@);
1989 if (tree) {
1990    proto_tree_add_text(tree,tvb,*offset-@length@, @length@, "@varname@ < @digits@, @scale@> = %s",seq);   
1991 }
1992
1993 g_free(seq);          /*  free buffer  */
1994 seq = NULL;
1995
1996 """
1997
1998             
1999     template_get_CDR_enum_symbolic = """\
2000     
2001 u_octet4 = get_CDR_enum(tvb,offset,stream_is_big_endian, boundary);
2002 if (tree) {
2003    proto_tree_add_text(tree,tvb,*offset-4,4,"Enum value = %u (%s)",u_octet4,val_to_str(u_octet4,@valstringarray@,"Unknown Enum Value"));
2004 }
2005 """
2006
2007
2008
2009     template_get_CDR_string = """\
2010 u_octet4 = get_CDR_string(tvb, &seq, offset, stream_is_big_endian, boundary);
2011 if (tree) {
2012    proto_tree_add_text(tree,tvb,*offset-4-u_octet4,4,"length = %u",u_octet4);
2013    if (u_octet4 > 0)
2014       proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ = %s",seq);
2015    
2016 }
2017
2018 g_free(seq);          /*  free buffer  */
2019 seq = NULL;
2020 """
2021
2022    
2023     template_get_CDR_wstring = """\
2024 u_octet4 = get_CDR_wstring(tvb, &seq, offset, stream_is_big_endian, boundary, header);
2025 if (tree) {
2026    proto_tree_add_text(tree,tvb,*offset-4-u_octet4,4,"length = %u",u_octet4);
2027    if (u_octet4 > 0)
2028       proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ = %s",seq);
2029    
2030 }
2031
2032 g_free(seq);          /*  free buffer  */
2033 seq = NULL;
2034 """
2035
2036    
2037     template_get_CDR_wchar = """\
2038 s_octet1 = get_CDR_wchar(tvb, &seq, offset, header);
2039 if (tree) {
2040     if (s_octet1 > 0)
2041         proto_tree_add_text(tree,tvb,*offset-1-s_octet1,1,"length = %u",s_octet1);
2042
2043     if (s_octet1 < 0)
2044         s_octet1 = -s_octet1;
2045
2046     if (s_octet1 > 0)
2047         proto_tree_add_text(tree,tvb,*offset-s_octet1,s_octet1,"@varname@ = %s",seq);
2048            
2049 }
2050         
2051 g_free(seq);          /*  free buffer  */
2052 seq = NULL;
2053 """
2054
2055
2056
2057
2058     template_get_CDR_TypeCode = """\
2059 u_octet4 = get_CDR_typeCode(tvb, tree, offset, stream_is_big_endian, boundary, header);
2060
2061 """
2062     
2063     template_get_CDR_object = """\
2064 get_CDR_object(tvb, pinfo, tree, offset, stream_is_big_endian, boundary);
2065
2066 """
2067     
2068
2069     template_get_CDR_sequence_length = """\
2070 u_octet4_loop_@seqname@ = get_CDR_ulong(tvb, offset, stream_is_big_endian, boundary);
2071 if (tree) {
2072    proto_tree_add_text(tree,tvb,*offset-4, 4 ,"Seq length of @seqname@ = %u",u_octet4_loop_@seqname@);   
2073 }
2074 """
2075
2076     template_get_CDR_sequence_loop_start = """\
2077 for (i_@seqname@=0; i_@seqname@ < u_octet4_loop_@seqname@; i_@seqname@++) {
2078 """
2079     template_get_CDR_sequence_loop_end = """\
2080 }
2081 """
2082     
2083
2084
2085     template_get_CDR_array_start = """\
2086 for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
2087 """
2088     template_get_CDR_array_end = """\
2089 }
2090 """
2091
2092     template_get_CDR_array_comment = """\
2093 /* Array: @aname@[ @asize@]  */
2094 """
2095             
2096
2097
2098
2099     template_structure_start = """\
2100 /*  Begin struct \"@name@\"  */
2101 """
2102
2103
2104     template_structure_end = """\
2105 /*  End struct \"@name@\"  */
2106 """
2107
2108     template_union_start = """\
2109 /*  Begin union \"@name@\"  */
2110 """
2111
2112
2113     template_union_end = """\
2114 /*  End union \"@name@\"  */
2115 """
2116
2117
2118
2119
2120 #
2121 # Program Header Template
2122 #
2123
2124     template_Header = """\
2125 /* packet-@dissector_name@.c
2126  * Routines for IDL dissection
2127  *
2128  * Autogenerated from idl2eth
2129  * Copyright 2001 Frank Singleton <frank.singleton@@ericsson.com>
2130  */
2131
2132 """
2133
2134     template_ethereal_copyright = """\
2135 /*
2136  * Ethereal - Network traffic analyzer
2137  * By Gerald Combs
2138  * Copyright 1999 Gerald Combs
2139  */
2140  
2141 """
2142     
2143
2144
2145 #
2146 # GPL Template
2147 #
2148
2149
2150     template_GPL = """\
2151 /*
2152  * This program is free software; you can redistribute it and/or
2153  * modify it under the terms of the GNU General Public License
2154  * as published by the Free Software Foundation; either version 2
2155  * of the License, or (at your option) any later version.
2156  * 
2157  * This program is distributed in the hope that it will be useful,
2158  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2159  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2160  * GNU General Public License for more details.
2161  * 
2162  * You should have received a copy of the GNU General Public License
2163  * along with this program; if not, write to the Free Software
2164  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
2165  *
2166  */
2167 """
2168
2169 #
2170 # Includes template
2171 #
2172
2173     template_Includes = """\
2174
2175 #ifdef HAVE_CONFIG_H
2176 # include "config.h"
2177 #endif
2178
2179 #include <stdio.h>
2180 #include <stdlib.h>
2181 #include <gmodule.h>
2182
2183 #ifdef NEED_SNPRINTF_H
2184 # include "snprintf.h"
2185 #endif
2186
2187 #include <string.h>
2188 #include <glib.h>
2189 #include <epan/packet.h>
2190 #include <epan/proto.h>
2191 #include <epan/dissectors/packet-giop.h>
2192
2193 #ifndef ENABLE_STATIC
2194 G_MODULE_EXPORT const gchar version[] = "0.0.1";
2195 #endif
2196
2197 """
2198
2199     
2200 #
2201 # Main dissector entry templates
2202 #
2203
2204     template_main_dissector_start = """\
2205 static gboolean dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, gchar *operation, gchar *idlname _U_) {
2206
2207     proto_item *ti = NULL;
2208     proto_tree *tree = NULL;            /* init later, inside if(tree) */
2209     
2210     gboolean be;                        /* big endianess */
2211
2212     if (check_col(pinfo->cinfo, COL_PROTOCOL))
2213        col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
2214
2215 /* 
2216  * Do not clear COL_INFO, as nothing is being written there by 
2217  * this dissector yet. So leave it as is from the GIOP dissector.
2218  * TODO: add something useful to COL_INFO 
2219  *  if (check_col(pinfo->cinfo, COL_INFO))
2220  *     col_clear(pinfo->cinfo, COL_INFO);
2221  */
2222
2223     if (ptree) {
2224        ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, -1, FALSE);
2225        tree = proto_item_add_subtree(ti, ett_@dissname@);
2226     }  
2227
2228
2229     be = is_big_endian(header);         /* get endianess - TODO use passed in stream_is_big_endian instead ? */
2230
2231     /* If we have a USER Exception, then decode it and return */
2232
2233     if ((header->message_type == Reply) && (header->rep_status == USER_EXCEPTION)) {
2234
2235        return decode_user_exception(tvb, pinfo, tree, offset, header, operation);
2236
2237     }
2238
2239     
2240 """
2241
2242
2243        
2244
2245     template_main_dissector_switch_msgtype_start = """\
2246 switch(header->message_type) {
2247 """
2248     
2249     template_main_dissector_switch_msgtype_start_request_reply = """\
2250 case Request:
2251 case Reply:
2252
2253 """
2254
2255     template_main_dissector_switch_msgtype_end_request_reply = """\
2256
2257 break;
2258 """
2259
2260
2261     template_main_dissector_switch_msgtype_all_other_msgtype = """\
2262 case CancelRequest:
2263 case LocateRequest:
2264 case LocateReply:
2265 case CloseConnection:
2266 case MessageError:
2267 case Fragment:
2268    return FALSE;      /* not handled yet */
2269
2270 default:
2271    return FALSE;      /* not handled yet */
2272
2273 }   /* switch */
2274
2275 """
2276
2277
2278     template_main_dissector_end = """\
2279
2280     return FALSE;
2281
2282 }  /* End of main dissector  */
2283
2284 """
2285
2286
2287     
2288
2289
2290
2291
2292 #-------------------------------------------------------------#
2293 #             Exception handling templates                    #
2294 #-------------------------------------------------------------#
2295
2296
2297
2298
2299
2300
2301
2302     template_exception_helpers_start = """\
2303 /*  Begin Exception Helper Functions  */
2304
2305 """
2306     
2307     template_exception_helpers_end = """\
2308     
2309 /*  End Exception Helper Functions  */
2310
2311 """
2312
2313
2314
2315 #
2316 # Templates for declaration of string constants for user exceptions.
2317 #
2318     
2319     template_comment_user_exceptions_string_declare_start = """\
2320 /*  Begin Exception (containing members) String  Declare  */
2321
2322 """
2323     
2324     template_user_exceptions_declare = """static const char user_exception_@sname@[] = \"@exname@\" ; """
2325
2326     
2327     template_comment_user_exceptions_string_declare_end = """\
2328     
2329 /*  End Exception (containing members) String Declare  */
2330
2331 """
2332
2333
2334     
2335
2336 #
2337 # template for Main delegator for exception handling
2338 #
2339
2340     template_main_exception_delegator_start = """\
2341
2342 /*
2343  * Main delegator for exception handling
2344  *
2345  */
2346  
2347 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_ ) {
2348     
2349     gboolean be _U_;                        /* big endianess */
2350
2351     
2352 """
2353
2354
2355 #
2356 # template for exception delegation code body
2357 #
2358     template_ex_delegate_code = """\
2359 if (!strcmp(header->exception_id, user_exception_@sname@ )) {
2360    decode_ex_@sname@(tvb, pinfo, tree, offset, header, operation);   /*  @exname@  */
2361    return TRUE;
2362 }
2363
2364 """
2365
2366
2367 #
2368 # End of Main delegator for exception handling
2369 #
2370
2371     template_main_exception_delegator_end = """\
2372
2373
2374     return FALSE;    /* user exception not found */
2375
2376 }
2377     
2378 """
2379     
2380 #
2381 # template for exception helper code
2382 #
2383
2384
2385     template_exception_helper_function_start = """\
2386
2387 /* Exception = @exname@ */
2388
2389 static void decode_ex_@sname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2390
2391     gboolean stream_is_big_endian;          /* big endianess */
2392 """
2393
2394
2395
2396     #
2397     # Template for the helper function
2398     # to get stream endianess from header
2399     #
2400
2401     template_exception_helper_function_get_endianess = """\
2402
2403 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2404
2405 """
2406
2407     
2408     template_exception_helper_function_end = """\
2409 }
2410 """
2411
2412     
2413 #
2414 # template for struct helper code
2415 #
2416
2417
2418     template_struct_helper_function_start = """\
2419
2420 /* Struct = @stname@ */
2421
2422 static void decode_@sname@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2423
2424     gboolean stream_is_big_endian;          /* big endianess */
2425 """
2426
2427
2428
2429
2430     #
2431     # Template for the helper function
2432     # to get stream endianess from header
2433     #
2434
2435     template_struct_helper_function_get_endianess = """\
2436
2437 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2438
2439 """
2440
2441     
2442     template_struct_helper_function_end = """\
2443 }
2444 """
2445
2446 #
2447 # template for union helper code
2448 #
2449
2450
2451     template_union_helper_function_start = """\
2452
2453 /* Union = @unname@ */
2454
2455 static void decode_@sname@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2456
2457     gboolean stream_is_big_endian;          /* big endianess */
2458 """
2459
2460
2461
2462
2463     #
2464     # Template for the helper function
2465     # to get stream endianess from header
2466     #
2467
2468     template_union_helper_function_get_endianess = """\
2469
2470 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2471
2472 """
2473
2474     
2475     template_union_helper_function_end = """\
2476 }
2477 """
2478
2479
2480
2481 #-------------------------------------------------------------#
2482 #             Value string  templates                         #
2483 #-------------------------------------------------------------#
2484
2485     template_value_string_start = """\
2486 static const value_string @valstringname@[] = {
2487 """
2488
2489     template_value_string_entry = """\
2490    { @intval@, \"@description@\" }, """    
2491     
2492     template_value_string_end = """\
2493    { 0,       NULL },
2494 };
2495     
2496 """
2497
2498     
2499
2500 #-------------------------------------------------------------#
2501 #             Enum   handling templates                       #
2502 #-------------------------------------------------------------#
2503
2504     template_comment_enums_start = """\
2505 /*
2506  * IDL Enums Start
2507  */
2508  
2509  """
2510     
2511     template_comment_enums_end = """\
2512 /*
2513  * IDL Enums End
2514  */
2515  
2516  """
2517     
2518     template_comment_enum_comment = """\
2519 /*
2520  * Enum = @ename@
2521  */
2522  
2523  """
2524
2525
2526
2527 #-------------------------------------------------------------#
2528 #             Attribute handling templates                    #
2529 #-------------------------------------------------------------#
2530
2531
2532     template_comment_attributes_start = """\
2533 /*
2534  * IDL Attributes Start
2535  */
2536  
2537  """
2538
2539     #
2540     # get/set accessor method names are language mapping dependant.
2541     #
2542
2543     template_attributes_declare_Java_get = """static const char get_@sname@_at[] = \"_get_@atname@\" ;"""
2544     template_attributes_declare_Java_set = """static const char set_@sname@_at[] = \"_set_@atname@\" ;"""
2545  
2546
2547     template_comment_attributes_end = """
2548 /*
2549  * IDL Attributes End
2550  */
2551  
2552 """
2553
2554
2555     #
2556     # template for Attribute delegation code
2557     #
2558     # Note: _get_xxx() should only be called for Reply with NO_EXCEPTION
2559     # Note: _set_xxx() should only be called for Request
2560     #
2561     #
2562
2563     template_at_delegate_code_get = """\
2564 if (!strcmp(operation, get_@sname@_at ) && (header->message_type == Reply) && (header->rep_status == NO_EXCEPTION) ) {
2565    decode_get_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2566    return TRUE;
2567 }
2568 """
2569     
2570     template_at_delegate_code_set = """\
2571 if (!strcmp(operation, set_@sname@_at ) && (header->message_type == Request) ) {
2572    decode_set_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2573    return TRUE;
2574 }
2575 """
2576         
2577
2578
2579     template_attribute_helpers_start = """\
2580 /*  Begin Attribute Helper Functions  */
2581
2582 """
2583     
2584     template_attribute_helpers_end = """\
2585     
2586 /*  End Attribute Helper Functions  */
2587
2588 """
2589
2590 #
2591 # template for attribute helper code
2592 #
2593
2594
2595     template_attribute_helper_function_start = """\
2596
2597 /* Attribute = @atname@ */
2598
2599 static void decode_@sname@_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2600
2601     gboolean stream_is_big_endian;          /* big endianess */
2602 """
2603
2604
2605
2606     #
2607     # Template for the helper function
2608     # to get stream endianess from header
2609     #
2610
2611     template_attribute_helper_function_get_endianess = """\
2612
2613 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2614
2615 """
2616
2617     
2618     template_attribute_helper_function_end = """\
2619 }
2620 """
2621
2622     
2623 #-------------------------------------------------------------#
2624 #                     Debugging  templates                    #
2625 #-------------------------------------------------------------#
2626
2627
2628     #
2629     # Template for outputting TODO "C" comments
2630     # so user know I need ti improve something.
2631     #
2632
2633     template_debug_TODO = """\
2634
2635 /* TODO - @message@ */
2636 """
2637
2638     #
2639     # Template for outputting WARNING "C" comments
2640     # so user know if I have found a problem.
2641     #
2642
2643     template_debug_WARNING = """\
2644
2645 /* WARNING - @message@ */
2646 """
2647
2648
2649     
2650 #-------------------------------------------------------------#
2651 #                     IDL Union  templates                    #
2652 #-------------------------------------------------------------#
2653
2654
2655
2656     template_comment_union_code_start = """\
2657 /*
2658  * IDL Union Start - @uname@
2659  */
2660  
2661  """
2662
2663
2664     template_comment_union_code_end = """
2665 /*
2666  * IDL union End - @uname@
2667  */
2668  
2669 """
2670
2671     template_comment_union_code_discriminant = """\
2672 /*
2673  * IDL Union - Discriminant - @uname@
2674  */
2675  
2676  """
2677
2678     #
2679     # Cast Unions types to something appropriate
2680     # Enum value cast to guint32, all others cast to gint32 
2681     # as omniidl accessor returns integer or Enum.
2682     #
2683
2684     template_union_code_save_discriminant_enum = """\
2685 disc_s_@discname@ = (gint32) u_octet4;     /* save Enum Value  discriminant and cast to gint32 */
2686 """
2687     
2688     template_union_code_save_discriminant_long = """\
2689 disc_s_@discname@ = (gint32) s_octet4;     /* save gint32 discriminant and cast to gint32 */
2690 """
2691
2692     template_union_code_save_discriminant_ulong = """\
2693 disc_s_@discname@ = (gint32) u_octet4;     /* save guint32 discriminant and cast to gint32 */
2694 """    
2695
2696     template_union_code_save_discriminant_short = """\
2697 disc_s_@discname@ = (gint32) s_octet2;     /* save gint16 discriminant and cast to gint32 */
2698 """    
2699
2700     template_union_code_save_discriminant_ushort = """\
2701 disc_s_@discname@ = (gint32) u_octet2;     /* save guint16 discriminant and cast to gint32 */
2702 """    
2703     
2704     template_union_code_save_discriminant_char = """\
2705 disc_s_@discname@ = (gint32) u_octet1;     /* save guint1 discriminant and cast to gint32 */
2706 """
2707     
2708     template_union_code_save_discriminant_boolean = """\
2709 disc_s_@discname@ = (gint32) u_octet1;     /* save guint1 discriminant and cast to gint32 */
2710 """
2711
2712
2713     template_comment_union_code_label_compare_start = """\
2714 if (disc_s_@discname@ == @labelval@) {
2715
2716  """
2717     template_comment_union_code_label_compare_end = """\
2718     return;     /* End Compare for this discriminant type */
2719 }
2720
2721  """
2722
2723
2724     template_comment_union_code_label_default_start = """
2725 /* Default Union Case Start */
2726  
2727 """
2728
2729     template_comment_union_code_label_default_end = """\
2730 /* Default Union Case End */
2731  
2732  """
2733
2734     #
2735     # Templates for function prototypes.
2736     # This is used in genDeclares() for declaring function prototypes
2737     # for structs and union helper functions.
2738     #
2739
2740     template_prototype_struct_start = """
2741 /* Struct prototype declaration Start */
2742  
2743 """
2744     
2745     template_prototype_struct_end = """
2746 /* Struct prototype declaration End */
2747  
2748 """
2749     
2750     template_prototype_struct_body = """
2751         
2752 /* Struct = @stname@ */
2753
2754 static void decode_@name@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
2755
2756 """
2757
2758     template_decode_struct = """
2759         
2760 decode_@name@_st(tvb, pinfo, tree, offset, header, operation);
2761
2762 """
2763
2764
2765     
2766     
2767     template_prototype_union_start = """
2768 /* Union prototype declaration Start */
2769  
2770 """
2771     
2772     template_prototype_union_end = """
2773 /* Union prototype declaration End */
2774  
2775 """
2776     
2777     template_prototype_union_body = """
2778         
2779 /* Union = @unname@ */
2780
2781 static void decode_@name@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
2782
2783 """
2784
2785     template_decode_union = """
2786         
2787 decode_@name@_un(tvb, pinfo, tree, offset, header, operation);
2788
2789 """
2790     
2791