1/* Convert between signal names and numbers.
2Copyright (C) 1990,92,93,95,96,99, 2002 Free Software Foundation, Inc.
3This file was part of the GNU C Library, but is now part of GNU make.
4
5GNU Make is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2, or (at your option)
8any later version.
9
10GNU Make is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with GNU Make; see the file COPYING.  If not, write to
17the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18Boston, MA 02111-1307, USA.  */
19
20#include "make.h"
21
22/* If the system provides strsignal, we don't need it. */
23
24#if !defined(HAVE_STRSIGNAL)
25
26/* If the system provides sys_siglist, we'll use that.
27   Otherwise create our own.
28 */
29
30#if !defined(SYS_SIGLIST_DECLARED)
31
32/* Some systems do not define NSIG in <signal.h>.  */
33#ifndef	NSIG
34#ifdef	_NSIG
35#define	NSIG	_NSIG
36#else
37#define	NSIG	32
38#endif
39#endif
40
41/* There is too much variation in Sys V signal numbers and names, so
42   we must initialize them at runtime.  */
43
44static const char *undoc;
45
46static const char *sys_siglist[NSIG];
47
48/* Table of abbreviations for signals.  Note:  A given number can
49   appear more than once with different abbreviations.  */
50#define SIG_TABLE_SIZE  (NSIG*2)
51
52typedef struct
53  {
54    int number;
55    const char *abbrev;
56  } num_abbrev;
57
58static num_abbrev sig_table[SIG_TABLE_SIZE];
59
60/* Number of elements of sig_table used.  */
61static int sig_table_nelts = 0;
62
63/* Enter signal number NUMBER into the tables with ABBREV and NAME.  */
64
65static void
66init_sig (number, abbrev, name)
67     int number;
68     const char *abbrev;
69     const char *name;
70{
71  /* If this value is ever greater than NSIG it seems like it'd be a bug in
72     the system headers, but... better safe than sorry.  We know, for
73     example, that this isn't always true on VMS.  */
74
75  if (number >= 0 && number < NSIG)
76    sys_siglist[number] = name;
77
78  if (sig_table_nelts < SIG_TABLE_SIZE)
79    {
80      sig_table[sig_table_nelts].number = number;
81      sig_table[sig_table_nelts++].abbrev = abbrev;
82    }
83}
84
85static int
86signame_init ()
87{
88  int i;
89
90  undoc = xstrdup (_("unknown signal"));
91
92  /* Initialize signal names.  */
93  for (i = 0; i < NSIG; i++)
94    sys_siglist[i] = undoc;
95
96  /* Initialize signal names.  */
97#if defined (SIGHUP)
98  init_sig (SIGHUP, "HUP", _("Hangup"));
99#endif
100#if defined (SIGINT)
101  init_sig (SIGINT, "INT", _("Interrupt"));
102#endif
103#if defined (SIGQUIT)
104  init_sig (SIGQUIT, "QUIT", _("Quit"));
105#endif
106#if defined (SIGILL)
107  init_sig (SIGILL, "ILL", _("Illegal Instruction"));
108#endif
109#if defined (SIGTRAP)
110  init_sig (SIGTRAP, "TRAP", _("Trace/breakpoint trap"));
111#endif
112  /* If SIGIOT == SIGABRT, we want to print it as SIGABRT because
113     SIGABRT is in ANSI and POSIX.1 and SIGIOT isn't.  */
114#if defined (SIGABRT)
115  init_sig (SIGABRT, "ABRT", _("Aborted"));
116#endif
117#if defined (SIGIOT)
118  init_sig (SIGIOT, "IOT", _("IOT trap"));
119#endif
120#if defined (SIGEMT)
121  init_sig (SIGEMT, "EMT", _("EMT trap"));
122#endif
123#if defined (SIGFPE)
124  init_sig (SIGFPE, "FPE", _("Floating point exception"));
125#endif
126#if defined (SIGKILL)
127  init_sig (SIGKILL, "KILL", _("Killed"));
128#endif
129#if defined (SIGBUS)
130  init_sig (SIGBUS, "BUS", _("Bus error"));
131#endif
132#if defined (SIGSEGV)
133  init_sig (SIGSEGV, "SEGV", _("Segmentation fault"));
134#endif
135#if defined (SIGSYS)
136  init_sig (SIGSYS, "SYS", _("Bad system call"));
137#endif
138#if defined (SIGPIPE)
139  init_sig (SIGPIPE, "PIPE", _("Broken pipe"));
140#endif
141#if defined (SIGALRM)
142  init_sig (SIGALRM, "ALRM", _("Alarm clock"));
143#endif
144#if defined (SIGTERM)
145  init_sig (SIGTERM, "TERM", _("Terminated"));
146#endif
147#if defined (SIGUSR1)
148  init_sig (SIGUSR1, "USR1", _("User defined signal 1"));
149#endif
150#if defined (SIGUSR2)
151  init_sig (SIGUSR2, "USR2", _("User defined signal 2"));
152#endif
153  /* If SIGCLD == SIGCHLD, we want to print it as SIGCHLD because that
154     is what is in POSIX.1.  */
155#if defined (SIGCHLD)
156  init_sig (SIGCHLD, "CHLD", _("Child exited"));
157#endif
158#if defined (SIGCLD)
159  init_sig (SIGCLD, "CLD", _("Child exited"));
160#endif
161#if defined (SIGPWR)
162  init_sig (SIGPWR, "PWR", _("Power failure"));
163#endif
164#if defined (SIGTSTP)
165  init_sig (SIGTSTP, "TSTP", _("Stopped"));
166#endif
167#if defined (SIGTTIN)
168  init_sig (SIGTTIN, "TTIN", _("Stopped (tty input)"));
169#endif
170#if defined (SIGTTOU)
171  init_sig (SIGTTOU, "TTOU", _("Stopped (tty output)"));
172#endif
173#if defined (SIGSTOP)
174  init_sig (SIGSTOP, "STOP", _("Stopped (signal)"));
175#endif
176#if defined (SIGXCPU)
177  init_sig (SIGXCPU, "XCPU", _("CPU time limit exceeded"));
178#endif
179#if defined (SIGXFSZ)
180  init_sig (SIGXFSZ, "XFSZ", _("File size limit exceeded"));
181#endif
182#if defined (SIGVTALRM)
183  init_sig (SIGVTALRM, "VTALRM", _("Virtual timer expired"));
184#endif
185#if defined (SIGPROF)
186  init_sig (SIGPROF, "PROF", _("Profiling timer expired"));
187#endif
188#if defined (SIGWINCH)
189  /* "Window size changed" might be more accurate, but even if that
190     is all that it means now, perhaps in the future it will be
191     extended to cover other kinds of window changes.  */
192  init_sig (SIGWINCH, "WINCH", _("Window changed"));
193#endif
194#if defined (SIGCONT)
195  init_sig (SIGCONT, "CONT", _("Continued"));
196#endif
197#if defined (SIGURG)
198  init_sig (SIGURG, "URG", _("Urgent I/O condition"));
199#endif
200#if defined (SIGIO)
201  /* "I/O pending" has also been suggested.  A disadvantage is
202     that signal only happens when the process has
203     asked for it, not everytime I/O is pending.  Another disadvantage
204     is the confusion from giving it a different name than under Unix.  */
205  init_sig (SIGIO, "IO", _("I/O possible"));
206#endif
207#if defined (SIGWIND)
208  init_sig (SIGWIND, "WIND", _("SIGWIND"));
209#endif
210#if defined (SIGPHONE)
211  init_sig (SIGPHONE, "PHONE", _("SIGPHONE"));
212#endif
213#if defined (SIGPOLL)
214  init_sig (SIGPOLL, "POLL", _("I/O possible"));
215#endif
216#if defined (SIGLOST)
217  init_sig (SIGLOST, "LOST", _("Resource lost"));
218#endif
219#if defined (SIGDANGER)
220  init_sig (SIGDANGER, "DANGER", _("Danger signal"));
221#endif
222#if defined (SIGINFO)
223  init_sig (SIGINFO, "INFO", _("Information request"));
224#endif
225#if defined (SIGNOFP)
226  init_sig (SIGNOFP, "NOFP", _("Floating point co-processor not available"));
227#endif
228
229  return 1;
230}
231
232#endif  /* SYS_SIGLIST_DECLARED */
233
234
235char *
236strsignal (signal)
237     int signal;
238{
239  static char buf[] = "Signal 12345678901234567890";
240
241#if !defined(SYS_SIGLIST_DECLARED)
242  static char sig_initted = 0;
243
244  if (!sig_initted)
245    sig_initted = signame_init ();
246#endif
247
248  if (signal > 0 || signal < NSIG)
249    return (char *) sys_siglist[signal];
250
251  sprintf (buf, "Signal %d", signal);
252  return buf;
253}
254
255#endif  /* HAVE_STRSIGNAL */
256