private-list.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 P,
  83. std::ostream &outs,
  84. bool include_summary=true)
  85. {
  86. for (pkgCache::VerIterator Ver = P.VersionList();
  87. Ver.end() == false; ++Ver)
  88. {
  89. ListSingleVersion(CacheFile, records, Ver, outs, include_summary);
  90. outs << "\n";
  91. }
  92. }
  93. /*}}}*/
  94. // list - list package based on criteria /*{{{*/
  95. // ---------------------------------------------------------------------
  96. bool DoList(CommandLine &Cmd)
  97. {
  98. pkgCacheFile CacheFile;
  99. pkgCache *Cache = CacheFile.GetPkgCache();
  100. if (unlikely(Cache == NULL))
  101. return false;
  102. pkgRecords records(CacheFile);
  103. const char **patterns;
  104. const char *all_pattern[] = { "*", NULL};
  105. if (strv_length(Cmd.FileList + 1) == 0)
  106. {
  107. patterns = all_pattern;
  108. } else {
  109. patterns = Cmd.FileList + 1;
  110. }
  111. std::map<std::string, std::string> output_map;
  112. std::map<std::string, std::string>::const_iterator K;
  113. bool includeSummary = _config->FindB("APT::Cmd::List-Include-Summary");
  114. PackageNameMatcher matcher(patterns);
  115. LocalitySortedVersionSet bag;
  116. OpTextProgress progress(*_config);
  117. progress.OverallProgress(0,
  118. Cache->Head().PackageCount,
  119. Cache->Head().PackageCount,
  120. _("Listing"));
  121. GetLocalitySortedVersionSet(CacheFile, bag, matcher, progress);
  122. bool ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false);
  123. for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V)
  124. {
  125. std::stringstream outs;
  126. if(ShowAllVersions == true)
  127. {
  128. ListAllVersions(CacheFile, records, V.ParentPkg(), outs, includeSummary);
  129. output_map.insert(std::make_pair<std::string, std::string>(
  130. V.ParentPkg().Name(), outs.str()));
  131. } else {
  132. ListSingleVersion(CacheFile, records, V, outs, includeSummary);
  133. output_map.insert(std::make_pair<std::string, std::string>(
  134. V.ParentPkg().Name(), outs.str()));
  135. }
  136. }
  137. // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
  138. // output the sorted map
  139. for (K = output_map.begin(); K != output_map.end(); ++K)
  140. std::cout << (*K).second << std::endl;
  141. // be nice and tell the user if there is more to see
  142. if (bag.size() == 1 && ShowAllVersions == false)
  143. {
  144. // start with -1 as we already displayed one version
  145. int versions = -1;
  146. pkgCache::VerIterator Ver = *bag.begin();
  147. for ( ; Ver.end() == false; Ver++)
  148. versions++;
  149. if (versions > 0)
  150. _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);
  151. }
  152. return true;
  153. }