xref: /src/contrib/llvm-project/lldb/source/Commands/CommandObjectRegexCommand.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1cfca06d7SDimitry Andric //===-- CommandObjectRegexCommand.cpp -------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9b60736ecSDimitry Andric #include "CommandObjectRegexCommand.h"
10f034231aSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
11f034231aSEd Maste #include "lldb/Interpreter/CommandReturnObject.h"
12f034231aSEd Maste 
13145449b1SDimitry Andric #include "llvm/Support/Errc.h"
14145449b1SDimitry Andric #include "llvm/Support/Error.h"
15145449b1SDimitry Andric 
16f034231aSEd Maste using namespace lldb;
17f034231aSEd Maste using namespace lldb_private;
18f034231aSEd Maste 
19f034231aSEd Maste // CommandObjectRegexCommand constructor
CommandObjectRegexCommand(CommandInterpreter & interpreter,llvm::StringRef name,llvm::StringRef help,llvm::StringRef syntax,uint32_t completion_type_mask,bool is_removable)2014f1b3e8SDimitry Andric CommandObjectRegexCommand::CommandObjectRegexCommand(
2114f1b3e8SDimitry Andric     CommandInterpreter &interpreter, llvm::StringRef name, llvm::StringRef help,
227fa27ce4SDimitry Andric     llvm::StringRef syntax, uint32_t completion_type_mask, bool is_removable)
2314f1b3e8SDimitry Andric     : CommandObjectRaw(interpreter, name, help, syntax),
247fa27ce4SDimitry Andric       m_completion_type_mask(completion_type_mask),
256f8fc217SDimitry Andric       m_is_removable(is_removable) {}
26f034231aSEd Maste 
27f034231aSEd Maste // Destructor
28344a3780SDimitry Andric CommandObjectRegexCommand::~CommandObjectRegexCommand() = default;
29f034231aSEd Maste 
SubstituteVariables(llvm::StringRef input,const llvm::SmallVectorImpl<llvm::StringRef> & replacements)30145449b1SDimitry Andric llvm::Expected<std::string> CommandObjectRegexCommand::SubstituteVariables(
31145449b1SDimitry Andric     llvm::StringRef input,
32145449b1SDimitry Andric     const llvm::SmallVectorImpl<llvm::StringRef> &replacements) {
33145449b1SDimitry Andric   std::string buffer;
34145449b1SDimitry Andric   llvm::raw_string_ostream output(buffer);
35145449b1SDimitry Andric 
36145449b1SDimitry Andric   llvm::SmallVector<llvm::StringRef, 4> parts;
37145449b1SDimitry Andric   input.split(parts, '%');
38145449b1SDimitry Andric 
39145449b1SDimitry Andric   output << parts[0];
40145449b1SDimitry Andric   for (llvm::StringRef part : drop_begin(parts)) {
41145449b1SDimitry Andric     size_t idx = 0;
42145449b1SDimitry Andric     if (part.consumeInteger(10, idx))
43145449b1SDimitry Andric       output << '%';
44145449b1SDimitry Andric     else if (idx < replacements.size())
45145449b1SDimitry Andric       output << replacements[idx];
46145449b1SDimitry Andric     else
47145449b1SDimitry Andric       return llvm::make_error<llvm::StringError>(
48145449b1SDimitry Andric           llvm::formatv("%{0} is out of range: not enough arguments specified",
49145449b1SDimitry Andric                         idx),
50145449b1SDimitry Andric           llvm::errc::invalid_argument);
51145449b1SDimitry Andric     output << part;
52145449b1SDimitry Andric   }
53145449b1SDimitry Andric 
54145449b1SDimitry Andric   return output.str();
55145449b1SDimitry Andric }
56145449b1SDimitry Andric 
DoExecute(llvm::StringRef command,CommandReturnObject & result)57b1c73532SDimitry Andric void CommandObjectRegexCommand::DoExecute(llvm::StringRef command,
5814f1b3e8SDimitry Andric                                           CommandReturnObject &result) {
59f034231aSEd Maste   EntryCollection::const_iterator pos, end = m_entries.end();
6014f1b3e8SDimitry Andric   for (pos = m_entries.begin(); pos != end; ++pos) {
61ead24645SDimitry Andric     llvm::SmallVector<llvm::StringRef, 4> matches;
62ead24645SDimitry Andric     if (pos->regex.Execute(command, &matches)) {
63145449b1SDimitry Andric       llvm::Expected<std::string> new_command =
64145449b1SDimitry Andric           SubstituteVariables(pos->command, matches);
65145449b1SDimitry Andric       if (!new_command) {
66145449b1SDimitry Andric         result.SetError(new_command.takeError());
67b1c73532SDimitry Andric         return;
68f034231aSEd Maste       }
69145449b1SDimitry Andric 
70f034231aSEd Maste       // Interpret the new command and return this as the result!
71f034231aSEd Maste       if (m_interpreter.GetExpandRegexAliases())
72145449b1SDimitry Andric         result.GetOutputStream().Printf("%s\n", new_command->c_str());
73145449b1SDimitry Andric       // We don't have to pass an override_context here, as the command that
74145449b1SDimitry Andric       // called us should have set up the context appropriately.
757fa27ce4SDimitry Andric       bool force_repeat_command = true;
76b1c73532SDimitry Andric       m_interpreter.HandleCommand(new_command->c_str(), eLazyBoolNo, result,
77b1c73532SDimitry Andric                                   force_repeat_command);
78b1c73532SDimitry Andric       return;
79f034231aSEd Maste     }
80f034231aSEd Maste   }
81f034231aSEd Maste   result.SetStatus(eReturnStatusFailed);
8214f1b3e8SDimitry Andric   if (!GetSyntax().empty())
83f034231aSEd Maste     result.AppendError(GetSyntax());
84f034231aSEd Maste   else
85145449b1SDimitry Andric     result.GetErrorStream() << "Command contents '" << command
86f73363f1SDimitry Andric                             << "' failed to match any "
87f73363f1SDimitry Andric                                "regular expression in the '"
88f73363f1SDimitry Andric                             << m_cmd_name << "' regex ";
89f034231aSEd Maste }
90f034231aSEd Maste 
AddRegexCommand(llvm::StringRef re_cstr,llvm::StringRef command_cstr)91b60736ecSDimitry Andric bool CommandObjectRegexCommand::AddRegexCommand(llvm::StringRef re_cstr,
92b60736ecSDimitry Andric                                                 llvm::StringRef command_cstr) {
93f034231aSEd Maste   m_entries.resize(m_entries.size() + 1);
94f034231aSEd Maste   // Only add the regular expression if it compiles
95b60736ecSDimitry Andric   m_entries.back().regex = RegularExpression(re_cstr);
96ead24645SDimitry Andric   if (m_entries.back().regex.IsValid()) {
97b60736ecSDimitry Andric     m_entries.back().command = command_cstr.str();
98f034231aSEd Maste     return true;
99f034231aSEd Maste   }
100f034231aSEd Maste   // The regex didn't compile...
101f034231aSEd Maste   m_entries.pop_back();
102f034231aSEd Maste   return false;
103f034231aSEd Maste }
104f034231aSEd Maste 
HandleCompletion(CompletionRequest & request)105ead24645SDimitry Andric void CommandObjectRegexCommand::HandleCompletion(CompletionRequest &request) {
10614f1b3e8SDimitry Andric   if (m_completion_type_mask) {
1077fa27ce4SDimitry Andric     lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks(
108f73363f1SDimitry Andric         GetCommandInterpreter(), m_completion_type_mask, request, nullptr);
109f034231aSEd Maste   }
110f034231aSEd Maste }
111