Re: exception handling for control C
From: Don Libes (libes_at_nist.gov)
Date: 12/17/03
- Next message: Don Libes: "Re: Please help me: Why I get out-of-sync in Expect interactive runs?"
- Previous message: Bruce Hartweg: "Re: Bind window iconify."
- In reply to: TingChong: "Re: exception handling for control C"
- Next in thread: TingChong: "Re: exception handling for control C"
- Reply: TingChong: "Re: exception handling for control C"
- Reply: TingChong: "Re: exception handling for control C"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Don Libes: "Re: Please help me: Why I get out-of-sync in Expect interactive runs?"
- Previous message: Bruce Hartweg: "Re: Bind window iconify."
- In reply to: TingChong: "Re: exception handling for control C"
- Next in thread: TingChong: "Re: exception handling for control C"
- Reply: TingChong: "Re: exception handling for control C"
- Reply: TingChong: "Re: exception handling for control C"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|