decryption works, but addid doesn't because of unique pointer layers
[wireshark-sm.git] / tools / make-regs.py
blobf17a3f4a88e420884893a035b0fed158b4dd8110
1 #!/usr/bin/env python3
3 # Looks for registration routines in the source files
4 # and assembles C code to call all the routines.
6 # SPDX-License-Identifier: GPL-2.0-or-later
9 import sys
10 import re
12 preamble = """\
14 * Do not modify this file. Changes will be overwritten.
16 * Generated automatically using \"make-regs.py\".
19 """
21 def gen_prototypes(funcs):
22 output = ""
23 for f in funcs:
24 output += "void {}(void);\n".format(f)
25 return output
27 def gen_array(funcs, name):
28 output = "{}[] = {{\n".format(name)
29 for f in funcs:
30 output += " {{ \"{0}\", {0} }},\n".format(f)
31 output += " { NULL, NULL }\n};\n"
32 return output
34 def scan_files(infiles, regs):
35 for path in infiles:
36 with open(path, 'r', encoding='utf8') as f:
37 source = f.read()
38 for array, regex in regs:
39 matches = re.findall(regex, source)
40 array.extend(matches)
42 def make_dissectors(outfile, infiles):
43 protos = []
44 protos_regex = r"void\s+(proto_register_[\w]+)\s*\(\s*void\s*\)\s*{"
45 handoffs = []
46 handoffs_regex = r"void\s+(proto_reg_handoff_[\w]+)\s*\(\s*void\s*\)\s*{"
48 scan_files(infiles, [(protos, protos_regex), (handoffs, handoffs_regex)])
50 if len(protos) < 1:
51 sys.exit("No protocol registrations found.")
53 protos.sort()
54 handoffs.sort()
56 output = preamble
57 output += """\
58 #include "dissectors.h"
60 const unsigned long dissector_reg_proto_count = {0};
61 const unsigned long dissector_reg_handoff_count = {1};
63 """.format(len(protos), len(handoffs))
65 output += gen_prototypes(protos)
66 output += "\n"
67 output += gen_array(protos, "dissector_reg_t const dissector_reg_proto")
68 output += "\n"
69 output += gen_prototypes(handoffs)
70 output += "\n"
71 output += gen_array(handoffs, "dissector_reg_t const dissector_reg_handoff")
73 with open(outfile, "w") as f:
74 f.write(output)
76 print("Found {0} registrations and {1} handoffs.".format(len(protos), len(handoffs)))
78 def make_wtap_modules(outfile, infiles):
79 wtap_modules = []
80 wtap_modules_regex = r"void\s+(register_[\w]+)\s*\(\s*void\s*\)\s*{"
82 scan_files(infiles, [(wtap_modules, wtap_modules_regex)])
84 if len(wtap_modules) < 1:
85 sys.exit("No wiretap registrations found.")
87 wtap_modules.sort()
89 output = preamble
90 output += """\
91 #include "wtap_modules.h"
93 const unsigned wtap_module_count = {0};
95 """.format(len(wtap_modules))
97 output += gen_prototypes(wtap_modules)
98 output += "\n"
99 output += gen_array(wtap_modules, "wtap_module_reg_t const wtap_module_reg")
101 with open(outfile, "w") as f:
102 f.write(output)
104 print("Found {0} registrations.".format(len(wtap_modules)))
106 def make_taps(outfile, infiles):
107 taps = []
108 taps_regex = r"void\s+(register_tap_listener_[\w]+)\s*\(\s*void\s*\)\s*{"
110 scan_files(infiles, [(taps, taps_regex)])
112 if len(taps) < 1:
113 sys.exit("No tap registrations found.")
115 taps.sort()
117 output = preamble
118 output += """\
119 #include "ui/taps.h"
121 const unsigned long tap_reg_listener_count = {0};
123 """.format(len(taps))
125 output += gen_prototypes(taps)
126 output += "\n"
127 output += gen_array(taps, "tap_reg_t const tap_reg_listener")
129 with open(outfile, "w") as f:
130 f.write(output)
132 print("Found {0} registrations.".format(len(taps)))
135 def print_usage():
136 sys.exit("Usage: {0} <dissectors|taps> <outfile> <infiles...|@filelist>\n".format(sys.argv[0]))
138 if __name__ == "__main__":
139 if len(sys.argv) < 4:
140 print_usage()
142 mode = sys.argv[1]
143 outfile = sys.argv[2]
144 if sys.argv[3].startswith("@"):
145 with open(sys.argv[3][1:]) as f:
146 infiles = [line.strip() for line in f.readlines()]
147 else:
148 infiles = sys.argv[3:]
150 if mode == "dissectors":
151 make_dissectors(outfile, infiles)
152 elif mode == "wtap_modules":
153 make_wtap_modules(outfile, infiles)
154 elif mode == "taps":
155 make_taps(outfile, infiles)
156 else:
157 print_usage()