diff options
| -rw-r--r-- | meson.build | 7 | ||||
| -rw-r--r-- | src/args.c | 6 | ||||
| -rw-r--r-- | src/config.c | 1 | ||||
| -rw-r--r-- | src/main.c | 4 | ||||
| -rw-r--r-- | src/package.c | 125 | ||||
| -rw-r--r-- | src/package.h | 6 | ||||
| -rw-r--r-- | src/pow_lua.c | 16 | ||||
| -rw-r--r-- | src/pow_lua.h | 3 | ||||
| -rw-r--r-- | src/repo.c | 14 | ||||
| -rw-r--r-- | testing/samurai/1.3.lua | 2 |
10 files changed, 173 insertions, 11 deletions
diff --git a/meson.build b/meson.build index d10c9de..fba438e 100644 --- a/meson.build +++ b/meson.build @@ -1,7 +1,7 @@ project('pow', 'c', default_options: ['c_std=c99']) deps = [ - dependency('lua54') + dependency('lua55') ] sources = [ @@ -11,7 +11,8 @@ sources = [ 'src/config.c', 'src/repo.c', 'src/utils.c', - 'src/package.c' + 'src/package.c', + 'src/pow_lua.c' ] -executable('pow', sources, dependencies: deps, c_args: ['-D_GNU_SOURCE']) +executable('pow', sources, dependencies: deps, c_args: ['-D_GNU_SOURCE', '-D_XOPEN_SOURCE=700']) @@ -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; @@ -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); @@ -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); } diff --git a/testing/samurai/1.3.lua b/testing/samurai/1.3.lua index eda38a5..8da795d 100644 --- a/testing/samurai/1.3.lua +++ b/testing/samurai/1.3.lua @@ -1,7 +1,5 @@ local package = {} -local pow = require("pow") - package.name = "samurai" package.version = "1.3" package.description = "ninja compatible build tool written in C" |
