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
/*{{{*/
43 #ifdef PACKAGE_MATCHER_ABI_COMPAT
44 #define PackageMatcher PackageNameMatchesFnmatch
47 PackageNameMatcher(const char **patterns
)
49 for(int i
=0; patterns
[i
] != NULL
; ++i
)
51 std::string pattern
= patterns
[i
];
52 #ifdef PACKAGE_MATCHER_ABI_COMPAT
53 APT::CacheFilter::PackageNameMatchesFnmatch
*cachefilter
= NULL
;
54 cachefilter
= new APT::CacheFilter::PackageNameMatchesFnmatch(pattern
);
56 APT::CacheFilter::PackageMatcher
*cachefilter
= NULL
;
57 if(_config
->FindB("APT::Cmd::Use-Regexp", false) == true)
58 cachefilter
= new APT::CacheFilter::PackageNameMatchesRegEx(pattern
);
60 cachefilter
= new APT::CacheFilter::PackageNameMatchesFnmatch(pattern
);
62 filters
.push_back(cachefilter
);
65 virtual ~PackageNameMatcher()
67 for(J
=filters
.begin(); J
!= filters
.end(); ++J
)
70 virtual bool operator () (const pkgCache::PkgIterator
&P
)
72 for(J
=filters
.begin(); J
!= filters
.end(); ++J
)
74 APT::CacheFilter::PackageMatcher
*cachefilter
= *J
;
82 std::vector
<APT::CacheFilter::PackageMatcher
*> filters
;
83 std::vector
<APT::CacheFilter::PackageMatcher
*>::const_iterator J
;
87 static void ListAllVersions(pkgCacheFile
&CacheFile
, pkgRecords
&records
,/*{{{*/
88 pkgCache::PkgIterator
const &P
, std::ostream
&outs
,
89 std::string
const &format
)
91 for (pkgCache::VerIterator Ver
= P
.VersionList();
92 Ver
.end() == false; ++Ver
)
94 ListSingleVersion(CacheFile
, records
, Ver
, outs
, format
);
99 // list - list package based on criteria /*{{{*/
100 // ---------------------------------------------------------------------
101 bool DoList(CommandLine
&Cmd
)
103 pkgCacheFile CacheFile
;
104 pkgCache
*Cache
= CacheFile
.GetPkgCache();
105 if (unlikely(Cache
== NULL
))
107 pkgRecords
records(CacheFile
);
109 const char **patterns
;
110 const char *all_pattern
[] = { "*", NULL
};
112 if (strv_length(Cmd
.FileList
+ 1) == 0)
114 patterns
= all_pattern
;
116 patterns
= Cmd
.FileList
+ 1;
119 std::string format
= "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}";
120 if (_config
->FindB("APT::Cmd::List-Include-Summary", false) == true)
121 format
+= "\n ${Description}\n";
123 PackageNameMatcher
matcher(patterns
);
124 LocalitySortedVersionSet bag
;
125 OpTextProgress
progress(*_config
);
126 progress
.OverallProgress(0,
127 Cache
->Head().PackageCount
,
128 Cache
->Head().PackageCount
,
130 GetLocalitySortedVersionSet(CacheFile
, &bag
, matcher
, &progress
);
131 bool const ShowAllVersions
= _config
->FindB("APT::Cmd::All-Versions", false);
132 std::map
<std::string
, std::string
> output_map
;
133 for (LocalitySortedVersionSet::iterator V
= bag
.begin(); V
!= bag
.end(); ++V
)
135 std::stringstream outs
;
136 if(ShowAllVersions
== true)
137 ListAllVersions(CacheFile
, records
, V
.ParentPkg(), outs
, format
);
139 ListSingleVersion(CacheFile
, records
, V
, outs
, format
);
140 output_map
.insert(std::make_pair
<std::string
, std::string
>(
141 V
.ParentPkg().Name(), outs
.str()));
144 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
145 // output the sorted map
146 std::map
<std::string
, std::string
>::const_iterator K
;
147 for (K
= output_map
.begin(); K
!= output_map
.end(); ++K
)
148 std::cout
<< (*K
).second
<< std::endl
;
150 // be nice and tell the user if there is more to see
151 if (bag
.size() == 1 && ShowAllVersions
== false)
153 // start with -1 as we already displayed one version
155 pkgCache::VerIterator Ver
= *bag
.begin();
156 for ( ; Ver
.end() == false; ++Ver
)
159 _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
);