tags:

views:

91

answers:

4

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 reaches the end of file ?

A: 

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

Dennis Williamson
That's for signal handling, end-of-file is not a signal.
Colin Macleod
You probably still want to use `trap`, even if just to reset the terminal back to normal on a real signal like SIGINT or SIGTSTP.
Donal Fellows
A: 

There is no such built-in feature in tclsh.

Bryan Oakley
then how can i implement it ?
vahagn
@vahagn: why do you need to? What problem are you trying to solve?
Bryan Oakley
@ Bryan Oakley: I am developing a tclsh based shell environment, and I need to do some initializing stuff when launching that environment. So I need the tclsh to source some init.tcl and stay on the interactive session (not to exit it while reading ^D at the end of file init.tcl).
vahagn
@vahagn: Then you can simply use the `[source]` command instead of passing init.tcl as command line argument. Or are you wondering about something else: like how to write a tclsh based shell in the first place?
slebetman
@slebetman: What you are suggesting is first to type tclsh (the interactive tcl session is opened), and then type source init.tcl (in that interactive tcl session). What I want is to do both these steps at once (by typing one bash command).
vahagn
@vahagn: are you familiar with the .tclsh file? If it exists in your home directory it will be sourced at startup.
Bryan Oakley
@Bryan Oakley: Yes, I am familiar with that file. Furthermore, currently the shell environment I am developing, has a workaround for the issue. It saves .tclshrc to a backup file, then copies init.tcl to .tclshrc, then launches the tclsh and then from the interactive tclsh session copies the backup file back to .tclsrhc (actually I had to make this last copying statement a part of init.tcl). This is an ugly workaround, and the point is not to have a workaround, but to have the issue fixed.
vahagn
@Bryan: You're correct, but unhelpful. Since he's asking about Ctrl+D, we know he's on Unix and so can exec the system stty program to make that key sequence non-special. Of course, at that point it's *really* non-special – it becomes just like any other normal character, like a letter or number – so putting the terminal into raw mode and dealing with everything might actually be the best way to get the effects desired…
Donal Fellows
A: 

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 ...

glenn jackman
Hmm, a downvote... let me edit.
glenn jackman
A: 

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.

Donal Fellows