【发布时间】:2012-02-04 00:24:26
【问题描述】:
考虑以下代码:
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 }
【问题讨论】:
-
你真正想做什么?按下键时逐个字符读取?您需要在什么平台上进行此操作?
-
我正在尝试编写类似于
tslsh的shell。上面的代码处理在 shell 中输入的输入。如果输入以换行符结尾,则 shell 必须尝试评估它,否则,即如果输入中有待处理的字符,则 shell 必须等待换行符。 -
@slebetman 请查看更新。