Commit 48034447 authored by Timo Teräs's avatar Timo Teräs

search: implement --exact and --all

also optimize search to happen for enumeration of package names.
fixes #39, fixes #560
parent 3197d0f6
...@@ -17,9 +17,13 @@ ...@@ -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) ...@@ -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 'a':
ictx->show_all = 1;
break;
case 'd': case 'd':
ictx->match = search_desc; ictx->search_description = 1;
ictx->search_exact = 1;
ictx->show_all = 1;
break; break;
case 'r': case 'e':
ictx->print_result = print_rdepends; 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)
{
int i;
if (pkg == NULL)
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;
}
if (i >= ctx->argc)
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 search_ctx *ictx = (struct search_ctx *) ctx;
struct apk_package *pkg = (struct apk_package *) item; struct apk_name *name = (struct apk_name *) item;
int i; int i;
for (i = 0; i < ictx->argc; i++) if (!ictx->search_description) {
if (ictx->match(pkg, ictx->argv[i])) for (i = 0; i < ictx->argc; i++)
break; if (fnmatch(ictx->argv[i], name->name, FNM_CASEFOLD) == 0)
if (ictx->argc == 0 || i < ictx->argc) { break;
ictx->print_result(ictx, pkg); if (ictx->argc > 0 && i >= ictx->argc)
printf("\n"); 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; if (!ictx->search_exact) {
ictx->argv = argv; ictx->argv = alloca(argc * sizeof(char*));
rc = apk_hash_foreach(&db->available.packages,
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 = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment