apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

private-list.cc
Go to the documentation of this file.
1 // Include Files /*{{{*/
2 #include <config.h>
3 
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/configuration.h>
9 #include <apt-pkg/macros.h>
10 #include <apt-pkg/pkgcache.h>
11 #include <apt-pkg/pkgrecords.h>
12 #include <apt-pkg/progress.h>
13 #include <apt-pkg/strutl.h>
14 
18 
19 #include <iostream>
20 #include <map>
21 #include <sstream>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include <apti18n.h>
27  /*}}}*/
28 
29 struct PackageSortAlphabetic /*{{{*/
30 {
31  bool operator () (const pkgCache::PkgIterator &p_lhs,
32  const pkgCache::PkgIterator &p_rhs)
33  {
34  const std::string &l_name = p_lhs.FullName(true);
35  const std::string &r_name = p_rhs.FullName(true);
36  return (l_name < r_name);
37  }
38 };
39 
41 {
42  static constexpr const char *const isfnmatch_strict = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-.:*";
44  public:
45  explicit PackageNameMatcher(pkgCacheFile &cacheFile, const char **patterns)
47  {
48  for(int i=0; patterns[i] != NULL; ++i)
49  {
50  std::string pattern = patterns[i];
51  APT::CacheFilter::Matcher *cachefilter = NULL;
52  if(_config->FindB("APT::Cmd::Use-Regexp", false) == true)
53  cachefilter = new APT::CacheFilter::PackageNameMatchesRegEx(pattern);
54  else if (pattern.find_first_not_of(isfnmatch_strict) == std::string::npos)
55  cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
56  else
57  cachefilter = APT::CacheFilter::ParsePattern(pattern, &cacheFile).release();
58 
59  if (cachefilter == nullptr) {
60  return;
61  filters.clear();
62  }
63  filters.push_back(cachefilter);
64  }
65  }
67  {
68  for(J=filters.begin(); J != filters.end(); ++J)
69  delete *J;
70  }
72  {
73  for(J=filters.begin(); J != filters.end(); ++J)
74  {
75  APT::CacheFilter::Matcher *cachefilter = *J;
76  if((*cachefilter)(P))
77  return true;
78  }
79  return false;
80  }
81 
82 private:
83  std::vector<APT::CacheFilter::Matcher*> filters;
84  std::vector<APT::CacheFilter::Matcher*>::const_iterator J;
85  #undef PackageMatcher
86 };
87  /*}}}*/
88 static void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records,/*{{{*/
89  pkgCache::PkgIterator const &P, std::ostream &outs,
90  std::string const &format)
91 {
92  for (pkgCache::VerIterator Ver = P.VersionList();
93  Ver.end() == false; ++Ver)
94  {
95  ListSingleVersion(CacheFile, records, Ver, outs, format);
96  outs << std::endl;
97  }
98 }
99  /*}}}*/
100 // list - list package based on criteria /*{{{*/
101 // ---------------------------------------------------------------------
102 bool DoList(CommandLine &Cmd)
103 {
105  pkgCache * const Cache = CacheFile.GetPkgCache();
106  if (unlikely(Cache == nullptr || CacheFile.GetDepCache() == nullptr))
107  return false;
108  pkgRecords records(CacheFile);
109 
110  const char **patterns;
111  const char *all_pattern[] = { "*", NULL};
112 
113  if (strv_length(Cmd.FileList + 1) == 0)
114  {
115  patterns = all_pattern;
116  } else {
117  patterns = Cmd.FileList + 1;
118  }
119 
120  std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}";
121  if (_config->FindB("APT::Cmd::List-Include-Summary", false) == true)
122  format += "\n ${Description}\n";
123 
124  PackageNameMatcher matcher(CacheFile, patterns);
126  OpTextProgress progress(*_config);
127  progress.OverallProgress(0,
128  Cache->Head().PackageCount,
129  Cache->Head().PackageCount,
130  _("Listing"));
131  GetLocalitySortedVersionSet(CacheFile, &bag, matcher, &progress);
132  bool const ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false);
133  std::map<std::string, std::string> output_map;
134  for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V)
135  {
136  std::stringstream outs;
137  if(ShowAllVersions == true)
138  ListAllVersions(CacheFile, records, V.ParentPkg(), outs, format);
139  else
140  ListSingleVersion(CacheFile, records, V, outs, format);
141  output_map.insert(std::make_pair<std::string, std::string>(
142  V.ParentPkg().FullName(), outs.str()));
143  }
144 
145  // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
146  // output the sorted map
147  std::map<std::string, std::string>::const_iterator K;
148  for (K = output_map.begin(); K != output_map.end(); ++K)
149  std::cout << (*K).second << std::endl;
150 
151  // be nice and tell the user if there is more to see
152  if (bag.size() == 1 && ShowAllVersions == false)
153  {
154  // start with -1 as we already displayed one version
155  int versions = -1;
156  pkgCache::VerIterator Ver = *bag.begin();
157  for ( ; Ver.end() == false; ++Ver)
158  ++versions;
159  if (versions > 0)
160  _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);
161  }
162 
163  return true;
164 }
165 
size_t size() const APT_OVERRIDE
Definition: cacheset.h:817
const_iterator begin() const
Definition: cacheset.h:825
const_iterator end() const
Definition: cacheset.h:826
const char ** FileList
Definition: cmndline.h:78
bool FindB(const char *Name, bool const &Default=false) const
void OverallProgress(unsigned long long Current, unsigned long long Total, unsigned long long Size, const std::string &Op)
Definition: progress.cc:55
static constexpr const char *const isfnmatch_strict
Definition: private-list.cc:42
PackageNameMatcher(pkgCacheFile &cacheFile, const char **patterns)
Definition: private-list.cc:45
pkgCacheFile & cacheFile
Definition: private-list.cc:43
std::vector< APT::CacheFilter::Matcher * > filters
Definition: private-list.cc:83
virtual bool operator()(const pkgCache::PkgIterator &P) APT_OVERRIDE
Definition: private-list.cc:71
virtual ~PackageNameMatcher()
Definition: private-list.cc:66
std::vector< APT::CacheFilter::Matcher * >::const_iterator J
Definition: private-list.cc:84
pkgDepCache * GetDepCache()
Definition: cachefile.h:74
pkgCache * GetPkgCache()
Definition: cachefile.h:73
Configuration * _config
#define APT_OVERRIDE
Definition: macros.h:111
APT_PUBLIC std::unique_ptr< APT::CacheFilter::Matcher > ParsePattern(APT::StringView pattern, pkgCacheFile *file)
Parse a pattern, return nullptr or pattern.
pkgCache - Structure definitions for the cache file
bool GetLocalitySortedVersionSet(pkgCacheFile &CacheFile, APT::VersionContainerInterface *const vci, OpProgress *const progress)
static void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records, pkgCache::PkgIterator const &P, std::ostream &outs, std::string const &format)
Definition: private-list.cc:88
bool DoList(CommandLine &Cmd)
void ListSingleVersion(pkgCacheFile &CacheFile, pkgRecords &records, pkgCache::VerIterator const &V, std::ostream &out, std::string const &format)
bool operator()(const pkgCache::PkgIterator &p_lhs, const pkgCache::PkgIterator &p_rhs)
Definition: private-list.cc:31
size_t strv_length(const char **str_array)
Definition: strutl.cc:1549