From: Andrew Lewis Date: Tue, 18 Apr 2017 14:32:42 +0000 (+0200) Subject: [Feature] UCL: register parser variables from Lua X-Git-Tag: 1.5.6~4^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=74a3a2ede2eb9aa53f20320645886a5906bb2a76;p=rspamd.git [Feature] UCL: register parser variables from Lua --- diff --git a/contrib/libucl/lua_ucl.c b/contrib/libucl/lua_ucl.c index 2f7df686a..0d6cc0830 100644 --- a/contrib/libucl/lua_ucl.c +++ b/contrib/libucl/lua_ucl.c @@ -626,6 +626,76 @@ lua_ucl_parser_parse_file (lua_State *L) return ret; } +/*** + * @method parser:register_variable(name, value) + * Register parser variable + * @param {string} name name of variable + * @param {string} value value of variable + * @return {bool} success +@example +local parser = ucl.parser() +local res = parser:register_variable('CONFDIR', '/etc/foo') + */ +static int +lua_ucl_parser_register_variable (lua_State *L) +{ + struct ucl_parser *parser; + const char *name, *value; + int ret = 2; + + parser = lua_ucl_parser_get (L, 1); + name = luaL_checkstring (L, 2); + value = luaL_checkstring (L, 3); + + if (parser != NULL && name != NULL && value != NULL) { + ucl_parser_register_variable (parser, name, value); + lua_pushboolean (L, true); + ret = 1; + } + else { + return luaL_error (L, "invalid arguments"); + } + + return ret; +} + +/*** + * @method parser:register_variables(vars) + * Register parser variables + * @param {table} vars names/values of variables + * @return {bool} success +@example +local parser = ucl.parser() +local res = parser:register_variables({CONFDIR = '/etc/foo', VARDIR = '/var'}) + */ +static int +lua_ucl_parser_register_variables (lua_State *L) +{ + struct ucl_parser *parser; + const char *name, *value; + int ret = 2; + + parser = lua_ucl_parser_get (L, 1); + + if (parser != NULL && lua_type (L, 2) == LUA_TTABLE) { + for (lua_pushnil (L); lua_next (L, 2); lua_pop (L, 1)) { + lua_pushvalue (L, -2); + name = luaL_checkstring (L, -1); + value = luaL_checkstring (L, -2); + ucl_parser_register_variable (parser, name, value); + lua_pop (L, 1); + } + + lua_pushboolean (L, true); + ret = 1; + } + else { + return luaL_error (L, "invalid arguments"); + } + + return ret; +} + /*** * @method parser:parse_string(input) * Parse UCL object from file. @@ -1014,6 +1084,12 @@ lua_ucl_parser_mt (lua_State *L) lua_pushcfunction (L, lua_ucl_parser_parse_string); lua_setfield (L, -2, "parse_string"); + lua_pushcfunction (L, lua_ucl_parser_register_variable); + lua_setfield (L, -2, "register_variable"); + + lua_pushcfunction (L, lua_ucl_parser_register_variables); + lua_setfield (L, -2, "register_variables"); + lua_pushcfunction (L, lua_ucl_parser_get_object); lua_setfield (L, -2, "get_object");