1*6ca2c52aSchristos /* Extended support for using signal values.
2*6ca2c52aSchristos    Written by Fred Fish.  fnf@cygnus.com
3*6ca2c52aSchristos    This file is in the public domain.  */
4*6ca2c52aSchristos 
5*6ca2c52aSchristos #include "config.h"
6*6ca2c52aSchristos #include "ansidecl.h"
7*6ca2c52aSchristos #include "libiberty.h"
8*6ca2c52aSchristos 
9*6ca2c52aSchristos /* We need to declare sys_siglist, because even if the system provides
10*6ca2c52aSchristos    it we can't assume that it is declared in <signal.h> (for example,
11*6ca2c52aSchristos    SunOS provides sys_siglist, but it does not declare it in any
12*6ca2c52aSchristos    header file).  However, we can't declare sys_siglist portably,
13*6ca2c52aSchristos    because on some systems it is declared with const and on some
14*6ca2c52aSchristos    systems it is declared without const.  If we were using autoconf,
15*6ca2c52aSchristos    we could work out the right declaration.  Until, then we just
16*6ca2c52aSchristos    ignore any declaration in the system header files, and always
17*6ca2c52aSchristos    declare it ourselves.  With luck, this will always work.  */
18*6ca2c52aSchristos #define sys_siglist no_such_symbol
19*6ca2c52aSchristos #define sys_nsig sys_nsig__no_such_symbol
20*6ca2c52aSchristos 
21*6ca2c52aSchristos #include <stdio.h>
22*6ca2c52aSchristos #include <signal.h>
23*6ca2c52aSchristos 
24*6ca2c52aSchristos /*  Routines imported from standard C runtime libraries. */
25*6ca2c52aSchristos 
26*6ca2c52aSchristos #ifdef HAVE_STDLIB_H
27*6ca2c52aSchristos #include <stdlib.h>
28*6ca2c52aSchristos #else
29*6ca2c52aSchristos extern PTR malloc ();
30*6ca2c52aSchristos #endif
31*6ca2c52aSchristos 
32*6ca2c52aSchristos #ifdef HAVE_STRING_H
33*6ca2c52aSchristos #include <string.h>
34*6ca2c52aSchristos #else
35*6ca2c52aSchristos extern PTR memset ();
36*6ca2c52aSchristos #endif
37*6ca2c52aSchristos 
38*6ca2c52aSchristos /* Undefine the macro we used to hide the definition of sys_siglist
39*6ca2c52aSchristos    found in the system header files.  */
40*6ca2c52aSchristos #undef sys_siglist
41*6ca2c52aSchristos #undef sys_nsig
42*6ca2c52aSchristos 
43*6ca2c52aSchristos #ifndef NULL
44*6ca2c52aSchristos #  define NULL (void *) 0
45*6ca2c52aSchristos #endif
46*6ca2c52aSchristos 
47*6ca2c52aSchristos #ifndef MAX
48*6ca2c52aSchristos #  define MAX(a,b) ((a) > (b) ? (a) : (b))
49*6ca2c52aSchristos #endif
50*6ca2c52aSchristos 
51*6ca2c52aSchristos static void init_signal_tables (void);
52*6ca2c52aSchristos 
53*6ca2c52aSchristos /* Translation table for signal values.
54*6ca2c52aSchristos 
55*6ca2c52aSchristos    Note that this table is generally only accessed when it is used at runtime
56*6ca2c52aSchristos    to initialize signal name and message tables that are indexed by signal
57*6ca2c52aSchristos    value.
58*6ca2c52aSchristos 
59*6ca2c52aSchristos    Not all of these signals will exist on all systems.  This table is the only
60*6ca2c52aSchristos    thing that should have to be updated as new signal numbers are introduced.
61*6ca2c52aSchristos    It's sort of ugly, but at least its portable. */
62*6ca2c52aSchristos 
63*6ca2c52aSchristos struct signal_info
64*6ca2c52aSchristos {
65*6ca2c52aSchristos   const int value;		/* The numeric value from <signal.h> */
66*6ca2c52aSchristos   const char *const name;	/* The equivalent symbolic value */
67*6ca2c52aSchristos #ifndef HAVE_SYS_SIGLIST
68*6ca2c52aSchristos   const char *const msg;	/* Short message about this value */
69*6ca2c52aSchristos #endif
70*6ca2c52aSchristos };
71*6ca2c52aSchristos 
72*6ca2c52aSchristos #ifndef HAVE_SYS_SIGLIST
73*6ca2c52aSchristos #   define ENTRY(value, name, msg)	{value, name, msg}
74*6ca2c52aSchristos #else
75*6ca2c52aSchristos #   define ENTRY(value, name, msg)	{value, name}
76*6ca2c52aSchristos #endif
77*6ca2c52aSchristos 
78*6ca2c52aSchristos static const struct signal_info signal_table[] =
79*6ca2c52aSchristos {
80*6ca2c52aSchristos #if defined (SIGHUP)
81*6ca2c52aSchristos   ENTRY(SIGHUP, "SIGHUP", "Hangup"),
82*6ca2c52aSchristos #endif
83*6ca2c52aSchristos #if defined (SIGINT)
84*6ca2c52aSchristos   ENTRY(SIGINT, "SIGINT", "Interrupt"),
85*6ca2c52aSchristos #endif
86*6ca2c52aSchristos #if defined (SIGQUIT)
87*6ca2c52aSchristos   ENTRY(SIGQUIT, "SIGQUIT", "Quit"),
88*6ca2c52aSchristos #endif
89*6ca2c52aSchristos #if defined (SIGILL)
90*6ca2c52aSchristos   ENTRY(SIGILL, "SIGILL", "Illegal instruction"),
91*6ca2c52aSchristos #endif
92*6ca2c52aSchristos #if defined (SIGTRAP)
93*6ca2c52aSchristos   ENTRY(SIGTRAP, "SIGTRAP", "Trace/breakpoint trap"),
94*6ca2c52aSchristos #endif
95*6ca2c52aSchristos /* Put SIGIOT before SIGABRT, so that if SIGIOT==SIGABRT then SIGABRT
96*6ca2c52aSchristos    overrides SIGIOT.  SIGABRT is in ANSI and POSIX.1, and SIGIOT isn't. */
97*6ca2c52aSchristos #if defined (SIGIOT)
98*6ca2c52aSchristos   ENTRY(SIGIOT, "SIGIOT", "IOT trap"),
99*6ca2c52aSchristos #endif
100*6ca2c52aSchristos #if defined (SIGABRT)
101*6ca2c52aSchristos   ENTRY(SIGABRT, "SIGABRT", "Aborted"),
102*6ca2c52aSchristos #endif
103*6ca2c52aSchristos #if defined (SIGEMT)
104*6ca2c52aSchristos   ENTRY(SIGEMT, "SIGEMT", "Emulation trap"),
105*6ca2c52aSchristos #endif
106*6ca2c52aSchristos #if defined (SIGFPE)
107*6ca2c52aSchristos   ENTRY(SIGFPE, "SIGFPE", "Arithmetic exception"),
108*6ca2c52aSchristos #endif
109*6ca2c52aSchristos #if defined (SIGKILL)
110*6ca2c52aSchristos   ENTRY(SIGKILL, "SIGKILL", "Killed"),
111*6ca2c52aSchristos #endif
112*6ca2c52aSchristos #if defined (SIGBUS)
113*6ca2c52aSchristos   ENTRY(SIGBUS, "SIGBUS", "Bus error"),
114*6ca2c52aSchristos #endif
115*6ca2c52aSchristos #if defined (SIGSEGV)
116*6ca2c52aSchristos   ENTRY(SIGSEGV, "SIGSEGV", "Segmentation fault"),
117*6ca2c52aSchristos #endif
118*6ca2c52aSchristos #if defined (SIGSYS)
119*6ca2c52aSchristos   ENTRY(SIGSYS, "SIGSYS", "Bad system call"),
120*6ca2c52aSchristos #endif
121*6ca2c52aSchristos #if defined (SIGPIPE)
122*6ca2c52aSchristos   ENTRY(SIGPIPE, "SIGPIPE", "Broken pipe"),
123*6ca2c52aSchristos #endif
124*6ca2c52aSchristos #if defined (SIGALRM)
125*6ca2c52aSchristos   ENTRY(SIGALRM, "SIGALRM", "Alarm clock"),
126*6ca2c52aSchristos #endif
127*6ca2c52aSchristos #if defined (SIGTERM)
128*6ca2c52aSchristos   ENTRY(SIGTERM, "SIGTERM", "Terminated"),
129*6ca2c52aSchristos #endif
130*6ca2c52aSchristos #if defined (SIGUSR1)
131*6ca2c52aSchristos   ENTRY(SIGUSR1, "SIGUSR1", "User defined signal 1"),
132*6ca2c52aSchristos #endif
133*6ca2c52aSchristos #if defined (SIGUSR2)
134*6ca2c52aSchristos   ENTRY(SIGUSR2, "SIGUSR2", "User defined signal 2"),
135*6ca2c52aSchristos #endif
136*6ca2c52aSchristos /* Put SIGCLD before SIGCHLD, so that if SIGCLD==SIGCHLD then SIGCHLD
137*6ca2c52aSchristos    overrides SIGCLD.  SIGCHLD is in POXIX.1 */
138*6ca2c52aSchristos #if defined (SIGCLD)
139*6ca2c52aSchristos   ENTRY(SIGCLD, "SIGCLD", "Child status changed"),
140*6ca2c52aSchristos #endif
141*6ca2c52aSchristos #if defined (SIGCHLD)
142*6ca2c52aSchristos   ENTRY(SIGCHLD, "SIGCHLD", "Child status changed"),
143*6ca2c52aSchristos #endif
144*6ca2c52aSchristos #if defined (SIGPWR)
145*6ca2c52aSchristos   ENTRY(SIGPWR, "SIGPWR", "Power fail/restart"),
146*6ca2c52aSchristos #endif
147*6ca2c52aSchristos #if defined (SIGWINCH)
148*6ca2c52aSchristos   ENTRY(SIGWINCH, "SIGWINCH", "Window size changed"),
149*6ca2c52aSchristos #endif
150*6ca2c52aSchristos #if defined (SIGURG)
151*6ca2c52aSchristos   ENTRY(SIGURG, "SIGURG", "Urgent I/O condition"),
152*6ca2c52aSchristos #endif
153*6ca2c52aSchristos #if defined (SIGIO)
154*6ca2c52aSchristos   /* "I/O pending" has also been suggested, but is misleading since the
155*6ca2c52aSchristos      signal only happens when the process has asked for it, not everytime
156*6ca2c52aSchristos      I/O is pending. */
157*6ca2c52aSchristos   ENTRY(SIGIO, "SIGIO", "I/O possible"),
158*6ca2c52aSchristos #endif
159*6ca2c52aSchristos #if defined (SIGPOLL)
160*6ca2c52aSchristos   ENTRY(SIGPOLL, "SIGPOLL", "Pollable event occurred"),
161*6ca2c52aSchristos #endif
162*6ca2c52aSchristos #if defined (SIGSTOP)
163*6ca2c52aSchristos   ENTRY(SIGSTOP, "SIGSTOP", "Stopped (signal)"),
164*6ca2c52aSchristos #endif
165*6ca2c52aSchristos #if defined (SIGTSTP)
166*6ca2c52aSchristos   ENTRY(SIGTSTP, "SIGTSTP", "Stopped (user)"),
167*6ca2c52aSchristos #endif
168*6ca2c52aSchristos #if defined (SIGCONT)
169*6ca2c52aSchristos   ENTRY(SIGCONT, "SIGCONT", "Continued"),
170*6ca2c52aSchristos #endif
171*6ca2c52aSchristos #if defined (SIGTTIN)
172*6ca2c52aSchristos   ENTRY(SIGTTIN, "SIGTTIN", "Stopped (tty input)"),
173*6ca2c52aSchristos #endif
174*6ca2c52aSchristos #if defined (SIGTTOU)
175*6ca2c52aSchristos   ENTRY(SIGTTOU, "SIGTTOU", "Stopped (tty output)"),
176*6ca2c52aSchristos #endif
177*6ca2c52aSchristos #if defined (SIGVTALRM)
178*6ca2c52aSchristos   ENTRY(SIGVTALRM, "SIGVTALRM", "Virtual timer expired"),
179*6ca2c52aSchristos #endif
180*6ca2c52aSchristos #if defined (SIGPROF)
181*6ca2c52aSchristos   ENTRY(SIGPROF, "SIGPROF", "Profiling timer expired"),
182*6ca2c52aSchristos #endif
183*6ca2c52aSchristos #if defined (SIGXCPU)
184*6ca2c52aSchristos   ENTRY(SIGXCPU, "SIGXCPU", "CPU time limit exceeded"),
185*6ca2c52aSchristos #endif
186*6ca2c52aSchristos #if defined (SIGXFSZ)
187*6ca2c52aSchristos   ENTRY(SIGXFSZ, "SIGXFSZ", "File size limit exceeded"),
188*6ca2c52aSchristos #endif
189*6ca2c52aSchristos #if defined (SIGWIND)
190*6ca2c52aSchristos   ENTRY(SIGWIND, "SIGWIND", "SIGWIND"),
191*6ca2c52aSchristos #endif
192*6ca2c52aSchristos #if defined (SIGPHONE)
193*6ca2c52aSchristos   ENTRY(SIGPHONE, "SIGPHONE", "SIGPHONE"),
194*6ca2c52aSchristos #endif
195*6ca2c52aSchristos #if defined (SIGLOST)
196*6ca2c52aSchristos   ENTRY(SIGLOST, "SIGLOST", "Resource lost"),
197*6ca2c52aSchristos #endif
198*6ca2c52aSchristos #if defined (SIGWAITING)
199*6ca2c52aSchristos   ENTRY(SIGWAITING, "SIGWAITING", "Process's LWPs are blocked"),
200*6ca2c52aSchristos #endif
201*6ca2c52aSchristos #if defined (SIGLWP)
202*6ca2c52aSchristos   ENTRY(SIGLWP, "SIGLWP", "Signal LWP"),
203*6ca2c52aSchristos #endif
204*6ca2c52aSchristos #if defined (SIGDANGER)
205*6ca2c52aSchristos   ENTRY(SIGDANGER, "SIGDANGER", "Swap space dangerously low"),
206*6ca2c52aSchristos #endif
207*6ca2c52aSchristos #if defined (SIGGRANT)
208*6ca2c52aSchristos   ENTRY(SIGGRANT, "SIGGRANT", "Monitor mode granted"),
209*6ca2c52aSchristos #endif
210*6ca2c52aSchristos #if defined (SIGRETRACT)
211*6ca2c52aSchristos   ENTRY(SIGRETRACT, "SIGRETRACT", "Need to relinguish monitor mode"),
212*6ca2c52aSchristos #endif
213*6ca2c52aSchristos #if defined (SIGMSG)
214*6ca2c52aSchristos   ENTRY(SIGMSG, "SIGMSG", "Monitor mode data available"),
215*6ca2c52aSchristos #endif
216*6ca2c52aSchristos #if defined (SIGSOUND)
217*6ca2c52aSchristos   ENTRY(SIGSOUND, "SIGSOUND", "Sound completed"),
218*6ca2c52aSchristos #endif
219*6ca2c52aSchristos #if defined (SIGSAK)
220*6ca2c52aSchristos   ENTRY(SIGSAK, "SIGSAK", "Secure attention"),
221*6ca2c52aSchristos #endif
222*6ca2c52aSchristos   ENTRY(0, NULL, NULL)
223*6ca2c52aSchristos };
224*6ca2c52aSchristos 
225*6ca2c52aSchristos /* Translation table allocated and initialized at runtime.  Indexed by the
226*6ca2c52aSchristos    signal value to find the equivalent symbolic value. */
227*6ca2c52aSchristos 
228*6ca2c52aSchristos static const char **signal_names;
229*6ca2c52aSchristos static int num_signal_names = 0;
230*6ca2c52aSchristos 
231*6ca2c52aSchristos /* Translation table allocated and initialized at runtime, if it does not
232*6ca2c52aSchristos    already exist in the host environment.  Indexed by the signal value to find
233*6ca2c52aSchristos    the descriptive string.
234*6ca2c52aSchristos 
235*6ca2c52aSchristos    We don't export it for use in other modules because even though it has the
236*6ca2c52aSchristos    same name, it differs from other implementations in that it is dynamically
237*6ca2c52aSchristos    initialized rather than statically initialized. */
238*6ca2c52aSchristos 
239*6ca2c52aSchristos #ifndef HAVE_SYS_SIGLIST
240*6ca2c52aSchristos 
241*6ca2c52aSchristos static int sys_nsig;
242*6ca2c52aSchristos static const char **sys_siglist;
243*6ca2c52aSchristos 
244*6ca2c52aSchristos #else
245*6ca2c52aSchristos 
246*6ca2c52aSchristos #ifdef NSIG
247*6ca2c52aSchristos static int sys_nsig = NSIG;
248*6ca2c52aSchristos #else
249*6ca2c52aSchristos #ifdef _NSIG
250*6ca2c52aSchristos static int sys_nsig = _NSIG;
251*6ca2c52aSchristos #endif
252*6ca2c52aSchristos #endif
253*6ca2c52aSchristos extern const char * const sys_siglist[];
254*6ca2c52aSchristos 
255*6ca2c52aSchristos #endif
256*6ca2c52aSchristos 
257*6ca2c52aSchristos 
258*6ca2c52aSchristos /*
259*6ca2c52aSchristos 
260*6ca2c52aSchristos NAME
261*6ca2c52aSchristos 
262*6ca2c52aSchristos 	init_signal_tables -- initialize the name and message tables
263*6ca2c52aSchristos 
264*6ca2c52aSchristos SYNOPSIS
265*6ca2c52aSchristos 
266*6ca2c52aSchristos 	static void init_signal_tables ();
267*6ca2c52aSchristos 
268*6ca2c52aSchristos DESCRIPTION
269*6ca2c52aSchristos 
270*6ca2c52aSchristos 	Using the signal_table, which is initialized at compile time, generate
271*6ca2c52aSchristos 	the signal_names and the sys_siglist (if needed) tables, which are
272*6ca2c52aSchristos 	indexed at runtime by a specific signal value.
273*6ca2c52aSchristos 
274*6ca2c52aSchristos BUGS
275*6ca2c52aSchristos 
276*6ca2c52aSchristos 	The initialization of the tables may fail under low memory conditions,
277*6ca2c52aSchristos 	in which case we don't do anything particularly useful, but we don't
278*6ca2c52aSchristos 	bomb either.  Who knows, it might succeed at a later point if we free
279*6ca2c52aSchristos 	some memory in the meantime.  In any case, the other routines know
280*6ca2c52aSchristos 	how to deal with lack of a table after trying to initialize it.  This
281*6ca2c52aSchristos 	may or may not be considered to be a bug, that we don't specifically
282*6ca2c52aSchristos 	warn about this particular failure mode.
283*6ca2c52aSchristos 
284*6ca2c52aSchristos */
285*6ca2c52aSchristos 
286*6ca2c52aSchristos static void
init_signal_tables(void)287*6ca2c52aSchristos init_signal_tables (void)
288*6ca2c52aSchristos {
289*6ca2c52aSchristos   const struct signal_info *eip;
290*6ca2c52aSchristos   int nbytes;
291*6ca2c52aSchristos 
292*6ca2c52aSchristos   /* If we haven't already scanned the signal_table once to find the maximum
293*6ca2c52aSchristos      signal value, then go find it now. */
294*6ca2c52aSchristos 
295*6ca2c52aSchristos   if (num_signal_names == 0)
296*6ca2c52aSchristos     {
297*6ca2c52aSchristos       for (eip = signal_table; eip -> name != NULL; eip++)
298*6ca2c52aSchristos 	{
299*6ca2c52aSchristos 	  if (eip -> value >= num_signal_names)
300*6ca2c52aSchristos 	    {
301*6ca2c52aSchristos 	      num_signal_names = eip -> value + 1;
302*6ca2c52aSchristos 	    }
303*6ca2c52aSchristos 	}
304*6ca2c52aSchristos     }
305*6ca2c52aSchristos 
306*6ca2c52aSchristos   /* Now attempt to allocate the signal_names table, zero it out, and then
307*6ca2c52aSchristos      initialize it from the statically initialized signal_table. */
308*6ca2c52aSchristos 
309*6ca2c52aSchristos   if (signal_names == NULL)
310*6ca2c52aSchristos     {
311*6ca2c52aSchristos       nbytes = num_signal_names * sizeof (char *);
312*6ca2c52aSchristos       if ((signal_names = (const char **) malloc (nbytes)) != NULL)
313*6ca2c52aSchristos 	{
314*6ca2c52aSchristos 	  memset (signal_names, 0, nbytes);
315*6ca2c52aSchristos 	  for (eip = signal_table; eip -> name != NULL; eip++)
316*6ca2c52aSchristos 	    {
317*6ca2c52aSchristos 	      signal_names[eip -> value] = eip -> name;
318*6ca2c52aSchristos 	    }
319*6ca2c52aSchristos 	}
320*6ca2c52aSchristos     }
321*6ca2c52aSchristos 
322*6ca2c52aSchristos #ifndef HAVE_SYS_SIGLIST
323*6ca2c52aSchristos 
324*6ca2c52aSchristos   /* Now attempt to allocate the sys_siglist table, zero it out, and then
325*6ca2c52aSchristos      initialize it from the statically initialized signal_table. */
326*6ca2c52aSchristos 
327*6ca2c52aSchristos   if (sys_siglist == NULL)
328*6ca2c52aSchristos     {
329*6ca2c52aSchristos       nbytes = num_signal_names * sizeof (char *);
330*6ca2c52aSchristos       if ((sys_siglist = (const char **) malloc (nbytes)) != NULL)
331*6ca2c52aSchristos 	{
332*6ca2c52aSchristos 	  memset (sys_siglist, 0, nbytes);
333*6ca2c52aSchristos 	  sys_nsig = num_signal_names;
334*6ca2c52aSchristos 	  for (eip = signal_table; eip -> name != NULL; eip++)
335*6ca2c52aSchristos 	    {
336*6ca2c52aSchristos 	      sys_siglist[eip -> value] = eip -> msg;
337*6ca2c52aSchristos 	    }
338*6ca2c52aSchristos 	}
339*6ca2c52aSchristos     }
340*6ca2c52aSchristos 
341*6ca2c52aSchristos #endif
342*6ca2c52aSchristos 
343*6ca2c52aSchristos }
344*6ca2c52aSchristos 
345*6ca2c52aSchristos 
346*6ca2c52aSchristos /*
347*6ca2c52aSchristos 
348*6ca2c52aSchristos @deftypefn Extension int signo_max (void)
349*6ca2c52aSchristos 
350*6ca2c52aSchristos Returns the maximum signal value for which a corresponding symbolic
351*6ca2c52aSchristos name or message is available.  Note that in the case where we use the
352*6ca2c52aSchristos @code{sys_siglist} supplied by the system, it is possible for there to
353*6ca2c52aSchristos be more symbolic names than messages, or vice versa.  In fact, the
354*6ca2c52aSchristos manual page for @code{psignal(3b)} explicitly warns that one should
355*6ca2c52aSchristos check the size of the table (@code{NSIG}) before indexing it, since
356*6ca2c52aSchristos new signal codes may be added to the system before they are added to
357*6ca2c52aSchristos the table.  Thus @code{NSIG} might be smaller than value implied by
358*6ca2c52aSchristos the largest signo value defined in @code{<signal.h>}.
359*6ca2c52aSchristos 
360*6ca2c52aSchristos We return the maximum value that can be used to obtain a meaningful
361*6ca2c52aSchristos symbolic name or message.
362*6ca2c52aSchristos 
363*6ca2c52aSchristos @end deftypefn
364*6ca2c52aSchristos 
365*6ca2c52aSchristos */
366*6ca2c52aSchristos 
367*6ca2c52aSchristos int
signo_max(void)368*6ca2c52aSchristos signo_max (void)
369*6ca2c52aSchristos {
370*6ca2c52aSchristos   int maxsize;
371*6ca2c52aSchristos 
372*6ca2c52aSchristos   if (signal_names == NULL)
373*6ca2c52aSchristos     {
374*6ca2c52aSchristos       init_signal_tables ();
375*6ca2c52aSchristos     }
376*6ca2c52aSchristos   maxsize = MAX (sys_nsig, num_signal_names);
377*6ca2c52aSchristos   return (maxsize - 1);
378*6ca2c52aSchristos }
379*6ca2c52aSchristos 
380*6ca2c52aSchristos 
381*6ca2c52aSchristos /*
382*6ca2c52aSchristos 
383*6ca2c52aSchristos @deftypefn Supplemental {const char *} strsignal (int @var{signo})
384*6ca2c52aSchristos 
385*6ca2c52aSchristos Maps an signal number to an signal message string, the contents of
386*6ca2c52aSchristos which are implementation defined.  On systems which have the external
387*6ca2c52aSchristos variable @code{sys_siglist}, these strings will be the same as the
388*6ca2c52aSchristos ones used by @code{psignal()}.
389*6ca2c52aSchristos 
390*6ca2c52aSchristos If the supplied signal number is within the valid range of indices for
391*6ca2c52aSchristos the @code{sys_siglist}, but no message is available for the particular
392*6ca2c52aSchristos signal number, then returns the string @samp{Signal @var{num}}, where
393*6ca2c52aSchristos @var{num} is the signal number.
394*6ca2c52aSchristos 
395*6ca2c52aSchristos If the supplied signal number is not a valid index into
396*6ca2c52aSchristos @code{sys_siglist}, returns @code{NULL}.
397*6ca2c52aSchristos 
398*6ca2c52aSchristos The returned string is only guaranteed to be valid only until the next
399*6ca2c52aSchristos call to @code{strsignal}.
400*6ca2c52aSchristos 
401*6ca2c52aSchristos @end deftypefn
402*6ca2c52aSchristos 
403*6ca2c52aSchristos */
404*6ca2c52aSchristos 
405*6ca2c52aSchristos #ifndef HAVE_STRSIGNAL
406*6ca2c52aSchristos 
407*6ca2c52aSchristos char *
strsignal(int signo)408*6ca2c52aSchristos strsignal (int signo)
409*6ca2c52aSchristos {
410*6ca2c52aSchristos   char *msg;
411*6ca2c52aSchristos   static char buf[32];
412*6ca2c52aSchristos 
413*6ca2c52aSchristos #ifndef HAVE_SYS_SIGLIST
414*6ca2c52aSchristos 
415*6ca2c52aSchristos   if (signal_names == NULL)
416*6ca2c52aSchristos     {
417*6ca2c52aSchristos       init_signal_tables ();
418*6ca2c52aSchristos     }
419*6ca2c52aSchristos 
420*6ca2c52aSchristos #endif
421*6ca2c52aSchristos 
422*6ca2c52aSchristos   if ((signo < 0) || (signo >= sys_nsig))
423*6ca2c52aSchristos     {
424*6ca2c52aSchristos       /* Out of range, just return NULL */
425*6ca2c52aSchristos       msg = NULL;
426*6ca2c52aSchristos     }
427*6ca2c52aSchristos   else if ((sys_siglist == NULL) || (sys_siglist[signo] == NULL))
428*6ca2c52aSchristos     {
429*6ca2c52aSchristos       /* In range, but no sys_siglist or no entry at this index. */
430*6ca2c52aSchristos       sprintf (buf, "Signal %d", signo);
431*6ca2c52aSchristos       msg = buf;
432*6ca2c52aSchristos     }
433*6ca2c52aSchristos   else
434*6ca2c52aSchristos     {
435*6ca2c52aSchristos       /* In range, and a valid message.  Just return the message.  We
436*6ca2c52aSchristos 	 can safely cast away const, since POSIX says the user must
437*6ca2c52aSchristos 	 not modify the result.	 */
438*6ca2c52aSchristos       msg = (char *) sys_siglist[signo];
439*6ca2c52aSchristos     }
440*6ca2c52aSchristos 
441*6ca2c52aSchristos   return (msg);
442*6ca2c52aSchristos }
443*6ca2c52aSchristos 
444*6ca2c52aSchristos #endif /* ! HAVE_STRSIGNAL */
445*6ca2c52aSchristos 
446*6ca2c52aSchristos /*
447*6ca2c52aSchristos 
448*6ca2c52aSchristos @deftypefn Extension {const char*} strsigno (int @var{signo})
449*6ca2c52aSchristos 
450*6ca2c52aSchristos Given an signal number, returns a pointer to a string containing the
451*6ca2c52aSchristos symbolic name of that signal number, as found in @code{<signal.h>}.
452*6ca2c52aSchristos 
453*6ca2c52aSchristos If the supplied signal number is within the valid range of indices for
454*6ca2c52aSchristos symbolic names, but no name is available for the particular signal
455*6ca2c52aSchristos number, then returns the string @samp{Signal @var{num}}, where
456*6ca2c52aSchristos @var{num} is the signal number.
457*6ca2c52aSchristos 
458*6ca2c52aSchristos If the supplied signal number is not within the range of valid
459*6ca2c52aSchristos indices, then returns @code{NULL}.
460*6ca2c52aSchristos 
461*6ca2c52aSchristos The contents of the location pointed to are only guaranteed to be
462*6ca2c52aSchristos valid until the next call to @code{strsigno}.
463*6ca2c52aSchristos 
464*6ca2c52aSchristos @end deftypefn
465*6ca2c52aSchristos 
466*6ca2c52aSchristos */
467*6ca2c52aSchristos 
468*6ca2c52aSchristos const char *
strsigno(int signo)469*6ca2c52aSchristos strsigno (int signo)
470*6ca2c52aSchristos {
471*6ca2c52aSchristos   const char *name;
472*6ca2c52aSchristos   static char buf[32];
473*6ca2c52aSchristos 
474*6ca2c52aSchristos   if (signal_names == NULL)
475*6ca2c52aSchristos     {
476*6ca2c52aSchristos       init_signal_tables ();
477*6ca2c52aSchristos     }
478*6ca2c52aSchristos 
479*6ca2c52aSchristos   if ((signo < 0) || (signo >= num_signal_names))
480*6ca2c52aSchristos     {
481*6ca2c52aSchristos       /* Out of range, just return NULL */
482*6ca2c52aSchristos       name = NULL;
483*6ca2c52aSchristos     }
484*6ca2c52aSchristos   else if ((signal_names == NULL) || (signal_names[signo] == NULL))
485*6ca2c52aSchristos     {
486*6ca2c52aSchristos       /* In range, but no signal_names or no entry at this index. */
487*6ca2c52aSchristos       sprintf (buf, "Signal %d", signo);
488*6ca2c52aSchristos       name = (const char *) buf;
489*6ca2c52aSchristos     }
490*6ca2c52aSchristos   else
491*6ca2c52aSchristos     {
492*6ca2c52aSchristos       /* In range, and a valid name.  Just return the name. */
493*6ca2c52aSchristos       name = signal_names[signo];
494*6ca2c52aSchristos     }
495*6ca2c52aSchristos 
496*6ca2c52aSchristos   return (name);
497*6ca2c52aSchristos }
498*6ca2c52aSchristos 
499*6ca2c52aSchristos 
500*6ca2c52aSchristos /*
501*6ca2c52aSchristos 
502*6ca2c52aSchristos @deftypefn Extension int strtosigno (const char *@var{name})
503*6ca2c52aSchristos 
504*6ca2c52aSchristos Given the symbolic name of a signal, map it to a signal number.  If no
505*6ca2c52aSchristos translation is found, returns 0.
506*6ca2c52aSchristos 
507*6ca2c52aSchristos @end deftypefn
508*6ca2c52aSchristos 
509*6ca2c52aSchristos */
510*6ca2c52aSchristos 
511*6ca2c52aSchristos int
strtosigno(const char * name)512*6ca2c52aSchristos strtosigno (const char *name)
513*6ca2c52aSchristos {
514*6ca2c52aSchristos   int signo = 0;
515*6ca2c52aSchristos 
516*6ca2c52aSchristos   if (name != NULL)
517*6ca2c52aSchristos     {
518*6ca2c52aSchristos       if (signal_names == NULL)
519*6ca2c52aSchristos 	{
520*6ca2c52aSchristos 	  init_signal_tables ();
521*6ca2c52aSchristos 	}
522*6ca2c52aSchristos       for (signo = 0; signo < num_signal_names; signo++)
523*6ca2c52aSchristos 	{
524*6ca2c52aSchristos 	  if ((signal_names[signo] != NULL) &&
525*6ca2c52aSchristos 	      (strcmp (name, signal_names[signo]) == 0))
526*6ca2c52aSchristos 	    {
527*6ca2c52aSchristos 	      break;
528*6ca2c52aSchristos 	    }
529*6ca2c52aSchristos 	}
530*6ca2c52aSchristos       if (signo == num_signal_names)
531*6ca2c52aSchristos 	{
532*6ca2c52aSchristos 	  signo = 0;
533*6ca2c52aSchristos 	}
534*6ca2c52aSchristos     }
535*6ca2c52aSchristos   return (signo);
536*6ca2c52aSchristos }
537*6ca2c52aSchristos 
538*6ca2c52aSchristos 
539*6ca2c52aSchristos /*
540*6ca2c52aSchristos 
541*6ca2c52aSchristos @deftypefn Supplemental void psignal (int @var{signo}, char *@var{message})
542*6ca2c52aSchristos 
543*6ca2c52aSchristos Print @var{message} to the standard error, followed by a colon,
544*6ca2c52aSchristos followed by the description of the signal specified by @var{signo},
545*6ca2c52aSchristos followed by a newline.
546*6ca2c52aSchristos 
547*6ca2c52aSchristos @end deftypefn
548*6ca2c52aSchristos 
549*6ca2c52aSchristos */
550*6ca2c52aSchristos 
551*6ca2c52aSchristos #ifndef HAVE_PSIGNAL
552*6ca2c52aSchristos 
553*6ca2c52aSchristos void
psignal(int signo,char * message)554*6ca2c52aSchristos psignal (int signo, char *message)
555*6ca2c52aSchristos {
556*6ca2c52aSchristos   if (signal_names == NULL)
557*6ca2c52aSchristos     {
558*6ca2c52aSchristos       init_signal_tables ();
559*6ca2c52aSchristos     }
560*6ca2c52aSchristos   if ((signo <= 0) || (signo >= sys_nsig))
561*6ca2c52aSchristos     {
562*6ca2c52aSchristos       fprintf (stderr, "%s: unknown signal\n", message);
563*6ca2c52aSchristos     }
564*6ca2c52aSchristos   else
565*6ca2c52aSchristos     {
566*6ca2c52aSchristos       fprintf (stderr, "%s: %s\n", message, sys_siglist[signo]);
567*6ca2c52aSchristos     }
568*6ca2c52aSchristos }
569*6ca2c52aSchristos 
570*6ca2c52aSchristos #endif	/* ! HAVE_PSIGNAL */
571*6ca2c52aSchristos 
572*6ca2c52aSchristos 
573*6ca2c52aSchristos /* A simple little main that does nothing but print all the signal translations
574*6ca2c52aSchristos    if MAIN is defined and this file is compiled and linked. */
575*6ca2c52aSchristos 
576*6ca2c52aSchristos #ifdef MAIN
577*6ca2c52aSchristos 
578*6ca2c52aSchristos #include <stdio.h>
579*6ca2c52aSchristos 
580*6ca2c52aSchristos int
main(void)581*6ca2c52aSchristos main (void)
582*6ca2c52aSchristos {
583*6ca2c52aSchristos   int signo;
584*6ca2c52aSchristos   int maxsigno;
585*6ca2c52aSchristos   const char *name;
586*6ca2c52aSchristos   const char *msg;
587*6ca2c52aSchristos 
588*6ca2c52aSchristos   maxsigno = signo_max ();
589*6ca2c52aSchristos   printf ("%d entries in names table.\n", num_signal_names);
590*6ca2c52aSchristos   printf ("%d entries in messages table.\n", sys_nsig);
591*6ca2c52aSchristos   printf ("%d is max useful index.\n", maxsigno);
592*6ca2c52aSchristos 
593*6ca2c52aSchristos   /* Keep printing values until we get to the end of *both* tables, not
594*6ca2c52aSchristos      *either* table.  Note that knowing the maximum useful index does *not*
595*6ca2c52aSchristos      relieve us of the responsibility of testing the return pointer for
596*6ca2c52aSchristos      NULL. */
597*6ca2c52aSchristos 
598*6ca2c52aSchristos   for (signo = 0; signo <= maxsigno; signo++)
599*6ca2c52aSchristos     {
600*6ca2c52aSchristos       name = strsigno (signo);
601*6ca2c52aSchristos       name = (name == NULL) ? "<NULL>" : name;
602*6ca2c52aSchristos       msg = strsignal (signo);
603*6ca2c52aSchristos       msg = (msg == NULL) ? "<NULL>" : msg;
604*6ca2c52aSchristos       printf ("%-4d%-18s%s\n", signo, name, msg);
605*6ca2c52aSchristos     }
606*6ca2c52aSchristos 
607*6ca2c52aSchristos   return 0;
608*6ca2c52aSchristos }
609*6ca2c52aSchristos 
610*6ca2c52aSchristos #endif
611