Move include of config.h before all others.
[make.git] / signame.c
blob96a6cfe15c96f0ade9080ad20338145e62ec8668
1 /* Convert between signal names and numbers.
2 Copyright (C) 1990, 1992, 1993, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
22 #include <stdio.h>
23 #include <sys/types.h> /* Some systems need this for <signal.h>. */
24 #include <signal.h>
26 /* Some systems declare `sys_siglist in <unistd.h>; if
27 configure defined SYS_SIGLIST_DECLARED, it may expect
28 to find the declaration there. */
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
34 /* Some systems do not define NSIG in <signal.h>. */
35 #ifndef NSIG
36 #ifdef _NSIG
37 #define NSIG _NSIG
38 #else
39 #define NSIG 32
40 #endif
41 #endif
43 #if !__STDC__
44 #define const
45 #endif
47 #include "signame.h"
49 #ifndef HAVE_SYS_SIGLIST
50 /* There is too much variation in Sys V signal numbers and names, so
51 we must initialize them at runtime. */
53 static const char undoc[] = "unknown signal";
55 const char *sys_siglist[NSIG];
57 #else /* HAVE_SYS_SIGLIST. */
59 #ifndef SYS_SIGLIST_DECLARED
60 extern char *sys_siglist[];
61 #endif /* Not SYS_SIGLIST_DECLARED. */
63 #endif /* Not HAVE_SYS_SIGLIST. */
65 /* Table of abbreviations for signals. Note: A given number can
66 appear more than once with different abbreviations. */
67 typedef struct
69 int number;
70 const char *abbrev;
71 } num_abbrev;
72 static num_abbrev sig_table[NSIG*2];
73 /* Number of elements of sig_table used. */
74 static int sig_table_nelts = 0;
76 /* Enter signal number NUMBER into the tables with ABBREV and NAME. */
78 static void
79 init_sig (number, abbrev, name)
80 int number;
81 const char *abbrev;
82 const char *name;
84 #ifndef HAVE_SYS_SIGLIST
85 sys_siglist[number] = name;
86 #endif
87 sig_table[sig_table_nelts].number = number;
88 sig_table[sig_table_nelts++].abbrev = abbrev;
91 void
92 signame_init ()
94 #ifndef HAVE_SYS_SIGLIST
95 int i;
96 /* Initialize signal names. */
97 for (i = 0; i < NSIG; i++)
98 sys_siglist[i] = undoc;
99 #endif /* !HAVE_SYS_SIGLIST */
101 /* Initialize signal names. */
102 #if defined (SIGHUP)
103 init_sig (SIGHUP, "HUP", "Hangup");
104 #endif
105 #if defined (SIGINT)
106 init_sig (SIGINT, "INT", "Interrupt");
107 #endif
108 #if defined (SIGQUIT)
109 init_sig (SIGQUIT, "QUIT", "Quit");
110 #endif
111 #if defined (SIGILL)
112 init_sig (SIGILL, "ILL", "Illegal Instruction");
113 #endif
114 #if defined (SIGTRAP)
115 init_sig (SIGTRAP, "TRAP", "Trace/breakpoint trap");
116 #endif
117 /* If SIGIOT == SIGABRT, we want to print it as SIGABRT because
118 SIGABRT is in ANSI and POSIX.1 and SIGIOT isn't. */
119 #if defined (SIGABRT)
120 init_sig (SIGABRT, "ABRT", "Aborted");
121 #endif
122 #if defined (SIGIOT)
123 init_sig (SIGIOT, "IOT", "IOT trap");
124 #endif
125 #if defined (SIGEMT)
126 init_sig (SIGEMT, "EMT", "EMT trap");
127 #endif
128 #if defined (SIGFPE)
129 init_sig (SIGFPE, "FPE", "Floating point exception");
130 #endif
131 #if defined (SIGKILL)
132 init_sig (SIGKILL, "KILL", "Killed");
133 #endif
134 #if defined (SIGBUS)
135 init_sig (SIGBUS, "BUS", "Bus error");
136 #endif
137 #if defined (SIGSEGV)
138 init_sig (SIGSEGV, "SEGV", "Segmentation fault");
139 #endif
140 #if defined (SIGSYS)
141 init_sig (SIGSYS, "SYS", "Bad system call");
142 #endif
143 #if defined (SIGPIPE)
144 init_sig (SIGPIPE, "PIPE", "Broken pipe");
145 #endif
146 #if defined (SIGALRM)
147 init_sig (SIGALRM, "ALRM", "Alarm clock");
148 #endif
149 #if defined (SIGTERM)
150 init_sig (SIGTERM, "TERM", "Terminated");
151 #endif
152 #if defined (SIGUSR1)
153 init_sig (SIGUSR1, "USR1", "User defined signal 1");
154 #endif
155 #if defined (SIGUSR2)
156 init_sig (SIGUSR2, "USR2", "User defined signal 2");
157 #endif
158 /* If SIGCLD == SIGCHLD, we want to print it as SIGCHLD because that
159 is what is in POSIX.1. */
160 #if defined (SIGCHLD)
161 init_sig (SIGCHLD, "CHLD", "Child exited");
162 #endif
163 #if defined (SIGCLD)
164 init_sig (SIGCLD, "CLD", "Child exited");
165 #endif
166 #if defined (SIGPWR)
167 init_sig (SIGPWR, "PWR", "Power failure");
168 #endif
169 #if defined (SIGTSTP)
170 init_sig (SIGTSTP, "TSTP", "Stopped");
171 #endif
172 #if defined (SIGTTIN)
173 init_sig (SIGTTIN, "TTIN", "Stopped (tty input)");
174 #endif
175 #if defined (SIGTTOU)
176 init_sig (SIGTTOU, "TTOU", "Stopped (tty output)");
177 #endif
178 #if defined (SIGSTOP)
179 init_sig (SIGSTOP, "STOP", "Stopped (signal)");
180 #endif
181 #if defined (SIGXCPU)
182 init_sig (SIGXCPU, "XCPU", "CPU time limit exceeded");
183 #endif
184 #if defined (SIGXFSZ)
185 init_sig (SIGXFSZ, "XFSZ", "File size limit exceeded");
186 #endif
187 #if defined (SIGVTALRM)
188 init_sig (SIGVTALRM, "VTALRM", "Virtual timer expired");
189 #endif
190 #if defined (SIGPROF)
191 init_sig (SIGPROF, "PROF", "Profiling timer expired");
192 #endif
193 #if defined (SIGWINCH)
194 /* "Window size changed" might be more accurate, but even if that
195 is all that it means now, perhaps in the future it will be
196 extended to cover other kinds of window changes. */
197 init_sig (SIGWINCH, "WINCH", "Window changed");
198 #endif
199 #if defined (SIGCONT)
200 init_sig (SIGCONT, "CONT", "Continued");
201 #endif
202 #if defined (SIGURG)
203 init_sig (SIGURG, "URG", "Urgent I/O condition");
204 #endif
205 #if defined (SIGIO)
206 /* "I/O pending" has also been suggested. A disadvantage is
207 that signal only happens when the process has
208 asked for it, not everytime I/O is pending. Another disadvantage
209 is the confusion from giving it a different name than under Unix. */
210 init_sig (SIGIO, "IO", "I/O possible");
211 #endif
212 #if defined (SIGWIND)
213 init_sig (SIGWIND, "WIND", "SIGWIND");
214 #endif
215 #if defined (SIGPHONE)
216 init_sig (SIGPHONE, "PHONE", "SIGPHONE");
217 #endif
218 #if defined (SIGPOLL)
219 init_sig (SIGPOLL, "POLL", "I/O possible");
220 #endif
221 #if defined (SIGLOST)
222 init_sig (SIGLOST, "LOST", "Resource lost");
223 #endif
224 #if defined (SIGDANGER)
225 init_sig (SIGDANGER, "DANGER", "Danger signal");
226 #endif
227 #if defined (SIGINFO)
228 init_sig (SIGINFO, "INFO", "Information request");
229 #endif
232 /* Return the abbreviation for signal NUMBER. */
234 char *
235 sig_abbrev (number)
236 int number;
238 int i;
240 if (sig_table_nelts == 0)
241 signame_init ();
243 for (i = 0; i < sig_table_nelts; i++)
244 if (sig_table[i].number == number)
245 return (char *)sig_table[i].abbrev;
246 return NULL;
249 /* Return the signal number for an ABBREV, or -1 if there is no
250 signal by that name. */
253 sig_number (abbrev)
254 const char *abbrev;
256 int i;
258 if (sig_table_nelts == 0)
259 signame_init ();
261 /* Skip over "SIG" if present. */
262 if (abbrev[0] == 'S' && abbrev[1] == 'I' && abbrev[2] == 'G')
263 abbrev += 3;
265 for (i = 0; i < sig_table_nelts; i++)
266 if (abbrev[0] == sig_table[i].abbrev[0]
267 && strcmp (abbrev, sig_table[i].abbrev) == 0)
268 return sig_table[i].number;
269 return -1;
272 #ifndef HAVE_PSIGNAL
273 /* Print to standard error the name of SIGNAL, preceded by MESSAGE and
274 a colon, and followed by a newline. */
276 void
277 psignal (signal, message)
278 int signal;
279 const char *message;
281 if (signal <= 0 || signal >= NSIG)
282 fprintf (stderr, "%s: unknown signal", message);
283 else
284 fprintf (stderr, "%s: %s\n", message, sys_siglist[signal]);
286 #endif
288 #ifndef HAVE_STRSIGNAL
289 /* Return the string associated with the signal number. */
291 char *
292 strsignal (signal)
293 int signal;
295 static char buf[] = "Signal 12345678901234567890";
297 if (signal > 0 || signal < NSIG)
298 return (char *) sys_siglist[signal];
300 sprintf (buf, "Signal %d", signal);
301 return buf;
303 #endif