summaryrefslogtreecommitdiff
path: root/apt-pkg/cachefilter-patterns.cc
blob: 9fab0281d7abd4c90ff34534106be07cc927bd9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
 * cachefilter-patterns.cc - Parser for aptitude-style patterns
 *
 * Copyright (c) 2019 Canonical Ltd
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#include <config.h>

#include <apt-pkg/cachefilter-patterns.h>

#include <apti18n.h>

namespace APT
{
namespace Internal
{

static const constexpr struct
{
   APT::StringView shortName;
   APT::StringView longName;
   bool takesArgument;
} shortPatterns[] = {
   {"r"_sv, "?architecture"_sv, true},
   {"A"_sv, "?archive"_sv, true},
   {"M"_sv, "?automatic"_sv, false},
   {"b"_sv, "?broken"_sv, false},
   {"c"_sv, "?config-files"_sv, false},
   {"E"_sv, "?essential"_sv, false},
   {"F"_sv, "?false"_sv, false},
   {"g"_sv, "?garbage"_sv, false},
   {"i"_sv, "?installed"_sv, false},
   {"n"_sv, "?name"_sv, true},
   {"o"_sv, "?obsolete"_sv, false},
   {"O"_sv, "?origin"_sv, true},
   {"s"_sv, "?section"_sv, true},
   {"e"_sv, "?source-package"_sv, true},
   {"T"_sv, "?true"_sv, false},
   {"U"_sv, "?upgradable"_sv, false},
   {"V"_sv, "?version"_sv, true},
   {"v"_sv, "?virtual"_sv, false},
};

template <class... Args>
std::string rstrprintf(Args... args)
{
   std::string str;
   strprintf(str, std::forward<Args>(args)...);
   return str;
}

// Parse a complete pattern, make sure it's the entire input
std::unique_ptr<PatternTreeParser::Node> PatternTreeParser::parseTop()
{
   skipSpace();
   auto node = parse();
   skipSpace();

   if (node->end != sentence.size())
      throw Error{Node{node->end, sentence.size()}, "Expected end of file"};

   return node;
}

// Parse any pattern
std::unique_ptr<PatternTreeParser::Node> PatternTreeParser::parse()
{
   return parseUnary();
}

std::unique_ptr<PatternTreeParser::Node> PatternTreeParser::parseUnary()
{

   if (sentence[state.offset] != '!')
      return parsePrimary();

   auto start = ++state.offset;
   auto primary = parsePrimary();

   if (primary == nullptr)
      throw Error{Node{start, sentence.size()}, "Expected pattern"};

   auto node = std::make_unique<PatternNode>();
   node->start = start;
   node->end = primary->end;
   node->term = "?not";
   node->arguments.push_back(std::move(primary));
   node->haveArgumentList = true;
   return node;
}

std::unique_ptr<PatternTreeParser::Node> PatternTreeParser::parsePrimary()
{
   std::unique_ptr<Node> node;
   if ((node = parseShortPattern()) != nullptr)
      return node;
   if ((node = parsePattern()) != nullptr)
      return node;
   if ((node = parseQuotedWord()) != nullptr)
      return node;
   if ((node = parseWord()) != nullptr)
      return node;

   throw Error{Node{state.offset, sentence.size()},
	       "Expected pattern, quoted word, or word"};
}

// Parse a short pattern
std::unique_ptr<PatternTreeParser::Node> PatternTreeParser::parseShortPattern()
{
   if (sentence[state.offset] != '~')
      return nullptr;

   for (auto &sp : shortPatterns)
   {
      if (sentence.substr(state.offset + 1, sp.shortName.size()) != sp.shortName)
	 continue;

      auto node = std::make_unique<PatternNode>();
      node->end = node->start = state.offset;
      node->term = sp.longName;

      state.offset += sp.shortName.size() + 1;
      if (sp.takesArgument)
      {
	 node->arguments.push_back(parse());
	 node->haveArgumentList = true;
      }
      node->end = state.offset;

      return node;
   }

   throw Error{Node{state.offset, sentence.size()}, "Unknown short pattern"};
}

// Parse a list pattern (or function call pattern)
std::unique_ptr<PatternTreeParser::Node> PatternTreeParser::parsePattern()
{
   static constexpr auto CHARS = ("0123456789"
				  "abcdefghijklmnopqrstuvwxyz"
				  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
				  "-"_sv);
   if (sentence[state.offset] != '?')
      return nullptr;

   auto node = std::make_unique<PatternNode>();
   node->end = node->start = state.offset;
   state.offset++;

   while (CHARS.find(sentence[state.offset]) != APT::StringView::npos)
   {
      ++state.offset;
   }

   node->term = sentence.substr(node->start, state.offset - node->start);

   node->end = skipSpace();
   // We don't have any arguments, return node;
   if (sentence[state.offset] != '(')
      return node;
   node->end = ++state.offset;
   skipSpace();

   node->haveArgumentList = true;

   // Empty argument list, return
   if (sentence[state.offset] == ')')
   {
      node->end = ++state.offset;
      return node;
   }

   node->arguments.push_back(parse());
   skipSpace();
   while (sentence[state.offset] == ',')
   {
      ++state.offset;
      skipSpace();
      // This was a trailing comma - allow it and break the loop
      if (sentence[state.offset] == ')')
	 break;
      node->arguments.push_back(parse());
      skipSpace();
   }

   node->end = state.offset;
   if (sentence[state.offset] != ')')
      throw Error{node->arguments.empty() ? *node : *node->arguments[node->arguments.size() - 1],
		  rstrprintf("Expected closing parenthesis or comma after last argument, received %c", sentence[state.offset])};

   node->end = ++state.offset;
   return node;
}

// Parse a quoted word atom
std::unique_ptr<PatternTreeParser::Node> PatternTreeParser::parseQuotedWord()
{
   if (sentence[state.offset] != '"')
      return nullptr;

   auto node = std::make_unique<WordNode>();
   node->start = state.offset;

   // Eat beginning of string
   state.offset++;

   while (sentence[state.offset] != '"' && sentence[state.offset] != '\0')
      state.offset++;

   // End of string
   if (sentence[state.offset] != '"')
      throw Error{*node, "Could not find end of string"};
   state.offset++;

   node->end = state.offset;
   node->word = sentence.substr(node->start + 1, node->end - node->start - 2);

   return node;
}

// Parse a bare word atom
std::unique_ptr<PatternTreeParser::Node> PatternTreeParser::parseWord()
{
   static const constexpr auto DISALLOWED_START = "!?~,()\0"_sv;
   static const constexpr auto DISALLOWED = ",()\0"_sv;
   if (DISALLOWED_START.find(sentence[state.offset]) != APT::StringView::npos)
      return nullptr;

   auto node = std::make_unique<WordNode>();
   node->start = state.offset;

   while (DISALLOWED.find(sentence[state.offset]) == APT::StringView::npos)
      state.offset++;

   node->end = state.offset;
   node->word = sentence.substr(node->start, node->end - node->start);
   return node;
}

// Rendering of the tree in JSON for debugging
std::ostream &PatternTreeParser::PatternNode::render(std::ostream &os)
{
   os << "{"
      << "\"term\": \"" << term.to_string() << "\",\n"
      << "\"arguments\": [\n";
   for (auto &node : arguments)
      node->render(os) << "," << std::endl;
   os << "null]\n";
   os << "}\n";
   return os;
}

std::ostream &PatternTreeParser::WordNode::render(std::ostream &os)
{
   os << '"' << word.to_string() << '"';
   return os;
}

std::nullptr_t PatternTreeParser::Node::error(std::string message)
{
   throw Error{*this, message};
}

bool PatternTreeParser::PatternNode::matches(APT::StringView name, int min, int max)
{
   if (name != term)
      return false;
   if (max != 0 && !haveArgumentList)
      error(rstrprintf("%s expects an argument list", term.to_string().c_str()));
   if (max == 0 && haveArgumentList)
      error(rstrprintf("%s does not expect an argument list", term.to_string().c_str()));
   if (min >= 0 && min == max && (arguments.size() != size_t(min)))
      error(rstrprintf("%s expects %d arguments, but received %d arguments", term.to_string().c_str(), min, arguments.size()));
   if (min >= 0 && arguments.size() < size_t(min))
      error(rstrprintf("%s expects at least %d arguments, but received %d arguments", term.to_string().c_str(), min, arguments.size()));
   if (max >= 0 && arguments.size() > size_t(max))
      error(rstrprintf("%s expects at most %d arguments, but received %d arguments", term.to_string().c_str(), max, arguments.size()));
   return true;
}

std::unique_ptr<APT::CacheFilter::Matcher> PatternParser::aPattern(std::unique_ptr<PatternTreeParser::Node> &nodeP)
{
   assert(nodeP != nullptr);
   auto node = dynamic_cast<PatternTreeParser::PatternNode *>(nodeP.get());
   if (node == nullptr)
      nodeP->error("Expected a pattern");

   if (node->matches("?architecture", 1, 1))
      return std::make_unique<APT::CacheFilter::PackageArchitectureMatchesSpecification>(aWord(node->arguments[0]));
   if (node->matches("?archive", 1, 1))
      return std::make_unique<Patterns::VersionIsArchive>(aWord(node->arguments[0]));
   if (node->matches("?all-versions", 1, 1))
      return std::make_unique<Patterns::VersionIsAllVersions>(aPattern(node->arguments[0]));
   if (node->matches("?any-version", 1, 1))
      return std::make_unique<Patterns::VersionIsAnyVersion>(aPattern(node->arguments[0]));
   if (node->matches("?automatic", 0, 0))
      return std::make_unique<Patterns::PackageIsAutomatic>(file);
   if (node->matches("?broken", 0, 0))
      return std::make_unique<Patterns::PackageIsBroken>(file);
   if (node->matches("?config-files", 0, 0))
      return std::make_unique<Patterns::PackageIsConfigFiles>();
   if (node->matches("?essential", 0, 0))
      return std::make_unique<Patterns::PackageIsEssential>();
   if (node->matches("?exact-name", 1, 1))
      return std::make_unique<Patterns::PackageHasExactName>(aWord(node->arguments[0]));
   if (node->matches("?false", 0, 0))
      return std::make_unique<APT::CacheFilter::FalseMatcher>();
   if (node->matches("?garbage", 0, 0))
      return std::make_unique<Patterns::PackageIsGarbage>(file);
   if (node->matches("?installed", 0, 0))
      return std::make_unique<Patterns::PackageIsInstalled>(file);
   if (node->matches("?name", 1, 1))
      return std::make_unique<APT::CacheFilter::PackageNameMatchesRegEx>(aWord(node->arguments[0]));
   if (node->matches("?not", 1, 1))
      return std::make_unique<APT::CacheFilter::NOTMatcher>(aPattern(node->arguments[0]).release());
   if (node->matches("?obsolete", 0, 0))
      return std::make_unique<Patterns::PackageIsObsolete>();
   if (node->matches("?origin", 1, 1))
      return std::make_unique<Patterns::VersionIsOrigin>(aWord(node->arguments[0]));
   if (node->matches("?section", 1, 1))
      return std::make_unique<Patterns::VersionIsSection>(aWord(node->arguments[0]));
   if (node->matches("?source-package", 1, 1))
      return std::make_unique<Patterns::VersionIsSourcePackage>(aWord(node->arguments[0]));
   if (node->matches("?source-version", 1, 1))
      return std::make_unique<Patterns::VersionIsSourceVersion>(aWord(node->arguments[0]));
   if (node->matches("?true", 0, 0))
      return std::make_unique<APT::CacheFilter::TrueMatcher>();
   if (node->matches("?upgradable", 0, 0))
      return std::make_unique<Patterns::PackageIsUpgradable>(file);
   if (node->matches("?version", 1, 1))
      return std::make_unique<Patterns::VersionIsVersion>(aWord(node->arguments[0]));
   if (node->matches("?virtual", 0, 0))
      return std::make_unique<Patterns::PackageIsVirtual>();
   if (node->matches("?x-name-fnmatch", 1, 1))
      return std::make_unique<APT::CacheFilter::PackageNameMatchesFnmatch>(aWord(node->arguments[0]));

   // Variable argument patterns
   if (node->matches("?and", 0, -1) || node->matches("?narrow", 0, -1))
   {
      auto pattern = std::make_unique<APT::CacheFilter::ANDMatcher>();
      for (auto &arg : node->arguments)
	 pattern->AND(aPattern(arg).release());
      if (node->term == "?narrow")
	 return std::make_unique<Patterns::VersionIsAnyVersion>(std::move(pattern));
      return pattern;
   }
   if (node->matches("?or", 0, -1))
   {
      auto pattern = std::make_unique<APT::CacheFilter::ORMatcher>();

      for (auto &arg : node->arguments)
	 pattern->OR(aPattern(arg).release());
      return pattern;
   }

   node->error(rstrprintf("Unrecognized pattern '%s'", node->term.to_string().c_str()));

   return nullptr;
}

std::string PatternParser::aWord(std::unique_ptr<PatternTreeParser::Node> &nodeP)
{
   assert(nodeP != nullptr);
   auto node = dynamic_cast<PatternTreeParser::WordNode *>(nodeP.get());
   if (node == nullptr)
      nodeP->error("Expected a word");
   return node->word.to_string();
}

namespace Patterns
{

BaseRegexMatcher::BaseRegexMatcher(std::string const &Pattern)
{
   pattern = new regex_t;
   int const Res = regcomp(pattern, Pattern.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
   if (Res == 0)
      return;

   delete pattern;
   pattern = NULL;
   char Error[300];
   regerror(Res, pattern, Error, sizeof(Error));
   _error->Error(_("Regex compilation error - %s"), Error);
}
bool BaseRegexMatcher::operator()(const char *string)
{
   if (unlikely(pattern == NULL))
      return false;
   else
      return regexec(pattern, string, 0, 0, 0) == 0;
}
BaseRegexMatcher::~BaseRegexMatcher()
{
   if (pattern == NULL)
      return;
   regfree(pattern);
   delete pattern;
}
} // namespace Patterns

} // namespace Internal

// The bridge into the public world
std::unique_ptr<APT::CacheFilter::Matcher> APT::CacheFilter::ParsePattern(APT::StringView pattern, pkgCacheFile *file)
{
   if (file != nullptr && !file->BuildDepCache())
      return nullptr;

   try
   {
      auto top = APT::Internal::PatternTreeParser(pattern).parseTop();
      APT::Internal::PatternParser parser{file};
      return parser.aPattern(top);
   }
   catch (APT::Internal::PatternTreeParser::Error &e)
   {
      std::stringstream ss;
      ss << "input:" << e.location.start << "-" << e.location.end << ": error: " << e.message << "\n";
      ss << pattern.to_string() << "\n";
      for (size_t i = 0; i < e.location.start; i++)
	 ss << " ";
      for (size_t i = e.location.start; i < e.location.end; i++)
	 ss << "^";

      ss << "\n";

      _error->Error("%s", ss.str().c_str());
      return nullptr;
   }
}

} // namespace APT