blob: 0b260c1f2cdf13ced68c061cf0878c8342434293 [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
3# Read #define's from stdin and translate to Python code on stdout.
Guido van Rossum09336f91994-05-03 14:37:30 +00004# Very primitive: non-#define's are ignored.
5# You will have to edit the output in some cases.
6# If one or more filenames are given, output is written to corresponding
7# filenames in the local directory, translated to all uppercase, with
8# the extension replaced by ".py".
Guido van Rossum2ba9f301992-03-02 16:20:32 +00009
10# XXX To do:
Guido van Rossum2ba9f301992-03-02 16:20:32 +000011# - turn trailing C comments into Python comments
12# - turn C string quotes into Python comments
13# - turn C Boolean operators "&& || !" into Python "and or not"
14# - what to do about #if(def)?
15# - what to do about macros with parameters?
16# - reject definitions with semicolons in them
17
Guido van Rossum09336f91994-05-03 14:37:30 +000018import sys, regex, string, getopt, os
Guido van Rossum2ba9f301992-03-02 16:20:32 +000019
20p_define = regex.compile('^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
21
Guido van Rossum047979e1992-06-05 15:13:53 +000022p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
Guido van Rossum2ba9f301992-03-02 16:20:32 +000023
24def main():
Guido van Rossum09336f91994-05-03 14:37:30 +000025 opts, args = getopt.getopt(sys.argv[1:], '')
26 if not args:
27 args = ['-']
28 for filename in args:
29 if filename == '-':
30 sys.stdout.write('# Generated by h2py from stdin\n')
31 process(sys.stdin, sys.stdout)
32 else:
33 fp = open(filename, 'r')
34 outfile = os.path.basename(filename)
35 i = string.rfind(outfile, '.')
36 if i > 0: outfile = outfile[:i]
37 outfile = string.upper(outfile)
38 outfile = outfile + '.py'
39 outfp = open(outfile, 'w')
40 outfp.write('# Generated by h2py from %s\n' % filename)
41 process(fp, outfp)
42 outfp.close()
43 fp.close()
Guido van Rossum2ba9f301992-03-02 16:20:32 +000044
Guido van Rossum09336f91994-05-03 14:37:30 +000045def process(fp, outfp):
46 env = {}
Guido van Rossum2ba9f301992-03-02 16:20:32 +000047 lineno = 0
48 while 1:
49 line = fp.readline()
50 if not line: break
51 lineno = lineno + 1
52 if p_define.match(line) >= 0:
53 # gobble up continuation lines
54 while line[-2:] == '\\\n':
55 nextline = fp.readline()
56 if not nextline: break
57 lineno = lineno + 1
58 line = line + nextline
59 regs = p_define.regs
60 a, b = regs[1] # where the macro name is
61 name = line[a:b]
62 a, b = regs[0] # the whole match
63 body = line[b:]
64 # replace comments by spaces
65 while p_comment.search(body) >= 0:
66 a, b = p_comment.regs[0]
67 body = body[:a] + ' ' + body[b:]
Guido van Rossum09336f91994-05-03 14:37:30 +000068 stmt = '%s = %s\n' % (name, string.strip(body))
69 ok = 0
70 try:
71 exec stmt in env
72 ok = 1
73 except:
74 sys.stderr.write('Skipping: %s' % stmt)
75 if ok:
76 outfp.write(stmt)
Guido van Rossum2ba9f301992-03-02 16:20:32 +000077
78main()