comp.lang.ada
 help / color / mirror / Atom feed
From: John McCormick <mccormick@cs.uni.edu>
Subject: Re: How to exit a loop with keyboard input
Date: Mon, 12 Apr 2010 06:59:32 -0700 (PDT)
Date: 2010-04-12T06:59:32-07:00	[thread overview]
Message-ID: <9667c83e-23d7-4c77-8dd9-d279c54b1c08@y14g2000yqm.googlegroups.com> (raw)
In-Reply-To: fac52fea-ee47-4d08-b322-7098fb68ea1e@i37g2000yqn.googlegroups.com

On Apr 12, 5:36 am, Jerry <lancebo...@qwest.net> 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.  Here is some GNAT code that runs under Windows XP in
which the input loop is interrutpted by Ctrl-c.  The interrupt handler
must be at the library level so I put it in its own package.

   select
      Ctrl_C_Interrupt.Wait;
      Put_Line ("Handled Ctrl C");
   then abort
      loop
         Put_Line ("Enter an integer (Ctrl C to exit)");
         Get (Value);
         Put (Value);
         New_Line;
      end loop;
   end select;

-----------------------------------------------------------
with Ada.Interrupts.Names;
package Ctrl_C is

   protected Ctrl_C_Interrupt is
      entry Wait;
      procedure Handler;
      pragma Attach_Handler (Handler, Ada.Interrupts.Names.SIGINT);
      pragma Interrupt_Priority;
   private
      Received : Boolean := False;
   end Ctrl_C_Interrupt;
end Ctrl_C;

-----------------------------------------------------------
package body Ctrl_C is

   protected body Ctrl_C_Interrupt is
      procedure Handler is
      begin
         Received := True;
      end Handler;

      entry Wait when Received is
      begin
         Received := False;
      end Wait;
   end Ctrl_C_Interrupt;

end Ctrl_C;

John




  reply	other threads:[~2010-04-12 13:59 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-12  4:12 How to exit a loop with keyboard input Jerry
2010-04-12  8:50 ` Georg Bauhaus
2010-04-12  9:21   ` Manuel Collado
2010-04-12 10:36 ` Jerry
2010-04-12 13:59   ` John McCormick [this message]
2010-04-13 19:38     ` Jerry
2010-04-12 15:14   ` Tero Koskinen
2010-04-12 16:16     ` John B. Matthews
2010-04-12 20:04     ` Simon Wright
2010-04-13 19:51       ` Jerry
2010-04-13 21:22         ` Simon Wright
2010-04-15  3:39           ` Jerry
2010-04-15  4:08             ` John B. Matthews
2010-04-15 18:51               ` Jerry
2010-04-16  0:44                 ` John B. Matthews
2010-04-16  4:43                   ` Simon Wright
2010-04-16 14:03                     ` John B. Matthews
2010-04-15 19:22             ` Simon Wright
2010-04-16 10:25               ` Jerry
2010-04-12 20:57 ` Alex Mentis
2010-04-12 22:51   ` Jerry
2010-04-16  0:06     ` BrianG
2010-04-16  7:23       ` Jerry
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox