]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/cacheset.cc
adapt to the new CacheSetHelper API
[apt.git] / apt-pkg / cacheset.cc
index c0b06ba323c58b17d5dde95c9bcdf1c7b704c53d..3c38895e972a67338bf2588f9094d9f4d602d4a3 100644 (file)
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
+#include <config.h>
+
 #include <apt-pkg/aptconfiguration.h>
 #include <apt-pkg/aptconfiguration.h>
-#include <apt-pkg/error.h>
+#include <apt-pkg/cachefile.h>
+#include <apt-pkg/cachefilter.h>
 #include <apt-pkg/cacheset.h>
 #include <apt-pkg/cacheset.h>
-#include <apt-pkg/strutl.h>
+#include <apt-pkg/error.h>
 #include <apt-pkg/versionmatch.h>
 #include <apt-pkg/versionmatch.h>
+#include <apt-pkg/pkgrecords.h>
+#include <apt-pkg/policy.h>
+#include <apt-pkg/cacheiterators.h>
+#include <apt-pkg/configuration.h>
+#include <apt-pkg/depcache.h>
+#include <apt-pkg/macros.h>
+#include <apt-pkg/pkgcache.h>
+#include <apt-pkg/fileutl.h>
 
 
-#include <apti18n.h>
-
+#include <stddef.h>
+#include <stdio.h>
+#include <string.h>
+#include <regex.h>
+#include <list>
+#include <string>
 #include <vector>
 
 #include <vector>
 
-#include <regex.h>
+#include <apti18n.h>
                                                                        /*}}}*/
 namespace APT {
                                                                        /*}}}*/
 namespace APT {
-// FromRegEx - Return all packages in the cache matching a pattern     /*{{{*/
-PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, std::ostream &out) {
-       PackageSet pkgset;
+// FromTask - Return all packages in the cache from a specific task    /*{{{*/
+bool PackageContainerInterface::FromTask(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
+       size_t const archfound = pattern.find_last_of(':');
        std::string arch = "native";
        std::string arch = "native";
-       static const char * const isregex = ".?+*|[^$";
+       if (archfound != std::string::npos) {
+               arch = pattern.substr(archfound+1);
+               pattern.erase(archfound);
+       }
 
 
+       if (pattern[pattern.length() -1] != '^')
+               return false;
+       pattern.erase(pattern.length()-1);
+
+       if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0))
+               return false;
+
+       bool const wasEmpty = pci->empty();
+       if (wasEmpty == true)
+               pci->setConstructor(CacheSetHelper::TASK);
+
+       // get the records
+       pkgRecords Recs(Cache);
+
+       // build regexp for the task
+       regex_t Pattern;
+       char S[300];
+       snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", pattern.c_str());
+       if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0) {
+               _error->Error("Failed to compile task regexp");
+               return false;
+       }
+
+       bool found = false;
+       for (pkgCache::GrpIterator Grp = Cache->GrpBegin(); Grp.end() == false; ++Grp) {
+               pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
+               if (Pkg.end() == true)
+                       continue;
+               pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache);
+               if(ver.end() == true)
+                       continue;
+
+               pkgRecords::Parser &parser = Recs.Lookup(ver.FileList());
+               const char *start, *end;
+               parser.GetRec(start,end);
+               unsigned int const length = end - start;
+               if (unlikely(length == 0))
+                  continue;
+               char buf[length];
+               strncpy(buf, start, length);
+               buf[length-1] = '\0';
+               if (regexec(&Pattern, buf, 0, 0, 0) != 0)
+                       continue;
+
+               pci->insert(Pkg);
+               helper.showPackageSelection(Pkg, CacheSetHelper::TASK, pattern);
+               found = true;
+       }
+       regfree(&Pattern);
+
+       if (found == false) {
+               helper.canNotFindPackage(CacheSetHelper::TASK, pci, Cache, pattern);
+               pci->setConstructor(CacheSetHelper::UNKNOWN);
+               return false;
+       }
+
+       if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
+               pci->setConstructor(CacheSetHelper::UNKNOWN);
+
+       return true;
+}
+                                                                       /*}}}*/
+// FromRegEx - Return all packages in the cache matching a pattern     /*{{{*/
+bool PackageContainerInterface::FromRegEx(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) {
+       static const char * const isregex = ".?+*|[^$";
        if (pattern.find_first_of(isregex) == std::string::npos)
        if (pattern.find_first_of(isregex) == std::string::npos)
-               return pkgset;
+               return false;
+
+       bool const wasEmpty = pci->empty();
+       if (wasEmpty == true)
+               pci->setConstructor(CacheSetHelper::REGEX);
 
        size_t archfound = pattern.find_last_of(':');
 
        size_t archfound = pattern.find_last_of(':');
+       std::string arch = "native";
        if (archfound != std::string::npos) {
                arch = pattern.substr(archfound+1);
                if (arch.find_first_of(isregex) == std::string::npos)
        if (archfound != std::string::npos) {
                arch = pattern.substr(archfound+1);
                if (arch.find_first_of(isregex) == std::string::npos)
@@ -40,18 +128,14 @@ PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, std::
                        arch = "native";
        }
 
                        arch = "native";
        }
 
-       regex_t Pattern;
-       int Res;
-       if ((Res = regcomp(&Pattern, pattern.c_str() , 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;
-       }
+       if (unlikely(Cache.GetPkgCache() == 0))
+               return false;
 
 
-       for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp)
-       {
-               if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0)
+       APT::CacheFilter::PackageNameMatchesRegEx regexfilter(pattern);
+
+       bool found = false;
+       for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
+               if (regexfilter(Grp) == false)
                        continue;
                pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
                if (Pkg.end() == true) {
                        continue;
                pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
                if (Pkg.end() == true) {
@@ -65,29 +149,89 @@ PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, std::
                                continue;
                }
 
                                continue;
                }
 
-               ioprintf(out, _("Note, selecting %s for regex '%s'\n"),
-                        Pkg.FullName(true).c_str(), pattern.c_str());
+               pci->insert(Pkg);
+               helper.showPackageSelection(Pkg, CacheSetHelper::REGEX, pattern);
+               found = true;
+       }
 
 
-               pkgset.insert(Pkg);
+       if (found == false) {
+               helper.canNotFindPackage(CacheSetHelper::REGEX, pci, Cache, pattern);
+               pci->setConstructor(CacheSetHelper::UNKNOWN);
+               return false;
        }
 
        }
 
-       regfree(&Pattern);
+       if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
+               pci->setConstructor(CacheSetHelper::UNKNOWN);
 
 
-       return pkgset;
+       return true;
 }
                                                                        /*}}}*/
 }
                                                                        /*}}}*/
-// FromCommandLine - Return all packages specified on commandline      /*{{{*/
-PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out) {
-       PackageSet pkgset;
-       for (const char **I = cmdline; *I != 0; ++I) {
-               PackageSet pset = FromString(Cache, *I, out);
-               pkgset.insert(pset.begin(), pset.end());
+// FromFnmatch - Returns the package defined  by this fnmatch          /*{{{*/
+bool 
+PackageContainerInterface::FromFnmatch(PackageContainerInterface * const pci, 
+                                       pkgCacheFile &Cache,
+                                       std::string pattern,
+                                       CacheSetHelper &helper)
+{
+       static const char * const isfnmatch = ".?*[]!";
+       if (pattern.find_first_of(isfnmatch) == std::string::npos)
+               return false;
+
+       bool const wasEmpty = pci->empty();
+       if (wasEmpty == true)
+               pci->setConstructor(CacheSetHelper::FNMATCH);
+
+       size_t archfound = pattern.find_last_of(':');
+       std::string arch = "native";
+       if (archfound != std::string::npos) {
+               arch = pattern.substr(archfound+1);
+               if (arch.find_first_of(isfnmatch) == std::string::npos)
+                       pattern.erase(archfound);
+               else
+                       arch = "native";
        }
        }
-       return pkgset;
+
+       if (unlikely(Cache.GetPkgCache() == 0))
+               return false;
+
+       APT::CacheFilter::PackageNameMatchesFnmatch filter(pattern);
+
+       bool found = false;
+       for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) {
+               if (filter(Grp) == false)
+                       continue;
+               pkgCache::PkgIterator Pkg = Grp.FindPkg(arch);
+               if (Pkg.end() == true) {
+                       if (archfound == std::string::npos) {
+                               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;
+               }
+
+               pci->insert(Pkg);
+               helper.showPackageSelection(Pkg, CacheSetHelper::FNMATCH, pattern);
+               found = true;
+       }
+
+       if (found == false) {
+               helper.canNotFindPackage(CacheSetHelper::FNMATCH, pci, Cache, pattern);
+               pci->setConstructor(CacheSetHelper::UNKNOWN);
+               return false;
+       }
+
+       if (wasEmpty == false && pci->getConstructor() != CacheSetHelper::UNKNOWN)
+               pci->setConstructor(CacheSetHelper::UNKNOWN);
+
+       return true;
 }
                                                                        /*}}}*/
 }
                                                                        /*}}}*/
-// FromString - Return all packages matching a specific string         /*{{{*/
-PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, std::ostream &out) {
+// FromName - Returns the package defined  by this string              /*{{{*/
+pkgCache::PkgIterator PackageContainerInterface::FromName(pkgCacheFile &Cache,
+                       std::string const &str, CacheSetHelper &helper) {
        std::string pkg = str;
        size_t archfound = pkg.find_last_of(':');
        std::string arch;
        std::string pkg = str;
        size_t archfound = pkg.find_last_of(':');
        std::string arch;
@@ -96,7 +240,10 @@ PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, s
                pkg.erase(archfound);
        }
 
                pkg.erase(archfound);
        }
 
