From 6e235c6640cdc6cb9a8c3eaa1e65a6141f463676 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 31 May 2010 14:16:44 +0200 Subject: [PATCH] improve the handling of regex together with the architecture modifier --- apt-pkg/packageset.cc | 35 +++++++++++++++++++++-------------- apt-pkg/packageset.h | 4 ++-- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/apt-pkg/packageset.cc b/apt-pkg/packageset.cc index baa1c211b..f296b7c45 100644 --- a/apt-pkg/packageset.cc +++ b/apt-pkg/packageset.cc @@ -22,20 +22,26 @@ /*}}}*/ 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 PackageSet::FromRegEx(pkgCache &Cache, std::string pattern, std::ostream &out) { PackageSet pkgset; + std::string arch = "native"; + static const char * const isregex = ".?+*|[^$"; - const char * I; - for (I = pattern; *I != 0; I++) - if (*I == '.' || *I == '?' || *I == '+' || *I == '*' || - *I == '|' || *I == '[' || *I == '^' || *I == '$') - break; - if (*I == 0) + if (pattern.find_first_of(isregex) == std::string::npos) return pkgset; + size_t archfound = pattern.find_last_of(':'); + if (archfound != std::string::npos) { + arch = pattern.substr(archfound+1); + if (arch.find_first_of(isregex) == std::string::npos) + pattern.erase(archfound); + else + arch = "native"; + } + regex_t Pattern; int Res; - if ((Res = regcomp(&Pattern, pattern , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) { + 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); @@ -46,19 +52,20 @@ PackageSet PackageSet::FromRegEx(pkgCache &Cache, const char * const pattern, st { if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0) continue; - pkgCache::PkgIterator Pkg = Grp.FindPkg("native"); + pkgCache::PkgIterator Pkg = Grp.FindPkg(arch); if (Pkg.end() == true) { - std::vector archs = APT::Configuration::getArchitectures(); - for (std::vector::const_iterator a = archs.begin(); - a != archs.end() || Pkg.end() != true; ++a) { - Pkg = Grp.FindPkg(*a); + if (archfound == std::string::npos) { + std::vector archs = APT::Configuration::getArchitectures(); + for (std::vector::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); + Pkg.FullName(true).c_str(), pattern.c_str()); pkgset.insert(Pkg); } diff --git a/apt-pkg/packageset.h b/apt-pkg/packageset.h index 0bd60c56b..9f80d60ed 100644 --- a/apt-pkg/packageset.h +++ b/apt-pkg/packageset.h @@ -64,8 +64,8 @@ public: /*{{{*/ \param Cache the packages are in \param pattern regular expression for package names \param out stream to print the notice to */ - static APT::PackageSet FromRegEx(pkgCache &Cache, const char *pattern, std::ostream &out); - static APT::PackageSet FromRegEx(pkgCache &Cache, const char *pattern) { + static APT::PackageSet FromRegEx(pkgCache &Cache, std::string pattern, std::ostream &out); + static APT::PackageSet FromRegEx(pkgCache &Cache, std::string const &pattern) { std::ostream out (std::ofstream("/dev/null").rdbuf()); return APT::PackageSet::FromRegEx(Cache, pattern, out); } -- 2.45.2