2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2020-04-29 17:42:10 +05:30
|
|
|
* 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.
|
2012-11-02 19:34:25 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
2014-05-25 14:33:31 +05:30
|
|
|
#include <regex.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 {
|
2014-09-11 04:45:51 +05:30
|
|
|
bool regex, repo_mode;
|
2015-01-15 01:23:01 +05:30
|
|
|
regex_t regexp;
|
2020-02-22 18:48:35 +05:30
|
|
|
unsigned int maxcols;
|
2014-05-26 21:00:44 +05:30
|
|
|
const char *pat, *prop, *repourl;
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t results;
|
2020-04-18 20:17:30 +05:30
|
|
|
char *linebuf;
|
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
|
|
|
{
|
2020-04-18 20:17:30 +05:30
|
|
|
const char *pkgver = NULL, *desc = NULL;
|
|
|
|
unsigned int align = 0, len;
|
2013-02-11 20:48:43 +05:30
|
|
|
|
|
|
|
/* Iterate over results array and find out largest pkgver string */
|
2020-04-18 20:17:30 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(sd->results); i += 2) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get_cstring_nocopy(sd->results, i, &pkgver);
|
2020-04-18 20:17:30 +05:30
|
|
|
if ((len = strlen(pkgver)) > align)
|
|
|
|
align = len;
|
2013-02-11 20:48:43 +05:30
|
|
|
}
|
2020-04-18 20:17:30 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(sd->results); i += 2) {
|
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
|
|
|
|
2020-04-18 20:17:30 +05:30
|
|
|
if (sd->linebuf == NULL) {
|
|
|
|
printf("[%s] %-*s %s\n",
|
|
|
|
xbps_pkgdb_get_pkg(xhp, pkgver) ? "*" : "-",
|
|
|
|
align, pkgver, desc);
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-11 20:48:43 +05:30
|
|
|
|
2020-04-18 20:17:30 +05:30
|
|
|
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) {
|
|
|
|
for (unsigned int j = 0; j < 3; j++)
|
|
|
|
sd->linebuf[sd->maxcols-j-1] = '.';
|
|
|
|
sd->linebuf[sd->maxcols] = '\0';
|
2013-02-11 20:48:43 +05:30
|
|
|
}
|
2020-04-18 20:17:30 +05:30
|
|
|
puts(sd->linebuf);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-07-18 07:54:26 +05:30
|
|
|
search_array_cb(struct xbps_handle *xhp UNUSED,
|
2013-08-29 14:15:30 +05:30
|
|
|
xbps_object_t obj,
|
2018-07-18 07:54:26 +05:30
|
|
|
const char *key UNUSED,
|
2013-08-29 14:15:30 +05:30
|
|
|
void *arg,
|
2018-07-18 07:54:26 +05:30
|
|
|
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;
|
2019-06-27 20:39:43 +05:30
|
|
|
const char *pkgver = NULL, *desc = NULL, *str = NULL;
|
2014-03-21 17:49:40 +05:30
|
|
|
|
2014-10-17 13:39:34 +05:30
|
|
|
if (!xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver))
|
|
|
|
return 0;
|
|
|
|
|
2014-03-21 17:49:40 +05:30
|
|
|
if (sd->prop == NULL) {
|
2014-05-26 21:00:44 +05:30
|
|
|
bool vpkgfound = false;
|
2014-03-21 17:49:40 +05:30
|
|
|
/* no prop set, match on pkgver/short_desc objects */
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "short_desc", &desc);
|
|
|
|
|
2014-09-11 04:45:51 +05:30
|
|
|
if (sd->repo_mode && xbps_match_virtual_pkg_in_dict(obj, sd->pat))
|
2014-05-26 21:00:44 +05:30
|
|
|
vpkgfound = true;
|
2014-03-21 17:49:40 +05:30
|
|
|
|
2014-05-26 21:00:44 +05:30
|
|
|
if (sd->regex) {
|
2015-01-15 01:23:01 +05:30
|
|
|
if ((regexec(&sd->regexp, pkgver, 0, 0, 0) == 0) ||
|
|
|
|
(regexec(&sd->regexp, desc, 0, 0, 0) == 0)) {
|
2014-05-26 21:00:44 +05:30
|
|
|
xbps_array_add_cstring_nocopy(sd->results, pkgver);
|
|
|
|
xbps_array_add_cstring_nocopy(sd->results, desc);
|
|
|
|
}
|
2014-07-06 13:03:59 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (vpkgfound) {
|
|
|
|
xbps_array_add_cstring_nocopy(sd->results, pkgver);
|
|
|
|
xbps_array_add_cstring_nocopy(sd->results, desc);
|
2014-05-26 21:00:44 +05:30
|
|
|
} else {
|
2015-02-20 15:31:18 +05:30
|
|
|
if ((strcasestr(pkgver, sd->pat)) ||
|
2015-02-20 15:37:35 +05:30
|
|
|
(strcasestr(desc, sd->pat)) ||
|
|
|
|
(xbps_pkgpattern_match(pkgver, sd->pat))) {
|
2014-05-26 21:00:44 +05:30
|
|
|
xbps_array_add_cstring_nocopy(sd->results, pkgver);
|
|
|
|
xbps_array_add_cstring_nocopy(sd->results, desc);
|
2014-03-21 17:49:40 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
2014-05-25 14:33:31 +05:30
|
|
|
if (sd->regex) {
|
2015-01-15 01:23:01 +05:30
|
|
|
if (regexec(&sd->regexp, str, 0, 0, 0) == 0) {
|
2014-09-11 04:45:51 +05:30
|
|
|
if (sd->repo_mode)
|
|
|
|
printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
|
|
|
|
else
|
|
|
|
printf("%s: %s\n", pkgver, str);
|
2014-05-25 14:33:31 +05:30
|
|
|
}
|
|
|
|
} else {
|
2015-02-20 15:31:18 +05:30
|
|
|
if (strcasestr(str, sd->pat)) {
|
2014-09-11 04:45:51 +05:30
|
|
|
if (sd->repo_mode)
|
|
|
|
printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
|
|
|
|
else
|
|
|
|
printf("%s: %s\n", pkgver, str);
|
2014-05-25 14:33:31 +05:30
|
|
|
}
|
2014-03-21 17:49:40 +05:30
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2014-08-02 15:41:28 +05:30
|
|
|
} else if (xbps_object_type(obj2) == XBPS_TYPE_NUMBER) {
|
|
|
|
/* property is a number */
|
|
|
|
char size[8];
|
|
|
|
|
|
|
|
if (xbps_humanize_number(size, xbps_number_integer_value(obj2)) == -1)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
if (sd->regex) {
|
2015-01-15 01:23:01 +05:30
|
|
|
if (regexec(&sd->regexp, size, 0, 0, 0) == 0) {
|
2014-09-11 04:45:51 +05:30
|
|
|
if (sd->repo_mode)
|
|
|
|
printf("%s: %s (%s)\n", pkgver, size, sd->repourl);
|
|
|
|
else
|
|
|
|
printf("%s: %s\n", pkgver, size);
|
2014-08-02 15:41:28 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strcasestr(size, sd->pat)) {
|
2014-09-11 04:45:51 +05:30
|
|
|
if (sd->repo_mode)
|
|
|
|
printf("%s: %s (%s)\n", pkgver, size, sd->repourl);
|
|
|
|
else
|
|
|
|
printf("%s: %s\n", pkgver, size);
|
2014-08-02 15:41:28 +05:30
|
|
|
}
|
|
|
|
}
|
2014-05-26 21:00:44 +05:30
|
|
|
} else if (xbps_object_type(obj2) == XBPS_TYPE_BOOL) {
|
|
|
|
/* property is a bool */
|
2014-09-11 04:45:51 +05:30
|
|
|
if (sd->repo_mode)
|
|
|
|
printf("%s: true (%s)\n", pkgver, sd->repourl);
|
|
|
|
else
|
|
|
|
printf("%s: true\n", pkgver);
|
|
|
|
|
2014-05-26 21:00:44 +05:30
|
|
|
} else if (xbps_object_type(obj2) == XBPS_TYPE_STRING) {
|
|
|
|
/* property is a string */
|
|
|
|
str = xbps_string_cstring_nocopy(obj2);
|
|
|
|
if (sd->regex) {
|
2015-01-15 01:23:01 +05:30
|
|
|
if (regexec(&sd->regexp, str, 0, 0, 0) == 0) {
|
2014-09-11 04:45:51 +05:30
|
|
|
if (sd->repo_mode)
|
|
|
|
printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
|
|
|
|
else
|
|
|
|
printf("%s: %s\n", pkgver, str);
|
2014-05-26 21:00:44 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strcasestr(str, sd->pat)) {
|
2014-09-11 04:45:51 +05:30
|
|
|
if (sd->repo_mode)
|
|
|
|
printf("%s: %s (%s)\n", pkgver, str, sd->repourl);
|
|
|
|
else
|
|
|
|
printf("%s: %s\n", pkgver, str);
|
2014-05-26 21:00:44 +05:30
|
|
|
}
|
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2013-07-27 13:17:16 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-07-18 07:54:26 +05:30
|
|
|
search_repo_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;
|
2015-01-10 23:41:31 +05:30
|
|
|
int rv;
|
2013-07-27 13:17:16 +05:30
|
|
|
|
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);
|
2015-01-10 23:41:31 +05:30
|
|
|
rv = xbps_array_foreach_cb(repo->xhp, allkeys, repo->idx, search_array_cb, sd);
|
|
|
|
xbps_object_release(allkeys);
|
|
|
|
return rv;
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-09-11 04:45:51 +05:30
|
|
|
search(struct xbps_handle *xhp, bool repo_mode, const char *pat, const char *prop, bool regex)
|
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-05-25 14:33:31 +05:30
|
|
|
sd.regex = regex;
|
2015-01-15 01:23:01 +05:30
|
|
|
if (regex) {
|
2015-01-18 22:20:26 +05:30
|
|
|
if (regcomp(&sd.regexp, pat, REG_EXTENDED|REG_NOSUB|REG_ICASE) != 0)
|
2015-01-15 01:23:01 +05:30
|
|
|
return errno;
|
|
|
|
}
|
2014-09-11 04:45:51 +05:30
|
|
|
sd.repo_mode = repo_mode;
|
2014-05-26 21:00:44 +05:30
|
|
|
sd.pat = pat;
|
2014-03-21 17:49:40 +05:30
|
|
|
sd.prop = prop;
|
2013-02-11 20:48:43 +05:30
|
|
|
sd.maxcols = get_maxcols();
|
2013-06-20 13:56:12 +05:30
|
|
|
sd.results = xbps_array_create();
|
2020-04-18 20:17:30 +05:30
|
|
|
sd.linebuf = NULL;
|
|
|
|
if (sd.maxcols > 0) {
|
|
|
|
sd.linebuf = malloc(sd.maxcols);
|
|
|
|
if (sd.linebuf == NULL)
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2014-09-11 04:45:51 +05:30
|
|
|
if (repo_mode) {
|
|
|
|
rv = xbps_rpool_foreach(xhp, search_repo_cb, &sd);
|
|
|
|
if (rv != 0 && rv != ENOTSUP) {
|
2023-02-09 15:24:36 +05:30
|
|
|
xbps_error_printf("Failed to initialize rpool: %s\n",
|
2014-09-11 04:45:51 +05:30
|
|
|
strerror(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rv = xbps_pkgdb_foreach_cb(xhp, search_array_cb, &sd);
|
|
|
|
if (rv != 0) {
|
2023-02-09 15:24:36 +05:30
|
|
|
xbps_error_printf("Failed to initialize pkgdb: %s\n",
|
2014-09-11 04:45:51 +05:30
|
|
|
strerror(rv));
|
|
|
|
return 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);
|
|
|
|
}
|
2015-01-15 01:23:01 +05:30
|
|
|
if (regex)
|
|
|
|
regfree(&sd.regexp);
|
2013-02-11 20:48:43 +05:30
|
|
|
|
2012-11-02 19:34:25 +05:30
|
|
|
return rv;
|
|
|
|
}
|