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.
12 # $Id: make-reg-dotc.py,v 1.3 2001/06/19 08:29:32 guy Exp $
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 filenames.append("%s/%s" % (srcdir, file))
43 # Look through all files, applying the regex to each line.
44 # If the pattern matches, save the "symbol" section to the
49 # For those that don't know Python, r"" indicates a raw string,
50 # devoid of Python escapes.
51 proto_regex0 = r"^(?P<symbol>proto_register_[_A-Za-z0-9]+)\s*\([^;]+$"
52 proto_regex1 = r"void\s+(?P<symbol>proto_register_[_A-Za-z0-9]+)\s*\([^;]+$"
54 handoff_regex0 = r"^(?P<symbol>proto_reg_handoff_[_A-Za-z0-9]+)\s*\([^;]+$"
55 handoff_regex1 = r"void\s+(?P<symbol>proto_reg_handoff_[_A-Za-z0-9]+)\s*\([^;]+$"
57 # This table drives the pattern-matching and symbol-harvesting
59 ( proto_reg, re.compile(proto_regex0) ),
60 ( proto_reg, re.compile(proto_regex1) ),
61 ( handoff_reg, re.compile(handoff_regex0) ),
62 ( handoff_reg, re.compile(handoff_regex1) ),
66 for filename in filenames:
68 # print "Searching %s" % (filename)
69 for line in file.readlines():
70 for action in patterns:
72 match = regex.search(line)
74 symbol = match.group("symbol")
79 # Sort the lists to make them pretty
83 # Make register_all_protocols()
84 reg_code.write("void register_all_protocols(void) {\n")
86 for symbol in proto_reg:
87 line = " {extern void %s (void); %s ();}\n" % (symbol, symbol)
93 # Make register_all_protocol_handoffs()
94 reg_code.write("void register_all_protocol_handoffs(void) {\n")
96 for symbol in handoff_reg:
97 line = " {extern void %s (void); %s ();}\n" % (symbol, symbol)
100 reg_code.write("}\n")
105 # Remove the old final_file if it exists.
107 os.stat(final_filename)
108 os.remove(final_filename)
112 # Move from tmp file to final file
113 os.rename(tmp_filename, final_filename)