これは、コマンド プロンプトを使用して対話型 TCL セッションを実装するだけのコードですMyShell >
。
puts -nonewline stdout "MyShell > "
flush stdout
catch { eval [gets stdin] } got
if { $got ne "" } {
puts stderr $got
}
このコードMyShell >
は端末でプロンプトを表示し、Enter ボタンが押されるのを待ちます。ヒットしていない間、コードは何もしません。これがgets
コマンドの動作です。
私が必要としているのは、gets
コマンドに代わるものcoolget
です。coolget
コマンドはエンターボタンを待つのではなく、ヒット時に呼び出されるスロットを登録し、実行を継続する必要があります。目的のコードは次のようになります。
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 }