[Scilab-Dev] Catch a CTRL+C

Collette Yann ycollet at freesurf.fr
Thu Nov 20 18:24:41 CET 2008


Hello,

Thanks to Serge answer.
The variable C2F(basbrk).iflag is not visible in scilab. Impossible to 
link a program with such a symbol.
But I managed to find a function in the core module:
csignal.[ch]. This function defines a handler to deal with SIGINT.
I was wondering if it was possible to extend this function so as to a 
user defined handler before the scilab one.

Something like this:

csignal.c:
#include "banier.h"
#include "csignal.h"
#include "sigbas.h"

extern int C2F(sigbas)();

typedef void (*sig_func_t)(int);

void controlC_handler (int);

static user_controlC_handler = controlC_handler;

void set_controlC_handler (sig_func_t myHandler)
{
  user_controlC_handler = myHandler;
}

sig_func_t get_controlC_handler ()
{
  return user_controlC_handler;
}

void controlC_handler (int sig)
{
  int j = SIGINT;
  C2F(sigbas)(&j);
}

int C2F(csignal)(void)
{
  signal (SIGINT, user_controlC_handler);
  return(0);
}


csignal.h:
#ifndef __CSIGNAL_H__
#define __CSIGNAL_H__
#include <signal.h>
#include "machine.h"

void controlC_handler (int sig);

typedef void (*sig_func_t)(int);

void set_usercontrolC_handler(sig_func_t handler);
sig_func_t get_controlC_handler ();

int C2F(csignal)(void);

#endif /* __CSIGNAL_H__ */


With such functions, to add a new SIGINT handler is easier:

sig_func_t old_handler = get_controlC_handler();

void my_controlC_handler(int sig)
{
  // Do something to close cleanly the C interface
  // Now call the scilab handler
  (*old_handler)(sig);
}

set_controlC_handler(my_controlC_handler);

I am open to suggestions ...
Can I commit these changes ?

YC
steer a écrit :
> ycollet at freesurf.fr a écrit :
>   
>> Hello,
>>
>> Is it possible to catch a CTRL+C (entered in the scilab shell) in a C
>> interface ?
>> I have a C interface which takes a lot of time to execute and I want
>> to interrupt this C interface while I am fine tuning some algorithms.
>>
>> YC
>>
>>
>>
>>
>>     
> Le Ctrl-C est trappé par Scilab qui assigne true a la variable 
> C2F(basbrk).iflag
> Il suffit donc de tester cette valeur et ne pas oublier de la remettre a
> false une fois prise en compte
>
> Serge
>
>   




More information about the dev mailing list