diff options
| -rw-r--r-- | meson.build | 2 | ||||
| -rw-r--r-- | src/args.c | 3 | ||||
| -rw-r--r-- | src/config.c | 70 | ||||
| -rw-r--r-- | src/config.h | 3 | ||||
| -rw-r--r-- | src/main.c | 3 | ||||
| -rw-r--r-- | src/repo.h | 4 |
6 files changed, 82 insertions, 3 deletions
diff --git a/meson.build b/meson.build index d80b3ce..f4ddee8 100644 --- a/meson.build +++ b/meson.build @@ -12,4 +12,4 @@ sources = [ 'src/repo.c' ] -executable('pow', sources, dependencies: deps) +executable('pow', sources, dependencies: deps, c_args: ['-D_GNU_SOURCE']) @@ -57,6 +57,9 @@ int pow_args_parse(int argc, char **argv, pow_args *args) { printf("Missing argument for -c\n"); } + if (!args->cfg_path) + args->cfg_path = DEFAULT_CONFIG_PATH; + args->flags = flags; return 0; diff --git a/src/config.c b/src/config.c index 54a505a..dd44129 100644 --- a/src/config.c +++ b/src/config.c @@ -2,6 +2,8 @@ #include "lauxlib.h" #include "lualib.h" +#include "stdlib.h" +#include "string.h" int pow_open_config(pow_config *cfg, char *path) { int err; @@ -46,14 +48,70 @@ int pow_open_config(pow_config *cfg, char *path) { lua_rawgeti(L, -1, i); if (lua_istable(L, -1)) { + pow_repo *repo = malloc(sizeof(pow_repo)); + + if (!repo) { + lua_pop(L, 1); + + printf("malloc for repo failed, what the fuck? something really bad " + "happened i guess\n"); + + return 1; + } + lua_getfield(L, -1, "path"); if (lua_isstring(L, -1)) { const char *path = lua_tostring(L, -1); + const char *path_dup = strdup(path); + + if (!path_dup) { + lua_pop(L, 1); + lua_pop(L, 1); + + free(repo); + + printf( + "malloc for path failed, what the fuck? something really bad " + "happened i guess\n"); + + return 1; + } + + repo->path = path_dup; + printf("%s\n", path); + } else { + printf("invalid (or non existent) path for repo\n"); + + lua_pop(L, 1); + lua_pop(L, 1); + + free(repo); + + continue; } + pow_repo **repos = + realloc(cfg->repos, (cfg->repos_count + 1) * sizeof(*repos)); + + if (!repos) { + printf("failed realloc! bad things coming\n"); + + lua_pop(L, 1); + lua_pop(L, 1); + + free(repo->path); + free(repo); + + return 1; + } + + repos[cfg->repos_count++] = repo; + + cfg->repos = repos; + lua_pop(L, 1); } @@ -63,3 +121,15 @@ int pow_open_config(pow_config *cfg, char *path) { return 0; } + +int pow_close_config(pow_config *cfg) { + for (size_t i = 0; i < cfg->repos_count; i++) { + free(cfg->repos[i]->path); + free(cfg->repos[i]); + } + + free(cfg->repos); + lua_close(cfg->L); + + return 0; +} diff --git a/src/config.h b/src/config.h index ba2d319..e672267 100644 --- a/src/config.h +++ b/src/config.h @@ -1,11 +1,14 @@ #include "repo.h" #include "lua.h" +#include <stddef.h> typedef struct { pow_repo **repos; + size_t repos_count; lua_State *L; } pow_config; int pow_open_config(pow_config *cfg, char *path); +int pow_close_config(pow_config *cfg); @@ -14,7 +14,8 @@ int main(int argc, char *argv[]) { pow_config config = {0}; - pow_open_config(&config, "config.lua"); + pow_open_config(&config, args.cfg_path); + pow_close_config(&config); return 0; } @@ -1,3 +1,5 @@ +#include "stddef.h" + typedef struct { - char *path; + const char *path; } pow_repo; |
