From e4ba6dbc3f1e76890b22773807ea37fe8fa2b1bc Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 10 Apr 2024 22:34:10 +0200 Subject: Adding upstream version 4.2.2. Signed-off-by: Daniel Baumann --- tools/make-regs.py | 157 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100755 tools/make-regs.py (limited to 'tools/make-regs.py') diff --git a/tools/make-regs.py b/tools/make-regs.py new file mode 100755 index 00000000..376b3c65 --- /dev/null +++ b/tools/make-regs.py @@ -0,0 +1,157 @@ +#!/usr/bin/env python3 +# +# Looks for registration routines in the source files +# and assembles C code to call all the routines. +# +# SPDX-License-Identifier: GPL-2.0-or-later +# + +import sys +import re + +preamble = """\ +/* + * Do not modify this file. Changes will be overwritten. + * + * Generated automatically using \"make-regs.py\". + */ + +""" + +def gen_prototypes(funcs): + output = "" + for f in funcs: + output += "void {}(void);\n".format(f) + return output + +def gen_array(funcs, name): + output = "{}[] = {{\n".format(name) + for f in funcs: + output += " {{ \"{0}\", {0} }},\n".format(f) + output += " { NULL, NULL }\n};\n" + return output + +def scan_files(infiles, regs): + for path in infiles: + with open(path, 'r', encoding='utf8') as f: + source = f.read() + for array, regex in regs: + matches = re.findall(regex, source) + array.extend(matches) + +def make_dissectors(outfile, infiles): + protos = [] + protos_regex = r"void\s+(proto_register_[\w]+)\s*\(\s*void\s*\)\s*{" + handoffs = [] + handoffs_regex = r"void\s+(proto_reg_handoff_[\w]+)\s*\(\s*void\s*\)\s*{" + + scan_files(infiles, [(protos, protos_regex), (handoffs, handoffs_regex)]) + + if len(protos) < 1: + sys.exit("No protocol registrations found.") + + protos.sort() + handoffs.sort() + + output = preamble + output += """\ +#include "dissectors.h" + +const unsigned long dissector_reg_proto_count = {0}; +const unsigned long dissector_reg_handoff_count = {1}; + +""".format(len(protos), len(handoffs)) + + output += gen_prototypes(protos) + output += "\n" + output += gen_array(protos, "dissector_reg_t dissector_reg_proto") + output += "\n" + output += gen_prototypes(handoffs) + output += "\n" + output += gen_array(handoffs, "dissector_reg_t dissector_reg_handoff") + + with open(outfile, "w") as f: + f.write(output) + + print("Found {0} registrations and {1} handoffs.".format(len(protos), len(handoffs))) + +def make_wtap_modules(outfile, infiles): + wtap_modules = [] + wtap_modules_regex = r"void\s+(register_[\w]+)\s*\(\s*void\s*\)\s*{" + + scan_files(infiles, [(wtap_modules, wtap_modules_regex)]) + + if len(wtap_modules) < 1: + sys.exit("No wiretap registrations found.") + + wtap_modules.sort() + + output = preamble + output += """\ +#include "wtap_modules.h" + +const unsigned wtap_module_count = {0}; + +""".format(len(wtap_modules)) + + output += gen_prototypes(wtap_modules) + output += "\n" + output += gen_array(wtap_modules, "wtap_module_reg_t wtap_module_reg") + + with open(outfile, "w") as f: + f.write(output) + + print("Found {0} registrations.".format(len(wtap_modules))) + +def make_taps(outfile, infiles): + taps = [] + taps_regex = r"void\s+(register_tap_listener_[\w]+)\s*\(\s*void\s*\)\s*{" + + scan_files(infiles, [(taps, taps_regex)]) + + if len(taps) < 1: + sys.exit("No tap registrations found.") + + taps.sort() + + output = preamble + output += """\ +#include "ui/taps.h" + +const unsigned long tap_reg_listener_count = {0}; + +""".format(len(taps)) + + output += gen_prototypes(taps) + output += "\n" + output += gen_array(taps, "tap_reg_t tap_reg_listener") + + with open(outfile, "w") as f: + f.write(output) + + print("Found {0} registrations.".format(len(taps))) + + +def print_usage(): + sys.exit("Usage: {0} \n".format(sys.argv[0])) + +if __name__ == "__main__": + if len(sys.argv) < 4: + print_usage() + + mode = sys.argv[1] + outfile = sys.argv[2] + if sys.argv[3].startswith("@"): + with open(sys.argv[3][1:]) as f: + infiles = [l.strip() for l in f.readlines()] + else: + infiles = sys.argv[3:] + + if mode == "dissectors": + make_dissectors(outfile, infiles) + elif mode == "wtap_modules": + make_wtap_modules(outfile, infiles) + elif mode == "taps": + make_taps(outfile, infiles) + else: + print_usage() -- cgit v1.2.3