Mail Archives: djgpp-workers/1998/02/01/16:49:31
On Sun, 11 Jan 1998, DJ Delorie wrote:
> > Should we add NSIG in v2.02?
>
> Only if we're prepared to add _sys_siglist[] also.
Here it is (together with a compatibility function `psignal' as desert).
If you wonder why did I go to such lengths instead of just defining a
static array, then that's because the static solution resulted in a 7KB
object file, most of which is the same string again and again, whereas
this one is only 2KB.
*** /dev/null Sat Jan 31 19:59:48 1998
--- src/libc/compat/signal/siglist.c Sat Jan 31 19:19:16 1998
***************
*** 0 ****
--- 1,96 ----
+ /* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */
+
+ #include <signal.h>
+ #include <string.h>
+ #include <stdlib.h>
+
+ char *sys_siglist[NSIG + 1]; /* initially all-zero */
+
+ static const char *known_signal[] = {
+ "Abort termination",
+ "Floating-point exception",
+ "Illegal instruction",
+ "Segmentation violation",
+ "Software termination signal",
+ "Alarm clock",
+ "Hangup",
+ "Interrupt",
+ "Kill",
+ "Write on pipe with no one to read it",
+ "Quit",
+ "User-defined signal 1",
+ "User-defined signal 2",
+ "Floating-point co-processor not present",
+ "Debugger/Breakpoint instruction",
+ "Timer tick signal",
+ "Profiler signal"
+ };
+
+ static char unknown_signal[] = "Unknown signal";
+
+ static void
+ put_hex_digits (char *str, int num, size_t idx)
+ {
+ static char xdigits[] = "0123456789ABCDEF";
+
+ str[idx] = xdigits[num / 16];
+ str[idx + 1] = xdigits[num & 15];
+ }
+
+ static char *
+ xstrdup (const char *src)
+ {
+ if (src)
+ {
+ size_t src_size = strlen (src) + 1;
+ char *new = (char *)malloc (src_size);
+
+ if (new)
+ {
+ memcpy (new, src, src_size);
+ return new;
+ }
+ }
+
+ return NULL;
+ }
+
+ static int signum;
+
+ static void
+ fill_dull_names (const char *template, size_t tpl_size, int count)
+ {
+ int i;
+
+ for (i = 0; i < count; i++)
+ {
+ char *signame = (char *)malloc (tpl_size);
+
+ memcpy (signame, template, tpl_size);
+ put_hex_digits (signame, i, tpl_size - 3);
+ sys_siglist[signum++] = signame;
+ }
+ }
+
+ static void __attribute__((constructor))
+ init_sys_siglist (void)
+ {
+ static char int_name[] = "Interrupt XXh";
+ static size_t int_size = sizeof(int_name);
+ static char excpt_name[] = "Exception XXh";
+ static size_t excpt_size = sizeof(excpt_name);
+ int i;
+
+ signum = 0;
+
+ fill_dull_names (int_name, int_size, 256);
+ fill_dull_names (excpt_name, excpt_size, 32);
+
+ for (i = 0; i < 17; i++)
+ sys_siglist[signum++] = xstrdup (known_signal[i]);
+
+ for (i = 305; i < 320; i++)
+ sys_siglist[signum++] = xstrdup (unknown_signal);
+
+ sys_siglist[signum] = 0;
+ }
*** /dev/null Sat Jan 31 20:00:15 1998
--- src/libc/compat/signal/psignal.c Sat Jan 31 19:40:54 1998
***************
*** 0 ****
--- 1,13 ----
+ /* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */
+
+ #include <signal.h>
+ #include <stdio.h>
+
+ void
+ psignal (int sig, const char *msg)
+ {
+ if (sig >= 0 && sig < NSIG)
+ fprintf (stderr, "%s: %s\n", msg, sys_siglist[sig]);
+ else
+ fprintf (stderr, "%s: signal %d\n", msg, sig);
+ }
*** /dev/null Sat Jan 31 20:00:26 1998
--- src/libc/compat/signal/siglist.txh Sat Jan 31 19:54:50 1998
***************
*** 0 ****
--- 1,33 ----
+ @c ----------------------------------------------------------------------
+
+ @node psignal, signal
+ @subheading Syntax
+
+ @example
+ #include <signal.h>
+
+ extern char *sys_siglist[];
+ void psignal (int sig, const char *msg);
+ @end example
+
+ @subheading Description
+
+ This function produces a message on the standard error stream describing
+ the signal given by its number in @var{sig}. It prints the string
+ pointed to by @var{msg}, then the name of the signal, and a newline.
+
+ The names of signals can be retrieved using the array
+ @code{sys_siglist}, with the signal number serving as an index into this
+ array.
+
+ @subheading Example
+
+ @example
+ #include <signal.h>
+
+ void sig_catcher (int sig)
+ @{
+ psignal (progname, sig);
+ return;
+ @}
+ @end example
*** /dev/null Sat Jan 31 20:00:54 1998
--- src/libc/compat/signal/makefile Sat Jan 31 19:56:56 1998
***************
*** 0 ****
--- 1,7 ----
+ # Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details
+ TOP=../..
+
+ SRC += siglist.c
+ SRC += psignal.c
+
+ include $(TOP)/../makefile.inc
*** include/signal.h~0 Sat Apr 27 00:14:42 1996
--- include/signal.h Sat Jan 31 19:54:58 1998
***************
*** 78,83 ****
--- 78,89 ----
#define SIGPROF 304
#define SIGMAX 320
+ #define NSIG SIGMAX
+
+ extern char *sys_siglist[];
+
+ void psignal(int _sig, const char *_msg);
+
#endif /* !_POSIX_SOURCE */
#endif /* !__STRICT_ANSI__ */
#endif /* !__dj_ENFORCE_ANSI_FREESTANDING */
- Raw text -