Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 3 | # 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 Rossum | 09336f9 | 1994-05-03 14:37:30 +0000 | [diff] [blame] | 10 | # 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 Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 01f5f62 | 1994-05-17 09:05:54 +0000 | [diff] [blame] | 14 | # 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 Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 17 | |
| 18 | # XXX To do: |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 19 | # - turn trailing C comments into Python comments |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 20 | # - turn C Boolean operators "&& || !" into Python "and or not" |
| 21 | # - what to do about #if(def)? |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 22 | # - what to do about macros with multiple parameters? |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 24 | import sys, regex, regsub, string, getopt, os |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 6100d91 | 1996-08-22 23:12:23 +0000 | [diff] [blame] | 26 | p_define = regex.compile('^[\t ]*#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+') |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 28 | p_macro = regex.compile( |
Guido van Rossum | 6100d91 | 1996-08-22 23:12:23 +0000 | [diff] [blame] | 29 | '^[\t ]*#[\t ]*define[\t ]+' |
| 30 | '\([a-zA-Z0-9_]+\)(\([_a-zA-Z][_a-zA-Z0-9]*\))[\t ]+') |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 6100d91 | 1996-08-22 23:12:23 +0000 | [diff] [blame] | 32 | p_include = regex.compile('^[\t ]*#[\t ]*include[\t ]+<\([a-zA-Z0-9_/\.]+\)') |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 047979e | 1992-06-05 15:13:53 +0000 | [diff] [blame] | 34 | p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?') |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 01f5f62 | 1994-05-17 09:05:54 +0000 | [diff] [blame] | 36 | ignores = [p_comment] |
| 37 | |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 38 | p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'") |
| 39 | |
| 40 | filedict = {} |
| 41 | |
Guido van Rossum | 514d351 | 1995-01-17 17:01:40 +0000 | [diff] [blame] | 42 | try: |
| 43 | searchdirs=string.splitfields(os.environ['include'],';') |
| 44 | except KeyError: |
| 45 | try: |
| 46 | searchdirs=string.splitfields(os.environ['INCLUDE'],';') |
| 47 | except KeyError: |
| 48 | searchdirs=['/usr/include'] |
| 49 | |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 50 | def main(): |
Guido van Rossum | 01f5f62 | 1994-05-17 09:05:54 +0000 | [diff] [blame] | 51 | opts, args = getopt.getopt(sys.argv[1:], 'i:') |
| 52 | for o, a in opts: |
| 53 | if o == '-i': |
| 54 | ignores.append(regex.compile(a)) |
Guido van Rossum | 09336f9 | 1994-05-03 14:37:30 +0000 | [diff] [blame] | 55 | if not args: |
| 56 | args = ['-'] |
| 57 | for filename in args: |
| 58 | if filename == '-': |
| 59 | sys.stdout.write('# Generated by h2py from stdin\n') |
| 60 | process(sys.stdin, sys.stdout) |
| 61 | else: |
| 62 | fp = open(filename, 'r') |
| 63 | outfile = os.path.basename(filename) |
| 64 | i = string.rfind(outfile, '.') |
| 65 | if i > 0: outfile = outfile[:i] |
| 66 | outfile = string.upper(outfile) |
| 67 | outfile = outfile + '.py' |
| 68 | outfp = open(outfile, 'w') |
| 69 | outfp.write('# Generated by h2py from %s\n' % filename) |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 70 | filedict = {} |
Guido van Rossum | 514d351 | 1995-01-17 17:01:40 +0000 | [diff] [blame] | 71 | for dir in searchdirs: |
| 72 | if filename[:len(dir)] == dir: |
| 73 | filedict[filename[len(dir)+1:]] = None # no '/' trailing |
| 74 | break |
Guido van Rossum | 09336f9 | 1994-05-03 14:37:30 +0000 | [diff] [blame] | 75 | process(fp, outfp) |
| 76 | outfp.close() |
| 77 | fp.close() |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 78 | |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 79 | def process(fp, outfp, env = {}): |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 80 | lineno = 0 |
| 81 | while 1: |
| 82 | line = fp.readline() |
| 83 | if not line: break |
| 84 | lineno = lineno + 1 |
Guido van Rossum | 01f5f62 | 1994-05-17 09:05:54 +0000 | [diff] [blame] | 85 | n = p_define.match(line) |
| 86 | if n >= 0: |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 87 | # gobble up continuation lines |
| 88 | while line[-2:] == '\\\n': |
| 89 | nextline = fp.readline() |
| 90 | if not nextline: break |
| 91 | lineno = lineno + 1 |
| 92 | line = line + nextline |
Guido van Rossum | 01f5f62 | 1994-05-17 09:05:54 +0000 | [diff] [blame] | 93 | name = p_define.group(1) |
| 94 | body = line[n:] |
| 95 | # replace ignored patterns by spaces |
| 96 | for p in ignores: |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 97 | body = regsub.gsub(p, ' ', body) |
| 98 | # replace char literals by ord(...) |
| 99 | body = regsub.gsub(p_char, 'ord(\\0)', body) |
Guido van Rossum | 09336f9 | 1994-05-03 14:37:30 +0000 | [diff] [blame] | 100 | stmt = '%s = %s\n' % (name, string.strip(body)) |
| 101 | ok = 0 |
| 102 | try: |
| 103 | exec stmt in env |
Guido van Rossum | 09336f9 | 1994-05-03 14:37:30 +0000 | [diff] [blame] | 104 | except: |
| 105 | sys.stderr.write('Skipping: %s' % stmt) |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 106 | else: |
Guido van Rossum | 09336f9 | 1994-05-03 14:37:30 +0000 | [diff] [blame] | 107 | outfp.write(stmt) |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 108 | n =p_macro.match(line) |
| 109 | if n >= 0: |
| 110 | macro, arg = p_macro.group(1, 2) |
| 111 | body = line[n:] |
| 112 | for p in ignores: |
| 113 | body = regsub.gsub(p, ' ', body) |
| 114 | body = regsub.gsub(p_char, 'ord(\\0)', body) |
| 115 | stmt = 'def %s(%s): return %s\n' % (macro, arg, body) |
| 116 | try: |
| 117 | exec stmt in env |
| 118 | except: |
| 119 | sys.stderr.write('Skipping: %s' % stmt) |
| 120 | else: |
| 121 | outfp.write(stmt) |
| 122 | if p_include.match(line) >= 0: |
| 123 | regs = p_include.regs |
| 124 | a, b = regs[1] |
| 125 | filename = line[a:b] |
| 126 | if not filedict.has_key(filename): |
| 127 | filedict[filename] = None |
Guido van Rossum | 514d351 | 1995-01-17 17:01:40 +0000 | [diff] [blame] | 128 | inclfp = None |
| 129 | for dir in searchdirs: |
| 130 | try: |
| 131 | inclfp = open(dir + '/' + filename, 'r') |
| 132 | break |
| 133 | except IOError: |
| 134 | pass |
| 135 | if inclfp: |
| 136 | outfp.write( |
| 137 | '\n# Included from %s\n' % filename) |
| 138 | process(inclfp, outfp, env) |
| 139 | else: |
| 140 | sys.stderr.write('Warning - could not find file %s' % filename) |
Guido van Rossum | 07c9645 | 1994-10-03 16:45:35 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | 514d351 | 1995-01-17 17:01:40 +0000 | [diff] [blame] | 142 | main() |