Posted to tcl by pooryorick at Sat Mar 23 21:16:36 GMT 2013view pretty

#! /bin/env tclsh

package require Expect

proc tclprompt {} {
    proc [lindex [info level 0] 0] {} {
        return {\n%\s$}
    }
    return {%\s*$}
}

proc input {fh {command {}}} {
    if {$command ne {} && [info complete $command]} {
        expect -re [tclprompt] {
            send $command
        }
        fileevent $fh readable [list input $fh]
        return
    }

    if {[eof $fh]} {
        set ::done 1
        return
    }

    gets $fh line
    if {[set line [string trimright $line]] ne {}} {
        append command $line\r
    }
    fileevent $fh readable [list input $fh $command]
}

spawn tclsh
set timeout -1
set fh [open [lindex $argv 0]]
fconfigure $fh -blocking 0
fileevent $fh readable [list input $fh]
vwait done