aboutsummaryrefslogtreecommitdiff
path: root/src/package.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/package.c')
-rw-r--r--src/package.c118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/package.c b/src/package.c
index 98d6f00..ad7e4d0 100644
--- a/src/package.c
+++ b/src/package.c
@@ -6,6 +6,7 @@
#include "pow_lua.h"
#include "stdio.h"
#include "string.h"
+#include <lua.h>
int extract_package_info(lua_State *L, pow_package *pkg) {
lua_getfield(L, -1, "name");
@@ -29,6 +30,8 @@ int extract_package_info(lua_State *L, pow_package *pkg) {
lua_getfield(L, -1, "version");
if (!lua_isstring(L, -1)) {
+ free(pkg->name);
+
printf("invalid package, version is not string (or doesn't exist)\n");
return 1;
@@ -39,11 +42,120 @@ int extract_package_info(lua_State *L, pow_package *pkg) {
lua_pop(L, 1);
if (!pkg->version) {
+ free(pkg->name);
+
printf("strdup failed trying to get package name\n");
return 1;
}
+ lua_getfield(L, -1, "sources");
+
+ if (!lua_istable(L, -1)) {
+ free(pkg->name);
+ free(pkg->version);
+
+ printf("sources is not table or doesn't exist\n");
+
+ return 1;
+ }
+
+ size_t sources_len = lua_rawlen(L, -1);
+
+ for (size_t i = 0; i < sources_len; i++) {
+ lua_pushinteger(L, i + 1);
+
+ lua_gettable(L, -2);
+
+ if (lua_type(L, -1) == LUA_TNIL)
+ break;
+
+ lua_getfield(L, -1, "path");
+
+ pow_package_source *src = malloc(sizeof(pow_package_source));
+
+ if (!src) {
+ printf("failed to malloc pow_package_source\n");
+
+ return 1;
+ }
+
+ if (!lua_isstring(L, -1)) {
+ lua_pop(L, 1);
+
+ printf("path is not string\n");
+
+ return 1;
+ }
+
+ const char *in = strdup(lua_tostring(L, -1));
+
+ if (!in) {
+ printf("failed to strdup path?");
+
+ return 1;
+ }
+
+ src->in = in;
+
+ lua_pop(L, 1);
+
+ lua_getfield(L, -1, "out");
+
+ if (!lua_isstring(L, -1)) {
+ lua_pop(L, 1);
+
+ printf("out is not string\n");
+
+ return 1;
+ }
+
+ const char *out = strdup(lua_tostring(L, -1));
+
+ if (!out) {
+ free(in);
+
+ lua_pop(L, 1);
+
+ printf("out strdup failed?\n");
+
+ return 1;
+ }
+
+ src->out = out;
+
+ lua_pop(L, 1);
+
+ lua_getfield(L, -1, "hash");
+
+ if (!lua_isstring(L, -1)) {
+ free(in);
+ free(out);
+
+ printf("hash is not string?\n");
+
+ return 1;
+ }
+
+ const char *hash = strdup(lua_tostring(L, -1));
+
+ src->hash = hash;
+
+ pkg->source_count++;
+
+ printf("attempting to malloc\n");
+
+ pkg->sources =
+ realloc(pkg->sources, pkg->source_count * sizeof(pow_package_source *));
+
+ pkg->sources[pkg->source_count - 1] = src;
+
+ lua_pop(L, 1);
+ lua_pop(L, 1);
+ }
+
+ lua_pop(L, 1);
+
return 0;
}
@@ -73,6 +185,7 @@ int pow_package_open(pow_package *pkg, const char *path) {
if (!tmp) {
fclose(fp);
+ free(package_buffer);
printf("failed to realloc package buffer\n");
@@ -87,6 +200,8 @@ int pow_package_open(pow_package *pkg, const char *path) {
package_buffer[total++] = c;
}
+ fclose(fp);
+
package_buffer[total] = '\0';
lua_State *L = luaL_newstate();
@@ -114,6 +229,9 @@ int pow_package_open(pow_package *pkg, const char *path) {
return 1;
}
+ pkg->source_count = 0;
+ pkg->sources = NULL;
+
extract_package_info(L, pkg);
pkg->path = path;