diff options
| author | ruki <[email protected]> | 2017-05-03 21:21:09 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-05-03 21:21:09 +0800 |
| commit | 021a778bc410cbe330ade85142bf29aed2485c39 (patch) | |
| tree | f41b7eebe3a65c6a98d4a78b3a4ba19c74be71ee /core/src | |
| parent | d05c6f778f125e94488fb785a2176e2854d98442 (diff) | |
add readline for linux and macosx
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/demo/makefile | 7 | ||||
| -rw-r--r-- | core/src/demo/xmake.lua | 5 | ||||
| -rw-r--r-- | core/src/xmake/sandbox/interactive.c | 40 |
3 files changed, 42 insertions, 10 deletions
diff --git a/core/src/demo/makefile b/core/src/demo/makefile index 7a9b7754c..b24bf4a95 100644 --- a/core/src/demo/makefile +++ b/core/src/demo/makefile @@ -15,10 +15,15 @@ demo_PKGS-$(TBOX) += tbox demo_PKGS-$(BASE) += base # others -demo_LIBS += xmake$(DTYPE) luajit$(DTYPE) +demo_LIBS += xmake$(DTYPE) luajit$(DTYPE) demo_INC_DIRS += ../ ../luajit/src demo_LIB_DIRS += ../xmake ../luajit demo_CXFLAGS += -D__tb_prefix__=\"xmake\" + +# add readline +ifneq ($(PLAT),windows) +demo_LIBS += readline +endif # suffix include $(PRO_DIR)/suffix.mak diff --git a/core/src/demo/xmake.lua b/core/src/demo/xmake.lua index 6449ef8d2..174b2fd38 100644 --- a/core/src/demo/xmake.lua +++ b/core/src/demo/xmake.lua @@ -23,6 +23,11 @@ target("demo") add_links("xmake", "luajit") add_linkdirs("$(buildir)") + -- link readline + if not is_plat("windows") then + add_links("readline") + end + -- add packages add_packages("tbox", "base") diff --git a/core/src/xmake/sandbox/interactive.c b/core/src/xmake/sandbox/interactive.c index 12aeafa2d..339ab9cc8 100644 --- a/core/src/xmake/sandbox/interactive.c +++ b/core/src/xmake/sandbox/interactive.c @@ -40,6 +40,10 @@ * includes */ #include "prefix.h" +#ifndef TB_CONFIG_OS_WINDOWS +# include <readline/readline.h> +# include <readline/history.h> +#endif /* ////////////////////////////////////////////////////////////////////////////////////// * macros @@ -123,14 +127,6 @@ static tb_int_t docall(lua_State *lua, tb_int_t narg, tb_int_t clear) return status; } -// print prompt -static tb_void_t write_prompt(lua_State *lua, tb_int_t firstline) -{ - // print prompt - tb_printf(firstline? LUA_PROMPT : LUA_PROMPT2); - tb_print_sync(); -} - // this line is incomplete? static tb_int_t incomplete(lua_State *lua, tb_int_t status) { @@ -152,13 +148,39 @@ static tb_int_t incomplete(lua_State *lua, tb_int_t status) // get input line static tb_int_t pushline(lua_State *lua, tb_int_t firstline) { +#ifndef TB_CONFIG_OS_WINDOWS + // get line + tb_char_t buffer[1024]; + tb_char_t const* line = readline(firstline? LUA_PROMPT : LUA_PROMPT2); + if (line) + { + // add line to history + add_history(line); + + // copy line to buffer + tb_size_t size = tb_strlcpy(buffer, line, sizeof(buffer)); + + // free line + tb_free(line); + + // truncated? + if (size >= sizeof(buffer)) + { + tb_printl("too long input!"); + return 0; + } +#else + // print prompt - write_prompt(lua, firstline); + tb_printf(firstline? LUA_PROMPT : LUA_PROMPT2); + tb_print_sync(); // get input buffer tb_char_t buffer[1024]; if (fgets(buffer, sizeof(buffer), stdin)) { + +#endif // split line '\0' tb_size_t n = tb_strlen(buffer); if (n < sizeof(buffer) && buffer[n - 1] == '\n') |
