search: implement --exact and --all
also optimize search to happen for enumeration of package names. fixes #39, fixes #560cute-signatures
parent
3197d0f64a
commit
4803444731
129
src/search.c
129
src/search.c
|
@ -17,9 +17,13 @@
|
||||||
#include "apk_database.h"
|
#include "apk_database.h"
|
||||||
|
|
||||||
struct search_ctx {
|
struct search_ctx {
|
||||||
int (*match)(struct apk_package *pkg, const char *str);
|
|
||||||
void (*print_result)(struct search_ctx *ctx, struct apk_package *pkg);
|
void (*print_result)(struct search_ctx *ctx, struct apk_package *pkg);
|
||||||
void (*print_package)(struct search_ctx *ctx, struct apk_package *pkg);
|
void (*print_package)(struct search_ctx *ctx, struct apk_package *pkg);
|
||||||
|
|
||||||
|
int show_all : 1;
|
||||||
|
int search_exact : 1;
|
||||||
|
int search_description : 1;
|
||||||
|
|
||||||
int argc;
|
int argc;
|
||||||
char **argv;
|
char **argv;
|
||||||
};
|
};
|
||||||
|
@ -69,103 +73,122 @@ static void print_rdepends(struct search_ctx *ctx, struct apk_package *pkg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int search_pkgname(struct apk_package *pkg, const char *str)
|
|
||||||
{
|
|
||||||
return fnmatch(str, pkg->name->name, 0) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int search_desc(struct apk_package *pkg, const char *str)
|
|
||||||
{
|
|
||||||
return strstr(pkg->name->name, str) != NULL ||
|
|
||||||
strstr(pkg->description, str) != NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int search_parse(void *ctx, struct apk_db_options *dbopts,
|
static int search_parse(void *ctx, struct apk_db_options *dbopts,
|
||||||
int optch, int optindex, const char *optarg)
|
int optch, int optindex, const char *optarg)
|
||||||
{
|
{
|
||||||
struct search_ctx *ictx = (struct search_ctx *) ctx;
|
struct search_ctx *ictx = (struct search_ctx *) ctx;
|
||||||
|
|
||||||
switch (optch) {
|
switch (optch) {
|
||||||
case 'd':
|
case 'a':
|
||||||
ictx->match = search_desc;
|
ictx->show_all = 1;
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'd':
|
||||||
ictx->print_result = print_rdepends;
|
ictx->search_description = 1;
|
||||||
|
ictx->search_exact = 1;
|
||||||
|
ictx->show_all = 1;
|
||||||
|
break;
|
||||||
|
case 'e':
|
||||||
|
ictx->search_exact = 1;
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
ictx->print_package = print_origin_name;
|
ictx->print_package = print_origin_name;
|
||||||
break;
|
break;
|
||||||
|
case 'r':
|
||||||
|
ictx->print_result = print_rdepends;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int match_packages(apk_hash_item item, void *ctx)
|
static void print_result(struct search_ctx *ctx, struct apk_package *pkg)
|
||||||
{
|
{
|
||||||
struct search_ctx *ictx = (struct search_ctx *) ctx;
|
|
||||||
struct apk_package *pkg = (struct apk_package *) item;
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < ictx->argc; i++)
|
if (pkg == NULL)
|
||||||
if (ictx->match(pkg, ictx->argv[i]))
|
return;
|
||||||
|
|
||||||
|
if (ctx->search_description) {
|
||||||
|
for (i = 0; i < ctx->argc; i++) {
|
||||||
|
if (strstr(pkg->description, ctx->argv[i]) != NULL ||
|
||||||
|
strstr(pkg->name->name, ctx->argv[i]) != NULL)
|
||||||
break;
|
break;
|
||||||
if (ictx->argc == 0 || i < ictx->argc) {
|
}
|
||||||
ictx->print_result(ictx, pkg);
|
if (i >= ctx->argc)
|
||||||
printf("\n");
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctx->print_result(ctx, pkg);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static int match_names(apk_hash_item item, void *ctx)
|
||||||
|
{
|
||||||
|
struct search_ctx *ictx = (struct search_ctx *) ctx;
|
||||||
|
struct apk_name *name = (struct apk_name *) item;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (!ictx->search_description) {
|
||||||
|
for (i = 0; i < ictx->argc; i++)
|
||||||
|
if (fnmatch(ictx->argv[i], name->name, FNM_CASEFOLD) == 0)
|
||||||
|
break;
|
||||||
|
if (ictx->argc > 0 && i >= ictx->argc)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ictx->show_all) {
|
||||||
|
for (i = 0; i < name->pkgs->num; i++)
|
||||||
|
print_result(ictx, name->pkgs->item[i]);
|
||||||
|
} else {
|
||||||
|
struct apk_package *pkg = NULL;
|
||||||
|
|
||||||
|
for (i = 0; i < name->pkgs->num; i++) {
|
||||||
|
if (pkg == NULL ||
|
||||||
|
apk_pkg_version_compare(name->pkgs->item[i], pkg) == APK_VERSION_GREATER)
|
||||||
|
pkg = name->pkgs->item[i];
|
||||||
|
}
|
||||||
|
print_result(ictx, pkg);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int search_main(void *ctx, struct apk_database *db, int argc, char **argv)
|
static int search_main(void *ctx, struct apk_database *db, int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct search_ctx *ictx = (struct search_ctx *) ctx;
|
struct search_ctx *ictx = (struct search_ctx *) ctx;
|
||||||
struct apk_name *name;
|
char s[256];
|
||||||
int rc = 0, i, j, slow_search;
|
int i, l;
|
||||||
|
|
||||||
slow_search = ictx->match != NULL || argc == 0;
|
|
||||||
if (!slow_search) {
|
|
||||||
for (i = 0; i < argc; i++)
|
|
||||||
if (strcspn(argv[i], "*?[") != strlen(argv[i])) {
|
|
||||||
slow_search = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ictx->match == NULL)
|
|
||||||
ictx->match = search_pkgname;
|
|
||||||
if (ictx->print_package == NULL)
|
if (ictx->print_package == NULL)
|
||||||
ictx->print_package = print_package_name;
|
ictx->print_package = print_package_name;
|
||||||
if (ictx->print_result == NULL)
|
if (ictx->print_result == NULL)
|
||||||
ictx->print_result = ictx->print_package;
|
ictx->print_result = ictx->print_package;
|
||||||
else if (argc == 0)
|
|
||||||
|
if (argc == 0 && ictx->search_description)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (slow_search) {
|
|
||||||
ictx->argc = argc;
|
ictx->argc = argc;
|
||||||
ictx->argv = argv;
|
if (!ictx->search_exact) {
|
||||||
rc = apk_hash_foreach(&db->available.packages,
|
ictx->argv = alloca(argc * sizeof(char*));
|
||||||
match_packages, ictx);
|
|
||||||
} else {
|
|
||||||
for (i = 0; i < argc; i++) {
|
for (i = 0; i < argc; i++) {
|
||||||
name = apk_db_query_name(db, APK_BLOB_STR(argv[i]));
|
l = snprintf(s, sizeof(s), "*%s*", argv[i]);
|
||||||
if (name == NULL)
|
ictx->argv[i] = alloca(l+1);
|
||||||
continue;
|
memcpy(ictx->argv[i], s, l);
|
||||||
for (j = 0; j < name->pkgs->num; j++) {
|
ictx->argv[i][l] = 0;
|
||||||
ictx->print_result(ctx, name->pkgs->item[j]);
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ictx->argv = argv;
|
||||||
}
|
}
|
||||||
|
|
||||||
return rc;
|
return apk_hash_foreach(&db->available.names, match_names, ictx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct apk_option search_options[] = {
|
static struct apk_option search_options[] = {
|
||||||
{ 'd', "description", "Search also package descriptions" },
|
{ 'a', "all", "Show all package versions (instead of latest only)" },
|
||||||
{ 'r', "rdepends", "Print reverse dependencies of package" },
|
{ 'd', "description", "Search package descriptions (implies -a)" },
|
||||||
|
{ 'e', "exact", "Require exact match (instead of substring match)" },
|
||||||
{ 'o', "origin", "Print origin package name instead of the subpackage" },
|
{ 'o', "origin", "Print origin package name instead of the subpackage" },
|
||||||
|
{ 'r', "rdepends", "Print reverse dependencies of package" },
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct apk_applet apk_search = {
|
static struct apk_applet apk_search = {
|
||||||
|
|
Loading…
Reference in New Issue