From: Nathan Wagner Date: Fri, 31 Mar 2017 05:04:38 +0000 (-0500) Subject: add zpm-findpkg X-Git-Tag: v0.1.6~130 X-Git-Url: https://pd.if.org/git/?p=zpackage;a=commitdiff_plain;h=324303554d33d5a3643fa73ec53c5c41e71cd54a add zpm-findpkg --- diff --git a/Makefile b/Makefile index 5352872..e31ae2e 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,9 @@ LZMAOBJ=$(filter-out lzma/common/stream_encoder_mt.o, $(LZMASRC:%.c=%.o)) curdir=$(shell pwd) -ZPKGBIN=zpm-addfile zpm-extract zpm-init zpm-vercmp zpm-stat zpm-hash +ZPKGBIN=zpm-addfile zpm-extract zpm-init zpm-vercmp zpm-stat zpm-hash \ + zpm-findpkg + def: programs d: printf '%s\n' $(LZMAOBJ) @@ -28,7 +30,8 @@ stest: $(ZPKGBIN) test: $(ZPKGBIN) PATH=$(curdir)/t:$(curdir):$(PATH) prove -e '' t/*.t -programs: elftype soname zpm-soneed zpm-addfile zpm-extract zpm-init zpm-vercmp +programs: elftype soname zpm-soneed zpm-addfile zpm-extract zpm-init \ + zpm-vercmp zpm-findpkg uncompress: uncompress.o $(CC) $(CFLAGS) -o $@ $+ -llzma @@ -54,6 +57,9 @@ zpm-init: zpm-init.o libzpm.a zpm-extract: zpm-extract.o libzpm.a $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -lzpm -lelf +zpm-findpkg: zpm-findpkg.o libzpm.a + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< -lzpm -lelf + newdb.c: db.sql echo "char createdb[] = {" > $@ xxd -i < $< >> $@ diff --git a/zpm-findpkg.c b/zpm-findpkg.c new file mode 100644 index 0000000..68baeca --- /dev/null +++ b/zpm-findpkg.c @@ -0,0 +1,61 @@ +#include +#include +#include "zpm.h" + +static int prow(void *f, int ncols, char **vals, char **cols) { + FILE *out = f; + int i; + + cols = 0; /* suppress warning */ + for (i=3;i3) fprintf(out, "\t"); + fprintf(out, "%s", vals[i]); + } + fprintf(out, "\n"); + return 0; +} + +int main(int ac, char **av){ + struct zpm pkg; + char *select = "select package, version, release, package||'-'||version||'-'||release as pkgid from packages"; + char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp"; + +// char *select = "select package, version, release, package||'-'||version||'-'||release as pkgid from packages"; +// char *order = "order by package, version collate vercmp, cast(release as integer)"; + char sql[2048]; + + if (ac < 2) { + fprintf(stderr, "usage: db path\n"); + return 1; + } + + /* this is really just read env */ + zpm_readopts(&pkg, ac, av); + + /* given a package name, get the packages */ + /* no package name, get all */ + + if (zpm_open(&pkg, av[1])) { + char *errmsg; +// char where[1024] = ""; + + /* TODO allow more args to nail down version and release */ + if (ac >= 3) { + sprintf(sql, "%s where package = '%s' %s;", + select, av[2], group); + } else { + sprintf(sql, "%s %s;", select, group); + } + + /* install a collation function */ + zpm_addvercmp(&pkg); + /* sqlite seems to need the columns in the result to + * do the sort right */ +// zpm_exec(&pkg, "select package, version, release, package||'-'||version||'-'||release as pkgid from packages order by package, version collate vercmp, cast(release as integer)", prow, stdout, &errmsg); +// fprintf(stdout, "\n"); + + zpm_exec(&pkg, sql, prow, stdout, &errmsg); + } + zpm_close(&pkg); + return 0; +}