From 859093dae7dcadaff2e15a3885a1824b0d5f5913 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 30 Aug 2014 11:29:45 +0200 Subject: support regular expressions in 'apt search' apt-cache search supported this since ever and in the code for apt was a fixme indicating this should be added here as well, so here we go. --- apt-private/private-cmndline.cc | 9 +++++-- apt-private/private-search.cc | 52 +++++++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 19 deletions(-) (limited to 'apt-private') diff --git a/apt-private/private-cmndline.cc b/apt-private/private-cmndline.cc index a21a9dc8c..a4490f5b4 100644 --- a/apt-private/private-cmndline.cc +++ b/apt-private/private-cmndline.cc @@ -70,6 +70,8 @@ static bool addArgumentsAPTCache(std::vector &Args, char cons else return false; + bool const found_something = Args.empty() == false; + // FIXME: move to the correct command(s) addArg('g', "generate", "APT::Cache::Generate", 0); addArg('t', "target-release", "APT::Default-Release", CommandLine::HasArg); @@ -77,7 +79,8 @@ static bool addArgumentsAPTCache(std::vector &Args, char cons addArg('p', "pkg-cache", "Dir::Cache::pkgcache", CommandLine::HasArg); addArg('s', "src-cache", "Dir::Cache::srcpkgcache", CommandLine::HasArg); - return true; + + return found_something; } /*}}}*/ static bool addArgumentsAPTCDROM(std::vector &Args, char const * const Cmd)/*{{{*/ @@ -172,6 +175,8 @@ static bool addArgumentsAPTGet(std::vector &Args, char const addArg('s', "no-act", "APT::Get::Simulate", 0); } + bool const found_something = Args.empty() == false; + // FIXME: move to the correct command(s) addArg('d',"download-only","APT::Get::Download-Only",0); addArg('y',"yes","APT::Get::Assume-Yes",0); @@ -197,7 +202,7 @@ static bool addArgumentsAPTGet(std::vector &Args, char const addArg(0,"install-suggests","APT::Install-Suggests",CommandLine::Boolean); addArg(0,"fix-policy","APT::Get::Fix-Policy-Broken",0); - return true; + return found_something; } /*}}}*/ static bool addArgumentsAPTMark(std::vector &Args, char const * const Cmd)/*{{{*/ diff --git a/apt-private/private-search.cc b/apt-private/private-search.cc index ecd5d7fad..2230c973a 100644 --- a/apt-private/private-search.cc +++ b/apt-private/private-search.cc @@ -36,8 +36,28 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ if (unlikely(Cache == NULL || Plcy == NULL)) return false; - const char **patterns; - patterns = CmdL.FileList + 1; + // Make sure there is at least one argument + unsigned int const NumPatterns = CmdL.FileSize() -1; + if (NumPatterns < 1) + return _error->Error(_("You must give at least one search pattern")); + +#define APT_FREE_PATTERNS() for (std::vector::iterator P = Patterns.begin(); \ + P != Patterns.end(); ++P) { regfree(&(*P)); } + + // Compile the regex pattern + std::vector Patterns; + for (unsigned int I = 0; I != NumPatterns; ++I) + { + regex_t pattern; + if (regcomp(&pattern, CmdL.FileList[I + 1], REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0) + { + APT_FREE_PATTERNS(); + return _error->Error("Regex compilation error"); + } + Patterns.push_back(pattern); + } + + bool const NamesOnly = _config->FindB("APT::Cache::NamesOnly", false); std::map output_map; std::map::const_iterator K; @@ -56,26 +76,23 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ if (Done%500 == 0) progress.Progress(Done); ++Done; - - int i; + pkgCache::DescIterator Desc = V.TranslatedDescription(); pkgRecords::Parser &parser = records.Lookup(Desc.FileList()); - + bool all_found = true; - for(i=0; patterns[i] != NULL; ++i) + for (std::vector::const_iterator pattern = Patterns.begin(); + pattern != Patterns.end(); ++pattern) { - // FIXME: use regexp instead of simple find() - const char *pattern = patterns[i]; - all_found &= ( - strstr(V.ParentPkg().Name(), pattern) != NULL || - strcasestr(parser.ShortDesc().c_str(), pattern) != NULL || - strcasestr(parser.LongDesc().c_str(), pattern) != NULL); - // search patterns are AND by default so we can skip looking further - // on the first mismatch - if(all_found == false) - break; + if (regexec(&(*pattern), V.ParentPkg().Name(), 0, 0, 0) == 0) + continue; + else if (NamesOnly == false && regexec(&(*pattern), parser.LongDesc().c_str(), 0, 0, 0) == 0) + continue; + // search patterns are AND, so one failing fails all + all_found = false; + break; } - if (all_found) + if (all_found == true) { std::stringstream outs; ListSingleVersion(CacheFile, records, V, outs); @@ -83,6 +100,7 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ V.ParentPkg().Name(), outs.str())); } } + APT_FREE_PATTERNS(); progress.Done(); // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status) -- cgit v1.2.3 From 206b6bb3ec7d4cf45f3ae67e6d317f1da63f5b98 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 1 Sep 2014 19:09:40 +0200 Subject: skip version if we already have this package as search-result Git-Dch: Ignore --- apt-private/private-search.cc | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'apt-private') diff --git a/apt-private/private-search.cc b/apt-private/private-search.cc index 2230c973a..2d427fa25 100644 --- a/apt-private/private-search.cc +++ b/apt-private/private-search.cc @@ -32,7 +32,6 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ pkgCacheFile CacheFile; pkgCache *Cache = CacheFile.GetPkgCache(); pkgDepCache::Policy *Plcy = CacheFile.GetPolicy(); - pkgRecords records(CacheFile); if (unlikely(Cache == NULL || Plcy == NULL)) return false; @@ -60,7 +59,6 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ bool const NamesOnly = _config->FindB("APT::Cache::NamesOnly", false); std::map output_map; - std::map::const_iterator K; LocalitySortedVersionSet bag; OpTextProgress progress(*_config); @@ -70,6 +68,7 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ progress.OverallProgress(50, 100, 50, _("Full Text Search")); progress.SubProgress(bag.size()); + pkgRecords records(CacheFile); int Done = 0; for ( ;V != bag.end(); ++V) { @@ -77,16 +76,22 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ progress.Progress(Done); ++Done; + // we want to list each package only once + char const * const PkgName = V.ParentPkg().Name(); + if (output_map.find(PkgName) != output_map.end()) + continue; + pkgCache::DescIterator Desc = V.TranslatedDescription(); pkgRecords::Parser &parser = records.Lookup(Desc.FileList()); + std::string const LongDesc = parser.LongDesc(); bool all_found = true; for (std::vector::const_iterator pattern = Patterns.begin(); pattern != Patterns.end(); ++pattern) { - if (regexec(&(*pattern), V.ParentPkg().Name(), 0, 0, 0) == 0) + if (regexec(&(*pattern), PkgName, 0, 0, 0) == 0) continue; - else if (NamesOnly == false && regexec(&(*pattern), parser.LongDesc().c_str(), 0, 0, 0) == 0) + else if (NamesOnly == false && regexec(&(*pattern), LongDesc.c_str(), 0, 0, 0) == 0) continue; // search patterns are AND, so one failing fails all all_found = false; @@ -97,7 +102,7 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ std::stringstream outs; ListSingleVersion(CacheFile, records, V, outs); output_map.insert(std::make_pair( - V.ParentPkg().Name(), outs.str())); + PkgName, outs.str())); } } APT_FREE_PATTERNS(); @@ -105,6 +110,7 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status) // output the sorted map + std::map::const_iterator K; for (K = output_map.begin(); K != output_map.end(); ++K) std::cout << (*K).second << std::endl; -- cgit v1.2.3 From 2409df55f65c4937ceda21ba069eec769af9e460 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 1 Sep 2014 21:43:15 +0200 Subject: use a format string in ListSingleVersion The method already deals with a format string, but had an else path doing a hardcoded format as well. This is changed now to use the same code for both - the format in the second case is still fixed though. Git-Dch: Ignore --- apt-private/private-output.cc | 147 +++++++++++++++++++++--------------------- 1 file changed, 75 insertions(+), 72 deletions(-) (limited to 'apt-private') diff --git a/apt-private/private-output.cc b/apt-private/private-output.cc index 522f7682d..7be56de2e 100644 --- a/apt-private/private-output.cc +++ b/apt-private/private-output.cc @@ -164,15 +164,26 @@ static std::string GetVersion(pkgCacheFile &/*CacheFile*/, pkgCache::VerIterator /*}}}*/ static std::string GetArchitecture(pkgCacheFile &CacheFile, pkgCache::PkgIterator P)/*{{{*/ { - pkgPolicy *policy = CacheFile.GetPolicy(); - pkgCache::VerIterator inst = P.CurrentVer(); - pkgCache::VerIterator cand = policy->GetCandidateVer(P); - - // this may happen for packages in dpkg "deinstall ok config-file" state - if (inst.IsGood() == false && cand.IsGood() == false) - return P.VersionList().Arch(); - - return inst ? inst.Arch() : cand.Arch(); + if (P->CurrentVer == 0) + { + pkgDepCache * const DepCache = CacheFile.GetDepCache(); + pkgDepCache::StateCache const &state = (*DepCache)[P]; + if (state.CandidateVer != NULL) + { + pkgCache::VerIterator const CandV(CacheFile, state.CandidateVer); + return CandV.Arch(); + } + else + { + pkgCache::VerIterator const V = P.VersionList(); + if (V.end() == false) + return V.Arch(); + else + return P.Arch(); + } + } + else + return P.CurrentVer().Arch(); } /*}}}*/ static std::string GetShortDescription(pkgCacheFile &CacheFile, pkgRecords &records, pkgCache::PkgIterator P)/*{{{*/ @@ -200,77 +211,69 @@ void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records, /*{{{*/ pkgCache::VerIterator V, std::ostream &out, bool include_summary) { - pkgCache::PkgIterator P = V.ParentPkg(); - - pkgDepCache *DepCache = CacheFile.GetDepCache(); - pkgDepCache::StateCache &state = (*DepCache)[P]; - - std::string suite = GetArchiveSuite(CacheFile, V); - std::string name_str = P.Name(); + pkgCache::PkgIterator const P = V.ParentPkg(); + pkgDepCache * const DepCache = CacheFile.GetDepCache(); + pkgDepCache::StateCache const &state = (*DepCache)[P]; + std::string output; if (_config->FindB("APT::Cmd::use-format", false)) + output = _config->Find("APT::Cmd::format", "${db::Status-Abbrev} ${Package} ${Version} ${Origin} ${Description}"); + else { - std::string format = _config->Find("APT::Cmd::format", "${db::Status-Abbrev} ${Package} ${Version} ${Origin} ${Description}"); - std::string output = format; - - output = SubstVar(output, "${db::Status-Abbrev}", GetFlagsStr(CacheFile, P)); - output = SubstVar(output, "${Package}", name_str); - output = SubstVar(output, "${installed:Version}", GetInstalledVersion(CacheFile, P)); - output = SubstVar(output, "${candidate:Version}", GetCandidateVersion(CacheFile, P)); - output = SubstVar(output, "${Version}", GetVersion(CacheFile, V)); - output = SubstVar(output, "${Description}", GetShortDescription(CacheFile, records, P)); - output = SubstVar(output, "${Origin}", GetArchiveSuite(CacheFile, V)); - out << output; - } else { - // raring/linux-kernel version [upradable: new-version] + // linux-kernel/unstable version [installed,upradable to: new-version] // description - pkgPolicy *policy = CacheFile.GetPolicy(); - std::string VersionStr = GetVersion(CacheFile, V); - std::string CandidateVerStr = GetCandidateVersion(CacheFile, P); - std::string InstalledVerStr = GetInstalledVersion(CacheFile, P); - std::string StatusStr; - if(P.CurrentVer() == V && state.Upgradable()) + output = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture} ${apt:Status}"; + } + + // FIXME: some of these names are really icky – and all is nowhere documented + output = SubstVar(output, "${db::Status-Abbrev}", GetFlagsStr(CacheFile, P)); + output = SubstVar(output, "${Package}", P.Name()); + std::string const ArchStr = GetArchitecture(CacheFile, P); + output = SubstVar(output, "${Architecture}", ArchStr); + std::string const InstalledVerStr = GetInstalledVersion(CacheFile, P); + output = SubstVar(output, "${installed:Version}", InstalledVerStr); + std::string const CandidateVerStr = GetCandidateVersion(CacheFile, P); + output = SubstVar(output, "${candidate:Version}", CandidateVerStr); + std::string const VersionStr = GetVersion(CacheFile, V); + output = SubstVar(output, "${Version}", VersionStr); + output = SubstVar(output, "${Origin}", GetArchiveSuite(CacheFile, V)); + + std::string StatusStr = ""; + if (P->CurrentVer != 0) + { + if (P.CurrentVer() == V) { - strprintf(StatusStr, _("[installed,upgradable to: %s]"), - CandidateVerStr.c_str()); - } else if (P.CurrentVer() == V) { - if(!V.Downloadable()) - StatusStr = _("[installed,local]"); - else - if(V.Automatic() && state.Garbage) - StatusStr = _("[installed,auto-removable]"); - else if (state.Flags & pkgCache::Flag::Auto) - StatusStr = _("[installed,automatic]"); - else - StatusStr = _("[installed]"); - } else if (P.CurrentVer() && - policy->GetCandidateVer(P) == V && - state.Upgradable()) { - strprintf(StatusStr, _("[upgradable from: %s]"), - InstalledVerStr.c_str()); - } else { - if (V.ParentPkg()->CurrentState == pkgCache::State::ConfigFiles) - StatusStr = _("[residual-config]"); - else - StatusStr = ""; + if (state.Upgradable() && state.CandidateVer != NULL) + strprintf(StatusStr, _("[installed,upgradable to: %s]"), + CandidateVerStr.c_str()); + else if (V.Downloadable() == false) + StatusStr = _("[installed,local]"); + else if(V.Automatic() == true && state.Garbage == true) + StatusStr = _("[installed,auto-removable]"); + else if ((state.Flags & pkgCache::Flag::Auto) == pkgCache::Flag::Auto) + StatusStr = _("[installed,automatic]"); + else + StatusStr = _("[installed]"); } - out << std::setiosflags(std::ios::left) - << _config->Find("APT::Color::Highlight", "") - << name_str - << _config->Find("APT::Color::Neutral", "") - << "/" << suite - << " " - << VersionStr << " " - << GetArchitecture(CacheFile, P); - if (StatusStr != "") - out << " " << StatusStr; + else if (state.CandidateVer == V && state.Upgradable()) + strprintf(StatusStr, _("[upgradable from: %s]"), + InstalledVerStr.c_str()); + } + else if (V.ParentPkg()->CurrentState == pkgCache::State::ConfigFiles) + StatusStr = _("[residual-config]"); + output = SubstVar(output, "${apt:Status}", StatusStr); + output = SubstVar(output, "${color:highlight}", _config->Find("APT::Color::Highlight", "")); + output = SubstVar(output, "${color:neutral}", _config->Find("APT::Color::Neutral", "")); + + output = APT::String::Strip(output); + if (_config->FindB("APT::Cmd::use-format", false) == false) + { if (include_summary) - { - out << std::endl - << " " << GetShortDescription(CacheFile, records, P) - << std::endl; - } + output += "\n ${Description}\n"; } + output = SubstVar(output, "${Description}", GetShortDescription(CacheFile, records, P)); + + out << output; } /*}}}*/ // ShowList - Show a list /*{{{*/ -- cgit v1.2.3 From 1a68655de92fd036ebc7c920bc2e5e88c54eb34e Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 2 Sep 2014 14:32:48 +0200 Subject: implement --full in apt search --- apt-private/private-list.cc | 39 ++++++++++++++++--------------------- apt-private/private-output.cc | 45 +++++++++++++++++++++++++++++-------------- apt-private/private-output.h | 4 ++-- apt-private/private-search.cc | 9 ++++++++- 4 files changed, 58 insertions(+), 39 deletions(-) (limited to 'apt-private') diff --git a/apt-private/private-list.cc b/apt-private/private-list.cc index b69002103..d746acf3f 100644 --- a/apt-private/private-list.cc +++ b/apt-private/private-list.cc @@ -85,15 +85,14 @@ private: }; /*}}}*/ static void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records,/*{{{*/ - pkgCache::PkgIterator P, - std::ostream &outs, - bool include_summary=true) + pkgCache::PkgIterator const &P, std::ostream &outs, + std::string const &format) { for (pkgCache::VerIterator Ver = P.VersionList(); Ver.end() == false; ++Ver) { - ListSingleVersion(CacheFile, records, Ver, outs, include_summary); - outs << "\n"; + ListSingleVersion(CacheFile, records, Ver, outs, format); + outs << std::endl; } } /*}}}*/ @@ -117,10 +116,9 @@ bool DoList(CommandLine &Cmd) patterns = Cmd.FileList + 1; } - std::map output_map; - std::map::const_iterator K; - - bool includeSummary = _config->FindB("APT::Cmd::List-Include-Summary"); + std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}"; + if (_config->FindB("APT::Cmd::List-Include-Summary", false) == true) + format += "\n ${Description}\n"; PackageNameMatcher matcher(patterns); LocalitySortedVersionSet bag; @@ -130,36 +128,33 @@ bool DoList(CommandLine &Cmd) Cache->Head().PackageCount, _("Listing")); GetLocalitySortedVersionSet(CacheFile, bag, matcher, progress); - bool ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false); + bool const ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false); + std::map output_map; for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V) { std::stringstream outs; if(ShowAllVersions == true) - { - ListAllVersions(CacheFile, records, V.ParentPkg(), outs, includeSummary); - output_map.insert(std::make_pair( - V.ParentPkg().Name(), outs.str())); - } else { - ListSingleVersion(CacheFile, records, V, outs, includeSummary); - output_map.insert(std::make_pair( - V.ParentPkg().Name(), outs.str())); - } + ListAllVersions(CacheFile, records, V.ParentPkg(), outs, format); + else + ListSingleVersion(CacheFile, records, V, outs, format); + output_map.insert(std::make_pair( + V.ParentPkg().Name(), outs.str())); } // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status) // output the sorted map + std::map::const_iterator K; for (K = output_map.begin(); K != output_map.end(); ++K) std::cout << (*K).second << std::endl; - // be nice and tell the user if there is more to see if (bag.size() == 1 && ShowAllVersions == false) { // start with -1 as we already displayed one version int versions = -1; pkgCache::VerIterator Ver = *bag.begin(); - for ( ; Ver.end() == false; Ver++) - versions++; + for ( ; Ver.end() == false; ++Ver) + ++versions; if (versions > 0) _error->Notice(P_("There is %i additional version. Please use the '-a' switch to see it", "There are %i additional versions. Please use the '-a' switch to see them.", versions), versions); } diff --git a/apt-private/private-output.cc b/apt-private/private-output.cc index 7be56de2e..fc76a05bc 100644 --- a/apt-private/private-output.cc +++ b/apt-private/private-output.cc @@ -207,9 +207,31 @@ static std::string GetShortDescription(pkgCacheFile &CacheFile, pkgRecords &reco return ShortDescription; } /*}}}*/ +static std::string GetLongDescription(pkgCacheFile &CacheFile, pkgRecords &records, pkgCache::PkgIterator P)/*{{{*/ +{ + pkgPolicy *policy = CacheFile.GetPolicy(); + + pkgCache::VerIterator ver; + if (P->CurrentVer != 0) + ver = P.CurrentVer(); + else + ver = policy->GetCandidateVer(P); + + std::string const EmptyDescription = "(none)"; + if(ver.end() == true) + return EmptyDescription; + + pkgCache::DescIterator const Desc = ver.TranslatedDescription(); + pkgRecords::Parser & parser = records.Lookup(Desc.FileList()); + std::string const longdesc = parser.LongDesc(); + if (longdesc.empty() == true) + return EmptyDescription; + return SubstVar(longdesc, "\n ", "\n "); +} + /*}}}*/ void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records, /*{{{*/ - pkgCache::VerIterator V, std::ostream &out, - bool include_summary) + pkgCache::VerIterator const &V, std::ostream &out, + std::string const &format) { pkgCache::PkgIterator const P = V.ParentPkg(); pkgDepCache * const DepCache = CacheFile.GetDepCache(); @@ -219,11 +241,7 @@ void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records, /*{{{*/ if (_config->FindB("APT::Cmd::use-format", false)) output = _config->Find("APT::Cmd::format", "${db::Status-Abbrev} ${Package} ${Version} ${Origin} ${Description}"); else - { - // linux-kernel/unstable version [installed,upradable to: new-version] - // description - output = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture} ${apt:Status}"; - } + output = format; // FIXME: some of these names are really icky – and all is nowhere documented output = SubstVar(output, "${db::Status-Abbrev}", GetFlagsStr(CacheFile, P)); @@ -264,14 +282,13 @@ void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records, /*{{{*/ output = SubstVar(output, "${apt:Status}", StatusStr); output = SubstVar(output, "${color:highlight}", _config->Find("APT::Color::Highlight", "")); output = SubstVar(output, "${color:neutral}", _config->Find("APT::Color::Neutral", "")); - - output = APT::String::Strip(output); - if (_config->FindB("APT::Cmd::use-format", false) == false) - { - if (include_summary) - output += "\n ${Description}\n"; - } output = SubstVar(output, "${Description}", GetShortDescription(CacheFile, records, P)); + output = SubstVar(output, "${LongDescription}", GetLongDescription(CacheFile, records, P)); + output = SubstVar(output, "${ }${ }", "${ }"); + output = SubstVar(output, "${ }\n", "\n"); + output = SubstVar(output, "${ }", " "); + if (APT::String::Endswith(output, " ") == true) + output.erase(output.length() - 1); out << output; } diff --git a/apt-private/private-output.h b/apt-private/private-output.h index 6f3a964d7..e0dc9bf62 100644 --- a/apt-private/private-output.h +++ b/apt-private/private-output.h @@ -23,8 +23,8 @@ APT_PUBLIC extern unsigned int ScreenWidth; APT_PUBLIC bool InitOutput(); void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records, - pkgCache::VerIterator V, std::ostream &out, - bool include_summary=true); + pkgCache::VerIterator const &V, std::ostream &out, + std::string const &format); // helper to describe global state diff --git a/apt-private/private-search.cc b/apt-private/private-search.cc index 2d427fa25..5e12902e8 100644 --- a/apt-private/private-search.cc +++ b/apt-private/private-search.cc @@ -69,6 +69,13 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ progress.OverallProgress(50, 100, 50, _("Full Text Search")); progress.SubProgress(bag.size()); pkgRecords records(CacheFile); + + std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}\n"; + if (_config->FindB("APT::Cache::ShowFull",false) == false) + format += " ${Description}\n"; + else + format += " ${LongDescription}\n"; + int Done = 0; for ( ;V != bag.end(); ++V) { @@ -100,7 +107,7 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ if (all_found == true) { std::stringstream outs; - ListSingleVersion(CacheFile, records, V, outs); + ListSingleVersion(CacheFile, records, V, outs, format); output_map.insert(std::make_pair( PkgName, outs.str())); } -- cgit v1.2.3 From 25594bb5bddc031afc4d62f3164cd9116d778517 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 2 Sep 2014 18:20:49 +0200 Subject: make GetLocalitySortedVersionSet more generic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No reason in and of by itself at the moment, but prepares for the goal of having 'apt search' and 'apt-cache search' using the same code now that they at least support the same stuff. The 'apt' code is just a multitude slower at the moment… Git-Dch: Ignore --- apt-private/private-cacheset.cc | 89 +++++++++++++++++++++-------------------- apt-private/private-cacheset.h | 12 +++--- apt-private/private-list.cc | 2 +- apt-private/private-search.cc | 17 ++++---- 4 files changed, 63 insertions(+), 57 deletions(-) (limited to 'apt-private') diff --git a/apt-private/private-cacheset.cc b/apt-private/private-cacheset.cc index e37e7b227..eb77be274 100644 --- a/apt-private/private-cacheset.cc +++ b/apt-private/private-cacheset.cc @@ -14,74 +14,77 @@ #include -bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile, - LocalitySortedVersionSet &output_set, - OpProgress &progress) +bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile, + APT::VersionContainerInterface * const vci, + OpProgress * const progress) { Matcher null_matcher = Matcher(); - return GetLocalitySortedVersionSet(CacheFile, output_set, + return GetLocalitySortedVersionSet(CacheFile, vci, null_matcher, progress); } -bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile, - LocalitySortedVersionSet &output_set, +bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile, + APT::VersionContainerInterface * const vci, Matcher &matcher, - OpProgress &progress) + OpProgress * const progress) { pkgCache *Cache = CacheFile.GetPkgCache(); pkgDepCache *DepCache = CacheFile.GetDepCache(); + APT::CacheSetHelper helper(false); int Done=0; - progress.SubProgress(Cache->Head().PackageCount, _("Sorting")); + if (progress != NULL) + progress->SubProgress(Cache->Head().PackageCount, _("Sorting")); + + bool const insertCurrentVer = _config->FindB("APT::Cmd::Installed", false); + bool const insertUpgradable = _config->FindB("APT::Cmd::Upgradable", false); + bool const insertManualInstalled = _config->FindB("APT::Cmd::Manual-Installed", false); + for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P) { - if (Done%500 == 0) - progress.Progress(Done); - Done++; + if (progress != NULL) + { + if (Done % 500 == 0) + progress->Progress(Done); + ++Done; + } + + // exclude virtual pkgs + if (P->VersionList == 0) + continue; if ((matcher)(P) == false) - continue; + continue; - // exclude virtual pkgs - if (P.VersionList() == 0) - continue; pkgDepCache::StateCache &state = (*DepCache)[P]; - if (_config->FindB("APT::Cmd::Installed") == true) + if (insertCurrentVer == true) { - if (P.CurrentVer() != NULL) - { - output_set.insert(P.CurrentVer()); - } + if (P->CurrentVer != 0) + vci->FromPackage(vci, CacheFile, P, APT::VersionContainerInterface::INSTALLED, helper); } - else if (_config->FindB("APT::Cmd::Upgradable") == true) + else if (insertUpgradable == true) { - if(P.CurrentVer() && state.Upgradable()) - { - pkgPolicy *policy = CacheFile.GetPolicy(); - output_set.insert(policy->GetCandidateVer(P)); - } + if(P.CurrentVer() && state.Upgradable()) + vci->FromPackage(vci, CacheFile, P, APT::VersionContainerInterface::CANDIDATE, helper); } - else if (_config->FindB("APT::Cmd::Manual-Installed") == true) + else if (insertManualInstalled == true) { - if (P.CurrentVer() && - ((*DepCache)[P].Flags & pkgCache::Flag::Auto) == false) - { - pkgPolicy *policy = CacheFile.GetPolicy(); - output_set.insert(policy->GetCandidateVer(P)); - } + if (P.CurrentVer() && + ((*DepCache)[P].Flags & pkgCache::Flag::Auto) == false) + vci->FromPackage(vci, CacheFile, P, APT::VersionContainerInterface::CANDIDATE, helper); } - else + else { - pkgPolicy *policy = CacheFile.GetPolicy(); - if (policy->GetCandidateVer(P).IsGood()) - output_set.insert(policy->GetCandidateVer(P)); - else - // no candidate, this may happen for packages in - // dpkg "deinstall ok config-file" state - we pick the first ver - // (which should be the only one) - output_set.insert(P.VersionList()); + if (vci->FromPackage(vci, CacheFile, P, APT::VersionContainerInterface::CANDIDATE, helper) == false) + { + // no candidate, this may happen for packages in + // dpkg "deinstall ok config-file" state - we pick the first ver + // (which should be the only one) + vci->insert(P.VersionList()); + } } } - progress.Done(); + if (progress != NULL) + progress->Done(); return true; } diff --git a/apt-private/private-cacheset.h b/apt-private/private-cacheset.h index 854d16922..ca8f4be5d 100644 --- a/apt-private/private-cacheset.h +++ b/apt-private/private-cacheset.h @@ -62,13 +62,13 @@ public: }; // FIXME: add default argument for OpProgress (or overloaded function) -bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile, - LocalitySortedVersionSet &output_set, +bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile, + APT::VersionContainerInterface * const vci, Matcher &matcher, - OpProgress &progress); -bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile, - LocalitySortedVersionSet &output_set, - OpProgress &progress); + OpProgress * const progress); +bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile, + APT::VersionContainerInterface * const vci, + OpProgress * const progress); // CacheSetHelper saving virtual packages /*{{{*/ diff --git a/apt-private/private-list.cc b/apt-private/private-list.cc index d746acf3f..536348640 100644 --- a/apt-private/private-list.cc +++ b/apt-private/private-list.cc @@ -127,7 +127,7 @@ bool DoList(CommandLine &Cmd) Cache->Head().PackageCount, Cache->Head().PackageCount, _("Listing")); - GetLocalitySortedVersionSet(CacheFile, bag, matcher, progress); + GetLocalitySortedVersionSet(CacheFile, &bag, matcher, &progress); bool const ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false); std::map output_map; for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V) diff --git a/apt-private/private-search.cc b/apt-private/private-search.cc index 5e12902e8..6bce9ffa4 100644 --- a/apt-private/private-search.cc +++ b/apt-private/private-search.cc @@ -63,7 +63,7 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ LocalitySortedVersionSet bag; OpTextProgress progress(*_config); progress.OverallProgress(0, 100, 50, _("Sorting")); - GetLocalitySortedVersionSet(CacheFile, bag, progress); + GetLocalitySortedVersionSet(CacheFile, &bag, &progress); LocalitySortedVersionSet::iterator V = bag.begin(); progress.OverallProgress(50, 100, 50, _("Full Text Search")); @@ -77,6 +77,7 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ format += " ${LongDescription}\n"; int Done = 0; + std::vector PkgsDone(Cache->Head().PackageCount, false); for ( ;V != bag.end(); ++V) { if (Done%500 == 0) @@ -84,10 +85,11 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ ++Done; // we want to list each package only once - char const * const PkgName = V.ParentPkg().Name(); - if (output_map.find(PkgName) != output_map.end()) + pkgCache::PkgIterator const P = V.ParentPkg(); + if (PkgsDone[P->ID] == true) continue; + char const * const PkgName = P.Name(); pkgCache::DescIterator Desc = V.TranslatedDescription(); pkgRecords::Parser &parser = records.Lookup(Desc.FileList()); std::string const LongDesc = parser.LongDesc(); @@ -106,10 +108,11 @@ bool FullTextSearch(CommandLine &CmdL) /*{{{*/ } if (all_found == true) { - std::stringstream outs; - ListSingleVersion(CacheFile, records, V, outs, format); - output_map.insert(std::make_pair( - PkgName, outs.str())); + PkgsDone[P->ID] = true; + std::stringstream outs; + ListSingleVersion(CacheFile, records, V, outs, format); + output_map.insert(std::make_pair( + PkgName, outs.str())); } } APT_FREE_PATTERNS(); -- cgit v1.2.3 From 22da5c135a74eee8ed998806136e25b8ed038bd0 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 9 Sep 2014 13:52:32 +0200 Subject: don't call pager in non-terminals for changelog Most pagers are nice and default to running non-interactively if they aren't connected to a terminal and we relied on that. On ci.debian.net the configured pager is printing a header out of nowhere though, so if we are printing to a non-terminal we call "cat" instead. In the rework we also "remove" the dependency on sensible-utils in sofar as we call some alternatives if calling the utils fail. This seems to be the last problem preventing a "PASS" status on ci.debian.net, so we close the associated bugreport. Closes: 755040 --- apt-private/private-utils.cc | 58 +++++++++++++++++++++++++++++++------------- apt-private/private-utils.h | 4 +-- 2 files changed, 43 insertions(+), 19 deletions(-) (limited to 'apt-private') diff --git a/apt-private/private-utils.cc b/apt-private/private-utils.cc index 9547a1b75..34af83c08 100644 --- a/apt-private/private-utils.cc +++ b/apt-private/private-utils.cc @@ -8,45 +8,69 @@ #include #include -// DisplayFileInPager - Display File with pager /*{{{*/ -void DisplayFileInPager(std::string filename) +// DisplayFileInPager - Display File with pager /*{{{*/ +void DisplayFileInPager(std::string const &filename) { - std::string pager = _config->Find("Dir::Bin::Pager", - "/usr/bin/sensible-pager"); - pid_t Process = ExecFork(); if (Process == 0) { const char *Args[3]; - Args[0] = pager.c_str(); Args[1] = filename.c_str(); - Args[2] = 0; + Args[2] = NULL; + if (isatty(STDOUT_FILENO) == 1) + { + // likely installed, provided by sensible-utils + std::string const pager = _config->Find("Dir::Bin::Pager", + "sensible-pager"); + Args[0] = pager.c_str(); + execvp(Args[0],(char **)Args); + // lets try some obvious alternatives + Args[0] = getenv("PAGER"); + if (Args[0] != NULL) + execvp(Args[0],(char **)Args); + + Args[0] = "pager"; + execvp(Args[0],(char **)Args); + } + // we could read the file ourselves, but… meh + Args[0] = "cat"; execvp(Args[0],(char **)Args); exit(100); } - + // Wait for the subprocess - ExecWait(Process, "sensible-pager", false); + ExecWait(Process, "pager", false); } /*}}}*/ -// EditFileInSensibleEditor - Edit File with editor /*{{{*/ -void EditFileInSensibleEditor(std::string filename) +// EditFileInSensibleEditor - Edit File with editor /*{{{*/ +void EditFileInSensibleEditor(std::string const &filename) { - std::string editor = _config->Find("Dir::Bin::Editor", - "/usr/bin/sensible-editor"); - pid_t Process = ExecFork(); if (Process == 0) { + // likely installed, provided by sensible-utils + std::string const editor = _config->Find("Dir::Bin::Editor", + "sensible-editor"); const char *Args[3]; Args[0] = editor.c_str(); Args[1] = filename.c_str(); - Args[2] = 0; + Args[2] = NULL; + execvp(Args[0],(char **)Args); + // the usual suspects we can try as an alternative + Args[0] = getenv("VISUAL"); + if (Args[0] != NULL) + execvp(Args[0],(char **)Args); + + Args[0] = getenv("EDITOR"); + if (Args[0] != NULL) + execvp(Args[0],(char **)Args); + + Args[0] = "editor"; execvp(Args[0],(char **)Args); exit(100); } - + // Wait for the subprocess - ExecWait(Process, "sensible-editor", false); + ExecWait(Process, "editor", false); } /*}}}*/ diff --git a/apt-private/private-utils.h b/apt-private/private-utils.h index 432699787..8ba480bd4 100644 --- a/apt-private/private-utils.h +++ b/apt-private/private-utils.h @@ -5,7 +5,7 @@ #include -APT_PUBLIC void DisplayFileInPager(std::string filename); -APT_PUBLIC void EditFileInSensibleEditor(std::string filename); +APT_PUBLIC void DisplayFileInPager(std::string const &filename); +APT_PUBLIC void EditFileInSensibleEditor(std::string const &filename); #endif -- cgit v1.2.3