From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,430e73ffe1dca4e9 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!j21g2000yqh.googlegroups.com!not-for-mail From: Jerry Newsgroups: comp.lang.ada Subject: Re: How to exit a loop with keyboard input Date: Tue, 13 Apr 2010 12:38:23 -0700 (PDT) Organization: http://groups.google.com Message-ID: <52ffa6a4-8b2d-4643-b7d3-154e553f1c35@j21g2000yqh.googlegroups.com> References: <176f2831-ce2a-4bb1-9e04-47f662fc7176@q23g2000yqd.googlegroups.com> <9667c83e-23d7-4c77-8dd9-d279c54b1c08@y14g2000yqm.googlegroups.com> NNTP-Posting-Host: 75.172.180.11 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1271187503 23465 127.0.0.1 (13 Apr 2010 19:38:23 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 13 Apr 2010 19:38:23 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: j21g2000yqh.googlegroups.com; posting-host=75.172.180.11; posting-account=x5rpZwoAAABMN2XPwcebPWPkebpwQNJG User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/528.16+(KHTML, like Gecko, Safari/528.16) OmniWeb/v622.8.0,gzip(gfe) Xref: g2news1.google.com comp.lang.ada:9977 Date: 2010-04-13T12:38:23-07:00 List-Id: On Apr 12, 6:59=A0am, John McCormick wrote: > On Apr 12, 5:36=A0am, Jerry wrote: > > > Thanks, Georg and Manuel, for testing. I'm on OS X 10.5.8 and: > > > MBPro:/ me$ gnat > > GNAT 4.4.0 20080314 (experimental) [trunk revision 133226] > > Copyright 1996-2007, Free Software Foundation, Inc. > > > I've tried the program on three different terminal programs with the > > same result: it prints out 0 and waits for RETURN, then prints out 1, > > etc. If I hit q then RETURN the loop is exited. But the loop never > > "free-runs." > > > Jerry > > Perhaps the Asynchronous Transfer of Control mechanism would be > appropriate. =A0Here is some GNAT code that runs under Windows XP in > which the input loop is interrutpted by Ctrl-c. =A0The interrupt handler > must be at the library level so I put it in its own package. > > =A0 =A0select > =A0 =A0 =A0 Ctrl_C_Interrupt.Wait; > =A0 =A0 =A0 Put_Line ("Handled Ctrl C"); > =A0 =A0then abort > =A0 =A0 =A0 loop > =A0 =A0 =A0 =A0 =A0Put_Line ("Enter an integer (Ctrl C to exit)"); > =A0 =A0 =A0 =A0 =A0Get (Value); > =A0 =A0 =A0 =A0 =A0Put (Value); > =A0 =A0 =A0 =A0 =A0New_Line; > =A0 =A0 =A0 end loop; > =A0 =A0end select; > > ----------------------------------------------------------- > with Ada.Interrupts.Names; > package Ctrl_C is > > =A0 =A0protected Ctrl_C_Interrupt is > =A0 =A0 =A0 entry Wait; > =A0 =A0 =A0 procedure Handler; > =A0 =A0 =A0 pragma Attach_Handler (Handler, Ada.Interrupts.Names.SIGINT); > =A0 =A0 =A0 pragma Interrupt_Priority; > =A0 =A0private > =A0 =A0 =A0 Received : Boolean :=3D False; > =A0 =A0end Ctrl_C_Interrupt; > end Ctrl_C; > > ----------------------------------------------------------- > package body Ctrl_C is > > =A0 =A0protected body Ctrl_C_Interrupt is > =A0 =A0 =A0 procedure Handler is > =A0 =A0 =A0 begin > =A0 =A0 =A0 =A0 =A0Received :=3D True; > =A0 =A0 =A0 end Handler; > > =A0 =A0 =A0 entry Wait when Received is > =A0 =A0 =A0 begin > =A0 =A0 =A0 =A0 =A0Received :=3D False; > =A0 =A0 =A0 end Wait; > =A0 =A0end Ctrl_C_Interrupt; > > end Ctrl_C; > > John Thanks, John. Your solution does work (after I added pragma Unreserve_All_Interrupts;) and I think I can adapt it to my specific problem but it takes 100% of CPU time so I'm not sure how that would affect my number-crunching loop. (A similar solution was suggested by Chris on the GNAT-OSX list but I haven't tried it yet.) Jerry