search.c 4.61 KB
Newer Older
Cameron Banta's avatar
Cameron Banta committed
1 2 3
/* info.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2009 Natanael Copa <n@tanael.org>
4
 * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
Cameron Banta's avatar
Cameron Banta committed
5 6 7 8 9 10 11
 * 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.
 */

12
#include <fnmatch.h>
Cameron Banta's avatar
Cameron Banta committed
13 14 15 16 17 18 19
#include <stdio.h>
#include "apk_defines.h"
#include "apk_applet.h"
#include "apk_package.h"
#include "apk_database.h"

struct search_ctx {
20
	int (*match)(struct apk_package *pkg, const char *str);
21 22
	void (*print_result)(struct search_ctx *ctx, struct apk_package *pkg);
	void (*print_package)(struct search_ctx *ctx, struct apk_package *pkg);
23 24
	int argc;
	char **argv;
Cameron Banta's avatar
Cameron Banta committed
25 26
};

27
static void print_package_name(struct search_ctx *ctx, struct apk_package *pkg)
Cameron Banta's avatar
Cameron Banta committed
28 29 30
{
	printf("%s", pkg->name->name);
	if (apk_verbosity > 0)
31 32
		printf("-" BLOB_FMT, BLOB_PRINTF(*pkg->version));
	if (apk_verbosity > 1)
Cameron Banta's avatar
Cameron Banta committed
33
		printf(" - %s", pkg->description);
34
}
Cameron Banta's avatar
Cameron Banta committed
35

36 37 38 39 40 41 42 43
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));
Cameron Banta's avatar
Cameron Banta committed
44 45
}

46
static void print_rdepends(struct search_ctx *ctx, struct apk_package *pkg)
Cameron Banta's avatar
Cameron Banta committed
47
{
48 49 50 51
	struct apk_name *name, *name0;
	struct apk_package *pkg0;
	struct apk_dependency *dep;
	int i, j, k;
Cameron Banta's avatar
Cameron Banta committed
52

53
	name = pkg->name;
Cameron Banta's avatar
Cameron Banta committed
54

55
	printf(PKG_VER_FMT ":", PKG_VER_PRINTF(pkg));
56 57 58 59 60 61 62
	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 &&
63
				    apk_dep_is_satisfied(dep, pkg)) {
64 65
					printf(" ");
					ctx->print_package(ctx, pkg0);
66 67
				}
			}
Cameron Banta's avatar
Cameron Banta committed
68
		}
69
	}
Cameron Banta's avatar
Cameron Banta committed
70 71
}

72
static int search_pkgname(struct apk_package *pkg, const char *str)
Cameron Banta's avatar
Cameron Banta committed
73
{
74
	return fnmatch(str, pkg->name->name, 0) == 0;
Cameron Banta's avatar
Cameron Banta committed
75 76
}

77
static int search_desc(struct apk_package *pkg, const char *str)
Cameron Banta's avatar
Cameron Banta committed
78
{
79 80
	return  strstr(pkg->name->name, str) != NULL ||
		strstr(pkg->description, str) != NULL;
Cameron Banta's avatar
Cameron Banta committed
81 82
}

83 84
static int search_parse(void *ctx, struct apk_db_options *dbopts,
		        int optch, int optindex, const char *optarg)
Cameron Banta's avatar
Cameron Banta committed
85 86 87 88 89
{
	struct search_ctx *ictx = (struct search_ctx *) ctx;

	switch (optch) {
	case 'd':
90 91 92
		ictx->match = search_desc;
		break;
	case 'r':
93 94 95 96
		ictx->print_result = print_rdepends;
		break;
	case 'o':
		ictx->print_package = print_origin_name;
Cameron Banta's avatar
Cameron Banta committed
97 98 99 100 101 102 103
		break;
	default:
		return -1;
	}
	return 0;
}

104 105 106 107 108 109 110 111 112
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;
113 114 115 116
	if (ictx->argc == 0 || i < ictx->argc) {
		ictx->print_result(ictx, pkg);
		printf("\n");
	}
117 118 119 120

	return 0;
}

121
static int search_main(void *ctx, struct apk_database *db, int argc, char **argv)
Cameron Banta's avatar
Cameron Banta committed
122 123
{
	struct search_ctx *ictx = (struct search_ctx *) ctx;
124 125 126 127 128 129 130 131 132 133 134 135 136 137
	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;
138 139 140 141
	if (ictx->print_package == NULL)
		ictx->print_package = print_package_name;
	if (ictx->print_result == NULL)
		ictx->print_result = ictx->print_package;
142 143
	else if (argc == 0)
		return -1;
Cameron Banta's avatar
Cameron Banta committed
144

145 146 147 148 149 150 151 152
	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]));
Timo Teräs's avatar
Timo Teräs committed
153
			if (name == NULL)
154
				continue;
155 156 157 158
			for (j = 0; j < name->pkgs->num; j++) {
				ictx->print_result(ctx, name->pkgs->item[j]);
				printf("\n");
			}
159 160
		}
	}
Cameron Banta's avatar
Cameron Banta committed
161

162
	return rc;
Cameron Banta's avatar
Cameron Banta committed
163 164
}

Timo Teräs's avatar
Timo Teräs committed
165
static struct apk_option search_options[] = {
166 167
	{ 'd', "description",	"Search also package descriptions" },
	{ 'r', "rdepends",	"Print reverse dependencies of package" },
168
	{ 'o', "origin",	"Print origin package name instead of the subpackage" },
Cameron Banta's avatar
Cameron Banta committed
169 170 171 172
};

static struct apk_applet apk_search = {
	.name = "search",
173
	.help = "Search package by PATTERNs or by indexed dependencies.",
Timo Teräs's avatar
Timo Teräs committed
174
	.arguments = "PATTERN",
175
	.open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE,
Cameron Banta's avatar
Cameron Banta committed
176 177 178 179 180 181 182 183 184
	.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);