3 # Copyright (C) 2006 Erik de Castro Lopo <erikd@mega-nerd.com>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
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
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.
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.
34 # This parses C code using regexes (yes, thats horrible) and makes sure
35 # that calling conventions to the function psf_binheader_writef are
40 import re, string, sys
42 _whitespace_re = re.compile ("\s+", re.MULTILINE)
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]
48 def find_format_string (s):
49 fmt = re.search ('"([^"]+)"', s)
51 print "Bad format in :\n\n\t%s\n\n" % s
55 print "Bad format in :\n\n\t%s\n\n" % s
57 return _whitespace_re.sub ("", fmt [0])
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]
66 def handle_file (fname):
68 data = open (fname, "r").read ()
70 writefs = find_binheader_writefs (data)
72 fmt = find_format_string (item)
73 params = get_param_list (item)
82 # print " param [%d] %c : %s" % (param_index, ch, params [param_index])
89 # print " param [%d] %c : %s <-> %s" % (param_index, ch, params [param_index], params [param_index + 1])
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:
95 print "\n%s :" % fname
96 print " param [%d] %c : %s <-> %s" % (param_index, ch, params [param_index], params [param_index + 1])
103 #===============================================================================
105 if len (sys.argv) > 1:
106 print "\n binheader_writef_check :",
109 for fname in sys.argv [1:]:
110 errors += handle_file (fname)
112 print "\nErrors : %d\n" % errors