]> pd.if.org Git - zpackage/commitdiff
add zpm-findpkg
authorNathan Wagner <nw@hydaspes.if.org>
Fri, 31 Mar 2017 05:04:38 +0000 (00:04 -0500)
committerNathan Wagner <nw@hydaspes.if.org>
Fri, 31 Mar 2017 05:05:17 +0000 (00:05 -0500)
Makefile
zpm-findpkg.c [new file with mode: 0644]

index 5352872d3d5097a61c08b10194592452d4426bb4..e31ae2e35a550d1f3b067af317b9adb16ed3c9ec 100644 (file)
--- 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 (file)
index 0000000..68baeca
--- /dev/null
@@ -0,0 +1,61 @@
+#include <stdlib.h>
+#include <stdio.h>
+#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;i<ncols;i++) {
+               if (i>3) 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;
+}