-       pkgCache::PkgIterator Pkg;
+       if (Cache.GetPkgCache() == 0)
+               return pkgCache::PkgIterator(Cache, 0);
+
+       pkgCache::PkgIterator Pkg(Cache, 0);
        if (arch.empty() == true) {
                pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
                if (Grp.end() == false)
        if (arch.empty() == true) {
                pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
                if (Grp.end() == false)
@@ -104,144 +251,579 @@ PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, s
        } else
                Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
 
        } else
                Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch);
 
-       if (Pkg.end() == false) {
-               PackageSet pkgset;
-               pkgset.insert(Pkg);
-               return pkgset;
+       if (Pkg.end() == true)
+               return helper.canNotFindPkgName(Cache, str);
+       return Pkg;
+}
+                                                                       /*}}}*/
+// FromGroup - Returns the package defined  by this string             /*{{{*/
+bool PackageContainerInterface::FromGroup(PackageContainerInterface * const pci, pkgCacheFile &Cache,
+                       std::string pkg, CacheSetHelper &helper) {
+       if (unlikely(Cache.GetPkgCache() == 0))
+               return false;
+
+       size_t const archfound = pkg.find_last_of(':');
+       std::string arch;
+       if (archfound != std::string::npos) {
+               arch = pkg.substr(archfound+1);
+               pkg.erase(archfound);
+               if (arch == "all" || arch == "native")
+                       arch = _config->Find("APT::Architecture");
+       }
+
+       pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg);
+       if (Grp.end() == false) {
+               if (arch.empty() == true) {
+                       pkgCache::PkgIterator Pkg = Grp.FindPreferredPkg();
+                       if (Pkg.end() == false)
+                       {
+                          pci->insert(Pkg);
+                          return true;
+                       }
+               } else {
+                       bool found = false;
+                       // for 'linux-any' return the first package matching, for 'linux-*' return all matches
+                       bool const isGlobal = arch.find('*') != std::string::npos;
+                       APT::CacheFilter::PackageArchitectureMatchesSpecification pams(arch);
+                       for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg)) {
+                               if (pams(Pkg) == false)
+                                       continue;
+                               pci->insert(Pkg);
+                               found = true;
+                               if (isGlobal == false)
+                                       break;
+                       }
+                       if (found == true)
+                               return true;
+               }
        }
        }
-       PackageSet regex = FromRegEx(Cache, str, out);
-       if (regex.empty() == true)
-               _error->Warning(_("Unable to locate package %s"), str.c_str());
-       return regex;
+
+       pkgCache::PkgIterator Pkg = helper.canNotFindPkgName(Cache, pkg);
+       if (Pkg.end() == true)
+          return false;
+
+       pci->insert(Pkg);
+       return true;
+}
+                                                                       /*}}}*/
+// FromString - Return all packages matching a specific string         /*{{{*/
+bool PackageContainerInterface::FromString(PackageContainerInterface * const pci, pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) {
+       bool found = true;
+       _error->PushToStack();
+
+       if (FromGroup(pci, Cache, str, helper) == false &&
+                FromTask(pci, Cache, str, helper) == false &&
+                // FIXME: hm, hm, regexp/fnmatch incompatible?
+                FromFnmatch(pci, Cache, str, helper) == false &&
+                FromRegEx(pci, Cache, str, helper) == false)
+       {
+               helper.canNotFindPackage(CacheSetHelper::PACKAGENAME, pci, Cache, str);
+               found = false;
+       }
+
+       if (found == true)
+               _error->RevertToStack();
+       else
+               _error->MergeWithStack();
+       return found;
+}
+                                                                       /*}}}*/
+// FromCommandLine - Return all packages specified on commandline      /*{{{*/
+bool PackageContainerInterface::FromCommandLine(PackageContainerInterface * const pci, pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) {
+       bool found = false;
+       for (const char **I = cmdline; *I != 0; ++I)
+               found |= PackageContainerInterface::FromString(pci, Cache, *I, helper);
+       return found;
+}
+                                                                       /*}}}*/
+// FromModifierCommandLine - helper doing the work for PKG:GroupedFromCommandLine      /*{{{*/
+bool PackageContainerInterface::FromModifierCommandLine(unsigned short &modID, PackageContainerInterface * const pci,
+                                                       pkgCacheFile &Cache, const char * cmdline,
+                                                       std::list<Modifier> const &mods, CacheSetHelper &helper) {
+       std::string str = cmdline;
+       unsigned short fallback = modID;
+       bool modifierPresent = false;
+       for (std::list<Modifier>::const_iterator mod = mods.begin();
+            mod != mods.end(); ++mod) {
+               size_t const alength = strlen(mod->Alias);
+               switch(mod->Pos) {
+               case Modifier::POSTFIX:
+                       if (str.compare(str.length() - alength, alength,
+                                       mod->Alias, 0, alength) != 0)
+                               continue;
+                       str.erase(str.length() - alength);
+                       modID = mod->ID;
+                       break;
+               case Modifier::PREFIX:
+                       continue;
+               case Modifier::NONE:
+                       continue;
+               }
+               modifierPresent = true;
+               break;
+       }
+       if (modifierPresent == true) {
+               bool const errors = helper.showErrors(false);
+               pkgCache::PkgIterator Pkg = FromName(Cache, cmdline, helper);
+               helper.showErrors(errors);
+               if (Pkg.end() == false) {
+                       pci->insert(Pkg);
+                       modID = fallback;
+                       return true;
+               }
+       }
+       return FromString(pci, Cache, str, helper);
+}
+                                                                       /*}}}*/
+// FromModifierCommandLine - helper doing the work for VER:GroupedFromCommandLine      /*{{{*/
+bool VersionContainerInterface::FromModifierCommandLine(unsigned short &modID,
+                                                       VersionContainerInterface * const vci,
+                                                       pkgCacheFile &Cache, const char * cmdline,
+                                                       std::list<Modifier> const &mods,
+                                                       CacheSetHelper &helper) {
+       CacheSetHelper::VerSelector select = CacheSetHelper::NEWEST;
+       std::string str = cmdline;
+       if (unlikely(str.empty() == true))
+               return false;
+       bool modifierPresent = false;
+       unsigned short fallback = modID;
+       for (std::list<Modifier>::const_iterator mod = mods.begin();
+            mod != mods.end(); ++mod) {
+               if (modID == fallback && mod->ID == fallback)
+                       select = mod->SelectVersion;
+               size_t const alength = strlen(mod->Alias);
+               switch(mod->Pos) {
+               case Modifier::POSTFIX:
+                       if (str.length() <= alength ||
+                             str.compare(str.length() - alength, alength, mod->Alias, 0, alength) != 0)
+                               continue;
+                       str.erase(str.length() - alength);
+                       modID = mod->ID;
+                       select = mod->SelectVersion;
+                       break;
+               case Modifier::PREFIX:
+                       continue;
+               case Modifier::NONE:
+                       continue;
+               }
+               modifierPresent = true;
+               break;
+       }
+       if (modifierPresent == true) {
+               bool const errors = helper.showErrors(false);
+               bool const found = VersionContainerInterface::FromString(vci, Cache, cmdline, select, helper, true);
+               helper.showErrors(errors);
+               if (found == true) {
+                       modID = fallback;
+                       return true;
+               }
+       }
+       return FromString(vci, Cache, str, select, helper);
 }
                                                                        /*}}}*/
 // FromCommandLine - Return all versions specified on commandline      /*{{{*/
 }
                                                                        /*}}}*/
 // FromCommandLine - Return all versions specified on commandline      /*{{{*/
-APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline,
-               APT::VersionSet::Version const &fallback, std::ostream &out) {
-       VersionSet verset;
-       for (const char **I = cmdline; *I != 0; ++I) {
-               std::string pkg = *I;
-               std::string ver;
-               bool verIsRel = false;
-               size_t const vertag = pkg.find_last_of("/=");
-               if (vertag != string::npos) {
-                       ver = pkg.substr(vertag+1);
-                       verIsRel = (pkg[vertag] == '/');
-                       pkg.erase(vertag);
+bool VersionContainerInterface::FromCommandLine(VersionContainerInterface * const vci,
+                                               pkgCacheFile &Cache, const char **cmdline,
+                                               CacheSetHelper::VerSelector const fallback,
+                                               CacheSetHelper &helper) {
+       bool found = false;
+       for (const char **I = cmdline; *I != 0; ++I)
+               found |= VersionContainerInterface::FromString(vci, Cache, *I, fallback, helper);
+       return found;
+}
+                                                                       /*}}}*/
+// FromString - Returns all versions spedcified by a string            /*{{{*/
+bool VersionContainerInterface::FromString(VersionContainerInterface * const vci,
+                                          pkgCacheFile &Cache, std::string pkg,
+                                          CacheSetHelper::VerSelector const fallback,
+                                          CacheSetHelper &helper,
+                                          bool const onlyFromName) {
+        PackageSet pkgset;
+        if(FileExists(pkg))
+        {
+                PackageContainerInterface::FromString(&pkgset, Cache, pkg, helper);
+                if(pkgset.size() == 0)
+                   return false;
+                return VersionContainerInterface::FromPackage(vci, Cache, pkgset.begin(), fallback, helper);
+        }
+
+       std::string ver;
+       bool verIsRel = false;
+       size_t const vertag = pkg.find_last_of("/=");
+       if (vertag != std::string::npos) {
+               ver = pkg.substr(vertag+1);
+               verIsRel = (pkg[vertag] == '/');
+               pkg.erase(vertag);
+       }
+       if (onlyFromName == false)
+               PackageContainerInterface::FromString(&pkgset, Cache, pkg, helper);
+       else {
+               pkgset.insert(PackageContainerInterface::FromName(Cache, pkg, helper));
+       }
+
+       bool errors = true;
+       if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
+               errors = helper.showErrors(false);
+
+       bool found = false;
+       for (PackageSet::const_iterator P = pkgset.begin();
+            P != pkgset.end(); ++P) {
+               if (vertag == std::string::npos) {
+                       found |= VersionContainerInterface::FromPackage(vci, Cache, P, fallback, helper);
+                       continue;
                }
                }
-               PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out);
-               for (PackageSet::const_iterator P = pkgset.begin();
-                    P != pkgset.end(); ++P) {
-                       if (vertag == string::npos) {
-                               AddSelectedVersion(Cache, verset, P, fallback);
+               pkgCache::VerIterator V;
+               if (ver == "installed")
+                       V = getInstalledVer(Cache, P, helper);
+               else if (ver == "candidate")
+                       V = getCandidateVer(Cache, P, helper);
+               else if (ver == "newest") {
+                       if (P->VersionList != 0)
+                               V = P.VersionList();
+                       else
+                               V = helper.canNotGetVersion(CacheSetHelper::NEWEST, Cache, P);
+               } else {
+                       pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
+                                       pkgVersionMatch::Version));
+                       V = Match.Find(P);
+                       if (V.end() == true) {
+                               if (verIsRel == true)
+                                       _error->Error(_("Release '%s' for '%s' was not found"),
+                                                       ver.c_str(), P.FullName(true).c_str());
+                               else
+                                       _error->Error(_("Version '%s' for '%s' was not found"),
+                                                       ver.c_str(), P.FullName(true).c_str());
                                continue;
                        }
                                continue;
                        }
-                       pkgCache::VerIterator V;
-                       if (ver == "installed")
-                               V = getInstalledVer(Cache, P);
-                       else if (ver == "candidate")
-                               V = getCandidateVer(Cache, P);
-                       else {
-                               pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release :
-                                               pkgVersionMatch::Version));
-                               V = Match.Find(P);
-                               if (V.end() == true) {
-                                       if (verIsRel == true)
-                                               _error->Error(_("Release '%s' for '%s' was not found"),
-                                                               ver.c_str(), P.FullName(true).c_str());
-                                       else
-                                               _error->Error(_("Version '%s' for '%s' was not found"),
-                                                               ver.c_str(), P.FullName(true).c_str());
-                                       continue;
-                               }
-                       }
-                       if (V.end() == true)
-                               continue;
-                       if (ver == V.VerStr())
-                               ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"),
-                                        V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str());
-                       verset.insert(V);
                }
                }
