1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | # Read #define's and translate to Python code. |
---|
4 | n/a | # Handle #include statements. |
---|
5 | n/a | # Handle #define macros with one argument. |
---|
6 | n/a | # Anything that isn't recognized or doesn't translate into valid |
---|
7 | n/a | # Python is ignored. |
---|
8 | n/a | |
---|
9 | n/a | # Without filename arguments, acts as a filter. |
---|
10 | n/a | # If one or more filenames are given, output is written to corresponding |
---|
11 | n/a | # filenames in the local directory, translated to all uppercase, with |
---|
12 | n/a | # the extension replaced by ".py". |
---|
13 | n/a | |
---|
14 | n/a | # By passing one or more options of the form "-i regular_expression" |
---|
15 | n/a | # you can specify additional strings to be ignored. This is useful |
---|
16 | n/a | # e.g. to ignore casts to u_long: simply specify "-i '(u_long)'". |
---|
17 | n/a | |
---|
18 | n/a | # XXX To do: |
---|
19 | n/a | # - turn trailing C comments into Python comments |
---|
20 | n/a | # - turn C Boolean operators "&& || !" into Python "and or not" |
---|
21 | n/a | # - what to do about #if(def)? |
---|
22 | n/a | # - what to do about macros with multiple parameters? |
---|
23 | n/a | |
---|
24 | n/a | import sys, re, getopt, os |
---|
25 | n/a | |
---|
26 | n/a | p_define = re.compile(r'^[\t ]*#[\t ]*define[\t ]+([a-zA-Z0-9_]+)[\t ]+') |
---|
27 | n/a | |
---|
28 | n/a | p_macro = re.compile( |
---|
29 | n/a | r'^[\t ]*#[\t ]*define[\t ]+' |
---|
30 | n/a | r'([a-zA-Z0-9_]+)\(([_a-zA-Z][_a-zA-Z0-9]*)\)[\t ]+') |
---|
31 | n/a | |
---|
32 | n/a | p_include = re.compile(r'^[\t ]*#[\t ]*include[\t ]+<([^>\n]+)>') |
---|
33 | n/a | |
---|
34 | n/a | p_comment = re.compile(r'/\*([^*]+|\*+[^/])*(\*+/)?') |
---|
35 | n/a | p_cpp_comment = re.compile('//.*') |
---|
36 | n/a | |
---|
37 | n/a | ignores = [p_comment, p_cpp_comment] |
---|
38 | n/a | |
---|
39 | n/a | p_char = re.compile(r"'(\\.[^\\]*|[^\\])'") |
---|
40 | n/a | |
---|
41 | n/a | p_hex = re.compile(r"0x([0-9a-fA-F]+)L?") |
---|
42 | n/a | |
---|
43 | n/a | filedict = {} |
---|
44 | n/a | importable = {} |
---|
45 | n/a | |
---|
46 | n/a | try: |
---|
47 | n/a | searchdirs=os.environ['include'].split(';') |
---|
48 | n/a | except KeyError: |
---|
49 | n/a | try: |
---|
50 | n/a | searchdirs=os.environ['INCLUDE'].split(';') |
---|
51 | n/a | except KeyError: |
---|
52 | n/a | searchdirs=['/usr/include'] |
---|
53 | n/a | try: |
---|
54 | n/a | searchdirs.insert(0, os.path.join('/usr/include', |
---|
55 | n/a | os.environ['MULTIARCH'])) |
---|
56 | n/a | except KeyError: |
---|
57 | n/a | pass |
---|
58 | n/a | |
---|
59 | n/a | def main(): |
---|
60 | n/a | global filedict |
---|
61 | n/a | opts, args = getopt.getopt(sys.argv[1:], 'i:') |
---|
62 | n/a | for o, a in opts: |
---|
63 | n/a | if o == '-i': |
---|
64 | n/a | ignores.append(re.compile(a)) |
---|
65 | n/a | if not args: |
---|
66 | n/a | args = ['-'] |
---|
67 | n/a | for filename in args: |
---|
68 | n/a | if filename == '-': |
---|
69 | n/a | sys.stdout.write('# Generated by h2py from stdin\n') |
---|
70 | n/a | process(sys.stdin, sys.stdout) |
---|
71 | n/a | else: |
---|
72 | n/a | fp = open(filename, 'r') |
---|
73 | n/a | outfile = os.path.basename(filename) |
---|
74 | n/a | i = outfile.rfind('.') |
---|
75 | n/a | if i > 0: outfile = outfile[:i] |
---|
76 | n/a | modname = outfile.upper() |
---|
77 | n/a | outfile = modname + '.py' |
---|
78 | n/a | outfp = open(outfile, 'w') |
---|
79 | n/a | outfp.write('# Generated by h2py from %s\n' % filename) |
---|
80 | n/a | filedict = {} |
---|
81 | n/a | for dir in searchdirs: |
---|
82 | n/a | if filename[:len(dir)] == dir: |
---|
83 | n/a | filedict[filename[len(dir)+1:]] = None # no '/' trailing |
---|
84 | n/a | importable[filename[len(dir)+1:]] = modname |
---|
85 | n/a | break |
---|
86 | n/a | process(fp, outfp) |
---|
87 | n/a | outfp.close() |
---|
88 | n/a | fp.close() |
---|
89 | n/a | |
---|
90 | n/a | def pytify(body): |
---|
91 | n/a | # replace ignored patterns by spaces |
---|
92 | n/a | for p in ignores: |
---|
93 | n/a | body = p.sub(' ', body) |
---|
94 | n/a | # replace char literals by ord(...) |
---|
95 | n/a | body = p_char.sub("ord('\\1')", body) |
---|
96 | n/a | # Compute negative hexadecimal constants |
---|
97 | n/a | start = 0 |
---|
98 | n/a | UMAX = 2*(sys.maxsize+1) |
---|
99 | n/a | while 1: |
---|
100 | n/a | m = p_hex.search(body, start) |
---|
101 | n/a | if not m: break |
---|
102 | n/a | s,e = m.span() |
---|
103 | n/a | val = int(body[slice(*m.span(1))], 16) |
---|
104 | n/a | if val > sys.maxsize: |
---|
105 | n/a | val -= UMAX |
---|
106 | n/a | body = body[:s] + "(" + str(val) + ")" + body[e:] |
---|
107 | n/a | start = s + 1 |
---|
108 | n/a | return body |
---|
109 | n/a | |
---|
110 | n/a | def process(fp, outfp, env = {}): |
---|
111 | n/a | lineno = 0 |
---|
112 | n/a | while 1: |
---|
113 | n/a | line = fp.readline() |
---|
114 | n/a | if not line: break |
---|
115 | n/a | lineno = lineno + 1 |
---|
116 | n/a | match = p_define.match(line) |
---|
117 | n/a | if match: |
---|
118 | n/a | # gobble up continuation lines |
---|
119 | n/a | while line[-2:] == '\\\n': |
---|
120 | n/a | nextline = fp.readline() |
---|
121 | n/a | if not nextline: break |
---|
122 | n/a | lineno = lineno + 1 |
---|
123 | n/a | line = line + nextline |
---|
124 | n/a | name = match.group(1) |
---|
125 | n/a | body = line[match.end():] |
---|
126 | n/a | body = pytify(body) |
---|
127 | n/a | ok = 0 |
---|
128 | n/a | stmt = '%s = %s\n' % (name, body.strip()) |
---|
129 | n/a | try: |
---|
130 | n/a | exec(stmt, env) |
---|
131 | n/a | except: |
---|
132 | n/a | sys.stderr.write('Skipping: %s' % stmt) |
---|
133 | n/a | else: |
---|
134 | n/a | outfp.write(stmt) |
---|
135 | n/a | match = p_macro.match(line) |
---|
136 | n/a | if match: |
---|
137 | n/a | macro, arg = match.group(1, 2) |
---|
138 | n/a | body = line[match.end():] |
---|
139 | n/a | body = pytify(body) |
---|
140 | n/a | stmt = 'def %s(%s): return %s\n' % (macro, arg, body) |
---|
141 | n/a | try: |
---|
142 | n/a | exec(stmt, env) |
---|
143 | n/a | except: |
---|
144 | n/a | sys.stderr.write('Skipping: %s' % stmt) |
---|
145 | n/a | else: |
---|
146 | n/a | outfp.write(stmt) |
---|
147 | n/a | match = p_include.match(line) |
---|
148 | n/a | if match: |
---|
149 | n/a | regs = match.regs |
---|
150 | n/a | a, b = regs[1] |
---|
151 | n/a | filename = line[a:b] |
---|
152 | n/a | if filename in importable: |
---|
153 | n/a | outfp.write('from %s import *\n' % importable[filename]) |
---|
154 | n/a | elif filename not in filedict: |
---|
155 | n/a | filedict[filename] = None |
---|
156 | n/a | inclfp = None |
---|
157 | n/a | for dir in searchdirs: |
---|
158 | n/a | try: |
---|
159 | n/a | inclfp = open(dir + '/' + filename) |
---|
160 | n/a | break |
---|
161 | n/a | except IOError: |
---|
162 | n/a | pass |
---|
163 | n/a | if inclfp: |
---|
164 | n/a | outfp.write( |
---|
165 | n/a | '\n# Included from %s\n' % filename) |
---|
166 | n/a | process(inclfp, outfp, env) |
---|
167 | n/a | else: |
---|
168 | n/a | sys.stderr.write('Warning - could not find file %s\n' % |
---|
169 | n/a | filename) |
---|
170 | n/a | |
---|
171 | n/a | if __name__ == '__main__': |
---|
172 | n/a | main() |
---|