3 # Looks for registration routines in the taps,
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.
21 # The first argument is the directory in which the source files live.
26 # The second argument is "taps".
28 registertype = sys.argv[2]
29 if registertype == "taps":
30 tmp_filename = "wireshark-tap-register.c-tmp"
31 final_filename = "wireshark-tap-register.c"
32 cache_filename = "wireshark-tap-register-cache.pkl"
33 elif registertype == "tshark-taps":
34 tmp_filename = "tshark-tap-register.c-tmp"
35 final_filename = "tshark-tap-register.c"
36 cache_filename = "tshark-tap-register-cache.pkl"
38 print "Unknown output type '%s'" % registertype
43 # All subsequent arguments are the files to scan.
47 # Create the proper list of filenames
50 if os.path.isfile(file):
51 filenames.append(file)
53 filenames.append("%s/%s" % (srcdir, file))
55 if len(filenames) < 1:
56 print "No files found"
60 # Look through all files, applying the regex to each line.
61 # If the pattern matches, save the "symbol" section to the
67 # For those that don't know Python, r"" indicates a raw string,
68 # devoid of Python escapes.
69 tap_regex0 = r"^(?P<symbol>register_tap_listener_[_A-Za-z0-9]+)\s*\([^;]+$"
70 tap_regex1 = r"void\s+(?P<symbol>register_tap_listener_[_A-Za-z0-9]+)\s*\([^;]+$"
72 # This table drives the pattern-matching and symbol-harvesting
74 ( 'tap_reg', re.compile(tap_regex0) ),
75 ( 'tap_reg', re.compile(tap_regex1) ),
78 # Open our registration symbol cache
82 cache_file = open(cache_filename, 'rb')
83 cache = pickle.load(cache_file)
89 for filename in filenames:
91 cur_mtime = os.fstat(file.fileno())[ST_MTIME]
92 if cache and cache.has_key(filename):
93 cdict = cache[filename]
94 if cur_mtime == cdict['mtime']:
95 # print "Pulling %s from cache" % (filename)
96 regs['tap_reg'].extend(cdict['tap_reg'])
99 # We don't have a cache entry
100 if cache is not None:
105 # print "Searching %s" % (filename)
106 for line in file.readlines():
107 for action in patterns:
109 match = regex.search(line)
111 symbol = match.group("symbol")
113 regs[sym_type].append(symbol)
114 if cache is not None:
115 # print "Caching %s for %s: %s" % (sym_type, filename, symbol)
116 cache[filename][sym_type].append(symbol)
119 if cache is not None and cache_filename is not None:
120 cache_file = open(cache_filename, 'wb')
121 pickle.dump(cache, cache_file)
124 # Make sure we actually processed something
125 if len(regs['tap_reg']) < 1:
126 print "No protocol registrations found"
129 # Sort the lists to make them pretty
130 regs['tap_reg'].sort()
132 reg_code = open(tmp_filename, "w")
134 reg_code.write("/* Do not modify this file. */\n")
135 reg_code.write("/* It is created automatically by the Makefile. */\n")
137 # Make the routine to register all taps
139 #include "register.h"
140 void register_all_tap_listeners(void) {
143 for symbol in regs['tap_reg']:
144 line = " {extern void %s (void); %s ();}\n" % (symbol, symbol)
147 reg_code.write("}\n")
153 # Remove the old final_file if it exists.
155 os.stat(final_filename)
156 os.remove(final_filename)
160 # Move from tmp file to final file
161 os.rename(tmp_filename, final_filename)