Posted to tcl by APN at Wed Sep 01 18:21:25 GMT 2010view raw

  1. ################################################################
  2. # Command line loop
  3.  
  4. # Asynch command line adapted from Welch.
  5. # Reads commands from standard input and executes them.
  6. proc ::woof::console::start {{prompt "woof>"}} {
  7. variable command_line
  8. set command_line ""
  9. puts -nonewline $prompt
  10. flush stdout
  11. fileevent stdin readable [list [namespace current]::run_command $prompt]
  12. }
  13.  
  14. # Callback from file event to execute a command
  15. proc ::woof::console::run_command {prompt} {
  16. variable command_line
  17.  
  18. if {[eof stdin]} { exit }
  19.  
  20. append command_line [gets stdin]
  21. if {[info complete $command_line]} {
  22.  
  23. if {[catch {uplevel \#0 $command_line} result]} {
  24. set chan stderr
  25. } else {
  26. set chan stdout
  27. }
  28. set command_line ""
  29. puts $chan $result
  30. flush $chan
  31. puts -nonewline $prompt
  32. flush stdout
  33. } else {
  34. # Command not complete
  35. append command_line "\n"
  36. }
  37. return
  38. }
  39.  
  40.  
  41. # Stops the command line loop
  42. proc ::woof::console::stop {} {
  43. variable command_line
  44. set command_line ""
  45. fileevent stdin readable {}
  46. }
  47.  
  48. ::woof::console::start
  49. vwait ::until_exit