diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-12-30 14:31:28 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-12-30 14:31:28 +0000 |
commit | 1eff956840f33ddb98da8960d6fa7f5b9c810d6e (patch) | |
tree | 9b3a3c0b556fa9205f6fed11d37d2e30361e18f4 /src | |
parent | b3df182a6aa145382d404b4d1e1e1a85d3fdde46 (diff) | |
download | rspamd-1eff956840f33ddb98da8960d6fa7f5b9c810d6e.tar.gz rspamd-1eff956840f33ddb98da8960d6fa7f5b9c810d6e.zip |
Add util.glob routine to lua API
Diffstat (limited to 'src')
-rw-r--r-- | src/lua/lua_util.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 3cae28211..a5d3a0f87 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -29,6 +29,7 @@ #include "tokenizers/tokenizers.h" #include "libserver/url.h" #include <math.h> +#include <glob.h> /*** * @module rspamd_util @@ -152,6 +153,15 @@ LUA_FUNCTION_DEF (util, humanize_number); */ LUA_FUNCTION_DEF (util, get_tld); +/** + * @function util.glob(pattern) + * Returns results for the glob match for the specified pattern + * + * @param {string} pattern glob pattern to match ('?' and '*' are supported) + * @return {table/string} list of matched files + */ +LUA_FUNCTION_DEF (util, glob); + static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, create_event_base), LUA_INTERFACE_DEF (util, load_rspamd_config), @@ -168,6 +178,7 @@ static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, is_uppercase), LUA_INTERFACE_DEF (util, humanize_number), LUA_INTERFACE_DEF (util, get_tld), + LUA_INTERFACE_DEF (util, glob), {NULL, NULL} }; @@ -736,6 +747,38 @@ lua_util_get_tld (lua_State *L) return 1; } + +static gint +lua_util_glob (lua_State *L) +{ + const gchar *pattern; + glob_t gl; + gint top, i, flags; + + top = lua_gettop (L); + memset (&gl, 0, sizeof (gl)); + flags = GLOB_NOSORT; + + for (i = 1; i <= top; i ++, flags |= GLOB_APPEND) { + pattern = luaL_checkstring (L, i); + + if (pattern) { + glob (pattern, flags, NULL, &gl); + } + } + + lua_newtable (L); + /* Push results */ + for (i = 0; i < (gint)gl.gl_pathc; i ++) { + lua_pushstring (L, gl.gl_pathv[i]); + lua_rawseti (L, -2, i + 1); + } + + globfree (&gl); + + return 1; +} + static gint lua_load_util (lua_State * L) { |