+               if (V.end() == true)
+                       continue;
+               if (verIsRel == true)
+                       helper.showVersionSelection(P, V, CacheSetHelper::RELEASE, ver);
+               else
+                       helper.showVersionSelection(P, V, CacheSetHelper::VERSIONNUMBER, ver);
+               vci->insert(V);
+               found = true;
        }
        }
-       return verset;
+       if (pkgset.getConstructor() != CacheSetHelper::UNKNOWN)
+               helper.showErrors(errors);
+       return found;
 }
                                                                        /*}}}*/
 }
                                                                        /*}}}*/
-// AddSelectedVersion - add version from package based on fallback     /*{{{*/
-bool VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset,
-               pkgCache::PkgIterator const &P, VersionSet::Version const &fallback,
-               bool const &AllowError) {
+// FromPackage - versions from package based on fallback               /*{{{*/
+bool VersionContainerInterface::FromPackage(VersionContainerInterface * const vci,
+                                           pkgCacheFile &Cache,
+                                           pkgCache::PkgIterator const &P,
+                                           CacheSetHelper::VerSelector const fallback,
+                                           CacheSetHelper &helper) {
        pkgCache::VerIterator V;
        pkgCache::VerIterator V;
+       bool showErrors;
+       bool found = false;
        switch(fallback) {
        switch(fallback) {
-       case VersionSet::ALL:
+       case CacheSetHelper::ALL:
                if (P->VersionList != 0)
                        for (V = P.VersionList(); V.end() != true; ++V)
                if (P->VersionList != 0)
                        for (V = P.VersionList(); V.end() != true; ++V)
-                               verset.insert(V);
-               else if (AllowError == false)
-                       return _error->Error(_("Can't select versions from package '%s' as it purely virtual"), P.FullName(true).c_str());
+                               found |= vci->insert(V);
                else
                else
-                       return false;
+                       helper.canNotFindVersion(CacheSetHelper::ALL, vci, Cache, P);
                break;
                break;
-       case VersionSet::CANDANDINST:
-               verset.insert(getInstalledVer(Cache, P, AllowError));
-               verset.insert(getCandidateVer(Cache, P, AllowError));
+       case CacheSetHelper::CANDANDINST:
+               found |= vci->insert(getInstalledVer(Cache, P, helper));
+               found |= vci->insert(getCandidateVer(Cache, P, helper));
                break;
                break;
-       case VersionSet::CANDIDATE:
-               verset.insert(getCandidateVer(Cache, P, AllowError));
+       case CacheSetHelper::CANDIDATE:
+               found |= vci->insert(getCandidateVer(Cache, P, helper));
                break;
                break;
-       case VersionSet::INSTALLED:
-               verset.insert(getInstalledVer(Cache, P, AllowError));
+       case CacheSetHelper::INSTALLED:
+               found |= vci->insert(getInstalledVer(Cache, P, helper));
                break;
                break;
-       case VersionSet::CANDINST:
-               V = getCandidateVer(Cache, P, true);
+       case CacheSetHelper::CANDINST:
+               showErrors = helper.showErrors(false);
+               V = getCandidateVer(Cache, P, helper);
                if (V.end() == true)
                if (V.end() == true)
-                       V = getInstalledVer(Cache, P, true);
+                       V = getInstalledVer(Cache, P, helper);
+               helper.showErrors(showErrors);
                if (V.end() == false)
                if (V.end() == false)
-                       verset.insert(V);
-               else if (AllowError == false)
-                       return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
+                       found |= vci->insert(V);
                else
                else
-                       return false;
+                       helper.canNotFindVersion(CacheSetHelper::CANDINST, vci, Cache, P);
                break;
                break;
-       case VersionSet::INSTCAND:
-               V = getInstalledVer(Cache, P, true);
+       case CacheSetHelper::INSTCAND:
+               showErrors = helper.showErrors(false);
+               V = getInstalledVer(Cache, P, helper);
                if (V.end() == true)
                if (V.end() == true)
-                       V = getCandidateVer(Cache, P, true);
+                       V = getCandidateVer(Cache, P, helper);
+               helper.showErrors(showErrors);
                if (V.end() == false)
                if (V.end() == false)
-                       verset.insert(V);
-               else if (AllowError == false)
-                       return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str());
+                       found |= vci->insert(V);
                else
                else
-                       return false;
+                       helper.canNotFindVersion(CacheSetHelper::INSTCAND, vci, Cache, P);
                break;
                break;
-       case VersionSet::NEWEST:
+       case CacheSetHelper::NEWEST:
                if (P->VersionList != 0)
                if (P->VersionList != 0)
-                       verset.insert(P.VersionList());
-               else if (AllowError == false)
-                       return _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), P.FullName(true).c_str());
+                       found |= vci->insert(P.VersionList());
                else
                else
-                       return false;
+                       helper.canNotFindVersion(CacheSetHelper::NEWEST, vci, Cache, P);
                break;
                break;
+       case CacheSetHelper::RELEASE:
+       case CacheSetHelper::VERSIONNUMBER:
+               // both make no sense here, so always false
+               return false;
        }
        }
