2009-03-07 01:33:31 +00:00
|
|
|
/* info.c - Alpine Package Keeper (APK)
|
|
|
|
*
|
|
|
|
* 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 {
|
2010-05-19 13:48:40 +00:00
|
|
|
int (*match)(struct apk_package *pkg, const char *str);
|
|
|
|
int (*print)(struct apk_package *pkg);
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
2009-03-07 01:33:31 +00:00
|
|
|
};
|
|
|
|
|
2010-05-19 13:48:40 +00:00
|
|
|
static int print_match(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);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-19 13:48:40 +00:00
|
|
|
static int print_rdepends(struct apk_package *pkg)
|
2009-03-07 01:33:31 +00:00
|
|
|
{
|
2010-05-19 13:48:40 +00:00
|
|
|
struct apk_name *name, *name0;
|
|
|
|
struct apk_package *pkg0;
|
|
|
|
struct apk_dependency *dep;
|
|
|
|
int i, j, k;
|
2009-03-07 01:33:31 +00:00
|
|
|
|
2010-05-19 13:48:40 +00:00
|
|
|
name = pkg->name;
|
2009-03-07 01:33:31 +00:00
|
|
|
|
2010-12-14 17:51:16 +00:00
|
|
|
printf(PKG_VER_FMT ":", PKG_VER_PRINTF(pkg));
|
2010-05-19 13:48:40 +00:00
|
|
|
for (i = 0; i < name->rdepends->num; i++) {
|
|
|
|
name0 = name->rdepends->item[i];
|
|
|
|
for (j = 0; j < name0->pkgs->num; j++) {
|
|
|
|
pkg0 = name0->pkgs->item[j];
|
|
|
|
for (k = 0; k < pkg0->depends->num; k++) {
|
|
|
|
dep = &pkg0->depends->item[k];
|
|
|
|
if (name == dep->name &&
|
2011-01-01 14:58:58 +00:00
|
|
|
apk_dep_is_satisfied(dep, pkg)) {
|
2010-12-14 17:51:16 +00:00
|
|
|
printf(" " PKG_VER_FMT,
|
|
|
|
PKG_VER_PRINTF(pkg0));
|
2010-05-19 13:48:40 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-07 01:33:31 +00:00
|
|
|
}
|
2010-05-19 13:48:40 +00:00
|
|
|
}
|
|
|
|
printf("\n");
|
2009-03-07 01:33:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-19 13:48:40 +00:00
|
|
|
static int search_pkgname(struct apk_package *pkg, const char *str)
|
2009-03-07 01:33:31 +00:00
|
|
|
{
|
2010-05-19 13:48:40 +00:00
|
|
|
return fnmatch(str, pkg->name->name, 0) == 0;
|
2009-03-07 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-19 13:48:40 +00:00
|
|
|
static int search_desc(struct apk_package *pkg, const char *str)
|
2009-03-07 01:33:31 +00:00
|
|
|
{
|
2010-05-19 13:48:40 +00:00
|
|
|
return strstr(pkg->name->name, str) != NULL ||
|
|
|
|
strstr(pkg->description, str) != NULL;
|
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) {
|
|
|
|
case 'd':
|
2010-05-19 13:48:40 +00:00
|
|
|
ictx->match = search_desc;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
ictx->print = print_rdepends;
|
2009-03-07 01:33:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-19 13:48:40 +00:00
|
|
|
static int match_packages(apk_hash_item item, void *ctx)
|
|
|
|
{
|
|
|
|
struct search_ctx *ictx = (struct search_ctx *) ctx;
|
|
|
|
struct apk_package *pkg = (struct apk_package *) item;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ictx->argc; i++)
|
|
|
|
if (ictx->match(pkg, ictx->argv[i]))
|
|
|
|
break;
|
|
|
|
if (ictx->argc == 0 || i < ictx->argc)
|
|
|
|
ictx->print(pkg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-06 11:25:03 +00:00
|
|
|
static int search_main(void *ctx, struct apk_database *db, int argc, char **argv)
|
2009-03-07 01:33:31 +00:00
|
|
|
{
|
|
|
|
struct search_ctx *ictx = (struct search_ctx *) ctx;
|
2010-05-19 13:48:40 +00:00
|
|
|
struct apk_name *name;
|
|
|
|
int rc = 0, i, j, slow_search;
|
|
|
|
|
|
|
|
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 == NULL)
|
|
|
|
ictx->print = print_match;
|
|
|
|
else if (argc == 0)
|
|
|
|
return -1;
|
2009-03-07 01:33:31 +00:00
|
|
|
|
2010-05-19 13:48:40 +00:00
|
|
|
if (slow_search) {
|
|
|
|
ictx->argc = argc;
|
|
|
|
ictx->argv = argv;
|
|
|
|
rc = apk_hash_foreach(&db->available.packages,
|
|
|
|
match_packages, ictx);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
name = apk_db_query_name(db, APK_BLOB_STR(argv[i]));
|
2010-06-05 09:06:41 +00:00
|
|
|
if (name == NULL)
|
2010-05-19 13:48:40 +00:00
|
|
|
continue;
|
|
|
|
for (j = 0; j < name->pkgs->num; j++)
|
|
|
|
ictx->print(name->pkgs->item[j]);
|
|
|
|
}
|
|
|
|
}
|
2009-03-07 01:33:31 +00:00
|
|
|
|
2010-05-19 13:48:40 +00:00
|
|
|
return rc;
|
2009-03-07 01:33:31 +00:00
|
|
|
}
|
|
|
|
|
2009-06-25 12:14:07 +00:00
|
|
|
static struct apk_option search_options[] = {
|
2010-05-19 13:48:40 +00:00
|
|
|
{ 'd', "description", "Search also package descriptions" },
|
|
|
|
{ 'r', "rdepends", "Print reverse dependencies of package" },
|
2009-03-07 01:33:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct apk_applet apk_search = {
|
|
|
|
.name = "search",
|
2010-05-19 13:48:40 +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);
|
|
|
|
|