private-list.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Include Files /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/error.h>
  4. #include <apt-pkg/cachefile.h>
  5. #include <apt-pkg/cachefilter.h>
  6. #include <apt-pkg/cacheset.h>
  7. #include <apt-pkg/init.h>
  8. #include <apt-pkg/progress.h>
  9. #include <apt-pkg/sourcelist.h>
  10. #include <apt-pkg/cmndline.h>
  11. #include <apt-pkg/strutl.h>
  12. #include <apt-pkg/fileutl.h>
  13. #include <apt-pkg/pkgrecords.h>
  14. #include <apt-pkg/srcrecords.h>
  15. #include <apt-pkg/version.h>
  16. #include <apt-pkg/policy.h>
  17. #include <apt-pkg/tagfile.h>
  18. #include <apt-pkg/algorithms.h>
  19. #include <apt-pkg/sptr.h>
  20. #include <apt-pkg/pkgsystem.h>
  21. #include <apt-pkg/indexfile.h>
  22. #include <apt-pkg/metaindex.h>
  23. #include <sstream>
  24. #include <vector>
  25. #include <utility>
  26. #include <cassert>
  27. #include <locale.h>
  28. #include <iostream>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <regex.h>
  32. #include <stdio.h>
  33. #include <algorithm>
  34. #include "private-cmndline.h"
  35. #include "private-list.h"
  36. #include "private-output.h"
  37. #include "private-cacheset.h"
  38. #include <apti18n.h>
  39. /*}}}*/
  40. struct PackageSortAlphabetic /*{{{*/
  41. {
  42. bool operator () (const pkgCache::PkgIterator &p_lhs,
  43. const pkgCache::PkgIterator &p_rhs)
  44. {
  45. const std::string &l_name = p_lhs.FullName(true);
  46. const std::string &r_name = p_rhs.FullName(true);
  47. return (l_name < r_name);
  48. }
  49. };
  50. /*}}}*/
  51. class PackageNameMatcher : public Matcher /*{{{*/
  52. {
  53. #ifdef PACKAGE_MATCHER_ABI_COMPAT
  54. #define PackageMatcher PackageNameMatchesFnmatch
  55. #endif
  56. public:
  57. PackageNameMatcher(const char **patterns)
  58. {
  59. for(int i=0; patterns[i] != NULL; i++)
  60. {
  61. std::string pattern = patterns[i];
  62. #ifdef PACKAGE_MATCHER_ABI_COMPAT
  63. APT::CacheFilter::PackageNameMatchesFnmatch *cachefilter = NULL;
  64. cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
  65. #else
  66. APT::CacheFilter::PackageMatcher *cachefilter = NULL;
  67. if(_config->FindB("APT::Cmd::Use-Regexp", false) == true)
  68. cachefilter = new APT::CacheFilter::PackageNameMatchesRegEx(pattern);
  69. else
  70. cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
  71. #endif
  72. filters.push_back(cachefilter);
  73. }
  74. }
  75. virtual ~PackageNameMatcher()
  76. {
  77. for(J=filters.begin(); J != filters.end(); J++)
  78. delete *J;
  79. }
  80. virtual bool operator () (const pkgCache::PkgIterator &P)
  81. {
  82. for(J=filters.begin(); J != filters.end(); J++)
  83. {
  84. APT::CacheFilter::PackageMatcher *cachefilter = *J;
  85. if((*cachefilter)(P))
  86. return true;
  87. }
  88. return false;
  89. }
  90. private:
  91. std::vector<APT::CacheFilter::PackageMatcher*> filters;
  92. std::vector<APT::CacheFilter::PackageMatcher*>::const_iterator J;
  93. #undef PackageMatcher
  94. };
  95. /*}}}*/
  96. void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records, /*{{{*/
  97. pkgCache::PkgIterator P,
  98. std::ostream &outs)
  99. {
  100. for (pkgCache::VerIterator Ver = P.VersionList();
  101. Ver.end() == false; Ver++)
  102. ListSingleVersion(CacheFile, records, Ver, outs);
  103. }
  104. /*}}}*/
  105. // list - list package based on criteria /*{{{*/
  106. // ---------------------------------------------------------------------
  107. bool List(CommandLine &Cmd)
  108. {
  109. pkgCacheFile CacheFile;
  110. pkgCache *Cache = CacheFile.GetPkgCache();
  111. pkgRecords records(CacheFile);
  112. if (unlikely(Cache == NULL))
  113. return false;
  114. const char **patterns;
  115. const char *all_pattern[] = { "*", NULL};
  116. if (strv_length(Cmd.FileList + 1) == 0)
  117. {
  118. patterns = all_pattern;
  119. } else {
  120. patterns = Cmd.FileList + 1;
  121. }
  122. std::map<std::string, std::string> output_map;
  123. std::map<std::string, std::string>::const_iterator K;
  124. bool includeSummary = _config->FindB("APT::Cmd::List-Include-Summary");
  125. PackageNameMatcher matcher(patterns);
  126. LocalitySortedVersionSet bag;
  127. OpTextProgress progress;
  128. progress.OverallProgress(0,
  129. Cache->Head().PackageCount,
  130. Cache->Head().PackageCount,
  131. _("Listing"));
  132. GetLocalitySortedVersionSet(CacheFile, bag, matcher, progress);
  133. for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); V++)
  134. {
  135. std::stringstream outs;
  136. if(_config->FindB("APT::Cmd::All-Versions", false) == true)
  137. {
  138. ListAllVersions(CacheFile, records, V.ParentPkg(), outs);
  139. output_map.insert(std::make_pair<std::string, std::string>(
  140. V.ParentPkg().Name(), outs.str()));
  141. } else {
  142. ListSingleVersion(CacheFile, records, V, outs, includeSummary);
  143. output_map.insert(std::make_pair<std::string, std::string>(
  144. V.ParentPkg().Name(), outs.str()));
  145. }
  146. }
  147. // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
  148. // output the sorted map
  149. for (K = output_map.begin(); K != output_map.end(); K++)
  150. std::cout << (*K).second << std::endl;
  151. return true;
  152. }