TCL通过-nohang选项获得命令吗? [英] TCL gets command with kind of -nohang option?

查看:233
本文介绍了TCL通过-nohang选项获得命令吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个仅通过命令提示符MyShell >实现交互式TCL会话的代码.

Here is a code which just implements an interactive TCL session with command prompt MyShell >.

puts -nonewline stdout "MyShell > "
flush stdout
catch { eval [gets stdin] } got
if { $got ne "" } {
    puts stderr $got
}

此代码在终端上提示MyShell >并等待按下Enter键;虽然未命中该代码,但无济于事.这就是gets命令的作用.

This code prompts MyShell > at the terminal and waits for the enter button to be hit; while it is not hit the code does nothing. This is what the gets command does.

我需要的是gets命令的替代方法,例如coolget. coolget命令不应等待输入按钮,而应在点击时注册要调用的某个插槽,然后继续执行.所需的代码应如下所示:

What I need, is some alternative to the gets command, say coolget. The coolget command should not wait for the enter button, but register some slot to be called when it is hit, and just continue the execution. The desired code should look like this:

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 }

推荐答案

我认为您需要查看fileevent,fconfigure和vwait命令.使用这些,您可以执行以下操作:

I think you need to look at the fileevent, fconfigure and vwait commands. Using these you can do something like the following:

proc GetData {chan} {
    if {[gets $chan line] >= 0} {
       puts -nonewline "Read data: "
       puts $line
    }
}

fconfigure stdin -blocking 0 -buffering line -translation crlf
fileevent stdin readable [list GetData stdin]

vwait x

此代码将GetData注册为stdin的可读文件事件处理程序,因此,只要有可读取的数据,就会调用它.

This code registers GetData as the readable file event handler for stdin, so whenever there is data available to be read it gets called.

这篇关于TCL通过-nohang选项获得命令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