"Fossies" - the Fresh Open Source Software Archive

Member "apt-1.9.4/apt-pkg/cachefilter-patterns.h" (19 Sep 2019, 5999 Bytes) of package /linux/misc/apt-1.9.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "cachefilter-patterns.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * cachefilter-patterns.h - Pattern parser and additional patterns as matchers
    3  *
    4  * Copyright (c) 2019 Canonical Ltd
    5  *
    6  * SPDX-License-Identifier: GPL-2.0+
    7  */
    8 
    9 #ifndef APT_CACHEFILTER_PATTERNS_H
   10 #define APT_CACHEFILTER_PATTERNS_H
   11 #include <apt-pkg/cachefile.h>
   12 #include <apt-pkg/cachefilter.h>
   13 #include <apt-pkg/error.h>
   14 #include <apt-pkg/string_view.h>
   15 #include <apt-pkg/strutl.h>
   16 #include <iostream>
   17 #include <memory>
   18 #include <sstream>
   19 #include <string>
   20 #include <vector>
   21 #include <assert.h>
   22 namespace APT
   23 {
   24 
   25 namespace Internal
   26 {
   27 /**
   28  * \brief PatternTreeParser parses the given sentence into a parse tree.
   29  *
   30  * The parse tree consists of nodes:
   31  *  - Word nodes which contains words or quoted words
   32  *  - Patterns, which represent ?foo and ?foo(...) patterns
   33  */
   34 struct PatternTreeParser
   35 {
   36 
   37    struct Node
   38    {
   39       size_t start = 0;
   40       size_t end = 0;
   41 
   42       virtual std::ostream &render(std::ostream &os) { return os; };
   43       std::nullptr_t error(std::string message);
   44    };
   45 
   46    struct Error : public std::exception
   47    {
   48       Node location;
   49       std::string message;
   50 
   51       Error(Node location, std::string message) : location(location), message(message) {}
   52       const char *what() const throw() override { return message.c_str(); }
   53    };
   54 
   55    struct PatternNode : public Node
   56    {
   57       APT::StringView term;
   58       std::vector<std::unique_ptr<Node>> arguments;
   59       bool haveArgumentList = false;
   60 
   61       std::ostream &render(std::ostream &stream) override;
   62       bool matches(APT::StringView name, int min, int max);
   63    };
   64 
   65    struct WordNode : public Node
   66    {
   67       APT::StringView word;
   68       bool quoted = false;
   69       std::ostream &render(std::ostream &stream) override;
   70    };
   71 
   72    struct State
   73    {
   74       off_t offset = 0;
   75    };
   76 
   77    APT::StringView sentence;
   78    State state;
   79 
   80    PatternTreeParser(APT::StringView sentence) : sentence(sentence){};
   81    off_t skipSpace()
   82    {
   83       while (sentence[state.offset] == ' ' || sentence[state.offset] == '\t' || sentence[state.offset] == '\r' || sentence[state.offset] == '\n')
   84      state.offset++;
   85       return state.offset;
   86    };
   87 
   88    /// \brief Parse a complete pattern
   89    ///
   90    /// There may not be anything before or after the pattern, except for
   91    /// whitespace.
   92    std::unique_ptr<Node> parseTop();
   93 
   94    private:
   95    std::unique_ptr<Node> parse();
   96    std::unique_ptr<Node> parsePattern();
   97    std::unique_ptr<Node> parseWord();
   98    std::unique_ptr<Node> parseQuotedWord();
   99 };
  100 
  101 /**
  102  * \brief PatternParser parses the given sentence into a parse tree.
  103  *
  104  * The parse tree consists of nodes:
  105  *  - Word nodes which contains words or quoted words
  106  *  - Patterns, which represent ?foo and ?foo(...) patterns
  107  */
  108 struct PatternParser
  109 {
  110    pkgCacheFile *file;
  111 
  112    std::unique_ptr<APT::CacheFilter::Matcher> aPattern(std::unique_ptr<PatternTreeParser::Node> &nodeP);
  113    std::string aWord(std::unique_ptr<PatternTreeParser::Node> &nodeP);
  114 };
  115 
  116 namespace Patterns
  117 {
  118 using namespace APT::CacheFilter;
  119 
  120 struct PackageIsAutomatic : public PackageMatcher
  121 {
  122    pkgCacheFile *Cache;
  123    explicit PackageIsAutomatic(pkgCacheFile *Cache) : Cache(Cache) {}
  124    bool operator()(pkgCache::PkgIterator const &Pkg) override
  125    {
  126       assert(Cache != nullptr);
  127       return ((*Cache)[Pkg].Flags & pkgCache::Flag::Auto) != 0;
  128    }
  129 };
  130 
  131 struct PackageIsBroken : public PackageMatcher
  132 {
  133    pkgCacheFile *Cache;
  134    explicit PackageIsBroken(pkgCacheFile *Cache) : Cache(Cache) {}
  135    bool operator()(pkgCache::PkgIterator const &Pkg) override
  136    {
  137       assert(Cache != nullptr);
  138       auto state = (*Cache)[Pkg];
  139       return state.InstBroken() || state.NowBroken();
  140    }
  141 };
  142 
  143 struct PackageIsConfigFiles : public PackageMatcher
  144 {
  145    bool operator()(pkgCache::PkgIterator const &Pkg) override
  146    {
  147       return Pkg->CurrentState == pkgCache::State::ConfigFiles;
  148    }
  149 };
  150 
  151 struct PackageIsGarbage : public PackageMatcher
  152 {
  153    pkgCacheFile *Cache;
  154    explicit PackageIsGarbage(pkgCacheFile *Cache) : Cache(Cache) {}
  155    bool operator()(pkgCache::PkgIterator const &Pkg) override
  156    {
  157       assert(Cache != nullptr);
  158       return (*Cache)[Pkg].Garbage;
  159    }
  160 };
  161 struct PackageIsEssential : public PackageMatcher
  162 {
  163    bool operator()(pkgCache::PkgIterator const &Pkg) override
  164    {
  165       return (Pkg->Flags & pkgCache::Flag::Essential) != 0;
  166    }
  167 };
  168 
  169 struct PackageHasExactName : public PackageMatcher
  170 {
  171    std::string name;
  172    explicit PackageHasExactName(std::string name) : name(name) {}
  173    bool operator()(pkgCache::PkgIterator const &Pkg) override
  174    {
  175       return Pkg.Name() == name;
  176    }
  177 };
  178 
  179 struct PackageIsInstalled : public PackageMatcher
  180 {
  181    pkgCacheFile *Cache;
  182    explicit PackageIsInstalled(pkgCacheFile *Cache) : Cache(Cache) {}
  183    bool operator()(pkgCache::PkgIterator const &Pkg) override
  184    {
  185       assert(Cache != nullptr);
  186       return Pkg->CurrentVer != 0;
  187    }
  188 };
  189 
  190 struct PackageIsObsolete : public PackageMatcher
  191 {
  192    bool operator()(pkgCache::PkgIterator const &pkg) override
  193    {
  194       // This code can be written without loops, as aptitude does, but it
  195       // is far less readable.
  196       if (pkg.CurrentVer().end())
  197      return false;
  198 
  199       // See if there is any version that exists in a repository,
  200       // if so return false
  201       for (auto ver = pkg.VersionList(); !ver.end(); ver++)
  202       {
  203      for (auto file = ver.FileList(); !file.end(); file++)
  204      {
  205         if ((file.File()->Flags & pkgCache::Flag::NotSource) == 0)
  206            return false;
  207      }
  208       }
  209 
  210       return true;
  211    }
  212 };
  213 
  214 struct PackageIsUpgradable : public PackageMatcher
  215 {
  216    pkgCacheFile *Cache;
  217    explicit PackageIsUpgradable(pkgCacheFile *Cache) : Cache(Cache) {}
  218    bool operator()(pkgCache::PkgIterator const &Pkg) override
  219    {
  220       assert(Cache != nullptr);
  221       return Pkg->CurrentVer != 0 && (*Cache)[Pkg].Upgradable();
  222    }
  223 };
  224 
  225 struct PackageIsVirtual : public PackageMatcher
  226 {
  227    bool operator()(pkgCache::PkgIterator const &Pkg) override
  228    {
  229       return Pkg->VersionList == 0;
  230    }
  231 };
  232 } // namespace Patterns
  233 } // namespace Internal
  234 } // namespace APT
  235 #endif