package

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

commit 0184aa26c1bb05d9d91ac219f1b5e05642964d3f
parent ebbe5de60058d7cd7f4c10d158c9f3c43156eeac
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sun, 20 Jan 2019 21:21:36 +0100

homogenize argument handling and loops implementation

Diffstat:
Mpackage-add.c | 15++++-----------
Mpackage-build.c | 8+++-----
Mpackage-dep.c | 10+++-------
Mpackage-get.c | 10+++-------
Mpackage-install.c | 6+-----
Mpackage-tree.c | 73++++++++++++++++++++++++++++++++++++++-----------------------------------
Mpackage.c | 50++++++++++++++++++++++----------------------------
7 files changed, 74 insertions(+), 98 deletions(-)

diff --git a/package-add.c b/package-add.c @@ -75,7 +75,6 @@ int main(int argc, char **argv) { genalloc ga; - size_t n; package p; char *pkg = PACKAGE_DIR; char *def = PACKAGE_DEF; @@ -93,20 +92,14 @@ main(int argc, char **argv) if (str_equal(*av, "-n")) { flag_n = 1; continue; } break; } - - if (!*av) log_usage(*argv, "[-p pkgdir] [-p pkgdir] [-d defdir] package[/version]"); - + if (!*av) log_usage(*argv, "[-p pkgdir] [-d defdir] [-r root] package[/version]"); if (!stat_isdir(def)) log_fatal_sys_1(101, def); if (!stat_isdir(pkg)) log_fatal_sys_1(101, pkg); - - n = package_scan(&p, *av, strlen(*av)); - if (n == 0 || (*av)[n] != '\0') - log_fatal_2(1, "invalid package name: ", *av); + if (**av == '\0' || (*av)[package_scan(&p, *av, strlen(*av))] != '\0') + log_fatal_sys_2(1, "invalid package format: ", *av); + if (!package_version(&p, def)) log_fatal_sys_2(101, "version ", p.name); p.dep_run = 1; - if (!package_version(&p, def)) - log_fatal_sys_3(101, "not read ", *av,"'s version"); - genalloc_init(&ga); if (!package_dep(&p, &ga, def)) log_fatal_sys_1(103, "could not resolve package dependencies"); diff --git a/package-build.c b/package-build.c @@ -166,22 +166,20 @@ main(int argc, char **argv) if (str_equal(*av, "-p")) { if (!(pkg = *++av)) break; continue; } break; } - if (!*av) log_usage(*argv, "[-p pkgdir] [-d defdir] package[/version]"); + if (**av == '\0' || (*av)[package_scan(&p, *av, strlen(*av))] != '\0') + log_fatal_sys_2(1, "invalid package format: ", *av); + if (!package_version(&p, def)) log_fatal_sys_2(101, "version ", p.name); tain_now(&tn); id[tain_fmt(&tn, id)] = '\0'; if (chdir(pkg) == -1) log_fatal_sys_2(101, "chdir ", pkg); - if ((*av)[package_scan(&p, *av, strlen(*av))] != '\0') - log_fatal_sys_2(1, "invalid package format: ", *av); if (mkdir(p.name, 075) == -1 && errno != EEXIST) log_fatal_sys_4(101, "mkdir ", pkg, "/", p.name); if (chdir(p.name) == -1) log_fatal_sys_4(101, "chdir ", pkg, "/", p.name); - if (!package_version(&p, def)) - log_fatal_sys_4(101, "version ", def, "/", p.name); if (mkdir(p.ver, 075) == -1 && errno != EEXIST) log_fatal_sys_6(101, "mkdir ", pkg, "/", p.name, "/", p.ver); if (chdir(p.ver) == -1) diff --git a/package-dep.c b/package-dep.c @@ -18,7 +18,6 @@ main(int argc, char **argv) char *def = PACKAGE_DEF; genalloc ga; size_t i; - size_t n; package p; char **av = argv; @@ -26,14 +25,11 @@ main(int argc, char **argv) if (str_equal(*av, "-d")) { if (!(def = *++av)) break; continue; } break; } - if (!*av) log_usage(*argv, "[-d defdir] package[/version]"); - if (!stat_isdir(def)) log_fatal_sys_1(101, def); - - n = package_scan(&p, *av, str_len(*av)); - if (n == 0 || (*av)[n] != '\0') - log_fatal_2(1, "invalid package name: ", *av); + if (**av == '\0' || (*av)[package_scan(&p, *av, strlen(*av))] != '\0') + log_fatal_sys_2(1, "invalid package format: ", *av); + if (!package_version(&p, def)) log_fatal_sys_2(101, "version ", p.name); if (!package_version(&p, def)) log_fatal_sys_3(103, "could not get ", *av," version"); diff --git a/package-get.c b/package-get.c @@ -33,7 +33,6 @@ main(int argc, char **argv) char **av = argv; int fd_uri = -1; int fd_log = -1; - size_t n; ssize_t r; (void)argc; @@ -43,15 +42,12 @@ main(int argc, char **argv) if (str_equal(*av, "-d")) { if (!(def = *++av)) break; continue; } break; } - if (!*av) log_usage(*argv, "package[/version]"); if (!stat_isdir(def)) log_fatal_sys_1(101, def); + if (**av == '\0' || (*av)[package_scan(&p, *av, strlen(*av))] != '\0') + log_fatal_sys_2(1, "invalid package format: ", *av); + if (!package_version(&p, def)) log_fatal_sys_2(101, "version ", p.name); - n = package_scan(&p, *av, strlen(*av)); - if ((*av)[n] != '\0') - log_fatal_2(1, "invalid package format: ", *argv); - if (!package_version(&p, def)) - log_fatal_sys_2(101, "package_version ", p.name); if (!package_env_dep(&p, def, pkg)) log_fatal_sys_1(100, "setting dependencies environment variables"); diff --git a/package-install.c b/package-install.c @@ -31,15 +31,11 @@ main(int argc, char **argv) if (str_equal(*av, "-r")) { if (!(root = *++av)) break; continue; } break; } - if (!*av) log_usage(*argv, "[-p pkgdir] [-d defdir] [-r rootdir] package[/version]"); - if (!stat_isdir(def)) log_fatal_sys_1(101, def); if (!stat_isdir(root)) log_fatal_sys_1(101, root); - - if (*av[0] == '\0' || (*av)[package_scan(&p, *av, strlen(*av))] != '\0') + if (**av == '\0' || (*av)[package_scan(&p, *av, strlen(*av))] != '\0') log_fatal_sys_2(1, "invalid package format: ", *av); - if (!package_version(&p, def)) log_fatal_sys_2(101, "version ", p.name); stralloc_init(&sa_pkg); diff --git a/package-tree.c b/package-tree.c @@ -16,8 +16,7 @@ tree(package *p, stralloc *prefix, char const *def) buffer b; char buf[1024]; ssize_t r; - int fd = -1; - char *ver[2]; + int fd[] = { -1, -1 }; size_t n; int ret = 0; @@ -26,46 +25,45 @@ tree(package *p, stralloc *prefix, char const *def) if (!stralloc_cats(prefix, " - ")) goto error; if (!stralloc_cats(prefix, p->ver)) goto error; if (!stralloc_cats(prefix, "/")) goto error; + buffer_puts(buffer_1, p->dep_download ? "d" : "-"); + buffer_puts(buffer_1, p->dep_build ? "b" : "-"); + buffer_puts(buffer_1, p->dep_lib ? "l" : "-"); + buffer_puts(buffer_1, p->dep_run ? "r" : "-"); buffer_puts(buffer_1, "\t"); buffer_put(buffer_1, prefix->x, prefix->n - 1); buffer_puts(buffer_1, "\n"); - stralloc_init(&sa); - if (!stralloc_cats(&sa, def)) goto error; - if (!stralloc_cats(&sa, "/")) goto error; - if (!stralloc_cats(&sa, p->name)) goto error; - if (!stralloc_cats(&sa, "/")) goto error; - n = sa.n; - ver[0] = p->ver; - ver[1] = "default"; errno = 0; - for (size_t i = 0; i < 2 && fd == -1; ++i) - { - sa.n = n; - if (!stralloc_cats(&sa, ver[i])) goto error; - if (!stralloc_cats(&sa, "/dependencies")) goto error; - if (!stralloc_cat0(&sa)) goto error; - fd = open_read(sa.x); - } - if (fd == -1) { ret = (errno == ENOENT); goto error; } + fd[0] = package_dep_open_default(p, def); + if (fd[0] == -1 && errno != ENOTDIR && errno != ENOENT) goto error; + fd[1] = package_dep_open_version(p, def); + if (fd[1] == -1 && errno != ENOTDIR && errno != ENOENT) goto error; - buffer_init(&b, &read, fd, buf, sizeof buf); - while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa))) + for (int i = 0; i < 2; ++i) { - if (sa.x[sa.n - 1] == '\n') --sa.n; - if (!stralloc_cat0(&sa)) goto error; - if (sa.x[package_dep_scan(p, sa.x, sa.n)] != '\0') goto error; - n = prefix->n; + if (fd[i] == -1) continue; + buffer_init(&b, &read, fd[i], buf, sizeof buf); + while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa)) > 0) + { + package p; + + sa.n -= (sa.x[sa.n - 1] == '\n'); + if (!stralloc_cat0(&sa)) goto error; + if (sa.x[package_dep_scan(&p, sa.x, sa.n)] != '\0') goto error; + if (!package_version(&p, def)) goto error; - if (!tree(p, prefix, def)) goto error; - prefix->n = n; + n = prefix->n; + if (!tree(&p, prefix, def)) goto error; + prefix->n = n; + } + if (r != 0) goto error; } - if (r != 0) goto error; ret = 1; error: stralloc_free(&sa); - if (fd >= 0) close (fd); + if (fd[0] >= 0) close (fd[0]); + if (fd[1] >= 0) close (fd[1]); return ret; } @@ -75,14 +73,19 @@ main(int argc, char **argv) char *def = PACKAGE_DEF; stralloc sa; package p; + char **av = argv; - if (!stat_isdir(def)) log_fatal_sys_1(101, def); - - if (argc != 2) log_usage(*argv, "package[/version]"); - ++argv; + (void)argc; - if (!package_scan(&p, *argv, strlen(*argv))) - log_fatal_2(1, "invalid package format: ", *argv); + while (*++av) { + if (str_equal(*av, "-d")) { if (!(def = *++av)) break; continue; } + break; + } + if (!*av) log_usage(*argv, "[-d defdir] package[/version]"); + if (!stat_isdir(def)) log_fatal_sys_1(101, def); + if (**av == '\0' || (*av)[package_scan(&p, *av, strlen(*av))] != '\0') + log_fatal_sys_2(1, "invalid package format: ", *av); + p.dep_run = 1; stralloc_init(&sa); if (!tree(&p, &sa, def)) diff --git a/package.c b/package.c @@ -96,11 +96,9 @@ int package_env_dep(package *p, char const *def, char const *pkg) { stralloc sa = STRALLOC_ZERO; - stralloc line = STRALLOC_ZERO; buffer b; char buf[1024]; size_t r; - size_t n; int fd[2] = { -1, -1 }; int ret = 0; @@ -113,27 +111,23 @@ package_env_dep(package *p, char const *def, char const *pkg) for (int i = 0; i < 2; ++i) { if (fd[i] == -1) continue; - - stralloc_init(&line); buffer_init(&b, &read, fd[i], buf, sizeof buf); - while (stralloc_zero(&line), (r = buffer_getline(&b, &line)) > 0) + while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa)) > 0) { - package new; - - line.n -= (line.x[line.n - 1] == '\n'); - if (!stralloc_cat0(&line)) goto error; - n = package_dep_scan(&new, line.x, line.n); - if (n == 0 || line.x[n] != '\0') goto error; - if (!package_version(&new, def)) goto error; - if (!package_env_dep_l(&new, pkg)) goto error; - if (!package_env_dep_bd(&new, pkg)) goto error; + package p; + + sa.n -= (sa.x[sa.n - 1] == '\n'); + if (!stralloc_cat0(&sa)) goto error; + if (sa.x[package_dep_scan(&p, sa.x, sa.n)] != '\0') goto error; + if (!package_version(&p, def)) goto error; + if (!package_env_dep_l(&p, pkg)) goto error; + if (!package_env_dep_bd(&p, pkg)) goto error; } if (r != 0) goto error; } ret = 1; error: stralloc_free(&sa); - stralloc_free(&line); if (fd[0] >= 0) close(fd[0]); if (fd[1] >= 0) close(fd[1]); return ret; @@ -314,10 +308,10 @@ package_dep(package const *p, genalloc *packages, char const *def) int ret = 0; errno = 0; - fd[0] = package_dep_open_version(p, def); - if (errno != 0 && errno != ENOTDIR && errno != ENOENT) goto error; - fd[1] = package_dep_open_default(p, def); - if (errno != 0 && errno != ENOTDIR && errno != ENOENT) goto error; + fd[0] = package_dep_open_default(p, def); + if (fd[0] == -1 && errno != ENOTDIR && errno != ENOENT) goto error; + fd[1] = package_dep_open_version(p, def); + if (fd[1] == -1 && errno != ENOTDIR && errno != ENOENT) goto error; for (int i = 0; i < 2; ++i) { @@ -327,15 +321,15 @@ package_dep(package const *p, genalloc *packages, char const *def) stralloc_init(&sa); while (stralloc_zero(&sa), (r = buffer_getline(&b, &sa)) > 0) { - package new; - size_t n; - - n = package_dep_scan(&new, sa.x, sa.n); - if (sa.x[n] != '\n' && sa.n != n) goto error; - if (!package_version(&new, def)) goto error; - if (package_hasdep(&new, packages)) continue; - if (!package_dep(&new, packages, def)) goto error; - if (!genalloc_append(packages, new)) goto error; + package p; + + sa.n -= (sa.x[sa.n - 1] == '\n'); + if (!stralloc_cat0(&sa)) goto error; + if (sa.x[package_dep_scan(&p, sa.x, sa.n)] != '\0') goto error; + if (!package_version(&p, def)) goto error; + if (package_hasdep(&p, packages)) continue; + if (!package_dep(&p, packages, def)) goto error; + if (!genalloc_append(packages, p)) goto error; } if (r != 0) goto error; }