Re: exception handling for control C

From: Don Libes (libes_at_nist.gov)
Date: 12/17/03


Date: 16 Dec 2003 19:53:21 -0500

Well, at this point, it's just a matter of getting your logic right.
Why don't you have GetResponse check traped before (as well as after)
it calls gets?

Don

ma7777772@hotmail.com (TingChong) writes:

> How to call a function inside an signal handler when the main program
> also calls that function which also encounters the same signal?
>
> e.g.
> trap {
> global traped
>
> set traped 1
> puts "Would you like to exit (y/n)?"
> set resp [GetResponse {"yn" ""}]
> if {$resp == "y"]} {
> puts "exit"
> exit 1
> }
> puts "program resume..."
> } SIGINT
>
> proc GetResponse {validChar {defaultChar ""} errorMessage} {
> global traped
>
> set pattern \[[string tolower $validChar]]
> while {1} {
> #ensure response has a valid value if exception occurs
> catch {gets stdin} response
> puts "traped $traped"
> if {$traped == 1} {
> set traped 0
> puts "$errorMessage"
> continue
> }
> puts "response $response"
> set response [string tolower [string trim $response]]
> if {$response eq ""} {
> set response $defaultChar
> break
> }
> set firstChar [string index $response 0]
> if {[string match $pattern $firstChar]} {
> set response $firstChar
> break
> }
> puts "Invalid input '$response'."
> puts "$errorMessage"
> }
> return $response
> }
>
> set i 0;
> while {1} {
> puts "Please insert a tape and press <Enter> to continue or \"e\" to
> exit."
> set resp [GetResponse "e" "" "Press <Enter> to continue or \"e\" to
> exit."]
> if {[string match $resp "e"]} {
> exit 0
> }
>
> puts [incr i]; after 20
> }
>
> # the output is:
> Please insert a tape and press <Enter> to continue or "e" to exit.
> ^CWould you like to exit (y/n)?
> traped 1
> Press <Enter> to continue or "e" to exit.
>
> ----------------------------------------
> The user do not have a chance to enter "y" or "n" after the "control
> C" is pressed at the prompt "Please insert a tape and press <Enter> to
> continue or "e" to exit.".
> How to fix that?
> Please help.
>
> Don Libes <libes@nist.gov> wrote in message news:<s6afzfpexbu.fsf@peace.mel.nist.gov>...
> > Have the signal handler set a global variable. Then check the
> > variable after returning from the gets to figure out whether it
> > returned normally or due to the signal handler. Then you can decide
> > whether to retry the gets or not.
> >
> > Don



Relevant Pages

  • Re: exception handling for control C
    ... Have the signal handler set a global variable. ... get a line as a response from user. ... > set response $defaultChar ... > exit." ...
    (comp.lang.tcl)
  • Re: the confrontation by the amateur inn is the button that bothers exactly
    ... Amber ... contacts the exit in view of hers and jointly bends. ... in conjunction with the stuck ear. ... rubber, diagnoses in response to it, freezing frantically. ...
    (sci.crypt)
  • Re: table form exit macro add a table row
    ... ' automatically on exit from the last cell in the present last ... If Response = vbYes Then ' User chose Yes. ... entered into the last cell. ...
    (microsoft.public.word.tables)
  • Re: exception handling for control C
    ... How to call a function inside an signal handler when the main program ... puts "Would you like to exit?" ... catch response ... Please insert a tape and press to continue or "e" to exit. ...
    (comp.lang.tcl)
  • NotInList Repeat
    ... When I try to exit out of it, it just goes through the same cycle ... Response As Integer) ... Dim cteNidia As String ... Response = acDataErrContinue ...
    (microsoft.public.access.formscoding)