2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2014-03-21 17:49:40 +05:30
|
|
|
* Copyright (c) 2008-2014 Juan Romero Pardines.
|
2012-11-02 19:34:25 +05:30
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_STRCASESTR
|
|
|
|
# define _GNU_SOURCE /* for strcasestr(3) */
|
|
|
|
#endif
|
|
|
|
|
2014-01-20 23:20:33 +05:30
|
|
|
#include "compat.h"
|
2012-11-02 19:34:25 +05:30
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <fnmatch.h>
|
2012-12-06 15:28:11 +05:30
|
|
|
#include <assert.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 16:01:02 +05:30
|
|
|
#include <xbps.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
#include "defs.h"
|
|
|
|
|
2013-02-11 20:48:43 +05:30
|
|
|
struct search_data {
|
2012-11-02 19:34:25 +05:30
|
|
|
int npatterns;
|
2013-06-12 13:34:10 +05:30
|
|
|
int maxcols;
|
2014-03-21 17:49:40 +05:30
|
|
|
char **patterns;
|
|
|
|
const char *prop, *repourl;
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t results;
|
2012-11-02 19:34:25 +05:30
|
|
|
};
|
|
|
|
|
2013-02-11 20:48:43 +05:30
|
|
|
static void
|
|
|
|
print_results(struct xbps_handle *xhp, struct search_data *sd)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-02-11 20:48:43 +05:30
|
|
|
const char *pkgver, *desc, *inststr;
|
|
|
|
char tmp[256], *out;
|
2013-09-15 13:36:49 +05:30
|
|
|
unsigned int j, tlen = 0, len = 0;
|
2013-02-11 20:48:43 +05:30
|
|
|
|
|
|
|
/* Iterate over results array and find out largest pkgver string */
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(sd->results); i++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get_cstring_nocopy(sd->results, i, &pkgver);
|
2013-02-11 20:48:43 +05:30
|
|
|
len = strlen(pkgver);
|
|
|
|
if (tlen == 0 || len > tlen)
|
|
|
|
tlen = len;
|
|
|
|
i++;
|
|
|
|
}
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(sd->results); i++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get_cstring_nocopy(sd->results, i, &pkgver);
|
|
|
|
xbps_array_get_cstring_nocopy(sd->results, i+1, &desc);
|
2013-02-11 20:48:43 +05:30
|
|
|
strncpy(tmp, pkgver, sizeof(tmp));
|
|
|
|
for (j = strlen(tmp); j < tlen; j++)
|
|
|
|
tmp[j] = ' ';
|
|
|
|
|
|
|
|
tmp[j] = '\0';
|
|
|
|
if (xbps_pkgdb_get_pkg(xhp, pkgver))
|
|
|
|
inststr = "[*]";
|
|
|
|
else
|
|
|
|
inststr = "[-]";
|
|
|
|
|
2013-02-28 20:37:11 +05:30
|
|
|
len = strlen(inststr) + strlen(tmp) + strlen(desc) + 3;
|
2013-06-12 13:34:10 +05:30
|
|
|
if ((int)len > sd->maxcols) {
|
2013-02-11 20:48:43 +05:30
|
|
|
out = malloc(sd->maxcols+1);
|
|
|
|
assert(out);
|
2013-02-28 20:37:11 +05:30
|
|
|
snprintf(out, sd->maxcols-3, "%s %s %s",
|
2013-02-11 20:48:43 +05:30
|
|
|
inststr, tmp, desc);
|
2013-02-28 20:37:11 +05:30
|
|
|
strncat(out, "...\n", sd->maxcols);
|
2013-02-11 20:48:43 +05:30
|
|
|
printf("%s", out);
|
|
|
|
free(out);
|
|
|
|
} else {
|
|
|
|
printf("%s %s %s\n", inststr, tmp, desc);
|
|
|
|
}
|
|
|
|
i++;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-08-29 14:15:30 +05:30
|
|
|
search_array_cb(struct xbps_handle *xhp _unused,
|
|
|
|
xbps_object_t obj,
|
|
|
|
const char *key _unused,
|
|
|
|
void *arg,
|
|
|
|
bool *done _unused)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2014-03-21 17:49:40 +05:30
|
|
|
xbps_object_t obj2;
|
2013-02-11 20:48:43 +05:30
|
|
|
struct search_data *sd = arg;
|
2014-03-21 17:49:40 +05:30
|
|
|
const char *pkgver, *desc, *str;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
if (sd->prop == NULL) {
|
|
|
|
/* no prop set, match on pkgver/short_desc objects */
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "short_desc", &desc);
|
|
|
|
|
|
|
|
for (x = 0; x < sd->npatterns; x++) {
|
|
|
|
bool vpkgfound = false;
|
|
|
|
|
2014-04-20 20:24:50 +05:30
|
|
|
if (xbps_match_virtual_pkg_in_dict(obj, sd->patterns[x]))
|
2014-03-21 17:49:40 +05:30
|
|
|
vpkgfound = true;
|
|
|
|
|
|
|
|
if ((xbps_pkgpattern_match(pkgver, sd->patterns[x])) ||
|
|
|
|
(strcasestr(pkgver, sd->patterns[x])) ||
|
|
|
|
(strcasestr(desc, sd->patterns[x])) || vpkgfound) {
|
|
|
|
xbps_array_add_cstring_nocopy(sd->results, pkgver);
|
|
|
|
xbps_array_add_cstring_nocopy(sd->results, desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* prop set, match on prop object instead */
|
|
|
|
obj2 = xbps_dictionary_get(obj, sd->prop);
|
|
|
|
if (xbps_object_type(obj2) == XBPS_TYPE_ARRAY) {
|
|
|
|
/* property is an array */
|
|
|
|
for (unsigned int i = 0; i < xbps_array_count(obj2); i++) {
|
|
|
|
xbps_array_get_cstring_nocopy(obj2, i, &str);
|
|
|
|
for (x = 0; x < sd->npatterns; x++) {
|
|
|
|
if ((strcasestr(str, sd->patterns[x])) ||
|
|
|
|
(fnmatch(sd->patterns[x], str, FNM_PERIOD)) == 0) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"pkgver", &pkgver);
|
|
|
|
printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (xbps_object_type(obj2) == XBPS_TYPE_BOOL) {
|
|
|
|
/* property is a bool */
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
|
|
|
printf("%s: true (%s)\n", pkgver, sd->repourl);
|
|
|
|
} else if (xbps_object_type(obj2) == XBPS_TYPE_STRING) {
|
|
|
|
/* property is a string */
|
|
|
|
str = xbps_string_cstring_nocopy(obj2);
|
|
|
|
for (x = 0; x < sd->npatterns; x++) {
|
|
|
|
if (strcasestr(str, sd->patterns[x])) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"pkgver", &pkgver);
|
|
|
|
printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
|
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
}
|
2013-07-27 13:17:16 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-08-29 14:15:30 +05:30
|
|
|
search_pkgs_cb(struct xbps_repo *repo, void *arg, bool *done _unused)
|
2013-07-27 13:17:16 +05:30
|
|
|
{
|
|
|
|
xbps_array_t allkeys;
|
|
|
|
struct search_data *sd = arg;
|
|
|
|
|
2013-10-05 15:08:04 +05:30
|
|
|
if (repo->idx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-21 17:49:40 +05:30
|
|
|
sd->repourl = repo->uri;
|
2013-07-27 13:17:16 +05:30
|
|
|
allkeys = xbps_dictionary_all_keys(repo->idx);
|
2014-03-21 17:49:40 +05:30
|
|
|
return xbps_array_foreach_cb(repo->xhp, allkeys, repo->idx, search_array_cb, sd);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-03-21 17:49:40 +05:30
|
|
|
repo_search(struct xbps_handle *xhp, int npatterns, char **patterns, const char *prop)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-02-11 20:48:43 +05:30
|
|
|
struct search_data sd;
|
2012-11-02 19:34:25 +05:30
|
|
|
int rv;
|
|
|
|
|
2014-03-21 17:49:40 +05:30
|
|
|
sd.prop = prop;
|
2013-02-11 20:48:43 +05:30
|
|
|
sd.npatterns = npatterns;
|
|
|
|
sd.patterns = patterns;
|
|
|
|
sd.maxcols = get_maxcols();
|
2013-06-20 13:56:12 +05:30
|
|
|
sd.results = xbps_array_create();
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-02-11 20:48:43 +05:30
|
|
|
rv = xbps_rpool_foreach(xhp, search_pkgs_cb, &sd);
|
2012-11-02 19:34:25 +05:30
|
|
|
if (rv != 0 && rv != ENOTSUP)
|
|
|
|
fprintf(stderr, "Failed to initialize rpool: %s\n",
|
|
|
|
strerror(rv));
|
|
|
|
|
2014-03-21 17:49:40 +05:30
|
|
|
if (!prop && xbps_array_count(sd.results)) {
|
2013-07-27 23:32:07 +05:30
|
|
|
print_results(xhp, &sd);
|
|
|
|
xbps_object_release(sd.results);
|
|
|
|
}
|
2013-02-11 20:48:43 +05:30
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
return rv;
|
|
|
|
}
|