【问题标题】:How to check if stdin is pending in TCL?如何检查标准输入是否在 TCL 中挂起?
【发布时间】: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 请查看更新。

标签: buffer tcl stdin


【解决方案1】:

使用事件循环运行交互式命令解释器是很有可能的,在 Tcler 的 Wiki 上描述了 in some detail。但是,如果您真的只是在运行 Tcl 命令,那么您应该考虑使用来自 TclX packagecommandloop 命令,因为它会为您处理细节。

【讨论】:

    猜你喜欢
    • 2012-02-03
    • 1970-01-01
    • 2012-02-03
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    相关资源
    最近更新 更多