private-list.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/cachefile.h>
  4. #include <apt-pkg/cachefilter.h>
  5. #include <apt-pkg/cacheset.h>
  6. #include <apt-pkg/cmndline.h>
  7. #include <apt-pkg/pkgrecords.h>
  8. #include <apt-pkg/progress.h>
  9. #include <apt-pkg/strutl.h>
  10. #include <apt-pkg/configuration.h>
  11. #include <apt-pkg/macros.h>
  12. #include <apt-pkg/pkgcache.h>
  13. #include <apt-pkg/cacheiterators.h>
  14. #include <apt-private/private-cacheset.h>
  15. #include <apt-private/private-list.h>
  16. #include <apt-private/private-output.h>
  17. #include <iostream>
  18. #include <sstream>
  19. #include <map>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include <apti18n.h>
  24. /*}}}*/
  25. struct PackageSortAlphabetic /*{{{*/
  26. {
  27. bool operator () (const pkgCache::PkgIterator &p_lhs,
  28. const pkgCache::PkgIterator &p_rhs)
  29. {
  30. const std::string &l_name = p_lhs.FullName(true);
  31. const std::string &r_name = p_rhs.FullName(true);
  32. return (l_name < r_name);
  33. }
  34. };
  35. class PackageNameMatcher : public Matcher
  36. {
  37. public:
  38. explicit PackageNameMatcher(const char **patterns)
  39. {
  40. for(int i=0; patterns[i] != NULL; ++i)
  41. {
  42. std::string pattern = patterns[i];
  43. APT::CacheFilter::PackageMatcher *cachefilter = NULL;
  44. if(_config->FindB("APT::Cmd::Use-Regexp", false) == true)
  45. cachefilter = new APT::CacheFilter::PackageNameMatchesRegEx(pattern);
  46. else
  47. cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
  48. filters.push_back(cachefilter);
  49. }
  50. }
  51. virtual ~PackageNameMatcher()
  52. {
  53. for(J=filters.begin(); J != filters.end(); ++J)
  54. delete *J;
  55. }
  56. virtual bool operator () (const pkgCache::PkgIterator &P) APT_OVERRIDE
  57. {
  58. for(J=filters.begin(); J != filters.end(); ++J)
  59. {
  60. APT::CacheFilter::PackageMatcher *cachefilter = *J;
  61. if((*cachefilter)(P))
  62. return true;
  63. }
  64. return false;
  65. }
  66. private:
  67. std::vector<APT::CacheFilter::PackageMatcher*> filters;
  68. std::vector<APT::CacheFilter::PackageMatcher*>::const_iterator J;
  69. #undef PackageMatcher
  70. };
  71. /*}}}*/
  72. static void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records,/*{{{*/
  73. pkgCache::PkgIterator const &P, std::ostream &outs,
  74. std::string const &format)
  75. {
  76. for (pkgCache::VerIterator Ver = P.VersionList();
  77. Ver.end() == false; ++Ver)
  78. {
  79. ListSingleVersion(CacheFile, records, Ver, outs, format);
  80. outs << std::endl;
  81. }
  82. }
  83. /*}}}*/
  84. // list - list package based on criteria /*{{{*/
  85. // ---------------------------------------------------------------------
  86. bool DoList(CommandLine &Cmd)
  87. {
  88. pkgCacheFile CacheFile;
  89. pkgCache * const Cache = CacheFile.GetPkgCache();
  90. if (unlikely(Cache == nullptr || CacheFile.GetDepCache() == nullptr))
  91. return false;
  92. pkgRecords records(CacheFile);
  93. const char **patterns;
  94. const char *all_pattern[] = { "*", NULL};
  95. if (strv_length(Cmd.FileList + 1) == 0)
  96. {
  97. patterns = all_pattern;
  98. } else {
  99. patterns = Cmd.FileList + 1;
  100. }
  101. std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}";
  102. if (_config->FindB("APT::Cmd::List-Include-Summary", false) == true)
  103. format += "\n ${Description}\n";
  104. PackageNameMatcher matcher(patterns);
  105. LocalitySortedVersionSet bag;
  106. OpTextProgress progress(*_config);
  107. progress.OverallProgress(0,
  108. Cache->Head().PackageCount,
  109. Cache->Head().PackageCount,
  110. _("Listing"));
  111. GetLocalitySortedVersionSet(CacheFile, &bag, matcher, &progress);
  112. bool const ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false);
  113. std::map<std::string, std::string> output_map;
  114. for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V)
  115. {
  116. std::stringstream outs;
  117. if(ShowAllVersions == true)
  118. ListAllVersions(CacheFile, records, V.ParentPkg(), outs, format);
  119. else
  120. ListSingleVersion(CacheFile, records, V, outs, format);
  121. output_map.insert(std::make_pair<std::string, std::string>(
  122. V.ParentPkg().Name(), outs.str()));
  123. }
  124. // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
  125. // output the sorted map
  126. std::map<std::string, std::string>::const_iterator K;
  127. for (K = output_map.begin(); K != output_map.end(); ++K)
  128. std::cout << (*K).second << std::endl;
  129. // be nice and tell the user if there is more to see
  130. if (bag.size() == 1 && ShowAllVersions == false)
  131. {
  132. // start with -1 as we already displayed one version
  133. int versions = -1;
  134. pkgCache::VerIterator Ver = *bag.begin();
  135. for ( ; Ver.end() == false; ++Ver)
  136. ++versions;
  137. if (versions > 0)
  138. _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);
  139. }
  140. return true;
  141. }