aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/debug.h (renamed from include/lua_vars.h)10
-rw-r--r--include/pkgit_lua.h (renamed from include/lua_globs.h)20
-rw-r--r--src/debug.c35
-rw-r--r--src/globs.c3
-rw-r--r--src/lua_globs.c77
-rw-r--r--src/lua_install.c128
-rw-r--r--src/lua_vars.c15
-rw-r--r--src/main.c37
-rw-r--r--src/parse_args.c4
9 files changed, 291 insertions, 38 deletions
diff --git a/include/lua_vars.h b/include/debug.h
index 439a335..e74a099 100644
--- a/include/lua_vars.h
+++ b/include/debug.h
@@ -18,12 +18,10 @@
*/
-#ifndef PKGIT_LUA_VARS_H
-#define PKGIT_LUA_VARS_H
-#include <lauxlib.h>
-#include <lua.h>
-#include <lualib.h>
+#ifndef PKGIT_DEBUG_H
+#define PKGIT_DEBUG_H
+
+void debug_print_inst_dirs(void);
-void init_install_directories(void);
#endif
diff --git a/include/lua_globs.h b/include/pkgit_lua.h
index 0a5426f..a06a995 100644
--- a/include/lua_globs.h
+++ b/include/pkgit_lua.h
@@ -18,8 +18,8 @@
*/
-#ifndef PKGIT_LUA_GLOBALS_H
-#define PKGIT_LUA_GLOBALS_H
+#ifndef PKGIT_LUA_H
+#define PKGIT_LUA_H
#include <lua.h>
#include <lauxlib.h>
@@ -30,13 +30,25 @@ extern lua_State *L;
extern lua_State *B;
extern bool config_loaded;
+// init
void push_lua_path(lua_State *L, const char *new_path);
void init_lua_state(void);
-void init_bldit(void);
+void init_bldit_state(void);
void free_lua_state(void);
-
+void free_bldit_state(void);
lua_State *get_lua_state(void);
+lua_State *get_bldit_state(void);
+
+// gobal vars
+void init_install_directories(void);
+void init_prefix_directory(void);
+// helpers
void lua_isnt_type(char* variable, char* type);
+bool lua_try_function(lua_State *L, char *lua_file, char *fname);
+bool lua_try_table(lua_State *L, char *lua_file, char *tname);
+
+// install
+void install_dependencies(lua_State *L);
#endif
diff --git a/src/debug.c b/src/debug.c
new file mode 100644
index 0000000..0a580df
--- /dev/null
+++ b/src/debug.c
@@ -0,0 +1,35 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "globs.h"
+
+void debug_print_inst_dirs(void) {
+ printf("PREFIX:\t");
+ str_println(&inst_dirs.prefix);
+ printf("ID_BIN:\t");
+ str_println(&inst_dirs.bin);
+ printf("ID_LIB:\t");
+ str_println(&inst_dirs.lib);
+ printf("ID_INCLUDE:\t");
+ str_println(&inst_dirs.include);
+ printf("ID_SRC:\t");
+ str_println(&inst_dirs.src);
+}
diff --git a/src/globs.c b/src/globs.c
index 860bafb..cdb9df8 100644
--- a/src/globs.c
+++ b/src/globs.c
@@ -24,7 +24,7 @@
#include "files.h"
#include "log.h"
-#include "lua_vars.h"
+#include "pkgit_lua.h"
#include "str.h"
install_dirs_t inst_dirs = {0};
@@ -48,6 +48,7 @@ void init_vars(void) {
cfg.repos = str_format("%.*s/repos.lua", str_fmt(&cfg.dir));
init_install_directories();
+ init_prefix_directory();
}
void free_vars(void) {
diff --git a/src/lua_globs.c b/src/lua_globs.c
index d6bd353..745df00 100644
--- a/src/lua_globs.c
+++ b/src/lua_globs.c
@@ -19,11 +19,13 @@
*/
#include <stdlib.h>
+#include <string.h>
+
+#include "pkgit_lua.h"
#include "files.h"
#include "globs.h"
#include "log.h"
-#include "lua_globs.h"
#include "str.h"
lua_State *L = NULL;
@@ -67,7 +69,7 @@ void init_lua_state(void) {
config_loaded = true;
}
-void init_bldit(void) {
+void init_bldit_state(void) {
if (B != NULL)
return;
B = luaL_newstate();
@@ -88,11 +90,78 @@ void free_lua_state(void) {
config_loaded = false;
}
-lua_State *get_lua_state(void) {
- return L;
+void free_bldit_state(void) {
+ if (L != NULL) {
+ lua_close(L);
+ L = NULL;
+ }
+ config_loaded = false;
}
+lua_State *get_lua_state(void) { return L; }
+lua_State *get_bldit_state(void) { return B; }
+
void lua_isnt_type(char *variable, char *type) {
if (flags.verbose)
log_error("init.lua: '%s' is not a %s.", variable, type);
}
+
+void bldit_isnt_type(char *variable, char *type) {
+ if (flags.verbose)
+ log_error("bldit.lua: '%s' is not a %s.", variable, type);
+}
+
+//bool is_bldit_usable() {
+// const char* bldit_version = bldit_getver();
+// if (
+// strcmp(bldit_version, "") != 0 &&
+// strcmp(bldit_version, version) == 0
+// ) return true;
+// bool prev_pass = false;
+// for (int i = 0; i < strlen(bldit_version); i++) {
+// if (bldit_version[i] == '.') continue;
+// if ((bldit_version[i] - '0') <= (version[i] - '0')) {
+// prev_pass = ((bldit_version[i] - '0') != (version[i] - '0'));
+// continue;
+// } else return prev_pass;
+// }
+// return true;
+//}
+
+bool lua_try_function(lua_State *L, char *lua_file, char *fname) {
+ lua_getfield(L, -1, fname);
+ if (!lua_isfunction(L, -1)) {
+ if (strcmp(lua_file, "bldit.lua"))
+ bldit_isnt_type(fname, "function");
+ else
+ lua_isnt_type(fname, "function");
+ } else if (lua_pcall(L, 0, 1, 0) != LUA_OK) {
+ log_warn(
+ "%s: '%s' function borked: %s",
+ lua_file, fname, lua_tostring(L, -1)
+ );
+ return false;
+ }
+ if (!lua_isnumber(L, -1) || lua_tonumber(L, -1) != 0) {
+ log_warn(
+ "%s: '%s' failed: %s",
+ lua_file, fname, lua_tostring(L, -1)
+ );
+ return false;
+ }
+ lua_pop(L, 1);
+ return true;
+}
+
+bool lua_try_table(lua_State *L, char *lua_file, char *tname) {
+ lua_getfield(L, -1, tname);
+ if (!lua_istable(L, -1)) {
+ if (strcmp(lua_file, "bldit.lua"))
+ bldit_isnt_type(tname, "table");
+ else
+ lua_isnt_type(tname, "table");
+ return false;
+ }
+ lua_pop(L, 1);
+ return true;
+}
diff --git a/src/lua_install.c b/src/lua_install.c
new file mode 100644
index 0000000..9c2369f
--- /dev/null
+++ b/src/lua_install.c
@@ -0,0 +1,128 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+
+#include <unistd.h>
+
+#include "pkgit_lua.h"
+
+#include "globs.h"
+#include "log.h"
+#include "pkg.h"
+
+void install_dependencies(lua_State *L) {
+ while (lua_next(L, -2) != 0) {
+ const char *depname = lua_tostring(L, -2);
+ if (depname && lua_istable(L, -1)) {
+ lua_getfield(L, -1, "url");
+ str_slc dep_url = mstrslc(lua_tostring(L, -1));
+ lua_pop(L, 1);
+ lua_getfield(L, -1, "version");
+ str_slc dep_version = mstrslc(lua_tostring(L, -1));
+ lua_pop(L, 1);
+ package_t pkg = pkg_create(dep_url);
+ pkg.version = str_from_str_slc(dep_version);
+ const int top = lua_gettop(L);
+ char cwd[MAX_PATH_LEN];
+ if (getcwd(cwd, sizeof(cwd)) != NULL) {
+ //install_pkg(pkg);
+ chdir(cwd);
+ }
+ lua_settop(L, top);
+ pkg_free(&pkg);
+ }
+ lua_pop(L, 1);
+ }
+}
+
+bool target_loop_install(lua_State *L, char* lua_file, str *target) {
+ if (!lua_try_table(L, lua_file, target->data)) return false;
+ lua_try_function(L, lua_file, "pre_install");
+ lua_try_function(L, lua_file, "install");
+ lua_try_function(L, lua_file, "post_install");
+ lua_pop(L, 1);
+ return true;
+}
+
+bool repo_install(const char *repository, const char* target) {
+ lua_getglobal(L, "repositories");
+ if (!config_loaded || !lua_istable(L, -1)) {
+ lua_isnt_type("repositories", "table");
+ lua_pop(L, 1);
+ return false;
+ }
+ str str_repo = mstr(repository);
+ if (!lua_try_table(L, "init.lua", str_repo.data)) {
+ lua_pop(L, 1);
+ return false;
+ }
+ str_free(&str_repo);
+ if (!lua_try_table(L, "init.lua", "targets")) return false;
+ str str_target = mstr(target);
+ bool target_loop_success = target_loop_install(L, "init.lua", &str_target);
+ str_free(&str_target);
+ lua_pop(L, 3);
+ return target_loop_success;
+}
+
+bool bldit_install(const char *target) {
+ init_bldit_state();
+ //if (!is_bldit_usable()) {
+ // log_error("bldit version is newer than the installed pkgit version");
+ // log_error("consider updating pkgit");
+ // if (!flags.force) return false;
+ //}
+ lua_pushfstring(B, "%s", inst_dirs.prefix.data);
+ lua_setglobal(B, "prefix");
+ lua_pop(B, 1);
+ lua_try_table(L, "bldit.lua", "targets");
+ str str_target = mstr(target);
+ bool target_loop_success = target_loop_install(B, "bldit.lua", &str_target);
+ str_free(&str_target);
+ lua_pop(B, 1);
+ lua_close(B);
+ return target_loop_success;
+}
+
+bool config_install(const char *path, const char *target) {
+ lua_getglobal(L, "build_systems");
+ lua_pushnil(L);
+ bool target_loop_success = false;
+ 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_try_table(L, "init.lua", "targets");
+ str str_target = mstr(target);
+ target_loop_success = target_loop_install(L, "init.lua", &str_target);
+ str_free(&str_target);
+ lua_pop(L, 2);
+ }
+ lua_pop(L, 1);
+ return target_loop_success;
+}
diff --git a/src/lua_vars.c b/src/lua_vars.c
index 3fd2ddb..ddb4c5a 100644
--- a/src/lua_vars.c
+++ b/src/lua_vars.c
@@ -18,10 +18,9 @@
*/
-#include "lua_vars.h"
+#include "pkgit_lua.h"
#include "globs.h"
-#include "lua_globs.h"
void init_install_directories(void) {
init_lua_state();
@@ -55,3 +54,15 @@ void init_install_directories(void) {
lua_pop(L, 1);
}
+
+void init_prefix_directory(void) {
+ lua_getglobal(L, "prefix");
+ if (!lua_isstring(L, -1)) {
+// printf(PRINT_ERROR "init.lua: 'prefix' is not a string.\n");
+ lua_isnt_type("prefix", "string.");
+ return;
+ }
+ inst_dirs.prefix = mstr(lua_tostring(L, -1));
+// snprintf(prefix_dir, MAX_PATH_LEN, "%s", lua_tostring(L, -1));
+ lua_pop(L, 1);
+}
diff --git a/src/main.c b/src/main.c
index 403355a..441332f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,27 +18,30 @@
*/
-// #include "cla_parse.h"
-#include "pkg.h"
+#include "parse_args.h"
#include "globs.h"
-#include "str.h"
+//#include "pkg.h"
+//#include "str.h"
+//#include "debug.h"
int main(int argc, char **argv) {
- (void)argc;
init_vars();
- package_t pkg = pkg_create(mstrslc(argv[1]));
- printf("NAME:\t");
- str_println(&pkg.name);
- printf("URL:\t");
- str_println(&pkg.url);
- printf("VER:\t");
- str_println(&pkg.version);
- printf("TRG:\t");
- str_println(&pkg.target);
- printf("SRC:\t");
- str_println(&pkg.src);
- pkg_free(&pkg);
- //cla_parse(argc, argv);
+ parse_args(argc, argv);
+
+ //package_t pkg = pkg_create(mstrslc(argv[1]));
+ //printf("NAME:\t");
+ //str_println(&pkg.name);
+ //printf("URL:\t");
+ //str_println(&pkg.url);
+ //printf("VER:\t");
+ //str_println(&pkg.version);
+ //printf("TRG:\t");
+ //str_println(&pkg.target);
+ //printf("SRC:\t");
+ //str_println(&pkg.src);
+ //pkg_free(&pkg);
+
+ //debug_print_inst_dirs();
free_vars();
return 0;
}
diff --git a/src/parse_args.c b/src/parse_args.c
index 1746ccc..785673d 100644
--- a/src/parse_args.c
+++ b/src/parse_args.c
@@ -177,7 +177,3 @@ void parse_args(int argc, char **argv) {
parse_flags(argc, argv);
parse_cmds(argc, argv);
}
-
-void str_parse_args(str *args[]) {
- if (!args[0]) { help(); return; }
-}