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