这是一个代码,它只是实现了一个带有命令提示符的交互式 TCL 会话MyShell >
。
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 }