2013-06-27 19:28:46 +00:00
|
|
|
/* search.c - Alpine Package Keeper (APK)
|
2009-03-07 01:33:31 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2009 Natanael Copa <n@tanael.org>
|
2011-09-13 08:53:01 +00:00
|
|
|
* Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
|
2009-03-07 01:33:31 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 as published
|
|
|
|
* by the Free Software Foundation. See http://www.gnu.org/ for details.
|
|
|
|
*/
|
|
|
|
|
2009-04-03 12:15:18 +00:00
|
|
|
#include <fnmatch.h>
|
2009-03-07 01:33:31 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "apk_defines.h"
|
|
|
|
#include "apk_applet.h"
|
|
|
|
#include "apk_package.h"
|
|
|
|
#include "apk_database.h"
|
|
|
|
|
|
|
|
struct search_ctx {
|
2011-09-14 08:56:44 +00:00
|
|
|
void (*print_result)(struct search_ctx *ctx, struct apk_package *pkg);
|
|
|
|
void (*print_package)(struct search_ctx *ctx, struct apk_package *pkg);
|
2011-09-14 12:26:43 +00:00
|
|
|
|
|
|
|
int show_all : 1;
|
|
|
|
int search_exact : 1;
|
|
|
|
int search_description : 1;
|
2013-08-28 14:17:07 +00:00
|
|
|
int search_origin : 1;
|
2011-09-14 12:26:43 +00:00
|
|
|
|
2013-06-15 17:39:05 +00:00
|
|
|
unsigned int matches;
|
2013-06-18 10:01:51 +00:00
|
|
|
struct apk_string_array *filter;
|
2009-03-07 01:33:31 +00:00
|
|
|
};
|
|
|
|
|
2011-09-14 08:56:44 +00:00
|
|
|
static void print_package_name(struct search_ctx *ctx, struct apk_package *pkg)
|
2009-03-07 01:33:31 +00:00
|
|
|
{
|
|
|
|
printf("%s", pkg->name->name);
|
|
|
|
if (apk_verbosity > 0)
|
2010-12-14 17:51:16 +00:00
|
|
|
printf("-" BLOB_FMT, BLOB_PRINTF(*pkg->version));
|
|
|
|
if (apk_verbosity > 1)
|
2009-03-07 01:33:31 +00:00
|
|
|
printf(" - %s", pkg->description);
|
2013-06-13 18:12:40 +00:00
|
|
|
printf("\n");
|
2011-09-14 08:56:44 +00:00
|
|
|
}
|
2009-03-07 01:33:31 +00:00
|
|
|
|
2011-09-14 08:56:44 +00:00
|
|
|
static void print_origin_name(struct search_ctx *ctx, struct apk_package *pkg)
|
|
|
|
{
|
|
|
|
if (pkg->origin != NULL)
|
|
|
|
printf(BLOB_FMT, BLOB_PRINTF(*pkg->origin));
|
|
|
|
else
|
|
|
|
printf("%s", pkg->name->name);
|
|
|
|
if (apk_verbosity > 0)
|
|
|
|
printf("-" BLOB_FMT, BLOB_PRINTF(*pkg->version));
|
2013-06-13 18:12:40 +00:00
|
|
|
printf("\n");
|
2009-03-07 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2013-06-13 18:12:40 +00:00
|
|
|
static void print_rdep_pkg(struct apk_package *pkg0, struct apk_dependency *dep0, struct apk_package *pkg, void *pctx)
|
2009-03-07 01:33:31 +00:00
|
|
|
{
|
2013-06-13 18:12:40 +00:00
|
|
|
struct search_ctx *ctx = (struct search_ctx *) pctx;
|
|
|
|
ctx->print_package(ctx, pkg0);
|
|
|
|
}
|
2009-03-07 01:33:31 +00:00
|
|
|
|
2013-06-13 18:12:40 +00:00
|
|
|
static void print_rdepends(struct search_ctx *ctx, struct apk_package *pkg)
|
|
|
|
{
|
|
|
|
if (apk_verbosity > 0) {
|
2013-06-15 17:39:05 +00:00
|
|
|
ctx->matches = apk_foreach_genid() | APK_DEP_SATISFIES;
|
2013-06-13 18:12:40 +00:00
|
|
|
printf(PKG_VER_FMT " is required by:\n", PKG_VER_PRINTF(pkg));
|
2010-05-19 13:48:40 +00:00
|
|
|
}
|
2013-06-15 17:39:05 +00:00
|
|
|
apk_pkg_foreach_reverse_dependency(pkg, ctx->matches, print_rdep_pkg, ctx);
|
2009-03-07 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2009-08-06 11:25:03 +00:00
|
|
|
static int search_parse(void *ctx, struct apk_db_options *dbopts,
|
|
|
|
int optch, int optindex, const char *optarg)
|
2009-03-07 01:33:31 +00:00
|
|
|
{
|
|
|
|
struct search_ctx *ictx = (struct search_ctx *) ctx;
|
|
|
|
|
|
|
|
switch (optch) {
|
2011-09-14 12:26:43 +00:00
|
|
|
case 'a':
|
|
|
|
ictx->show_all = 1;
|
|
|
|
break;
|
2009-03-07 01:33:31 +00:00
|
|
|
case 'd':
|
2011-09-14 12:26:43 +00:00
|
|
|
ictx->search_description = 1;
|
|
|
|
ictx->search_exact = 1;
|
|
|
|
ictx->show_all = 1;
|
2010-05-19 13:48:40 +00:00
|
|
|
break;
|
2011-09-14 12:26:43 +00:00
|
|
|
case 'e':
|
2013-06-30 08:02:18 +00:00
|
|
|
case 'x':
|
2011-09-14 12:26:43 +00:00
|
|
|
ictx->search_exact = 1;
|
2011-09-14 08:56:44 +00:00
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
ictx->print_package = print_origin_name;
|
2009-03-07 01:33:31 +00:00
|
|
|
break;
|
2011-09-14 12:26:43 +00:00
|
|
|
case 'r':
|
|
|
|
ictx->print_result = print_rdepends;
|
|
|
|
break;
|
2013-08-28 14:17:07 +00:00
|
|
|
case 0x10000:
|
|
|
|
ictx->search_origin = 1;
|
|
|
|
ictx->search_exact = 1;
|
|
|
|
ictx->show_all = 1;
|
|
|
|
break;
|
2009-03-07 01:33:31 +00:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-13 18:18:40 +00:00
|
|
|
static void print_result_pkg(struct search_ctx *ctx, struct apk_package *pkg)
|
2011-09-14 12:26:43 +00:00
|
|
|
{
|
2013-06-18 10:01:51 +00:00
|
|
|
char **pmatch;
|
2011-09-14 12:26:43 +00:00
|
|
|
|
|
|
|
if (ctx->search_description) {
|
2013-06-18 10:01:51 +00:00
|
|
|
foreach_array_item(pmatch, ctx->filter) {
|
|
|
|
if (strstr(*pmatch, pkg->description) != NULL ||
|
|
|
|
strstr(*pmatch, pkg->name->name) != NULL)
|
|
|
|
goto match;
|
2011-09-14 12:26:43 +00:00
|
|
|
}
|
2013-06-18 10:01:51 +00:00
|
|
|
return;
|
2011-09-14 12:26:43 +00:00
|
|
|
}
|
2013-08-28 14:17:07 +00:00
|
|
|
if (ctx->search_origin) {
|
|
|
|
foreach_array_item(pmatch, ctx->filter) {
|
|
|
|
if (pkg->origin != NULL && strcmp(*pmatch, apk_blob_cstr(*pkg->origin)) == 0)
|
|
|
|
goto match;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2013-06-18 10:01:51 +00:00
|
|
|
match:
|
2011-09-14 12:26:43 +00:00
|
|
|
ctx->print_result(ctx, pkg);
|
|
|
|
}
|
|
|
|
|
2013-06-18 10:01:51 +00:00
|
|
|
static void print_result(struct apk_database *db, const char *match, struct apk_name *name, void *pctx)
|
2010-05-19 13:48:40 +00:00
|
|
|
{
|
2013-06-18 10:01:51 +00:00
|
|
|
struct search_ctx *ctx = pctx;
|
2013-06-14 17:26:48 +00:00
|
|
|
struct apk_provider *p;
|
|
|
|
struct apk_package *pkg = NULL;
|
2010-05-19 13:48:40 +00:00
|
|
|
|
2013-06-13 18:12:40 +00:00
|
|
|
if (ctx->show_all) {
|
2013-06-14 17:26:48 +00:00
|
|
|
foreach_array_item(p, name->providers)
|
|
|
|
print_result_pkg(ctx, p->pkg);
|
2011-09-14 12:26:43 +00:00
|
|
|
} else {
|
2013-06-13 18:12:40 +00:00
|
|
|
foreach_array_item(p, name->providers) {
|
2013-06-18 10:01:51 +00:00
|
|
|
if (pkg == NULL ||
|
|
|
|
apk_version_compare_blob(*p->version, *pkg->version) == APK_VERSION_GREATER)
|
2013-06-13 18:12:40 +00:00
|
|
|
pkg = p->pkg;
|
2011-09-14 12:26:43 +00:00
|
|
|
}
|
2013-06-18 10:01:51 +00:00
|
|
|
if (pkg)
|
|
|
|
print_result_pkg(ctx, pkg);
|
2011-09-14 12:26:43 +00:00
|
|
|
}
|
2013-06-13 18:18:40 +00:00
|
|
|
}
|
|
|
|
|
2013-06-18 10:01:51 +00:00
|
|
|
static int print_pkg(apk_hash_item item, void *pctx)
|
2013-06-13 18:18:40 +00:00
|
|
|
{
|
2013-06-18 10:01:51 +00:00
|
|
|
print_result_pkg((struct search_ctx *) pctx, (struct apk_package *) item);
|
2010-05-19 13:48:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-18 10:01:51 +00:00
|
|
|
static int search_main(void *pctx, struct apk_database *db, struct apk_string_array *args)
|
2009-03-07 01:33:31 +00:00
|
|
|
{
|
2013-06-13 18:12:40 +00:00
|
|
|
struct search_ctx *ctx = (struct search_ctx *) pctx;
|
2013-06-18 10:01:51 +00:00
|
|
|
char *tmp, **pmatch;
|
2010-05-19 13:48:40 +00:00
|
|
|
|
2013-06-18 10:01:51 +00:00
|
|
|
ctx->filter = args;
|
2013-06-15 17:39:05 +00:00
|
|
|
ctx->matches = apk_foreach_genid() | APK_DEP_SATISFIES;
|
2013-06-13 18:12:40 +00:00
|
|
|
if (ctx->print_package == NULL)
|
|
|
|
ctx->print_package = print_package_name;
|
|
|
|
if (ctx->print_result == NULL)
|
|
|
|
ctx->print_result = ctx->print_package;
|
2011-09-14 12:26:43 +00:00
|
|
|
|
2013-08-28 14:17:07 +00:00
|
|
|
if (ctx->search_description || ctx->search_origin)
|
2013-06-18 10:01:51 +00:00
|
|
|
return apk_hash_foreach(&db->available.packages, print_pkg, ctx);
|
2009-03-07 01:33:31 +00:00
|
|
|
|
2013-06-13 18:12:40 +00:00
|
|
|
if (!ctx->search_exact) {
|
2013-06-18 10:01:51 +00:00
|
|
|
foreach_array_item(pmatch, ctx->filter) {
|
|
|
|
tmp = alloca(strlen(*pmatch) + 3);
|
|
|
|
sprintf(tmp, "*%s*", *pmatch);
|
|
|
|
*pmatch = tmp;
|
2013-06-13 18:18:40 +00:00
|
|
|
}
|
2010-05-19 13:48:40 +00:00
|
|
|
}
|
2013-06-19 17:55:01 +00:00
|
|
|
apk_name_foreach_matching(
|
|
|
|
db, args, APK_FOREACH_NULL_MATCHES_ALL | apk_foreach_genid(),
|
|
|
|
print_result, ctx);
|
2013-06-18 10:01:51 +00:00
|
|
|
return 0;
|
2009-03-07 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2009-06-25 12:14:07 +00:00
|
|
|
static struct apk_option search_options[] = {
|
2011-09-14 12:26:43 +00:00
|
|
|
{ 'a', "all", "Show all package versions (instead of latest only)" },
|
|
|
|
{ 'd', "description", "Search package descriptions (implies -a)" },
|
2013-06-30 08:02:18 +00:00
|
|
|
{ 'x', "exact", "Require exact match (instead of substring match)" },
|
|
|
|
{ 'e', NULL, "Synonym for -x (deprecated)" },
|
2011-09-14 08:56:44 +00:00
|
|
|
{ 'o', "origin", "Print origin package name instead of the subpackage" },
|
2011-09-14 12:26:43 +00:00
|
|
|
{ 'r', "rdepends", "Print reverse dependencies of package" },
|
2013-08-28 14:17:07 +00:00
|
|
|
{ 0x10000, "has-origin","List packages that have the given origin" },
|
2009-03-07 01:33:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct apk_applet apk_search = {
|
|
|
|
.name = "search",
|
2013-06-19 13:43:51 +00:00
|
|
|
.help = "Search package by PATTERNs or by indexed dependencies",
|
2009-06-25 12:14:07 +00:00
|
|
|
.arguments = "PATTERN",
|
2009-08-06 11:25:03 +00:00
|
|
|
.open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE,
|
2009-03-07 01:33:31 +00:00
|
|
|
.context_size = sizeof(struct search_ctx),
|
|
|
|
.num_options = ARRAY_SIZE(search_options),
|
|
|
|
.options = search_options,
|
|
|
|
.parse = search_parse,
|
|
|
|
.main = search_main,
|
|
|
|
};
|
|
|
|
|
|
|
|
APK_DEFINE_APPLET(apk_search);
|