]> pd.if.org Git - zpackage/blob - src/findpkg.c
remove stray debug fprintf
[zpackage] / src / findpkg.c
1 #define _POSIX_C_SOURCE 2 
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <ctype.h>
6 #include <unistd.h>
7
8 #include "zpm.h"
9
10 void usage(void) {
11         fprintf(stderr, "zpm-findpkg [-I] [-s <status> ...] [-S <status>] [package]\n");
12 }
13
14 int main(int ac, char **av){
15         int opt, argn;
16         struct zpm zpm;
17         char *dbfile = 0, *pkgstr = 0, *pkgid = 0;
18         int range = 0, wantleast = 0, argismax = 0;
19         char *minpkg = 0, *maxpkg = 0;
20
21         char *sql;
22         sqlite3_str *include;
23         sqlite3_str *exclude;
24         sqlite3_str *query;
25
26         dbfile = getenv("ZPMDB");
27         if (!dbfile) {
28                 dbfile = "/var/lib/zpm/local.db";
29         }
30
31         include = sqlite3_str_new(NULL);
32         exclude = sqlite3_str_new(NULL);
33
34         while ((opt = getopt(ac, av, "f:s:S:Imrl")) != -1) {
35                 switch (opt) {
36                         case 'f': dbfile = optarg; break;
37                         case 's': sqlite3_str_appendf(include,",%Q", optarg);
38                                   break;
39                         case 'S': sqlite3_str_appendf(exclude,",%Q", optarg);
40                                   break;
41                         case 'I': sqlite3_str_appendall(include,",'installed'");
42                                   break;
43                         case 'l': wantleast = 1; break;
44                         case 'r': range = 1; break;
45                         case 'm': argismax = 1; range = 1; break;
46                         default:
47                                   usage();
48                                   exit(EXIT_FAILURE);
49                                   break;
50                 }
51         }
52         argn = optind;
53
54         if (ac < argn) {
55                 usage();
56                 exit(EXIT_FAILURE);
57         }
58
59         pkgstr = minpkg = av[argn];
60         if (ac > argn) {
61                 maxpkg = av[argn+1];
62         }
63
64         if (maxpkg) {
65                 range = 1;
66         }
67
68         if (!dbfile) {
69                 fprintf(stderr, "must specify db\n");
70                 return 1;
71         }
72
73         query = sqlite3_str_new(NULL);
74
75         if (zpm_open(&zpm, dbfile)) {
76                 char *excludes, *includes;
77
78                 excludes = sqlite3_str_value(exclude);
79                 includes = sqlite3_str_value(include);
80
81                 if (includes) {
82                         sqlite3_str_appendf(query, "status in (%s)", includes+1);
83                 }
84                 if (excludes) {
85                         sqlite3_str_appendf(query, "%sstatus not in (%s)",
86                                        includes ? " and " : "",
87                                        excludes+1);
88                 }
89
90                 sql = sqlite3_str_value(query);
91
92                 if (range) {
93                         if (argismax) {
94                                 /* swap the sense of the arguments */
95                                 minpkg = maxpkg;
96                                 maxpkg = pkgstr;
97                         }
98                         pkgid = zpm_findpkg_range(&zpm, minpkg, maxpkg, sql, wantleast);
99                 } else {
100                         pkgid = zpm_findpkg(&zpm, pkgstr, sql);
101                 }
102
103                 sqlite3_free(sql);
104                 if (pkgid) {
105                         printf("%s\n", pkgid);
106                 }
107                 zpm_close(&zpm);
108         }
109         return pkgid ? 0 : 1;
110 }