def findlocals(line):
    """If a local variable is to be referenced with a system command the variable will be escaped with a $.
    This function replaces the variable with it's value.
    The $ character can be escaped anywhere with a \ that will be removed.
    (a literal '\$' then becomes '\\$')
    """
    lpos = line.find(' ')
    if lpos == -1: return line      # if there's no space (no command) then it's not a variable name
    command = line[:lpos]
    params = line[lpos:].strip()
    escapelist = ['$']          # currently just the $ character
    variable = ''
    varon = 0
    newparams = ''
    skip = 0
    params += ' '        # so that a variable at the end of a line is terminated
    for char in params:
        if skip:                    # was the last characetr a '\'
            if char in escapelist:  # if the '\' is followed by a '$' it's not a variable - so jsut move on
                newparams += char
            else:
                newparams += '\\' + char
            skip = 0
            continue
            
        if char == '\\':        # is this an escape character ?
            skip = 1
            continue
        if varon:                           # are we in the process of replacing a variable name
            if char == ' ' or char == '\"': # is this the end of the name ?
                if globals().has_key(variable):
                    newparams += str(globals()[variable]) + ' '              # str or repr ??
                else:
                    print 'No such variable as ' + variable
                    return
                varon = 0
                variable = ''
            else:
                variable += char
            continue
        if char == '$':     # have we found the start of a variable name ?
            varon = 1
            continue
        newparams += char
    newline = command + ' ' + newparams     # rebuild the command line
    return  newline
If a local variable is to be referenced with a system command the variable will be escaped with a $.
    This function replaces the variable with it's value.
    The $ character can be escaped anywhere with a \ that will be removed.
    (a literal '\$' then becomes '\$')
    
recipes/Python/279407_Windows_Console/recipe-279407.py