3 # Read #define's and translate to Python code.
4 # Handle #include statements.
5 # Handle #define macros with one argument.
6 # Anything that isn't recognized or doesn't translate into valid
9 # Without filename arguments, acts as a filter.
10 # If one or more filenames are given, output is written to corresponding
11 # filenames in the local directory, translated to all uppercase, with
12 # the extension replaced by ".py".
14 # By passing one or more options of the form "-i regular_expression"
15 # you can specify additional strings to be ignored. This is useful
16 # e.g. to ignore casts to u_long: simply specify "-i '(u_long)'".
19 # - turn trailing C comments into Python comments
20 # - turn C Boolean operators "&& || !" into Python "and or not"
21 # - what to do about #if(def)?
22 # - what to do about macros with multiple parameters?
24 import sys
, re
, getopt
, os
26 p_define
= re
.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+')
29 '^[\t ]*#[\t ]*define[\t ]+'
30 '([a-zA-Z0-9_]+)\(([_a-zA-Z][_a-zA-Z0-9]*)\)[\t ]+')
32 p_include
= re
.compile('^[\t ]*#[\t ]*include[\t ]+<([a-zA-Z0-9_/\.]+)')
34 p_comment
= re
.compile(r
'/\*([^*]+|\*+[^/])*(\*+/)?')
35 p_cpp_comment
= re
.compile('//.*')
37 ignores
= [p_comment
, p_cpp_comment
]
39 p_char
= re
.compile(r
"'(\\.[^\\]*|[^\\])'")
45 searchdirs
=os
.environ
['include'].split(';')
48 searchdirs
=os
.environ
['INCLUDE'].split(';')
51 if sys
.platform
.find("beos") == 0:
52 searchdirs
=os
.environ
['BEINCLUDES'].split(';')
56 searchdirs
=['/usr/include']
60 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'i:')
63 ignores
.append(re
.compile(a
))
68 sys
.stdout
.write('# Generated by h2py from stdin\n')
69 process(sys
.stdin
, sys
.stdout
)
71 fp
= open(filename
, 'r')
72 outfile
= os
.path
.basename(filename
)
73 i
= outfile
.rfind('.')
74 if i
> 0: outfile
= outfile
[:i
]
75 modname
= outfile
.upper()
76 outfile
= modname
+ '.py'
77 outfp
= open(outfile
, 'w')
78 outfp
.write('# Generated by h2py from %s\n' % filename
)
80 for dir in searchdirs
:
81 if filename
[:len(dir)] == dir:
82 filedict
[filename
[len(dir)+1:]] = None # no '/' trailing
83 importable
[filename
[len(dir)+1:]] = modname
89 def process(fp
, outfp
, env
= {}):
95 match
= p_define
.match(line
)
97 # gobble up continuation lines
98 while line
[-2:] == '\\\n':
99 nextline
= fp
.readline()
100 if not nextline
: break
102 line
= line
+ nextline
103 name
= match
.group(1)
104 body
= line
[match
.end():]
105 # replace ignored patterns by spaces
107 body
= p
.sub(' ', body
)
108 # replace char literals by ord(...)
109 body
= p_char
.sub('ord(\\0)', body
)
110 stmt
= '%s = %s\n' % (name
, body
.strip())
115 sys
.stderr
.write('Skipping: %s' % stmt
)
118 match
= p_macro
.match(line
)
120 macro
, arg
= match
.group(1, 2)
121 body
= line
[match
.end():]
123 body
= p
.sub(' ', body
)
124 body
= p_char
.sub('ord(\\0)', body
)
125 stmt
= 'def %s(%s): return %s\n' % (macro
, arg
, body
)
129 sys
.stderr
.write('Skipping: %s' % stmt
)
132 match
= p_include
.match(line
)
137 if importable
.has_key(filename
):
138 outfp
.write('from %s import *\n' % importable
[filename
])
139 elif not filedict
.has_key(filename
):
140 filedict
[filename
] = None
142 for dir in searchdirs
:
144 inclfp
= open(dir + '/' + filename
)
150 '\n# Included from %s\n' % filename
)
151 process(inclfp
, outfp
, env
)
153 sys
.stderr
.write('Warning - could not find file %s\n' %