From 13a25e4386456d30cae07334ccb771043e216e8e Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Wed, 26 Sep 2018 00:42:13 +0200 Subject: Lua: Allow evaluation in globals --- lua/filter.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'lua') diff --git a/lua/filter.c b/lua/filter.c index 97911148..d99e631f 100644 --- a/lua/filter.c +++ b/lua/filter.c @@ -149,3 +149,36 @@ int lua_filter_same(struct lua_filter_chunk *new, struct lua_filter_chunk *old) else return 0; } + +uint lua_eval(struct f_inst *inst) +{ + struct f_val string = f_eval(inst, cfg_mem); + if (string.type != T_STRING) { + cf_error("Lua filter must be a string"); + return -1; + } + + lua_State *L = luaB_getstate(); + int dores = luaL_dostring(L, string.val.s); + warn("lua_eval dores '%s' %d", string.val.s, dores); + switch (dores) { + case LUA_ERRMEM: + luaB_close(L); + cf_error("Memory allocation error occured when loading lua chunk"); + return -1; + case LUA_ERRSYNTAX: + { + const char *e = lua_tostring(L, -1); + char *ec = cfg_alloc(strlen(e) + 1); + strcpy(ec, e); + luaB_close(L); + cf_error("Lua syntax error: %s", ec); + return -1; + } + case 0: /* Everything OK */ + break; + } + luaB_close(L); + + return 0; +} -- cgit v1.2.3