diff options
| -rw-r--r-- | src/config.c | 37 | ||||
| -rw-r--r-- | src/config.h | 1 | ||||
| -rw-r--r-- | src/package.c | 7 | ||||
| -rw-r--r-- | src/pow_lua.c | 32 | ||||
| -rw-r--r-- | src/pow_lua.h | 2 |
5 files changed, 64 insertions, 15 deletions
diff --git a/src/config.c b/src/config.c index cbbbbe2..100c3c6 100644 --- a/src/config.c +++ b/src/config.c @@ -1,6 +1,9 @@ #include "config.h" #include "log.h" +#include "paths.h" +#include "unistd.h" +#include "lua.h" #include "lauxlib.h" #include "lualib.h" #include "stdlib.h" @@ -126,8 +129,42 @@ int pow_open_config(lua_State *L_global, pow_config *cfg, char *path) { } cfg->build_cfg_ref = luaL_ref(L, LUA_REGISTRYINDEX); + + lua_pop(L, 1); + } + + if (access("/etc/pow/use.lua", F_OK) != 0) { + pow_log_info("use.lua does not exist"); + + return 0; + } + + lua_State *L_use = lua_newthread(L_global); + + err = luaL_loadfile(L_use, "/etc/pow/use.lua"); + + if (err) { + pow_log_error("failed to load use.lua"); + + return 0; + } + + err = lua_pcall(L_use, 0, 1, 0); + + if (err) { + pow_log_error("failed to execute use.lua"); + + return 0; } + if (!lua_istable(L_use, -1)) { + pow_log_error("invalid return value of use.lua"); + + return 0; + } + + cfg->use_ref = luaL_ref(L_use, LUA_REGISTRYINDEX); + return 0; } diff --git a/src/config.h b/src/config.h index 8bbee8b..136c1c0 100644 --- a/src/config.h +++ b/src/config.h @@ -14,6 +14,7 @@ typedef struct pow_config { lua_State *L_cfg; int build_cfg_ref; + int use_ref; } pow_config; int pow_open_config(lua_State *L, pow_config *cfg, char *path); diff --git a/src/package.c b/src/package.c index 2a947c0..021e8f7 100644 --- a/src/package.c +++ b/src/package.c @@ -218,8 +218,7 @@ int pow_package_open(lua_State *L_global, pow_package *pkg, pow_config *cfg, con // push the config object from cfg thread to pkg thread - luaL_openlibs(L); - pow_lua_load(L, cfg); + pow_lua_load(L, cfg, "einfach/base"); luaL_loadstring(L, package_buffer); @@ -254,9 +253,7 @@ int pow_package_close(pow_package *pkg) { free(pkg->name); free(pkg->version); - lua_close(pkg->L); - - for (size_t i = 0; i > pkg->source_count; i++) { + for (size_t i = 0; i < pkg->source_count; i++) { pow_source_close(pkg->sources[i]); } diff --git a/src/pow_lua.c b/src/pow_lua.c index b3fa69b..1a765b5 100644 --- a/src/pow_lua.c +++ b/src/pow_lua.c @@ -36,7 +36,6 @@ int parse_octal_string(const char *str) { int pow_lua_extract(lua_State *L) { int r; - ssize_t size; struct archive *a; struct archive_entry *entry; @@ -300,28 +299,43 @@ int pow_lua_chdir(lua_State *L) { luaL_Reg libpow[] = {{"chdir", pow_lua_chdir}, {"extract", pow_lua_extract}, {"install", pow_lua_install}, {NULL, NULL}}; -int pow_lua_load(lua_State *L, pow_config *cfg) { +int pow_lua_load(lua_State *L, pow_config *cfg, char *package_name) { luaL_newlib(L, libpow); - lua_createtable(L, 0, 1); + lua_rawgeti(cfg->L_cfg, LUA_REGISTRYINDEX, cfg->build_cfg_ref); + if (!lua_istable(cfg->L_cfg, -1)) { + pow_log_error("failed to initialize lua: config.build is not a table"); + lua_pop(cfg->L_cfg, 1); + return -1; + } + lua_xmove(cfg->L_cfg, L, 1); lua_setfield(L, -2, "config"); - lua_rawgeti(cfg->L_cfg, LUA_REGISTRYINDEX, cfg->build_cfg_ref); + if (package_name == NULL) { + pow_log_error("failed to initialize lua: package_name is NULL"); + return -1; + } + lua_rawgeti(cfg->L_cfg, LUA_REGISTRYINDEX, cfg->use_ref); if (!lua_istable(cfg->L_cfg, -1)) { - pow_log_error("failed to initialize lua: config.build is not a table"); + pow_log_error("failed to initialize lua: config.use is not a table"); lua_pop(cfg->L_cfg, 1); + return -1; + } - return 1; + lua_getfield(cfg->L_cfg, -1, package_name); + + if (lua_isnil(cfg->L_cfg, -1)) { + lua_pop(cfg->L_cfg, 1); + lua_newtable(cfg->L_cfg); } lua_xmove(cfg->L_cfg, L, 1); - - lua_setfield(L, -2, "config"); + lua_pop(cfg->L_cfg, 1); + lua_setfield(L, -2, "use"); lua_setglobal(L, "pow"); return 0; } - diff --git a/src/pow_lua.h b/src/pow_lua.h index 3af17af..b87a921 100644 --- a/src/pow_lua.h +++ b/src/pow_lua.h @@ -2,4 +2,4 @@ typedef struct pow_config pow_config; -int pow_lua_load(lua_State *L, pow_config *cfg); +int pow_lua_load(lua_State *L, pow_config *cfg, char *package_name); |
