annotate Tools/scripts/h2py.py @ 8831:00a6bb65ff29 legacy-trunk

Getting rid of this ancient stuff.
author Guido van Rossum <guido@python.org>
date Wed, 05 Aug 1998 21:10:09 +0000
parents 9a57ca229681
children 2cd58a61b051
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 4112
diff changeset
1 #! /usr/bin/env python
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
3 # Read #define's and translate to Python code.
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
4 # Handle #include statements.
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
5 # Handle #define macros with one argument.
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
6 # Anything that isn't recognized or doesn't translate into valid
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
7 # Python is ignored.
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
8
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
9 # Without filename arguments, acts as a filter.
1602
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
10 # If one or more filenames are given, output is written to corresponding
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
11 # filenames in the local directory, translated to all uppercase, with
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
12 # the extension replaced by ".py".
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
13
1639
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
14 # By passing one or more options of the form "-i regular_expression"
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
15 # you can specify additional strings to be ignored. This is useful
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
16 # e.g. to ignore casts to u_long: simply specify "-i '(u_long)'".
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # XXX To do:
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # - turn trailing C comments into Python comments
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # - turn C Boolean operators "&& || !" into Python "and or not"
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # - what to do about #if(def)?
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
22 # - what to do about macros with multiple parameters?
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
24 import sys, regex, regsub, string, getopt, os
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
4112
5726be8ee59f Allow whitespace before '#' in preprocessor directives.
Guido van Rossum <guido@python.org>
parents: 2169
diff changeset
26 p_define = regex.compile('^[\t ]*#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
28 p_macro = regex.compile(
4112
5726be8ee59f Allow whitespace before '#' in preprocessor directives.
Guido van Rossum <guido@python.org>
parents: 2169
diff changeset
29 '^[\t ]*#[\t ]*define[\t ]+'
5726be8ee59f Allow whitespace before '#' in preprocessor directives.
Guido van Rossum <guido@python.org>
parents: 2169
diff changeset
30 '\([a-zA-Z0-9_]+\)(\([_a-zA-Z][_a-zA-Z0-9]*\))[\t ]+')
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
31
4112
5726be8ee59f Allow whitespace before '#' in preprocessor directives.
Guido van Rossum <guido@python.org>
parents: 2169
diff changeset
32 p_include = regex.compile('^[\t ]*#[\t ]*include[\t ]+<\([a-zA-Z0-9_/\.]+\)')
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
33
898
ef86502508e2 Fix regexp recognizing comments to cope with unterminated comments.
Guido van Rossum <guido@python.org>
parents: 687
diff changeset
34 p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
6032
9a57ca229681 support C++ comments
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
35 p_cpp_comment = regex.compile('//.*')
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
6032
9a57ca229681 support C++ comments
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
37 ignores = [p_comment, p_cpp_comment]
1639
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
38
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
39 p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
40
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
41 filedict = {}
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
42
2169
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
43 try:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
44 searchdirs=string.splitfields(os.environ['include'],';')
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
45 except KeyError:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
46 try:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
47 searchdirs=string.splitfields(os.environ['INCLUDE'],';')
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
48 except KeyError:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
49 searchdirs=['/usr/include']
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
50
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 def main():
6032
9a57ca229681 support C++ comments
Guido van Rossum <guido@python.org>
parents: 4573
diff changeset
52 global filedict
1639
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
53 opts, args = getopt.getopt(sys.argv[1:], 'i:')
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
54 for o, a in opts:
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
55 if o == '-i':
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
56 ignores.append(regex.compile(a))
1602
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
57 if not args:
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
58 args = ['-']
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
59 for filename in args:
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
60 if filename == '-':
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
61 sys.stdout.write('# Generated by h2py from stdin\n')
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
62 process(sys.stdin, sys.stdout)
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
63 else:
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
64 fp = open(filename, 'r')
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
65 outfile = os.path.basename(filename)
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
66 i = string.rfind(outfile, '.')
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
67 if i > 0: outfile = outfile[:i]
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
68 outfile = string.upper(outfile)
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
69 outfile = outfile + '.py'
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
70 outfp = open(outfile, 'w')
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
71 outfp.write('# Generated by h2py from %s\n' % filename)
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
72 filedict = {}
2169
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
73 for dir in searchdirs:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
74 if filename[:len(dir)] == dir:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
75 filedict[filename[len(dir)+1:]] = None # no '/' trailing
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
76 break
1602
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
77 process(fp, outfp)
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
78 outfp.close()
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
79 fp.close()
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
81 def process(fp, outfp, env = {}):
687
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 lineno = 0
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 while 1:
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 line = fp.readline()
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 if not line: break
e15bea971a5b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 lineno = lineno + 1
1639
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
87 n = p_define.match(line)
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
88 if n >= 0:
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
89 # gobble up continuation lines
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
90 while line[-2:] == '\\\n':
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
91 nextline = fp.readline()
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
92 if not nextline: break
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
93 lineno = lineno + 1
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
94 line = line + nextline
1639
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
95 name = p_define.group(1)
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
96 body = line[n:]
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
97 # replace ignored patterns by spaces
37cf87f561ec Added -i option to ignore general regexps
Guido van Rossum <guido@python.org>
parents: 1602
diff changeset
98 for p in ignores:
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
99 body = regsub.gsub(p, ' ', body)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
100 # replace char literals by ord(...)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
101 body = regsub.gsub(p_char, 'ord(\\0)', body)
1602
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
102 stmt = '%s = %s\n' % (name, string.strip(body))
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
103 ok = 0
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
104 try:
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
105 exec stmt in env
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
106 except:
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
107 sys.stderr.write('Skipping: %s' % stmt)
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
108 else:
1602
907c5dd89c6c Process command line arguments; skip #defines that generate invalid
Guido van Rossum <guido@python.org>
parents: 1269
diff changeset
109 outfp.write(stmt)
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
110 n =p_macro.match(line)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
111 if n >= 0:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
112 macro, arg = p_macro.group(1, 2)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
113 body = line[n:]
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
114 for p in ignores:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
115 body = regsub.gsub(p, ' ', body)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
116 body = regsub.gsub(p_char, 'ord(\\0)', body)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
117 stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
118 try:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
119 exec stmt in env
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
120 except:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
121 sys.stderr.write('Skipping: %s' % stmt)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
122 else:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
123 outfp.write(stmt)
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
124 if p_include.match(line) >= 0:
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
125 regs = p_include.regs
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
126 a, b = regs[1]
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
127 filename = line[a:b]
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
128 if not filedict.has_key(filename):
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
129 filedict[filename] = None
2169
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
130 inclfp = None
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
131 for dir in searchdirs:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
132 try:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
133 inclfp = open(dir + '/' + filename, 'r')
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
134 break
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
135 except IOError:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
136 pass
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
137 if inclfp:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
138 outfp.write(
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
139 '\n# Included from %s\n' % filename)
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
140 process(inclfp, outfp, env)
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
141 else:
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
142 sys.stderr.write('Warning - could not find file %s' % filename)
92cd85470a76 use $INCLUDE path (Mark Hammond)
Guido van Rossum <guido@python.org>
parents: 1928
diff changeset
143
1928
a8b7ee0e572d Get rid of freeze (now its own directory).
Guido van Rossum <guido@python.org>
parents: 1639
diff changeset
144 main()