private-list.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /*}}}*/
  36. class PackageNameMatcher : public Matcher /*{{{*/
  37. {
  38. #ifdef PACKAGE_MATCHER_ABI_COMPAT
  39. #define PackageMatcher PackageNameMatchesFnmatch
  40. #endif
  41. public:
  42. PackageNameMatcher(const char **patterns)
  43. {
  44. for(int i=0; patterns[i] != NULL; ++i)
  45. {
  46. std::string pattern = patterns[i];
  47. #ifdef PACKAGE_MATCHER_ABI_COMPAT
  48. APT::CacheFilter::PackageNameMatchesFnmatch *cachefilter = NULL;
  49. cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
  50. #else
  51. APT::CacheFilter::PackageMatcher *cachefilter = NULL;
  52. if(_config->FindB("APT::Cmd::Use-Regexp", false) == true)
  53. cachefilter = new APT::CacheFilter::PackageNameMatchesRegEx(pattern);
  54. else
  55. cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
  56. #endif
  57. filters.push_back(cachefilter);
  58. }
  59. }
  60. virtual ~PackageNameMatcher()
  61. {
  62. for(J=filters.begin(); J != filters.end(); ++J)
  63. delete *J;
  64. }
  65. virtual bool operator () (const pkgCache::PkgIterator &P)
  66. {
  67. for(J=filters.begin(); J != filters.end(); ++J)
  68. {
  69. APT::CacheFilter::PackageMatcher *cachefilter = *J;
  70. if((*cachefilter)(P))
  71. return true;
  72. }
  73. return false;
  74. }
  75. private:
  76. std::vector<APT::CacheFilter::PackageMatcher*> filters;
  77. std::vector<APT::CacheFilter::PackageMatcher*>::const_iterator J;
  78. #undef PackageMatcher
  79. };
  80. /*}}}*/
  81. static void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records,/*{{{*/
  82. pkgCache::PkgIterator const &P, std::ostream &outs,
  83. std::string const &format)
  84. {
  85. for (pkgCache::VerIterator Ver = P.VersionList();
  86. Ver.end() == false; ++Ver)
  87. {
  88. ListSingleVersion(CacheFile, records, Ver, outs, format);
  89. outs << std::endl;
  90. }
  91. }
  92. /*}}}*/
  93. // list - list package based on criteria /*{{{*/
  94. // ---------------------------------------------------------------------
  95. bool DoList(CommandLine &Cmd)
  96. {
  97. pkgCacheFile CacheFile;
  98. pkgCache *Cache = CacheFile.GetPkgCache();
  99. if (unlikely(Cache == NULL))
  100. return false;
  101. pkgRecords records(CacheFile);
  102. const char **patterns;
  103. const char *all_pattern[] = { "*", NULL};
  104. if (strv_length(Cmd.FileList + 1) == 0)
  105. {
  106. patterns = all_pattern;
  107. } else {
  108. patterns = Cmd.FileList + 1;
  109. }
  110. std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}";
  111. if (_config->FindB("APT::Cmd::List-Include-Summary", false) == true)
  112. format += "\n ${Description}\n";
  113. PackageNameMatcher matcher(patterns);
  114. LocalitySortedVersionSet bag;
  115. OpTextProgress progress(*_config);
  116. progress.OverallProgress(0,
  117. Cache->Head().PackageCount,
  118. Cache->Head().PackageCount,
  119. _("Listing"));
  120. GetLocalitySortedVersionSet(CacheFile, &bag, matcher, &progress);
  121. bool const ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false);
  122. std::map<std::string, std::string> output_map;
  123. for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V)
  124. {
  125. std::stringstream outs;
  126. if(ShowAllVersions == true)
  127. ListAllVersions(CacheFile, records, V.ParentPkg(), outs, format);
  128. else
  129. ListSingleVersion(CacheFile, records, V, outs, format);
  130. output_map.insert(std::make_pair<std::string, std::string>(
  131. V.ParentPkg().Name(), outs.str()));
  132. }
  133. // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
  134. // output the sorted map
  135. std::map<std::string, std::string>::const_iterator K;
  136. for (K = output_map.begin(); K != output_map.end(); ++K)
  137. std::cout << (*K).second << std::endl;
  138. // be nice and tell the user if there is more to see
  139. if (bag.size() == 1 && ShowAllVersions == false)
  140. {
  141. // start with -1 as we already displayed one version
  142. int versions = -1;
  143. pkgCache::VerIterator Ver = *bag.begin();
  144. for ( ; Ver.end() == false; ++Ver)
  145. ++versions;
  146. if (versions > 0)
  147. _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);
  148. }
  149. return true;
  150. }