Sign in Join

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

Mailing lists / fpc-pascal / 2015-10

How to use the `signals` unit?

6 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

silvioprog

Hello,

What is the secret to use this it on Windows?

I have the following example in C:

==== begin code ===

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
void sigproc() {
    printf("You have pressed Ctrl-C\n");
    exit(0);
}
main()  {
    signal(SIGINT, sigproc);
    printf("Use Ctrl-C to quit\n");
    for(;;);
}

==== end code ===

So when I use the Ctrl+C command I get the "You have pressed Ctrl-C" in the terminal.

However, using this code (compiled in FPC from trunk):

==== begin code ===

program project1;
{$mode objfpc}{$H+}
uses
  signals;
  function sigproc(v: LongInt): longint; cdecl;
  begin
    WriteLn('You have pressed Ctrl-C');
    Result := v;
    Halt(0);
  end;
begin
  signal(SIGINT, @sigproc);
  WriteLn('Use Ctrl-C to quit');
  while true do ;
end.

==== end code ===

It doesn't work. :-(

The program quits, but I can't get the "You have pressed Ctrl-C".

I can use the `Windows.SetConsoleCtrlHandler()`, but I would be happy to understand how to use the `signals` unit. =)

Silvio Clécio
silvioprog
to Sven Barth
Sven Barth wrote 6 lines
Am 22.10.2015 09:23 schrieb "silvioprog" : > What is the secret to use this it on Windows? The secret is to not use it. It's only real use is in the textmode IDE and maybe there it should be checked whether it can be replaced.

So is there some alternative for signals? ( SetConsoleCtrlHandler() on Windows, but, what in Linux? )

I have a console application, and I need to block the `Ctrl+C` command, allowing only the `Ctrl+\` (or Ctrl+Break on Windows). See a small example (*) that works fine on Windows (7 / 10) and Linux (*ubuntu 14.04). The original demo was written in C and is hosted here[1].

Show quoted text 6 lines
AFAIR the signals unit is even completely broken if SEH is enabled (default on x86_64-win64, but to be made default on i386-win32). Regards, Sven
(*)

=== begin code ===

program project1;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
  BaseUnix,
{$ENDIF}
  ctypes, sysutils;
const
  LIB_NAME = {$IFDEF MSWINDOWS}'msvcrt'{$ELSE}'c'{$ENDIF};
{$IFDEF MSWINDOWS}
  SIGINT = 2;
  SIGILL = 4;
  SIGFPE = 8;
  SIGSEGV = 11;
  SIGTERM = 15;
  SIGBREAK = 21;
  SIGABRT = 22;
{$ENDIF}
type
  signal_func = procedure(sig: cint); cdecl;
  function signal(sig: cint; func: signal_func): signal_func; cdecl;
external LIB_NAME Name 'signal';
  procedure sigproc(sig: cint); cdecl;
  begin
    signal(SIGINT, @sigproc);
    WriteLn('you have pressed ctrl-c');
  end;
  procedure quitproc(sig: cint); cdecl;
  begin
    WriteLn('ctrl+break (or ctrl+\ on linux) pressed to quit');
    halt(0);
  end;
begin
  signal(SIGINT, @sigproc);
{$IFDEF MSWINDOWS}
  signal(SIGBREAK, @quitproc);
{$ELSE}
  signal(SIGTERM, @quitproc);
{$ENDIF}
  WriteLn('ctrl-c disabled use ctrl+break (or ctrl+\ on linux) to quit');
  while True do Sleep(100);
end.

=== end code ===

[1] https://www.cs.cf.ac.uk/Dave/C/node24.html

Silvio Clécio
Sven Barth
to silvioprog
"silvioprog" wrote
On Thu, Oct 22, 2015 at 6:40 AM, Sven Barth
Show quoted text 5 lines
> > Am 22.10.2015 09:23 schrieb "silvioprog" : > > What is the secret to use this it on Windows? > > The secret is to not use it. It's only real use is in the textmode IDE

and maybe there it should be checked whether it can be replaced.

So is there some alternative for signals? ( SetConsoleCtrlHandler() on

Windows, but, what in Linux? )

Currently you need to do this for each platform separately.

Regards,
Sven

silvioprog
to Sven Barth
Sven Barth wrote 17 lines
Am 23.10.2015 06:29 schrieb "silvioprog" : > > On Thu, Oct 22, 2015 at 6:40 AM, Sven Barth wrote: >> >> Am 22.10.2015 09:23 schrieb "silvioprog" : >> > What is the secret to use this it on Windows? >> >> The secret is to not use it. It's only real use is in the textmode IDE and maybe there it should be checked whether it can be replaced. > > So is there some alternative for signals? ( SetConsoleCtrlHandler() on Windows, but, what in Linux? ) > Currently you need to do this for each platform separately.

It isn't a problem for me, because my app needs to run just on Windows and Linux. =)

Silvio Clécio