/*
**    Name: init.c
**
**    Date: Mon Aug 19 19:37:25 1996
**
*/

#include <signal.h>
#include "kernel.h"
#include "support.h"
#include "wimp.h"
#include "setjmp.h"

wimp_t task;

extern jmp_buf ossup_jmp_buf;

/* Install signal handlers */

int init_signals(void)
    {
    signal(SIGABRT,sigabrt);
    signal(SIGFPE,sigfpe);
    signal(SIGILL,sigill);
    signal(SIGINT,sigint);
    signal(SIGSEGV,sigsegv);
    signal(SIGTERM,sigterm);
    signal(SIGSTAK,sigstak);
    signal(SIGOSERROR,sigoserror);

    return FALSE;
    }


void sigabrt(int sig)
    {
    int response = 1;
    if (!report_prompt("The application encountered an unexpected error. Abort signal received. Click OK to continue or Cancel to exit the application."))
        {
        response = 2;
        }
    signal(SIGABRT,sigabrt);
    longjmp (ossup_jmp_buf, response);
    }


void sigfpe(int sig)
    {
    int response = 1;
    if (!report_prompt("The application encountered an unexpected error. Floating point exception. Click OK to continue or Cancel to exit the application."))
        {
        response = 2;
        }
    signal(SIGFPE,sigfpe);
    longjmp (ossup_jmp_buf, response);
    }


void sigill(int sig)
    {
    int response = 1;
    if (!report_prompt("The application encountered an unexpected error. Illegal instruction. Click OK to continue or Cancel to exit the application."))
        {
        response = 2;
        }
    signal(SIGILL,sigill);
    longjmp (ossup_jmp_buf, response);
    }


void sigint(int sig)
    {
    int response = 1;
    if (!report_prompt("The application encountered an unexpected error. User interrupt received. Click OK to continue or Cancel to exit the application."))
        {
        response = 2;
        }
    signal(SIGINT,sigint);
    longjmp (ossup_jmp_buf, response);
    }


void sigsegv(int sig)
    {
    int response = 1;
    if (!report_prompt("The application encountered an unexpected error. Segmentation violation. Click OK to continue or Cancel to exit the application."))
        {
        response = 2;
        }
    signal(SIGSEGV,sigsegv);
    longjmp (ossup_jmp_buf, response);
    }


void sigterm(int sig)
    {
    int response = 1;
    if (!report_prompt("The application encountered an unexpected error. Termination request. Click OK to continue or Cancel to exit the application."))
        {
        response = 2;
        }
    signal(SIGTERM,sigterm);
    longjmp (ossup_jmp_buf, response);
    }


void sigstak(int sig)
    {
    int response = 1;
    if (!report_prompt("The application encountered an unexpected error. Stack overflow. Click OK to continue or Cancel to exit the application."))
        {
        response = 2;
        }
    signal(SIGSTAK,sigstak);
    longjmp (ossup_jmp_buf, response);
    }


void sigoserror(int sig)
    {
    _kernel_oserror *oserror;

    int response = 1;

    NOT_USED(sig);

    oserror = _kernel_last_oserror();

    report_error(oserror->errnum,oserror->errmess);
    signal(SIGOSERROR,sigoserror);
    longjmp (ossup_jmp_buf, response);
    }

