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 == '\'' || !isgraph(*pstr)) {
36 if (*pstr == '-' && isdigit(*(pstr+1))) {
49 while (*pstr && *pstr != '-') {
50 if (*pstr == '\'' || !isgraph(*pstr)) {
69 return havename + havever + haverel;
73 * flags 0 = find installed
74 * flags 1 = skip installed
75 * flags 2 = skip not installed
78 struct zpm_package *zpm_package_alloc(struct zpm *zpm) {
79 struct zpm_package *pkg;
81 pkg = malloc(sizeof *pkg);
89 void zpm_sqlite_error(struct zpm *zpm) {
91 if (zpm->errmsg) free(zpm->errmsg);
92 zpm->errmsg = strdup((const char *)sqlite3_errmsg(zpm->db));
95 char *zpm_findpkg(struct zpm *zpm, char *pkgstr) {
96 char *select = "select pkgid, package, version, release from packages_pkgid";
97 char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp limit 1";
100 "status = 'installed'",
102 "status != 'installed'"
106 // char *order = "order by package, version collate vercmp, cast(release as integer)";
114 /* null pkgstr find "best" package
115 * best is shortest complete package if any are complete
116 * shortest incomplete if any are incomplete
117 * where more than one version is in those sets, best is
118 * latest as determined by vercmp
121 /* given a package name, get the packages */
122 /* no package name, get all */
124 // char where[1024] = "";
126 zpm_parse_package(pkgstr, package, version, &release);
128 /* install a collation function */
131 /* TODO allow more args to nail down version and release */
133 sql = sqlite3_str_new(zpm->db);
134 sqlite3_str_appendall(sql, select);
135 sqlite3_str_appendf(sql, " where package = %q", package);
137 sqlite3_str_appendf(sql, " and version = %q", version);
140 sqlite3_str_appendf(sql, " and release = %d", release);
142 sqlite3_str_appendf(sql, " %s", group);
143 sqlite3_str_appendf(sql, " limit 1");
145 if (sqlite3_str_errcode(sql)) {
147 sqlite3_free(sqlite3_str_finish(sql));
151 query = sqlite3_str_finish(sql);
152 sqlite3_prepare_v2(zpm->db, query, strlen(query), &stmt, NULL);
158 switch (sqlite3_step(stmt)) {
160 zpm->pkgid = strdup((const char *)sqlite3_column_text(stmt, 0));
166 zpm_sqlite_error(zpm);
169 sqlite3_finalize(stmt);