1 #define _POSIX_C_SOURCE 200809L
12 int zpm_parse_package(char *pstr, char *name, char *ver, int *rel) {
20 /* string - ver - rel */
21 /* rel is all digits */
23 * ^(.+)-([0-9][^-]*)-([\d+])$
26 * The main problem in parsing is that the package name itself
27 * can contain a '-', so you can't just split on '-'
28 * Also, the version can be just digits.
31 /* everything up to the first '-' is in the name */
33 if (*pstr == '-' && isdigit(*(pstr+1))) {
46 while (*pstr && *pstr != '-') {
63 return havename + havever + haverel;
67 * flags 0 = find installed
68 * flags 1 = skip installed
69 * flags 2 = skip not installed
72 struct zpm_package *zpm_package_alloc(struct zpm *zpm) {
73 struct zpm_package *pkg;
75 pkg = malloc(sizeof *pkg);
83 void zpm_sqlite_error(struct zpm *zpm) {
85 if (zpm->errmsg) free(zpm->errmsg);
86 zpm->errmsg = strdup((const char *)sqlite3_errmsg(zpm->db));
89 char *zpm_findpkg(struct zpm *zpm, char *pkgstr) {
90 char *select = "select pkgid, package, version, release from packages_pkgid";
91 char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp limit 1";
94 "status = 'installed'",
96 "status != 'installed'"
100 // char *order = "order by package, version collate vercmp, cast(release as integer)";
108 /* null pkgstr find "best" package
109 * best is shortest complete package if any are complete
110 * shortest incomplete if any are incomplete
111 * where more than one version is in those sets, best is
112 * latest as determined by vercmp
115 /* given a package name, get the packages */
116 /* no package name, get all */
118 // char where[1024] = "";
120 zpm_parse_package(pkgstr, package, version, &release);
122 /* install a collation function */
125 /* TODO allow more args to nail down version and release */
127 sql = sqlite3_str_new(zpm->db);
128 sqlite3_str_appendall(sql, select);
129 sqlite3_str_appendf(sql, " where package = %q", package);
131 sqlite3_str_appendf(sql, " and version = %q", version);
134 sqlite3_str_appendf(sql, " and release = %d", release);
136 sqlite3_str_appendf(sql, " %s", group);
137 sqlite3_str_appendf(sql, " limit 1");
139 if (sqlite3_str_errcode(sql)) {
141 sqlite3_free(sqlite3_str_finish(sql));
145 query = sqlite3_str_finish(sql);
146 sqlite3_prepare_v2(zpm->db, query, strlen(query), &stmt, NULL);
152 switch (sqlite3_step(stmt)) {
154 zpm->pkgid = strdup((const char *)sqlite3_column_text(stmt, 0));
160 zpm_sqlite_error(zpm);
163 sqlite3_finalize(stmt);