1 // Include Files /*{{{*/
4 #include <apt-pkg/cachefile.h>
5 #include <apt-pkg/cachefilter.h>
6 #include <apt-pkg/cacheset.h>
7 #include <apt-pkg/cmndline.h>
8 #include <apt-pkg/pkgrecords.h>
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>
16 #include <apt-private/private-cacheset.h>
17 #include <apt-private/private-list.h>
18 #include <apt-private/private-output.h>
30 struct PackageSortAlphabetic
/*{{{*/
32 bool operator () (const pkgCache::PkgIterator
&p_lhs
,
33 const pkgCache::PkgIterator
&p_rhs
)
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
);
41 class PackageNameMatcher
: public Matcher
44 PackageNameMatcher(const char **patterns
)
46 for(int i
=0; patterns
[i
] != NULL
; ++i
)
48 std::string pattern
= patterns
[i
];
49 APT::CacheFilter::PackageMatcher
*cachefilter
= NULL
;
50 if(_config
->FindB("APT::Cmd::Use-Regexp", false) == true)
51 cachefilter
= new APT::CacheFilter::PackageNameMatchesRegEx(pattern
);
53 cachefilter
= new APT::CacheFilter::PackageNameMatchesFnmatch(pattern
);
54 filters
.push_back(cachefilter
);
57 virtual ~PackageNameMatcher()
59 for(J
=filters
.begin(); J
!= filters
.end(); ++J
)
62 virtual bool operator () (const pkgCache::PkgIterator
&P
) APT_OVERRIDE
64 for(J
=filters
.begin(); J
!= filters
.end(); ++J
)
66 APT::CacheFilter::PackageMatcher
*cachefilter
= *J
;
74 std::vector
<APT::CacheFilter::PackageMatcher
*> filters
;
75 std::vector
<APT::CacheFilter::PackageMatcher
*>::const_iterator J
;
79 static void ListAllVersions(pkgCacheFile
&CacheFile
, pkgRecords
&records
,/*{{{*/
80 pkgCache::PkgIterator
const &P
, std::ostream
&outs
,
81 std::string
const &format
)
83 for (pkgCache::VerIterator Ver
= P
.VersionList();
84 Ver
.end() == false; ++Ver
)
86 ListSingleVersion(CacheFile
, records
, Ver
, outs
, format
);
91 // list - list package based on criteria /*{{{*/
92 // ---------------------------------------------------------------------
93 bool DoList(CommandLine
&Cmd
)
95 pkgCacheFile CacheFile
;
96 pkgCache
*Cache
= CacheFile
.GetPkgCache();
97 if (unlikely(Cache
== NULL
))
99 pkgRecords
records(CacheFile
);
101 const char **patterns
;
102 const char *all_pattern
[] = { "*", NULL
};
104 if (strv_length(Cmd
.FileList
+ 1) == 0)
106 patterns
= all_pattern
;
108 patterns
= Cmd
.FileList
+ 1;
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";
115 PackageNameMatcher
matcher(patterns
);
116 LocalitySortedVersionSet bag
;
117 OpTextProgress
progress(*_config
);
118 progress
.OverallProgress(0,
119 Cache
->Head().PackageCount
,
120 Cache
->Head().PackageCount
,
122 GetLocalitySortedVersionSet(CacheFile
, &bag
, matcher
, &progress
);
123 bool const ShowAllVersions
= _config
->FindB("APT::Cmd::All-Versions", false);
124 std::map
<std::string
, std::string
> output_map
;
125 for (LocalitySortedVersionSet::iterator V
= bag
.begin(); V
!= bag
.end(); ++V
)
127 std::stringstream outs
;
128 if(ShowAllVersions
== true)
129 ListAllVersions(CacheFile
, records
, V
.ParentPkg(), outs
, format
);
131 ListSingleVersion(CacheFile
, records
, V
, outs
, format
);
132 output_map
.insert(std::make_pair
<std::string
, std::string
>(
133 V
.ParentPkg().Name(), outs
.str()));
136 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
137 // output the sorted map
138 std::map
<std::string
, std::string
>::const_iterator K
;
139 for (K
= output_map
.begin(); K
!= output_map
.end(); ++K
)
140 std::cout
<< (*K
).second
<< std::endl
;
142 // be nice and tell the user if there is more to see
143 if (bag
.size() == 1 && ShowAllVersions
== false)
145 // start with -1 as we already displayed one version
147 pkgCache::VerIterator Ver
= *bag
.begin();
148 for ( ; Ver
.end() == false; ++Ver
)
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
);