// Minimal signal.h for the W65816 runtime.  No real signal handling
// — IIgs has no concept of POSIX signals.  signal() always returns
// SIG_ERR; raise() always returns -1.  These exist so portable code
// (e.g. asserts that map abort() through raise(SIGABRT)) compiles.

#ifndef _SIGNAL_H
#define _SIGNAL_H

#ifdef __cplusplus
extern "C" {
#endif

typedef int sig_atomic_t;

typedef void (*__sighandler_t)(int);

#define SIG_DFL ((__sighandler_t)0)
#define SIG_IGN ((__sighandler_t)1)
#define SIG_ERR ((__sighandler_t)-1)

#define SIGINT   2
#define SIGILL   4
#define SIGABRT  6
#define SIGFPE   8
#define SIGSEGV 11
#define SIGTERM 15
#define SIGALRM 14   // alarm-clock signal (GNO value; used by sleep/usleep)

__sighandler_t signal(int sig, __sighandler_t handler);
int            raise(int sig);

// ---- POSIX signal surface (GNO/ME kernel-backed; see libcGno.c) --------
#define NSIG   32   // GNO: signal numbers are 1..31 (mask is 1-32)

#define SIGHUP   1
#define SIGQUIT  3
#define SIGTRAP  5
#define SIGKILL  9
#define SIGBUS  10
#define SIGPIPE 13
#define SIGSTOP 17
#define SIGTSTP 18
#define SIGCONT 19
#define SIGCHLD 20
#define SIGTTIN 21
#define SIGTTOU 22
#define SIGUSR1 30
#define SIGUSR2 31

typedef unsigned long sigset_t;
typedef void (*sig_t)(int);

#define SIG_BLOCK    0
#define SIG_UNBLOCK  1
#define SIG_SETMASK  2

struct sigaction {
    void (*sa_handler)(int);
    sigset_t sa_mask;
    int      sa_flags;
};

int kill(int pid, int sig);
int sigaction(int sig, const struct sigaction *act, struct sigaction *oact);
int sigprocmask(int how, const sigset_t *set, sigset_t *oset);
int sigsuspend(const sigset_t *mask);
unsigned long sigblock(unsigned long mask);
unsigned long sigsetmask(unsigned long mask);

// Pure-C bitset ops (GNO defines these as macros, not functions).
#define sigemptyset(set)        (*(set) = 0, 0)
#define sigfillset(set)         (*(set) = ~(sigset_t)0, 0)
#define sigaddset(set, signo)   (*(set) |= (sigset_t)1 << ((signo) - 1), 0)
#define sigdelset(set, signo)   (*(set) &= ~((sigset_t)1 << ((signo) - 1)), 0)
#define sigismember(set, signo) ((*(set) & ((sigset_t)1 << ((signo) - 1))) != 0)

#ifdef __cplusplus
}
#endif

#endif
