#!/usr/bin/python

import os
import sys
import re

#call cilly and ask it to dump the call tree
cillyroot = os.environ['CILLYROOT']
cilly_bin = os.path.join(cillyroot,'cilly') 


#set CUR_CIL_FILE env variable to tell the cilly routine which file
#is currently being processed
matched = False;
curFile = "-";

#assume file separator char is '/'
valid_path_chars = "[_a-zA-Z0-9/.-]*?"; #path not required?
valid_fname_chars = "[_a-zA-Z0-9-]+?";  #excluding the extension
valid_prefix = valid_path_chars + valid_fname_chars;

#watch out for the order...
#don't need .h files? inlining already done? src msg wrong though?
reg = re.compile(valid_prefix + "\.cil\.c|" +
                 valid_prefix + "\.cpp|" +
                 valid_prefix + "\.C|" +
                 valid_prefix + "\.cc|" +
                 valid_prefix + "\.c");

for a in sys.argv[1:]:
    mat = reg.search(a);
    if mat <> None:
        if matched:
            print >> sys.stderr, "matched too many sources";
        else:
            matched = True;
            curFile = mat.group(0);
            
if (not matched) :
    print >> sys.stderr, "didn't match any sources";
else :
    os.environ['CUR_CIL_FILE'] = curFile
    print >> sys.stderr, "duppy'ing %s to %s" % (os.environ['CUR_CIL_FILE'],
                                                 os.environ['DUMPROOT'])



# still need ptr analysis to check if fp calls are to malloc
cillyargs = ['cilly', '--dotrans_alloc', '--domyptranal', '--dodumpfile'] + sys.argv[1:];

os.execv(cilly_bin, cillyargs);
