如何检查stdin中的stdin是否可读?

时间:2012-01-05 18:20:11

标签: tcl stdin gets readable

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

fileevent stdin readable thatCallback

这意味着在执行更新命令期间,当thatCallback处有可用输入时,它会一次又一次评估stdin

如何检查stdin处的输入是否可用?

2 个答案:

答案 0 :(得分:3)

您只需在回调中读取/获取stdin即可。基本上,模式就像fileevent example from Kevin Kenny

中的这个片段一样
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

答案 1 :(得分:0)

如果查看this问题的答案,可以看到如何使用fconfigure将频道置于非阻塞模式。有关此Tcl手册的更多详细信息,您需要查看fconfigure手册页以及vwait手册页。