While running a TCL script, if someone hits Ctrl-C, does anyone know
if there is a way to capture such Ctrl-C character (so my script can
do some minimum cleanup job first before quitting)?
Thanks,
Victor
What platform?
On unix for sure you can use the Tclx extension to handle the SIGINT
signal that results from a Ctrl-C.
I also remember someone created a signals extension; google that. You
can also use Expect extension to
catch input from user ( interact ) using \003 as the regexp .
Carl
Some of these claims are at least potentially misleading.
<URL: http://wiki.tcl.tk/1886 > has more details. I sum-
marize: yes, on Unix it's good to approach this problem
in terms of signals.
You only get Ctrl-C as \003 if you put the terminal in raw mode. If
you don't, it is converted to a signal before it gets to Tcl. (OK,
strictly you could also reconfigure the terminal to make another
character be the INTR sequence. But that's just moving the problem
around. But that's really just scraping the surface of Unix terminals,
and is probably more than you want to know.) Since the original
question related to cleaning up on exit, signals are absolutely the
right way to do it.
Donal.
Thanks for all advice. I was able to implement the trap but it seems
the "puts" command is no longer working. Please see source code and
actual output after I hit "Ctrl-C":
trap {exec touch trap_file3; puts "HIT TRAP";exit} SIGINT
error writing "stdout": broken pipe
while executing
"puts "HIT TRAP""
error writing "stdout": broken pipe
while executing
It successfully trapped Ctrl-C and finished executing "exec touch
trap_file3" as I could see such file generated, but it bombed on the
next command "puts".
Is there anything else I should do here?
Thanks,
Victor
Try :
trap {exec touch trap_file3; puts "HIT TRAP";flush stdout; exit }
SIGINT
Carl
It didn't really make a difference because the same error occurs.
Thanks,
Victor