From 13331f801c66a889245a35895a05c2fa6d107c6b Mon Sep 17 00:00:00 2001 From: Juan RP Date: Thu, 22 Dec 2011 09:33:54 +0100 Subject: [PATCH] xbps-repo: the 'search' target now accepts multiple patterns as arguments. --- NEWS | 8 ++++++ bin/xbps-bin/defs.h | 1 - bin/xbps-bin/util.c | 40 ------------------------------ bin/xbps-repo/defs.h | 4 ++- bin/xbps-repo/list.c | 7 +++--- bin/xbps-repo/main.c | 13 ++++++++-- bin/xbps-repo/show.c | 51 ++++++++++++++++++++++++++++++++++++--- bin/xbps-repo/xbps-repo.8 | 6 ++--- 8 files changed, 75 insertions(+), 55 deletions(-) diff --git a/NEWS b/NEWS index 52df03b5..20bb1009 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,11 @@ +xbps-0.11.1 (???): + + * libxbps: more paranoid type checking and allocation results, to make + sure that out of memory conditions are handled gracefully. + + * xbps-repo(8): the 'search' target accepts multiple patterns, such as: + $ xbps-repo search 'foo-[0-9]*' '*blah*' ... + xbps-0.11.0 (2011-12-20): * xbps-bin(8): it is possible now to reinstall a package even if it's diff --git a/bin/xbps-bin/defs.h b/bin/xbps-bin/defs.h index df7ba392..11be530b 100644 --- a/bin/xbps-bin/defs.h +++ b/bin/xbps-bin/defs.h @@ -100,7 +100,6 @@ void unpack_progress_cb(const struct xbps_unpack_cb_data *, void *); int show_pkg_files(prop_dictionary_t); void show_pkg_info(prop_dictionary_t); void show_pkg_info_one(prop_dictionary_t, const char *); -int show_pkg_namedesc(prop_object_t, void *, bool *); int list_strings_in_array(prop_object_t, void *, bool *); int list_strings_sep_in_array(prop_object_t, void *, bool *); size_t find_longest_pkgver(prop_dictionary_t); diff --git a/bin/xbps-bin/util.c b/bin/xbps-bin/util.c index 8fa86592..aba79ee9 100644 --- a/bin/xbps-bin/util.c +++ b/bin/xbps-bin/util.c @@ -23,9 +23,6 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifdef HAVE_STRCASESTR -# define _GNU_SOURCE /* for strcasestr(3) */ -#endif #include #include #include @@ -36,7 +33,6 @@ #include #include -#include "compat.h" #include "defs.h" #include "../xbps-repo/defs.h" @@ -199,42 +195,6 @@ find_longest_pkgver(prop_dictionary_t d) return len; } -int -show_pkg_namedesc(prop_object_t obj, void *arg, bool *loop_done) -{ - struct repo_search_data *rsd = arg; - const char *pkgver, *pkgname, *desc; - char *tmp = NULL; - size_t i; - - (void)loop_done; - - assert(prop_object_type(obj) == PROP_TYPE_DICTIONARY); - assert(rsd->pattern != NULL); - - prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); - prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); - prop_dictionary_get_cstring_nocopy(obj, "short_desc", &desc); - - if ((xbps_pkgpattern_match(pkgver, rsd->pattern) == 1) || - (xbps_pkgpattern_match(desc, rsd->pattern) == 1) || - (strcasecmp(pkgname, rsd->pattern) == 0) || - (strcasestr(pkgver, rsd->pattern)) || - (strcasestr(desc, rsd->pattern))) { - tmp = calloc(1, rsd->pkgver_len + 1); - if (tmp == NULL) - return errno; - - strlcpy(tmp, pkgver, rsd->pkgver_len + 1); - for (i = strlen(tmp); i < rsd->pkgver_len; i++) - tmp[i] = ' '; - - printf(" %s %s\n", tmp, desc); - free(tmp); - } - - return 0; -} int list_strings_in_array(prop_object_t obj, void *arg, bool *loop_done) diff --git a/bin/xbps-repo/defs.h b/bin/xbps-repo/defs.h index 767f4e70..7ce90729 100644 --- a/bin/xbps-repo/defs.h +++ b/bin/xbps-repo/defs.h @@ -33,7 +33,8 @@ #include struct repo_search_data { - char *pattern; + int npatterns; + char **patterns; size_t pkgver_len; }; @@ -51,6 +52,7 @@ int repo_search_pkgs_cb(struct repository_pool_index *, void *, bool *); /* From show.c */ int show_pkg_info_from_repolist(const char *, const char *); int show_pkg_deps_from_repolist(const char *); +int show_pkg_namedesc(prop_object_t, void *, bool *); #endif /* !_XBPS_REPO_DEFS_H_ */ diff --git a/bin/xbps-repo/list.c b/bin/xbps-repo/list.c index 8b8430b3..2daf10f3 100644 --- a/bin/xbps-repo/list.c +++ b/bin/xbps-repo/list.c @@ -79,15 +79,14 @@ repo_list_uri_cb(struct repository_pool_index *rpi, void *arg, bool *done) int repo_search_pkgs_cb(struct repository_pool_index *rpi, void *arg, bool *done) { - struct repo_search_data rsd; + struct repo_search_data *rsd = arg; (void)done; - rsd.pattern = arg; - rsd.pkgver_len = find_longest_pkgver(rpi->rpi_repod); + rsd->pkgver_len = find_longest_pkgver(rpi->rpi_repod); printf("From %s repository ...\n", rpi->rpi_uri); (void)xbps_callback_array_iter_in_dict(rpi->rpi_repod, - "packages", show_pkg_namedesc, &rsd); + "packages", show_pkg_namedesc, rsd); return 0; } diff --git a/bin/xbps-repo/main.c b/bin/xbps-repo/main.c index aa3e32e6..ae29f010 100644 --- a/bin/xbps-repo/main.c +++ b/bin/xbps-repo/main.c @@ -55,6 +55,7 @@ main(int argc, char **argv) { struct xbps_handle *xhp; struct xferstat xfer; + struct repo_search_data *rsd = NULL; prop_dictionary_t pkgd; const char *rootdir, *cachedir, *conffile, *option; int c, rv = 0; @@ -148,10 +149,18 @@ main(int argc, char **argv) * Search for a package by looking at pkgname/short_desc * by using shell style match patterns (fnmatch(3)). */ - if (argc != 2) + if (argc < 2) usage(xhp); - rv = xbps_repository_pool_foreach(repo_search_pkgs_cb, argv[1]); + rsd = malloc(sizeof(*rsd)); + if (rsd == NULL) { + rv = ENOMEM; + goto out; + } + rsd->npatterns = argc; + rsd->patterns = argv; + rv = xbps_repository_pool_foreach(repo_search_pkgs_cb, rsd); + free(rsd); if (rv == ENOTSUP) xbps_error_printf("xbps-repo: no repositories " "currently registered!\n"); diff --git a/bin/xbps-repo/show.c b/bin/xbps-repo/show.c index 40efa474..c4ae409a 100644 --- a/bin/xbps-repo/show.c +++ b/bin/xbps-repo/show.c @@ -23,6 +23,14 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#ifdef HAVE_STRCASESTR +# define _GNU_SOURCE /* for strcasestr(3) */ +#endif + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include #include @@ -36,10 +44,6 @@ #include "../xbps-bin/defs.h" #include "defs.h" -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - int show_pkg_info_from_repolist(const char *pkgname, const char *option) { @@ -85,3 +89,42 @@ show_pkg_deps_from_repolist(const char *pkgname) prop_object_release(pkgd); return 0; } + +int +show_pkg_namedesc(prop_object_t obj, void *arg, bool *loop_done) +{ + struct repo_search_data *rsd = arg; + const char *pkgver, *pkgname, *desc; + char *tmp = NULL; + size_t i, x; + + (void)loop_done; + + assert(prop_object_type(obj) == PROP_TYPE_DICTIONARY); + assert(rsd->patterns != NULL); + + prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); + prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); + prop_dictionary_get_cstring_nocopy(obj, "short_desc", &desc); + + for (i = 1; i < (size_t)rsd->npatterns; i++) { + if ((xbps_pkgpattern_match(pkgver, rsd->patterns[i]) == 1) || + (xbps_pkgpattern_match(desc, rsd->patterns[i]) == 1) || + (strcasecmp(pkgname, rsd->patterns[i]) == 0) || + (strcasestr(pkgver, rsd->patterns[i])) || + (strcasestr(desc, rsd->patterns[i]))) { + tmp = calloc(1, rsd->pkgver_len + 1); + if (tmp == NULL) + return errno; + + strlcpy(tmp, pkgver, rsd->pkgver_len + 1); + for (x = strlen(tmp); x < rsd->pkgver_len; x++) + tmp[x] = ' '; + + printf(" %s %s\n", tmp, desc); + free(tmp); + } + } + + return 0; +} diff --git a/bin/xbps-repo/xbps-repo.8 b/bin/xbps-repo/xbps-repo.8 index 2c20eeb7..d93fbe93 100644 --- a/bin/xbps-repo/xbps-repo.8 +++ b/bin/xbps-repo/xbps-repo.8 @@ -1,4 +1,4 @@ -.Dd December 15, 2011 +.Dd December 22, 2011 .Os Void GNU/Linux .Dt xbps-repo 8 .Sh NAME @@ -76,7 +76,7 @@ will be shown. The argument expects a decimal number starting from 0, matching the output of the .Ar list target. -.It Sy search Ar pattern +.It Sy search Ar pattern Ar [patterns ...] Search for packages containing the shell .Em pattern (see @@ -86,7 +86,7 @@ in its or .Em description values in repository pool. Please note that patterns are matched in case -insensitive mode. +insensitive mode. Multiple patterns can be specified as arguments. .It Sy show Ar pkgname Shows information about binary package .Em pkgname .