Posted to tcl by nscerqueira at Mon Sep 06 14:21:50 GMT 2010view raw
- set cmd1 " ls -la"
- set cmd2 " ls -lh"
-
- proc isReadable { f } {
- # The channel is readable; try to read it.
- set status [catch { gets $f line } result]
- if { $status != 0 } {
- # Error on the channel
- puts "error reading $f: $result"
- set ::DONE 2
- } elseif { $result >= 0 } {
- # Successfully read the channel
- puts "got: $line"
- } elseif { [eof $f] } {
- # End of file on the channel
- puts "end of file"
- set ::DONE 1
- } elseif { [fblocked $f] } {
- # Read blocked. Just return
- } else {
- # Something else
- puts "can't happen"
- set ::DONE 3
- }
- }
-
-
-
- # Open a pipe
- set fid1 [open "|$cmd1"]
- set fid2 [open "|$cmd2"]
-
- # Set up to deliver file events on the pipe
- # fconfigure $fid -blocking false
- fileevent $fid1 readable [list isReadable $fid1]
- fileevent $fid2 readable [list isReadable $fid2]
-
- # Launch the event loop and wait for the file events to finish
- vwait ::DONE
-
- # Close the pipe
- close $fid1
- close $fid2
Comments
Posted by dkf at Mon Sep 06 14:27:17 GMT 2010 [text] [code]
As long as you close the channel on [eof], you don't need to manually test for errors from [gets]. It only errors out (when non-blocking) if the channel is closed.