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

  1. #! /bin/env tclsh
  2.  
  3. package require Expect
  4.  
  5. proc tclprompt {} {
  6. proc [lindex [info level 0] 0] {} {
  7. return {\n%\s$}
  8. }
  9. return {%\s*$}
  10. }
  11.  
  12. proc input {fh {command {}}} {
  13. if {$command ne {} && [info complete $command]} {
  14. expect -re [tclprompt] {
  15. send $command
  16. }
  17. fileevent $fh readable [list input $fh]
  18. return
  19. }
  20.  
  21. if {[eof $fh]} {
  22. set ::done 1
  23. return
  24. }
  25.  
  26. gets $fh line
  27. if {[set line [string trimright $line]] ne {}} {
  28. append command $line\r
  29. }
  30. fileevent $fh readable [list input $fh $command]
  31. }
  32.  
  33. spawn tclsh
  34. set timeout -1
  35. set fh [open [lindex $argv 0]]
  36. fconfigure $fh -blocking 0
  37. fileevent $fh readable [list input $fh]
  38. vwait done
  39.