Set the svn:eol-style property on all text files to "native", so that
[obnox/wireshark/wip.git] / plugins / plugin_gen.py
1 #! /usr/bin/python
2 # -*- python -*-
3 #
4 # $Id$
5 #
6 # mmelchior@xs4all.nl
7 #
8 # generate files for the windows plugin interface from a file with declarations
9 #
10 # The input for this script is generated by gcc using the following command:
11 #
12 # gcc -aux-info xyzzy $(pkg-config --cflags glib-2.0) -I ethereal-0.9.13 -c plugin_api_list.c
13 #
14 #   this gives one declaration per line, with consistent spacing.
15 #
16 #   with a much more elaborate parser than the one RE we have now, we could do without gcc.
17 #
18
19 """Ethereal Windows interface generator."""
20
21 import sys, string, os, re
22 from string import strip, replace
23
24 pattFile = re.compile('.*plugin_api_list.* extern (.*)') # match filename and select declaration
25 pattName = re.compile('\w* .*?(\w*) \(.*')               # select function name
26
27 if len(sys.argv) > 1:
28     file = open(sys.argv[1], 'r')       # input name on command line
29 else:
30     file = sys.stdin                    # read from a stream
31     
32 f2 = open("Xplugin_api.h", 'w')         # defines to hide indirection
33 f3 = open("Xplugin_api.c", 'w')         # statements to copy addresses from structure
34 f4 = open("Xplugin_api_decls.h", 'w')   # pointer definitions
35 f5 = open("Xplugin_table.h", 'w')       # type definitions
36 f6 = open("Xass-list", 'w');            # exported structure initialization
37
38 comment = "/* This file is generated by %s, do not edit. */\n\n" % sys.argv[0]
39 f2.write(comment)
40 f3.write(comment)
41 f4.write(comment)
42 f5.write(comment)
43 f6.write(comment)
44
45 pos = 0
46 count = 0
47 while 1:
48     line = file.readline()
49     if not line: break
50     matchobj = pattFile.match(line)
51     if matchobj:
52         # print "+", count, " ", strip(line)
53         decl = matchobj.group(1)
54         # print "=      ", decl
55         matchobj = pattName.match(decl)
56         if matchobj:
57             count = count + 1
58             name = matchobj.group(1)
59             # print "       ", name
60             f2.write("#define %s (*p_%s)\n" % (name, name))
61             f3.write("p_%s = pat->p_%s;\n" % (name, name))
62             f4.write("addr_%s p_%s;\n" % (name, name))
63             f5.write(replace("typedef %s\n" % decl, name, "(*addr_%s)" % name))
64             f6.write(name)
65             pos = pos + len(name) + 2
66             if pos > 60:
67                 pos = 0
68                 f6.write(",\n")
69             else:
70                 f6.write(", ")
71         else:
72             print '**** function name not fount in "%s"' % decl
73             
74 f6.write('\n')
75
76 print "%d symbols exported" % count
77
78 file.close()
79 f2.close()
80 f3.close()
81 f4.close()
82 f5.close()
83 f6.close()