From c587c4f4499b32698b1240b7cd43617ef4181662 Mon Sep 17 00:00:00 2001 From: Dominik Kaszewski Date: Thu, 28 Mar 2024 18:26:26 +0100 Subject: Implement os.SIGDFL and os.SIGIGN --- core/src/xmake/os/signal.c | 67 ++++++++++++++++++++++++++++++--------- xmake/core/base/os.lua | 2 ++ xmake/core/sandbox/modules/os.lua | 2 ++ 3 files changed, 56 insertions(+), 15 deletions(-) diff --git a/core/src/xmake/os/signal.c b/core/src/xmake/os/signal.c index 5d3fa5e3c..fa58b8dd7 100644 --- a/core/src/xmake/os/signal.c +++ b/core/src/xmake/os/signal.c @@ -49,6 +49,12 @@ typedef enum __xm_os_signal_e { XM_OS_SIGINT = 1 }xm_os_signal_e; +typedef enum __xm_os_signal_handler_e { + XM_OS_SIGFUN = 0, + XM_OS_SIGDFL = 1, + XM_OS_SIGIGN = 2, +}xm_os_signal_handler_e; + /* ////////////////////////////////////////////////////////////////////////////////////// * globals */ @@ -103,35 +109,66 @@ tb_int_t xm_os_signal(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); g_lua = lua; + tb_int_t handler = XM_OS_SIGFUN; // check signal handler - if (!lua_isfunction(lua, 2)) + if (lua_isnumber(lua, 2)) + handler = (tb_int_t) luaL_checkinteger(lua, 2); + else if (!lua_isfunction(lua, 2)) return 0; // save signal handler tb_int_t signo = (tb_int_t)luaL_checkinteger(lua, 1); - tb_char_t name[64] = {0}; - tb_snprintf(name, sizeof(name), "_SIGNAL_HANDLER_%d", signo); - lua_pushvalue(lua, 2); - lua_setglobal(lua, name); + if (handler == XM_OS_SIGFUN) + { + tb_char_t name[64] = {0}; + tb_snprintf(name, sizeof(name), "_SIGNAL_HANDLER_%d", signo); + lua_pushvalue(lua, 2); + lua_setglobal(lua, name); + } #if defined(TB_CONFIG_OS_WINDOWS) - if (signo == XM_OS_SIGINT) - SetConsoleCtrlHandler(xm_os_signal_handler, TRUE); -#elif defined(SIGINT) // for checking signal - tb_int_t signo_native = -1; + if (signo != XM_OS_SIGINT) + return 0; + + switch (handler) + { + case XM_OS_SIGFUN: + SetConsoleCtrlHandler(xm_os_signal_handler, TRUE); + break; + case XM_OS_SIGDFL: + SetConsoleCtrlHandler(NULL, FALSE); + break; + case XM_OS_SIGIGN: + SetConsoleCtrlHandler(NULL, TRUE); + break; + default: + break; + } +#elif defined(SIGINT) switch (signo) { case XM_OS_SIGINT: -#ifdef SIGINT - signo_native = SIGINT; -#endif + signo = SIGINT; break; default: - break; + return 0; + } + + switch (handler) + { + case XM_OS_SIGFUN: + signal(signo, xm_os_signal_handler); + break; + case XM_OS_SIGDFL: + signal(signo, SIG_DFL); + break; + case XM_OS_SIGIGN: + signal(signo, SIG_IGN); + break; + default: + break; } - if (signo_native >= 0) - signal(signo_native, xm_os_signal_handler); #endif return 0; } diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 836a8991f..f143b5f51 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -56,6 +56,8 @@ os.SYSERR_NOT_ACCESS = 3 -- signal code os.SIGINT = 1 +os.SIGDFL = 1 +os.SIGIGN = 2 -- copy single file or directory function os._cp(src, dst, rootdir, opt) diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index 5e2a94e92..9a12650f4 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -93,6 +93,8 @@ sandbox_os.SYSERR_NOT_ACCESS = os.SYSERR_NOT_ACCESS -- signal code sandbox_os.SIGINT = os.SIGINT +sandbox_os.SIGDFL = os.SIGDFL +sandbox_os.SIGIGN = os.SIGIGN -- copy file or directory function sandbox_os.cp(srcpath, dstpath, opt) -- cgit v1.3.1 From 536ad613e5c7b2ba8581c740548bf359b0fea91f Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 31 Mar 2024 21:49:38 +0800 Subject: add signal module --- xmake/core/base/os.lua | 11 ------ xmake/core/base/signal.lua | 46 ++++++++++++++++++++++ .../sandbox/modules/import/core/base/signal.lua | 24 +++++++++++ xmake/core/sandbox/modules/os.lua | 6 --- 4 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 xmake/core/base/signal.lua create mode 100644 xmake/core/sandbox/modules/import/core/base/signal.lua diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index f143b5f51..2d7023ac4 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -45,7 +45,6 @@ os._getenvs = os._getenvs or os.getenvs os._cpuinfo = os._cpuinfo or os.cpuinfo os._meminfo = os._meminfo or os.meminfo os._readlink = os._readlink or os.readlink -os._signal = os._signal or os.signal -- syserror code os.SYSERR_UNKNOWN = -1 @@ -54,11 +53,6 @@ os.SYSERR_NOT_PERM = 1 os.SYSERR_NOT_FILEDIR = 2 os.SYSERR_NOT_ACCESS = 3 --- signal code -os.SIGINT = 1 -os.SIGDFL = 1 -os.SIGIGN = 2 - -- copy single file or directory function os._cp(src, dst, rootdir, opt) opt = opt or {} @@ -1421,11 +1415,6 @@ function os.readlink(symlink) return os._readlink(path.absolute(symlink)) end --- register signal handler -function os.signal(signo, handler) - os._signal(signo, handler) -end - -- get the program directory function os.programdir() return xmake._PROGRAM_DIR diff --git a/xmake/core/base/signal.lua b/xmake/core/base/signal.lua new file mode 100644 index 000000000..afcbdb684 --- /dev/null +++ b/xmake/core/base/signal.lua @@ -0,0 +1,46 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file signal.lua +-- + +-- define module: signal +local signal = signal or {} + +-- load modules +local os = require("base/os") + +-- signal code +signal.SIGINT = 1 + +-- register signal handler +function signal.register(signo, handler) + os.signal(signo, handler) +end + +-- reset signal, SIGDFL +function signal.reset(signo) + os.signal(signo, 1) +end + +-- ignore signal, SIGIGN +function signal.ignore(signo) + os.signal(signo, 2) +end + +-- return module: signal +return signal diff --git a/xmake/core/sandbox/modules/import/core/base/signal.lua b/xmake/core/sandbox/modules/import/core/base/signal.lua new file mode 100644 index 000000000..2cf3fc0bb --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/base/signal.lua @@ -0,0 +1,24 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file signal.lua +-- + +-- load modules +return require("base/signal") + + diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index 9a12650f4..9004f66ee 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -82,7 +82,6 @@ sandbox_os.projectdir = os.projectdir sandbox_os.projectfile = os.projectfile sandbox_os.getwinsize = os.getwinsize sandbox_os.getpid = os.getpid -sandbox_os.signal = os.signal -- syserror code sandbox_os.SYSERR_UNKNOWN = os.SYSERR_UNKNOWN @@ -91,11 +90,6 @@ sandbox_os.SYSERR_NOT_PERM = os.SYSERR_NOT_PERM sandbox_os.SYSERR_NOT_FILEDIR = os.SYSERR_NOT_FILEDIR sandbox_os.SYSERR_NOT_ACCESS = os.SYSERR_NOT_ACCESS --- signal code -sandbox_os.SIGINT = os.SIGINT -sandbox_os.SIGDFL = os.SIGDFL -sandbox_os.SIGIGN = os.SIGIGN - -- copy file or directory function sandbox_os.cp(srcpath, dstpath, opt) assert(srcpath and dstpath) -- cgit v1.3.1 From 454048ab2a488f706801c4cc15a06272a8b005a8 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 31 Mar 2024 21:57:56 +0800 Subject: add signal test --- core/src/xmake/os/signal.c | 2 +- core/src/xmake/os/sleep.c | 7 ------- tests/modules/signal/sigint.lua | 8 ++++++++ xmake/core/base/signal.lua | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 tests/modules/signal/sigint.lua diff --git a/core/src/xmake/os/signal.c b/core/src/xmake/os/signal.c index fa58b8dd7..066cc7042 100644 --- a/core/src/xmake/os/signal.c +++ b/core/src/xmake/os/signal.c @@ -46,7 +46,7 @@ * types */ typedef enum __xm_os_signal_e { - XM_OS_SIGINT = 1 + XM_OS_SIGINT = 2 }xm_os_signal_e; typedef enum __xm_os_signal_handler_e { diff --git a/core/src/xmake/os/sleep.c b/core/src/xmake/os/sleep.c index 87d6340a7..e89ebff25 100644 --- a/core/src/xmake/os/sleep.c +++ b/core/src/xmake/os/sleep.c @@ -35,15 +35,8 @@ */ tb_int_t xm_os_sleep(lua_State* lua) { - // check tb_assert_and_check_return_val(lua, 0); - - // get the interval (ms) tb_long_t interval = (tb_long_t)luaL_checklong(lua, 1); - - // sleep it if (interval >= 0) tb_msleep(interval); - - // ok return 0; } diff --git a/tests/modules/signal/sigint.lua b/tests/modules/signal/sigint.lua new file mode 100644 index 000000000..5b4cb03a3 --- /dev/null +++ b/tests/modules/signal/sigint.lua @@ -0,0 +1,8 @@ +import("core.base.signal") + +function main() + signal.register(signal.SIGINT, function (signo) + print("signal.SIGINT(%d)", signo) + end) + io.read() +end diff --git a/xmake/core/base/signal.lua b/xmake/core/base/signal.lua index afcbdb684..e281bb1f4 100644 --- a/xmake/core/base/signal.lua +++ b/xmake/core/base/signal.lua @@ -25,7 +25,7 @@ local signal = signal or {} local os = require("base/os") -- signal code -signal.SIGINT = 1 +signal.SIGINT = 2 -- register signal handler function signal.register(signo, handler) -- cgit v1.3.1