1
考虑下面的代码:如何检查stdin是否在TCL中挂起?
chan configure stdin -blocking false
while { true } {
chan gets stdin
if { chan blocked stdin } { break }
}
这里在循环的最后一行chan blocked stdin
返回true,在两种情况:当在stdin
可没有更多的投入,而当有有可用的stdin
一些投入,但不会以换行符结束。我需要区分这两种情况。
我该怎么做?
chan pending input stdin
在两种情况下也返回0
。
以下是使用上述代码的上下文。
proc prompt { } { puts -nonewline stdout "MyShell > "; flush stdout }
proc evaluate { } \
{
chan configure stdin -blocking false
while { true } {
# for "stdin -blocking false" "read stdin" acts like "gets stdin"
set part [chan read stdin 100]
append command $part
if { $part eq "" } { break }
}
chan configure stdin -blocking true
# Here I want test if there are pending characters at stdin,
# and while so, I want to wait for newline character.
# It should be like this:
# while { *the required test goes here* } {
# append command [chan gets stdin]\n
# }
while { ! [info complete $command] } {
append command [chan gets stdin]\n
}
catch { uplevel #0 $command } got
if { $got ne "" } {
puts stderr $got
flush stderr
}
prompt
}
chan event stdin readable evaluate
prompt
while { true } { update; after 100 }
你真的想做什么?在按键时按字符逐字读取?你需要什么平台? – slebetman 2012-01-06 08:27:40
我正在尝试编写一个类似'tslsh'的shell。上面的代码处理在shell中输入的输入。如果输入以换行符结束,那么shell必须尝试对其进行评估,否则,即在输入处有未决字符的情况下,shell必须等待换行符。 – Vahagn 2012-01-06 09:13:45
@slebetman请参阅更新。 – Vahagn 2012-01-06 09:31:59