blob: 393a4adc09840b64b8dfef91bd4cbe98fd8048a3 [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
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000024import sys, re, getopt, os
Guido van Rossum2ba9f301992-03-02 16:20:32 +000025
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000026p_define = re.compile('^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+')
Guido van Rossum2ba9f301992-03-02 16:20:32 +000027
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000028p_macro = re.compile(
Guido van Rossum6100d911996-08-22 23:12:23 +000029 '^[\t ]*#[\t ]*define[\t ]+'
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000030 '([a-zA-Z0-9_]+)\(([_a-zA-Z][_a-zA-Z0-9]*)\)[\t ]+')
Guido van Rossum07c96451994-10-03 16:45:35 +000031
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000032p_include = re.compile('^[\t ]*#[\t ]*include[\t ]+<([a-zA-Z0-9_/\.]+)')
Guido van Rossum07c96451994-10-03 16:45:35 +000033
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000034p_comment = re.compile(r'/\*([^*]+|\*+[^/])*(\*+/)?')
35p_cpp_comment = re.compile('//.*')
Guido van Rossum2ba9f301992-03-02 16:20:32 +000036
Guido van Rossum9189bda1997-08-14 20:14:29 +000037ignores = [p_comment, p_cpp_comment]
Guido van Rossum01f5f621994-05-17 09:05:54 +000038
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000039p_char = re.compile(r"'(\\.[^\\]*|[^\\])'")
Guido van Rossum07c96451994-10-03 16:45:35 +000040
41filedict = {}
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000042importable = {}
Guido van Rossum07c96451994-10-03 16:45:35 +000043
Guido van Rossum514d3511995-01-17 17:01:40 +000044try:
Guido van Rossume51c3f52001-12-06 03:24:30 +000045 searchdirs=os.environ['include'].split(';')
Guido van Rossum514d3511995-01-17 17:01:40 +000046except KeyError:
Tim Peters70c43782001-01-17 08:48:39 +000047 try:
Guido van Rossume51c3f52001-12-06 03:24:30 +000048 searchdirs=os.environ['INCLUDE'].split(';')
Tim Peters70c43782001-01-17 08:48:39 +000049 except KeyError:
50 try:
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000051 if sys.platform.find("beos") == 0:
Guido van Rossume51c3f52001-12-06 03:24:30 +000052 searchdirs=os.environ['BEINCLUDES'].split(';')
Martin v. Löwisf90ae202002-06-11 06:22:31 +000053 elif sys.platform.startswith("atheos"):
54 searchdirs=os.environ['C_INCLUDE_PATH'].split(':')
Tim Peters70c43782001-01-17 08:48:39 +000055 else:
56 raise KeyError
57 except KeyError:
58 searchdirs=['/usr/include']
Guido van Rossum514d3511995-01-17 17:01:40 +000059
Guido van Rossum2ba9f301992-03-02 16:20:32 +000060def main():
Tim Peters70c43782001-01-17 08:48:39 +000061 global filedict
62 opts, args = getopt.getopt(sys.argv[1:], 'i:')
63 for o, a in opts:
64 if o == '-i':
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000065 ignores.append(re.compile(a))
Tim Peters70c43782001-01-17 08:48:39 +000066 if not args:
67 args = ['-']
68 for filename in args:
69 if filename == '-':
70 sys.stdout.write('# Generated by h2py from stdin\n')
71 process(sys.stdin, sys.stdout)
72 else:
73 fp = open(filename, 'r')
74 outfile = os.path.basename(filename)
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000075 i = outfile.rfind('.')
Tim Peters70c43782001-01-17 08:48:39 +000076 if i > 0: outfile = outfile[:i]
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000077 modname = outfile.upper()
78 outfile = modname + '.py'
Tim Peters70c43782001-01-17 08:48:39 +000079 outfp = open(outfile, 'w')
80 outfp.write('# Generated by h2py from %s\n' % filename)
81 filedict = {}
82 for dir in searchdirs:
83 if filename[:len(dir)] == dir:
84 filedict[filename[len(dir)+1:]] = None # no '/' trailing
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000085 importable[filename[len(dir)+1:]] = modname
Tim Peters70c43782001-01-17 08:48:39 +000086 break
87 process(fp, outfp)
88 outfp.close()
89 fp.close()
Guido van Rossum2ba9f301992-03-02 16:20:32 +000090
Guido van Rossum07c96451994-10-03 16:45:35 +000091def process(fp, outfp, env = {}):
Tim Peters70c43782001-01-17 08:48:39 +000092 lineno = 0
93 while 1:
94 line = fp.readline()
95 if not line: break
96 lineno = lineno + 1
Martin v. Löwis4f85bf32001-08-09 12:24:38 +000097 match = p_define.match(line)
98 if match:
Tim Peters70c43782001-01-17 08:48:39 +000099 # gobble up continuation lines
100 while line[-2:] == '\\\n':
101 nextline = fp.readline()
102 if not nextline: break
103 lineno = lineno + 1
104 line = line + nextline
Martin v. Löwis4f85bf32001-08-09 12:24:38 +0000105 name = match.group(1)
106 body = line[match.end():]
Tim Peters70c43782001-01-17 08:48:39 +0000107 # replace ignored patterns by spaces
108 for p in ignores:
Martin v. Löwis4f85bf32001-08-09 12:24:38 +0000109 body = p.sub(' ', body)
Tim Peters70c43782001-01-17 08:48:39 +0000110 # replace char literals by ord(...)
Martin v. Löwis4f85bf32001-08-09 12:24:38 +0000111 body = p_char.sub('ord(\\0)', body)
112 stmt = '%s = %s\n' % (name, body.strip())
Tim Peters70c43782001-01-17 08:48:39 +0000113 ok = 0
114 try:
115 exec stmt in env
116 except:
117 sys.stderr.write('Skipping: %s' % stmt)
118 else:
119 outfp.write(stmt)
Martin v. Löwis4f85bf32001-08-09 12:24:38 +0000120 match = p_macro.match(line)
121 if match:
122 macro, arg = match.group(1, 2)
123 body = line[match.end():]
Tim Peters70c43782001-01-17 08:48:39 +0000124 for p in ignores:
Martin v. Löwis4f85bf32001-08-09 12:24:38 +0000125 body = p.sub(' ', body)
126 body = p_char.sub('ord(\\0)', body)
Tim Peters70c43782001-01-17 08:48:39 +0000127 stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
128 try:
129 exec stmt in env
130 except:
131 sys.stderr.write('Skipping: %s' % stmt)
132 else:
133 outfp.write(stmt)
Martin v. Löwis4f85bf32001-08-09 12:24:38 +0000134 match = p_include.match(line)
135 if match:
136 regs = match.regs
Tim Peters70c43782001-01-17 08:48:39 +0000137 a, b = regs[1]
138 filename = line[a:b]
Martin v. Löwis4f85bf32001-08-09 12:24:38 +0000139 if importable.has_key(filename):
Martin v. Löwisf2f8c512001-08-09 12:32:10 +0000140 outfp.write('from %s import *\n' % importable[filename])
Martin v. Löwis4f85bf32001-08-09 12:24:38 +0000141 elif not filedict.has_key(filename):
Tim Peters70c43782001-01-17 08:48:39 +0000142 filedict[filename] = None
143 inclfp = None
144 for dir in searchdirs:
145 try:
Martin v. Löwis4f85bf32001-08-09 12:24:38 +0000146 inclfp = open(dir + '/' + filename)
Tim Peters70c43782001-01-17 08:48:39 +0000147 break
148 except IOError:
149 pass
150 if inclfp:
151 outfp.write(
152 '\n# Included from %s\n' % filename)
153 process(inclfp, outfp, env)
154 else:
Guido van Rossum436fd752001-12-06 03:28:17 +0000155 sys.stderr.write('Warning - could not find file %s\n' %
156 filename)
Guido van Rossum07c96451994-10-03 16:45:35 +0000157
Guido van Rossum514d3511995-01-17 17:01:40 +0000158main()