【发布时间】:2012-02-01 22:38:13
【问题描述】:
这是一个代码,它只是实现了一个带有命令提示符MyShell > 的交互式 TCL 会话。
puts -nonewline stdout "MyShell > "
flush stdout
catch { eval [gets stdin] } got
if { $got ne "" } {
puts stderr $got
}
这段代码在终端提示MyShell >,等待回车键被按下;虽然它没有被击中,但代码什么也不做。这就是gets 命令的作用。
我需要的是gets 命令的替代方法,例如coolget。 coolget 命令不应该等待回车按钮,而是注册一些当它被击中时调用的槽,然后继续执行。所需的代码应如下所示:
proc evaluate { string } \
{
catch { eval $string } got
if { $got ne "" } {
puts stderr $got
}
}
puts -nonewline stdout "MyShell > "
flush stdout
coolgets stdin evaluate; # this command should not wait for the enter button
# here goes some code which is to be executed before the enter button is hit
这是我需要的:
proc prompt { } \
{
puts -nonewline stdout "MyShell > "
flush stdout
}
proc process { } \
{
catch { uplevel #0 [gets stdin] } got
if { $got ne "" } {
puts stderr $got
flush stderr
}
prompt
}
fileevent stdin readable process
prompt
while { true } { update; after 100 }
【问题讨论】:
标签: tcl stdin interactive gets