You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

157 lines
4.7 KiB

  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. for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V)
  123. {
  124. std::stringstream outs;
  125. if(_config->FindB("APT::Cmd::All-Versions", false) == true)
  126. {
  127. ListAllVersions(CacheFile, records, V.ParentPkg(), outs, includeSummary);
  128. output_map.insert(std::make_pair<std::string, std::string>(
  129. V.ParentPkg().Name(), outs.str()));
  130. } else {
  131. ListSingleVersion(CacheFile, records, V, outs, includeSummary);
  132. output_map.insert(std::make_pair<std::string, std::string>(
  133. V.ParentPkg().Name(), outs.str()));
  134. }
  135. }
  136. // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
  137. // output the sorted map
  138. for (K = output_map.begin(); K != output_map.end(); ++K)
  139. std::cout << (*K).second << std::endl;
  140. return true;
  141. }