diff options
| author | dacctal <dacctal@symlinx.net> | 2026-06-03 15:01:21 +0000 |
|---|---|---|
| committer | dacctal <dacctal@symlinx.net> | 2026-06-03 15:01:21 +0000 |
| commit | 198cde49db902ee97775c74c5cfd971b07cb0c78 (patch) | |
| tree | 95fbdf28cb9a0a02f18bd944ccfd7e3567545876 | |
| parent | 62480d6cc957c9ad36dc2ca6fc5802d79b6e8590 (diff) | |
install and remove commands are much more customizable :)
| -rw-r--r-- | include/lua_state.h | 10 | ||||
| -rw-r--r-- | src/install_pkg.c | 26 | ||||
| -rw-r--r-- | src/lua_state.c | 214 | ||||
| -rw-r--r-- | src/remove_pkg.c | 117 |
4 files changed, 303 insertions, 64 deletions
diff --git a/include/lua_state.h b/include/lua_state.h index 26a14f8..b882cd7 100644 --- a/include/lua_state.h +++ b/include/lua_state.h @@ -17,11 +17,17 @@ void init_lua_state(void); void free_lua_state(void); lua_State* get_lua_state(void); +void cache_repos(void); +void cache_build_systems(void); void cache_install_directories(void); bool repo_build(const char *repository); +bool repo_install(const char *repository); +bool repo_uninstall(const char *repository); bool bldit(const char *target); +bool bldit_install(const char *target); +bool bldit_uninstall(const char *target); bool config_build(const char *path); -void cache_repos(void); -void cache_build_systems(void); +bool config_install(const char *path); +bool config_uninstall(const char *path); #endif
\ No newline at end of file diff --git a/src/install_pkg.c b/src/install_pkg.c index 2f4f88f..4e3b3c6 100644 --- a/src/install_pkg.c +++ b/src/install_pkg.c @@ -7,8 +7,8 @@ #include "add_repo.h" #include "fetch_src.h" #include "build.h" -#include "copy_install.h" -#include "link_install.h" +//#include "copy_install.h" +//#include "link_install.h" #include "lua_state.h" #include "name_from_url.h" #include "vars.h" @@ -25,13 +25,23 @@ void install_pkg(Pkg pkg) { if (is_verbose) printf("%sbuilt %s%s%s\n", print_pkgit, green, pkg.name, color_reset); printf("%sinstalling %s%s%s\n", print_pkgit, green, pkg.name, color_reset); - if (is_auto_installed) { - if (is_symlink_install) { - link_install(pkg.src); - } else { - copy_install(pkg.src); - } + bool install_success = false; + if (!install_success && repo_install(pkg.url)) install_success = true; + if (!install_success && bldit_install(pkg.target)) install_success = true; + if (!install_success && config_install(pkg.src)) install_success = true; + if (!install_success) { + printf("%sno install function availible for package: %s\n", + print_error, pkg.name); + return; } + //is_auto_installed = true; + //if (is_auto_installed) { + // if (is_symlink_install) { + // link_install(pkg.src); + // } else { + // copy_install(pkg.src); + // } + //} printf("%sinstalled %s%s%s\n", print_success, green, pkg.name, color_reset); bool repo_exists = false; diff --git a/src/lua_state.c b/src/lua_state.c index 86626ce..eb67375 100644 --- a/src/lua_state.c +++ b/src/lua_state.c @@ -142,6 +142,24 @@ bool repo_build(const char *repository) { return false; } + lua_pop(L, 2); + return true; +} + +bool repo_install(const char *repository) { + lua_getglobal(L, "repositories"); + if (!config_loaded || !lua_istable(L, -1)) { + if (is_verbose) printf("%slua variable 'repositories' is not a table.\n", print_warning); + lua_pop(L, 1); + return false; + } + lua_getfield(L, -1, repository); + if (!lua_istable(L, -1)) { + if (is_verbose) printf("%s'repositories' lua variable '%s' is not a table.\n", print_warning, repository); + lua_pop(L, 2); + return false; + } + lua_getfield(L, -1, "pre_install"); if (!lua_isfunction(L, -1)) { if (is_verbose) printf("%s'repositories' lua variable 'pre_install' is not a function.\n", print_warning); @@ -154,7 +172,7 @@ bool repo_build(const char *repository) { if (!lua_isfunction(L, -1)) { if (is_verbose) printf("%s'repositories' lua variable 'install' is not a function.\n", print_warning); } else { - is_auto_installed = false; + return false; } if (lua_pcall(L, 0, 0, 0) != LUA_OK) { if (is_verbose) printf("%s'repositories' install failed: %s\n", print_warning, lua_tostring(L, -1)); @@ -173,6 +191,35 @@ bool repo_build(const char *repository) { return true; } +bool repo_uninstall(const char *repository) { + lua_getglobal(L, "repositories"); + if (!config_loaded || !lua_istable(L, -1)) { + if (is_verbose) printf("%slua variable 'repositories' is not a table.\n", print_warning); + lua_pop(L, 1); + return false; + } + lua_getfield(L, -1, repository); + if (!lua_istable(L, -1)) { + if (is_verbose) printf("%s'repositories' lua variable '%s' is not a table.\n", print_warning, repository); + lua_pop(L, 2); + return false; + } + + lua_getfield(L, -1, "uninstall"); + if (!lua_isfunction(L, -1)) { + if (is_verbose) printf("%s'repositories' lua variable 'uninstall' is not a function.\n", print_warning); + return false; + } + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + if (is_verbose) printf("%s'repositories' uninstall failed: %s\n", print_warning, lua_tostring(L, -1)); + return false; + } + lua_pop(L, 1); + + lua_pop(L, 2); + return true; +} + bool bldit(const char *target) { lua_State *B = luaL_newstate(); luaL_openlibs(B); @@ -245,6 +292,37 @@ bool bldit(const char *target) { return false; } + lua_pop(B, 2); + lua_close(B); + return true; +} + +bool bldit_install(const char *target) { + lua_State *B = luaL_newstate(); + luaL_openlibs(B); + + if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) { + if (is_verbose) printf("%scannot run bldit script: %s\n", print_warning, lua_tostring(B, -1)); + lua_close(B); + return false; + } + + lua_getglobal(B, "targets"); + if (!lua_istable(B, -1)) { + if (is_verbose) printf("%sbldit variable 'targets' is not a table.\n", print_warning); + lua_pop(B, 1); + lua_close(B); + return false; + } + + lua_getfield(B, -1, target); + if (!lua_istable(B, -1)) { + if (is_verbose) printf("%sbldit variable '%s' is not a table.\n", print_warning, target); + lua_pop(B, 2); + lua_close(B); + return false; + } + lua_getfield(B, -1, "pre_install"); if (!lua_isfunction(B, -1)) { if (is_verbose) printf("%s'repositories' lua variable 'pre_install' is not a function.\n", @@ -259,7 +337,7 @@ bool bldit(const char *target) { if (is_verbose) printf("%s'repositories' lua variable 'install' is not a function.\n", print_warning); } else { - is_auto_installed = false; + return false; } if (lua_pcall(B, 0, 0, 0) != LUA_OK) { if (is_verbose) printf("%sinstall failed: %s\n", print_warning, lua_tostring(B, -1)); @@ -279,6 +357,47 @@ bool bldit(const char *target) { return true; } +bool bldit_uninstall(const char *target) { + lua_State *B = luaL_newstate(); + luaL_openlibs(B); + + if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) { + if (is_verbose) printf("%scannot run bldit script: %s\n", print_warning, lua_tostring(B, -1)); + lua_close(B); + return false; + } + + lua_getglobal(B, "targets"); + if (!lua_istable(B, -1)) { + if (is_verbose) printf("%sbldit variable 'targets' is not a table.\n", print_warning); + lua_close(B); + return false; + } + + lua_getfield(B, -1, target); + if (!lua_istable(B, -1)) { + if (is_verbose) printf("%sbldit variable '%s' is not a table.\n", print_warning, target); + lua_pop(B, 2); + lua_close(B); + return false; + } + + lua_getfield(B, -1, "uninstall"); + if (!lua_isfunction(B, -1)) { + if (is_verbose) printf("%s'repositories' lua variable 'uninstall' is not a function.\n", + print_warning); + return false; + } + if (lua_pcall(B, 0, 0, 0) != LUA_OK) { + if (is_verbose) printf("%suninstall failed: %s\n", print_warning, lua_tostring(B, -1)); + return false; + } + + lua_pop(B, 2); + lua_close(B); + return true; +} + void cache_repos() { cached_repos_count = 0; lua_getglobal(L, "repositories"); @@ -403,4 +522,93 @@ bool config_build(const char *path) { } lua_pop(L, 1); return false; -}
\ No newline at end of file +} + +bool config_install(const char *path) { + lua_getglobal(L, "build_systems"); + if (!config_loaded || !lua_istable(L, -1)) { + lua_pop(L, 1); + return false; + } + lua_pushnil(L); + while (lua_next(L, -2) != 0) { + const char *key = lua_tostring(L, -2); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + continue; + } + char file_path[MAX_PATH_LEN]; + snprintf(file_path, sizeof(file_path), "%s/%s", path, key); + if (access(file_path, F_OK) != 0) { + lua_pop(L, 1); + continue; + } + lua_getfield(L, -1, "pre_install"); + if (lua_isfunction(L, -1)) { + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + if (is_verbose) printf("%slua function 'pre_install' is not available\n", + print_warning); + } + } else { lua_pop(L, 1); } + + lua_getfield(L, -1, "install"); + if (lua_isfunction(L, -1)) { + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + if (is_verbose) printf("%s lua function 'install' is not available\n", + print_warning); + } + } else { lua_pop(L, 1); } + + lua_getfield(L, -1, "post_install"); + if (!lua_isfunction(L, -1)) { + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + if (is_verbose) printf("%slua function 'post_install' is not available\n", + print_warning); + } + } else { lua_pop(L, 1); } + + lua_pop(L, 1); + lua_pop(L, 1); + return true; + } + lua_pop(L, 1); + return false; +} + +bool config_uninstall(const char *path) { + lua_getglobal(L, "build_systems"); + if (!config_loaded || !lua_istable(L, -1)) { + lua_pop(L, 1); + return false; + } + lua_pushnil(L); + while (lua_next(L, -2) != 0) { + const char *key = lua_tostring(L, -2); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + continue; + } + char file_path[MAX_PATH_LEN]; + snprintf(file_path, sizeof(file_path), "%s/%s", path, key); + if (access(file_path, F_OK) != 0) { + lua_pop(L, 1); + continue; + } + + lua_getfield(L, -1, "uninstall"); + if (!lua_isfunction(L, -1)) { + lua_pop(L, 1); + continue; + } + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + lua_pop(L, 1); + continue; + } + + lua_pop(L, 1); + lua_pop(L, 1); + return true; + } + lua_pop(L, 1); + return false; +} diff --git a/src/remove_pkg.c b/src/remove_pkg.c index 31627f9..1dc39db 100644 --- a/src/remove_pkg.c +++ b/src/remove_pkg.c @@ -1,72 +1,87 @@ +#include <ftw.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include <sys/stat.h> -#include <ftw.h> +#include <unistd.h> +#include "lua_state.h" #include "remove_pkg.h" #include "vars.h" -static int remove_installed(const char *src_path, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { - (void)sb; - (void)ftwbuf; +static int remove_installed(const char *src_path, const struct stat *sb, + int typeflag, struct FTW *ftwbuf) { + (void)sb; + (void)ftwbuf; - if (typeflag == FTW_F) { - const char *filename = src_path + ftwbuf->base; - const char *ext = strrchr(filename, '.'); - if (!ext) ext = ""; + if (typeflag == FTW_F) { + const char *filename = src_path + ftwbuf->base; + const char *ext = strrchr(filename, '.'); + if (!ext) + ext = ""; - if (strncmp(ext, ".so", 3) == 0) { - char dest[MAX_PATH_LEN]; - snprintf(dest, sizeof(dest), "%s/%s", get_install_dir("lib"), filename); - if (file_exists(dest)) remove(dest); - } else if (access(src_path, X_OK) == 0) { - if (strcmp(ext, ".sample") != 0 && strcmp(filename, "bldit") != 0 && - strcmp(filename, "build.sh") != 0 && strcmp(filename, "compile.sh") != 0) { - char dest[MAX_PATH_LEN]; - snprintf(dest, sizeof(dest), "%s/%s", get_install_dir("bin"), filename); - if (file_exists(dest)) remove(dest); - } - } else if (strcmp(ext, ".h") == 0) { - char dest[MAX_PATH_LEN]; - snprintf(dest, sizeof(dest), "%s/%s", get_install_dir("include"), filename); - if (file_exists(dest)) remove(dest); - } + if (strncmp(ext, ".so", 3) == 0) { + char dest[MAX_PATH_LEN]; + snprintf(dest, sizeof(dest), "%s/%s", get_install_dir("lib"), filename); + if (file_exists(dest)) + remove(dest); + } else if (access(src_path, X_OK) == 0) { + if (strcmp(ext, ".sample") != 0 && strcmp(filename, "bldit") != 0 && + strcmp(filename, "build.sh") != 0 && + strcmp(filename, "compile.sh") != 0) { + char dest[MAX_PATH_LEN]; + snprintf(dest, sizeof(dest), "%s/%s", get_install_dir("bin"), filename); + if (file_exists(dest)) + remove(dest); + } + } else if (strcmp(ext, ".h") == 0) { + char dest[MAX_PATH_LEN]; + snprintf(dest, sizeof(dest), "%s/%s", get_install_dir("include"), + filename); + if (file_exists(dest)) + remove(dest); } + } - return 0; + return 0; } -static int remove_tree(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { - (void)sb; - (void)ftwbuf; - if (typeflag == FTW_F || typeflag == FTW_SL) { - unlink(fpath); - } else if (typeflag == FTW_DP) { - rmdir(fpath); - } - return 0; +static int remove_tree(const char *fpath, const struct stat *sb, int typeflag, + struct FTW *ftwbuf) { + (void)sb; + (void)ftwbuf; + if (typeflag == FTW_F || typeflag == FTW_SL) { + unlink(fpath); + } else if (typeflag == FTW_DP) { + rmdir(fpath); + } + return 0; } void remove_pkg(Pkg pkg) { - if (!file_exists(pkg.src)) { - printf("%s%s is not installed!\n", print_pkgit, pkg.name); - return; - } + if (!file_exists(pkg.src)) { + printf("%s%s is not installed!\n", print_pkgit, pkg.name); + return; + } + chdir(pkg.src); - nftw(pkg.src, remove_installed, 64, FTW_PHYS); - - const char *last_slash = strrchr(pkg.src, '/'); - char target[MAX_PATH_LEN]; - if (last_slash && last_slash != pkg.src) { - size_t parent_len = last_slash - pkg.src; - snprintf(target, sizeof(target), "%.*s", (int)parent_len, pkg.src); - } else { - snprintf(target, sizeof(target), "%s", pkg.src); - } + if (repo_uninstall(pkg.name)) return; + if (bldit_uninstall(pkg.target)) return; + if (config_uninstall(pkg.src)) return; - nftw(target, remove_tree, 64, FTW_DEPTH | FTW_PHYS); + printf("%sno uninstall function availible for package: %s\n", + print_error, pkg.name); + return; - printf("%sremoved %s\n", print_pkgit, pkg.name); + //nftw(pkg.src, remove_installed, 64, FTW_PHYS); + //const char *last_slash = strrchr(pkg.src, '/'); + //char target[MAX_PATH_LEN]; + //if (last_slash && last_slash != pkg.src) { + // size_t parent_len = last_slash - pkg.src; + // snprintf(target, sizeof(target), "%.*s", (int)parent_len, pkg.src); + //} else { + // snprintf(target, sizeof(target), "%s", pkg.src); + //} + //nftw(target, remove_tree, 64, FTW_DEPTH | FTW_PHYS); + //printf("%sremoved %s\n", print_pkgit, pkg.name); }
\ No newline at end of file |
