From e93dbf2800bca2a6b8319826b7ad2243b4067faa Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Thu, 12 Mar 2020 21:02:40 +0100 Subject: apt-helper: Add analyze-pattern helper The analyze-pattern helper parses a pattern and then renders the parsed pattern, allowing you to analyze how the parser interpreted the string. This can be useful to analyse (yes, analyse-pattern also works) why a pattern is different from aptitude or why it does not work as expected. It can also be used to check if apt has pattern support, although that will miss out on the version shipped in eoan, but who really cares about that longer term anyway? --- cmdline/apt-helper.cc | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'cmdline') diff --git a/cmdline/apt-helper.cc b/cmdline/apt-helper.cc index 97a4342f5..3d6a692e0 100644 --- a/cmdline/apt-helper.cc +++ b/cmdline/apt-helper.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -257,6 +258,35 @@ static bool DropPrivsAndRun(CommandLine &CmdL) /*{{{*/ return ExecWait(pid, CmdL.FileList[1]); } /*}}}*/ +static bool AnalyzePattern(CommandLine &CmdL) /*{{{*/ +{ + if (CmdL.FileSize() != 2) + return _error->Error("Expect one argument, a pattern"); + + try + { + auto top = APT::Internal::PatternTreeParser(CmdL.FileList[1]).parseTop(); + top->render(std::cout) << "\n"; + } + catch (APT::Internal::PatternTreeParser::Error &e) + { + std::stringstream ss; + ss << "input:" << e.location.start << "-" << e.location.end << ": error: " << e.message << "\n"; + ss << CmdL.FileList[1] << "\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 false; + } + + return true; +} + /*}}}*/ static bool ShowHelp(CommandLine &) /*{{{*/ { std::cout << @@ -278,6 +308,8 @@ static std::vector GetCommands() /*{{{*/ {"auto-detect-proxy", &DoAutoDetectProxy, _("detect proxy using apt.conf")}, {"wait-online", &DoWaitOnline, _("wait for system to be online")}, {"drop-privs", &DropPrivsAndRun, _("drop privileges before running given command")}, + {"analyze-pattern", &AnalyzePattern, _("analyse a pattern")}, + {"analyse-pattern", &AnalyzePattern, nullptr}, {nullptr, nullptr, nullptr}}; } /*}}}*/ -- cgit v1.2.3