summaryrefslogtreecommitdiff
path: root/src/search.c
blob: 91c291371e576f6bd673a800817ae3453c269be5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* info.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2009 Natanael Copa <n@tanael.org>
 * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi>
 * 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.
 */

#include <fnmatch.h>
#include <stdio.h>
#include "apk_defines.h"
#include "apk_applet.h"
#include "apk_package.h"
#include "apk_database.h"
#include "apk_state.h"

struct search_ctx {
	int (*action)(struct apk_database *db, int argc, char **argv);
};

struct search_query_ctx {
	struct apk_database *db;
	const char *query;
};

static int search_list_print(apk_hash_item item, void *ctx)
{
	struct apk_package *pkg = (struct apk_package *) item;

	printf("%s", pkg->name->name);
	if (apk_verbosity > 0)
		printf("-%s", pkg->version);
	if (apk_verbosity > 1) {
		printf(" - %s", pkg->description);
	}
	printf("\n");

	return 0;
}

static int search_query_print(apk_hash_item item, void *ctx)
{
	struct search_query_ctx *ictx = (struct search_query_ctx *) ctx;
	struct apk_package *pkg = (struct apk_package *) item;

	if (fnmatch(ictx->query, pkg->name->name, 0) != 0)
		return 0;
	search_list_print(item, ictx->db);

	return 0;
}

static int search_list(struct apk_database *db, int argc, char **argv)
{
	int i;
	struct search_query_ctx ctx;

	ctx.db = db;

	if (argc == 0)
		apk_hash_foreach(&db->available.packages, search_list_print, db);
	else
		for (i=0; i<argc; i++) {
			ctx.query = argv[i];
			apk_hash_foreach(&db->available.packages, search_query_print, &ctx);
		}

	return 0;
}

static int search_query_desc_print(apk_hash_item item, void *ctx)
{
	struct search_query_ctx *ictx = (struct search_query_ctx *) ctx;
	struct apk_package *pkg = (struct apk_package *) item;

	if( strstr(pkg->description, ictx->query) == NULL )
		return 0;
	search_list_print(item, ictx->db);

	return 0;
}

static int search_desc(struct apk_database *db, int argc, char **argv)
{
	int i;
	struct search_query_ctx ctx;

	ctx.db = db;

	for (i=0; i<argc; i++) {
		ctx.query = argv[i];
		apk_hash_foreach(&db->available.packages, search_query_desc_print, &ctx);
	}

	return 0;
}

static int search_parse(void *ctx, struct apk_db_options *dbopts,
		        int optch, int optindex, const char *optarg)
{
	struct search_ctx *ictx = (struct search_ctx *) ctx;

	switch (optch) {
	case 'd':
		ictx->action = search_desc;
		break;
	default:
		return -1;
	}
	return 0;
}

static int search_main(void *ctx, struct apk_database *db, int argc, char **argv)
{
	struct search_ctx *ictx = (struct search_ctx *) ctx;

	if (ictx->action != NULL)
		return ictx->action(db, argc, argv);

	return search_list(db, argc, argv);
}

static struct apk_option search_options[] = {
	{ 'd', "description", "Search also package descriptions" },
};

static struct apk_applet apk_search = {
	.name = "search",
	.help = "Search package names (and descriptions) by wildcard PATTERN.",
	.arguments = "PATTERN",
	.open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE,
	.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);