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