-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathsearch.c
More file actions
345 lines (301 loc) · 8.81 KB
/
search.c
File metadata and controls
345 lines (301 loc) · 8.81 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*-
* Copyright (c) 2008-2015 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <libgen.h>
#include <fnmatch.h>
#include <assert.h>
#include <regex.h>
#include <xbps.h>
#include "defs.h"
struct search_ctx {
bool regex, repo_mode;
regex_t regexp;
unsigned int maxcols;
const char *pattern;
const char *repourl;
xbps_array_t results;
char *linebuf;
};
static void
print_results(struct xbps_handle *xhp, struct search_ctx *sd)
{
const char *pkgver = NULL, *desc = NULL;
unsigned int align = 0, len;
/* Iterate over results array and find out largest pkgver string */
for (unsigned int i = 0; i < xbps_array_count(sd->results); i += 2) {
xbps_array_get_cstring_nocopy(sd->results, i, &pkgver);
if ((len = strlen(pkgver)) > align)
align = len;
}
for (unsigned int i = 0; i < xbps_array_count(sd->results); i += 2) {
xbps_array_get_cstring_nocopy(sd->results, i, &pkgver);
xbps_array_get_cstring_nocopy(sd->results, i+1, &desc);
if (sd->linebuf == NULL) {
printf("[%s] %-*s %s\n",
xbps_pkgdb_get_pkg(xhp, pkgver) ? "*" : "-",
align, pkgver, desc);
continue;
}
len = snprintf(sd->linebuf, sd->maxcols, "[%s] %-*s %s",
xbps_pkgdb_get_pkg(xhp, pkgver) ? "*" : "-",
align, pkgver, desc);
/* add ellipsis if the line was truncated */
if (len >= sd->maxcols && sd->maxcols > 4) {
sd->linebuf[sd->maxcols - 4] = '.';
sd->linebuf[sd->maxcols - 3] = '.';
sd->linebuf[sd->maxcols - 2] = '.';
}
puts(sd->linebuf);
}
}
static int
search_cb(struct xbps_handle *xhp UNUSED, xbps_object_t pkgd,
const char *key UNUSED, void *arg, bool *done UNUSED)
{
struct search_ctx *ctx = arg;
bool vpkgfound = false;
const char *pkgver = NULL, *desc = NULL;
if (!xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver))
abort();
if (!xbps_dictionary_get_cstring_nocopy(pkgd, "short_desc", &desc)) {
return xbps_error_errno(
EINVAL, "%s: missing short_desc property\n", pkgver);
}
if (ctx->repo_mode && xbps_match_virtual_pkg_in_dict(pkgd, ctx->pattern))
vpkgfound = true;
if (ctx->regex) {
if ((regexec(&ctx->regexp, pkgver, 0, 0, 0) == 0) ||
(regexec(&ctx->regexp, desc, 0, 0, 0) == 0)) {
if (!xbps_array_add_cstring_nocopy(ctx->results, pkgver))
return xbps_error_oom();
if (!xbps_array_add_cstring_nocopy(ctx->results, desc))
return xbps_error_oom();
}
return 0;
}
if (vpkgfound) {
if (!xbps_array_add_cstring_nocopy(ctx->results, pkgver))
return xbps_error_oom();
if (!xbps_array_add_cstring_nocopy(ctx->results, desc))
return xbps_error_oom();
} else {
if ((strcasestr(pkgver, ctx->pattern)) ||
(strcasestr(desc, ctx->pattern)) ||
(xbps_pkgpattern_match(pkgver, ctx->pattern))) {
if (!xbps_array_add_cstring_nocopy(ctx->results, pkgver))
return xbps_error_oom();
if (!xbps_array_add_cstring_nocopy(ctx->results, desc))
return xbps_error_oom();
}
}
return 0;
}
static int
search_repo_cb(struct xbps_repo *repo, void *arg, bool *done UNUSED)
{
xbps_array_t keys;
struct search_ctx *ctx = arg;
int rv;
keys = xbps_dictionary_all_keys(repo->idx);
if (!keys)
return xbps_error_oom();
ctx->repourl = repo->uri;
rv = xbps_array_foreach_cb(repo->xhp, keys, repo->idx, search_cb, ctx);
xbps_object_release(keys);
return rv;
}
int
search(struct xbps_handle *xhp, bool repo_mode, const char *pattern, bool regex)
{
struct search_ctx ctx = {
.repo_mode = repo_mode,
.regex = regex,
.pattern = pattern,
.maxcols = get_maxcols(),
};
int r;
if (regex) {
r = regcomp(
&ctx.regexp, pattern, REG_EXTENDED | REG_NOSUB | REG_ICASE);
if (r != 0) {
char errbuf[4096];
regerror(r, &ctx.regexp, errbuf, sizeof(errbuf));
xbps_error_printf("failed to compile regexp: %s: %s\n",
pattern, errbuf);
return EXIT_FAILURE;
}
}
ctx.results = xbps_array_create();
if (!ctx.results) {
r = xbps_error_oom();
goto err;
}
if (ctx.maxcols > 0) {
ctx.linebuf = malloc(ctx.maxcols);
if (!ctx.linebuf) {
r = xbps_error_oom();
goto err;
}
}
if (repo_mode)
r = xbps_rpool_foreach(xhp, search_repo_cb, &ctx);
else
r = xbps_pkgdb_foreach_cb(xhp, search_cb, &ctx);
if (r != 0)
goto err;
print_results(xhp, &ctx);
err:
if (ctx.results)
xbps_object_release(ctx.results);
if (regex)
regfree(&ctx.regexp);
free(ctx.linebuf);
return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
struct search_prop_ctx {
bool regex, repo_mode;
const char *pattern;
const char *prop;
regex_t regexp;
const char *repourl;
};
static int
match_prop_cstring(const struct search_prop_ctx *ctx, const char *pkgver, const char *str)
{
if (ctx->regex) {
if (regexec(&ctx->regexp, str, 0, 0, 0) != 0)
return 0;
} else {
if (!strcasestr(str, ctx->pattern))
return 0;
}
if (ctx->repo_mode)
printf("%s: %s (%s)\n", pkgver, str, ctx->repourl);
else
printf("%s: %s\n", pkgver, str);
return 0;
}
static int
match_prop_number(const struct search_prop_ctx *ctx, const char *pkgver, xbps_number_t num)
{
char fmt[8];
if (xbps_humanize_number(fmt, xbps_number_integer_value(num)) == -1)
return -EINVAL;
return match_prop_cstring(ctx, pkgver, fmt);
}
static int
match_prop_object(const struct search_prop_ctx *ctx, const char *pkgver, xbps_object_t obj)
{
switch (xbps_object_type(obj)) {
case XBPS_TYPE_UNKNOWN:
case XBPS_TYPE_ARRAY:
case XBPS_TYPE_DATA:
case XBPS_TYPE_DICTIONARY:
case XBPS_TYPE_DICT_KEYSYM:
return 0;
case XBPS_TYPE_BOOL:
return match_prop_cstring(
ctx, pkgver, xbps_bool_true(obj) ? "true" : "false");
case XBPS_TYPE_NUMBER:
return match_prop_number(ctx, pkgver, obj);
case XBPS_TYPE_STRING:
return match_prop_cstring(
ctx, pkgver, xbps_string_cstring_nocopy(obj));
}
abort();
}
static int
match_prop_array(const struct search_prop_ctx *ctx, const char *pkgver, xbps_array_t array)
{
for (unsigned int i = 0; i < xbps_array_count(array); i++) {
int r = match_prop_object(ctx, pkgver, xbps_array_get(array, i));
if (r < 0)
return r;
}
return 0;
}
static int
search_prop_cb(struct xbps_handle *xhp UNUSED, xbps_object_t pkgd,
const char *key UNUSED, void *arg, bool *done UNUSED)
{
const struct search_prop_ctx *ctx = arg;
xbps_object_t obj;
const char *pkgver = NULL;
if (!xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver))
abort();
obj = xbps_dictionary_get(pkgd, ctx->prop);
if (xbps_object_type(obj) == XBPS_TYPE_ARRAY)
return match_prop_array(ctx, pkgver, obj);
return match_prop_object(ctx, pkgver, obj);
}
static int
search_prop_repo_cb(struct xbps_repo *repo, void *arg, bool *done UNUSED)
{
xbps_array_t keys;
struct search_prop_ctx *ctx = arg;
int rv;
keys = xbps_dictionary_all_keys(repo->idx);
if (!keys)
return xbps_error_oom();
ctx->repourl = repo->uri;
rv = xbps_array_foreach_cb(
repo->xhp, keys, repo->idx, search_prop_cb, ctx);
xbps_object_release(keys);
return rv;
}
int
search_prop(struct xbps_handle *xhp, bool repo_mode, const char *pattern, const char *prop, bool regex)
{
struct search_prop_ctx ctx = {
.repo_mode = repo_mode,
.regex = regex,
.pattern = pattern,
.prop = prop,
};
int r;
if (regex) {
r = regcomp(
&ctx.regexp, pattern, REG_EXTENDED | REG_NOSUB | REG_ICASE);
if (r != 0) {
char errbuf[4096];
regerror(r, &ctx.regexp, errbuf, sizeof(errbuf));
xbps_error_printf("failed to compile regexp: %s: %s\n",
pattern, errbuf);
return EXIT_FAILURE;
}
}
if (repo_mode)
r = xbps_rpool_foreach(xhp, search_prop_repo_cb, &ctx);
else
r = xbps_pkgdb_foreach_cb(xhp, search_prop_cb, &ctx);
if (r != 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}