diff options
| author | TitanSnow <[email protected]> | 2017-05-10 10:09:45 +0800 |
|---|---|---|
| committer | TitanSnow <[email protected]> | 2017-05-10 10:09:45 +0800 |
| commit | ff2327bbe5533fe9656ed6f630caad69877b4941 (patch) | |
| tree | 6cdb1b5d9257acae0450855c4ac78f96172adf68 /core/src | |
| parent | c8a1edb15828a188b2c2e2139a366aad006aaf0e (diff) | |
add readline.get_history_state()
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/machine.c | 6 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 3 | ||||
| -rw-r--r-- | core/src/xmake/readline/get_history_state.c | 92 |
3 files changed, 98 insertions, 3 deletions
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 |
