使用以下命令,您可以注册一些回调stdin
:
fileevent stdin readable thatCallback
这意味着在执行更新命令期间,thatCallback
当有可用输入时,它将一次又一次地评估stdin
。
如何检查输入是否可用stdin
?
您只需在回调中从标准输入读取/获取。基本上,该模式类似于Kevin Kenny 的 fileevent 示例中的这个片段:
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 fid [open "|ls"]
# Set up to deliver file events on the pipe
fconfigure $fid -blocking false
fileevent $fid readable [list isReadable $fid]
# Launch the event loop and wait for the file events to finish
vwait ::DONE
# Close the pipe
close $fid
如果您查看此问题的答案,您可以了解如何使用 fconfigure 将通道置于非阻塞模式。这本 Tcl 手册有很多更详细的信息,您需要查看 fconfigure 手册页和 vwait 手册页。