#!/usr/bin/python import os,posix,sys import re from subprocess import * from os.path import * def usage (): print "Usage: " + sys.argv[0] + " {-help} File" sys.exit(1) def get_dcl (): get_a_line = True dcl = "" semi_count = 0 while (get_a_line): line = d_file.readline() if (line == ""): break if (line.lstrip().find("//") >= 0): continue dcl = dcl + line for c in line: if (c == '{'): semi_count += 1 elif (c == '}'): semi_count -= 1 if (semi_count == 0 and re.compile("^.*; *$").match(line)): break return dcl if (len (sys.argv) == 1): usage() if (sys.argv[1] == "-help"): print "The input file contains C declarations that are converted to tests." print "The following declaration forms are recognized:" print "basic_type string = initial;" print "type string [N]... = {initial};" print "type (* string) [N]... = initial;" print "struct string {initial};" print "struct string string = {initial};" print "enum string {initial};" usage() d_file = open(sys.argv[1], 'r') if (d_file == 0): usage() dcl = get_dcl() types = {'char': "char", 'short': "short", 'int': "int", 'long': "long", 'long long': "long long", 'float': "float", 'double': "double"} while (dcl != ""): dcl = dcl.rstrip() initial = "" if (dcl.find("=") == -1 or dcl.find("enum") >= 0): dcl_end = len(dcl) else: dcl_end = dcl.find("=")-1 print "\nDcl: " + dcl.rstrip()[0:dcl_end] have_arr_ptr = re.compile("\( *\*.*\)").search(dcl) # struct bar b = { 0, 0 }; have_struct_type = re.compile("^\s*(struct|union).*{.*};").search(dcl) and dcl.find("=") == -1 have_typedef = False have_initial = dcl.find("=") >= 0 and dcl.find("enum") == -1 dcl = dcl.rstrip(';') if (have_struct_type): tokens = dcl.split() name = tokens[0] + " " + tokens[1] dcl = re.compile("\s*struct\s*\w+\s+").sub("",dcl,1) elif (have_arr_ptr): name = have_arr_ptr.group(0) name = name[2:len(name)-1].strip() dcl = re.compile("\s" + name + "\s").sub(" ",dcl,1) else: tokens = dcl.split() previous = "" name = tokens[len(tokens)-1] if (have_initial): for t in tokens: if (t == "="): name = previous initial = dcl[dcl.find("="):] break previous = t if (tokens[0] == "typedef"): have_typedef = True if name.find('[') >= 0: name = name[0:name.find('[')] dcl = re.compile("\s+" + name + "[^\w]").sub(" ",dcl,1) print "Name: " + name print "Initial: " + initial.lstrip("=").strip().strip(";") dcl = dcl.replace(initial,"").strip() dcl = re.compile("\s*(static|extern|typedef)\s*").sub("",dcl,1) print "Type: " + dcl if (have_struct_type or have_typedef): types[name] = dcl try: print "Expanded Type:" + types[dcl] except KeyError: True dcl = get_dcl()