X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=zpm-findpkg.c;h=95085d5590f3516ba21911189a2f3c5696cd40a8;hb=119f7176a2cedfe3c87417a35d91ec3b35b582b1;hp=b7cb3fdb9272241af642003ad945d28b7632bc0c;hpb=84f276b21e57aa8fe1e16d01e75fee1097d5daf8;p=zpackage diff --git a/zpm-findpkg.c b/zpm-findpkg.c index b7cb3fd..95085d5 100644 --- a/zpm-findpkg.c +++ b/zpm-findpkg.c @@ -1,7 +1,11 @@ #include #include +#include + #include "zpm.h" +static int found = 0; + static int prow(void *f, int ncols, char **vals, char **cols) { FILE *out = f; int i; @@ -14,11 +18,58 @@ static int prow(void *f, int ncols, char **vals, char **cols) { fprintf(out, "%s", vals[i]); } fprintf(out, "\n"); + found++; 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 *dbfile, *pkgstr; 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"; @@ -31,33 +82,47 @@ int main(int ac, char **av){ return 1; } - /* this is really just read env */ - zpm_readopts(&pkg, ac, av); + dbfile = getenv("ZPMDB"); + + if (ac > 1) { + dbfile = av[1]; + } /* given a package name, get the packages */ /* no package name, get all */ - if (zpm_open(&pkg, av[1])) { + if (zpm_open(&pkg, dbfile)) { char *errmsg; -// char where[1024] = ""; - /* TODO allow more args to nail down version and release */ - if (ac >= 3) { + if (ac >= 2) { + int release; + char version[32]; + char package[32]; + + pkgstr = av[2]; + + parse_package(pkgstr, 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); } - /* 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; + return found ? 0 : 1; }