diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2017-04-18 18:52:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-18 18:52:46 +0100 |
commit | 33511b81d685101798b1cd641c5f553f720000d1 (patch) | |
tree | 82358c36c35390d8d0d8740ca1e7cf489c83af86 | |
parent | 7c8bd501c816d097d5fbc0e9b7a219512a67e33b (diff) | |
parent | 74a3a2ede2eb9aa53f20320645886a5906bb2a76 (diff) | |
download | rspamd-33511b81d685101798b1cd641c5f553f720000d1.tar.gz rspamd-33511b81d685101798b1cd641c5f553f720000d1.zip |
Merge pull request #1601 from fatalbanana/ucl
[Feature] UCL: register parser variables from Lua
-rw-r--r-- | contrib/libucl/lua_ucl.c | 76 |
1 files changed, 76 insertions, 0 deletions
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 @@ -627,6 +627,76 @@ lua_ucl_parser_parse_file (lua_State *L) } /*** + * @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. * @param {string} input string to parse @@ -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"); |