[Diameter] Add a couple of command codes from IANA.
[metze/wireshark/wip.git] / tools / wireshark_be.py
1 # -*- python -*-
2 #
3 #    File      : wireshark_be.py
4 #
5 #    Author    : Frank Singleton (frank.singleton@ericsson.com)
6 #
7 #    Copyright (C) 2001 Frank Singleton, Ericsson Inc.
8 #
9 #  This file is a backend to "omniidl", used to generate "Wireshark"
10 #  dissectors from IDL descriptions. The output language generated
11 #  is "C". It will generate code to use the GIOP/IIOP get_CDR_XXX API.
12 #
13 #  Please see packet-giop.h in Wireshark distro for API description.
14 #  Wireshark is available at http://www.wireshark.org/
15 #
16 #  Omniidl is part of the OmniOrb distribution, and is available at
17 #  http://omniorb.sourceforge.net
18 #
19 #  This program is free software; you can redistribute it and/or modify it
20 #  under the terms of the GNU General Public License as published by
21 #  the Free Software Foundation; either version 2 of the License, or
22 #  (at your option) any later version.
23 #
24 #  This program is distributed in the hope that it will be useful,
25 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
26 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27 #  General Public License for more details.
28 #
29 #  You should have received a copy of the GNU General Public License
30 #  along with this program; if not, write to the Free Software
31 #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #
33 # Description:
34 #
35 #   Omniidl Back-end which parses an IDL data structure provided by the frontend
36 #   and generates packet-idl-xxx.[ch] for compiling as a dissector in 
37 #   Wireshark IP protocol anlayser.
38 #
39 #
40 #
41 #
42 # Strategy. 
43 #
44 # Crawl all the way down all branches until I hit  "Operation", "Enum", "Attribute",
45 # "Struct" and "Union" nodes.  Then store these nodes in lists.
46 #
47 # Pass these lists (via an object ref) to the src code
48 # generator (wireshark_gen) class and let it do the hard work ! 
49 #
50 #
51 # Don't forget structs can contain embedded structs etc .. so don't forget
52 # to peek inside and check :-)
53 #
54 #
55
56
57 """Wireshark IDL compiler back-end."""
58
59 from omniidl import idlast, idltype, idlvisitor, idlutil, output
60 import sys, string
61 from os import path
62 from wireshark_gen import wireshark_gen_C
63
64 #
65 # This class finds the "Operation" nodes ,Enum Nodes, "Attribute" nodes, Struct Nodes
66 # and Union Nodes. Then it hands them off to an instance of the source code generator
67 # class "wireshark_gen" 
68 #
69
70 class WiresharkVisitor:
71
72     DEBUG = 0                           # debug flag
73
74     def __init__(self, st):
75         self.st = st
76         self.oplist = []                # list of operation nodes
77         self.enlist = []                # list of enum nodes
78         self.atlist = []                # list of attribute nodes
79         self.stlist = []                # list of struct nodes
80         self.unlist = []                # list of union nodes
81
82
83     def visitAST(self, node):
84         if self.DEBUG:
85             print "XXX visitAST() node = ", node
86
87         for n in node.declarations():
88             if isinstance(n, idlast.Module):
89                 self.visitModule(n)
90             if isinstance(n, idlast.Interface):
91                 self.visitInterface(n)
92             if isinstance(n, idlast.Operation):
93                 self.visitOperation(n)
94             if isinstance(n, idlast.Attribute):
95                 self.visitAttribute(n)
96             if isinstance(n, idlast.Enum):
97                 self.visitEnum(n)
98             if isinstance(n, idlast.Struct):
99                 self.visitStruct(n)
100             if isinstance(n, idlast.Union):
101                 self.visitUnion(n)
102
103             # Check for Typedef structs and unions
104
105             if isinstance(n, idlast.Typedef):
106                 self.visitTypedef(n)    # who are you ?
107
108
109     def visitModule(self, node):
110         if self.DEBUG:
111             print "XXX visitModule() node = ", node
112
113         for n in node.definitions():
114             if isinstance(n, idlast.Module):
115                 self.visitModule(n)
116             if isinstance(n, idlast.Interface):
117                 self.visitInterface(n)
118             if isinstance(n, idlast.Operation):
119                 self.visitOperation(n)
120             if isinstance(n, idlast.Attribute):
121                 self.visitAttribute(n)
122             if isinstance(n, idlast.Enum):
123                 self.visitEnum(n)
124             if isinstance(n, idlast.Struct):
125                 self.visitStruct(n)
126             if isinstance(n, idlast.Union):
127                 self.visitUnion(n)
128
129             # Check for Typedef structs and unions
130
131             if isinstance(n, idlast.Typedef):
132                 self.visitTypedef(n)    # who are you ?
133
134
135     def visitInterface(self, node):
136         if self.DEBUG:
137             print "XXX visitInterface() node = ", node
138
139         for c in node.callables():
140             if isinstance(c, idlast.Operation):
141                 self.visitOperation(c)
142             if isinstance(c, idlast.Attribute):
143                 self.visitAttribute(c)
144
145         for d in node.contents():
146             if isinstance(d, idlast.Enum):
147                 self.visitEnum(d)
148
149             if isinstance(d, idlast.Struct):
150                 self.visitStruct(d)
151
152             if isinstance(d, idlast.Union):
153                 self.visitUnion(d)
154
155             # Check for Typedef structs and unions
156
157             if isinstance(d, idlast.Typedef):
158                 self.visitTypedef(d)    # who are you ?
159
160
161     #
162     # visitOperation
163     #
164     # populates the operations node list "oplist"
165     #
166     #
167
168     def visitOperation(self,opnode):
169         if not opnode in self.oplist:
170             self.oplist.append(opnode)      # store operation node
171
172     #
173     # visitAttribute
174     #
175     # populates the attribute node list "atlist"
176     #
177     #
178
179     def visitAttribute(self,atnode):
180         if not atnode in self.atlist:
181             self.atlist.append(atnode)      # store attribute node
182
183
184     #
185     # visitEnum
186     #
187     # populates the Enum node list "enlist"
188     #
189     #
190
191     def visitEnum(self,enode):
192         if not enode in self.enlist:
193             self.enlist.append(enode)      # store enum node if unique
194
195     #
196     # visitTypedef
197     #
198     # Search to see if its a typedef'd struct, union, or enum
199     #
200     # eg: typdef enum colors {red, green, blue } mycolors;
201     #
202
203     def visitTypedef(self,td):
204         d = td.aliasType()              # get Type, possibly Declared
205         if isinstance(d,idltype.Declared):
206             self.visitDeclared(d)
207
208
209     #
210     # visitDeclared
211     #
212     # Search to see if its a struct, union, or enum
213     #
214     #
215
216     def visitDeclared(self,d):
217         if isinstance(d,idltype.Declared):
218             sue = d.decl()             # grab the struct or union or enum 
219
220             if isinstance(sue, idlast.Struct):
221                 self.visitStruct(sue)
222             if isinstance(sue, idlast.Union):
223                 self.visitUnion(sue)
224             if isinstance(sue, idlast.Enum):
225                 self.visitEnum(sue)
226
227
228
229
230     #
231     # visitStruct
232     #
233     # populates the struct node list "stlist"
234     # and checks its members also
235     #
236     #
237
238     def visitStruct(self,stnode):
239         if not stnode in self.stlist:
240             self.stlist.append(stnode)      # store struct node if unique and avoid recursive loops
241                                             # if we come across recursive structs
242
243             for m in stnode.members():      # find embedded struct definitions within this
244                 mt = m.memberType()
245                 if isinstance(mt,idltype.Declared):
246                     self.visitDeclared(mt)      # if declared, then check it out 
247
248
249     #
250     # visitUnion
251     #
252     # populates the struct node list "unlist"
253     # and checks its members also
254     #
255     #
256
257     def visitUnion(self,unnode):
258         if not unnode in self.unlist:
259             self.unlist.append(unnode)      # store union node if unique
260
261             if unnode.constrType():         # enum defined within switch type
262                 if isinstance(unnode.switchType(),idltype.Declared):
263                     self.visitDeclared(unnode.switchType())
264
265             for c in unnode.cases():
266                 ct =  c.caseType()
267                 if isinstance(ct,idltype.Declared):
268                     self.visitDeclared(ct)      # if declared, then check it out 
269
270
271 def run(tree, args):
272
273     st = output.Stream(sys.stdout, 4)   # set indent for stream
274     ev = WiresharkVisitor(st)            # create visitor object
275
276     ev.visitAST(tree)                   # go find some operations
277
278     #
279     # Grab name of main IDL file being compiled.
280     # 
281     # Assumption: Name is of the form   abcdefg.xyz  (eg: CosNaming.idl)
282     #
283
284     fname = path.basename(tree.file())    # grab basename only, dont care about path
285     nl = string.split(fname,".")[0]       # split name of main IDL file using "." as separator
286                                           # and grab first field (eg: CosNaming)
287
288     if ev.DEBUG:
289         for i in ev.oplist:
290             print "XXX - Operation node ", i, " repoId() = ", i.repoId()
291         for i in ev.atlist:
292             print "XXX - Attribute node ", i, " identifiers() = ", i.identifiers()
293         for i in ev.enlist:
294             print "XXX - Enum node ", i, " repoId() = ", i.repoId()
295         for i in ev.stlist:
296             print "XXX - Struct node ", i, " repoId() = ", i.repoId()
297         for i in ev.unlist:
298             print "XXX - Union node ", i, " repoId() = ", i.repoId()
299
300
301     # create a C generator object
302     # and generate some C code
303
304
305     eg = wireshark_gen_C(ev.st, string.upper(nl), string.lower(nl), string.capitalize(nl) + " Dissector Using GIOP API") 
306     eg.genCode(ev.oplist, ev.atlist, ev.enlist, ev.stlist, ev.unlist)    # pass them onto the C generator
307
308 #
309 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
310 #
311 # Local variables:
312 # c-basic-offset: 4
313 # indent-tabs-mode: nil
314 # End:
315 #
316 # vi: set shiftwidth=4 expandtab:
317 # :indentSize=4:noTabs=true:
318 #