From e02a05e5fa50f21a6d4128582bf8a15141d6a763 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 18 Dec 2021 23:17:45 +0800 Subject: fix term mode --- core/src/xmake/engine.c | 13 ++++ core/src/xmake/makefile | 3 +- core/src/xmake/tty/prefix.h | 32 ++++++++++ core/src/xmake/tty/term_mode.c | 70 ++++++++++++++++++++++ xmake/core/base/os.lua | 2 - xmake/core/base/tty.lua | 20 +++++++ .../action/require/impl/install_packages.lua | 9 +++ 7 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 core/src/xmake/tty/prefix.h create mode 100644 core/src/xmake/tty/term_mode.c diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index f407f2cd6..e87fce465 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -232,6 +232,9 @@ tb_int_t xm_libc_dataptr(lua_State* lua); tb_int_t xm_libc_byteof(lua_State* lua); tb_int_t xm_libc_setbyte(lua_State* lua); +// the tty functions +tb_int_t xm_tty_term_mode(lua_State* lua); + #ifdef XM_CONFIG_API_HAVE_CURSES // register curses __tb_extern_c_enter__ @@ -438,6 +441,13 @@ static luaL_Reg const g_libc_functions[] = , { tb_null, tb_null } }; +// the tty functions +static luaL_Reg const g_tty_functions[] = +{ + { "term_mode", xm_tty_term_mode } +, { tb_null, tb_null } +}; + /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ @@ -883,6 +893,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c // bind libc functions xm_lua_register(engine->lua, "libc", g_libc_functions); + // bind tty functions + xm_lua_register(engine->lua, "tty", g_tty_functions); + #ifdef XM_CONFIG_API_HAVE_CURSES // bind curses xm_curses_register(engine->lua); diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 9e0a3decb..c85696f1e 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -123,7 +123,8 @@ xmake_C_FILES += \ libc/dataptr \ libc/byteof \ libc/setbyte \ - libc/strndup + libc/strndup \ + tty/term_mode iswin = ifeq ($(PLAT),windows) diff --git a/core/src/xmake/tty/prefix.h b/core/src/xmake/tty/prefix.h new file mode 100644 index 000000000..82a2c3a76 --- /dev/null +++ b/core/src/xmake/tty/prefix.h @@ -0,0 +1,32 @@ +/*!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 prefix.h + * + */ +#ifndef XM_PATH_PREFIX_H +#define XM_PATH_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif + + diff --git a/core/src/xmake/tty/term_mode.c b/core/src/xmake/tty/term_mode.c new file mode 100644 index 000000000..c4500fdf5 --- /dev/null +++ b/core/src/xmake/tty/term_mode.c @@ -0,0 +1,70 @@ +/*!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 term_mode.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "term_mode" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_OS_WINDOWS +# include +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +/* local oldmode = tty.term_mode(stdtype) + * local oldmode = tty.term_mode(stdtype, newmode) + */ +tb_int_t xm_tty_term_mode(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // get std type, (stdin: 1, stdout: 2, stderr: 3) + tb_int_t stdtype = (tb_int_t)luaL_checkinteger(lua, 1); + + // get terminal mode + DWORD mode = 0; +#ifdef TB_CONFIG_OS_WINDOWS + HANDLE console_handle; + switch (stdtype) + { + case 1: console_handle = GetStdHandle(STD_INPUT_HANDLE); break; + case 2: console_handle = GetStdHandle(STD_OUTPUT_HANDLE); break; + case 3: console_handle = GetStdHandle(STD_ERROR_HANDLE); break; + } + GetConsoleMode(console_handle, &mode); + if (lua_isinteger(lua, 2)) + { + tb_int_t newmode = (tb_int_t)lua_tointeger(lua, 2); + SetConsoleMode(console_handle, (DWORD)newmode); + } +#endif + lua_pushinteger(lua, (tb_int_t)mode); + return 1; +} diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 5c3057e5a..18b59f61c 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -761,8 +761,6 @@ function os.execv(program, argv, opt) -- cannot execute process return nil, os.strerror() end - - -- ok? return ok end diff --git a/xmake/core/base/tty.lua b/xmake/core/base/tty.lua index 0417f9b8f..a744f12e0 100644 --- a/xmake/core/base/tty.lua +++ b/xmake/core/base/tty.lua @@ -24,6 +24,9 @@ local tty = tty or {} -- load modules local io = require("base/io") +-- save metatable and builtin functions +tty._term_mode = tty._term_mode or tty.term_mode + -- @see http://www.termsys.demon.co.uk/vtansi.htm -- write control characters @@ -392,5 +395,22 @@ function tty.has_color24() return has_color24 end +-- get term mode, e.g. stdin, stdout, stderr +-- +-- local oldmode = tty.term_mode(stdtype) +-- local oldmode = tty.term_mode(stdtype, newmode) +-- +function tty.term_mode(stdtype, newmode) + local oldmode = 0 + if stdtype == "stdin" then + oldmode = tty._term_mode(1, newmode) + elseif stdtype == "stdout" then + oldmode = tty._term_mode(2, newmode) + elseif stdtype == "stderr" then + oldmode = tty._term_mode(3, newmode) + end + return oldmode +end + -- return module return tty diff --git a/xmake/modules/private/action/require/impl/install_packages.lua b/xmake/modules/private/action/require/impl/install_packages.lua index 714b98461..f62a0fb52 100644 --- a/xmake/modules/private/action/require/impl/install_packages.lua +++ b/xmake/modules/private/action/require/impl/install_packages.lua @@ -268,6 +268,9 @@ function _install_packages(packages_install, packages_download, installdeps) packages_installed[tostring(instance)] = false end + -- save terminal mode for stdout, @see https://github.com/xmake-io/xmake/issues/1924 + local term_mode_stdout = tty.term_mode("stdout") + -- do install local progress_helper = show_wait and progress.new() or nil local packages_installing = {} @@ -438,6 +441,12 @@ function _install_packages(packages_install, packages_download, installdeps) end end + -- fix terminal mode to avoid some subprocess to change it + -- @see https://github.com/xmake-io/xmake/issues/1924 + if term_mode_stdout ~= tty.term_mode("stdout") then + tty.term_mode("stdout", term_mode_stdout) + end + -- trace progress_helper:clear() tty.erase_line_to_start().cr() -- cgit v1.3.1 From ad540b6b68843b139be61dc98b84de1ad354d564 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 18 Dec 2021 23:21:48 +0800 Subject: fix compiler error --- core/src/xmake/tty/term_mode.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/src/xmake/tty/term_mode.c b/core/src/xmake/tty/term_mode.c index c4500fdf5..66a3887ea 100644 --- a/core/src/xmake/tty/term_mode.c +++ b/core/src/xmake/tty/term_mode.c @@ -49,8 +49,8 @@ tb_int_t xm_tty_term_mode(lua_State* lua) tb_int_t stdtype = (tb_int_t)luaL_checkinteger(lua, 1); // get terminal mode - DWORD mode = 0; #ifdef TB_CONFIG_OS_WINDOWS + DWORD mode = 0; HANDLE console_handle; switch (stdtype) { @@ -64,6 +64,8 @@ tb_int_t xm_tty_term_mode(lua_State* lua) tb_int_t newmode = (tb_int_t)lua_tointeger(lua, 2); SetConsoleMode(console_handle, (DWORD)newmode); } +#else + tb_int_t mode = 0; #endif lua_pushinteger(lua, (tb_int_t)mode); return 1; -- cgit v1.3.1 From 0c9e6e48e08bbfdfb029aeb1a8228a32fa047762 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 18 Dec 2021 23:24:06 +0800 Subject: update macro --- core/src/xmake/tty/prefix.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/xmake/tty/prefix.h b/core/src/xmake/tty/prefix.h index 82a2c3a76..d1fa5ba07 100644 --- a/core/src/xmake/tty/prefix.h +++ b/core/src/xmake/tty/prefix.h @@ -18,8 +18,8 @@ * @file prefix.h * */ -#ifndef XM_PATH_PREFIX_H -#define XM_PATH_PREFIX_H +#ifndef XM_TTY_PREFIX_H +#define XM_TTY_PREFIX_H /* ////////////////////////////////////////////////////////////////////////////////////// * includes -- cgit v1.3.1 From 06a82b1a794b0481a91db073754f188d54b0d87f Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 18 Dec 2021 23:43:19 +0800 Subject: Update term_mode.c --- core/src/xmake/tty/term_mode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/xmake/tty/term_mode.c b/core/src/xmake/tty/term_mode.c index 66a3887ea..e035236a5 100644 --- a/core/src/xmake/tty/term_mode.c +++ b/core/src/xmake/tty/term_mode.c @@ -45,11 +45,11 @@ tb_int_t xm_tty_term_mode(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); +#ifdef TB_CONFIG_OS_WINDOWS + // get std type, (stdin: 1, stdout: 2, stderr: 3) tb_int_t stdtype = (tb_int_t)luaL_checkinteger(lua, 1); - // get terminal mode -#ifdef TB_CONFIG_OS_WINDOWS DWORD mode = 0; HANDLE console_handle; switch (stdtype) -- cgit v1.3.1 From 2ab866f4457c9074255d0104b32fce420bb8b13e Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 19 Dec 2021 00:16:02 +0800 Subject: fix compile errors --- core/src/xmake/tty/term_mode.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/xmake/tty/term_mode.c b/core/src/xmake/tty/term_mode.c index e035236a5..975c8fc37 100644 --- a/core/src/xmake/tty/term_mode.c +++ b/core/src/xmake/tty/term_mode.c @@ -50,6 +50,7 @@ tb_int_t xm_tty_term_mode(lua_State* lua) // get std type, (stdin: 1, stdout: 2, stderr: 3) tb_int_t stdtype = (tb_int_t)luaL_checkinteger(lua, 1); + // get and set terminal mode DWORD mode = 0; HANDLE console_handle; switch (stdtype) @@ -59,7 +60,7 @@ tb_int_t xm_tty_term_mode(lua_State* lua) case 3: console_handle = GetStdHandle(STD_ERROR_HANDLE); break; } GetConsoleMode(console_handle, &mode); - if (lua_isinteger(lua, 2)) + if (lua_isnumber(lua, 2)) { tb_int_t newmode = (tb_int_t)lua_tointeger(lua, 2); SetConsoleMode(console_handle, (DWORD)newmode); -- cgit v1.3.1