/**********************************************************************
 * sigint.cc
 * Tom Kelliher
 *
 * This program demonstrates a simple use of a SIGINT (ctrl-c) interrupt
 * handler.  A SIGINT handler is installed and then a loop controlled
 * by a global variable is entered.  When the signal is received, the
 * handler is called.  The handler changes the loop control variable so
 * that the loop is exited.
 **********************************************************************/


#include <signal.h>
#include <iostream.h>
#include <stdlib.h>


int go = 1;   // The loop control variable.


// Prototypes.
void intHandler(int);


/**********************************************************************
 * main
 **********************************************************************/

int main()
{

   // Try to install the handler.
   if (signal(SIGINT, intHandler) == SIG_ERR)
   {
      cout << "Couldn't install signal handler.\n";
      exit(1);
   }

   // Sit in a loop until the signal occurs.
   while (go)
      cout << "Still going...\n";

   cout << "Exiting.\n";

   return 0;
}


/**********************************************************************
 * intHandler --- An interrupt handler.  It will reset the global
 * variabl go, causing main to leave its "infinite" loop.
 **********************************************************************/

void intHandler(int sig)
{
   if (sig == SIGINT)
      cout << "Caught SIGINT.\n";
   else
      cout << "Caught some other signal.\n";

   go = 0;
}
