LLDB  mainline
CommandObjectRegexCommand.cpp
Go to the documentation of this file.
1 //===-- CommandObjectRegexCommand.cpp -------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
12 
13 #include "llvm/Support/Errc.h"
14 #include "llvm/Support/Error.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 // CommandObjectRegexCommand constructor
20 CommandObjectRegexCommand::CommandObjectRegexCommand(
21  CommandInterpreter &interpreter, llvm::StringRef name, llvm::StringRef help,
22  llvm::StringRef syntax, uint32_t max_matches, uint32_t completion_type_mask,
23  bool is_removable)
24  : CommandObjectRaw(interpreter, name, help, syntax),
25  m_max_matches(max_matches), m_completion_type_mask(completion_type_mask),
26  m_is_removable(is_removable) {}
27 
28 // Destructor
30 
32  llvm::StringRef input,
33  const llvm::SmallVectorImpl<llvm::StringRef> &replacements) {
34  std::string buffer;
35  llvm::raw_string_ostream output(buffer);
36 
37  llvm::SmallVector<llvm::StringRef, 4> parts;
38  input.split(parts, '%');
39 
40  output << parts[0];
41  for (llvm::StringRef part : drop_begin(parts)) {
42  size_t idx = 0;
43  if (part.consumeInteger(10, idx))
44  output << '%';
45  else if (idx < replacements.size())
46  output << replacements[idx];
47  else
48  return llvm::make_error<llvm::StringError>(
49  llvm::formatv("%{0} is out of range: not enough arguments specified",
50  idx),
51  llvm::errc::invalid_argument);
52  output << part;
53  }
54 
55  return output.str();
56 }
57 
58 bool CommandObjectRegexCommand::DoExecute(llvm::StringRef command,
59  CommandReturnObject &result) {
60  EntryCollection::const_iterator pos, end = m_entries.end();
61  for (pos = m_entries.begin(); pos != end; ++pos) {
62  llvm::SmallVector<llvm::StringRef, 4> matches;
63  if (pos->regex.Execute(command, &matches)) {
64  llvm::Expected<std::string> new_command =
65  SubstituteVariables(pos->command, matches);
66  if (!new_command) {
67  result.SetError(new_command.takeError());
68  return false;
69  }
70 
71  // Interpret the new command and return this as the result!
73  result.GetOutputStream().Printf("%s\n", new_command->c_str());
74  // We don't have to pass an override_context here, as the command that
75  // called us should have set up the context appropriately.
76  return m_interpreter.HandleCommand(new_command->c_str(),
77  eLazyBoolNo, result);
78  }
79  }
81  if (!GetSyntax().empty())
82  result.AppendError(GetSyntax());
83  else
84  result.GetErrorStream() << "Command contents '" << command
85  << "' failed to match any "
86  "regular expression in the '"
87  << m_cmd_name << "' regex ";
88  return false;
89 }
90 
91 bool CommandObjectRegexCommand::AddRegexCommand(llvm::StringRef re_cstr,
92  llvm::StringRef command_cstr) {
93  m_entries.resize(m_entries.size() + 1);
94  // Only add the regular expression if it compiles
95  m_entries.back().regex = RegularExpression(re_cstr);
96  if (m_entries.back().regex.IsValid()) {
97  m_entries.back().command = command_cstr.str();
98  return true;
99  }
100  // The regex didn't compile...
101  m_entries.pop_back();
102  return false;
103 }
104 
108  GetCommandInterpreter(), m_completion_type_mask, request, nullptr);
109  }
110 }
lldb_private::RegularExpression
Definition: RegularExpression.h:18
lldb_private::CommandReturnObject::SetError
void SetError(const Status &error, const char *fallback_error_cstr=nullptr)
Definition: CommandReturnObject.cpp:107
lldb_private::CommandInterpreter::HandleCommand
bool HandleCommand(const char *command_line, LazyBool add_to_history, const ExecutionContext &override_context, CommandReturnObject &result)
lldb_private::CommandObjectRegexCommand::HandleCompletion
void HandleCompletion(CompletionRequest &request) override
This default version handles calling option argument completions and then calls HandleArgumentComplet...
Definition: CommandObjectRegexCommand.cpp:105
CommandReturnObject.h
lldb_private::CommandObjectRegexCommand::m_entries
EntryCollection m_entries
Definition: CommandObjectRegexCommand.h:55
lldb_private::CommandObjectRegexCommand::DoExecute
bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override
Definition: CommandObjectRegexCommand.cpp:58
lldb_private::CommandObjectRegexCommand::m_completion_type_mask
const uint32_t m_completion_type_mask
Definition: CommandObjectRegexCommand.h:54
lldb_private::CommandObject::GetSyntax
virtual llvm::StringRef GetSyntax()
Definition: CommandObject.cpp:59
lldb_private::CommandReturnObject::SetStatus
void SetStatus(lldb::ReturnStatus status)
Definition: CommandReturnObject.cpp:127
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
Definition: CommandCompletions.cpp:51
lldb_private::CommandReturnObject::GetOutputStream
Stream & GetOutputStream()
Definition: CommandReturnObject.h:46
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:214
lldb::eReturnStatusFailed
@ eReturnStatusFailed
Definition: lldb-enumerations.h:265
CommandObjectRegexCommand.h
lldb_private::CommandObject::m_interpreter
CommandInterpreter & m_interpreter
Definition: CommandObject.h:368
lldb_private::CommandReturnObject::GetErrorStream
Stream & GetErrorStream()
Definition: CommandReturnObject.h:56
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::CommandObjectRegexCommand::~CommandObjectRegexCommand
~CommandObjectRegexCommand() override
lldb_private::CommandObjectRaw
Definition: CommandObject.h:406
lldb_private::CommandObjectRegexCommand::SubstituteVariables
static llvm::Expected< std::string > SubstituteVariables(llvm::StringRef input, const llvm::SmallVectorImpl< llvm::StringRef > &replacements)
Substitute variables of the format %\d+ in the input string.
Definition: CommandObjectRegexCommand.cpp:31
lldb_private::CommandReturnObject
Definition: CommandReturnObject.h:26
uint32_t
lldb_private::eLazyBoolNo
@ eLazyBoolNo
Definition: lldb-private-enumerations.h:115
lldb_private::CommandObject::m_cmd_name
std::string m_cmd_name
Definition: CommandObject.h:371
lldb_private::CommandObject::GetCommandInterpreter
CommandInterpreter & GetCommandInterpreter()
Definition: CommandObject.h:122
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::CommandInterpreter::GetExpandRegexAliases
bool GetExpandRegexAliases() const
Definition: CommandInterpreter.cpp:141
CommandInterpreter.h
llvm::SmallVectorImpl
Definition: Disassembler.h:42
lldb_private::CommandReturnObject::AppendError
void void AppendError(llvm::StringRef in_string)
Definition: CommandReturnObject.cpp:100
lldb_private::CommandObjectRegexCommand::AddRegexCommand
bool AddRegexCommand(llvm::StringRef re_cstr, llvm::StringRef command_cstr)
Definition: CommandObjectRegexCommand.cpp:91
lldb
Definition: SBAddress.h:15
lldb_private::CompletionRequest
Definition: CompletionRequest.h:100