fix usage of "if(tree) {" to display the right things, even if no coloring rule is set
[obnox/wireshark/wip.git] / epan / dissectors / 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$
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         if os.path.isfile(file):
41                 filenames.append(file)
42         else:
43                 filenames.append("%s/%s" % (srcdir, file))
44
45
46 # Look through all files, applying the regex to each line.
47 # If the pattern matches, save the "symbol" section to the
48 # appropriate array.
49 proto_reg = []
50 handoff_reg = []
51
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*\([^;]+$"
56
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*\([^;]+$"
59
60 # This table drives the pattern-matching and symbol-harvesting
61 patterns = [
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) ),
66         ]
67
68 # Grep
69 for filename in filenames:
70         file = open(filename)
71 #       print "Searching %s" % (filename)
72         for line in file.readlines():
73                 for action in patterns:
74                         regex = action[1]
75                         match = regex.search(line)
76                         if match:
77                                 symbol = match.group("symbol")
78                                 list = action[0]
79                                 list.append(symbol)
80         file.close()
81
82 # Sort the lists to make them pretty
83 proto_reg.sort()
84 handoff_reg.sort()
85
86 # Make register_all_protocols()
87 reg_code.write("void register_all_protocols(void) {\n")
88
89 for symbol in proto_reg:
90         line = "  {extern void %s (void); %s ();}\n" % (symbol, symbol)
91         reg_code.write(line)
92
93 reg_code.write("}\n")
94
95
96 # Make register_all_protocol_handoffs()
97 reg_code.write("void register_all_protocol_handoffs(void) {\n")
98
99 for symbol in handoff_reg:
100         line = "  {extern void %s (void); %s ();}\n" % (symbol, symbol)
101         reg_code.write(line)
102
103 reg_code.write("}\n")
104
105 # Close the file
106 reg_code.close()
107
108 # Remove the old final_file if it exists.
109 try:
110         os.stat(final_filename)
111         os.remove(final_filename)
112 except OSError:
113         pass
114
115 # Move from tmp file to final file
116 os.rename(tmp_filename, final_filename)
117
118