Git init
[external/libsndfile.git] / src / binheader_writef_check.py
1 #!/usr/bin/python2.5
2
3 # Copyright (C) 2006 Erik de Castro Lopo <erikd@mega-nerd.com>
4 #
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 #
11 #     * Redistributions of source code must retain the above copyright
12 #       notice, this list of conditions and the following disclaimer.
13 #     * Redistributions in binary form must reproduce the above copyright
14 #       notice, this list of conditions and the following disclaimer in
15 #       the documentation and/or other materials provided with the
16 #       distribution.
17 #     * Neither the author nor the names of any contributors may be used
18 #       to endorse or promote products derived from this software without
19 #       specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33
34 # This parses C code using regexes (yes, thats horrible) and makes sure
35 # that calling conventions to the function psf_binheader_writef are
36 # correct.
37
38
39
40 import re, string, sys
41
42 _whitespace_re = re.compile ("\s+", re.MULTILINE)
43
44 def find_binheader_writefs (data):
45         lst = re.findall ('psf_binheader_writef\s*\(\s*[a-zA-Z_]+\s*,\s*\"[^;]+;', data, re.MULTILINE)
46         return [_whitespace_re.sub (" ", x) for x in lst]
47
48 def find_format_string (s):
49         fmt = re.search ('"([^"]+)"', s)
50         if not fmt:
51                 print "Bad format in :\n\n\t%s\n\n" % s
52                 sys.exit (1)
53         fmt = fmt.groups ()
54         if len (fmt) != 1:
55                 print "Bad format in :\n\n\t%s\n\n" % s
56                 sys.exit (1)
57         return _whitespace_re.sub ("", fmt [0])
58
59 def get_param_list (data):
60         dlist = re.search ("\((.+)\)\s*;", data)
61         dlist = dlist.groups ()[0]
62         dlist = string.split (dlist, ",")
63         dlist = [string.strip (x) for x in dlist]
64         return dlist [2:]
65
66 def handle_file (fname):
67         errors = 0
68         data = open (fname, "r").read ()
69
70         writefs = find_binheader_writefs (data)
71         for item in writefs:
72                 fmt = find_format_string (item)
73                 params = get_param_list (item)
74                 param_index = 0
75
76                 # print item
77
78                 for ch in fmt:
79                         if ch in 'Eet ':
80                                 continue
81
82                         # print "    param [%d] %c : %s" % (param_index, ch, params [param_index])
83
84                         if ch != 'b':
85                                 param_index += 1
86                                 continue
87
88                         # print item
89                         # print "    param [%d] %c : %s <-> %s" % (param_index, ch, params [param_index], params [param_index + 1])
90
91                         if string.find (params [param_index + 1], "sizeof") < 0 \
92                                                 and string.find (params [param_index + 1], "make_size_t") < 0 \
93                                                 and string.find (params [param_index + 1], "strlen") < 0:
94                                 if errors == 0: print
95                                 print "\n%s :" % fname
96                                 print "    param [%d] %c : %s <-> %s" % (param_index, ch, params [param_index], params [param_index + 1])
97                                 print "    %s" % item
98                                 errors += 1
99                         param_index += 2
100                         
101         return errors
102
103 #===============================================================================
104
105 if len (sys.argv) > 1:
106         print "\n    binheader_writef_check                   :",
107         sys.stdout.flush ()
108         errors = 0
109         for fname in sys.argv [1:]:
110                 errors += handle_file (fname)
111         if errors > 0:
112                 print "\nErrors : %d\n" % errors
113                 sys.exit (1)
114
115 print "ok\n"
116