# include "opihi.h"

static int Nint = 0;

/* return FALSE if we are called many times in a row 
   without a valid answer or any answer */

void handle_interrupt (int input) {
  
  char string[64];
  int Nask;

  signal (SIGINT, SIG_IGN);

  Nask = 0;
  Nint ++;

  // 3 ctrl-c in a row will interrupt regardless
  if (Nint > 3) { 
    interrupt = TRUE;
    return;
  }
  if (Nint > 100) { 
    exit (3);
  }
  
  while (1) {
    gprint (GP_ERR, "operation halted, continue? (y/n) ");
    fscanf (stdin, "%s", string);
    
    if ((string[0] == 'y') || (string[0] == 'Y')) {
      interrupt = FALSE;
      signal (SIGINT, handle_interrupt);
      Nint = 0;
      return;
    }

    if ((string[0] == 'n') || (string[0] == 'N')) {
      interrupt = TRUE;
      signal (SIGINT, handle_interrupt);
      Nint = 0;
      return;
    }
    Nask ++;
    if (Nask > 3) {
      interrupt = TRUE;
      signal (SIGINT, handle_interrupt);
      Nint = 0;
      return;
    }
  }  

}
