private-list.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #ifdef PACKAGE_MATCHER_ABI_COMPAT
  51. #define PackageMatcher PackageNameMatchesFnmatch
  52. #endif
  53. class PackageNameMatcher : public Matcher
  54. {
  55. public:
  56. PackageNameMatcher(const char **patterns)
  57. {
  58. for(int i=0; patterns[i] != NULL; i++)
  59. {
  60. std::string pattern = patterns[i];
  61. #ifdef PACKAGE_MATCHER_ABI_COMPAT
  62. APT::CacheFilter::PackageNameMatchesFnmatch *cachefilter = NULL;
  63. cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
  64. #else
  65. APT::CacheFilter::PackageMatcher *cachefilter = NULL;
  66. if(_config->FindB("APT::Cmd::UseRegexp", false) == true)
  67. cachefilter = new APT::CacheFilter::PackageNameMatchesRegEx(pattern);
  68. else
  69. cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
  70. #endif
  71. filters.push_back(cachefilter);
  72. }
  73. }
  74. virtual ~PackageNameMatcher()
  75. {
  76. for(J=filters.begin(); J != filters.end(); J++)
  77. delete *J;
  78. }
  79. virtual bool operator () (const pkgCache::PkgIterator &P)
  80. {
  81. for(J=filters.begin(); J != filters.end(); J++)
  82. {
  83. APT::CacheFilter::PackageMatcher *cachefilter = *J;
  84. if((*cachefilter)(P))
  85. return true;
  86. }
  87. return false;
  88. }
  89. private:
  90. std::vector<APT::CacheFilter::PackageMatcher*> filters;
  91. std::vector<APT::CacheFilter::PackageMatcher*>::const_iterator J;
  92. #undef PackageMatcher
  93. };
  94. void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records,
  95. pkgCache::PkgIterator P,
  96. std::ostream &outs)
  97. {
  98. for (pkgCache::VerIterator Ver = P.VersionList();
  99. Ver.end() == false; Ver++)
  100. ListSingleVersion(CacheFile, records, Ver, outs);
  101. }
  102. // list - list package based on criteria /*{{{*/
  103. // ---------------------------------------------------------------------
  104. bool List(CommandLine &Cmd)
  105. {
  106. pkgCacheFile CacheFile;
  107. pkgCache *Cache = CacheFile.GetPkgCache();
  108. pkgRecords records(CacheFile);
  109. if (unlikely(Cache == NULL))
  110. return false;
  111. const char **patterns;
  112. const char *all_pattern[] = { "*", NULL};
  113. if (strv_length(Cmd.FileList + 1) == 0)
  114. {
  115. patterns = all_pattern;
  116. } else {
  117. patterns = Cmd.FileList + 1;
  118. }
  119. std::map<std::string, std::string> output_map;
  120. std::map<std::string, std::string>::const_iterator K;
  121. PackageNameMatcher matcher(patterns);
  122. LocalitySortedVersionSet bag;
  123. OpTextProgress progress;
  124. progress.OverallProgress(0,
  125. Cache->Head().PackageCount,
  126. Cache->Head().PackageCount,
  127. _("Listing"));
  128. GetLocalitySortedVersionSet(CacheFile, bag, matcher, progress);
  129. for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); V++)
  130. {
  131. std::stringstream outs;
  132. if(_config->FindB("APT::Cmd::AllVersions", false) == true)
  133. {
  134. ListAllVersions(CacheFile, records, V.ParentPkg(), outs);
  135. output_map.insert(std::make_pair<std::string, std::string>(
  136. V.ParentPkg().Name(), outs.str()));
  137. } else {
  138. ListSingleVersion(CacheFile, records, V, outs);
  139. output_map.insert(std::make_pair<std::string, std::string>(
  140. V.ParentPkg().Name(), outs.str()));
  141. }
  142. }
  143. // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
  144. // output the sorted map
  145. for (K = output_map.begin(); K != output_map.end(); K++)
  146. std::cout << (*K).second << std::endl;
  147. return true;
  148. }