xbps-query(8): added local search mode support (-s [-p]).

This commit is contained in:
Juan RP 2014-09-11 01:15:51 +02:00
parent 01dbb968dd
commit ed67cad564
4 changed files with 59 additions and 22 deletions

3
NEWS
View File

@ -1,5 +1,8 @@
xbps-0.38 (???): xbps-0.38 (???):
* xbps-query(8): added -s/--search local mode, the previous repository mode must
be specified with -R/--repository mode explicitly.
* pkgdb-0.38 format: the pkgdb plist file now contains all pkg metadata objects * pkgdb-0.38 format: the pkgdb plist file now contains all pkg metadata objects
plus the pkgdb specific ones (automatic-install, install-date, metafile-sha256) plus the pkgdb specific ones (automatic-install, install-date, metafile-sha256)
with the pkg files list being the exception. The pkgdb code will automatically with the pkg files list being the exception. The pkgdb code will automatically

View File

@ -65,6 +65,7 @@ int list_pkgs_pkgdb(struct xbps_handle *);
int repo_list(struct xbps_handle *); int repo_list(struct xbps_handle *);
/* from search.c */ /* from search.c */
int repo_search(struct xbps_handle *, const char *, const char *, bool); int search(struct xbps_handle *, bool, const char *, const char *, bool);
#endif /* !_XBPS_QUERY_DEFS_H_ */ #endif /* !_XBPS_QUERY_DEFS_H_ */

View File

