From c8a1edb15828a188b2c2e2139a366aad006aaf0e Mon Sep 17 00:00:00 2001 From: TitanSnow Date: Wed, 10 May 2017 07:05:41 +0800 Subject: add module readline --- core/src/xmake/machine.c | 19 +++++++++++ core/src/xmake/makefile | 3 +- core/src/xmake/readline/prefix.h | 41 ++++++++++++++++++++++++ core/src/xmake/readline/readline.c | 65 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 core/src/xmake/readline/prefix.h create mode 100644 core/src/xmake/readline/readline.c (limited to 'core/src') diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index b9977d92a..8eca36a8b 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -103,6 +103,11 @@ tb_int_t xm_process_close(lua_State* lua); // the sandbox functions tb_int_t xm_sandbox_interactive(lua_State* lua); +#ifdef XM_CONFIG_API_HAVE_READLINE +// the readline functions +tb_int_t xm_readline_readline(lua_State* lua); +#endif + /* ////////////////////////////////////////////////////////////////////////////////////// * globals */ @@ -173,6 +178,15 @@ static luaL_Reg const g_sandbox_functions[] = , { tb_null, tb_null } }; +#ifdef XM_CONFIG_API_HAVE_READLINE +// the readline functions +static luaL_Reg const g_readline_functions[] = +{ + { "readline", xm_readline_readline } +, { tb_null, tb_null } +}; +#endif + /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ @@ -379,6 +393,11 @@ xm_machine_ref_t xm_machine_init() // bind sandbox functions luaL_register(impl->lua, "sandbox", g_sandbox_functions); +#ifdef XM_CONFIG_API_HAVE_READLINE + // bind readline functions + luaL_register(impl->lua, "readline", g_readline_functions); +#endif + // init host #if defined(TB_CONFIG_OS_WINDOWS) lua_pushstring(impl->lua, "windows"); diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 0d41f4439..49de9cc8e 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -49,7 +49,8 @@ xmake_C_FILES += \ process/wait \ process/waitlist \ process/close \ - sandbox/interactive + sandbox/interactive \ + readline/readline # flags diff --git a/core/src/xmake/readline/prefix.h b/core/src/xmake/readline/prefix.h new file mode 100644 index 000000000..626cde146 --- /dev/null +++ b/core/src/xmake/readline/prefix.h @@ -0,0 +1,41 @@ +/*!The Make-like Build Utility based on Lua + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 - 2017, TBOOX Open Source Group. + * + * @author TitanSnow + * @file prefix.h + * + */ +#ifndef XM_READLINE_PREFIX_H +#define XM_READLINE_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" +#ifdef XM_CONFIG_API_HAVE_READLINE +# include // on some OS (like centos) required +# include +# include +#endif + + +#endif + + diff --git a/core/src/xmake/readline/readline.c b/core/src/xmake/readline/readline.c new file mode 100644 index 000000000..68027a4ec --- /dev/null +++ b/core/src/xmake/readline/readline.c @@ -0,0 +1,65 @@ +/*!The Make-like Build Utility based on Lua + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 - 2017, TBOOX Open Source Group. + * + * @author TitanSnow + * @file readline.c + * + */ + +#ifdef XM_CONFIG_API_HAVE_READLINE + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "readline" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// readline wrapper +tb_int_t xm_readline_readline(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // get the prompt + tb_char_t const* prompt = tb_null; + if (luaL_typename(lua, 1) != "nil") + prompt = luaL_checkstring(lua, 1); + + // call readline + tb_char_t const* line = readline(prompt); + if (line) + lua_pushstring(lua, line); + else + lua_pushnil(lua); + + // ok + return 1; +} + +#endif -- cgit v1.3.1 From ff2327bbe5533fe9656ed6f630caad69877b4941 Mon Sep 17 00:00:00 2001 From: TitanSnow Date: Wed, 10 May 2017 10:09:45 +0800 Subject: add readline.get_history_state() --- core/src/xmake/machine.c | 6 +- core/src/xmake/makefile | 3 +- core/src/xmake/readline/get_history_state.c | 92 ++++++++++++++++++++++ xmake/core/sandbox/modules/import/lib/readline.lua | 1 + 4 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 core/src/xmake/readline/get_history_state.c (limited to 'core/src') diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 8eca36a8b..e31a75abf 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -106,6 +106,7 @@ tb_int_t xm_sandbox_interactive(lua_State* lua); #ifdef XM_CONFIG_API_HAVE_READLINE // the readline functions tb_int_t xm_readline_readline(lua_State* lua); +tb_int_t xm_readline_get_history_state(lua_State* lua); #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -182,8 +183,9 @@ static luaL_Reg const g_sandbox_functions[] = // the readline functions static luaL_Reg const g_readline_functions[] = { - { "readline", xm_readline_readline } -, { tb_null, tb_null } + { "readline", xm_readline_readline } +, { "get_history_state", xm_readline_get_history_state } +, { tb_null, tb_null } }; #endif diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 49de9cc8e..6150c564d 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -50,7 +50,8 @@ xmake_C_FILES += \ process/waitlist \ process/close \ sandbox/interactive \ - readline/readline + readline/readline \ + readline/get_history_state # flags diff --git a/core/src/xmake/readline/get_history_state.c b/core/src/xmake/readline/get_history_state.c new file mode 100644 index 000000000..5b2acc4ac --- /dev/null +++ b/core/src/xmake/readline/get_history_state.c @@ -0,0 +1,92 @@ +/*!The Make-like Build Utility based on Lua + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 - 2017, TBOOX Open Source Group. + * + * @author TitanSnow + * @file get_history_state.c + * + */ + +#ifdef XM_CONFIG_API_HAVE_READLINE + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "get_history_state" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// get_history_state wrapper +tb_int_t xm_readline_get_history_state(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // HISTORY_STATE table + lua_newtable(lua); + HISTORY_STATE *hs = history_get_history_state(); + // field offset + lua_pushstring(lua, "offset"); + lua_pushinteger(lua, hs -> offset); + lua_settable(lua, -3); + // field length + lua_pushstring(lua, "length"); + lua_pushinteger(lua, hs -> length); + lua_settable(lua, -3); + // field size + lua_pushstring(lua, "size"); + lua_pushinteger(lua, hs -> size); + lua_settable(lua, -3); + // field flags + lua_pushstring(lua, "flags"); + lua_pushinteger(lua, hs -> flags); + lua_settable(lua, -3); + // field entries + lua_pushstring(lua, "entries"); + lua_newtable(lua); + for (HIST_ENTRY **p = hs -> entries; *p; ++p) + { + lua_newtable(lua); + // field line + lua_pushstring(lua, "line"); + lua_pushstring(lua, (*p) -> line); + lua_settable(lua, -3); + // field timestamp + lua_pushstring(lua, "timestamp"); + lua_pushstring(lua, (*p) -> timestamp); + lua_settable(lua, -3); + + // set back + lua_rawseti(lua, -2, p - hs -> entries + 1); + } + lua_settable(lua, -3); + + // ok + return 1; +} + +#endif diff --git a/xmake/core/sandbox/modules/import/lib/readline.lua b/xmake/core/sandbox/modules/import/lib/readline.lua index d1b721ee2..15b51af76 100644 --- a/xmake/core/sandbox/modules/import/lib/readline.lua +++ b/xmake/core/sandbox/modules/import/lib/readline.lua @@ -31,6 +31,7 @@ end -- inherit some builtin interfaces sandbox_readline.readline = readline.readline +sandbox_readline.get_history_state = readline.get_history_state -- return module return sandbox_readline -- cgit v1.3.1 From b0807b28f1d1bf5fc93b692f58ec3bdd60f2a273 Mon Sep 17 00:00:00 2001 From: TitanSnow Date: Wed, 10 May 2017 10:15:24 +0800 Subject: free the line got from readline --- core/src/xmake/readline/readline.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'core/src') diff --git a/core/src/xmake/readline/readline.c b/core/src/xmake/readline/readline.c index 68027a4ec..c0088a582 100644 --- a/core/src/xmake/readline/readline.c +++ b/core/src/xmake/readline/readline.c @@ -58,6 +58,9 @@ tb_int_t xm_readline_readline(lua_State* lua) else lua_pushnil(lua); + // free it + tb_free((void*)line); + // ok return 1; } -- cgit v1.3.1 From 1fe068e98cac89e96c2f42f3a4685ce267dd33b5 Mon Sep 17 00:00:00 2001 From: TitanSnow Date: Wed, 10 May 2017 12:32:23 +0800 Subject: add readline.add_history() --- core/src/xmake/machine.c | 2 + core/src/xmake/makefile | 3 +- core/src/xmake/readline/add_history.c | 63 ++++++++++++++++++++++ xmake/core/sandbox/modules/import/lib/readline.lua | 1 + 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 core/src/xmake/readline/add_history.c (limited to 'core/src') diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index e31a75abf..5a8a7b4b8 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -107,6 +107,7 @@ tb_int_t xm_sandbox_interactive(lua_State* lua); // the readline functions tb_int_t xm_readline_readline(lua_State* lua); tb_int_t xm_readline_get_history_state(lua_State* lua); +tb_int_t xm_readline_add_history(lua_State* lua); #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -185,6 +186,7 @@ static luaL_Reg const g_readline_functions[] = { { "readline", xm_readline_readline } , { "get_history_state", xm_readline_get_history_state } +, { "add_history", xm_readline_add_history } , { tb_null, tb_null } }; #endif diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 6150c564d..930f58df6 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -51,7 +51,8 @@ xmake_C_FILES += \ process/close \ sandbox/interactive \ readline/readline \ - readline/get_history_state + readline/get_history_state \ + readline/add_history # flags diff --git a/core/src/xmake/readline/add_history.c b/core/src/xmake/readline/add_history.c new file mode 100644 index 000000000..1b466ca6a --- /dev/null +++ b/core/src/xmake/readline/add_history.c @@ -0,0 +1,63 @@ +/*!The Make-like Build Utility based on Lua + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 - 2017, TBOOX Open Source Group. + * + * @author TitanSnow + * @file add_history.c + * + */ + +#ifdef XM_CONFIG_API_HAVE_READLINE + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "add_history" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// add_history wrapper +tb_int_t xm_readline_add_history(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // get history + tb_char_t const* history = luaL_checkstring(lua, 1); + tb_check_return_val(history, 0); + + // call add_history + add_history(history); + + // pushnil + lua_pushnil(lua); + + // ok + return 1; +} + +#endif diff --git a/xmake/core/sandbox/modules/import/lib/readline.lua b/xmake/core/sandbox/modules/import/lib/readline.lua index 15b51af76..02d183bc4 100644 --- a/xmake/core/sandbox/modules/import/lib/readline.lua +++ b/xmake/core/sandbox/modules/import/lib/readline.lua @@ -32,6 +32,7 @@ end -- inherit some builtin interfaces sandbox_readline.readline = readline.readline sandbox_readline.get_history_state = readline.get_history_state +sandbox_readline.add_history = readline.add_history -- return module return sandbox_readline -- cgit v1.3.1 From 74084408890a446c97376d80f2b342fbab96191e Mon Sep 17 00:00:00 2001 From: TitanSnow Date: Wed, 10 May 2017 13:17:39 +0800 Subject: add readline.clear_history() --- core/src/xmake/machine.c | 2 + core/src/xmake/makefile | 3 +- core/src/xmake/readline/clear_history.c | 59 ++++++++++++++++++++++ xmake/core/sandbox/modules/import/lib/readline.lua | 1 + 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 core/src/xmake/readline/clear_history.c (limited to 'core/src') diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 5a8a7b4b8..50da4ea7c 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -108,6 +108,7 @@ tb_int_t xm_sandbox_interactive(lua_State* lua); tb_int_t xm_readline_readline(lua_State* lua); tb_int_t xm_readline_get_history_state(lua_State* lua); tb_int_t xm_readline_add_history(lua_State* lua); +tb_int_t xm_readline_clear_history(lua_State* lua); #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -187,6 +188,7 @@ static luaL_Reg const g_readline_functions[] = { "readline", xm_readline_readline } , { "get_history_state", xm_readline_get_history_state } , { "add_history", xm_readline_add_history } +, { "clear_history", xm_readline_clear_history } , { tb_null, tb_null } }; #endif diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 930f58df6..7f800d2d7 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -52,7 +52,8 @@ xmake_C_FILES += \ sandbox/interactive \ readline/readline \ readline/get_history_state \ - readline/add_history + readline/add_history \ + readline/clear_history # flags diff --git a/core/src/xmake/readline/clear_history.c b/core/src/xmake/readline/clear_history.c new file mode 100644 index 000000000..fbab27d5b --- /dev/null +++ b/core/src/xmake/readline/clear_history.c @@ -0,0 +1,59 @@ +/*!The Make-like Build Utility based on Lua + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 - 2017, TBOOX Open Source Group. + * + * @author TitanSnow + * @file clear_history.c + * + */ + +#ifdef XM_CONFIG_API_HAVE_READLINE + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "clear_history" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// clear_history wrapper +tb_int_t xm_readline_clear_history(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // call clear_history + clear_history(); + + // pushnil + lua_pushnil(lua); + + // ok + return 1; +} + +#endif diff --git a/xmake/core/sandbox/modules/import/lib/readline.lua b/xmake/core/sandbox/modules/import/lib/readline.lua index 02d183bc4..d96381413 100644 --- a/xmake/core/sandbox/modules/import/lib/readline.lua +++ b/xmake/core/sandbox/modules/import/lib/readline.lua @@ -33,6 +33,7 @@ end sandbox_readline.readline = readline.readline sandbox_readline.get_history_state = readline.get_history_state sandbox_readline.add_history = readline.add_history +sandbox_readline.clear_history = readline.clear_history -- return module return sandbox_readline -- cgit v1.3.1 From 48f6176b8bf318ab6e03321168b36352a5763ad7 Mon Sep 17 00:00:00 2001 From: TitanSnow Date: Wed, 10 May 2017 14:29:26 +0800 Subject: add host, arch, nuldev to os.versioninfo() --- core/src/xmake/os/versioninfo.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'core/src') diff --git a/core/src/xmake/os/versioninfo.c b/core/src/xmake/os/versioninfo.c index ea75049c3..cc8504a11 100644 --- a/core/src/xmake/os/versioninfo.c +++ b/core/src/xmake/os/versioninfo.c @@ -228,6 +228,45 @@ tb_int_t xm_os_versioninfo(lua_State* lua) // set back to table lua_settable(lua, -3); + lua_pushstring(lua, "host"); + // init host +#if defined(TB_CONFIG_OS_WINDOWS) + lua_pushstring(lua, "windows"); +#elif defined(TB_CONFIG_OS_MACOSX) + lua_pushstring(lua, "macosx"); +#elif defined(TB_CONFIG_OS_LINUX) + lua_pushstring(lua, "linux"); +#elif defined(TB_CONFIG_OS_IOS) + lua_pushstring(lua, "ios"); +#elif defined(TB_CONFIG_OS_ANDROID) + lua_pushstring(lua, "android"); +#elif defined(TB_CONFIG_OS_LIKE_UNIX) + lua_pushstring(lua, "unix"); +#else + lua_pushstring(lua, "unknown"); +#endif + lua_settable(lua, -3); + + lua_pushstring(lua, "arch"); + // init architecture +#if defined(TB_ARCH_x86) || defined(TB_CONFIG_OS_WINDOWS) + lua_pushstring(lua, "i386"); +#elif defined(TB_ARCH_x64) + lua_pushstring(lua, "x86_64"); +#else + lua_pushstring(lua, TB_ARCH_STRING); +#endif + lua_settable(lua, -3); + + lua_pushstring(lua, "nuldev"); + // init redirect to null +#if defined(TB_CONFIG_OS_WINDOWS) + lua_pushstring(lua, "nul"); +#else + lua_pushstring(lua, "/dev/null"); +#endif + lua_settable(lua, -3); + // ok return 1; } -- cgit v1.3.1 From 067d960804f438b5759f6b094a929a94b20b4deb Mon Sep 17 00:00:00 2001 From: TitanSnow Date: Wed, 10 May 2017 15:02:52 +0800 Subject: fix self-build --- core/src/xmake/readline/add_history.c | 4 ++-- core/src/xmake/readline/clear_history.c | 4 ++-- core/src/xmake/readline/get_history_state.c | 4 ++-- core/src/xmake/readline/readline.c | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/readline/add_history.c b/core/src/xmake/readline/add_history.c index 1b466ca6a..57f947d0c 100644 --- a/core/src/xmake/readline/add_history.c +++ b/core/src/xmake/readline/add_history.c @@ -23,8 +23,6 @@ * */ -#ifdef XM_CONFIG_API_HAVE_READLINE - /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ @@ -36,6 +34,8 @@ */ #include "prefix.h" +#ifdef XM_CONFIG_API_HAVE_READLINE + /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ diff --git a/core/src/xmake/readline/clear_history.c b/core/src/xmake/readline/clear_history.c index fbab27d5b..7517e2e43 100644 --- a/core/src/xmake/readline/clear_history.c +++ b/core/src/xmake/readline/clear_history.c @@ -23,8 +23,6 @@ * */ -#ifdef XM_CONFIG_API_HAVE_READLINE - /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ @@ -36,6 +34,8 @@ */ #include "prefix.h" +#ifdef XM_CONFIG_API_HAVE_READLINE + /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ diff --git a/core/src/xmake/readline/get_history_state.c b/core/src/xmake/readline/get_history_state.c index 5b2acc4ac..184f0e9fc 100644 --- a/core/src/xmake/readline/get_history_state.c +++ b/core/src/xmake/readline/get_history_state.c @@ -23,8 +23,6 @@ * */ -#ifdef XM_CONFIG_API_HAVE_READLINE - /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ @@ -36,6 +34,8 @@ */ #include "prefix.h" +#ifdef XM_CONFIG_API_HAVE_READLINE + /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ diff --git a/core/src/xmake/readline/readline.c b/core/src/xmake/readline/readline.c index c0088a582..c52ff02aa 100644 --- a/core/src/xmake/readline/readline.c +++ b/core/src/xmake/readline/readline.c @@ -23,8 +23,6 @@ * */ -#ifdef XM_CONFIG_API_HAVE_READLINE - /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ @@ -36,6 +34,8 @@ */ #include "prefix.h" +#ifdef XM_CONFIG_API_HAVE_READLINE + /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ @@ -48,7 +48,7 @@ tb_int_t xm_readline_readline(lua_State* lua) // get the prompt tb_char_t const* prompt = tb_null; - if (luaL_typename(lua, 1) != "nil") + if (tb_strcmp("nil", luaL_typename(lua, 1)) != 0) prompt = luaL_checkstring(lua, 1); // call readline -- cgit v1.3.1 From 5894c5279ca8fc5757eb697bfd901889a4b9e065 Mon Sep 17 00:00:00 2001 From: TitanSnow Date: Wed, 10 May 2017 15:43:55 +0800 Subject: get_history_state -> history_list --- core/src/xmake/machine.c | 12 +-- core/src/xmake/makefile | 2 +- core/src/xmake/readline/get_history_state.c | 92 ---------------------- core/src/xmake/readline/history_list.c | 72 +++++++++++++++++ xmake/core/sandbox/modules/import/lib/readline.lua | 2 +- xmake/plugins/lua/xmake.lua | 2 +- 6 files changed, 81 insertions(+), 101 deletions(-) delete mode 100644 core/src/xmake/readline/get_history_state.c create mode 100644 core/src/xmake/readline/history_list.c (limited to 'core/src') diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 50da4ea7c..4001af365 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -106,7 +106,7 @@ tb_int_t xm_sandbox_interactive(lua_State* lua); #ifdef XM_CONFIG_API_HAVE_READLINE // the readline functions tb_int_t xm_readline_readline(lua_State* lua); -tb_int_t xm_readline_get_history_state(lua_State* lua); +tb_int_t xm_readline_history_list(lua_State* lua); tb_int_t xm_readline_add_history(lua_State* lua); tb_int_t xm_readline_clear_history(lua_State* lua); #endif @@ -185,11 +185,11 @@ static luaL_Reg const g_sandbox_functions[] = // the readline functions static luaL_Reg const g_readline_functions[] = { - { "readline", xm_readline_readline } -, { "get_history_state", xm_readline_get_history_state } -, { "add_history", xm_readline_add_history } -, { "clear_history", xm_readline_clear_history } -, { tb_null, tb_null } + { "readline", xm_readline_readline } +, { "history_list", xm_readline_history_list } +, { "add_history", xm_readline_add_history } +, { "clear_history", xm_readline_clear_history} +, { tb_null, tb_null } }; #endif diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 7f800d2d7..0b9770bf6 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -51,7 +51,7 @@ xmake_C_FILES += \ process/close \ sandbox/interactive \ readline/readline \ - readline/get_history_state \ + readline/history_list \ readline/add_history \ readline/clear_history diff --git a/core/src/xmake/readline/get_history_state.c b/core/src/xmake/readline/get_history_state.c deleted file mode 100644 index 184f0e9fc..000000000 --- a/core/src/xmake/readline/get_history_state.c +++ /dev/null @@ -1,92 +0,0 @@ -/*!The Make-like Build Utility based on Lua - * - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 - 2017, TBOOX Open Source Group. - * - * @author TitanSnow - * @file get_history_state.c - * - */ - -/* ////////////////////////////////////////////////////////////////////////////////////// - * trace - */ -#define TB_TRACE_MODULE_NAME "get_history_state" -#define TB_TRACE_MODULE_DEBUG (0) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "prefix.h" - -#ifdef XM_CONFIG_API_HAVE_READLINE - -/* ////////////////////////////////////////////////////////////////////////////////////// - * implementation - */ - -// get_history_state wrapper -tb_int_t xm_readline_get_history_state(lua_State* lua) -{ - // check - tb_assert_and_check_return_val(lua, 0); - - // HISTORY_STATE table - lua_newtable(lua); - HISTORY_STATE *hs = history_get_history_state(); - // field offset - lua_pushstring(lua, "offset"); - lua_pushinteger(lua, hs -> offset); - lua_settable(lua, -3); - // field length - lua_pushstring(lua, "length"); - lua_pushinteger(lua, hs -> length); - lua_settable(lua, -3); - // field size - lua_pushstring(lua, "size"); - lua_pushinteger(lua, hs -> size); - lua_settable(lua, -3); - // field flags - lua_pushstring(lua, "flags"); - lua_pushinteger(lua, hs -> flags); - lua_settable(lua, -3); - // field entries - lua_pushstring(lua, "entries"); - lua_newtable(lua); - for (HIST_ENTRY **p = hs -> entries; *p; ++p) - { - lua_newtable(lua); - // field line - lua_pushstring(lua, "line"); - lua_pushstring(lua, (*p) -> line); - lua_settable(lua, -3); - // field timestamp - lua_pushstring(lua, "timestamp"); - lua_pushstring(lua, (*p) -> timestamp); - lua_settable(lua, -3); - - // set back - lua_rawseti(lua, -2, p - hs -> entries + 1); - } - lua_settable(lua, -3); - - // ok - return 1; -} - -#endif diff --git a/core/src/xmake/readline/history_list.c b/core/src/xmake/readline/history_list.c new file mode 100644 index 000000000..68597b945 --- /dev/null +++ b/core/src/xmake/readline/history_list.c @@ -0,0 +1,72 @@ +/*!The Make-like Build Utility based on Lua + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 - 2017, TBOOX Open Source Group. + * + * @author TitanSnow + * @file history_list.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "history_list" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +#ifdef XM_CONFIG_API_HAVE_READLINE + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// history_list wrapper +tb_int_t xm_readline_history_list(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // history list + lua_newtable(lua); + tb_int_t i = 1; + for (HIST_ENTRY **p = history_list(); *p; ++p, ++i) + { + lua_newtable(lua); + // field line + lua_pushstring(lua, "line"); + lua_pushstring(lua, (*p) -> line); + lua_settable(lua, -3); + // field timestamp + lua_pushstring(lua, "timestamp"); + lua_pushstring(lua, (*p) -> timestamp); + lua_settable(lua, -3); + + // set back + lua_rawseti(lua, -2, i); + } + + // ok + return 1; +} + +#endif diff --git a/xmake/core/sandbox/modules/import/lib/readline.lua b/xmake/core/sandbox/modules/import/lib/readline.lua index d96381413..b392d806a 100644 --- a/xmake/core/sandbox/modules/import/lib/readline.lua +++ b/xmake/core/sandbox/modules/import/lib/readline.lua @@ -31,7 +31,7 @@ end -- inherit some builtin interfaces sandbox_readline.readline = readline.readline -sandbox_readline.get_history_state = readline.get_history_state +sandbox_readline.history_list = readline.history_list sandbox_readline.add_history = readline.add_history sandbox_readline.clear_history = readline.clear_history diff --git a/xmake/plugins/lua/xmake.lua b/xmake/plugins/lua/xmake.lua index 269e1b944..efa044f4b 100644 --- a/xmake/plugins/lua/xmake.lua +++ b/xmake/plugins/lua/xmake.lua @@ -78,7 +78,7 @@ task("lua") if enable_readline then -- save to history - local entries = readline.get_history_state().entries + local entries = readline.history_list() if #entries > #replhistory then for i = #replhistory+1, #entries do history.save("replhistory", entries[i].line) -- cgit v1.3.1 From 8e33ff0dd31832fcf55f350d948ed6d7ec696109 Mon Sep 17 00:00:00 2001 From: TitanSnow Date: Wed, 10 May 2017 18:10:47 +0800 Subject: rm field timestamp --- core/src/xmake/readline/history_list.c | 4 ---- 1 file changed, 4 deletions(-) (limited to 'core/src') diff --git a/core/src/xmake/readline/history_list.c b/core/src/xmake/readline/history_list.c index 68597b945..30df2ef6f 100644 --- a/core/src/xmake/readline/history_list.c +++ b/core/src/xmake/readline/history_list.c @@ -56,10 +56,6 @@ tb_int_t xm_readline_history_list(lua_State* lua) lua_pushstring(lua, "line"); lua_pushstring(lua, (*p) -> line); lua_settable(lua, -3); - // field timestamp - lua_pushstring(lua, "timestamp"); - lua_pushstring(lua, (*p) -> timestamp); - lua_settable(lua, -3); // set back lua_rawseti(lua, -2, i); -- cgit v1.3.1 From 86f74ff4d66738713e596ef5f9cba3275be3958c Mon Sep 17 00:00:00 2001 From: TitanSnow Date: Wed, 10 May 2017 18:55:43 +0800 Subject: fix history_list on macos --- core/src/xmake/readline/history_list.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'core/src') diff --git a/core/src/xmake/readline/history_list.c b/core/src/xmake/readline/history_list.c index 30df2ef6f..3b0f8c0d3 100644 --- a/core/src/xmake/readline/history_list.c +++ b/core/src/xmake/readline/history_list.c @@ -48,6 +48,19 @@ tb_int_t xm_readline_history_list(lua_State* lua) // history list lua_newtable(lua); +#ifdef TB_CONFIG_OS_MACOSX + for (tb_int_t i = 1; i <= history_length; ++i) + { + lua_newtable(lua); + // field line + lua_pushstring(lua, "line"); + lua_pushstring(lua, history_get(i) -> line); + lua_settable(lua, -3); + + // set back + lua_rawseti(lua, -2, i); + } +#else tb_int_t i = 1; for (HIST_ENTRY **p = history_list(); *p; ++p, ++i) { @@ -60,6 +73,7 @@ tb_int_t xm_readline_history_list(lua_State* lua) // set back lua_rawseti(lua, -2, i); } +#endif // ok return 1; -- cgit v1.3.1