package

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

commit 4bf91a50b13811ca0c6762b9419a0573f491753d
parent 30c5bd25f2a2858f72375399fda1782a7cfb890c
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sun, 13 Jan 2019 11:57:31 +0100

pack-tree: new tool list dependencies as a tree

Diffstat:
M.gitignore | 1+
MMakefile | 6+++++-
Mpack-get.c | 3++-
Mpack-tree.c | 88++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Mpack.c | 2+-
5 files changed, 58 insertions(+), 42 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -4,3 +4,4 @@ pack-build pack-add pack-dep pack-get +pack-tree diff --git a/Makefile b/Makefile @@ -1,4 +1,4 @@ -bin = pack-build pack-dep pack-add pack-get +bin = pack-build pack-dep pack-add pack-get pack-tree .PHONY: all all: ${bin} @@ -29,6 +29,10 @@ pack-get-o = pack-get.o pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o pack-get: ${pack-get-o} ./make-bin $@ ${pack-get-o} +pack-tree-o = pack-tree.o pack.o buffer.o fmt.o fs.o log.o mem.o str.o stralloc.o forkexec.o +pack-tree: ${pack-tree-o} + ./make-bin $@ ${pack-tree-o} + .c.o: ./make-o $< diff --git a/pack-get.c b/pack-get.c @@ -86,7 +86,8 @@ main(int argc, char **argv) buffer_init(&b, &read, fd, buf, sizeof buf); while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa)) > 0) { - if (sa.x[sa.n - 1] == '\n') sa.x[--(sa.n)] = '\0'; + if (sa.x[sa.n - 1] == '\n') --sa.n; + if (!stralloc_cat0(&sa)) die_nomem(); if (str_start(sa.x, "http://")) if (get(&p, "pack-http", sa.x)) break; if (str_start(sa.x, "https://")) if (get(&p, "pack-http", sa.x)) break; if (str_start(sa.x, "git://")) if (get(&p, "pack-git", sa.x)) break; diff --git a/pack-tree.c b/pack-tree.c @@ -1,55 +1,62 @@ +#include <unistd.h> +#include <errno.h> + #include "pack.h" -#include "stalloc.h" +#include "stralloc.h" #include "open.h" #include "buffer.h" +#include "env.h" +#include "fs.h" +#include "log.h" -void -pack_tree(pack *p, stralloc *prefix) +int +tree(pack *p, stralloc *prefix, char const *def) { stralloc sa = STRALLOC_ZERO; buffer b; char buf[1024]; ssize_t r; int fd = -1; - int ret; + char *ver[2]; + size_t n; + int ret = 0; if (!pack_version(p, def)) goto err; - - stralloc_init(&sa); - if (stralloc_cats(&sa, def)) goto err; - if (stralloc_cats(&sa, "/")) goto err; - if (stralloc_cats(&sa, p.name)) goto err; - if (stralloc_cats(&sa, "/")) goto err; - if (stralloc_cats(&sa, p.ver)) goto err; - if (stralloc_cats(&sa, "/dependencies")) goto err; - if (stralloc_cat0(&sa)) goto err; - - errno = 0; - fd = open_read(sa.x); - if (fd == -1) { ret = (errno != ENOENT); goto end; } - - buffer_put(buffer_1, prefix->x, sa->n); + if (!stralloc_cats(prefix, p->name)) goto err; + if (!stralloc_cats(prefix, " - ")) goto err; + if (!stralloc_cats(prefix, p->ver)) goto err; + if (!stralloc_cats(prefix, "/")) goto err; buffer_puts(buffer_1, "\t"); - buffer_puts(buffer_1, pack.name); - buffer_puts(buffer_1, "/"); - buffer_puts(buffer_1, pack.ver); + buffer_put(buffer_1, prefix->x, prefix->n - 1); buffer_puts(buffer_1, "\n"); - buffer_init(&b, fd, &read, buf, sizeof buf); - while (stralloc_zero(&sa), (r = buffer_getline(buffer_, &sa))) + stralloc_init(&sa); + if (!stralloc_cats(&sa, def)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + if (!stralloc_cats(&sa, p->name)) goto err; + if (!stralloc_cats(&sa, "/")) goto err; + n = sa.n; + ver[0] = p->ver; ver[1] = "default"; + errno = 0; + for (size_t i = 0; i < 2 && fd == -1; ++i) { - pack p; - size_t n; - - if (sa.x[sa.n] == '\n') sa.x[--sa.n] = '\0'; - if (line[pack_scan(&p, sa.x)] != '\0') goto err; + sa.n = n; + if (!stralloc_cats(&sa, ver[i])) goto err; + if (!stralloc_cats(&sa, "/dependencies")) goto err; + if (!stralloc_cat0(&sa)) goto err; + fd = open_read(sa.x); + } + if (fd == -1) { ret = (errno == ENOENT); goto err; } + buffer_init(&b, &read, fd, buf, sizeof buf); + while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa))) + { + if (sa.x[sa.n - 1] == '\n') --sa.n; + if (!stralloc_cat0(&sa)) goto err; + if (sa.x[pack_scan(p, sa.x)] != '\0') goto err; n = prefix->n; - if (!stralloc_cats(prefix, "\t")) goto err; - if (!stralloc_cats(prefix, p.name)) goto err; - if (!stralloc_cats(prefix, "/")) goto err; - if (!stralloc_cats(prefix, p.ver)) goto err; - pack_tree(&p, prefix); + + if (!tree(p, prefix, def)) goto err; prefix->n = n; } if (r != 0) goto err; @@ -65,19 +72,22 @@ int main(int argc, char **argv) { char *def; - stralloc *sa; + stralloc sa; pack p; def = env_get("PACK_DEF"); if (!def) def = PACK_DEF; if (!fs_isdir(def)) log_diesys1(101, def); - if (argc != 2) log_usage(*argv, "package[/version]") + if (argc != 2) log_usage(*argv, "package[/version]"); + ++argv; - if (!pack_scan(&p, argv)) + if (!pack_scan(&p, *argv)) log_die2(1, "invalid package format: ", *argv); - if (!pack_tree(&p, &sa, def)) - log_diesys(100, "generating tree for ", p.name, "/", p.version); + stralloc_init(&sa); + if (!tree(&p, &sa, def)) + log_diesys4(100, "generating tree for ", p.name, "/", p.ver); + stralloc_free(&sa); buffer_flush(buffer_1); return 0; diff --git a/pack.c b/pack.c @@ -82,7 +82,7 @@ pack_version(pack *p, char const *def) stralloc_zero(&sa); buffer_init(&b, &read, fd, buf, sizeof buf); if (buffer_getline(&b, &sa) <= 0) goto err; - if (sa.x[sa.n - 1] == '\n') sa.x[--(sa.n)] = '\0'; + if (sa.x[sa.n - 1] == '\n') --sa.n; if (sa.n >= sizeof p->ver) goto err;