/*
 * signal.c - signal(), raise(), abort()

    This file is part of libRILc, a standard C library for GCC on Amiga OS.
    Copyright © 1998  Rask Ingemann Lambertsen

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    As a special exception, if you link this library with files compiled
    with a GNU compiler to produce an executable, this does not cause the
    resulting executable to be covered by the GNU General Public License.
    This exception does not however invalidate any other reasons why the
    executable file might be covered by the GNU General Public License.
*/

#include <dos/dos.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include "internal/globals.h"
#include "internal/systemcalls.h"
#include "internal/signalcheck.h"

#ifdef __PPC__
#include <ppcproto/dos.h>
#else
#include <proto/dos.h>
#endif

static char *signal_messages[NSIG - 1] =
{
  /* SIGINT   */  "***Break\n",
  /* SIGKILL  */  "***Killed\n",
  /* SIGHUP   */  "***Hangup\n",
  /* SIGABRT  */  "***Abort (core dumps not implemented)\n",
  /* SIGSEGV  */  "***Segmentation fault (core dumps not implemented)\n"
};

void (*signal(int sig, void (*func)(int)))(int)
{
  void (*oldhandler)(int);
  __syscall_handle sh;

  sh = __syscall_start ();
  switch (sig)
  {
    case SIGINT:
    case SIGHUP:
    case SIGABRT:
    case SIGSEGV:
    oldhandler = __signal_handlers[sig - 1];
    __signal_handlers[sig - 1] = func;
    break;

    case SIGKILL:
    default:
    oldhandler = SIG_ERR;
    __ioerr = ERROR_BAD_NUMBER;
    errno = EINVAL;
    break;
  }
  __syscall_end (sh);
  return (oldhandler);
}

int raise (int sig)
{
  int retval;
  void (*handler)(int);

  switch (sig)
  {
    case SIGINT:
    case SIGKILL:
    case SIGHUP:
    case SIGSEGV:
    handler = __signal_handlers[sig - 1];
    switch ((ptrdiff_t) handler)
    {
      case SIG_IGN:
      break;

      case SIG_DFL:
      /* So far all implemented signals are "fatal" by default. */
      fputs (signal_messages[sig - 1], stderr);
      exit (RETURN_FAIL);

      default:
      /* Restore default behaviour and call signal handler. */
      signal (sig, SIG_DFL);
      /* call_handler (handler, sig); */
      (*handler) (sig);
      break;
    }
    /* Fall through. */

    case 0:
    retval = 0;
    break;

    default:
    __ioerr = ERROR_BAD_NUMBER;
    errno = EINVAL;
    retval = -1;
  }
  return (retval);
}

void abort (void)
{
  raise (SIGABRT);
  exit (RETURN_FAIL);
}
