X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=lib%2Ffindpkg.c;h=072f9f1cbd21745259eeb1c0b59e412424bd490a;hb=af49b10669467685a03ca860dd9d7c5fc3a590b5;hp=dec9283e1201c571282eff2ebec82d9a363c843a;hpb=ca85ec82110205f83cbaadae6433ff199d49c625;p=zpackage diff --git a/lib/findpkg.c b/lib/findpkg.c index dec9283..072f9f1 100644 --- a/lib/findpkg.c +++ b/lib/findpkg.c @@ -9,59 +9,7 @@ #include "sqlite3.h" -int zpm_parse_package(char *pstr, char *name, char *ver, int *rel) { - if (name) *name = 0; - if (ver) *ver = 0; - if (rel) *rel = 0; - int havename = 0; - int havever = 0; - int haverel = 0; - - /* 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++; - havename = 1; - } - if (name) *name = 0; - if (*pstr == '-') { - pstr++; - } - while (*pstr && *pstr != '-') { - if (ver) { - *ver++ = *pstr; - } - pstr++; - havever = 1; - } - if (ver) *ver = 0; - if (*pstr == '-') { - pstr++; - } - /* TODO use strtol */ - if (rel && *pstr) { - haverel = 1; - *rel = atoi(pstr); - } - - return havename + havever + haverel; -} +#define DMARK fprintf(stderr, "mark %s %s:%d\n", __FILE__, __func__, __LINE__) /* * flags 0 = find installed @@ -88,7 +36,7 @@ void zpm_sqlite_error(struct zpm *zpm) { char *zpm_findpkg(struct zpm *zpm, char *pkgstr) { char *select = "select pkgid, package, version, release from packages_pkgid"; - char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp limit 1"; + char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp"; #if 0 char *sstr[] = { "status = 'installed'", @@ -100,11 +48,16 @@ char *zpm_findpkg(struct zpm *zpm, char *pkgstr) { // char *order = "order by package, version collate vercmp, cast(release as integer)"; sqlite3_str *sql; sqlite3_stmt *stmt; - char *query; + char *query, *pkgid = 0; char package[32]; char version[32]; int release; + if (pkgstr == NULL) { + fprintf(stderr, "trying to parse null string\n"); + return NULL; + } + /* null pkgstr find "best" package * best is shortest complete package if any are complete * shortest incomplete if any are incomplete @@ -126,9 +79,9 @@ char *zpm_findpkg(struct zpm *zpm, char *pkgstr) { sql = sqlite3_str_new(zpm->db); sqlite3_str_appendall(sql, select); - sqlite3_str_appendf(sql, " where package = %q", package); + sqlite3_str_appendf(sql, " where package = %Q", package); if (*version) { - sqlite3_str_appendf(sql, " and version = %q", version); + sqlite3_str_appendf(sql, " and version = %Q", version); } if (release) { sqlite3_str_appendf(sql, " and release = %d", release); @@ -146,12 +99,16 @@ char *zpm_findpkg(struct zpm *zpm, char *pkgstr) { sqlite3_prepare_v2(zpm->db, query, strlen(query), &stmt, NULL); sqlite3_free(query); - free(zpm->pkgid); +#if 0 + if (zpm->pkgid) { + free(zpm->pkgid); + } zpm->pkgid = 0; +#endif switch (sqlite3_step(stmt)) { case SQLITE_ROW: - zpm->pkgid = strdup((const char *)sqlite3_column_text(stmt, 0)); + pkgid = strdup((const char *)sqlite3_column_text(stmt, 0)); break; case SQLITE_DONE: /* not found */ @@ -162,5 +119,5 @@ char *zpm_findpkg(struct zpm *zpm, char *pkgstr) { } sqlite3_finalize(stmt); - return zpm->pkgid; + return pkgid; }