package

simple and robust package manager
Log | Files | Refs | README

commit c850526893338588d5b0fc2ed8546a5955e41746
parent 0521e5ef966ebbf55c7cb0224c2e8981d4c352f3
Author: Josuah Demangeon <mail@josuah.net>
Date:   Tue,  1 Jan 2019 22:31:56 +0100

add a recursion function

Diffstat:
M.gitignore | 1+
Mgenalloc.h | 23++++++++++++-----------
Dpack-build | 0
Mpack.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
4 files changed, 87 insertions(+), 19 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1,2 @@ *.o +pack-build diff --git a/genalloc.h b/genalloc.h @@ -7,16 +7,17 @@ typedef stralloc genalloc; #define GENALLOC_ZERO STRALLOC_ZERO -#define genalloc_ready(type, sa, n) stralloc_ready((sa), (n) * sizeof (type)) -#define genalloc_len(type, sa) ((sa)->n / sizeof (type)) -#define genalloc_cat(sa1, sa2) stralloc_cat((sa1), (sa2)) -#define genalloc_catb(sa, b) stralloc_catb((sa), (x), sizeof *(x) * (n)) -#define genalloc_append(sa, x) stralloc_catb((sa), (char *)&(x), sizeof (x)) -#define genalloc_copy(sa1, sa2) stralloc_copy((sa1), (sa2)) -#define genalloc_copyb(sa, x, n) genalloc_copyb((sa), (char const) (x), (n) * sizeof (x)) -#define genalloc_free(sa) stralloc_free(sa) -#define genalloc_init(sa) stralloc_init(sa) -#define genalloc_zero(sa) stralloc_zero(sa) -#define genalloc_get(type, sa, i) (((type *)(sa)->x) + i) +#define genalloc_ready(sa, type, n) stralloc_ready((sa), (n) * sizeof (type)) +#define genalloc_readyplus(sa, type, n) stralloc_readyplus((sa), (n) * sizeof (type)) +#define genalloc_len(sa, type) ((sa)->n / sizeof (type)) +#define genalloc_get(sa, type, i) (((type *)(sa)->x) + i) +#define genalloc_cat(sa1, sa2) stralloc_cat((sa1), (sa2)) +#define genalloc_catb(sa, b) stralloc_catb((sa), (x), sizeof *(x) * (n)) +#define genalloc_append(sa, x) stralloc_catb((sa), (char *)&(x), sizeof (x)) +#define genalloc_copy(sa1, sa2) stralloc_copy((sa1), (sa2)) +#define genalloc_copyb(sa, x, n) genalloc_copyb((sa), (char const) (x), (n) * sizeof (x)) +#define genalloc_free(sa) stralloc_free(sa) +#define genalloc_init(sa) stralloc_init(sa) +#define genalloc_zero(sa) stralloc_zero(sa) #endif diff --git a/pack-build b/pack-build Binary files differ. diff --git a/pack.c b/pack.c @@ -1,10 +1,14 @@ #include <sys/stat.h> #include <ctype.h> +#include <unistd.h> +#include <errno.h> -#include "pack.h" -#include "stralloc.h" +#include "buffer.h" #include "fs.h" #include "mem.h" +#include "open.h" +#include "pack.h" +#include "stralloc.h" size_t pack_scan(pack *p, char const *s) @@ -15,7 +19,7 @@ pack_scan(pack *p, char const *s) if (s[0] == '/') return 0; for (i = 0 ;; ++i) { - if (!isprint(s[i])) return 0; + if (!isprint(s[i])) { *p->ver = '\0'; return i; } if (s[i] == ' ') return 0; if (s[i] == '/') break; if (i >= sizeof p->name) return 0; @@ -35,18 +39,80 @@ pack_scan(pack *p, char const *s) } int -pack_dep(genalloc *packs, char const *def) +pack_version(pack *p) { - (void)packs; - (void)def; + if (*p->ver) return 1; return 1; } +static int +pack_dep_has(pack const *p, genalloc *packs) +{ + size_t i; + + for (i = 0; i < genalloc_len(packs, pack); ++i) { + pack *x = genalloc_get(packs, pack, i); + if (str_equal(x->name, p->name) && str_equal(x->ver, p->ver)) return 1; + } + + return 0; +} + int -pack_version(pack *p) +pack_dep_recurse(pack const *p, genalloc *packs, char const *def) { - (void)p; + stralloc path; + stralloc line; + buffer b; + char buf[1024]; + int fd = -1; + int ret = 0; + + stralloc_init(&path); + if (!stralloc_copys(&path, def)) goto err; + if (!stralloc_append(&path, '/')) goto err; + if (!stralloc_cats(&path, p->name)) goto err; + if (!stralloc_append(&path, '/')) goto err; + if (!stralloc_cats(&path, p->ver)) goto err; + if (!stralloc_cats(&path, "/dependencies")) goto err; + if (!stralloc_append(&path, '\0')) goto err; + + fd = open_read(path.x); + stralloc_free(&path); + if (fd == -1) return 0; + + errno = 0; + buffer_init(&b, &read, fd, buf, sizeof buf); + stralloc_init(&line); + while ((buffer_getline(&b, &line))) { + pack new; + + genalloc_get(packs, pack, genalloc_len(packs, pack)); + pack_scan(&new, line.x); + pack_version(&new); + + if (pack_dep_has(p, packs)) continue; + if (!genalloc_append(packs, new)) goto err; + } + stralloc_free(&line); + + ret = 1; + +err: close(fd); + return ret && (errno == 0); +} + +int +pack_dep(genalloc *packs, char const *def) +{ + size_t n = genalloc_len(packs, pack); + size_t i; + + for (i = 0; i < n; ++i) { + pack *p = genalloc_get(packs, pack, i); + if (!pack_dep_recurse(p, packs, def)) return 0; + } return 1; }