Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2008-02

EControlC exception

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Guillermo Martínez Jiménez

Hi.

Recently I discovered that Delphi can detect [Ctrl]+C keys using the EControlC exception. I was curious so I decided to test it in Linux using Free Pascal. The program I wrote:
_________________________________

PROGRAM ExControlC;
(* How to capture [Ctrl+C] in Object Pascal.  *)
{$mode objfpc}{$H+}
USES
  SysUtils;
VAR
  Tmp: STRING;
BEGIN
  TRY
    WriteLN ('Press [Enter] or [Ctrl+C].');
    ReadLN (Tmp);
    WriteLN ('You pressed [Enter].');
  EXCEPT
    ON EControlC DO WriteLN ('You pressed [Ctrl+C].');
  END;
  WriteLN ('Have a nice day.');
END.
_________________________________

That's simple. I tested it on Linux but it doesn't worked. It compiles and run, but if you press [Ctrl]+C the program exits without the "You pressed [Ctrl+C]." message. Is it an issue or a bug? May be it's not possible to detect [Ctrl]+C in Linux?

Guillermo "Ñuño" Martínez

Michael Van Canneyt
to Guillermo Martínez Jiménez
Guillermo Martínez Jiménez wrote 33 lines
Hi. Recently I discovered that Delphi can detect [Ctrl]+C keys using the EControlC exception. I was curious so I decided to test it in Linux using Free Pascal. The program I wrote: _________________________________ PROGRAM ExControlC; (* How to capture [Ctrl+C] in Object Pascal. *) {$mode objfpc}{$H+} USES SysUtils; VAR Tmp: STRING; BEGIN TRY WriteLN ('Press [Enter] or [Ctrl+C].'); ReadLN (Tmp); WriteLN ('You pressed [Enter].'); EXCEPT ON EControlC DO WriteLN ('You pressed [Ctrl+C].'); END; WriteLN ('Have a nice day.'); END. _________________________________ That's simple. I tested it on Linux but it doesn't worked. It compiles and run, but if you press [Ctrl]+C the program exits without the "You pressed [Ctrl+C]." message. Is it an issue or a bug? May be it's not possible to detect [Ctrl]+C in Linux?

It is not always possible. The shell catches the ctrl-c and kills your program using a SIGINT signal.

You can catch this signal using a signal handler:

PROGRAM ExControlC;
(* How to capture [Ctrl+C] in Object Pascal.  *)
{$mode objfpc}{$H+}
USES
  SysUtils,baseunix;
procedure term(sig :longint);cdecl;
begin
  Writeln('Interrupt caught');
  Halt;
end;
VAR
 Tmp: STRING;
BEGIN
  fpsignal(SIGINT,@term);
  TRY
    WriteLN ('Press [Enter] or [Ctrl+C].');
    ReadLN (Tmp);
    WriteLN ('You pressed [Enter].');
  EXCEPT
    ON EControlC DO WriteLN ('You pressed [Ctrl+C].');
  END;
  WriteLN ('Have a nice day.');
END.

Should print
gru: >fpc tt.pp
gru: >./tt
Press [Enter] or [Ctrl+C].
Interrupt caught

Michael.

Show quoted text 6 lines
Guillermo "Ñuño" Martínez _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Jonas Maebe
to Michael Van Canneyt
Michael Van Canneyt wrote 17 lines
You can catch this signal using a signal handler: PROGRAM ExControlC; (* How to capture [Ctrl+C] in Object Pascal. *) {$mode objfpc}{$H+} USES SysUtils,baseunix; procedure term(sig :longint);cdecl; begin Writeln('Interrupt caught'); Halt; end;

Note that in real programs, you should never call writeln (or pretty much any other RTL function) from a signal handler, as many routines are not re-intrant (and there is no documentation regarding which routines are safe to call from signal handlers and which aren't).

Jonas