开发者

How to make tclsh to ignore EOF?

At csh you can do

set ignoreeof

or at bash tou can do

export ignoreeof=1

and this will make csh/bash to ignore EOF, i.e. it will not exit on Ctrl+D, or when it reaches the end or file.

Is there a way to make the same with tclsh ?

Is there a way to make tclsh not to exit when it reac开发者_StackOverflowhes the end of file ?


If tclsh is running a script, it doesn't exit on detecting an EOF on stdin; that's purely a feature of the built-in REPL. You can detect such a condition yourself using eof stdin, at which point you can decide what to do about it.

If you're wanting to make a Ctrl+D not be EOF, then your easiest method is to put the terminal into raw mode, like this:

set sttySettings [exec stty -g <@stdin]
exec stty -echo raw <@stdin

When you're done, switch back like this:

exec stty $sttySettings <@stdin

Make sure you switch back before the program exits!

The other thing is that if you're working with raw input, you've got to handle all line editing yourself. A convenient way to do this is to use a pure Tcl readline-alike system such as this example from the Tcler's Wiki. You might need to adapt it a bit to make Ctrl+D do what you want.

An alternative is to do this which leaves things in cooked mode and just makes Ctrl+D non-special (tested on OSX):

exec stty eof "" <@stdin

Again, you need to set things back on exit, and the fact that it's not special at all might cause problems elsewhere; after that trick above, it's just a normal character.


You might want Expect: save this as tclsh.exp

#! /usr/bin/env expect
log_user 0
spawn tclsh
if {[llength $argv] > 0} {
    send -- "set argv [list [lrange $argv 1 end]]; source [lindex $argv 0]\r"
}
interact

then run tclsh.exp somefile.tcl arg arg ...


You might be able to use the trap command from TclX.


There is no such built-in feature in tclsh.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