aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2017-10-16 08:31:14 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2017-10-16 08:31:14 +0100
commit83c4748d4c4460fd3e3d929e3daaedb277f9d78e (patch)
tree629328092f0a569fb3a616c5fd56479d3c755cbe
parent9cbe9d6993ef31abdebf150d54bb6d470197ffe6 (diff)
downloadrspamd-83c4748d4c4460fd3e3d929e3daaedb277f9d78e.tar.gz
rspamd-83c4748d4c4460fd3e3d929e3daaedb277f9d78e.zip
[Minor] Add utility to read user's input with prompt
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/client/CMakeLists.txt1
-rw-r--r--src/lua/lua_util.c33
-rw-r--r--utils/CMakeLists.txt1
4 files changed, 37 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 4cca7dc02..f01f7a45c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -183,6 +183,8 @@ IF (ENABLE_HYPERSCAN MATCHES "ON")
TARGET_LINK_LIBRARIES(rspamd hs)
ENDIF()
+TARGET_LINK_LIBRARIES(rspamd rspamd-linenoise)
+
IF(USE_CXX_LINKER)
SET_TARGET_PROPERTIES(rspamd PROPERTIES LINKER_LANGUAGE CXX)
ENDIF()
diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt
index 0a92019d8..29abfb2c0 100644
--- a/src/client/CMakeLists.txt
+++ b/src/client/CMakeLists.txt
@@ -8,6 +8,7 @@ ADD_EXECUTABLE(rspamc ${RSPAMCSRC} ${LIBRSPAMDCLIENTSRC})
SET_TARGET_PROPERTIES(rspamc PROPERTIES COMPILE_FLAGS "-I${CMAKE_SOURCE_DIR}/lib")
TARGET_LINK_LIBRARIES(rspamc rspamd-server)
TARGET_LINK_LIBRARIES(rspamc ${RSPAMD_REQUIRED_LIBRARIES})
+TARGET_LINK_LIBRARIES(rspamc rspamd-linenoise)
IF(USE_CXX_LINKER)
SET_TARGET_PROPERTIES(rspamc PROPERTIES LINKER_LANGUAGE CXX)
ENDIF()
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 */
/******************************************************************************
diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt
index 9d42c604e..0f65b06ce 100644
--- a/utils/CMakeLists.txt
+++ b/utils/CMakeLists.txt
@@ -19,6 +19,7 @@ MACRO(ADD_UTIL NAME)
IF(ENABLE_HIREDIS MATCHES "ON")
TARGET_LINK_LIBRARIES("${NAME}" rspamd-hiredis)
ENDIF()
+ TARGET_LINK_LIBRARIES(${NAME} rspamd-linenoise)
TARGET_LINK_LIBRARIES("${NAME}" ${RSPAMD_REQUIRED_LIBRARIES})
ENDMACRO()