libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit c3a94604ee2f8d7cc12c0deff2da0b344c37cc3f
parent 0f2dc56924ffd4207de3a7654915de12c02d96a5
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon,  3 Apr 2017 22:18:10 +0200

Add(Packer): Beginning of JSON config parsing

Diffstat:
Minc/packer.h | 24++++++++++++++++++++----
Msrc/packer.c | 153+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
2 files changed, 147 insertions(+), 30 deletions(-)

diff --git a/inc/packer.h b/inc/packer.h @@ -22,7 +22,6 @@ # include <sys/stat.h> # include <json.h> - # define PACKER_DEF_CONF_FN "package.json" typedef enum packer_type_e { @@ -30,10 +29,27 @@ typedef enum packer_type_e { PACKER_TYPE_ARCHIVE } packer_type_t; +# define PACKER_CONF_PACKAGE_TOKEN "package" +# define PACKER_CONF_PACKAGE_NAME_TOKEN "name" +# define PACKER_CONF_PACKAGE_VERSION_TOKEN "version" +# define PACKER_CONF_PACKAGE_DESC_TOKEN "description" + + +typedef struct packer_header_package_s { + char *name; /*!< Name of the package */ + char *version; /*!< Version of the package */ + char *description; /*!< Description of the package */ +} packer_header_package_t; + +typedef struct packer_header_s { + packer_header_package_t *package; /*!< Package header section */ +} packer_header_t; + typedef struct packer_s { - char *str; /*!< Directory, or path of the package */ - json_object *json; /*!< JSON object */ - packer_type_t type; /*!< Type of packer */ + char *str; /*!< Directory, or path of the package */ + json_object *json; /*!< JSON object */ + packer_type_t type; /*!< Type of packer */ + packer_header_t *header; /*!< Header of the MPX format */ } packer_t; /*! diff --git a/src/packer.c b/src/packer.c @@ -36,6 +36,48 @@ cleanup: } +static packer_header_package_t *packer_header_package_init(void) { + packer_header_package_t *ret; + + ret = malloc(sizeof(*ret)); + if (ret == NULL) + return NULL; + + ret->name = NULL; + ret->version = NULL; + ret->description = NULL; + return ret; +} + +static void packer_header_package_free(packer_header_package_t *ptr) { + if (ptr != NULL) + { + free(ptr->name); + free(ptr->version); + free(ptr->description); + free(ptr); + } +} + +static packer_header_t *packer_header_init(void) { + packer_header_t *ret; + + ret = malloc(sizeof(*ret)); + if (ret == NULL) + return NULL; + + ret->package = NULL; + return ret; +} + +static void packer_header_free(packer_header_t *ptr) { + if (ptr != NULL) + { + packer_header_package_free(ptr->package); + free(ptr); + } +} + packer_t *packer_init_dir(const char *dir) { packer_t *ret; @@ -65,48 +107,107 @@ void packer_free(packer_t *ptr) { } } +static bool packer_read_config_package(packer_t *ctx, struct json_object *obj) +{ + struct json_object_iterator it, it_end; + struct json_object *tmp; + const char *name; + + assert(ctx != NULL); + + /* Can't raise an assertion since NULL is a valid type in JSON */ + if (obj == NULL) + return false; + + it = json_object_iter_begin(obj); + it_end = json_object_iter_end(obj); + ctx->header->package = packer_header_package_init(); + + while (!json_object_iter_equal(&it, &it_end)) + { + name = json_object_iter_peek_name(&it); + if (strcmp(name, PACKER_CONF_PACKAGE_NAME_TOKEN) == 0) + { + tmp = json_object_iter_peek_value(&it); + if (json_object_get_type(tmp) != json_type_string) + goto cleanup; + ctx->header->package->name = strdup(json_object_get_string(tmp)); + } + else if (strcmp(name, PACKER_CONF_PACKAGE_VERSION_TOKEN) == 0) + { + tmp = json_object_iter_peek_value(&it); + if (json_object_get_type(tmp) != json_type_string) + goto cleanup; + ctx->header->package->version = strdup(json_object_get_string(tmp)); + } + else if (strcmp(name, PACKER_CONF_PACKAGE_DESC_TOKEN) == 0) + { + tmp = json_object_iter_peek_value(&it); + if (json_object_get_type(tmp) != json_type_string) + goto cleanup; + ctx->header->package->description = strdup(json_object_get_string(tmp)); + } + /* Wrong token */ + else + goto cleanup; + json_object_iter_next(&it); + } + return true; + +cleanup: + packer_header_package_free(ctx->header->package); + return false; +} + +static bool packer_read_config_file(packer_t *ctx) { + struct json_object_iterator it, it_end; + const char *name; + + assert(ctx != NULL); + it = json_object_iter_begin(ctx->json); + it_end = json_object_iter_end(ctx->json); + ctx->header = packer_header_init(); + + while (!json_object_iter_equal(&it, &it_end)) + { + name = json_object_iter_peek_name(&it); + if (strcmp(name, PACKER_CONF_PACKAGE_TOKEN) == 0) + packer_read_config_package(ctx, json_object_iter_peek_value(&it)); + else if (strcmp(name, "compilation")) + ; + else if (strcmp(name, "dependencies")) + ; + /* Wrong token */ + else + goto cleanup; + json_object_iter_next(&it); + } + return true; + +cleanup: + packer_header_free(ctx->header); + return false; +} + bool packer_read_dir(packer_t *ctx) { - struct stat st; - int fd; - char *file_c = NULL; char *old_pwd = getenv("PWD"); assert(ctx != NULL); if (ctx->type != PACKER_TYPE_DIRECTORY) return false; - if (chdir(ctx->str) == -1) return false; - fd = open(PACKER_DEF_CONF_FN, O_RDONLY); - if (fd == -1) - goto error; - - if (fstat(fd, &st) == -1) - goto error; - - file_c = malloc(st.st_size + 1); - if (file_c == NULL) - goto error; - if (read(fd, file_c, st.st_size) == -1) - goto error; - - file_c[st.st_size] = 0; - ctx->json = json_tokener_parse(file_c); + ctx->json = json_object_from_file(PACKER_DEF_CONF_FN); if (ctx->json == NULL) goto error; chdir(old_pwd); - free(file_c); - close(fd); - return true; + return packer_read_config_file(ctx); error: - if (file_c != NULL) - free(file_c); - if (fd != -1) - close(fd); chdir(old_pwd); return false; } +