3 # Looks for registration routines in the protocol dissectors,
4 # and assembles C code to call all the routines.
6 # This is a Python version of the make-reg-dotc shell script.
7 # Running the shell script on Win32 is very very slow because of
8 # all the process-launching that goes on --- multiple greps and
9 # seds for each input file. I wrote this python version so that
10 # less processes would have to be started.
18 tmp_filename = "register.c-tmp"
19 final_filename = "register.c"
22 # The first argument is the directory in which the source files live.
27 # All subsequent arguments are the files to scan.
31 reg_code = open(tmp_filename, "w")
33 reg_code.write("/* Do not modify this file. */\n")
34 reg_code.write("/* It is created automatically by the Makefile. */\n")
35 reg_code.write('#include "register.h"\n')
37 # Create the proper list of filenames
40 if os.path.isfile(file):
41 filenames.append(file)
43 filenames.append("%s/%s" % (srcdir, file))
46 # Look through all files, applying the regex to each line.
47 # If the pattern matches, save the "symbol" section to the
52 # For those that don't know Python, r"" indicates a raw string,
53 # devoid of Python escapes.
54 proto_regex0 = r"^(?P<symbol>proto_register_[_A-Za-z0-9]+)\s*\([^;]+$"
55 proto_regex1 = r"void\s+(?P<symbol>proto_register_[_A-Za-z0-9]+)\s*\([^;]+$"
57 handoff_regex0 = r"^(?P<symbol>proto_reg_handoff_[_A-Za-z0-9]+)\s*\([^;]+$"
58 handoff_regex1 = r"void\s+(?P<symbol>proto_reg_handoff_[_A-Za-z0-9]+)\s*\([^;]+$"
60 # This table drives the pattern-matching and symbol-harvesting
62 ( proto_reg, re.compile(proto_regex0) ),
63 ( proto_reg, re.compile(proto_regex1) ),
64 ( handoff_reg, re.compile(handoff_regex0) ),
65 ( handoff_reg, re.compile(handoff_regex1) ),
69 for filename in filenames:
71 # print "Searching %s" % (filename)
72 for line in file.readlines():
73 for action in patterns:
75 match = regex.search(line)
77 symbol = match.group("symbol")
82 # Sort the lists to make them pretty
86 # Make register_all_protocols()
87 reg_code.write("void register_all_protocols(void) {\n")
89 for symbol in proto_reg:
90 line = " {extern void %s (void); %s ();}\n" % (symbol, symbol)
96 # Make register_all_protocol_handoffs()
97 reg_code.write("void register_all_protocol_handoffs(void) {\n")
99 for symbol in handoff_reg:
100 line = " {extern void %s (void); %s ();}\n" % (symbol, symbol)
103 reg_code.write("}\n")
108 # Remove the old final_file if it exists.
110 os.stat(final_filename)
111 os.remove(final_filename)
115 # Move from tmp file to final file
116 os.rename(tmp_filename, final_filename)