]> pd.if.org Git - zpackage/blob - zpm-findpkg.c
c3ce4e4ceb80fb217373ca10b7abf32ca94071c9
[zpackage] / zpm-findpkg.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <ctype.h>
4
5 #include "zpm.h"
6
7 static int prow(void *f, int ncols, char **vals, char **cols) {
8         FILE *out = f;
9         int i;
10
11         if (cols == 0) {
12                 fprintf(stderr, "sqlite can't get column names\n");
13         }
14         for (i=3;i<ncols;i++) {
15                 if (i>3) fprintf(out, "\t");
16                 fprintf(out, "%s", vals[i]);
17         }
18         fprintf(out, "\n");
19         return 0;
20 }
21
22 void parse_package(char *pstr, char *name, char *ver, int *rel) {
23         if (name) *name = 0;
24         if (ver) *ver = 0;
25         if (rel) *rel = -1;
26
27         /* string - ver - rel */
28         /* rel is all digits */
29         /* possible forms:
30          * ^(.+)-([0-9][^-]*)-([\d+])$
31          * ^(.+)-([0-9][^-]*)$
32          * ^(.+)$
33          * The main problem in parsing is that the package name itself
34          * can contain a '-', so you can't just split on '-'
35          * Also, the version can be just digits.
36          */
37
38         /* everything up to the first '-' is in the name */
39         while (*pstr) {
40                 if (*pstr == '-' && isdigit(*(pstr+1))) {
41                         break;
42                 }
43                 if (name) {
44                         *name++ = *pstr;
45                 }
46                 pstr++;
47         }
48         if (name) *name = 0;
49         if (*pstr == '-') {
50                 pstr++;
51         }
52         while (*pstr && *pstr != '-') {
53                 if (ver) {
54                         *ver++ = *pstr;
55                 }
56                 pstr++;
57         }
58         if (ver) *ver = 0;
59         if (*pstr == '-') {
60                 pstr++;
61         }
62         if (rel && *pstr) {
63                 *rel = atoi(pstr);
64         }
65 }
66
67 int main(int ac, char **av){
68         struct zpm pkg;
69         char *select = "select package, version, release, package||'-'||version||'-'||release as pkgid from packages";
70         char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp";
71
72 //      char *select = "select package, version, release, package||'-'||version||'-'||release as pkgid from packages";
73 //      char *order = "order by package, version collate vercmp, cast(release as integer)";
74         char sql[2048];
75
76         if (ac < 2) {
77                 fprintf(stderr, "usage: db path\n");
78                 return 1;
79         }
80
81         /* this is really just read env */
82         zpm_readopts(&pkg, ac, av);
83
84         /* given a package name, get the packages */
85         /* no package name, get all */
86
87         if (zpm_open(&pkg, av[1])) {
88                 char *errmsg;
89 //              char where[1024] = "";
90
91                 /* TODO allow more args to nail down version and release */
92                 if (ac >= 3) {
93                         int release;
94                         char version[32];
95                         char package[32];
96
97                         parse_package(av[2], package, version, &release);
98                         if (release != -1) {
99                                 /* all three */
100                         sprintf(sql, "%s where package = '%s' and version = "
101                                         "'%s' and release = %d %s;", select,
102                                         package, version, release, group);
103                         } else if (*version != 0) {
104                         sprintf(sql, "%s where package = '%s' and version = "
105                                         "'%s' %s;", select,
106                                         package, version, group);
107
108                         } else {
109
110                         sprintf(sql, "%s where package = '%s' %s;",
111                                         select, av[2], group);
112                         }
113                 } else {
114                         sprintf(sql, "%s %s;", select, group);
115                 }
116
117                 /* install a collation function */
118                 zpm_addvercmp(&pkg);
119                 /* sqlite seems to need the columns in the result to
120                  * do the sort right */
121 //              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);
122 //              fprintf(stdout, "\n");  
123
124                 zpm_exec(&pkg, sql, prow, stdout, &errmsg);
125         }
126         zpm_close(&pkg);
127         return 0;
128 }