summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorruki <[email protected]>2017-05-10 20:59:21 +0800
committerGitHub <[email protected]>2017-05-10 20:59:21 +0800
commit966876f494902efecb1ea475bade16cd96fe160c (patch)
tree1f91f27feead302448faed74adc3b214275fe08a /core
parent194980341bd177815feb13a529892519138b9cb0 (diff)
parentda6e4cce0e4c1736db0898d53c16ac8ca0441846 (diff)
Merge pull request #84 from TitanSnow/replhistory
replhistory
Diffstat (limited to 'core')
-rw-r--r--core/src/xmake/machine.c25
-rw-r--r--core/src/xmake/makefile6
-rw-r--r--core/src/xmake/os/versioninfo.c39
-rw-r--r--core/src/xmake/readline/add_history.c63
-rw-r--r--core/src/xmake/readline/clear_history.c59
-rw-r--r--core/src/xmake/readline/history_list.c82
-rw-r--r--core/src/xmake/readline/prefix.h41
-rw-r--r--core/src/xmake/readline/readline.c68
8 files changed, 382 insertions, 1 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index b9977d92a..4001af365 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -103,6 +103,14 @@ 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);
+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
+
/* //////////////////////////////////////////////////////////////////////////////////////
* globals
*/
@@ -173,6 +181,18 @@ 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 }
+, { "history_list", xm_readline_history_list }
+, { "add_history", xm_readline_add_history }
+, { "clear_history", xm_readline_clear_history}
+, { tb_null, tb_null }
+};
+#endif
+
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
@@ -379,6 +399,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..0b9770bf6 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -49,7 +49,11 @@ xmake_C_FILES += \
process/wait \
process/waitlist \
process/close \
- sandbox/interactive
+ sandbox/interactive \
+ readline/readline \
+ readline/history_list \
+ readline/add_history \
+ readline/clear_history
# flags
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;
}
diff --git a/core/src/xmake/readline/add_history.c b/core/src/xmake/readline/add_history.c
new file mode 100644
index 000000000..57f947d0c
--- /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
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "add_history"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+#ifdef XM_CONFIG_API_HAVE_READLINE
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * 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/core/src/xmake/readline/clear_history.c b/core/src/xmake/readline/clear_history.c
new file mode 100644
index 000000000..7517e2e43
--- /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
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "clear_history"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+#ifdef XM_CONFIG_API_HAVE_READLINE
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * 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/core/src/xmake/readline/history_list.c b/core/src/xmake/readline/history_list.c
new file mode 100644
index 000000000..3b0f8c0d3
--- /dev/null
+++ b/core/src/xmake/readline/history_list.c
@@ -0,0 +1,82 @@
+/*!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);
+#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)
+ {
+ lua_newtable(lua);
+ // field line
+ lua_pushstring(lua, "line");
+ lua_pushstring(lua, (*p) -> line);
+ lua_settable(lua, -3);
+
+ // set back
+ lua_rawseti(lua, -2, i);
+ }
+#endif
+
+ // ok
+ return 1;
+}
+
+#endif
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 <stdio.h> // on some OS (like centos) required
+# include <readline/readline.h>
+# include <readline/history.h>
+#endif
+
+
+#endif
+
+
diff --git a/core/src/xmake/readline/readline.c b/core/src/xmake/readline/readline.c
new file mode 100644
index 000000000..c52ff02aa
--- /dev/null
+++ b/core/src/xmake/readline/readline.c
@@ -0,0 +1,68 @@
+/*!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
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "readline"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+#ifdef XM_CONFIG_API_HAVE_READLINE
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * 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 (tb_strcmp("nil", luaL_typename(lua, 1)) != 0)
+ prompt = luaL_checkstring(lua, 1);
+
+ // call readline
+ tb_char_t const* line = readline(prompt);
+ if (line)
+ lua_pushstring(lua, line);
+ else
+ lua_pushnil(lua);
+
+ // free it
+ tb_free((void*)line);
+
+ // ok
+ return 1;
+}
+
+#endif