Werner's Miscellanea
Sign in or create your account | Project List | Help
Werner's Miscellanea Git Source Tree
Root/
Source at commit 6a3e471304b939fa920adcf6f6a9ac31b22fd976 created 13 years 4 months ago. By Werner Almesberger, qpkg: added some field names and relational operators from Ubuntu | |
---|---|
1 | /* |
2 | * qpkg.c - Quick package database query |
3 | * |
4 | * Written 2010 by Werner Almesberger |
5 | * Copyright 2010 Werner Almesberger |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | */ |
12 | |
13 | |
14 | #include <stdlib.h> |
15 | #include <stdio.h> |
16 | #include <string.h> |
17 | #include <assert.h> |
18 | |
19 | #include "jrb.h" |
20 | #include "id.h" |
21 | #include "prereq.h" |
22 | #include "gobble.h" |
23 | #include "fixup.h" |
24 | #include "pkg.h" |
25 | #include "qpkg.h" |
26 | |
27 | |
28 | struct tree *packages = NULL; |
29 | struct tree *versions = NULL; |
30 | |
31 | int debug = 0; |
32 | |
33 | |
34 | static void list_all_packages(void) |
35 | { |
36 | const struct jrb *n; |
37 | const struct pkg *pkg; |
38 | |
39 | for (n = jrb_first(packages->root); n != jrb_nil(packages->root); |
40 | n = jrb_next(n)) { |
41 | const struct id *id = n->key; |
42 | |
43 | for (pkg = n->val; pkg; pkg = pkg->more) { |
44 | printf("%.*s", ID2PF(id)); |
45 | if (!pkg) |
46 | printf(" (virtual)"); |
47 | else { |
48 | if (pkg->version) |
49 | printf(" (%.*s)", ID2PF(pkg->version)); |
50 | if (pkg->more) |
51 | printf(" +++"); |
52 | } |
53 | printf("\n"); |
54 | } |
55 | } |
56 | } |
57 | |
58 | |
59 | static void list_one_package(const char *name) |
60 | { |
61 | const struct jrb *n = find_id(packages, name, strlen(name)); |
62 | const struct pkg *pkg; |
63 | |
64 | if (!n) { |
65 | fprintf(stderr, "no such package \"%s\"\n", name); |
66 | exit(1); |
67 | } |
68 | for (pkg = n->val; pkg; pkg = pkg->more) |
69 | printf("%.*s\n", ID2PF(pkg->version)); |
70 | } |
71 | |
72 | |
73 | static void find_prereq(const char *name, const char *version) |
74 | { |
75 | const struct jrb *n = find_id(packages, name, strlen(name)); |
76 | struct pkg *pkg; |
77 | struct pkg **pkgs; |
78 | |
79 | if (!n) { |
80 | fprintf(stderr, "no such package \"%s\"\n", name); |
81 | exit(1); |
82 | } |
83 | pkg = n->val; |
84 | if (!pkg) { |
85 | fprintf(stderr, "package %s is a ghost\n", name); |
86 | exit(1); |
87 | } |
88 | if (version) { |
89 | n = find_id(versions, version, strlen(version)); |
90 | if (!n) { |
91 | fprintf(stderr, "no such version\"%s\"\n", version); |
92 | exit(1); |
93 | } |
94 | while (pkg && pkg->version != n->key) |
95 | pkg = pkg->more; |
96 | if (pkg) { |
97 | fprintf(stderr, "no %s version\"%s\" found\n", |
98 | name, version); |
99 | exit(1); |
100 | } |
101 | } |
102 | |
103 | pkgs = prereq(pkg); |
104 | if (!pkgs) { |
105 | fprintf(stderr, "can't resolve %s%s%s\n", |
106 | name, version ? " version " : "", version ? version : ""); |
107 | exit(1); |
108 | } |
109 | while (*pkgs) { |
110 | const char *file = (*pkgs)->filename; |
111 | const char *end; |
112 | |
113 | assert(file); |
114 | end = strchr(file, '\n'); |
115 | printf("%.*s\n", (int) (end-file), file); |
116 | pkgs++; |
117 | } |
118 | } |
119 | |
120 | |
121 | static void do_fixups(void) |
122 | { |
123 | sort_versions(); |
124 | complete_provisions(); |
125 | } |
126 | |
127 | |
128 | static void usage(const char *name) |
129 | { |
130 | fprintf(stderr, |
131 | "usage: %s options [pkg-list ...] list [pkg]\n" |
132 | " %s options [pkg-list ...] prereq pkg [version]\n\n" |
133 | " -d enable debugging output\n" |
134 | , name, name); |
135 | exit(1); |
136 | } |
137 | |
138 | |
139 | int main(int argc, char **argv) |
140 | { |
141 | int arg; |
142 | |
143 | packages = make_tree(comp_id); |
144 | versions = make_tree(comp_id); |
145 | |
146 | if (argc == 1) |
147 | usage(*argv); |
148 | |
149 | for (arg = 1; arg != argc; arg++) { |
150 | if (!strcmp(argv[arg], "-d")) { |
151 | debug = 1; |
152 | continue; |
153 | } |
154 | if (*argv[arg] == '-') |
155 | usage(*argv); |
156 | if (!strcmp(argv[arg], "list")) { |
157 | do_fixups(); |
158 | switch (argc-arg) { |
159 | case 1: |
160 | list_all_packages(); |
161 | return 0; |
162 | case 2: |
163 | list_one_package(argv[arg+1]); |
164 | return 0; |
165 | default: |
166 | usage(*argv); |
167 | } |
168 | } else if (!strcmp(argv[arg], "prereq")) { |
169 | do_fixups(); |
170 | switch (argc-arg) { |
171 | case 2: |
172 | find_prereq(argv[arg+1], NULL); |
173 | return 0; |
174 | case 3: |
175 | find_prereq(argv[arg+1], argv[arg+2]); |
176 | return 0; |
177 | default: |
178 | usage(*argv); |
179 | } |
180 | } else { |
181 | gobble(argv[arg]); |
182 | } |
183 | } |
184 | return 0; |
185 | } |
186 |
Branches:
master