Guido van Rossum | 41ffccb | 1993-04-01 20:50:35 +0000 | [diff] [blame] | 1 | #! /usr/local/bin/python |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 2 | |
| 3 | # Read #define's from stdin and translate to Python code on stdout. |
Guido van Rossum | 09336f9 | 1994-05-03 14:37:30 +0000 | [diff] [blame^] | 4 | # 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 Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 9 | |
| 10 | # XXX To do: |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 11 | # - 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 Rossum | 09336f9 | 1994-05-03 14:37:30 +0000 | [diff] [blame^] | 18 | import sys, regex, string, getopt, os |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 19 | |
| 20 | p_define = regex.compile('^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+') |
| 21 | |
Guido van Rossum | 047979e | 1992-06-05 15:13:53 +0000 | [diff] [blame] | 22 | p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?') |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 23 | |
| 24 | def main(): |
Guido van Rossum | 09336f9 | 1994-05-03 14:37:30 +0000 | [diff] [blame^] | 25 | 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 Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 09336f9 | 1994-05-03 14:37:30 +0000 | [diff] [blame^] | 45 | def process(fp, outfp): |
| 46 | env = {} |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 47 | 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 Rossum | 09336f9 | 1994-05-03 14:37:30 +0000 | [diff] [blame^] | 68 | 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 Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 77 | |
| 78 | main() |