From f113371523bc26e320e5ef8799e6ab123af9618e Mon Sep 17 00:00:00 2001 From: forever2k38 Date: Wed, 30 Sep 2026 22:01:45 +0200 Subject: initial install support, better readme, relicense, and more! --- src/package.c | 72 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 25 deletions(-) (limited to 'src/package.c') diff --git a/src/package.c b/src/package.c index 6aeee65..9c538f7 100644 --- a/src/package.c +++ b/src/package.c @@ -1,3 +1,24 @@ +/* + + pow! - the pow!erful package manager + + Copyright (C) 2026 forever2k38 + + 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 . + +*/ + #include "package.h" #include "lauxlib.h" @@ -6,13 +27,14 @@ #include "pow_lua.h" #include "stdio.h" #include "string.h" +#include "log.h" #include -int extract_package_info(lua_State *L, pow_package *pkg) { +int pow_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"); + pow_log_error("invalid package, name is not string (or doesn't exist)"); return 1; } @@ -20,7 +42,7 @@ int extract_package_info(lua_State *L, pow_package *pkg) { pkg->name = strdup(lua_tostring(L, -1)); if (!pkg->name) { - printf("strdup failed trying to get package name\n"); + pow_log_error("strdup failed trying to get package name"); return 1; } @@ -32,7 +54,7 @@ int extract_package_info(lua_State *L, pow_package *pkg) { if (!lua_isstring(L, -1)) { free(pkg->name); - printf("invalid package, version is not string (or doesn't exist)\n"); + pow_log_error("invalid package, version is not string (or doesn't exist)"); return 1; } @@ -44,7 +66,7 @@ int extract_package_info(lua_State *L, pow_package *pkg) { if (!pkg->version) { free(pkg->name); - printf("strdup failed trying to get package name\n"); + pow_log_error("strdup failed trying to get package name"); return 1; } @@ -55,14 +77,14 @@ int extract_package_info(lua_State *L, pow_package *pkg) { free(pkg->name); free(pkg->version); - printf("sources is not table or doesn't exist\n"); + pow_log_error("sources is not table or doesn't exist"); return 1; } - size_t sources_len = lua_rawlen(L, -1); + size_t sources_len = lua_objlen(L, -1); - for (size_t i = 0; i < sources_len; i++) { + for (size_t i = 0; i < sources_len; i++) { lua_pushinteger(L, i + 1); lua_gettable(L, -2); @@ -75,7 +97,7 @@ int extract_package_info(lua_State *L, pow_package *pkg) { pow_package_source *src = malloc(sizeof(pow_package_source)); if (!src) { - printf("failed to malloc pow_package_source\n"); + pow_log_error("failed to malloc pow_package_source\n"); return 1; } @@ -83,7 +105,7 @@ int extract_package_info(lua_State *L, pow_package *pkg) { if (!lua_isstring(L, -1)) { lua_pop(L, 1); - printf("path is not string\n"); + pow_log_error("path is not string"); return 1; } @@ -91,7 +113,7 @@ int extract_package_info(lua_State *L, pow_package *pkg) { const char *in = strdup(lua_tostring(L, -1)); if (!in) { - printf("failed to strdup path?"); + pow_log_error("failed to strdup path?"); return 1; } @@ -105,7 +127,7 @@ int extract_package_info(lua_State *L, pow_package *pkg) { if (!lua_isstring(L, -1)) { lua_pop(L, 1); - printf("out is not string\n"); + pow_log_error("out is not string"); return 1; } @@ -117,7 +139,7 @@ int extract_package_info(lua_State *L, pow_package *pkg) { lua_pop(L, 1); - printf("out strdup failed?\n"); + pow_log_error("out strdup failed?"); return 1; } @@ -132,7 +154,7 @@ int extract_package_info(lua_State *L, pow_package *pkg) { free(in); free(out); - printf("hash is not string?\n"); + pow_log_error("hash is not string?"); return 1; } @@ -143,7 +165,7 @@ int extract_package_info(lua_State *L, pow_package *pkg) { free(in); free(out); - printf("failed to strdup hash\n"); + pow_log_error("failed to strdup hash"); return 1; } @@ -158,15 +180,15 @@ int extract_package_info(lua_State *L, pow_package *pkg) { pkg->sources[pkg->source_count - 1] = src; lua_pop(L, 1); - lua_pop(L, 1); } lua_pop(L, 1); - + lua_pop(L, 1); + return 0; } -int pow_package_open(pow_package *pkg, const char *path) { +int pow_package_open(pow_package *pkg, pow_config *cfg, const char *path) { FILE *fp; int c; int err; @@ -174,7 +196,7 @@ int pow_package_open(pow_package *pkg, const char *path) { fp = fopen(path, "r"); if (!fp) { - printf("error opening package definition, doesn't exist?\n"); + pow_log_error("error opening package definition, doesn't exist?"); return 1; } @@ -194,12 +216,12 @@ int pow_package_open(pow_package *pkg, const char *path) { fclose(fp); free(package_buffer); - printf("failed to realloc package buffer\n"); + pow_log_error("failed to realloc package buffer"); return 1; } - printf("reallocated package buffer due to size constraints\n"); + pow_log_info("reallocated package buffer due to size constraints"); package_buffer = tmp; } @@ -216,7 +238,7 @@ int pow_package_open(pow_package *pkg, const char *path) { pkg->L = L; luaL_openlibs(L); - pow_lua_load(L); + pow_lua_load(L, cfg); luaL_loadstring(L, package_buffer); @@ -225,13 +247,13 @@ int pow_package_open(pow_package *pkg, const char *path) { free(package_buffer); if (err) { - printf("%s\n", lua_tostring(L, -1)); + pow_log_error("%s", lua_tostring(L, -1)); return 1; } if (!lua_istable(L, -1)) { - printf("invalid package, scripts must return table\n"); + pow_log_error("invalid package, scripts must return table"); return 1; } @@ -239,7 +261,7 @@ int pow_package_open(pow_package *pkg, const char *path) { pkg->source_count = 0; pkg->sources = NULL; - extract_package_info(L, pkg); + pow_extract_package_info(L, pkg); pkg->path = path; pkg->pkg_ref = luaL_ref(L, LUA_REGISTRYINDEX); -- cgit v1.3