Imported from ../bash-2.05a.tar.gz.
[platform/upstream/bash.git] / support / mksignames.c
1 /* signames.c -- Create and write `signames.h', which contains an array of
2    signal names. */
3
4 /* Copyright (C) 1992 Free Software Foundation, Inc.
5
6    This file is part of GNU Bash, the Bourne Again SHell.
7
8    Bash is free software; you can redistribute it and/or modify it under
9    the terms of the GNU General Public License as published by the Free
10    Software Foundation; either version 2, or (at your option) any later
11    version.
12
13    Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14    WARRANTY; without even the implied warranty of MERCHANTABILITY or
15    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16    for more details.
17
18    You should have received a copy of the GNU General Public License along
19    with Bash; see the file COPYING.  If not, write to the Free Software
20    Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
21
22 #include "config.h"
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <signal.h>
27 #if defined (HAVE_STDLIB_H)
28 #  include <stdlib.h>
29 #else
30 #  include "ansi_stdlib.h"
31 #endif /* HAVE_STDLIB_H */
32
33 #if !defined (NSIG)
34 #  define NSIG 64
35 #endif
36
37 /*
38  * Special traps:
39  *      EXIT == 0
40  *      DEBUG == NSIG
41  *      ERR == NSIG+1
42  */
43 #define LASTSIG NSIG+1
44
45 char *signal_names[2 * NSIG + 3];
46
47 #define signal_names_size (sizeof(signal_names)/sizeof(signal_names[0]))
48
49 char *progname;
50
51 /* AIX 4.3 defines SIGRTMIN and SIGRTMAX as 888 and 999 respectively.
52    I don't want to allocate so much unused space for the intervening signal
53    numbers, so we just punt if SIGRTMAX is past the bounds of the
54    signal_names array (handled in configure). */
55 #if defined (SIGRTMAX) && defined (UNUSABLE_RT_SIGNALS)
56 #  undef SIGRTMAX
57 #  undef SIGRTMIN
58 #endif
59
60 #if defined (SIGRTMAX) || defined (SIGRTMIN)
61 #  define RTLEN 14
62 #  define RTLIM 256
63 #endif
64
65 void
66 initialize_signames ()
67 {
68   register int i;
69 #if defined (SIGRTMAX) || defined (SIGRTMIN)
70   int rtmin, rtmax, rtcnt;
71 #endif
72
73   for (i = 1; i < signal_names_size; i++)
74     signal_names[i] = (char *)NULL;
75
76   /* `signal' 0 is what we do on exit. */
77   signal_names[0] = "EXIT";
78
79   /* Place signal names which can be aliases for more common signal
80      names first.  This allows (for example) SIGABRT to overwrite SIGLOST. */
81
82   /* POSIX 1003.1b-1993 real time signals, but take care of incomplete
83      implementations. Acoording to the standard, both, SIGRTMIN and
84      SIGRTMAX must be defined, SIGRTMIN must be stricly less than
85      SIGRTMAX, and the difference must be at least 7, that is, there
86      must be at least eight distinct real time signals. */
87
88   /* The generated signal names are SIGRTMIN, SIGRTMIN+1, ...,
89      SIGRTMIN+x, SIGRTMAX-x, ..., SIGRTMAX-1, SIGRTMAX. If the number
90      of RT signals is odd, there is an extra SIGRTMIN+(x+1).
91      These names are the ones used by ksh and /usr/xpg4/bin/sh on SunOS5. */
92
93 #if defined (SIGRTMIN)
94   rtmin = SIGRTMIN;
95   signal_names[rtmin] = "SIGRTMIN";
96 #endif
97
98 #if defined (SIGRTMAX)
99   rtmax = SIGRTMAX;
100   signal_names[rtmax] = "SIGRTMAX";
101 #endif
102
103 #if defined (SIGRTMAX) && defined (SIGRTMIN)
104   if (rtmax > rtmin)
105     {
106       rtcnt = (rtmax - rtmin - 1) / 2;
107       /* croak if there are too many RT signals */
108       if (rtcnt >= RTLIM/2)
109         {
110           rtcnt = RTLIM/2-1;
111           fprintf(stderr, "%s: error: more than %i real time signals, fix `%s'\n",
112                   progname, RTLIM, progname);
113         }
114
115       for (i = 1; i <= rtcnt; i++)
116         {
117           signal_names[rtmin+i] = (char *)malloc(RTLEN);
118           if (signal_names[rtmin+i])
119             sprintf (signal_names[rtmin+i], "SIGRTMIN+%d", i);
120           signal_names[rtmax-i] = (char *)malloc(RTLEN);
121           if (signal_names[rtmax-i])
122             sprintf (signal_names[rtmax-i], "SIGRTMAX-%d", i);
123         }
124
125       if (rtcnt < RTLIM/2-1 && rtcnt != (rtmax-rtmin)/2)
126         {
127           /* Need an extra RTMIN signal */
128           signal_names[rtmin+rtcnt+1] = (char *)malloc(RTLEN);
129           if (signal_names[rtmin+rtcnt+1])
130             sprintf (signal_names[rtmin+rtcnt+1], "SIGRTMIN+%d", rtcnt+1);
131         }
132     }
133 #endif /* SIGRTMIN && SIGRTMAX */
134
135 /* AIX */
136 #if defined (SIGLOST)   /* resource lost (eg, record-lock lost) */
137   signal_names[SIGLOST] = "SIGLOST";
138 #endif
139
140 #if defined (SIGMSG)    /* HFT input data pending */
141   signal_names[SIGMSG] = "SIGMSG";
142 #endif
143
144 #if defined (SIGDANGER) /* system crash imminent */
145   signal_names[SIGDANGER] = "SIGDANGER";
146 #endif
147
148 #if defined (SIGMIGRATE) /* migrate process to another CPU */
149   signal_names[SIGMIGRATE] = "SIGMIGRATE";
150 #endif
151
152 #if defined (SIGPRE)    /* programming error */
153   signal_names[SIGPRE] = "SIGPRE";
154 #endif
155
156 #if defined (SIGVIRT)   /* AIX virtual time alarm */
157   signal_names[SIGVIRT] = "SIGVIRT";
158 #endif
159
160 #if defined (SIGALRM1)  /* m:n condition variables */
161   signal_names[SIGALRM1] = "SIGALRM1";
162 #endif
163
164 #if defined (SIGWAITING)        /* m:n scheduling */
165   signal_names[SIGWAITING] = "SIGWAITING";
166 #endif
167
168 #if defined (SIGGRANT)  /* HFT monitor mode granted */
169   signal_names[SIGGRANT] = "SIGGRANT";
170 #endif
171
172 #if defined (SIGKAP)    /* keep alive poll from native keyboard */
173   signal_names[SIGKAP] = "SIGKAP";
174 #endif
175
176 #if defined (SIGRETRACT) /* HFT monitor mode retracted */
177   signal_names[SIGRETRACT] = "SIGRETRACT";
178 #endif
179
180 #if defined (SIGSOUND)  /* HFT sound sequence has completed */
181   signal_names[SIGSOUND] = "SIGSOUND";
182 #endif
183
184 #if defined (SIGSAK)    /* Secure Attention Key */
185   signal_names[SIGSAK] = "SIGSAK";
186 #endif
187
188 /* SunOS5 */
189 #if defined (SIGLWP)    /* special signal used by thread library */
190   signal_names[SIGLWP] = "SIGLWP";
191 #endif
192
193 #if defined (SIGFREEZE) /* special signal used by CPR */
194   signal_names[SIGFREEZE] = "SIGFREEZE";
195 #endif
196
197 #if defined (SIGTHAW)   /* special signal used by CPR */
198   signal_names[SIGTHAW] = "SIGTHAW";
199 #endif
200
201 #if defined (SIGCANCEL) /* thread cancellation signal used by libthread */
202   signal_names[SIGCANCEL] = "SIGCANCEL";
203 #endif
204
205 /* HP-UX */
206 #if defined (SIGDIL)    /* DIL signal (?) */
207   signal_names[SIGDIL] = "SIGDIL";
208 #endif
209
210 /* System V */
211 #if defined (SIGCLD)    /* Like SIGCHLD.  */
212   signal_names[SIGCLD] = "SIGCLD";
213 #endif
214
215 #if defined (SIGPWR)    /* power state indication */
216   signal_names[SIGPWR] = "SIGPWR";
217 #endif
218
219 #if defined (SIGPOLL)   /* Pollable event (for streams)  */
220   signal_names[SIGPOLL] = "SIGPOLL";
221 #endif
222
223 /* Unknown */
224 #if defined (SIGWINDOW)
225   signal_names[SIGWINDOW] = "SIGWINDOW";
226 #endif
227
228 /* Common */
229 #if defined (SIGHUP)    /* hangup */
230   signal_names[SIGHUP] = "SIGHUP";
231 #endif
232
233 #if defined (SIGINT)    /* interrupt */
234   signal_names[SIGINT] = "SIGINT";
235 #endif
236
237 #if defined (SIGQUIT)   /* quit */
238   signal_names[SIGQUIT] = "SIGQUIT";
239 #endif
240
241 #if defined (SIGILL)    /* illegal instruction (not reset when caught) */
242   signal_names[SIGILL] = "SIGILL";
243 #endif
244
245 #if defined (SIGTRAP)   /* trace trap (not reset when caught) */
246   signal_names[SIGTRAP] = "SIGTRAP";
247 #endif
248
249 #if defined (SIGIOT)    /* IOT instruction */
250   signal_names[SIGIOT] = "SIGIOT";
251 #endif
252
253 #if defined (SIGABRT)   /* Cause current process to dump core. */
254   signal_names[SIGABRT] = "SIGABRT";
255 #endif
256
257 #if defined (SIGEMT)    /* EMT instruction */
258   signal_names[SIGEMT] = "SIGEMT";
259 #endif
260
261 #if defined (SIGFPE)    /* floating point exception */
262   signal_names[SIGFPE] = "SIGFPE";
263 #endif
264
265 #if defined (SIGKILL)   /* kill (cannot be caught or ignored) */
266   signal_names[SIGKILL] = "SIGKILL";
267 #endif
268
269 #if defined (SIGBUS)    /* bus error */
270   signal_names[SIGBUS] = "SIGBUS";
271 #endif
272
273 #if defined (SIGSEGV)   /* segmentation violation */
274   signal_names[SIGSEGV] = "SIGSEGV";
275 #endif
276
277 #if defined (SIGSYS)    /* bad argument to system call */
278   signal_names[SIGSYS] = "SIGSYS";
279 #endif
280
281 #if defined (SIGPIPE)   /* write on a pipe with no one to read it */
282   signal_names[SIGPIPE] = "SIGPIPE";
283 #endif
284
285 #if defined (SIGALRM)   /* alarm clock */
286   signal_names[SIGALRM] = "SIGALRM";
287 #endif
288
289 #if defined (SIGTERM)   /* software termination signal from kill */
290   signal_names[SIGTERM] = "SIGTERM";
291 #endif
292
293 #if defined (SIGURG)    /* urgent condition on IO channel */
294   signal_names[SIGURG] = "SIGURG";
295 #endif
296
297 #if defined (SIGSTOP)   /* sendable stop signal not from tty */
298   signal_names[SIGSTOP] = "SIGSTOP";
299 #endif
300
301 #if defined (SIGTSTP)   /* stop signal from tty */
302   signal_names[SIGTSTP] = "SIGTSTP";
303 #endif
304
305 #if defined (SIGCONT)   /* continue a stopped process */
306   signal_names[SIGCONT] = "SIGCONT";
307 #endif
308
309 #if defined (SIGCHLD)   /* to parent on child stop or exit */
310   signal_names[SIGCHLD] = "SIGCHLD";
311 #endif
312
313 #if defined (SIGTTIN)   /* to readers pgrp upon background tty read */
314   signal_names[SIGTTIN] = "SIGTTIN";
315 #endif
316
317 #if defined (SIGTTOU)   /* like TTIN for output if (tp->t_local&LTOSTOP) */
318   signal_names[SIGTTOU] = "SIGTTOU";
319 #endif
320
321 #if defined (SIGIO)     /* input/output possible signal */
322   signal_names[SIGIO] = "SIGIO";
323 #endif
324
325 #if defined (SIGXCPU)   /* exceeded CPU time limit */
326   signal_names[SIGXCPU] = "SIGXCPU";
327 #endif
328
329 #if defined (SIGXFSZ)   /* exceeded file size limit */
330   signal_names[SIGXFSZ] = "SIGXFSZ";
331 #endif
332
333 #if defined (SIGVTALRM) /* virtual time alarm */
334   signal_names[SIGVTALRM] = "SIGVTALRM";
335 #endif
336
337 #if defined (SIGPROF)   /* profiling time alarm */
338   signal_names[SIGPROF] = "SIGPROF";
339 #endif
340
341 #if defined (SIGWINCH)  /* window changed */
342   signal_names[SIGWINCH] = "SIGWINCH";
343 #endif
344
345 /* 4.4 BSD */
346 #if defined (SIGINFO) && !defined (_SEQUENT_)   /* information request */
347   signal_names[SIGINFO] = "SIGINFO";
348 #endif
349
350 #if defined (SIGUSR1)   /* user defined signal 1 */
351   signal_names[SIGUSR1] = "SIGUSR1";
352 #endif
353
354 #if defined (SIGUSR2)   /* user defined signal 2 */
355   signal_names[SIGUSR2] = "SIGUSR2";
356 #endif
357
358 #if defined (SIGKILLTHR)        /* BeOS: Kill Thread */
359   signal_names[SIGKILLTHR] = "SIGKILLTHR";
360 #endif
361
362   for (i = 0; i < NSIG; i++)
363     if (signal_names[i] == (char *)NULL)
364       {
365         signal_names[i] = (char *)malloc (18);
366         if (signal_names[i])
367           sprintf (signal_names[i], "SIGJUNK(%d)", i);
368       }
369
370   signal_names[NSIG] = "DEBUG";
371   signal_names[NSIG+1] = "ERR";
372 }
373
374 void
375 write_signames (stream)
376      FILE *stream;
377 {
378   register int i;
379
380   fprintf (stream, "/* This file was automatically created by %s.\n",
381            progname);
382   fprintf (stream, "   Do not edit.  Edit support/mksignames.c instead. */\n\n");
383   fprintf (stream,
384            "/* A translation list so we can be polite to our users. */\n");
385   fprintf (stream, "char *signal_names[NSIG + 3] = {\n");
386
387   for (i = 0; i <= LASTSIG; i++)
388     fprintf (stream, "    \"%s\",\n", signal_names[i]);
389
390   fprintf (stream, "    (char *)0x0\n");
391   fprintf (stream, "};\n");
392 }
393
394 int
395 main (argc, argv)
396      int argc;
397      char **argv;
398 {
399   char *stream_name;
400   FILE *stream;
401
402   progname = argv[0];
403
404   if (argc == 1)
405     {
406       stream_name = "stdout";
407       stream = stdout;
408     }
409   else if (argc == 2)
410     {
411       stream_name = argv[1];
412       stream = fopen (stream_name, "w");
413     }
414   else
415     {
416       fprintf (stderr, "Usage: %s [output-file]\n", progname);
417       exit (1);
418     }
419
420   if (!stream)
421     {
422       fprintf (stderr, "%s: %s: cannot open for writing\n",
423                progname, stream_name);
424       exit (2);
425     }
426
427   initialize_signames ();
428   write_signames (stream);
429   exit (0);
430 }