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