@ -103,11 +103,11 @@ main(int argc, char **argv)
int c, flags, rv; int c, flags, rv;
bool list_pkgs, list_repos, orphans, own; bool list_pkgs, list_repos, orphans, own;
bool list_manual, list_hold, show_prop, show_files, show_deps, show_rdeps; bool list_manual, list_hold, show_prop, show_files, show_deps, show_rdeps;
bool show, search, regex, repo_mode, opmode, fulldeptree; bool show, pkg_search, regex, repo_mode, opmode, fulldeptree;
rootdir = cachedir = conffile = props = pkg = NULL; rootdir = cachedir = conffile = props = pkg = NULL;
flags = rv = c = 0; flags = rv = c = 0;
list_pkgs = list_repos = list_hold = orphans = search = own = false; list_pkgs = list_repos = list_hold = orphans = pkg_search = own = false;
list_manual = show_prop = show_files = false; list_manual = show_prop = show_files = false;
regex = show = show_deps = show_rdeps = fulldeptree = false; regex = show = show_deps = show_rdeps = fulldeptree = false;
repo_mode = opmode = false; repo_mode = opmode = false;
@ -173,7 +173,7 @@ main(int argc, char **argv)
break; break;
case 's': case 's':
pkg = optarg; pkg = optarg;
search = opmode = true; pkg_search = opmode = true;
break; break;
case 'v': case 'v':
flags |= XBPS_FLAG_VERBOSE; flags |= XBPS_FLAG_VERBOSE;
@ -252,9 +252,9 @@ main(int argc, char **argv)
/* ownedby mode */ /* ownedby mode */
rv = ownedby(&xh, pkg, repo_mode, regex); rv = ownedby(&xh, pkg, repo_mode, regex);
} else if (search) { } else if (pkg_search) {
/* search mode */ /* search mode */
rv = repo_search(&xh, pkg, props, regex); rv = search(&xh, repo_mode, pkg, props, regex);
} else if (show || show_prop) { } else if (show || show_prop) {
/* show mode */ /* show mode */

View File

@ -44,7 +44,7 @@
#include "defs.h" #include "defs.h"
struct search_data { struct search_data {
bool regex; bool regex, repo_mode;
int maxcols; int maxcols;
const char *pat, *prop, *repourl; const char *pat, *prop, *repourl;
xbps_array_t results; xbps_array_t results;
@ -112,7 +112,7 @@ search_array_cb(struct xbps_handle *xhp _unused,
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
xbps_dictionary_get_cstring_nocopy(obj, "short_desc", &desc); xbps_dictionary_get_cstring_nocopy(obj, "short_desc", &desc);
if (xbps_match_virtual_pkg_in_dict(obj, sd->pat)) if (sd->repo_mode && xbps_match_virtual_pkg_in_dict(obj, sd->pat))
vpkgfound = true; vpkgfound = true;
if (sd->regex) { if (sd->regex) {
@ -151,14 +151,20 @@ search_array_cb(struct xbps_handle *xhp _unused,
return errno; return errno;
if (regexec(&regex, str, 0, 0, 0) == 0) { if (regexec(&regex, str, 0, 0, 0) == 0) {
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
if (sd->repo_mode)
printf("%s: %s (%s)\n", pkgver, str, sd->repourl); printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
else
printf("%s: %s\n", pkgver, str);
} }
regfree(&regex); regfree(&regex);
} else { } else {
if ((strcasestr(str, sd->pat)) || if ((strcasestr(str, sd->pat)) ||
(fnmatch(sd->pat, str, FNM_PERIOD)) == 0) { (fnmatch(sd->pat, str, FNM_PERIOD)) == 0) {
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
if (sd->repo_mode)
printf("%s: %s (%s)\n", pkgver, str, sd->repourl); printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
else
printf("%s: %s\n", pkgver, str);
} }
} }
} }
@ -174,18 +180,28 @@ search_array_cb(struct xbps_handle *xhp _unused,
if (regcomp(&regex, sd->pat, REG_EXTENDED|REG_NOSUB) != 0) if (regcomp(&regex, sd->pat, REG_EXTENDED|REG_NOSUB) != 0)
return errno; return errno;
if (regexec(&regex, size, 0, 0, 0) == 0) { if (regexec(&regex, size, 0, 0, 0) == 0) {
if (sd->repo_mode)
printf("%s: %s (%s)\n", pkgver, size, sd->repourl); printf("%s: %s (%s)\n", pkgver, size, sd->repourl);
else
printf("%s: %s\n", pkgver, size);
} }
regfree(&regex); regfree(&regex);
} else { } else {
if (strcasestr(size, sd->pat)) { if (strcasestr(size, sd->pat)) {
if (sd->repo_mode)
printf("%s: %s (%s)\n", pkgver, size, sd->repourl); printf("%s: %s (%s)\n", pkgver, size, sd->repourl);
else
printf("%s: %s\n", pkgver, size);
} }
} }
} else if (xbps_object_type(obj2) == XBPS_TYPE_BOOL) { } else if (xbps_object_type(obj2) == XBPS_TYPE_BOOL) {
/* property is a bool */ /* property is a bool */
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
if (sd->repo_mode)
printf("%s: true (%s)\n", pkgver, sd->repourl); printf("%s: true (%s)\n", pkgver, sd->repourl);
else
printf("%s: true\n", pkgver);
} else if (xbps_object_type(obj2) == XBPS_TYPE_STRING) { } else if (xbps_object_type(obj2) == XBPS_TYPE_STRING) {
/* property is a string */ /* property is a string */
str = xbps_string_cstring_nocopy(obj2); str = xbps_string_cstring_nocopy(obj2);
@ -194,13 +210,19 @@ search_array_cb(struct xbps_handle *xhp _unused,
return errno; return errno;
if (regexec(&regex, str, 0, 0, 0) == 0) { if (regexec(&regex, str, 0, 0, 0) == 0) {
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
if (sd->repo_mode)
printf("%s: %s (%s)\n", pkgver, str, sd->repourl); printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
else
printf("%s: %s\n", pkgver, str);
} }
regfree(&regex); regfree(&regex);
} else { } else {
if (strcasestr(str, sd->pat)) { if (strcasestr(str, sd->pat)) {
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
if (sd->repo_mode)
printf("%s: %s (%s)\n", pkgver, str, sd->repourl); printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
else
printf("%s: %s\n", pkgver, str);
} }
} }
} }
@ -208,7 +230,7 @@ search_array_cb(struct xbps_handle *xhp _unused,
} }
static int static int
search_pkgs_cb(struct xbps_repo *repo, void *arg, bool *done _unused) search_repo_cb(struct xbps_repo *repo, void *arg, bool *done _unused)
{ {
xbps_array_t allkeys; xbps_array_t allkeys;
struct search_data *sd = arg; struct search_data *sd = arg;
@ -222,22 +244,33 @@ search_pkgs_cb(struct xbps_repo *repo, void *arg, bool *done _unused)
} }
int int
repo_search(struct xbps_handle *xhp, const char *pat, const char *prop, bool regex) search(struct xbps_handle *xhp, bool repo_mode, const char *pat, const char *prop, bool regex)
{ {
struct search_data sd; struct search_data sd;
int rv; int rv;
sd.regex = regex; sd.regex = regex;
sd.repo_mode = repo_mode;
sd.pat = pat; sd.pat = pat;
sd.prop = prop; sd.prop = prop;
sd.maxcols = get_maxcols(); sd.maxcols = get_maxcols();
sd.results = xbps_array_create(); sd.results = xbps_array_create();
rv = xbps_rpool_foreach(xhp, search_pkgs_cb, &sd); if (repo_mode) {
if (rv != 0 && rv != ENOTSUP) rv = xbps_rpool_foreach(xhp, search_repo_cb, &sd);
if (rv != 0 && rv != ENOTSUP) {
fprintf(stderr, "Failed to initialize rpool: %s\n", fprintf(stderr, "Failed to initialize rpool: %s\n",
strerror(rv)); strerror(rv));
return rv;
}
} else {
rv = xbps_pkgdb_foreach_cb(xhp, search_array_cb, &sd);
if (rv != 0) {
fprintf(stderr, "Failed to initialize pkgdb: %s\n",
strerror(rv));
return rv;
}
}
if (!prop && xbps_array_count(sd.results)) { if (!prop && xbps_array_count(sd.results)) {
print_results(xhp, &sd); print_results(xhp, &sd);
xbps_object_release(sd.results); xbps_object_release(sd.results);