aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/args.c3
-rw-r--r--src/config.c70
-rw-r--r--src/config.h3
-rw-r--r--src/main.c3
-rw-r--r--src/repo.h4
5 files changed, 81 insertions, 2 deletions
diff --git a/src/args.c b/src/args.c
index 7962ad4..b353579 100644
--- a/src/args.c
+++ b/src/args.c
@@ -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);
diff --git a/src/main.c b/src/main.c
index 1eff24e..abc3ffc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;
}
diff --git a/src/repo.h b/src/repo.h
index 9787375..be04b3c 100644
--- a/src/repo.h
+++ b/src/repo.h
@@ -1,3 +1,5 @@
+#include "stddef.h"
+
typedef struct {
- char *path;
+ const char *path;
} pow_repo;