commit: 16b89d52601e0cd1bc252214fa476a7919abf69a
parent f4c510906fb09085bf1f3d32d48980aeb62135a6
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Wed, 7 Aug 2024 08:50:32 +0200
lib/sys_signame: compile-time table
Diffstat:
5 files changed, 99 insertions(+), 124 deletions(-)
diff --git a/Makefile b/Makefile
@@ -71,8 +71,8 @@ C_SOURCES = cmd/*.c lib/*.h lib/*.c test-lib/*.c configure.d/*.c
format: $(C_SOURCES)
clang-format -style=file -assume-filename=.clang-format -i $(C_SOURCES)
-lib/sys_signame.h: lib/sys_signame.sh
- lib/sys_signame.sh >|lib/sys_signame.h
+lib/sys_signame.c: lib/sys_signame.sh
+ lib/sys_signame.sh >|lib/sys_signame.c
cmd/cat: cmd/cat.c config.mk Makefile
@@ -94,9 +94,9 @@ cmd/sleep: cmd/sleep.c lib/strtodur.c Makefile
$(RM) -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno}
$(CC) -std=c99 $(CFLAGS) -o $@ cmd/sleep.c lib/strtodur.c $(LDFLAGS) $(LDSTATIC)
-cmd/timeout: cmd/timeout.c lib/strtodur.c lib/sys_signame.h Makefile
+cmd/timeout: cmd/timeout.c lib/strtodur.c lib/sys_signame.h lib/sys_signame.c Makefile
$(RM) -f ${<:=.gcov} ${@:=.gcda} ${@:=.gcno}
- $(CC) -std=c99 $(CFLAGS) -o $@ cmd/timeout.c lib/strtodur.c $(LDFLAGS) $(LDSTATIC)
+ $(CC) -std=c99 $(CFLAGS) -o $@ cmd/timeout.c lib/strtodur.c lib/sys_signame.c $(LDFLAGS) $(LDSTATIC)
test-lib/mode: test-lib/mode.c lib/mode.c Makefile
$(CC) -std=c99 $(CFLAGS) -o $@ test-lib/mode.c lib/mode.c $(LDFLAGS) $(LDSTATIC)
diff --git a/cmd/timeout.c b/cmd/timeout.c
@@ -77,8 +77,6 @@ main(int argc, char *argv[])
cmd_exit_timeout = 0;
break;
case 's':
- init_util_sys_signame();
-
if(isdigit(optarg[0]))
{
assert(errno == 0);
diff --git a/lib/sys_signame.c b/lib/sys_signame.c
@@ -0,0 +1,89 @@
+// utils-std: Collection of commonly available Unix tools
+// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-License-Identifier: CC0-1.0 OR WTFPL
+// /!\ File generated by lib/sys_signame.sh avoid editing
+#include "sys_signame.h"
+
+char *util_sys_signame[NSIG] = {
+#ifdef SIGABRT
+ [SIGABRT] = "ABRT",
+#endif
+#ifdef SIGALRM
+ [SIGALRM] = "ALRM",
+#endif
+#ifdef SIGBUS
+ [SIGBUS] = "BUS",
+#endif
+#ifdef SIGCHLD
+ [SIGCHLD] = "CHLD",
+#endif
+#ifdef SIGCONT
+ [SIGCONT] = "CONT",
+#endif
+#ifdef SIGFPE
+ [SIGFPE] = "FPE",
+#endif
+#ifdef SIGHUP
+ [SIGHUP] = "HUP",
+#endif
+#ifdef SIGILL
+ [SIGILL] = "ILL",
+#endif
+#ifdef SIGINT
+ [SIGINT] = "INT",
+#endif
+#ifdef SIGKILL
+ [SIGKILL] = "KILL",
+#endif
+#ifdef SIGPIPE
+ [SIGPIPE] = "PIPE",
+#endif
+#ifdef SIGQUIT
+ [SIGQUIT] = "QUIT",
+#endif
+#ifdef SIGSEGV
+ [SIGSEGV] = "SEGV",
+#endif
+#ifdef SIGSTOP
+ [SIGSTOP] = "STOP",
+#endif
+#ifdef SIGTERM
+ [SIGTERM] = "TERM",
+#endif
+#ifdef SIGTSTP
+ [SIGTSTP] = "TSTP",
+#endif
+#ifdef SIGTTIN
+ [SIGTTIN] = "TTIN",
+#endif
+#ifdef SIGTTOU
+ [SIGTTOU] = "TTOU",
+#endif
+#ifdef SIGUSR1
+ [SIGUSR1] = "USR1",
+#endif
+#ifdef SIGUSR2
+ [SIGUSR2] = "USR2",
+#endif
+#ifdef SIGWINCH
+ [SIGWINCH] = "WINCH",
+#endif
+#ifdef SIGSYS
+ [SIGSYS] = "SYS",
+#endif
+#ifdef SIGTRAP
+ [SIGTRAP] = "TRAP",
+#endif
+#ifdef SIGURG
+ [SIGURG] = "URG",
+#endif
+#ifdef SIGVTALRM
+ [SIGVTALRM] = "VTALRM",
+#endif
+#ifdef SIGXCPU
+ [SIGXCPU] = "XCPU",
+#endif
+#ifdef SIGXFSZ
+ [SIGXFSZ] = "XFSZ",
+#endif
+};
diff --git a/lib/sys_signame.h b/lib/sys_signame.h
@@ -1,7 +1,6 @@
// utils-std: Collection of commonly available Unix tools
// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
// SPDX-License-Identifier: CC0-1.0 OR WTFPL
-// /!\ File generated by lib/sys_signame.sh avoid editing
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE // NSIG
#endif
@@ -21,91 +20,6 @@
#endif
#endif
-static char *util_sys_signame[NSIG];
-static size_t util_sys_signame_len = NSIG;
+extern char *util_sys_signame[NSIG];
-static void
-init_util_sys_signame()
-{
-#ifdef SIGABRT
- util_sys_signame[SIGABRT] = "ABRT";
-#endif
-#ifdef SIGALRM
- util_sys_signame[SIGALRM] = "ALRM";
-#endif
-#ifdef SIGBUS
- util_sys_signame[SIGBUS] = "BUS";
-#endif
-#ifdef SIGCHLD
- util_sys_signame[SIGCHLD] = "CHLD";
-#endif
-#ifdef SIGCONT
- util_sys_signame[SIGCONT] = "CONT";
-#endif
-#ifdef SIGFPE
- util_sys_signame[SIGFPE] = "FPE";
-#endif
-#ifdef SIGHUP
- util_sys_signame[SIGHUP] = "HUP";
-#endif
-#ifdef SIGILL
- util_sys_signame[SIGILL] = "ILL";
-#endif
-#ifdef SIGINT
- util_sys_signame[SIGINT] = "INT";
-#endif
-#ifdef SIGKILL
- util_sys_signame[SIGKILL] = "KILL";
-#endif
-#ifdef SIGPIPE
- util_sys_signame[SIGPIPE] = "PIPE";
-#endif
-#ifdef SIGQUIT
- util_sys_signame[SIGQUIT] = "QUIT";
-#endif
-#ifdef SIGSEGV
- util_sys_signame[SIGSEGV] = "SEGV";
-#endif
-#ifdef SIGSTOP
- util_sys_signame[SIGSTOP] = "STOP";
-#endif
-#ifdef SIGTERM
- util_sys_signame[SIGTERM] = "TERM";
-#endif
-#ifdef SIGTSTP
- util_sys_signame[SIGTSTP] = "TSTP";
-#endif
-#ifdef SIGTTIN
- util_sys_signame[SIGTTIN] = "TTIN";
-#endif
-#ifdef SIGTTOU
- util_sys_signame[SIGTTOU] = "TTOU";
-#endif
-#ifdef SIGUSR1
- util_sys_signame[SIGUSR1] = "USR1";
-#endif
-#ifdef SIGUSR2
- util_sys_signame[SIGUSR2] = "USR2";
-#endif
-#ifdef SIGWINCH
- util_sys_signame[SIGWINCH] = "WINCH";
-#endif
-#ifdef SIGSYS
- util_sys_signame[SIGSYS] = "SYS";
-#endif
-#ifdef SIGTRAP
- util_sys_signame[SIGTRAP] = "TRAP";
-#endif
-#ifdef SIGURG
- util_sys_signame[SIGURG] = "URG";
-#endif
-#ifdef SIGVTALRM
- util_sys_signame[SIGVTALRM] = "VTALRM";
-#endif
-#ifdef SIGXCPU
- util_sys_signame[SIGXCPU] = "XCPU";
-#endif
-#ifdef SIGXFSZ
- util_sys_signame[SIGXFSZ] = "XFSZ";
-#endif
-}
+static size_t util_sys_signame_len = NSIG;
diff --git a/lib/sys_signame.sh b/lib/sys_signame.sh
@@ -1,44 +1,18 @@
#!/bin/sh
-# utils-std: Collection of commonly available Unix tools
-# SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
-# SPDX-License-Identifier: CC0-1.0 OR WTFPL
-
cat <<-EOF
// utils-std: Collection of commonly available Unix tools
// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
// SPDX-License-Identifier: CC0-1.0 OR WTFPL
// /!\ File generated by lib/sys_signame.sh avoid editing
-#ifndef _DEFAULT_SOURCE
-#define _DEFAULT_SOURCE // NSIG
-#endif
-
-#include <signal.h> // NSIG
-
-// POSIX defect 741 (applied in issue 8 draft 4.1) adds _SC_NSIG
-// https://www.austingroupbugs.net/view.php?id=741
-// But it isn't implemented in current (musl 1.2.5, glibc 2.38) systems,
-// so I'm keeping it this way for now.
-#ifndef NSIG
-#if defined(SIGMAX)
-#define NSIG (SIGMAX + 1)
-#else
-#warning NSIG nor SIGMAX were defined, this might be a bug.
-#define NSIG (8 * sizeof(sigset_t) + 1)
-#endif
-#endif
-
-static char *util_sys_signame[NSIG];
-static size_t util_sys_signame_len = NSIG;
+#include "sys_signame.h"
-static void
-init_util_sys_signame()
-{
+char *util_sys_signame[NSIG] = {
EOF
# From signal.h definition in POSIX
for i in SIGABRT SIGALRM SIGBUS SIGCHLD SIGCONT SIGFPE SIGHUP SIGILL SIGINT SIGKILL SIGPIPE SIGQUIT SIGSEGV SIGSTOP SIGTERM SIGTSTP SIGTTIN SIGTTOU SIGUSR1 SIGUSR2 SIGWINCH SIGSYS SIGTRAP SIGURG SIGVTALRM SIGXCPU SIGXFSZ
do
- printf '#ifdef %s\n\tutil_sys_signame[%s] = "%s";\n#endif\n' "$i" "$i" "${i#SIG}"
+ printf '#ifdef %s\n\t[%s] = "%s",\n#endif\n' "$i" "$i" "${i#SIG}"
done
-printf '}\n'
+printf '};\n'