forked from gentoo/portage-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qwhich.c
241 lines (222 loc) · 6.29 KB
/
qwhich.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright 2021 Gentoo Foundation
* Distributed under the terms of the GNU General Public License v2
*
* Copyright 2021- Fabian Groffen - <grobian@gentoo.org>
*/
#include "main.h"
#include "applets.h"
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <sys/stat.h>
#include "atom.h"
#include "tree.h"
#define QWHICH_FLAGS "IbtpdRflTAF:" COMMON_FLAGS
static struct option const qwhich_long_opts[] = {
{"vdb", no_argument, NULL, 'I'},
{"binpkg", no_argument, NULL, 'b'},
{"tree", no_argument, NULL, 't'},
{"pretty", no_argument, NULL, 'p'},
{"dir", no_argument, NULL, 'd'},
{"repo", no_argument, NULL, 'R'},
{"first", no_argument, NULL, 'f'},
{"latest", no_argument, NULL, 'l'},
{"novirtual", no_argument, NULL, 'T'},
{"noacct", no_argument, NULL, 'A'},
{"format", a_argument, NULL, 'F'},
COMMON_LONG_OPTS
};
static const char * const qwhich_opts_help[] = {
"Look in VDB (installed packages)",
"Look at binary packages",
"Look in main tree and overlays",
"Print (pretty) atom instead of path for use with -F",
"Print directory instead of path",
"Print repository name instead of path for tree/overlay matches",
"Stop searching after first match (implies -l)",
"Only return latest version for each match",
"Skip virtual category",
"Skip acct-user and acct-group categories",
"Print matched using given format string",
COMMON_OPTS_HELP
};
static const char qwhich_desc[] = "";
#define qwhich_usage(ret) \
usage(ret, QWHICH_FLAGS, qwhich_long_opts, qwhich_opts_help, qwhich_desc, lookup_applet_idx("qwhich"))
struct qwhich_mode {
char do_vdb:1;
char do_binpkg:1;
char do_tree:1;
char print_atom:1;
char print_path:1;
char print_repo:1;
char match_first:1;
char match_latest:1;
char skip_virtual:1;
char skip_acct:1;
const char *fmt;
};
int qwhich_main(int argc, char **argv)
{
depend_atom *atom;
DECLARE_ARRAY(atoms);
DECLARE_ARRAY(trees);
struct qwhich_mode m;
struct tree_match_ctx *tmc;
struct tree_match_ctx *tmcw;
size_t i;
size_t j;
char *overlay;
size_t n;
int ret;
tree_ctx *t;
char *repo;
int repolen;
const char *ext;
memset(&m, 0, sizeof(m));
while ((ret = GETOPT_LONG(QWHICH, qwhich, "")) != -1) {
switch (ret) {
COMMON_GETOPTS_CASES(qwhich)
case 'I': m.do_vdb = true; break;
case 'b': m.do_binpkg = true; break;
case 't': m.do_tree = true; break;
case 'p': m.print_atom = true; break;
case 'd': m.print_path = true; break;
case 'R': m.print_repo = true; break;
case 'f': m.match_first = true; break;
case 'l': m.match_latest = true; break;
case 'T': m.skip_virtual = true; break;
case 'A': m.skip_acct = true; break;
case 'F': m.fmt = optarg; break;
}
}
/* defaults: no options at all, enable latest match,
* no selectors, enable tree + overlays */
if (!m.do_vdb && !m.do_binpkg && !m.do_tree) {
if (!m.print_atom && !m.print_path && !m.match_first && m.fmt == NULL)
m.match_latest = true;
m.do_tree = true;
}
/* when printing path, we better just match the first, else we get a
* lot of dups */
if (m.print_path || m.match_first)
m.match_latest = true;
/* set format if none given */
if (m.fmt == NULL) {
if (verbose)
m.fmt = "%[CATEGORY]%[PF]";
else
m.fmt = "%[CATEGORY]%[PN]";
} else {
/* makes no sense to use formatter if we're not going to use it */
m.print_atom = true;
}
argc -= optind;
argv += optind;
for (i = 0; i < (size_t)argc; ++i) {
atom = atom_explode(argv[i]);
if (!atom)
warn("invalid atom: %s", argv[i]);
else
xarraypush_ptr(atoms, atom);
}
/* TODO: silence when the path doesn't exist -- reasonable though? */
if (m.do_vdb) {
t = tree_open_vdb(portroot, portvdb);
if (t != NULL)
xarraypush_ptr(trees, t);
}
if (m.do_binpkg) {
t = tree_open_binpkg("/", pkgdir);
if (t != NULL)
xarraypush_ptr(trees, t);
}
if (m.do_tree) {
array_for_each(overlays, n, overlay) {
t = tree_open(portroot, overlay);
if (t != NULL)
xarraypush_ptr(trees, t);
}
}
/* at least keep the IO constrained to a tree at a time */
array_for_each(trees, j, t) {
if (m.print_repo && t->repo != NULL) {
repo = t->repo;
repolen = strlen(repo);
} else {
repo = t->path;
if (t->cachetype == CACHE_METADATA_MD5)
repolen = strlen(t->path) - (sizeof("/metadata/md5-cache") - 1);
else if (t->cachetype == CACHE_METADATA_PMS)
repolen = strlen(t->path) - (sizeof("/metadata/cache") - 1);
else if (t->cachetype == CACHE_EBUILD ||
t->cachetype == CACHE_BINPKGS ||
t->cachetype == CACHE_PACKAGES)
repolen = strlen(t->path);
else
repolen = 0;
}
switch (t->cachetype) {
case CACHE_BINPKGS:
case CACHE_PACKAGES:
ext = "tbz2";
break;
default:
ext = "ebuild";
break;
}
array_for_each(atoms, i, atom) {
tmc = tree_match_atom(t, atom,
(m.match_latest ? TREE_MATCH_LATEST : 0 ) |
(m.match_first ? TREE_MATCH_FIRST : 0 ) |
(m.skip_virtual ? 0 : TREE_MATCH_VIRTUAL) |
(m.skip_acct ? 0 : TREE_MATCH_ACCT ));
for (tmcw = tmc; tmcw != NULL; tmcw = tmcw->next) {
if (m.print_atom) {
printf("%s\n", atom_format(m.fmt, tmcw->atom));
} else {
if (t->cachetype == CACHE_METADATA_MD5 ||
t->cachetype == CACHE_METADATA_PMS ||
t->cachetype == CACHE_EBUILD ||
t->cachetype == CACHE_BINPKGS ||
t->cachetype == CACHE_PACKAGES)
{
if (m.print_path)
printf("%s%.*s%s%s%s/%s%s%s\n",
GREEN, repolen, repo,
m.print_repo ? "::" : "/",
BOLD, tmcw->atom->CATEGORY,
DKBLUE, tmcw->atom->PN,
NORM);
else
printf("%s%.*s%s%s%s/%s%s/%s%s%s.%s%s\n",
DKGREEN, repolen, repo,
m.print_repo ? "::" : "/",
BOLD, tmcw->atom->CATEGORY,
DKBLUE, tmcw->atom->PN,
BLUE, tmcw->atom->PF,
DKGREEN, ext, NORM);
} else if (t->cachetype == CACHE_VDB && !m.print_path) {
printf("%s%s/%s%s%s.ebuild%s\n",
DKBLUE, tmcw->path,
BLUE, tmcw->atom->PF,
DKGREEN, NORM);
} else {
printf("%s%s%s\n", DKBLUE, tmcw->path, NORM);
}
}
}
tree_match_close(tmc);
}
tree_close(t);
}
array_for_each(atoms, i, atom)
atom_implode(atom);
xarrayfree_int(atoms);
xarrayfree_int(trees);
return EXIT_SUCCESS;
}