diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-10-16 08:31:14 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-10-16 08:31:14 +0100 |
commit | 83c4748d4c4460fd3e3d929e3daaedb277f9d78e (patch) | |
tree | 629328092f0a569fb3a616c5fd56479d3c755cbe /src/lua | |
parent | 9cbe9d6993ef31abdebf150d54bb6d470197ffe6 (diff) | |
download | rspamd-83c4748d4c4460fd3e3d929e3daaedb277f9d78e.tar.gz rspamd-83c4748d4c4460fd3e3d929e3daaedb277f9d78e.zip |
[Minor] Add utility to read user's input with prompt
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/lua_util.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index eedfcb625..6f782b254 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -19,6 +19,7 @@ #include "unix-std.h" #include "contrib/zstd/zstd.h" #include "libmime/email_addr.h" +#include "linenoise.h" #include <math.h> #include <glob.h> @@ -375,6 +376,13 @@ LUA_FUNCTION_DEF (util, is_utf_spoofed); LUA_FUNCTION_DEF (util, is_valid_utf8); /*** + * @function util.readline([prompt]) + * Returns string read from stdin with history and editing support + * @return {boolean} true if a string is spoofed + */ +LUA_FUNCTION_DEF (util, readline); + +/*** * @function util.pack(fmt, ...) * * Backport of Lua 5.3 `string.pack` function: @@ -516,6 +524,7 @@ static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, caseless_hash_fast), LUA_INTERFACE_DEF (util, is_utf_spoofed), LUA_INTERFACE_DEF (util, is_valid_utf8), + LUA_INTERFACE_DEF (util, readline), LUA_INTERFACE_DEF (util, get_hostname), LUA_INTERFACE_DEF (util, pack), LUA_INTERFACE_DEF (util, unpack), @@ -2046,6 +2055,30 @@ lua_util_is_valid_utf8 (lua_State *L) return 1; } +static gint +lua_util_readline (lua_State *L) +{ + const gchar *prompt = NULL; + gchar *input; + + if (lua_type (L, 1) == LUA_TSTRING) { + prompt = lua_tostring (L, 1); + } + + input = linenoise (prompt); + + if (input) { + lua_pushstring (L, input); + linenoiseHistoryAdd (input); + linenoiseFree (input); + } + else { + lua_pushnil (L); + } + + return 1; +} + /* Backport from Lua 5.3 */ /****************************************************************************** |