1 #! /usr/local/bin/python
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
, regex
, regsub
, string
, getopt
, os
26 p_define
= regex
.compile('^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
28 p_macro
= regex
.compile(
29 '^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)(\([_a-zA-Z][_a-zA-Z0-9]*\))[\t ]+')
31 p_include
= regex
.compile('^#[\t ]*include[\t ]+<\([a-zA-Z0-9_/\.]+\)')
33 p_comment
= regex
.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
37 p_char
= regex
.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
42 searchdirs
=string
.splitfields(os
.environ
['include'],';')
45 searchdirs
=string
.splitfields(os
.environ
['INCLUDE'],';')
47 searchdirs
=['/usr/include']
50 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'i:')
53 ignores
.append(regex
.compile(a
))
58 sys
.stdout
.write('# Generated by h2py from stdin\n')
59 process(sys
.stdin
, sys
.stdout
)
61 fp
= open(filename
, 'r')
62 outfile
= os
.path
.basename(filename
)
63 i
= string
.rfind(outfile
, '.')
64 if i
> 0: outfile
= outfile
[:i
]
65 outfile
= string
.upper(outfile
)
66 outfile
= outfile
+ '.py'
67 outfp
= open(outfile
, 'w')
68 outfp
.write('# Generated by h2py from %s\n' % filename
)
70 for dir in searchdirs
:
71 if filename
[:len(dir)] == dir:
72 filedict
[filename
[len(dir)+1:]] = None # no '/' trailing
78 def process(fp
, outfp
, env
= {}):
84 n
= p_define
.match(line
)
86 # gobble up continuation lines
87 while line
[-2:] == '\\\n':
88 nextline
= fp
.readline()
89 if not nextline
: break
91 line
= line
+ nextline
92 name
= p_define
.group(1)
94 # replace ignored patterns by spaces
96 body
= regsub
.gsub(p
, ' ', body
)
97 # replace char literals by ord(...)
98 body
= regsub
.gsub(p_char
, 'ord(\\0)', body
)
99 stmt
= '%s = %s\n' % (name
, string
.strip(body
))
104 sys
.stderr
.write('Skipping: %s' % stmt
)
107 n
=p_macro
.match(line
)
109 macro
, arg
= p_macro
.group(1, 2)
112 body
= regsub
.gsub(p
, ' ', body
)
113 body
= regsub
.gsub(p_char
, 'ord(\\0)', body
)
114 stmt
= 'def %s(%s): return %s\n' % (macro
, arg
, body
)
118 sys
.stderr
.write('Skipping: %s' % stmt
)
121 if p_include
.match(line
) >= 0:
122 regs
= p_include
.regs
125 if not filedict
.has_key(filename
):
126 filedict
[filename
] = None
128 for dir in searchdirs
:
130 inclfp
= open(dir + '/' + filename
, 'r')
136 '\n# Included from %s\n' % filename
)
137 process(inclfp
, outfp
, env
)
139 sys
.stderr
.write('Warning - could not find file %s' % filename
)