summaryrefslogtreecommitdiff
path: root/apt-pkg/packageset.cc
blob: baa1c211b0a2d3dd533ea4e82f8569fc0e692404 (plain)
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
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
/* ######################################################################

   Simple wrapper around a std::set to provide a similar interface to
   a set of packages as to the complete set of all packages in the
   pkgCache.

   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#include <apt-pkg/aptconfiguration.h>
#include <apt-pkg/error.h>
#include <apt-pkg/packageset.h>
#include <apt-pkg/strutl.h>

#include <apti18n.h>

#include <vector>

#include <regex.h>
									/*}}}*/
namespace APT {
// FromRegEx - Return all packages in the cache matching a pattern	/*{{{*/
PackageSet PackageSet::FromRegEx(pkgCache &Cache, const char * const pattern, std::ostream &out) {
	PackageSet pkgset;

	const char * I;
	for (I = pattern; *I != 0; I++)
		if (*I == '.' || *I == '?' || *I == '+' || *I == '*' ||
		    *I == '|' || *I == '[' || *I == '^' || *I == '$')
			break;
	if (*I == 0)
		return pkgset;

	regex_t Pattern;
	int Res;
	if ((Res = regcomp(&Pattern, pattern , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
		char Error[300];
		regerror(Res, &Pattern, Error, sizeof(Error));
		_error->Error(_("Regex compilation error - %s"), Error);
		return pkgset;
	}

	for (pkgCache::GrpIterator Grp = Cache.GrpBegin(); Grp.end() == false; ++Grp)
	{
		if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
			continue;
		pkgCache::PkgIterator Pkg = Grp.FindPkg("native");
		if (Pkg.end() == true) {
			std::vector<std::string> archs = APT::Configuration::getArchitectures();
			for (std::vector<std::string>::const_iterator a = archs.begin();
			     a != archs.end() || Pkg.end() != true; ++a) {
				Pkg = Grp.FindPkg(*a);
			}
			if (Pkg.end() == true)
				continue;
		}

		ioprintf(out, _("Note, selecting %s for regex '%s'\n"),
			 Pkg.FullName(true).c_str(), pattern);

		pkgset.insert(Pkg);
	}

	regfree(&Pattern);

	return pkgset;
}
									/*}}}*/
// FromCommandLine - Return all packages specified on commandline	/*{{{*/
PackageSet PackageSet::FromCommandLine(pkgCache &Cache, const char **cmdline, std::ostream &out) {
	PackageSet pkgset;
	for (const char **I = cmdline + 1; *I != 0; I++) {
		pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
		if (Pkg.end() == true) {
			std::vector<std::string> archs = APT::Configuration::getArchitectures();
			for (std::vector<std::string>::const_iterator a = archs.begin();
			     a != archs.end() || Pkg.end() != true; ++a) {
				Pkg = Cache.FindPkg(*I, *a);
			}
			if (Pkg.end() == true) {
				PackageSet regex = FromRegEx(Cache, *I, out);
				if (regex.empty() == true)
					_error->Warning(_("Unable to locate package %s"),*I);
				else
					pkgset.insert(regex.begin(), regex.end());
				continue;
			}
		}
		pkgset.insert(Pkg);
	}
	return pkgset;
}
									/*}}}*/
}