
/* onbreak.c - ryb

  Handles interupt caused by ^C.  Argument is a pointer to a handler function.
  A null pointer argument resets handler to the default.  The handler returns
  an int.  If it is zero, control returns to the program.  Otherwise, the
  program is exited via the default handler.  A ^C signal is ignored while
  the handler is executing. */

#include <stddef.h>
#include <signal.h>

typedef int (*onbreak_func_t) (void);

static onbreak_func_t onbreak_func;

static void
onbreak_handler (int sig)
{
  if (onbreak_func ())
    raise (SIGINT);
  else
    signal (SIGINT, onbreak_handler);
}

void
onbreak (int (*func) (void))
{
  onbreak_func = func;
  signal (SIGINT, (func != NULL) ? onbreak_handler : SIG_DFL);
}
