Driver.pm (pidl): fix spelling typo found by lintian
[metze/wireshark/wip.git] / tools / ftsanity.py
1 #!/usr/bin/env python
2 """
3 Check the sanity of field definitions in Wireshark.
4 """
5 #
6 # Gilbert Ramirez <gram [AT] alumni.rice.edu>
7 #
8 # Wireshark - Network traffic analyzer
9 # By Gerald Combs <gerald@wireshark.org>
10 # Copyright 1998 Gerald Combs
11 #
12 # This program is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 2
15 # of the License, or (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 import sys
27
28 from optparse import OptionParser
29 import subprocess
30
31
32 errors = 0
33
34 class Proto:
35     """Data for a protocol."""
36     def __init__(self, line):
37         data = line.split("\t")
38         assert len(data) == 3, "expected 3 columns in %s" % data
39         assert data[0] == "P"
40         self.name = data[1]
41         self.abbrev = data[2]
42
43 class Field:
44     """Data for a field."""
45     def __init__(self, line):
46         data = line.split("\t")
47         assert len(data) == 8, "expected 8 columns in %s" % data
48         assert data[0] == "F"
49         self.name = data[1]
50         self.abbrev = data[2]
51         self.ftype = data[3]
52         self.parent = data[4]
53         self.base = data[5]
54         self.bitmask = int(data[6],0)
55         self.blurb = data[7]
56
57
58 def gather_data(tshark):
59     """Calls tshark and gathers data."""
60     proc = subprocess.Popen([tshark, "-G", "fields"],
61         stdout=subprocess.PIPE)
62     output, error = proc.communicate()
63
64     if proc.returncode != 0:
65         sys.exit("Failed: tshark -G fields")
66
67     if sys.version_info[0] >= 3:
68         output = output.decode('utf-8')
69
70     lines = output.splitlines()
71     protos = [Proto(x) for x in lines if x[0] == "P"]
72     fields = [Field(x) for x in lines if x[0] == "F"]
73
74     return protos, fields
75
76
77 def check_fields(fields):
78     """Looks for problems in field definitions."""
79     global errors
80     for field in fields:
81         if field.bitmask != 0:
82             if field.ftype.find("FT_UINT") != 0 and \
83                     field.ftype.find("FT_INT") != 0 and \
84                     field.ftype != "FT_BOOLEAN" and \
85                     field.ftype != "FT_CHAR":
86                 print("%s has a bitmask 0x%x but is type %s" % \
87                         (field.abbrev, field.bitmask, field.ftype))
88                 errors += 1
89
90 def run(tshark):
91     """Run the tests."""
92     global errors
93     protos, fields = gather_data(tshark)
94
95     check_fields(fields)
96
97     if errors > 0:
98         sys.exit("%d errors found" % (errors,))
99     else:
100         print("Success.")
101
102 def main():
103     """Parse the command-line."""
104     usage = "%prog tshark"
105     parser = OptionParser(usage=usage)
106
107     (options, args) = parser.parse_args()
108
109     if len(args) != 1:
110         parser.error("Need location of tshark.")
111
112     run(args[0])
113
114 if __name__ == "__main__":
115     main()