]> pd.if.org Git - zpackage/commitdiff
teach findpkg to parse full or partial package triples
authorNathan Wagner <nw@hydaspes.if.org>
Sat, 4 Aug 2018 01:02:08 +0000 (01:02 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Sat, 4 Aug 2018 01:02:08 +0000 (01:02 +0000)
zpm-findpkg.c

index b7cb3fdb9272241af642003ad945d28b7632bc0c..c3ce4e4ceb80fb217373ca10b7abf32ca94071c9 100644 (file)
@@ -1,5 +1,7 @@
 #include <stdlib.h>
 #include <stdio.h>
+#include <ctype.h>
+
 #include "zpm.h"
 
 static int prow(void *f, int ncols, char **vals, char **cols) {
@@ -17,6 +19,51 @@ static int prow(void *f, int ncols, char **vals, char **cols) {
        return 0;
 }
 
+void parse_package(char *pstr, char *name, char *ver, int *rel) {
+       if (name) *name = 0;
+       if (ver) *ver = 0;
+       if (rel) *rel = -1;
+
+       /* string - ver - rel */
+       /* rel is all digits */
+       /* possible forms:
+        * ^(.+)-([0-9][^-]*)-([\d+])$
+        * ^(.+)-([0-9][^-]*)$
+        * ^(.+)$
+        * The main problem in parsing is that the package name itself
+        * can contain a '-', so you can't just split on '-'
+        * Also, the version can be just digits.
+        */
+
+       /* everything up to the first '-' is in the name */
+       while (*pstr) {
+               if (*pstr == '-' && isdigit(*(pstr+1))) {
+                       break;
+               }
+               if (name) {
+                       *name++ = *pstr;
+               }
+               pstr++;
+       }
+       if (name) *name = 0;
+       if (*pstr == '-') {
+               pstr++;
+       }
+       while (*pstr && *pstr != '-') {
+               if (ver) {
+                       *ver++ = *pstr;
+               }
+               pstr++;
+       }
+       if (ver) *ver = 0;
+       if (*pstr == '-') {
+               pstr++;
+       }
+       if (rel && *pstr) {
+               *rel = atoi(pstr);
+       }
+}
+
 int main(int ac, char **av){
        struct zpm pkg;
        char *select = "select package, version, release, package||'-'||version||'-'||release as pkgid from packages";
@@ -43,8 +90,26 @@ int main(int ac, char **av){
 
                /* TODO allow more args to nail down version and release */
                if (ac >= 3) {
+                       int release;
+                       char version[32];
+                       char package[32];
+
+                       parse_package(av[2], package, version, &release);
+                       if (release != -1) {
+                               /* all three */
+                       sprintf(sql, "%s where package = '%s' and version = "
+                                       "'%s' and release = %d %s;", select,
+                                       package, version, release, group);
+                       } else if (*version != 0) {
+                       sprintf(sql, "%s where package = '%s' and version = "
+                                       "'%s' %s;", select,
+                                       package, version, group);
+
+                       } else {
+
                        sprintf(sql, "%s where package = '%s' %s;",
                                        select, av[2], group);
+                       }
                } else {
                        sprintf(sql, "%s %s;", select, group);
                }