summaryrefslogtreecommitdiff
path: root/core/src/xmake
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/xmake')
-rw-r--r--core/src/xmake/machine.c19
-rw-r--r--core/src/xmake/makefile3
-rw-r--r--core/src/xmake/readline/prefix.h41
-rw-r--r--core/src/xmake/readline/readline.c65
4 files changed, 127 insertions, 1 deletions
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 <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..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