TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags
[wireshark-sm.git] / tools / make-plugin-reg.py
blob135850c2ee4def7d838893ff0120220502cf2e38
1 #!/usr/bin/env python3
3 # Looks for registration routines in the plugins
4 # and assembles C code to call all the routines.
5 # A new "plugin.c" file will be written in the current directory.
8 import os
9 import sys
10 import re
13 # The first argument is the directory in which the source files live.
15 srcdir = sys.argv[1]
17 # The second argument is either "plugin", "plugin_wtap", "plugin_codec",
18 # or "plugin_tap".
20 registertype = sys.argv[2]
22 # All subsequent arguments are the files to scan.
24 files = sys.argv[3:]
26 final_filename = "plugin.c"
27 preamble = """\
29 * Do not modify this file. Changes will be overwritten.
31 * Generated automatically from %s.
33 """ % (os.path.basename(sys.argv[0]))
35 # Create the proper list of filenames
36 filenames = []
37 for file in files:
38 if os.path.isfile(file):
39 filenames.append(file)
40 else:
41 filenames.append(os.path.join(srcdir, file))
43 if len(filenames) < 1:
44 print("No files found")
45 sys.exit(1)
48 # Look through all files, applying the regex to each line.
49 # If the pattern matches, save the "symbol" section to the
50 # appropriate set.
51 regs = {
52 'proto_reg': set(),
53 'handoff_reg': set(),
54 'wtap_register': set(),
55 'codec_register': set(),
56 'register_tap_listener': set(),
59 # For those that don't know Python, r"" indicates a raw string,
60 # devoid of Python escapes.
61 proto_regex = r"\bproto_register_(?P<symbol>[\w]+)\s*\(\s*void\s*\)\s*{"
63 handoff_regex = r"\bproto_reg_handoff_(?P<symbol>[\w]+)\s*\(\s*void\s*\)\s*{"
65 wtap_reg_regex = r"\bwtap_register_(?P<symbol>[\w]+)\s*\(\s*void\s*\)\s*{"
67 codec_reg_regex = r"\bcodec_register_(?P<symbol>[\w]+)\s*\(\s*void\s*\)\s*{"
69 tap_reg_regex = r"\bregister_tap_listener_(?P<symbol>[\w]+)\s*\(\s*void\s*\)\s*{"
71 # This table drives the pattern-matching and symbol-harvesting
72 patterns = [
73 ( 'proto_reg', re.compile(proto_regex, re.MULTILINE | re.ASCII) ),
74 ( 'handoff_reg', re.compile(handoff_regex, re.MULTILINE | re.ASCII) ),
75 ( 'wtap_register', re.compile(wtap_reg_regex, re.MULTILINE | re.ASCII) ),
76 ( 'codec_register', re.compile(codec_reg_regex, re.MULTILINE | re.ASCII) ),
77 ( 'register_tap_listener', re.compile(tap_reg_regex, re.MULTILINE | re.ASCII) ),
80 # Grep
81 for filename in filenames:
82 file = open(filename)
83 # Read the whole file into memory
84 contents = file.read()
85 for action in patterns:
86 regex = action[1]
87 for match in regex.finditer(contents):
88 symbol = match.group("symbol")
89 sym_type = action[0]
90 regs[sym_type].add(symbol)
91 # We're done with the file contents
92 del contents
93 file.close()
95 # Make sure we actually processed something
96 if (len(regs['proto_reg']) < 1 and len(regs['wtap_register']) < 1 and len(regs['codec_register']) < 1 and len(regs['register_tap_listener']) < 1):
97 print("No plugin registrations found")
98 sys.exit(1)
100 # Convert the sets into sorted lists to make the output pretty
101 regs['proto_reg'] = sorted(regs['proto_reg'])
102 regs['handoff_reg'] = sorted(regs['handoff_reg'])
103 regs['wtap_register'] = sorted(regs['wtap_register'])
104 regs['codec_register'] = sorted(regs['codec_register'])
105 regs['register_tap_listener'] = sorted(regs['register_tap_listener'])
107 reg_code = ""
109 reg_code += preamble
111 reg_code += """
112 #include "config.h"
114 #include <gmodule.h>
116 /* plugins are DLLs on Windows */
117 #define WS_BUILD_DLL
118 #include "ws_symbol_export.h"
119 #include <wsutil/plugins.h>
123 if registertype == "plugin":
124 reg_code += "#include \"epan/proto.h\"\n\n"
125 if registertype == "plugin_wtap":
126 reg_code += "#include \"wiretap/wtap.h\"\n\n"
127 if registertype == "plugin_codec":
128 reg_code += "#include \"wsutil/codecs.h\"\n\n"
129 if registertype == "plugin_tap":
130 reg_code += "#include \"epan/tap.h\"\n\n"
132 for symbol in regs['proto_reg']:
133 reg_code += "void proto_register_%s(void);\n" % (symbol)
134 for symbol in regs['handoff_reg']:
135 reg_code += "void proto_reg_handoff_%s(void);\n" % (symbol)
136 for symbol in regs['wtap_register']:
137 reg_code += "void wtap_register_%s(void);\n" % (symbol)
138 for symbol in regs['codec_register']:
139 reg_code += "void codec_register_%s(void);\n" % (symbol)
140 for symbol in regs['register_tap_listener']:
141 reg_code += "void register_tap_listener_%s(void);\n" % (symbol)
143 DESCRIPTION_FLAG = {
144 'plugin': 'WS_PLUGIN_DESC_DISSECTOR',
145 'plugin_wtap': 'WS_PLUGIN_DESC_FILE_TYPE',
146 'plugin_codec': 'WS_PLUGIN_DESC_CODEC',
147 'plugin_tap': 'WS_PLUGIN_DESC_TAP_LISTENER'
150 reg_code += """
151 WS_DLL_PUBLIC_DEF const char plugin_version[] = PLUGIN_VERSION;
152 WS_DLL_PUBLIC_DEF const int plugin_want_major = VERSION_MAJOR;
153 WS_DLL_PUBLIC_DEF const int plugin_want_minor = VERSION_MINOR;
155 WS_DLL_PUBLIC void plugin_register(void);
156 WS_DLL_PUBLIC uint32_t plugin_describe(void);
158 uint32_t plugin_describe(void)
160 return %s;
163 void plugin_register(void)
165 """ % DESCRIPTION_FLAG[registertype]
167 if registertype == "plugin":
168 for symbol in regs['proto_reg']:
169 reg_code +=" static proto_plugin plug_%s;\n\n" % (symbol)
170 reg_code +=" plug_%s.register_protoinfo = proto_register_%s;\n" % (symbol, symbol)
171 if symbol in regs['handoff_reg']:
172 reg_code +=" plug_%s.register_handoff = proto_reg_handoff_%s;\n" % (symbol, symbol)
173 else:
174 reg_code +=" plug_%s.register_handoff = NULL;\n" % (symbol)
175 reg_code += " proto_register_plugin(&plug_%s);\n" % (symbol)
176 if registertype == "plugin_wtap":
177 for symbol in regs['wtap_register']:
178 reg_code += " static wtap_plugin plug_%s;\n\n" % (symbol)
179 reg_code += " plug_%s.register_wtap_module = wtap_register_%s;\n" % (symbol, symbol)
180 reg_code += " wtap_register_plugin(&plug_%s);\n" % (symbol)
181 if registertype == "plugin_codec":
182 for symbol in regs['codec_register']:
183 reg_code += " static codecs_plugin plug_%s;\n\n" % (symbol)
184 reg_code += " plug_%s.register_codec_module = codec_register_%s;\n" % (symbol, symbol)
185 reg_code += " codecs_register_plugin(&plug_%s);\n" % (symbol)
186 if registertype == "plugin_tap":
187 for symbol in regs['register_tap_listener']:
188 reg_code += " static tap_plugin plug_%s;\n\n" % (symbol)
189 reg_code += " plug_%s.register_tap_listener = register_tap_listener_%s;\n" % (symbol, symbol)
190 reg_code += " tap_register_plugin(&plug_%s);\n" % (symbol)
192 reg_code += "}\n"
194 try:
195 fh = open(final_filename, 'w')
196 fh.write(reg_code)
197 fh.close()
198 except OSError:
199 sys.exit('Unable to write ' + final_filename + '.\n')
202 # Editor modelines - https://www.wireshark.org/tools/modelines.html
204 # Local variables:
205 # c-basic-offset: 4
206 # indent-tabs-mode: nil
207 # End:
209 # vi: set shiftwidth=4 expandtab:
210 # :indentSize=4:noTabs=true: