Reduce the CinemaScope-like proportions of the preferences dialog by
[obnox/wireshark/wip.git] / make-reg-dotc.py
1 #!/usr/bin/python
2 #
3 # Looks for registration routines in the protocol dissectors,
4 # and assembles C code to call all the routines.
5 #
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.
11 #
12 # $Id: make-reg-dotc.py,v 1.3 2001/06/19 08:29:32 guy Exp $
13
14 import os
15 import sys
16 import re
17
18 tmp_filename = "register.c-tmp"
19 final_filename = "register.c"
20
21 #
22 # The first argument is the directory in which the source files live.
23 #
24 srcdir = sys.argv[1]
25
26 #
27 # All subsequent arguments are the files to scan.
28 #
29 files = sys.argv[2:]
30
31 reg_code = open(tmp_filename, "w")
32
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')
36
37 # Create the proper list of filenames
38 filenames = []
39 for file in files:
40         filenames.append("%s/%s" % (srcdir, file))
41
42
43 # Look through all files, applying the regex to each line.
44 # If the pattern matches, save the "symbol" section to the
45 # appropriate array.
46 proto_reg = []
47 handoff_reg = []
48
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*\([^;]+$"
53
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*\([^;]+$"
56
57 # This table drives the pattern-matching and symbol-harvesting
58 patterns = [
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) ),
63         ]
64
65 # Grep
66 for filename in filenames:
67         file = open(filename)
68 #       print "Searching %s" % (filename)
69         for line in file.readlines():
70                 for action in patterns:
71                         regex = action[1]
72                         match = regex.search(line)
73                         if match:
74                                 symbol = match.group("symbol")
75                                 list = action[0]
76                                 list.append(symbol)
77         file.close()
78
79 # Sort the lists to make them pretty
80 proto_reg.sort()
81 handoff_reg.sort()
82
83 # Make register_all_protocols()
84 reg_code.write("void register_all_protocols(void) {\n")
85
86 for symbol in proto_reg:
87         line = "  {extern void %s (void); %s ();}\n" % (symbol, symbol)
88         reg_code.write(line)
89
90 reg_code.write("}\n")
91
92
93 # Make register_all_protocol_handoffs()
94 reg_code.write("void register_all_protocol_handoffs(void) {\n")
95
96 for symbol in handoff_reg:
97         line = "  {extern void %s (void); %s ();}\n" % (symbol, symbol)
98         reg_code.write(line)
99
100 reg_code.write("}\n")
101
102 # Close the file
103 reg_code.close()
104
105 # Remove the old final_file if it exists.
106 try:
107         os.stat(final_filename)
108         os.remove(final_filename)
109 except OSError:
110         pass
111
112 # Move from tmp file to final file
113 os.rename(tmp_filename, final_filename)
114
115