-       return true;
+       return found;
 }
                                                                        /*}}}*/
 // getCandidateVer - Returns the candidate version of the given package        /*{{{*/
 }
                                                                        /*}}}*/
 // getCandidateVer - Returns the candidate version of the given package        /*{{{*/
-pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache,
-               pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
-       if (unlikely(Cache.BuildDepCache() == false))
-               return pkgCache::VerIterator(*Cache);
-       pkgCache::VerIterator Cand = Cache[Pkg].CandidateVerIter(Cache);
-       if (AllowError == false && Cand.end() == true)
-               _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
+pkgCache::VerIterator VersionContainerInterface::getCandidateVer(pkgCacheFile &Cache,
+               pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
+       pkgCache::VerIterator Cand;
+       if (Cache.IsPolicyBuilt() == true || Cache.IsDepCacheBuilt() == false) {
+               if (unlikely(Cache.GetPolicy() == 0))
+                       return pkgCache::VerIterator(Cache);
+               Cand = Cache.GetPolicy()->GetCandidateVer(Pkg);
+       } else {
+               Cand = Cache[Pkg].CandidateVerIter(Cache);
+       }
+       if (Cand.end() == true)
+               return helper.canNotGetVersion(CacheSetHelper::CANDIDATE, Cache, Pkg);
        return Cand;
 }
                                                                        /*}}}*/
 // getInstalledVer - Returns the installed version of the given package        /*{{{*/
        return Cand;
 }
                                                                        /*}}}*/
 // getInstalledVer - Returns the installed version of the given package        /*{{{*/
-pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache,
-               pkgCache::PkgIterator const &Pkg, bool const &AllowError) {
-       if (AllowError == false && Pkg->CurrentVer == 0)
-               _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
+pkgCache::VerIterator VersionContainerInterface::getInstalledVer(pkgCacheFile &Cache,
+               pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) {
+       if (Pkg->CurrentVer == 0)
+               return helper.canNotGetVersion(CacheSetHelper::INSTALLED, Cache, Pkg);
        return Pkg.CurrentVer();
 }
                                                                        /*}}}*/
        return Pkg.CurrentVer();
 }
                                                                        /*}}}*/
