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