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