aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorforever2k38 <forever2k38@noreply.codeberg.org>2026-08-31 13:31:32 +0200
committerforever2k38 <forever2k38@noreply.codeberg.org>2026-08-31 13:31:32 +0200
commit0334d0060c17f57d7a9b64677e3ca199a6029019 (patch)
treeaae025393d3c37e92fb071954618964671c5d9bf /src
parent8f5dc1930debe95e106595da33951bd870672d85 (diff)
feat: package info
Diffstat (limited to 'src')
-rw-r--r--src/args.c6
-rw-r--r--src/config.c1
-rw-r--r--src/main.c4
-rw-r--r--src/package.c125
-rw-r--r--src/package.h6
-rw-r--r--src/pow_lua.c16
-rw-r--r--src/pow_lua.h3
-rw-r--r--src/repo.c14
8 files changed, 169 insertions, 6 deletions
diff --git a/src/args.c b/src/args.c
index d5ea57e..44117b9 100644
--- a/src/args.c
+++ b/src/args.c
@@ -86,11 +86,11 @@ int pow_args_parse(int argc, char **argv, pow_args *args) {
case 'Q': {
if (flags & POW_FLAG_QUERY) {
printf("flag double defined, skipping\n");
+ }
- flags |= POW_FLAG_QUERY;
+ flags |= POW_FLAG_QUERY;
- next_flag_package = true;
- }
+ next_flag_package = true;
}
}
}
diff --git a/src/config.c b/src/config.c
index d82cf9f..d01499d 100644
--- a/src/config.c
+++ b/src/config.c
@@ -127,6 +127,7 @@ int pow_close_config(pow_config *cfg) {
}
free(cfg->repos);
+
lua_close(cfg->L);
return 0;
diff --git a/src/main.c b/src/main.c
index 64cb537..67ba153 100644
--- a/src/main.c
+++ b/src/main.c
@@ -20,10 +20,12 @@ int main(int argc, char *argv[]) {
pow_open_config(&config, args.cfg_path);
if (args.flags & POW_FLAG_QUERY) {
- repo_check_package(config.repos, config.repos_count, args.package);
+ repo_check_package(config.repos, config.repos_count, "samurai");
}
pow_close_config(&config);
+ fflush(stdout);
+
return 0;
}
diff --git a/src/package.c b/src/package.c
index e69de29..2e571e9 100644
--- a/src/package.c
+++ b/src/package.c
@@ -0,0 +1,125 @@
+#include "package.h"
+
+#include "lauxlib.h"
+#include "lualib.h"
+#include "malloc.h"
+#include "pow_lua.h"
+#include "stdio.h"
+#include "string.h"
+
+int extract_package_info(lua_State *L, pow_package *pkg) {
+ lua_getfield(L, -1, "name");
+
+ if (!lua_isstring(L, -1)) {
+ printf("invalid package, name is not string (or doesn't exist)\n");
+
+ return 1;
+ }
+
+ pkg->name = strdup(lua_tostring(L, -1));
+
+ if (!pkg->name) {
+ printf("strdup failed trying to get package name\n");
+
+ return 1;
+ }
+
+ lua_pop(L, 1);
+
+ lua_getfield(L, -1, "version");
+
+ if (!lua_isstring(L, -1)) {
+ printf("invalid package, version is not string (or doesn't exist)\n");
+
+ return 1;
+ }
+
+ pkg->version = strdup(lua_tostring(L, -1));
+
+ if (!pkg->version) {
+ printf("strdup failed trying to get package name\n");
+ }
+
+ return 0;
+}
+
+int package_open(pow_package *pkg, const char *path) {
+ FILE *fp;
+ int c;
+ int err;
+
+ fp = fopen(path, "r");
+
+ if (!fp) {
+ printf("error opening package definition, doesn't exist?\n");
+
+ return 1;
+ }
+
+ char *package_buffer = malloc(4096);
+
+ size_t total = 0;
+ size_t capacity = 4096;
+
+ while ((c = fgetc(fp)) != EOF) {
+ if (total + 1 >= capacity) {
+ capacity *= 2;
+
+ char *tmp = realloc(package_buffer, capacity);
+
+ if (!tmp) {
+ fclose(fp);
+
+ printf("failed to realloc package buffer\n");
+
+ return 1;
+ }
+
+ printf("reallocated package buffer due to size constraints\n");
+
+ package_buffer = tmp;
+ }
+
+ package_buffer[total++] = c;
+ }
+
+ package_buffer[total] = '\0';
+
+ lua_State *L = luaL_newstate();
+
+ pkg->L = L;
+
+ luaL_openlibs(L);
+ pow_lua_load(L);
+
+ luaL_loadstring(L, package_buffer);
+
+ err = lua_pcall(L, 0, 1, 0);
+
+ free(package_buffer);
+
+ if (err) {
+ printf("%s\n", lua_tostring(L, -1));
+
+ return 1;
+ }
+
+ if (!lua_istable(L, -1)) {
+ printf("invalid package, scripts must return table\n");
+
+ return 1;
+ }
+
+ extract_package_info(L, pkg);
+
+ return 0;
+}
+
+int package_close(pow_package *pkg) {
+ free(pkg->name);
+ free(pkg->version);
+
+ lua_close(pkg->L);
+
+ return 0;
+}
diff --git a/src/package.h b/src/package.h
index c555428..a1e4595 100644
--- a/src/package.h
+++ b/src/package.h
@@ -1,3 +1,4 @@
+#include "lua.h"
#include "stddef.h"
typedef struct {
@@ -6,4 +7,9 @@ typedef struct {
const char *description;
size_t *arch;
+
+ lua_State *L;
} pow_package;
+
+int package_open(pow_package *pkg, const char *path);
+int package_close(pow_package *pkg);
diff --git a/src/pow_lua.c b/src/pow_lua.c
new file mode 100644
index 0000000..0d0214d
--- /dev/null
+++ b/src/pow_lua.c
@@ -0,0 +1,16 @@
+#include "pow_lua.h"
+
+#include "lauxlib.h"
+#include "lualib.h"
+
+int pow_lua_hello(lua_State *L) { printf("hi\n"); }
+
+luaL_Reg libpow[] = {{"hello", pow_lua_hello}};
+
+int pow_lua_load(lua_State *L) {
+ luaL_newlib(L, libpow);
+
+ lua_setglobal(L, "pow");
+
+ return 0;
+}
diff --git a/src/pow_lua.h b/src/pow_lua.h
new file mode 100644
index 0000000..2196ae9
--- /dev/null
+++ b/src/pow_lua.h
@@ -0,0 +1,3 @@
+#include "lua.h"
+
+int pow_lua_load(lua_State *L);
diff --git a/src/repo.c b/src/repo.c
index 9238a27..8d16320 100644
--- a/src/repo.c
+++ b/src/repo.c
@@ -1,11 +1,11 @@
-#define _XOPEN_SOURCE 700
-
#include "repo.h"
#include "dirent.h"
#include "limits.h"
#include "malloc.h"
+#include "package.h"
#include "stdio.h"
+#include <lua.h>
int repo_check_package(pow_repo **repos, size_t repo_count, const char *query) {
if (!repos) {
@@ -49,6 +49,16 @@ int repo_check_package(pow_repo **repos, size_t repo_count, const char *query) {
printf("found package %s in repo %s\n", query, repos[i]->path);
+ pow_package *pkg = malloc(sizeof(pow_package));
+
+ package_open(pkg, "../testing/samurai/1.3.lua");
+
+ printf("Name: %s\n"
+ "Version: %s\n",
+ pkg->name, pkg->version);
+
+ package_close(pkg);
+
closedir(dp);
}