2012-01-06 52 views
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 } 
+0

你真的想做什么?在按键时按字符逐字读取?你需要什么平台? – slebetman 2012-01-06 08:27:40

+0

我正在尝试编写一个类似'tslsh'的shell。上面的代码处理在shell中输入的输入。如果输入以换行符结束,那么shell必须尝试对其进行评估,否则,即在输入处有未决字符的情况下,shell必须等待换行符。 – Vahagn 2012-01-06 09:13:45

+0

@slebetman请参阅更新。 – Vahagn 2012-01-06 09:31:59

回答

2

运行与事件循环交互式命令解释器是完全可能的,并且描述in some detail在Tcler的维基。但是,如果您真的只是运行Tcl命令,那么您应该考虑使用TclX package中的commandloop命令,因为这会为您处理细节。

相关问题