如何检查stdin在TCL中是否可读



使用以下命令,您可以为stdin:注册一些回调

fileevent stdin readable thatCallback

这意味着在执行更新命令期间,当在stdin处有可用的输入时,它将一次又一次地评估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手册页面一起查看。

相关内容

  • 没有找到相关文章

最新更新