minor change in the uninstaller "Publisher" string
[obnox/wireshark/wip.git] / ethereal_gen.py
1 # -*- python -*-
2 #
3 # $Id: ethereal_gen.py,v 1.28 2002/08/02 23:35:46 jmayer Exp $
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_short):
1196             self.st.out(self.template_union_code_save_discriminant_short, discname=un.identifier() )
1197             self.addvar(self.c_s_disc + un.identifier() + ";")            
1198
1199         elif (st.kind() == idltype.tk_boolean):
1200             self.st.out(self.template_union_code_save_discriminant_boolean, discname=un.identifier()  )
1201             self.addvar(self.c_s_disc + un.identifier() + ";")            
1202             
1203         elif (st.kind() == idltype.tk_char):
1204             self.st.out(self.template_union_code_save_discriminant_char, discname=un.identifier() )
1205             self.addvar(self.c_s_disc + un.identifier() + ";")            
1206
1207         else:
1208             print "XXX Unknown st.kind() = ", st.kind()
1209             
1210         #
1211         # Loop over all cases in this union
1212         #
1213         
1214         for uc in un.cases():           # for all UnionCase objects in this union
1215             for cl in uc.labels():      # for all Caselabel objects in this UnionCase
1216
1217                 # get integer value, even if discriminant is
1218                 # an Enumerator node
1219                 
1220                 if isinstance(cl.value(),idlast.Enumerator):
1221                     if self.DEBUG:
1222                         print "XXX clv.identifier()", cl.value().identifier()
1223                         print "XXX clv.repoId()", cl.value().repoId()
1224                         print "XXX clv.scopedName()", cl.value().scopedName()
1225                                             
1226                     # find index of enumerator in enum declaration
1227                     # eg: RED is index 0 in enum Colors { RED, BLUE, GREEN }
1228                     
1229                     clv = self.valFromEnum(std,cl.value())
1230                     
1231                 else:
1232                     clv = cl.value()
1233                     
1234                 #print "XXX clv = ",clv
1235
1236                 #    
1237                 # if char, dont convert to int, but put inside single quotes so that it is understood by C.
1238                 # eg: if (disc == 'b')..
1239                 #
1240                 # TODO : handle \xxx chars generically from a function or table lookup rather than
1241                 #        a whole bunch of "if" statements. -- FS
1242                 
1243                                           
1244                 if (st.kind() == idltype.tk_char):
1245                     if (clv == '\n'):          # newline
1246                         string_clv = "'\\n'" 
1247                     elif (clv == '\t'):        # tab
1248                         string_clv = "'\\t'"
1249                     else:
1250                         string_clv = "'" + clv + "'"
1251                 else:                    
1252                     string_clv = '%i ' % clv
1253
1254                 #
1255                 # If default case, then skp comparison with discriminator
1256                 #
1257                 
1258                 if not cl.default():
1259                     self.st.out(self.template_comment_union_code_label_compare_start, discname=un.identifier(),labelval=string_clv )
1260                     self.st.inc_indent()       
1261                 else:
1262                     self.st.out(self.template_comment_union_code_label_default_start  )
1263                     
1264
1265                 self.getCDR3(uc.caseType(),uc.declarator().identifier())
1266
1267                 if not cl.default():
1268                     self.st.dec_indent()       
1269                     self.st.out(self.template_comment_union_code_label_compare_end )
1270                 else:
1271                     self.st.out(self.template_comment_union_code_label_default_end  )
1272
1273         self.st.dec_indent()
1274         self.st.out(self.template_union_helper_function_end)
1275
1276
1277
1278     #
1279     # Currently, get_CDR_alias is geared to finding typdef 
1280     #
1281
1282     def get_CDR_alias(self,type,pn):
1283         if self.DEBUG:
1284             print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1285             print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1286             print "XXX get_CDR_alias, type.decl().alias() = " ,type.decl().alias()
1287
1288         decl = type.decl()              # get declarator object
1289         
1290         if (decl.sizes()):        # a typedef array 
1291             indices = self.get_indices_from_sizes(decl.sizes())
1292             string_indices = '%i ' % indices # convert int to string
1293             self.st.out(self.template_get_CDR_array_comment, aname=pn, asize=string_indices)
1294             
1295             self.st.out(self.template_get_CDR_array_start, aname=pn, aval=string_indices)                        
1296             self.addvar(self.c_i + pn + ";")            
1297             self.st.inc_indent()       
1298             self.getCDR3(type.decl().alias().aliasType(),  pn )
1299             
1300             self.st.dec_indent()
1301             self.st.out(self.template_get_CDR_array_end)
1302             
1303             
1304         else:                           # a simple typdef
1305             if self.DEBUG:
1306                 print "XXX get_CDR_alias, type = " ,type , " pn = " , pn
1307                 print "XXX get_CDR_alias, type.decl() = " ,type.decl()
1308
1309             self.getCDR3(type, decl.identifier() )
1310             
1311             
1312             
1313         
1314         
1315
1316     #
1317     # Handle structs, including recursive
1318     #
1319     
1320     def get_CDR_struct(self,type,pn):       
1321
1322         #  If I am a typedef struct {..}; node then find the struct node
1323         
1324         if isinstance(type.decl(), idlast.Declarator):
1325             ntype = type.decl().alias().aliasType().decl()           
1326         else:
1327             ntype = type.decl()         # I am a struct node
1328
1329         sname = self.namespace(ntype, "_")
1330         self.st.out(self.template_structure_start, name=sname )
1331
1332         # Output a call to the struct helper function so I can handle recursive structs also.
1333         
1334         self.st.out(self.template_decode_struct,name=sname)    
1335
1336         self.st.out(self.template_structure_end, name=sname )
1337
1338     #
1339     # genStructhelper() 
1340     #
1341     # Generate private helper functions to decode a struct
1342     #
1343     # in: stnode ( a struct node)
1344     #
1345     
1346     def genStructHelper(self,st):
1347         if self.DEBUG:
1348             print "XXX genStructHelper"
1349             
1350         sname = self.namespace(st, "_")
1351         self.curr_sname = sname         # update current opnode/exnode/stnode scoped name
1352         if not self.fn_hash_built:
1353             self.fn_hash[sname] = []        # init empty list as val for this sname key
1354                                             # but only if the fn_hash is not already built
1355
1356         self.st.out(self.template_struct_helper_function_start, sname=sname, stname=st.repoId())
1357         self.st.inc_indent()
1358
1359         self.st.out(self.template_helper_function_vars_start)
1360         self.dumpCvars(sname)
1361         self.st.out(self.template_helper_function_vars_end )
1362         
1363         self.st.out(self.template_struct_helper_function_get_endianess)
1364
1365         for m in st.members():
1366             for decl in m.declarators():
1367                 if decl.sizes():        # an array
1368                     indices = self.get_indices_from_sizes(decl.sizes())
1369                     string_indices = '%i ' % indices # convert int to string
1370                     self.st.out(self.template_get_CDR_array_comment, aname=decl.identifier(), asize=string_indices)     
1371                     self.st.out(self.template_get_CDR_array_start, aname=decl.identifier(), aval=string_indices)
1372                     self.addvar(self.c_i + decl.identifier() + ";")
1373                     
1374                     self.st.inc_indent()       
1375                     self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )                    
1376                     self.st.dec_indent()
1377                     self.st.out(self.template_get_CDR_array_end)
1378                     
1379                     
1380                 else:                            
1381                     self.getCDR3(m.memberType(), st.identifier() + "_" + decl.identifier() )
1382                             
1383         self.st.dec_indent()
1384         self.st.out(self.template_struct_helper_function_end)
1385
1386
1387
1388
1389
1390     #
1391     # Generate code to access a sequence of a type
1392     #
1393
1394     
1395     def get_CDR_sequence(self,type, pn):
1396         self.st.out(self.template_get_CDR_sequence_length, seqname=pn )
1397         self.st.out(self.template_get_CDR_sequence_loop_start, seqname=pn )
1398         self.addvar(self.c_i_lim + pn + ";" )
1399         self.addvar(self.c_i + pn + ";")
1400
1401         self.st.inc_indent()       
1402         self.getCDR3(type.unalias().seqType(), pn ) # and start all over with the type
1403         self.st.dec_indent()     
1404         
1405         self.st.out(self.template_get_CDR_sequence_loop_end)
1406
1407
1408         
1409     #
1410     # Generate code to access arrays, 
1411     #
1412     # This is handled elsewhere. Arrays are either typedefs or in
1413     # structs
1414     #
1415     # TODO - Remove this
1416     #
1417     
1418     def get_CDR_array(self,type, decl):
1419         if self.DEBUG:
1420             print "XXX get_CDR_array called "
1421             print "XXX array size = " ,decl.sizes()
1422         
1423
1424    #
1425    # namespace()
1426    #
1427    # in - op node
1428    #
1429    # out - scoped operation name, using sep character instead of "::"
1430    #
1431    # eg: Penguin::Echo::echoWString => Penguin_Echo_echoWString if sep = "_"
1432    #
1433    #
1434    
1435     def namespace(self,node,sep):    
1436         sname = string.replace(idlutil.ccolonName(node.scopedName()), '::', sep)
1437         #print "XXX namespace: sname = " + sname
1438         return sname
1439
1440
1441     #
1442     # generate code for plugin initialisation
1443     #
1444
1445     def gen_plugin_init(self):
1446         self.st.out(self.template_plugin_init, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1447
1448     #
1449     # generate  register_giop_user_module code, and register only
1450     # unique interfaces that contain operations. Also output
1451     # a heuristic register in case we want to use that.
1452     #
1453     # TODO - make this a command line option
1454     #
1455     # -e explicit
1456     # -h heuristic
1457     #
1458     
1459     
1460
1461     def gen_proto_reg_handoff(self, oplist):
1462
1463         self.st.out(self.template_proto_reg_handoff_start, dissector_name=self.dissname)
1464         self.st.inc_indent()
1465
1466         for iname in self.get_intlist(oplist):
1467             self.st.out(self.template_proto_reg_handoff_body, dissector_name=self.dissname, protocol_name=self.protoname, interface=iname )
1468             
1469         self.st.out(self.template_proto_reg_handoff_heuristic, dissector_name=self.dissname,  protocol_name=self.protoname)
1470         self.st.dec_indent()
1471         
1472         self.st.out(self.template_proto_reg_handoff_end)
1473
1474
1475
1476     #
1477     # generate  proto_register_<protoname> code,
1478     #
1479     
1480
1481     def gen_proto_register(self):
1482         self.st.out(self.template_proto_register, description=self.description, protocol_name=self.protoname, dissector_name=self.dissname)
1483     
1484
1485     #
1486     # in - oplist[]
1487     #
1488     # out - a list of unique interface names. This will be used in
1489     # register_giop_user_module(dissect_giop_auto, "TEST IDL", "Penguin/Echo" );   so the operation
1490     # name must be removed from the scope. And we also only want unique interfaces.
1491     #
1492
1493     def get_intlist(self,oplist):
1494         int_hash = {}                   # holds a hash of unique interfaces
1495         for op in oplist:
1496             sc = op.scopedName()        # eg: penguin,tux,bite
1497             sc1 = sc[:-1]               # drop last entry
1498             sn = idlutil.slashName(sc1)         # penguin/tux
1499             if not int_hash.has_key(sn):
1500                 int_hash[sn] = 0;       # dummy val, but at least key is unique
1501                 
1502         return int_hash.keys()
1503
1504
1505
1506     #
1507     # in - oplist[]
1508     #
1509     # out - a list of exception nodes (unique). This will be used in
1510     # to generate dissect_exception_XXX functions.
1511     #
1512     
1513
1514
1515     def get_exceptionList(self,oplist):
1516         ex_hash = {}                   # holds a hash of unique exceptions.
1517         for op in oplist:
1518             for ex in op.raises():
1519                 if not ex_hash.has_key(ex):
1520                     ex_hash[ex] = 0; # dummy val, but at least key is unique
1521                     if self.DEBUG:
1522                         print "XXX Exception = " + ex.identifier()
1523
1524         return ex_hash.keys()
1525
1526
1527
1528     #
1529     # Simple function to take a list of array sizes and find the
1530     # total number of elements
1531     #
1532     #
1533     # eg: temp[4][3] = 12 elements
1534     #
1535
1536     def get_indices_from_sizes(self,sizelist):
1537         val = 1;
1538         for i in sizelist:
1539             val = val * i
1540             
1541         return val
1542
1543     #
1544     # Determine how many octets contain requested number
1545     # of digits for an "fixed" IDL type  "on the wire"
1546     #
1547
1548     def dig_to_len(self,dignum):
1549         return (dignum/2) + 1
1550
1551             
1552
1553     #
1554     # Output some TODO comment
1555     #
1556     
1557
1558     def genTODO(self,message):
1559         self.st.out(self.template_debug_TODO, message=message)
1560
1561     #
1562     # Output some WARNING comment
1563     #
1564     
1565
1566     def genWARNING(self,message):
1567         self.st.out(self.template_debug_WARNING, message=message)
1568             
1569     #
1570     # Templates for C code
1571     #
1572
1573
1574
1575     template_comment_operations_start = """\
1576 /*
1577  * IDL Operations Start
1578  */
1579  
1580  """
1581
1582
1583     template_operations_declare = """static const char @sname@_op[] = \"@opname@\" ;"""
1584     
1585
1586     template_comment_operations_end = """
1587 /*
1588  * IDL Operations End
1589  */
1590  
1591 """
1592
1593     template_helper_function_comment = """\
1594
1595 /*
1596  * @repoid@
1597  */
1598  
1599 """
1600     
1601     template_helper_function_vars_start = """
1602 /* Operation specific Variable declarations Begin */
1603 """
1604     
1605     template_helper_function_vars_end = """
1606 /* Operation specific Variable declarations End */
1607 """
1608
1609
1610
1611     template_helper_function_start = """\
1612 static void decode_@sname@(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_, int *offset _U_, MessageHeader *header, gchar *operation _U_) {
1613
1614     gboolean stream_is_big_endian;          /* big endianess */
1615 """
1616     
1617     template_helper_function_end = """\
1618 }
1619 """
1620     
1621     #
1622     # proto_reg_handoff() templates
1623     #
1624
1625
1626     template_proto_reg_handoff_start = """
1627 /* register me as handler for these interfaces */
1628
1629 void proto_register_handoff_giop_@dissector_name@(void) {
1630
1631 """
1632
1633     template_proto_reg_handoff_body = """
1634 #if 0
1635
1636 /* Register for Explicit Dissection */
1637
1638 register_giop_user_module(dissect_@dissector_name@, \"@protocol_name@\", \"@interface@\", proto_@dissector_name@ );     /* explicit dissector */
1639
1640 #endif
1641
1642 """
1643
1644     template_proto_reg_handoff_heuristic = """
1645
1646 /* Register for Heuristic Dissection */
1647
1648 register_giop_user(dissect_@dissector_name@, \"@protocol_name@\" ,proto_@dissector_name@);     /* heuristic dissector */ 
1649
1650 """
1651
1652     template_proto_reg_handoff_end = """
1653 }
1654 """
1655
1656
1657     #
1658     # Initialize the protocol
1659     #
1660
1661     template_protocol = """
1662 /* Initialise the protocol and subtree pointers */
1663
1664 static int proto_@dissector_name@ = -1;
1665
1666 static gint ett_@dissector_name@ = -1;
1667
1668 """
1669
1670
1671
1672     #
1673     # Initialize the boundary Alignment
1674     #
1675
1676     template_init_boundary = """
1677 /* Initialise the initial Alignment */
1678
1679 static guint32  boundary = GIOP_HEADER_SIZE;  /* initial value */
1680
1681 """
1682
1683
1684
1685
1686
1687     #
1688     # Initialize the Registered fields
1689     #
1690
1691     template_registered_fields = """
1692
1693 /* Initialise the Registered fields */
1694
1695 /* TODO - Use registered fields */
1696
1697 """
1698
1699     #
1700     # plugin_init and plugin_reg_handoff templates
1701     #
1702
1703     template_plugin_init = """
1704
1705 #ifndef __ETHEREAL_STATIC__
1706
1707 G_MODULE_EXPORT void
1708 plugin_reg_handoff(void){
1709    proto_register_handoff_giop_@dissector_name@();
1710 }
1711
1712 G_MODULE_EXPORT void
1713 plugin_init(plugin_address_table_t *pat
1714 #ifndef PLUGINS_NEED_ADDRESS_TABLE
1715 _U_
1716 #endif
1717 ){
1718    /* initialise the table of pointers needed in Win32 DLLs */
1719    plugin_address_table_init(pat);
1720    if (proto_@dissector_name@ == -1) {
1721      proto_register_giop_@dissector_name@();
1722    }
1723 }
1724
1725 #endif
1726
1727 """
1728
1729     #
1730     # proto_register_<dissector name>(void) templates
1731     #
1732
1733
1734     template_proto_register = """
1735
1736 /* Register the protocol with Ethereal */
1737
1738 void proto_register_giop_@dissector_name@(void) {
1739
1740    /* setup list of header fields */
1741
1742 #if 0
1743    static hf_register_info hf[] = {
1744
1745       /* no fields yet */
1746       
1747    };
1748 #endif
1749
1750    /* setup protocol subtree array */
1751
1752    static gint *ett[] = {
1753       &ett_@dissector_name@,
1754    };
1755
1756    /* Register the protocol name and description */
1757    
1758    proto_@dissector_name@ = proto_register_protocol(\"@description@\" , \"@protocol_name@\", \"giop-@dissector_name@\" );
1759
1760 #if 0
1761    proto_register_field_array(proto_@dissector_name@, hf, array_length(hf));
1762 #endif
1763    proto_register_subtree_array(ett,array_length(ett));
1764    
1765 }
1766
1767
1768 """
1769
1770     #
1771     # template for delegation code
1772     #
1773
1774     template_op_delegate_code = """\
1775 if (!strcmp(operation, @sname@_op )) {
1776    decode_@sname@(tvb, pinfo, tree, offset, header, operation);
1777    return TRUE;
1778 }
1779 """
1780
1781     #
1782     # Templates for the helper functions
1783     #
1784     #
1785     #
1786
1787
1788     template_helper_switch_msgtype_start = """\
1789
1790 stream_is_big_endian = is_big_endian(header);
1791
1792 switch(header->message_type) {
1793 """
1794
1795     template_helper_switch_msgtype_default_start = """\
1796 default:
1797
1798     /* Unknown GIOP Exception */
1799
1800     g_warning("Unknown GIOP Message");
1801     
1802 """
1803     template_helper_switch_msgtype_default_end = """\
1804 break;
1805 """
1806
1807     
1808     
1809     template_helper_switch_msgtype_end = """\
1810     
1811 } /* switch(header->message_type) */ 
1812 """
1813
1814     template_helper_switch_msgtype_request_start = """\
1815 case Request:
1816 """
1817     
1818     template_helper_switch_msgtype_request_end = """\
1819 break;
1820 """
1821
1822     template_helper_switch_msgtype_reply_start = """\
1823 case Reply:
1824 """
1825     
1826     template_helper_switch_msgtype_reply_no_exception_start = """\
1827 case NO_EXCEPTION:
1828 """
1829     
1830     template_helper_switch_msgtype_reply_no_exception_end = """\
1831 break;
1832 """
1833
1834     
1835     template_helper_switch_msgtype_reply_user_exception_start = """\
1836 case USER_EXCEPTION:
1837 """
1838     
1839     template_helper_switch_msgtype_reply_user_exception_end = """\
1840 break;
1841 """
1842
1843     template_helper_switch_msgtype_reply_default_start = """\
1844 default:
1845
1846     /* Unknown Exception */
1847
1848     g_warning("Unknown Exception ");
1849
1850     
1851 """
1852     
1853     template_helper_switch_msgtype_reply_default_end = """\
1854     break;
1855 """
1856
1857
1858     template_helper_switch_msgtype_reply_end = """\
1859 break;
1860 """
1861
1862     template_helper_switch_msgtype_default_start = """\
1863 default:
1864
1865     /* Unknown GIOP Message */
1866
1867     g_warning("Unknown GIOP Message");
1868     
1869 """
1870
1871     template_helper_switch_msgtype_default_end = """\
1872     break;
1873 """
1874
1875
1876     
1877     template_helper_switch_rep_status_start = """\
1878 switch(header->rep_status) {
1879 """
1880
1881     template_helper_switch_rep_status_default_start = """\
1882 default:
1883
1884     /* Unknown Reply Status */
1885
1886     g_warning("Unknown Reply Status");
1887     
1888 """
1889     
1890     template_helper_switch_rep_status_default_end = """\
1891     break;  
1892 """
1893     
1894     template_helper_switch_rep_status_end = """\
1895
1896 }   /* switch(header->message_type) */
1897
1898 break;   
1899 """
1900
1901             
1902
1903
1904
1905     #
1906     # Templates for get_CDR_xxx accessors
1907     #
1908     
1909     template_get_CDR_ulong = """\
1910 u_octet4 = get_CDR_ulong(tvb,offset,stream_is_big_endian, boundary);
1911 if (tree) {
1912    proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %u",u_octet4);
1913 }
1914 """
1915
1916     template_get_CDR_short = """\
1917 s_octet2 = get_CDR_short(tvb,offset,stream_is_big_endian, boundary);
1918 if (tree) {
1919    proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %i",s_octet2);
1920 }
1921 """
1922     
1923     template_get_CDR_void = """\
1924
1925 /* Function returns void */
1926
1927 """
1928
1929     template_get_CDR_long = """\
1930 s_octet4 = get_CDR_long(tvb,offset,stream_is_big_endian, boundary);
1931 if (tree) {
1932    proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %i",s_octet4);
1933 }
1934 """
1935
1936     template_get_CDR_ushort = """\
1937 u_octet2 = get_CDR_ushort(tvb,offset,stream_is_big_endian, boundary);
1938 if (tree) {
1939    proto_tree_add_text(tree,tvb,*offset-2,2,"@varname@ = %u",u_octet2);
1940 }
1941 """
1942     template_get_CDR_float = """\
1943 my_float = get_CDR_float(tvb,offset,stream_is_big_endian, boundary);
1944 if (tree) {
1945    proto_tree_add_text(tree,tvb,*offset-4,4,"@varname@ = %.6e",my_float);
1946 }
1947 """
1948     
1949     template_get_CDR_double = """\
1950 my_double = get_CDR_double(tvb,offset,stream_is_big_endian, boundary);
1951 if (tree) {
1952    proto_tree_add_text(tree,tvb,*offset-8,8,"@varname@ = %.15e",my_double);
1953 }
1954 """
1955     
1956     template_get_CDR_boolean = """\
1957 u_octet1 = get_CDR_boolean(tvb,offset);
1958 if (tree) {
1959    proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1960 }
1961 """
1962     
1963     template_get_CDR_char = """\
1964 u_octet1 = get_CDR_char(tvb,offset);
1965 if (tree) {
1966    proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1967 }
1968 """
1969     
1970     template_get_CDR_octet = """\
1971 u_octet1 = get_CDR_octet(tvb,offset);
1972 if (tree) {
1973    proto_tree_add_text(tree,tvb,*offset-1,1,"@varname@ = %u",u_octet1);
1974 }
1975 """
1976
1977
1978
1979     template_get_CDR_any = """\
1980 get_CDR_any(tvb,tree,offset,stream_is_big_endian, boundary, header);
1981
1982 """
1983
1984     template_get_CDR_fixed = """\
1985 get_CDR_fixed(tvb, &seq, offset, @digits@, @scale@);
1986 if (tree) {
1987    proto_tree_add_text(tree,tvb,*offset-@length@, @length@, "@varname@ < @digits@, @scale@> = %s",seq);   
1988 }
1989
1990 g_free(seq);          /*  free buffer  */
1991 seq = NULL;
1992
1993 """
1994
1995             
1996     template_get_CDR_enum_symbolic = """\
1997     
1998 u_octet4 = get_CDR_enum(tvb,offset,stream_is_big_endian, boundary);
1999 if (tree) {
2000    proto_tree_add_text(tree,tvb,*offset-4,4,"Enum value = %u (%s)",u_octet4,val_to_str(u_octet4,@valstringarray@,"Unknown Enum Value"));
2001 }
2002 """
2003
2004
2005
2006     template_get_CDR_string = """\
2007 u_octet4 = get_CDR_string(tvb, &seq, offset, stream_is_big_endian, boundary);
2008 if (tree) {
2009    proto_tree_add_text(tree,tvb,*offset-4-u_octet4,4,"length = %u",u_octet4);
2010    if (u_octet4 > 0)
2011       proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ = %s",seq);
2012    
2013 }
2014
2015 g_free(seq);          /*  free buffer  */
2016 seq = NULL;
2017 """
2018
2019    
2020     template_get_CDR_wstring = """\
2021 u_octet4 = get_CDR_wstring(tvb, &seq, offset, stream_is_big_endian, boundary, header);
2022 if (tree) {
2023    proto_tree_add_text(tree,tvb,*offset-4-u_octet4,4,"length = %u",u_octet4);
2024    if (u_octet4 > 0)
2025       proto_tree_add_text(tree,tvb,*offset-u_octet4,u_octet4,"@varname@ = %s",seq);
2026    
2027 }
2028
2029 g_free(seq);          /*  free buffer  */
2030 seq = NULL;
2031 """
2032
2033    
2034     template_get_CDR_wchar = """\
2035 s_octet1 = get_CDR_wchar(tvb, &seq, offset, header);
2036 if (tree) {
2037     if (s_octet1 > 0)
2038         proto_tree_add_text(tree,tvb,*offset-1-s_octet1,1,"length = %u",s_octet1);
2039
2040     if (s_octet1 < 0)
2041         s_octet1 = -s_octet1;
2042
2043     if (s_octet1 > 0)
2044         proto_tree_add_text(tree,tvb,*offset-s_octet1,s_octet1,"@varname@ = %s",seq);
2045            
2046 }
2047         
2048 g_free(seq);          /*  free buffer  */
2049 seq = NULL;
2050 """
2051
2052
2053
2054
2055     template_get_CDR_TypeCode = """\
2056 u_octet4 = get_CDR_typeCode(tvb, tree, offset, stream_is_big_endian, boundary, header);
2057
2058 """
2059     
2060     template_get_CDR_object = """\
2061 get_CDR_object(tvb, pinfo, tree, offset, stream_is_big_endian, boundary);
2062
2063 """
2064     
2065
2066     template_get_CDR_sequence_length = """\
2067 u_octet4_loop_@seqname@ = get_CDR_ulong(tvb, offset, stream_is_big_endian, boundary);
2068 if (tree) {
2069    proto_tree_add_text(tree,tvb,*offset-4, 4 ,"Seq length of @seqname@ = %u",u_octet4_loop_@seqname@);   
2070 }
2071 """
2072
2073     template_get_CDR_sequence_loop_start = """\
2074 for (i_@seqname@=0; i_@seqname@ < u_octet4_loop_@seqname@; i_@seqname@++) {
2075 """
2076     template_get_CDR_sequence_loop_end = """\
2077 }
2078 """
2079     
2080
2081
2082     template_get_CDR_array_start = """\
2083 for (i_@aname@=0; i_@aname@ < @aval@; i_@aname@++) {
2084 """
2085     template_get_CDR_array_end = """\
2086 }
2087 """
2088
2089     template_get_CDR_array_comment = """\
2090 /* Array: @aname@[ @asize@]  */
2091 """
2092             
2093
2094
2095
2096     template_structure_start = """\
2097 /*  Begin struct \"@name@\"  */
2098 """
2099
2100
2101     template_structure_end = """\
2102 /*  End struct \"@name@\"  */
2103 """
2104
2105     template_union_start = """\
2106 /*  Begin union \"@name@\"  */
2107 """
2108
2109
2110     template_union_end = """\
2111 /*  End union \"@name@\"  */
2112 """
2113
2114
2115
2116
2117 #
2118 # Program Header Template
2119 #
2120
2121     template_Header = """\
2122 /* packet-@dissector_name@.c
2123  * Routines for IDL dissection
2124  *
2125  * Autogenerated from idl2eth
2126  * Copyright 2001 Frank Singleton <frank.singleton@@ericsson.com>
2127  */
2128
2129 """
2130
2131     template_ethereal_copyright = """\
2132 /*
2133  * Ethereal - Network traffic analyzer
2134  * By Gerald Combs
2135  * Copyright 1999 Gerald Combs
2136  */
2137  
2138 """
2139     
2140
2141
2142 #
2143 # GPL Template
2144 #
2145
2146
2147     template_GPL = """\
2148 /*
2149  * This program is free software; you can redistribute it and/or
2150  * modify it under the terms of the GNU General Public License
2151  * as published by the Free Software Foundation; either version 2
2152  * of the License, or (at your option) any later version.
2153  * 
2154  * This program is distributed in the hope that it will be useful,
2155  * but WITHOUT ANY WARRANTY; without even the implied warranty of
2156  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2157  * GNU General Public License for more details.
2158  * 
2159  * You should have received a copy of the GNU General Public License
2160  * along with this program; if not, write to the Free Software
2161  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
2162  *
2163  */
2164 """
2165
2166 #
2167 # Includes template
2168 #
2169
2170     template_Includes = """\
2171
2172 #ifdef HAVE_CONFIG_H
2173 # include "config.h"
2174 #endif
2175
2176 #include "plugins/plugin_api.h"
2177
2178 #include <stdio.h>
2179 #include <stdlib.h>
2180 #include <gmodule.h>
2181
2182 #ifdef NEED_SNPRINTF_H
2183 # include "snprintf.h"
2184 #endif
2185
2186 #include <string.h>
2187 #include <glib.h>
2188 #include <epan/packet.h>
2189 #include <epan/proto.h>
2190 #include "packet-giop.h"
2191
2192 #include "plugins/plugin_api_defs.h"
2193
2194 #ifndef __ETHEREAL_STATIC__
2195 G_MODULE_EXPORT const gchar version[] = "0.0.1";
2196 #endif
2197
2198 """
2199
2200     
2201 #
2202 # Main dissector entry templates
2203 #
2204
2205     template_main_dissector_start = """\
2206 static gboolean dissect_@dissname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *ptree, int *offset, MessageHeader *header, gchar *operation, gchar *idlname _U_) {
2207
2208     proto_item *ti = NULL;
2209     proto_tree *tree = NULL;            /* init later, inside if(tree) */
2210     
2211     gboolean be;                        /* big endianess */
2212
2213     if (check_col(pinfo->cinfo, COL_PROTOCOL))
2214        col_set_str(pinfo->cinfo, COL_PROTOCOL, \"@disprot@\");
2215
2216 /* 
2217  * Do not clear COL_INFO, as nothing is being written there by 
2218  * this dissector yet. So leave it as is from the GIOP dissector.
2219  * TODO: add something useful to COL_INFO 
2220  *  if (check_col(pinfo->cinfo, COL_INFO))
2221  *     col_clear(pinfo->cinfo, COL_INFO);
2222  */
2223
2224     if (ptree) {
2225        ti = proto_tree_add_item(ptree, proto_@dissname@, tvb, *offset, tvb_length(tvb) - *offset, FALSE);
2226        tree = proto_item_add_subtree(ti, ett_@dissname@);
2227     }  
2228
2229
2230     be = is_big_endian(header);         /* get endianess - TODO use passed in stream_is_big_endian instead ? */
2231
2232     /* If we have a USER Exception, then decode it and return */
2233
2234     if ((header->message_type == Reply) && (header->rep_status == USER_EXCEPTION)) {
2235
2236        return decode_user_exception(tvb, pinfo, tree, offset, header, operation);
2237
2238     }
2239
2240     
2241 """
2242
2243
2244        
2245
2246     template_main_dissector_switch_msgtype_start = """\
2247 switch(header->message_type) {
2248 """
2249     
2250     template_main_dissector_switch_msgtype_start_request_reply = """\
2251 case Request:
2252 case Reply:
2253
2254 """
2255
2256     template_main_dissector_switch_msgtype_end_request_reply = """\
2257
2258 break;
2259 """
2260
2261
2262     template_main_dissector_switch_msgtype_all_other_msgtype = """\
2263 case CancelRequest:
2264 case LocateRequest:
2265 case LocateReply:
2266 case CloseConnection:
2267 case MessageError:
2268 case Fragment:
2269    return FALSE;      /* not handled yet */
2270
2271 default:
2272    return FALSE;      /* not handled yet */
2273
2274 }   /* switch */
2275
2276 """
2277
2278
2279     template_main_dissector_end = """\
2280
2281     return FALSE;
2282
2283 }  /* End of main dissector  */
2284
2285 """
2286
2287
2288     
2289
2290
2291
2292
2293 #-------------------------------------------------------------#
2294 #             Exception handling templates                    #
2295 #-------------------------------------------------------------#
2296
2297
2298
2299
2300
2301
2302
2303     template_exception_helpers_start = """\
2304 /*  Begin Exception Helper Functions  */
2305
2306 """
2307     
2308     template_exception_helpers_end = """\
2309     
2310 /*  End Exception Helper Functions  */
2311
2312 """
2313
2314
2315
2316 #
2317 # Templates for declaration of string constants for user exceptions.
2318 #
2319     
2320     template_comment_user_exceptions_string_declare_start = """\
2321 /*  Begin Exception (containing members) String  Declare  */
2322
2323 """
2324     
2325     template_user_exceptions_declare = """static const char user_exception_@sname@[] = \"@exname@\" ; """
2326
2327     
2328     template_comment_user_exceptions_string_declare_end = """\
2329     
2330 /*  End Exception (containing members) String Declare  */
2331
2332 """
2333
2334
2335     
2336
2337 #
2338 # template for Main delegator for exception handling
2339 #
2340
2341     template_main_exception_delegator_start = """\
2342
2343 /*
2344  * Main delegator for exception handling
2345  *
2346  */
2347  
2348 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_ ) {
2349     
2350     gboolean be _U_;                        /* big endianess */
2351
2352     
2353 """
2354
2355
2356 #
2357 # template for exception delegation code body
2358 #
2359     template_ex_delegate_code = """\
2360 if (!strcmp(header->exception_id, user_exception_@sname@ )) {
2361    decode_ex_@sname@(tvb, pinfo, tree, offset, header, operation);   /*  @exname@  */
2362    return TRUE;
2363 }
2364
2365 """
2366
2367
2368 #
2369 # End of Main delegator for exception handling
2370 #
2371
2372     template_main_exception_delegator_end = """\
2373
2374
2375     return FALSE;    /* user exception not found */
2376
2377 }
2378     
2379 """
2380     
2381 #
2382 # template for exception helper code
2383 #
2384
2385
2386     template_exception_helper_function_start = """\
2387
2388 /* Exception = @exname@ */
2389
2390 static void decode_ex_@sname@(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2391
2392     gboolean stream_is_big_endian;          /* big endianess */
2393 """
2394
2395
2396
2397     #
2398     # Template for the helper function
2399     # to get stream endianess from header
2400     #
2401
2402     template_exception_helper_function_get_endianess = """\
2403
2404 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2405
2406 """
2407
2408     
2409     template_exception_helper_function_end = """\
2410 }
2411 """
2412
2413     
2414 #
2415 # template for struct helper code
2416 #
2417
2418
2419     template_struct_helper_function_start = """\
2420
2421 /* Struct = @stname@ */
2422
2423 static void decode_@sname@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2424
2425     gboolean stream_is_big_endian;          /* big endianess */
2426 """
2427
2428
2429
2430
2431     #
2432     # Template for the helper function
2433     # to get stream endianess from header
2434     #
2435
2436     template_struct_helper_function_get_endianess = """\
2437
2438 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2439
2440 """
2441
2442     
2443     template_struct_helper_function_end = """\
2444 }
2445 """
2446
2447 #
2448 # template for union helper code
2449 #
2450
2451
2452     template_union_helper_function_start = """\
2453
2454 /* Union = @unname@ */
2455
2456 static void decode_@sname@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2457
2458     gboolean stream_is_big_endian;          /* big endianess */
2459 """
2460
2461
2462
2463
2464     #
2465     # Template for the helper function
2466     # to get stream endianess from header
2467     #
2468
2469     template_union_helper_function_get_endianess = """\
2470
2471 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2472
2473 """
2474
2475     
2476     template_union_helper_function_end = """\
2477 }
2478 """
2479
2480
2481
2482 #-------------------------------------------------------------#
2483 #             Value string  templates                         #
2484 #-------------------------------------------------------------#
2485
2486     template_value_string_start = """\
2487 static const value_string @valstringname@[] = {
2488 """
2489
2490     template_value_string_entry = """\
2491    { @intval@, \"@description@\" }, """    
2492     
2493     template_value_string_end = """\
2494    { 0,       NULL },
2495 };
2496     
2497 """
2498
2499     
2500
2501 #-------------------------------------------------------------#
2502 #             Enum   handling templates                       #
2503 #-------------------------------------------------------------#
2504
2505     template_comment_enums_start = """\
2506 /*
2507  * IDL Enums Start
2508  */
2509  
2510  """
2511     
2512     template_comment_enums_end = """\
2513 /*
2514  * IDL Enums End
2515  */
2516  
2517  """
2518     
2519     template_comment_enum_comment = """\
2520 /*
2521  * Enum = @ename@
2522  */
2523  
2524  """
2525
2526
2527
2528 #-------------------------------------------------------------#
2529 #             Attribute handling templates                    #
2530 #-------------------------------------------------------------#
2531
2532
2533     template_comment_attributes_start = """\
2534 /*
2535  * IDL Attributes Start
2536  */
2537  
2538  """
2539
2540     #
2541     # get/set accessor method names are language mapping dependant.
2542     #
2543
2544     template_attributes_declare_Java_get = """static const char get_@sname@_at[] = \"_get_@atname@\" ;"""
2545     template_attributes_declare_Java_set = """static const char set_@sname@_at[] = \"_set_@atname@\" ;"""
2546  
2547
2548     template_comment_attributes_end = """
2549 /*
2550  * IDL Attributes End
2551  */
2552  
2553 """
2554
2555
2556     #
2557     # template for Attribute delegation code
2558     #
2559     # Note: _get_xxx() should only be called for Reply with NO_EXCEPTION
2560     # Note: _set_xxx() should only be called for Request
2561     #
2562     #
2563
2564     template_at_delegate_code_get = """\
2565 if (!strcmp(operation, get_@sname@_at ) && (header->message_type == Reply) && (header->rep_status == NO_EXCEPTION) ) {
2566    decode_get_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2567    return TRUE;
2568 }
2569 """
2570     
2571     template_at_delegate_code_set = """\
2572 if (!strcmp(operation, set_@sname@_at ) && (header->message_type == Request) ) {
2573    decode_set_@sname@_at(tvb, pinfo, tree, offset, header, operation);
2574    return TRUE;
2575 }
2576 """
2577         
2578
2579
2580     template_attribute_helpers_start = """\
2581 /*  Begin Attribute Helper Functions  */
2582
2583 """
2584     
2585     template_attribute_helpers_end = """\
2586     
2587 /*  End Attribute Helper Functions  */
2588
2589 """
2590
2591 #
2592 # template for attribute helper code
2593 #
2594
2595
2596     template_attribute_helper_function_start = """\
2597
2598 /* Attribute = @atname@ */
2599
2600 static void decode_@sname@_at(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation) {
2601
2602     gboolean stream_is_big_endian;          /* big endianess */
2603 """
2604
2605
2606
2607     #
2608     # Template for the helper function
2609     # to get stream endianess from header
2610     #
2611
2612     template_attribute_helper_function_get_endianess = """\
2613
2614 stream_is_big_endian = is_big_endian(header);  /* get stream endianess */
2615
2616 """
2617
2618     
2619     template_attribute_helper_function_end = """\
2620 }
2621 """
2622
2623     
2624 #-------------------------------------------------------------#
2625 #                     Debugging  templates                    #
2626 #-------------------------------------------------------------#
2627
2628
2629     #
2630     # Template for outputting TODO "C" comments
2631     # so user know I need ti improve something.
2632     #
2633
2634     template_debug_TODO = """\
2635
2636 /* TODO - @message@ */
2637 """
2638
2639     #
2640     # Template for outputting WARNING "C" comments
2641     # so user know if I have found a problem.
2642     #
2643
2644     template_debug_WARNING = """\
2645
2646 /* WARNING - @message@ */
2647 """
2648
2649
2650     
2651 #-------------------------------------------------------------#
2652 #                     IDL Union  templates                    #
2653 #-------------------------------------------------------------#
2654
2655
2656
2657     template_comment_union_code_start = """\
2658 /*
2659  * IDL Union Start - @uname@
2660  */
2661  
2662  """
2663
2664
2665     template_comment_union_code_end = """
2666 /*
2667  * IDL union End - @uname@
2668  */
2669  
2670 """
2671
2672     template_comment_union_code_discriminant = """\
2673 /*
2674  * IDL Union - Discriminant - @uname@
2675  */
2676  
2677  """
2678
2679     #
2680     # Cast Unions types to something appropriate
2681     # Enum value cast to guint32, all others cast to gint32 
2682     # as omniidl accessor returns integer or Enum.
2683     #
2684
2685     template_union_code_save_discriminant_enum = """\
2686 disc_s_@discname@ = (gint32) u_octet4;     /* save Enum Value  discriminant and cast to gint32 */
2687 """
2688     
2689     template_union_code_save_discriminant_long = """\
2690 disc_s_@discname@ = (gint32) s_octet4;     /* save gint32 discriminant and cast to gint32 */
2691 """
2692
2693     template_union_code_save_discriminant_short = """\
2694 disc_s_@discname@ = (gint32) s_octet2;     /* save gint16 discriminant and cast to gint32 */
2695 """    
2696     
2697     template_union_code_save_discriminant_char = """\
2698 disc_s_@discname@ = (gint32) u_octet1;     /* save guint1 discriminant and cast to gint32 */
2699 """
2700     
2701     template_union_code_save_discriminant_boolean = """\
2702 disc_s_@discname@ = (gint32) u_octet1;     /* save guint1 discriminant and cast to gint32 */
2703 """
2704
2705
2706     template_comment_union_code_label_compare_start = """\
2707 if (disc_s_@discname@ == @labelval@) {
2708
2709  """
2710     template_comment_union_code_label_compare_end = """\
2711     return;     /* End Compare for this discriminant type */
2712 }
2713
2714  """
2715
2716
2717     template_comment_union_code_label_default_start = """
2718 /* Default Union Case Start */
2719  
2720 """
2721
2722     template_comment_union_code_label_default_end = """\
2723 /* Default Union Case End */
2724  
2725  """
2726
2727     #
2728     # Templates for function prototypes.
2729     # This is used in genDeclares() for declaring function prototypes
2730     # for structs and union helper functions.
2731     #
2732
2733     template_prototype_struct_start = """
2734 /* Struct prototype declaration Start */
2735  
2736 """
2737     
2738     template_prototype_struct_end = """
2739 /* Struct prototype declaration End */
2740  
2741 """
2742     
2743     template_prototype_struct_body = """
2744         
2745 /* Struct = @stname@ */
2746
2747 static void decode_@name@_st(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
2748
2749 """
2750
2751     template_decode_struct = """
2752         
2753 decode_@name@_st(tvb, pinfo, tree, offset, header, operation);
2754
2755 """
2756
2757
2758     
2759     
2760     template_prototype_union_start = """
2761 /* Union prototype declaration Start */
2762  
2763 """
2764     
2765     template_prototype_union_end = """
2766 /* Union prototype declaration End */
2767  
2768 """
2769     
2770     template_prototype_union_body = """
2771         
2772 /* Union = @unname@ */
2773
2774 static void decode_@name@_un(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, int *offset, MessageHeader *header, gchar *operation);
2775
2776 """
2777
2778     template_decode_union = """
2779         
2780 decode_@name@_un(tvb, pinfo, tree, offset, header, operation);
2781
2782 """
2783     
2784