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.
19 # The first argument is the directory in which the source files live.
24 # The second argument is either "plugin" or "dissectors"; if it's
25 # "plugin", we build a plugin.c for a plugin, and if it's
26 # "dissectors", we build a register.c for libethereal.
28 registertype = sys.argv[2]
29 if registertype == "plugin":
30 tmp_filename = "plugin.c-tmp"
31 final_filename = "plugin.c"
32 elif registertype == "dissectors":
33 tmp_filename = "register.c-tmp"
34 final_filename = "register.c"
36 print "Unknown output type '%s'" % registertype
41 # All subsequent arguments are the files to scan.
45 # Create the proper list of filenames
48 if os.path.isfile(file):
49 filenames.append(file)
51 filenames.append("%s/%s" % (srcdir, file))
54 # Look through all files, applying the regex to each line.
55 # If the pattern matches, save the "symbol" section to the
60 # For those that don't know Python, r"" indicates a raw string,
61 # devoid of Python escapes.
62 proto_regex0 = r"^(?P<symbol>proto_register_[_A-Za-z0-9]+)\s*\([^;]+$"
63 proto_regex1 = r"void\s+(?P<symbol>proto_register_[_A-Za-z0-9]+)\s*\([^;]+$"
65 handoff_regex0 = r"^(?P<symbol>proto_reg_handoff_[_A-Za-z0-9]+)\s*\([^;]+$"
66 handoff_regex1 = r"void\s+(?P<symbol>proto_reg_handoff_[_A-Za-z0-9]+)\s*\([^;]+$"
68 # This table drives the pattern-matching and symbol-harvesting
70 ( proto_reg, re.compile(proto_regex0) ),
71 ( proto_reg, re.compile(proto_regex1) ),
72 ( handoff_reg, re.compile(handoff_regex0) ),
73 ( handoff_reg, re.compile(handoff_regex1) ),
77 for filename in filenames:
79 # print "Searching %s" % (filename)
80 for line in file.readlines():
81 for action in patterns:
83 match = regex.search(line)
85 symbol = match.group("symbol")
90 # Sort the lists to make them pretty
94 reg_code = open(tmp_filename, "w")
96 reg_code.write("/* Do not modify this file. */\n")
97 reg_code.write("/* It is created automatically by the Makefile. */\n")
99 # Make the routine to register all protocols
100 if registertype == "plugin":
107 #include "register.h"
109 #include "moduleinfo.h"
111 #ifndef ENABLE_STATIC
112 G_MODULE_EXPORT const gchar version[] = VERSION;
114 /* Start the functions we need for the plugin stuff */
117 plugin_register (void)
122 #include "register.h"
124 register_all_protocols(void)
128 for symbol in proto_reg:
129 line = " {extern void %s (void); %s ();}\n" % (symbol, symbol)
132 reg_code.write("}\n")
135 # Make the routine to register all protocol handoffs
136 if registertype == "plugin":
139 plugin_reg_handoff(void)
145 register_all_protocol_handoffs(void)
149 for symbol in handoff_reg:
150 line = " {extern void %s (void); %s ();}\n" % (symbol, symbol)
153 reg_code.write("}\n")
155 if registertype == "plugin":
156 reg_code.write("#endif");
161 # Remove the old final_file if it exists.
163 os.stat(final_filename)
164 os.remove(final_filename)
168 # Move from tmp file to final file
169 os.rename(tmp_filename, final_filename)