wireshark/tools/make-regs.py

158 lines
3.9 KiB
Python
Raw Normal View History

#!/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 gulong dissector_reg_proto_count = {0};
const gulong 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"
wiretap: more work on file type/subtypes. Provide a wiretap routine to get an array of all savable file type/subtypes, sorted with pcap and pcapng at the top, followed by the other types, sorted either by the name or the description. Use that routine to list options for the -F flag for various commands Rename wtap_get_savable_file_types_subtypes() to wtap_get_savable_file_types_subtypes_for_file(), to indicate that it provides an array of all file type/subtypes in which a given file can be saved. Have it sort all types, other than the default type/subtype and, if there is one, the "other" type (both of which are put at the top), by the name or the description. Don't allow wtap_register_file_type_subtypes() to override any existing registrations; have them always register a new type. In that routine, if there are any emply slots in the table, due to an entry being unregistered, use it rather than allocating a new slot. Don't allow unregistration of built-in types. Rename the "dump open table" to the "file type/subtype table", as it has entries for all types/subtypes, even if we can't write them. Initialize that table in a routine that pre-allocates the GArray before filling it with built-in types/subtypes, so it doesn't keep getting reallocated. Get rid of wtap_num_file_types_subtypes - it's just a copy of the size of the GArray. Don't have wtap_file_type_subtype_description() crash if handed an file type/subtype that isn't a valid array index - just return NULL, as we do with wtap_file_type_subtype_name(). In wtap_name_to_file_type_subtype(), don't use WTAP_FILE_TYPE_SUBTYPE_ names for the backwards-compatibility names - map those names to the current names, and then look them up. This reduces the number of uses of hardwired WTAP_FILE_TYPE_SUBTYPE_ values. Clean up the type of wtap_module_count - it has no need to be a gulong. Have built-in wiretap file handlers register names to be used for their file type/subtypes, rather than building the table in init.lua. Add a new Lua C function get_wtap_filetypes() to construct the wtap_filetypes table, based on the registered names, and use it in init.lua. Add a #define WSLUA_INTERNAL_FUNCTION to register functions intended only for internal use in init.lua, so they can be made available from Lua without being documented. Get rid of WTAP_NUM_FILE_TYPES_SUBTYPES - most code has no need to use it, as it can just request arrays of types, and the space of type/subtype codes can be sparse due to registration in any case, so code has to be careful using it. wtap_get_num_file_types_subtypes() is no longer used, so remove it. It returns the number of elements in the file type/subtype array, which is not necessarily the name of known file type/subtypes, as there may have been some deregistered types, and those types do *not* get removed from the array, they just get cleared so that they're available for future allocation (we don't want the indices of any registered types to changes if another type is deregistered, as those indicates are the type/subtype values, so we can't shrink the array). Clean up white space and remove some comments that shouldn't have been added.
2021-02-17 06:24:47 +00:00
const guint 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 gulong 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} <dissectors|taps> <outfile> <infiles...|@filelist>\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()