diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2018-09-26 00:42:13 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2019-02-22 23:12:41 +0100 |
commit | 13a25e4386456d30cae07334ccb771043e216e8e (patch) | |
tree | 25bd4c183a152340b3b072b35e471f23ed8ce465 | |
parent | e7a3efd842dc8626e93c34af99f61a3880bf87f2 (diff) |
Lua: Allow evaluation in globals
-rw-r--r-- | filter/config.Y | 1 | ||||
-rw-r--r-- | filter/filter.h | 1 | ||||
-rw-r--r-- | lua/filter.c | 33 |
3 files changed, 35 insertions, 0 deletions
diff --git a/filter/config.Y b/filter/config.Y index 3c4d8db6..6004c961 100644 --- a/filter/config.Y +++ b/filter/config.Y @@ -453,6 +453,7 @@ filter_def: conf: filter_eval ; filter_eval: EVAL term { f_eval_int($2); } + | EVAL LUA constant { lua_eval($3); } ; conf: custom_attr ; diff --git a/filter/filter.h b/filter/filter.h index b49c4c43..2b1176dc 100644 --- a/filter/filter.h +++ b/filter/filter.h @@ -320,5 +320,6 @@ extern void (*bt_assert_hook)(int result, struct f_inst *assert); struct filter * lua_new_filter(struct f_inst *inst); struct f_val lua_interpret(struct lua_filter_chunk *chunk, struct rte **e, struct rta **a, struct ea_list **ea, struct linpool *lp, int flags); int lua_filter_same(struct lua_filter_chunk *new, struct lua_filter_chunk *old); +uint lua_eval(struct f_inst *inst); #endif 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; +} |