summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-12-19 00:53:36 +0800
committerGitHub <[email protected]>2021-12-19 00:53:36 +0800
commitcbff24314b4fb76b2617f56d4f202389feb259fd (patch)
treeccfef243ae29218ea696315f7959d51feee92676
parent7be1bc272914b757a4a7fe1792f7590de4a91cdf (diff)
parent2ab866f4457c9074255d0104b32fce420bb8b13e (diff)
Merge pull request #1926 from xmake-io/term_mode
fix term mode
-rw-r--r--core/src/xmake/engine.c13
-rw-r--r--core/src/xmake/makefile3
-rw-r--r--core/src/xmake/tty/prefix.h32
-rw-r--r--core/src/xmake/tty/term_mode.c73
-rw-r--r--xmake/core/base/os.lua2
-rw-r--r--xmake/core/base/tty.lua20
-rw-r--r--xmake/modules/private/action/require/impl/install_packages.lua9
7 files changed, 149 insertions, 3 deletions
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..d1fa5ba07
--- /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_TTY_PREFIX_H
+#define XM_TTY_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..975c8fc37
--- /dev/null
+++ b/core/src/xmake/tty/term_mode.c
@@ -0,0 +1,73 @@
+/*!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 <windows.h>
+#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);
+
+#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 and set terminal mode
+ DWORD mode = 0;
+ 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_isnumber(lua, 2))
+ {
+ 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;
+}
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()