blob: 4cea2353cb7595dbb95886b0cbc023bf7f5f448b [file] [log] [blame]
Guido van Rossum41ffccb1993-04-01 20:50:35 +00001#! /usr/local/bin/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
26p_define = regex.compile('^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
27
Guido van Rossum07c96451994-10-03 16:45:35 +000028p_macro = regex.compile(
29 '^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)(\([_a-zA-Z][_a-zA-Z0-9]*\))[\t ]+')
30
31p_include = regex.compile('^#[\t ]*include[\t ]+<\([a-zA-Z0-9_/\.]+\)')
32
Guido van Rossum047979e1992-06-05 15:13:53 +000033p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
Guido van Rossum2ba9f301992-03-02 16:20:32 +000034
Guido van Rossum01f5f621994-05-17 09:05:54 +000035ignores = [p_comment]
36
Guido van Rossum07c96451994-10-03 16:45:35 +000037p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
38
39filedict = {}
40
Guido van Rossum514d3511995-01-17 17:01:40 +000041try:
42 searchdirs=string.splitfields(os.environ['include'],';')
43except KeyError:
44 try:
45 searchdirs=string.splitfields(os.environ['INCLUDE'],';')
46 except KeyError:
47 searchdirs=['/usr/include']
48
Guido van Rossum2ba9f301992-03-02 16:20:32 +000049def main():
Guido van Rossum01f5f621994-05-17 09:05:54 +000050 opts, args = getopt.getopt(sys.argv[1:], 'i:')
51 for o, a in opts:
52 if o == '-i':
53 ignores.append(regex.compile(a))
Guido van Rossum09336f91994-05-03 14:37:30 +000054 if not args:
55 args = ['-']
56 for filename in args:
57 if filename == '-':
58 sys.stdout.write('# Generated by h2py from stdin\n')
59 process(sys.stdin, sys.stdout)
60 else:
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)
Guido van Rossum07c96451994-10-03 16:45:35 +000069 filedict = {}
Guido van Rossum514d3511995-01-17 17:01:40 +000070 for dir in searchdirs:
71 if filename[:len(dir)] == dir:
72 filedict[filename[len(dir)+1:]] = None # no '/' trailing
73 break
Guido van Rossum09336f91994-05-03 14:37:30 +000074 process(fp, outfp)
75 outfp.close()
76 fp.close()
Guido van Rossum2ba9f301992-03-02 16:20:32 +000077
Guido van Rossum07c96451994-10-03 16:45:35 +000078def process(fp, outfp, env = {}):
Guido van Rossum2ba9f301992-03-02 16:20:32 +000079 lineno = 0
80 while 1:
81 line = fp.readline()
82 if not line: break
83 lineno = lineno + 1
Guido van Rossum01f5f621994-05-17 09:05:54 +000084 n = p_define.match(line)
85 if n >= 0:
Guido van Rossum07c96451994-10-03 16:45:35 +000086 # gobble up continuation lines
87 while line[-2:] == '\\\n':
88 nextline = fp.readline()
89 if not nextline: break
90 lineno = lineno + 1
91 line = line + nextline
Guido van Rossum01f5f621994-05-17 09:05:54 +000092 name = p_define.group(1)
93 body = line[n:]
94 # replace ignored patterns by spaces
95 for p in ignores:
Guido van Rossum07c96451994-10-03 16:45:35 +000096 body = regsub.gsub(p, ' ', body)
97 # replace char literals by ord(...)
98 body = regsub.gsub(p_char, 'ord(\\0)', body)
Guido van Rossum09336f91994-05-03 14:37:30 +000099 stmt = '%s = %s\n' % (name, string.strip(body))
100 ok = 0
101 try:
102 exec stmt in env
Guido van Rossum09336f91994-05-03 14:37:30 +0000103 except:
104 sys.stderr.write('Skipping: %s' % stmt)
Guido van Rossum07c96451994-10-03 16:45:35 +0000105 else:
Guido van Rossum09336f91994-05-03 14:37:30 +0000106 outfp.write(stmt)
Guido van Rossum07c96451994-10-03 16:45:35 +0000107 n =p_macro.match(line)
108 if n >= 0:
109 macro, arg = p_macro.group(1, 2)
110 body = line[n:]
111 for p in ignores:
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)
115 try:
116 exec stmt in env
117 except:
118 sys.stderr.write('Skipping: %s' % stmt)
119 else:
120 outfp.write(stmt)
121 if p_include.match(line) >= 0:
122 regs = p_include.regs
123 a, b = regs[1]
124 filename = line[a:b]
125 if not filedict.has_key(filename):
126 filedict[filename] = None
Guido van Rossum514d3511995-01-17 17:01:40 +0000127 inclfp = None
128 for dir in searchdirs:
129 try:
130 inclfp = open(dir + '/' + filename, 'r')
131 break
132 except IOError:
133 pass
134 if inclfp:
135 outfp.write(
136 '\n# Included from %s\n' % filename)
137 process(inclfp, outfp, env)
138 else:
139 sys.stderr.write('Warning - could not find file %s' % filename)
Guido van Rossum07c96451994-10-03 16:45:35 +0000140
Guido van Rossum514d3511995-01-17 17:01:40 +0000141main()