0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2025-03-19 21:07:04 +00:00
bird/lua/filter.c

33 lines
944 B
C
Raw Normal View History

#include "nest/bird.h"
#include "filter/filter.h"
2017-02-06 15:25:48 +01:00
#include "lua.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
/* Docs: http://pgl.yoyo.org/luai/i/luaL_dostring */
struct f_val filter_lua_chunk(const char *chunk, struct rte **e, struct rta *a, struct ea_list **ea, struct linpool *lp) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_bird_state *lbs = luaB_init(L, lp);
2017-02-08 12:36:32 +01:00
luaB_push_route(L, *e);
int le = luaL_dostring(L, chunk);
struct f_val out = F_VAL_VOID;
if (le && lbs->exception) {
out = F_VAL(T_RETURN, i, lbs->exception);
} else if (le) {
log(L_ERR "bad lua: %s", lua_tostring(L, -1));
out = F_VAL(T_RETURN, i, F_ERROR);
} else if (lua_isnumber(L, -1)) {
out = F_VAL(T_INT, i, lua_tonumber(L, -1));
} else {
log(L_WARN "lua return value is not a number (unimplemented): %s", lua_tostring(L, -1));
out = F_VAL(T_RETURN, i, F_ERROR);
}
lua_close(L);
return out;
}