]> git.saurik.com Git - apt.git/blame - apt-pkg/packageset.cc
move cmdline parsing from apt-cache to new FromCommandLine method
[apt.git] / apt-pkg / packageset.cc
CommitLineData
ffee1c2b
DK
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 Simple wrapper around a std::set to provide a similar interface to
6 a set of packages as to the complete set of all packages in the
7 pkgCache.
8
9 ##################################################################### */
10 /*}}}*/
11// Include Files /*{{{*/
78c32596 12#include <apt-pkg/aptconfiguration.h>
ffee1c2b
DK
13#include <apt-pkg/error.h>
14#include <apt-pkg/packageset.h>
15#include <apt-pkg/strutl.h>
16
17#include <apti18n.h>
18
78c32596
DK
19#include <vector>
20
ffee1c2b
DK
21#include <regex.h>
22 /*}}}*/
23namespace APT {
24// FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
25PackageSet PackageSet::FromRegEx(pkgCache &Cache, const char * const pattern, std::ostream &out) {
26 PackageSet pkgset;
27
28 const char * I;
29 for (I = pattern; *I != 0; I++)
30 if (*I == '.' || *I == '?' || *I == '+' || *I == '*' ||
31 *I == '|' || *I == '[' || *I == '^' || *I == '$')
32 break;
33 if (*I == 0)
34 return pkgset;
35
36 regex_t Pattern;
37 int Res;
38 if ((Res = regcomp(&Pattern, pattern , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) {
39 char Error[300];
40 regerror(Res, &Pattern, Error, sizeof(Error));
41 _error->Error(_("Regex compilation error - %s"), Error);
42 return pkgset;
43 }
44
45 for (pkgCache::GrpIterator Grp = Cache.GrpBegin(); Grp.end() == false; ++Grp)
46 {
47 if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
48 continue;
49 pkgCache::PkgIterator Pkg = Grp.FindPkg("native");
78c32596
DK
50 if (Pkg.end() == true) {
51 std::vector<std::string> archs = APT::Configuration::getArchitectures();
52 for (std::vector<std::string>::const_iterator a = archs.begin();
53 a != archs.end() || Pkg.end() != true; ++a) {
54 Pkg = Grp.FindPkg(*a);
55 }
56 if (Pkg.end() == true)
57 continue;
58 }
ffee1c2b
DK
59
60 ioprintf(out, _("Note, selecting %s for regex '%s'\n"),
78c32596 61 Pkg.FullName(true).c_str(), pattern);
ffee1c2b
DK
62
63 pkgset.insert(Pkg);
64 }
65
66 regfree(&Pattern);
67
78c32596
DK
68 return pkgset;
69}
70 /*}}}*/
71// FromCommandLine - Return all packages specified on commandline /*{{{*/
72PackageSet PackageSet::FromCommandLine(pkgCache &Cache, const char **cmdline, std::ostream &out) {
73 PackageSet pkgset;
74 for (const char **I = cmdline + 1; *I != 0; I++) {
75 pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
76 if (Pkg.end() == true) {
77 std::vector<std::string> archs = APT::Configuration::getArchitectures();
78 for (std::vector<std::string>::const_iterator a = archs.begin();
79 a != archs.end() || Pkg.end() != true; ++a) {
80 Pkg = Cache.FindPkg(*I, *a);
81 }
82 if (Pkg.end() == true) {
83 PackageSet regex = FromRegEx(Cache, *I, out);
84 if (regex.empty() == true)
85 _error->Warning(_("Unable to locate package %s"),*I);
86 else
87 pkgset.insert(regex.begin(), regex.end());
88 continue;
89 }
90 }
91 pkgset.insert(Pkg);
92 }
ffee1c2b
DK
93 return pkgset;
94}
95 /*}}}*/
96}