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