+
+// canNotFindPackage - with the given selector and pattern             /*{{{*/
+void CacheSetHelper::canNotFindPackage(enum PkgSelector const select,
+      PackageContainerInterface * const pci, pkgCacheFile &Cache,
+      std::string const &pattern) {
+       switch (select) {
+#if __GNUC__ >= 4
+       #pragma GCC diagnostic push
+       #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#endif
+       case REGEX: canNotFindRegEx(pci, Cache, pattern); break;
+       case TASK: canNotFindTask(pci, Cache, pattern); break;
+       case FNMATCH: canNotFindFnmatch(pci, Cache, pattern); break;
+       case PACKAGENAME: canNotFindPackage(pci, Cache, pattern); break;
+       case UNKNOWN: break;
+#if __GNUC__ >= 4
+       #pragma GCC diagnostic pop
+#endif
+       }
+}
+// canNotFindTask - handle the case no package is found for a task     /*{{{*/
+void CacheSetHelper::canNotFindTask(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
+       if (ShowError == true)
+               _error->Insert(ErrorType, _("Couldn't find task '%s'"), pattern.c_str());
+}
+                                                                       /*}}}*/
+// canNotFindRegEx - handle the case no package is found by a regex    /*{{{*/
+void CacheSetHelper::canNotFindRegEx(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
+       if (ShowError == true)
+               _error->Insert(ErrorType, _("Couldn't find any package by regex '%s'"), pattern.c_str());
+}
+                                                                       /*}}}*/
+// canNotFindFnmatch - handle the case no package is found by a fnmatch        /*{{{*/
+   void CacheSetHelper::canNotFindFnmatch(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string pattern) {
+       if (ShowError == true)
+               _error->Insert(ErrorType, _("Couldn't find any package by glob '%s'"), pattern.c_str());
+}
+                                                                       /*}}}*/
+// canNotFindPackage - handle the case no package is found from a string/*{{{*/
+APT_CONST void CacheSetHelper::canNotFindPackage(PackageContainerInterface * const /*pci*/, pkgCacheFile &/*Cache*/, std::string const &/*str*/) {
+}
+                                                                       /*}}}*/
+                                                                       /*}}}*/
+// canNotFindPkgName - handle the case no package has this name                /*{{{*/
+pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache,
+                       std::string const &str) {
+       if (ShowError == true)
+               _error->Insert(ErrorType, _("Unable to locate package %s"), str.c_str());
+       return pkgCache::PkgIterator(Cache, 0);
+}
+                                                                       /*}}}*/
+// canNotFindVersion - for package by selector                         /*{{{*/
+void CacheSetHelper::canNotFindVersion(enum VerSelector const select, VersionContainerInterface * const vci, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg)
+{
+       switch (select) {
+#if __GNUC__ >= 4
+       #pragma GCC diagnostic push
+       #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#endif
+       case ALL: canNotFindAllVer(vci, Cache, Pkg); break;
+       case INSTCAND: canNotFindInstCandVer(vci, Cache, Pkg); break;
+       case CANDINST: canNotFindCandInstVer(vci, Cache, Pkg); break;
+       case NEWEST: canNotFindNewestVer(Cache, Pkg); break;
+       case CANDIDATE: canNotFindCandidateVer(Cache, Pkg); break;
+       case INSTALLED: canNotFindInstalledVer(Cache, Pkg); break;
+#if __GNUC__ >= 4
+       #pragma GCC diagnostic pop
+#endif
+       case CANDANDINST: canNotGetCandInstVer(Cache, Pkg); break;
+       case RELEASE:
+       case VERSIONNUMBER:
+               // invalid in this branch
+               break;
+       }
+}
+// canNotFindAllVer                                                    /*{{{*/
+void CacheSetHelper::canNotFindAllVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &/*Cache*/,
+               pkgCache::PkgIterator const &Pkg) {
+       if (ShowError == true)
+               _error->Insert(ErrorType, _("Can't select versions from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
+}
+                                                                       /*}}}*/
+// canNotFindInstCandVer                                               /*{{{*/
+void CacheSetHelper::canNotFindInstCandVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
+               pkgCache::PkgIterator const &Pkg) {
+       canNotGetInstCandVer(Cache, Pkg);
+}
+                                                                       /*}}}*/
+// canNotFindInstCandVer                                               /*{{{*/
+void CacheSetHelper::canNotFindCandInstVer(VersionContainerInterface * const /*vci*/, pkgCacheFile &Cache,
+               pkgCache::PkgIterator const &Pkg) {
+       canNotGetCandInstVer(Cache, Pkg);
+}
+                                                                       /*}}}*/
+                                                                       /*}}}*/
+// canNotGetVersion - for package by selector                          /*{{{*/
+pkgCache::VerIterator CacheSetHelper::canNotGetVersion(enum VerSelector const select, pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg) {
+       switch (select) {
+#if __GNUC__ >= 4
+       #pragma GCC diagnostic push
+       #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#endif
+       case NEWEST: return canNotFindNewestVer(Cache, Pkg);
+       case CANDIDATE: return canNotFindCandidateVer(Cache, Pkg);
+       case INSTALLED: return canNotFindInstalledVer(Cache, Pkg);
+#if __GNUC__ >= 4
+       #pragma GCC diagnostic pop
+#endif
+       case CANDINST: return canNotGetCandInstVer(Cache, Pkg);
+       case INSTCAND: return canNotGetInstCandVer(Cache, Pkg);
+       case ALL:
+       case CANDANDINST:
+       case RELEASE:
+       case VERSIONNUMBER:
+               // invalid in this branch
+               return pkgCache::VerIterator(Cache, 0);
+       }
+       return pkgCache::VerIterator(Cache, 0);
+}
+// canNotFindNewestVer                                                 /*{{{*/
+pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache,
+               pkgCache::PkgIterator const &Pkg) {
+       if (ShowError == true)
+               _error->Insert(ErrorType, _("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str());
+       return pkgCache::VerIterator(Cache, 0);
+}
+                                                                       /*}}}*/
+// canNotFindCandidateVer                                              /*{{{*/
+pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache,
+               pkgCache::PkgIterator const &Pkg) {
+       if (ShowError == true)
+               _error->Insert(ErrorType, _("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str());
+       return pkgCache::VerIterator(Cache, 0);
+}
+                                                                       /*}}}*/
+// canNotFindInstalledVer                                              /*{{{*/
+pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache,
+               pkgCache::PkgIterator const &Pkg) {
+       if (ShowError == true)
+               _error->Insert(ErrorType, _("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str());
+       return pkgCache::VerIterator(Cache, 0);
+}
+                                                                       /*}}}*/
+// canNotFindInstCandVer                                               /*{{{*/
+pkgCache::VerIterator CacheSetHelper::canNotGetInstCandVer(pkgCacheFile &Cache,
+               pkgCache::PkgIterator const &Pkg) {
+       if (ShowError == true)
+               _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
+       return pkgCache::VerIterator(Cache, 0);
+}
+                                                                       /*}}}*/
+// canNotFindInstCandVer                                               /*{{{*/
+pkgCache::VerIterator CacheSetHelper::canNotGetCandInstVer(pkgCacheFile &Cache,
+               pkgCache::PkgIterator const &Pkg) {
+       if (ShowError == true)
+               _error->Insert(ErrorType, _("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str());
+       return pkgCache::VerIterator(Cache, 0);
+}
+                                                                       /*}}}*/
+                                                                       /*}}}*/
+// showPackageSelection - by selector and given pattern                        /*{{{*/
+APT_CONST void CacheSetHelper::showPackageSelection(pkgCache::PkgIterator const &pkg, enum PkgSelector const select,
+                                      std::string const &pattern) {
+       switch (select) {
+#if __GNUC__ >= 4
+       #pragma GCC diagnostic push
+       #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#endif
+       case REGEX: showRegExSelection(pkg, pattern); break;
+       case TASK: showTaskSelection(pkg, pattern); break;
+       case FNMATCH: showFnmatchSelection(pkg, pattern); break;
+#if __GNUC__ >= 4
+       #pragma GCC diagnostic pop
+#endif
+       case PACKAGENAME: /* no suprises here */ break;
+       case UNKNOWN: break;
+       }
+}
+// showTaskSelection                                                   /*{{{*/
+APT_CONST void CacheSetHelper::showTaskSelection(pkgCache::PkgIterator const &/*pkg*/,
+                                      std::string const &/*pattern*/) {
+}
+                                                                       /*}}}*/
+// showRegExSelection                                                  /*{{{*/
+APT_CONST void CacheSetHelper::showRegExSelection(pkgCache::PkgIterator const &/*pkg*/,
+                                       std::string const &/*pattern*/) {
+}
+                                                                       /*}}}*/
+// showFnmatchSelection                                                        /*{{{*/
+APT_CONST void CacheSetHelper::showFnmatchSelection(pkgCache::PkgIterator const &/*pkg*/,
+                                         std::string const &/*pattern*/) {
+}
+                                                                       /*}}}*/
+                                                                       /*}}}*/
+// showVersionSelection                                                        /*{{{*/
+APT_CONST void CacheSetHelper::showVersionSelection(pkgCache::PkgIterator const &Pkg,
+      pkgCache::VerIterator const &Ver, enum VerSelector const select, std::string const &pattern) {
+       switch (select) {
+#if __GNUC__ >= 4
+       #pragma GCC diagnostic push
+       #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#endif
+       case RELEASE:
+               showSelectedVersion(Pkg, Ver, pattern, true);
+               break;
+       case VERSIONNUMBER:
+               showSelectedVersion(Pkg, Ver, pattern, false);
+               break;
+#if __GNUC__ >= 4
+       #pragma GCC diagnostic pop
+       #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+#endif
+       case NEWEST:
+       case CANDIDATE:
+       case INSTALLED:
+       case CANDINST:
+       case INSTCAND:
+       case ALL:
+       case CANDANDINST:
+               // not really suprises, but in fact: just not implemented
+               break;
+       }
+}
+APT_CONST void CacheSetHelper::showSelectedVersion(pkgCache::PkgIterator const &/*Pkg*/,
+                                        pkgCache::VerIterator const /*Ver*/,
+                                        std::string const &/*ver*/,
+                                        bool const /*verIsRel*/) {
+}
+                                                                       /*}}}*/
 }
 }