diff options
| author | ruki <[email protected]> | 2025-12-15 23:24:55 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-12-15 23:24:55 +0800 |
| commit | b9116ea123e77faec8a4a0cfbfcbb38f2ebc2e66 (patch) | |
| tree | bc8976c77263b6941f181fef460dad43dc2f5ee6 /core/src/xmake | |
| parent | fb06feeb88ecd2512a4a737d7e543b88892ad783 (diff) | |
add tty.session_id
Diffstat (limited to 'core/src/xmake')
| -rw-r--r-- | core/src/xmake/engine.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/tty/session_id.c | 65 |
2 files changed, 67 insertions, 0 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 530071a68..ea9dc2af1 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -330,6 +330,7 @@ tb_int_t xm_libc_setbyte(lua_State *lua); // the tty functions tb_int_t xm_tty_term_mode(lua_State *lua); +tb_int_t xm_tty_session_id(lua_State *lua); // the package functions tb_int_t xm_package_loadxmi(lua_State *lua); @@ -649,6 +650,7 @@ static luaL_Reg const g_libc_functions[] = { // the tty functions static luaL_Reg const g_tty_functions[] = { { "term_mode", xm_tty_term_mode }, + { "session_id", xm_tty_session_id }, { tb_null, tb_null }, }; diff --git a/core/src/xmake/tty/session_id.c b/core/src/xmake/tty/session_id.c new file mode 100644 index 000000000..d075a624a --- /dev/null +++ b/core/src/xmake/tty/session_id.c @@ -0,0 +1,65 @@ +/*!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, Xmake Open Source Community. + * + * @author ruki + * @file session_id.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "session_id" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifdef TB_CONFIG_OS_WINDOWS +#include <windows.h> +#else +#include <unistd.h> +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// tty.session_id() +tb_int_t xm_tty_session_id(lua_State *lua) { + tb_assert_and_check_return_val(lua, 0); + +#ifdef TB_CONFIG_OS_WINDOWS + HWND hwnd = GetConsoleWindow(); + if (hwnd) { + // use hex string of hwnd as session id + lua_pushfstring(lua, "%p", hwnd); + return 1; + } +#else + // we use the tty name as session id + tb_char_t const* name = ttyname(STDIN_FILENO); + if (name) { + lua_pushstring(lua, name); + return 1; + } +#endif + + // failed + lua_pushnil(lua); + return 1; +} |
