#include "config.h"
#include "ansidecl.h"
#include "libiberty.h"
#define sys_siglist no_such_symbol
#define sys_nsig sys_nsig__no_such_symbol
#include <stdio.h>
#include <signal.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#else
extern PTR malloc ();
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#else
extern PTR memset ();
#endif
#undef sys_siglist
#undef sys_nsig
#ifndef NULL
# define NULL (void *) 0
#endif
#ifndef MAX
# define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
static void init_signal_tables (void);
struct signal_info
{
const int value;
const char *const name;
#ifndef HAVE_SYS_SIGLIST
const char *const msg;
#endif
};
#ifndef HAVE_SYS_SIGLIST
# define ENTRY(value, name, msg) {value, name, msg}
#else
# define ENTRY(value, name, msg) {value, name}
#endif
static const struct signal_info signal_table[] =
{
#if defined (SIGHUP)
ENTRY(SIGHUP, "SIGHUP", "Hangup"),
#endif
#if defined (SIGINT)
ENTRY(SIGINT, "SIGINT", "Interrupt"),
#endif
#if defined (SIGQUIT)
ENTRY(SIGQUIT, "SIGQUIT", "Quit"),
#endif
#if defined (SIGILL)
ENTRY(SIGILL, "SIGILL", "Illegal instruction"),
#endif
#if defined (SIGTRAP)
ENTRY(SIGTRAP, "SIGTRAP", "Trace/breakpoint trap"),
#endif
#if defined (SIGIOT)
ENTRY(SIGIOT, "SIGIOT", "IOT trap"),
#endif
#if defined (SIGABRT)
ENTRY(SIGABRT, "SIGABRT", "Aborted"),
#endif
#if defined (SIGEMT)
ENTRY(SIGEMT, "SIGEMT", "Emulation trap"),
#endif
#if defined (SIGFPE)
ENTRY(SIGFPE, "SIGFPE", "Arithmetic exception"),
#endif
#if defined (SIGKILL)
ENTRY(SIGKILL, "SIGKILL", "Killed"),
#endif
#if defined (SIGBUS)
ENTRY(SIGBUS, "SIGBUS", "Bus error"),
#endif
#if defined (SIGSEGV)
ENTRY(SIGSEGV, "SIGSEGV", "Segmentation fault"),
#endif
#if defined (SIGSYS)
ENTRY(SIGSYS, "SIGSYS", "Bad system call"),
#endif
#if defined (SIGPIPE)
ENTRY(SIGPIPE, "SIGPIPE", "Broken pipe"),
#endif
#if defined (SIGALRM)
ENTRY(SIGALRM, "SIGALRM", "Alarm clock"),
#endif
#if defined (SIGTERM)
ENTRY(SIGTERM, "SIGTERM", "Terminated"),
#endif
#if defined (SIGUSR1)
ENTRY(SIGUSR1, "SIGUSR1", "User defined signal 1"),
#endif
#if defined (SIGUSR2)
ENTRY(SIGUSR2, "SIGUSR2", "User defined signal 2"),
#endif
#if defined (SIGCLD)
ENTRY(SIGCLD, "SIGCLD", "Child status changed"),
#endif
#if defined (SIGCHLD)
ENTRY(SIGCHLD, "SIGCHLD", "Child status changed"),
#endif
#if defined (SIGPWR)
ENTRY(SIGPWR, "SIGPWR", "Power fail/restart"),
#endif
#if defined (SIGWINCH)
ENTRY(SIGWINCH, "SIGWINCH", "Window size changed"),
#endif
#if defined (SIGURG)
ENTRY(SIGURG, "SIGURG", "Urgent I/O condition"),
#endif
#if defined (SIGIO)
ENTRY(SIGIO, "SIGIO", "I/O possible"),
#endif
#if defined (SIGPOLL)
ENTRY(SIGPOLL, "SIGPOLL", "Pollable event occurred"),
#endif
#if defined (SIGSTOP)
ENTRY(SIGSTOP, "SIGSTOP", "Stopped (signal)"),
#endif
#if defined (SIGTSTP)
ENTRY(SIGTSTP, "SIGTSTP", "Stopped (user)"),
#endif
#if defined (SIGCONT)
ENTRY(SIGCONT, "SIGCONT", "Continued"),
#endif
#if defined (SIGTTIN)
ENTRY(SIGTTIN, "SIGTTIN", "Stopped (tty input)"),
#endif
#if defined (SIGTTOU)
ENTRY(SIGTTOU, "SIGTTOU", "Stopped (tty output)"),
#endif
#if defined (SIGVTALRM)
ENTRY(SIGVTALRM, "SIGVTALRM", "Virtual timer expired"),
#endif
#if defined (SIGPROF)
ENTRY(SIGPROF, "SIGPROF", "Profiling timer expired"),
#endif
#if defined (SIGXCPU)
ENTRY(SIGXCPU, "SIGXCPU", "CPU time limit exceeded"),
#endif
#if defined (SIGXFSZ)
ENTRY(SIGXFSZ, "SIGXFSZ", "File size limit exceeded"),
#endif
#if defined (SIGWIND)
ENTRY(SIGWIND, "SIGWIND", "SIGWIND"),
#endif
#if defined (SIGPHONE)
ENTRY(SIGPHONE, "SIGPHONE", "SIGPHONE"),
#endif
#if defined (SIGLOST)
ENTRY(SIGLOST, "SIGLOST", "Resource lost"),
#endif
#if defined (SIGWAITING)
ENTRY(SIGWAITING, "SIGWAITING", "Process's LWPs are blocked"),
#endif
#if defined (SIGLWP)
ENTRY(SIGLWP, "SIGLWP", "Signal LWP"),
#endif
#if defined (SIGDANGER)
ENTRY(SIGDANGER, "SIGDANGER", "Swap space dangerously low"),
#endif
#if defined (SIGGRANT)
ENTRY(SIGGRANT, "SIGGRANT", "Monitor mode granted"),
#endif
#if defined (SIGRETRACT)
ENTRY(SIGRETRACT, "SIGRETRACT", "Need to relinguish monitor mode"),
#endif
#if defined (SIGMSG)
ENTRY(SIGMSG, "SIGMSG", "Monitor mode data available"),
#endif
#if defined (SIGSOUND)
ENTRY(SIGSOUND, "SIGSOUND", "Sound completed"),
#endif
#if defined (SIGSAK)
ENTRY(SIGSAK, "SIGSAK", "Secure attention"),
#endif
ENTRY(0, NULL, NULL)
};
static const char **signal_names;
static int num_signal_names = 0;
#ifndef HAVE_SYS_SIGLIST
static int sys_nsig;
static const char **sys_siglist;
#else
#ifdef NSIG
static int sys_nsig = NSIG;
#else
#ifdef _NSIG
static int sys_nsig = _NSIG;
#endif
#endif
extern const char * const sys_siglist[];
#endif
static void
init_signal_tables (void)
{
const struct signal_info *eip;
int nbytes;
if (num_signal_names == 0)
{
for (eip = signal_table; eip -> name != NULL; eip++)
{
if (eip -> value >= num_signal_names)
{
num_signal_names = eip -> value + 1;
}
}
}
if (signal_names == NULL)
{
nbytes = num_signal_names * sizeof (char *);
if ((signal_names = (const char **) malloc (nbytes)) != NULL)
{
memset (signal_names, 0, nbytes);
for (eip = signal_table; eip -> name != NULL; eip++)
{
signal_names[eip -> value] = eip -> name;
}
}
}
#ifndef HAVE_SYS_SIGLIST
if (sys_siglist == NULL)
{
nbytes = num_signal_names * sizeof (char *);
if ((sys_siglist = (const char **) malloc (nbytes)) != NULL)
{
memset (sys_siglist, 0, nbytes);
sys_nsig = num_signal_names;
for (eip = signal_table; eip -> name != NULL; eip++)
{
sys_siglist[eip -> value] = eip -> msg;
}
}
}
#endif
}
int
signo_max (void)
{
int maxsize;
if (signal_names == NULL)
{
init_signal_tables ();
}
maxsize = MAX (sys_nsig, num_signal_names);
return (maxsize - 1);
}
#ifndef HAVE_STRSIGNAL
const char *
strsignal (int signo)
{
const char *msg;
static char buf[32];
#ifndef HAVE_SYS_SIGLIST
if (signal_names == NULL)
{
init_signal_tables ();
}
#endif
if ((signo < 0) || (signo >= sys_nsig))
{
msg = NULL;
}
else if ((sys_siglist == NULL) || (sys_siglist[signo] == NULL))
{
sprintf (buf, "Signal %d", signo);
msg = (const char *) buf;
}
else
{
msg = (const char *) sys_siglist[signo];
}
return (msg);
}
#endif
const char *
strsigno (int signo)
{
const char *name;
static char buf[32];
if (signal_names == NULL)
{
init_signal_tables ();
}
if ((signo < 0) || (signo >= num_signal_names))
{
name = NULL;
}
else if ((signal_names == NULL) || (signal_names[signo] == NULL))
{
sprintf (buf, "Signal %d", signo);
name = (const char *) buf;
}
else
{
name = signal_names[signo];
}
return (name);
}
int
strtosigno (const char *name)
{
int signo = 0;
if (name != NULL)
{
if (signal_names == NULL)
{
init_signal_tables ();
}
for (signo = 0; signo < num_signal_names; signo++)
{
if ((signal_names[signo] != NULL) &&
(strcmp (name, signal_names[signo]) == 0))
{
break;
}
}
if (signo == num_signal_names)
{
signo = 0;
}
}
return (signo);
}
#ifndef HAVE_PSIGNAL
void
psignal (unsigned signo, char *message)
{
if (signal_names == NULL)
{
init_signal_tables ();
}
if ((signo <= 0) || (signo >= sys_nsig))
{
fprintf (stderr, "%s: unknown signal\n", message);
}
else
{
fprintf (stderr, "%s: %s\n", message, sys_siglist[signo]);
}
}
#endif
#ifdef MAIN
#include <stdio.h>
int
main (void)
{
int signo;
int maxsigno;
const char *name;
const char *msg;
maxsigno = signo_max ();
printf ("%d entries in names table.\n", num_signal_names);
printf ("%d entries in messages table.\n", sys_nsig);
printf ("%d is max useful index.\n", maxsigno);
for (signo = 0; signo <= maxsigno; signo++)
{
name = strsigno (signo);
name = (name == NULL) ? "<NULL>" : name;
msg = strsignal (signo);
msg = (msg == NULL) ? "<NULL>" : msg;
printf ("%-4d%-18s%s\n", signo, name, msg);
}
return 0;
}
#endif