Skip to content

Commit

Permalink
signum is now platform independent
Browse files Browse the repository at this point in the history
  • Loading branch information
findstr committed Jul 26, 2024
1 parent 1c1b318 commit 9edc506
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 42 deletions.
49 changes: 49 additions & 0 deletions lualib-src/lualib-core.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <unistd.h>
#include <assert.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
Expand Down Expand Up @@ -182,6 +183,53 @@ ltimercancel(lua_State *L)
return 1;
}

static int
lsignalmap(lua_State *L)
{
size_t i;
lua_newtable(L);
struct signal {
const char *name;
int signum;
} signals[] = {
{"SIGINT", SIGINT},
{"SIGILL", SIGILL},
{"SIGABRT", SIGABRT},
{"SIGFPE", SIGFPE},
{"SIGSEGV", SIGSEGV},
{"SIGTERM", SIGTERM},
{"SIGHUP", SIGHUP},
{"SIGQUIT", SIGQUIT},
{"SIGTRAP", SIGTRAP},
{"SIGKILL", SIGKILL},
{"SIGBUS", SIGBUS},
{"SIGSYS", SIGSYS},
{"SIGPIPE", SIGPIPE},
{"SIGALRM", SIGALRM},
{"SIGURG", SIGURG},
{"SIGSTOP", SIGSTOP},
{"SIGTSTP", SIGTSTP},
{"SIGCONT", SIGCONT},
{"SIGCHLD", SIGCHLD},
{"SIGTTIN", SIGTTIN},
{"SIGTTOU", SIGTTOU},
{"SIGPOLL", SIGPOLL},
{"SIGXCPU", SIGXCPU},
{"SIGXFSZ", SIGXFSZ},
{"SIGVTALRM", SIGVTALRM},
{"SIGPROF", SIGPROF},
{"SIGUSR1", SIGUSR1},
{"SIGUSR2", SIGUSR2},
};
for (i = 0; i < sizeof(signals) / sizeof(signals[0]); i++) {
lua_pushinteger(L, signals[i].signum);
lua_setfield(L, -2, signals[i].name);
lua_pushstring(L, signals[i].name);
lua_seti(L, -2, signals[i].signum);
}
return 1;
}

static int
lsignal(lua_State *L)
{
Expand Down Expand Up @@ -527,6 +575,7 @@ luaopen_core_c(lua_State *L)
{"dispatch", ldispatch},
{"timeout", ltimeout},
{"timercancel", ltimercancel},
{"signalmap", lsignalmap},
{"signal", lsignal},
{"genid", lgenid},
{"tostring", ltostring},
Expand Down
43 changes: 1 addition & 42 deletions lualib/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,8 @@ end

--signal
local signal = c.signal
local signal_map = c.signalmap()
local signal_dispatch = {}
local signal_map = {}
do
local sig_list = {
SIGINT = 2, -- Interactive attention signal.
SIGILL = 4, -- Illegal instruction.
SIGABRT = 6, -- Abnormal termination.
SIGFPE = 8, -- Erroneous arithmetic operation.
SIGSEGV = 11, -- Invalid access to storage.
SIGTERM = 15, -- Termination request.

--Historical signals specified by POSIX.
SIGHUP = 1, -- Hangup.
SIGQUIT = 3, -- Quit.
SIGTRAP = 5, -- Trace/breakpoint trap.
SIGKILL = 9, -- Killed.
SIGBUS = 10, -- Bus error.
SIGSYS = 12, -- Bad system call.
SIGPIPE = 13, -- Broken pipe.
SIGALRM = 14, -- Alarm clock.

--New(er) POSIX signals (1003.1-2008, 1003.1-2013).

SIGURG = 16, -- Urgent data is available at a socket.
SIGSTOP = 17, -- Stop, unblockable.
SIGTSTP = 18, -- Keyboard stop.
SIGCONT = 19, -- Continue.
SIGCHLD = 20, -- Child terminated or stopped.
SIGTTIN = 21, -- Background read from control terminal.
SIGTTOU = 22, -- Background write to control terminal.
SIGPOLL = 23, -- Pollable event occurred (System V).
SIGXCPU = 24, -- CPU time limit exceeded.
SIGXFSZ = 25, -- File size limit exceeded.
SIGVTALRM = 26, -- Virtual timer expired.
SIGPROF = 27, -- Profiling timer expired.
SIGUSR1 = 30, -- User-defined signal 1.
SIGUSR2 = 31, -- User-defined signal 2.
}
for k, v in pairs(sig_list) do
signal_map[k] = v
signal_map[v] = k
end
end

--coroutine
--state migrate(RUN (WAIT->READY)/SLEEP RUN)
Expand Down

0 comments on commit 9edc506

Please sign in to comment.