Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it possible to catch Ctrl-C keystroke?

1,763 views
Skip to first unread message

Victor

unread,
Jun 27, 2008, 6:26:29 PM6/27/08
to
Hi,

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

pal...@yahoo.com

unread,
Jun 27, 2008, 9:41:46 PM6/27/08
to

What platform?

Bezoar

unread,
Jun 27, 2008, 10:42:40 PM6/27/08
to

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

Cameron Laird

unread,
Jun 28, 2008, 3:10:19 PM6/28/08
to
In article <b53a39dc-d309-4c86...@f36g2000hsa.googlegroups.com>,

Bezoar <cwj...@gmail.com> wrote:
>On Jun 27, 8:41 pm, "palm...@yahoo.com" <palm...@yahoo.com> wrote:
>> On Jun 28, 3:26 am, Victor <vhnguy...@yahoo.com> wrote:
>>
>> > Hi,
>>
>> > 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)?
.
.
.

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

Donal K. Fellows

unread,
Jun 28, 2008, 6:42:56 PM6/28/08
to
Bezoar wrote:
> You can also use Expect extension  to
> catch input from user ( interact )  using  \003 as the regexp .

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.

Victor

unread,
Jul 10, 2008, 1:15:04 PM7/10/08
to
On Jun 28, 3:42 pm, "Donal K. Fellows" <donal.k.fell...@man.ac.uk>
wrote:

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


Bezoar

unread,
Jul 10, 2008, 4:20:59 PM7/10/08
to

Try :

trap {exec touch trap_file3; puts "HIT TRAP";flush stdout; exit }
SIGINT

Carl

Victor

unread,
Jul 10, 2008, 6:08:21 PM7/10/08
to
> Carl- Hide quoted text -
>
> - Show quoted text -


It didn't really make a difference because the same error occurs.

Thanks,
Victor

0 new messages