1 #define _POSIX_C_SOURCE 200809L
12 #define DMARK fprintf(stderr, "mark %s %s:%d\n", __FILE__, __func__, __LINE__)
14 int zpm_parse_package(char *pstr, char *name, char *ver, int *rel) {
22 /* string - ver - rel */
23 /* rel is all digits */
25 * ^(.+)-([0-9][^-]*)-([\d+])$
28 * The main problem in parsing is that the package name itself
29 * can contain a '-', so you can't just split on '-'
30 * Also, the version can be just digits.
33 /* everything up to the first '-' is in the name */
35 if (*pstr == '\'' || !isgraph(*pstr)) {
38 if (*pstr == '-' && isdigit(*(pstr+1))) {
51 while (*pstr && *pstr != '-') {
52 if (*pstr == '\'' || !isgraph(*pstr)) {
71 return havename + havever + haverel;
75 * flags 0 = find installed
76 * flags 1 = skip installed
77 * flags 2 = skip not installed
80 struct zpm_package *zpm_package_alloc(struct zpm *zpm) {
81 struct zpm_package *pkg;
83 pkg = malloc(sizeof *pkg);
91 void zpm_sqlite_error(struct zpm *zpm) {
93 if (zpm->errmsg) free(zpm->errmsg);
94 zpm->errmsg = strdup((const char *)sqlite3_errmsg(zpm->db));
97 char *zpm_findpkg(struct zpm *zpm, char *pkgstr) {
98 char *select = "select pkgid, package, version, release from packages_pkgid";
99 char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp";
102 "status = 'installed'",
104 "status != 'installed'"
108 // char *order = "order by package, version collate vercmp, cast(release as integer)";
111 char *query, *pkgid = 0;
116 if (pkgstr == NULL) {
117 fprintf(stderr, "trying to parse null string\n");
121 /* null pkgstr find "best" package
122 * best is shortest complete package if any are complete
123 * shortest incomplete if any are incomplete
124 * where more than one version is in those sets, best is
125 * latest as determined by vercmp
128 /* given a package name, get the packages */
129 /* no package name, get all */
131 // char where[1024] = "";
133 zpm_parse_package(pkgstr, package, version, &release);
135 /* install a collation function */
138 /* TODO allow more args to nail down version and release */
140 sql = sqlite3_str_new(zpm->db);
141 sqlite3_str_appendall(sql, select);
142 sqlite3_str_appendf(sql, " where package = %Q", package);
144 sqlite3_str_appendf(sql, " and version = %Q", version);
147 sqlite3_str_appendf(sql, " and release = %d", release);
149 sqlite3_str_appendf(sql, " %s", group);
150 sqlite3_str_appendf(sql, " limit 1");
152 if (sqlite3_str_errcode(sql)) {
154 sqlite3_free(sqlite3_str_finish(sql));
158 query = sqlite3_str_finish(sql);
159 sqlite3_prepare_v2(zpm->db, query, strlen(query), &stmt, NULL);
169 switch (sqlite3_step(stmt)) {
171 pkgid = strdup((const char *)sqlite3_column_text(stmt, 0));
177 zpm_sqlite_error(zpm);
180 sqlite3_finalize(stmt);