Posted to tcl by validuser at Wed Nov 21 09:30:26 GMT 2007view raw

  1. # it behaves almost like a normal interactive session :P
  2. # invoke 'eventloop' to enter the event loop
  3. proc stdin {} {
  4. global stdin
  5. if {[info complete [append stdin [gets stdin]]]} {
  6. if {$stdin=="exit"} {
  7. unset ::eventloop; unset stdin
  8. } {
  9. catch {uplevel #0 $stdin} stdin
  10. puts -nonewline "$stdin\n% "
  11. flush stdout
  12. set stdin ""
  13. }
  14. } {
  15. append stdin "\n"
  16. }
  17. }
  18. proc eventloop {} {
  19. if {[info exists eventloop]} {
  20. puts -nonewline "You're already in the event loop :)\n%"
  21. flush stdout
  22. } {
  23. fileevent stdin readable stdin
  24. set eventloop 1
  25. puts -nonewline "Entering the event loop...\n% "
  26. flush stdout
  27. vwait eventloop
  28. puts "Exiting the event loop..."
  29. fileevent stdin readable {}
  30. }
  31. }