From edd85242dd5f0c07d5ba51516a65fc0cdb096f9d Mon Sep 17 00:00:00 2001 From: DL6ER Date: Mon, 3 Aug 2020 03:45:57 +0200 Subject: [PATCH] Automatically load bundled libraries and make them available globally. Signed-off-by: DL6ER --- src/lua/ftl_lua.c | 13 ++++++++++++- src/lua/ftl_lua.h | 4 ++++ src/lua/lua.c | 6 +++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/lua/ftl_lua.c b/src/lua/ftl_lua.c index 3c0d4538..dc4e41d0 100644 --- a/src/lua/ftl_lua.c +++ b/src/lua/ftl_lua.c @@ -23,6 +23,7 @@ ** from 'lua_getinfo' into result table. Key is always a string; ** value can be a string, an int, or a boolean. */ + static void settabss (lua_State *L, const char *k, const char *v) { lua_pushstring(L, v); lua_setfield(L, -2, k); @@ -40,6 +41,7 @@ static void settabsb (lua_State *L, const char *k, int v) { static bool shm_connected = false; +// pihole.ftl_version() static int pihole_ftl_version(lua_State *L) { lua_pushstring(L, get_FTL_version()); return 1; @@ -58,10 +60,12 @@ static int _pihole_connect(lua_State *L) { return LUA_OK; } +// pihole.connect() static int pihole_connect(lua_State *L) { return _pihole_connect(L); } +// pihole.counters() static int pihole_counters(lua_State *L) { if(!shm_connected) { @@ -92,7 +96,14 @@ static const luaL_Reg piholelib[] = { {NULL, NULL} }; +// Register pihole library LUAMOD_API int luaopen_pihole(lua_State *L) { luaL_newlib(L, piholelib); return LUA_YIELD; -} \ No newline at end of file +} + +// Load bundled libraries and make the available globally +void ftl_lua_init(lua_State *L) +{ + dolibrary(L, "inspect"); +} diff --git a/src/lua/ftl_lua.h b/src/lua/ftl_lua.h index c05291e9..34d2311f 100644 --- a/src/lua/ftl_lua.h +++ b/src/lua/ftl_lua.h @@ -17,4 +17,8 @@ int lua_main (int argc, char **argv); int luac_main (int argc, char **argv); +extern int dolibrary (lua_State *L, const char *name); + +void ftl_lua_init(lua_State *L); + #endif //LUA_H \ No newline at end of file diff --git a/src/lua/lua.c b/src/lua/lua.c index 3b30c474..6c720719 100644 --- a/src/lua/lua.c +++ b/src/lua/lua.c @@ -190,7 +190,7 @@ static int dostring (lua_State *L, const char *s, const char *name) { ** Calls 'require(name)' and stores the result in a global variable ** with the given name. */ -static int dolibrary (lua_State *L, const char *name) { +int dolibrary (lua_State *L, const char *name) { int status; lua_getglobal(L, "require"); lua_pushstring(L, name); @@ -597,6 +597,10 @@ static int pmain (lua_State *L) { if (handle_luainit(L) != LUA_OK) /* run LUA_INIT */ return 0; /* error running LUA_INIT */ } + + // Load and enable libraries bundled with Pi-hole + ftl_lua_init(L); + if (!runargs(L, argv, script)) /* execute arguments -e and -l */ return 0; /* something failed */ if (script < argc && /* execute main script (if there is one) */