blob: c34073df5fc0d74a91df0f239aee3a767f5c46fe [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum2ba9f301992-03-02 16:20:32 +00002
Guido van Rossum07c96451994-10-03 16:45:35 +00003# 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
7# Python is ignored.
8
9# Without filename arguments, acts as a filter.
Guido van Rossum09336f91994-05-03 14:37:30 +000010# 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".
Guido van Rossum07c96451994-10-03 16:45:35 +000013
Guido van Rossum01f5f621994-05-17 09:05:54 +000014# 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)'".
Guido van Rossum2ba9f301992-03-02 16:20:32 +000017
18# XXX To do:
Guido van Rossum2ba9f301992-03-02 16:20:32 +000019# - turn trailing C comments into Python comments
Guido van Rossum2ba9f301992-03-02 16:20:32 +000020# - turn C Boolean operators "&& || !" into Python "and or not"
21# - what to do about #if(def)?
Guido van Rossum07c96451994-10-03 16:45:35 +000022# - what to do about macros with multiple parameters?
Guido van Rossum2ba9f301992-03-02 16:20:32 +000023
Guido van Rossum07c96451994-10-03 16:45:35 +000024import sys, regex, regsub, string, getopt, os
Guido van Rossum2ba9f301992-03-02 16:20:32 +000025
Guido van Rossum6100d911996-08-22 23:12:23 +000026p_define = regex.compile('^[\t ]*#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
Guido van Rossum2ba9f301992-03-02 16:20:32 +000027
Guido van Rossum07c96451994-10-03 16:45:35 +000028p_macro = regex.compile(
Guido van Rossum6100d911996-08-22 23:12:23 +000029 '^[\t ]*#[\t ]*define[\t ]+'
30 '\([a-zA-Z0-9_]+\)(\([_a-zA-Z][_a-zA-Z0-9]*\))[\t ]+')
Guido van Rossum07c96451994-10-03 16:45:35 +000031
Guido van Rossum6100d911996-08-22 23:12:23 +000032p_include = regex.compile('^[\t ]*#[\t ]*include[\t ]+<\([a-zA-Z0-9_/\.]+\)')
Guido van Rossum07c96451994-10-03 16:45:35 +000033
Guido van Rossum047979e1992-06-05 15:13:53 +000034p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
Guido van Rossum2ba9f301992-03-02 16:20:32 +000035
Guido van Rossum01f5f621994-05-17 09:05:54 +000036ignores = [p_comment]
37
Guido van Rossum07c96451994-10-03 16:45:35 +000038p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
39
40filedict = {}
41
Guido van Rossum514d3511995-01-17 17:01:40 +000042try:
43 searchdirs=string.splitfields(os.environ['include'],';')
44except KeyError:
45 try:
46 searchdirs=string.splitfields(os.environ['INCLUDE'],';')
47 except KeyError:
48 searchdirs=['/usr/include']
49
Guido van Rossum2ba9f301992-03-02 16:20:32 +000050def main():
Guido van Rossum01f5f621994-05-17 09:05:54 +000051 opts, args = getopt.getopt(sys.argv[1:], 'i:')
52 for o, a in opts:
53 if o == '-i':
54 ignores.append(regex.compile(a))
Guido van Rossum09336f91994-05-03 14:37:30 +000055 if not args:
56 args = ['-']
57 for filename in args:
58 if filename == '-':
59 sys.stdout.write('# Generated by h2py from stdin\n')
60 process(sys.stdin, sys.stdout)
61 else:
62 fp = open(filename, 'r')
63 outfile = os.path.basename(filename)
64 i = string.rfind(outfile, '.')
65 if i > 0: outfile = outfile[:i]
66 outfile = string.upper(outfile)
67 outfile = outfile + '.py'
68 outfp = open(outfile, 'w')
69 outfp.write('# Generated by h2py from %s\n' % filename)
Guido van Rossum07c96451994-10-03 16:45:35 +000070 filedict = {}
Guido van Rossum514d3511995-01-17 17:01:40 +000071 for dir in searchdirs:
72 if filename[:len(dir)] == dir:
73 filedict[filename[len(dir)+1:]] = None # no '/' trailing
74 break
Guido van Rossum09336f91994-05-03 14:37:30 +000075 process(fp, outfp)
76 outfp.close()
77 fp.close()
Guido van Rossum2ba9f301992-03-02 16:20:32 +000078
Guido van Rossum07c96451994-10-03 16:45:35 +000079def process(fp, outfp, env = {}):
Guido van Rossum2ba9f301992-03-02 16:20:32 +000080 lineno = 0
81 while 1:
82 line = fp.readline()
83 if not line: break
84 lineno = lineno + 1
Guido van Rossum01f5f621994-05-17 09:05:54 +000085 n = p_define.match(line)
86 if n >= 0:
Guido van Rossum07c96451994-10-03 16:45:35 +000087 # gobble up continuation lines
88 while line[-2:] == '\\\n':
89 nextline = fp.readline()
90 if not nextline: break
91 lineno = lineno + 1
92 line = line + nextline
Guido van Rossum01f5f621994-05-17 09:05:54 +000093 name = p_define.group(1)
94 body = line[n:]
95 # replace ignored patterns by spaces
96 for p in ignores:
Guido van Rossum07c96451994-10-03 16:45:35 +000097 body = regsub.gsub(p, ' ', body)
98 # replace char literals by ord(...)
99 body = regsub.gsub(p_char, 'ord(\\0)', body)
Guido van Rossum09336f91994-05-03 14:37:30 +0000100 stmt = '%s = %s\n' % (name, string.strip(body))
101 ok = 0
102 try:
103 exec stmt in env
Guido van Rossum09336f91994-05-03 14:37:30 +0000104 except:
105 sys.stderr.write('Skipping: %s' % stmt)
Guido van Rossum07c96451994-10-03 16:45:35 +0000106 else:
Guido van Rossum09336f91994-05-03 14:37:30 +0000107 outfp.write(stmt)
Guido van Rossum07c96451994-10-03 16:45:35 +0000108 n =p_macro.match(line)
109 if n >= 0:
110 macro, arg = p_macro.group(1, 2)
111 body = line[n:]
112 for p in ignores:
113 body = regsub.gsub(p, ' ', body)
114 body = regsub.gsub(p_char, 'ord(\\0)', body)
115 stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
116 try:
117 exec stmt in env
118 except:
119 sys.stderr.write('Skipping: %s' % stmt)
120 else:
121 outfp.write(stmt)
122 if p_include.match(line) >= 0:
123 regs = p_include.regs
124 a, b = regs[1]
125 filename = line[a:b]
126 if not filedict.has_key(filename):
127 filedict[filename] = None
Guido van Rossum514d3511995-01-17 17:01:40 +0000128 inclfp = None
129 for dir in searchdirs:
130 try:
131 inclfp = open(dir + '/' + filename, 'r')
132 break
133 except IOError:
134 pass
135 if inclfp:
136 outfp.write(
137 '\n# Included from %s\n' % filename)
138 process(inclfp, outfp, env)
139 else:
140 sys.stderr.write('Warning - could not find file %s' % filename)
Guido van Rossum07c96451994-10-03 16:45:35 +0000141
Guido van Rossum514d3511995-01-17 17:01:40 +0000142main()