Posted to tcl by aspect at Fri Apr 24 04:11:25 GMT 2015view raw
- package require pkg
- package require chans
- pkg -export {} repl {
- proc tcp {port {myaddr 127.0.0.1}} {
- debug log {REPL listening on $myaddr:$port}
- socket -server [namespace which accept] -myaddr $myaddr $port
- }
- proc accept {chan addr port} {
- debug log {REPL connected from $addr:$port}
- fconfigure $chan -blocking 0 -buffering none -encoding utf-8
- coroutine tcp:$addr:$port chan $chan
- }
- proc chan {chan {outchan ""} {errchan ""}} {
- if {$outchan eq ""} {set outchan $chan}
- if {$errchan eq ""} {set errchan $outchan}
- set iter [chans::iterobj new $chan]
- try {
- while {1} {
- puts -nonewline $outchan "% "
- set cmd [$iter next]
- try {
- set res [uplevel #0 $cmd]
- } on ok {} {
- puts $outchan $res
- } on error {e o} {
- puts $errchan "ERROR: $e"
- }
- }
- } finally {
- $iter destroy
- }
- }
- }