]> git.saurik.com Git - apt.git/blame - apt-private/private-list.cc
apt-helper: Add a cat-file command for concatening files
[apt.git] / apt-private / private-list.cc
CommitLineData
b9179170
MV
1// Include Files /*{{{*/
2#include <config.h>
3
b9179170
MV
4#include <apt-pkg/cachefile.h>
5#include <apt-pkg/cachefilter.h>
6#include <apt-pkg/cacheset.h>
b9179170 7#include <apt-pkg/cmndline.h>
b9179170 8#include <apt-pkg/pkgrecords.h>
453b82a3
DK
9#include <apt-pkg/progress.h>
10#include <apt-pkg/strutl.h>
11#include <apt-pkg/configuration.h>
12#include <apt-pkg/macros.h>
13#include <apt-pkg/pkgcache.h>
14#include <apt-pkg/cacheiterators.h>
15
16#include <apt-private/private-cacheset.h>
17#include <apt-private/private-list.h>
18#include <apt-private/private-output.h>
b9179170 19
453b82a3 20#include <iostream>
b9179170 21#include <sstream>
453b82a3
DK
22#include <map>
23#include <string>
b9179170 24#include <utility>
453b82a3 25#include <vector>
b9179170
MV
26
27#include <apti18n.h>
28 /*}}}*/
29
ee0167c4 30struct PackageSortAlphabetic /*{{{*/
b9179170
MV
31{
32 bool operator () (const pkgCache::PkgIterator &p_lhs,
33 const pkgCache::PkgIterator &p_rhs)
34 {
35 const std::string &l_name = p_lhs.FullName(true);
36 const std::string &r_name = p_rhs.FullName(true);
37 return (l_name < r_name);
38 }
39};
38d2959f
MV
40
41class PackageNameMatcher : public Matcher
ee0167c4 42{
b9179170 43 public:
258b9e51 44 explicit PackageNameMatcher(const char **patterns)
b9179170 45 {
9ce3cfc9 46 for(int i=0; patterns[i] != NULL; ++i)
b9179170
MV
47 {
48 std::string pattern = patterns[i];
49 APT::CacheFilter::PackageMatcher *cachefilter = NULL;
c1a61d1c 50 if(_config->FindB("APT::Cmd::Use-Regexp", false) == true)
b9179170
MV
51 cachefilter = new APT::CacheFilter::PackageNameMatchesRegEx(pattern);
52 else
53 cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
54 filters.push_back(cachefilter);
55 }
56 }
57 virtual ~PackageNameMatcher()
58 {
9ce3cfc9 59 for(J=filters.begin(); J != filters.end(); ++J)
b9179170
MV
60 delete *J;
61 }
6d7122b5 62 virtual bool operator () (const pkgCache::PkgIterator &P) APT_OVERRIDE
b9179170 63 {
9ce3cfc9 64 for(J=filters.begin(); J != filters.end(); ++J)
b9179170
MV
65 {
66 APT::CacheFilter::PackageMatcher *cachefilter = *J;
67 if((*cachefilter)(P))
68 return true;
69 }
70 return false;
71 }
72
73private:
74 std::vector<APT::CacheFilter::PackageMatcher*> filters;
75 std::vector<APT::CacheFilter::PackageMatcher*>::const_iterator J;
dd4d9729 76 #undef PackageMatcher
b9179170 77};
ee0167c4 78 /*}}}*/
c3ccac92 79static void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records,/*{{{*/
1a68655d
DK
80 pkgCache::PkgIterator const &P, std::ostream &outs,
81 std::string const &format)
b9179170
MV
82{
83 for (pkgCache::VerIterator Ver = P.VersionList();
796673c3 84 Ver.end() == false; ++Ver)
14109555 85 {
1a68655d
DK
86 ListSingleVersion(CacheFile, records, Ver, outs, format);
87 outs << std::endl;
14109555 88 }
b9179170 89}
ee0167c4 90 /*}}}*/
b9179170
MV
91// list - list package based on criteria /*{{{*/
92// ---------------------------------------------------------------------
ec4371ac 93bool DoList(CommandLine &Cmd)
b9179170
MV
94{
95 pkgCacheFile CacheFile;
96 pkgCache *Cache = CacheFile.GetPkgCache();
b9179170
MV
97 if (unlikely(Cache == NULL))
98 return false;
ec4371ac 99 pkgRecords records(CacheFile);
b9179170
MV
100
101 const char **patterns;
102 const char *all_pattern[] = { "*", NULL};
103
104 if (strv_length(Cmd.FileList + 1) == 0)
105 {
106 patterns = all_pattern;
107 } else {
108 patterns = Cmd.FileList + 1;
109 }
110
1a68655d
DK
111 std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}";
112 if (_config->FindB("APT::Cmd::List-Include-Summary", false) == true)
113 format += "\n ${Description}\n";
e1dc051a 114
b9179170
MV
115 PackageNameMatcher matcher(patterns);
116 LocalitySortedVersionSet bag;
14109555 117 OpTextProgress progress(*_config);
b9179170
MV
118 progress.OverallProgress(0,
119 Cache->Head().PackageCount,
120 Cache->Head().PackageCount,
121 _("Listing"));
25594bb5 122 GetLocalitySortedVersionSet(CacheFile, &bag, matcher, &progress);
1a68655d
DK
123 bool const ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false);
124 std::map<std::string, std::string> output_map;
9ce3cfc9 125 for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V)
b9179170
MV
126 {
127 std::stringstream outs;
d6570f85 128 if(ShowAllVersions == true)
1a68655d
DK
129 ListAllVersions(CacheFile, records, V.ParentPkg(), outs, format);
130 else
131 ListSingleVersion(CacheFile, records, V, outs, format);
132 output_map.insert(std::make_pair<std::string, std::string>(
133 V.ParentPkg().Name(), outs.str()));
b9179170
MV
134 }
135
136 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
137 // output the sorted map
1a68655d 138 std::map<std::string, std::string>::const_iterator K;
9ce3cfc9 139 for (K = output_map.begin(); K != output_map.end(); ++K)
b9179170
MV
140 std::cout << (*K).second << std::endl;
141
d6570f85
MV
142 // be nice and tell the user if there is more to see
143 if (bag.size() == 1 && ShowAllVersions == false)
144 {
145 // start with -1 as we already displayed one version
146 int versions = -1;
147 pkgCache::VerIterator Ver = *bag.begin();
1a68655d
DK
148 for ( ; Ver.end() == false; ++Ver)
149 ++versions;
d6570f85
MV
150 if (versions > 0)
151 _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);
152 }
153
b9179170
MV
154 return true;
155}
156