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 using namespace lldb;
14 using namespace lldb_private;
15 
16 // CommandObjectRegexCommand constructor
17 CommandObjectRegexCommand::CommandObjectRegexCommand(
18  CommandInterpreter &interpreter, llvm::StringRef name, llvm::StringRef help,
19  llvm::StringRef syntax, uint32_t max_matches, uint32_t completion_type_mask,
20  bool is_removable)
21  : CommandObjectRaw(interpreter, name, help, syntax),
22  m_max_matches(max_matches), m_completion_type_mask(completion_type_mask),
23  m_entries(), m_is_removable(is_removable) {}
24 
25 // Destructor
27 
28 bool CommandObjectRegexCommand::DoExecute(llvm::StringRef command,
29  CommandReturnObject &result) {
30  EntryCollection::const_iterator pos, end = m_entries.end();
31  for (pos = m_entries.begin(); pos != end; ++pos) {
32  llvm::SmallVector<llvm::StringRef, 4> matches;
33  if (pos->regex.Execute(command, &matches)) {
34  std::string new_command(pos->command);
35  char percent_var[8];
36  size_t idx, percent_var_idx;
37  for (uint32_t match_idx = 1; match_idx <= m_max_matches; ++match_idx) {
38  if (match_idx < matches.size()) {
39  const std::string match_str = matches[match_idx].str();
40  const int percent_var_len =
41  ::snprintf(percent_var, sizeof(percent_var), "%%%u", match_idx);
42  for (idx = 0; (percent_var_idx = new_command.find(
43  percent_var, idx)) != std::string::npos;) {
44  new_command.erase(percent_var_idx, percent_var_len);
45  new_command.insert(percent_var_idx, match_str);
46  idx += percent_var_idx + match_str.size();
47  }
48  }
49  }
50  // Interpret the new command and return this as the result!
52  result.GetOutputStream().Printf("%s\n", new_command.c_str());
53  // Pass in true for "no context switching". The command that called us
54  // should have set up the context appropriately, we shouldn't have to
55  // redo that.
56  return m_interpreter.HandleCommand(new_command.c_str(),
57  eLazyBoolCalculate, result);
58  }
59  }
61  if (!GetSyntax().empty())
62  result.AppendError(GetSyntax());
63  else
64  result.GetOutputStream() << "Command contents '" << command
65  << "' failed to match any "
66  "regular expression in the '"
67  << m_cmd_name << "' regex ";
68  return false;
69 }
70 
71 bool CommandObjectRegexCommand::AddRegexCommand(llvm::StringRef re_cstr,
72  llvm::StringRef command_cstr) {
73  m_entries.resize(m_entries.size() + 1);
74  // Only add the regular expression if it compiles
75  m_entries.back().regex = RegularExpression(re_cstr);
76  if (m_entries.back().regex.IsValid()) {
77  m_entries.back().command = command_cstr.str();
78  return true;
79  }
80  // The regex didn't compile...
81  m_entries.pop_back();
82  return false;
83 }
84 
88  GetCommandInterpreter(), m_completion_type_mask, request, nullptr);
89  }
90 }
lldb_private::RegularExpression
Definition: RegularExpression.h:18
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:85
CommandReturnObject.h
lldb_private::CommandObjectRegexCommand::m_entries
EntryCollection m_entries
Definition: CommandObjectRegexCommand.h:50
lldb_private::CommandObjectRegexCommand::m_max_matches
const uint32_t m_max_matches
Definition: CommandObjectRegexCommand.h:48
lldb_private::CommandObjectRegexCommand::DoExecute
bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override
Definition: CommandObjectRegexCommand.cpp:28
lldb_private::CommandObjectRegexCommand::m_completion_type_mask
const uint32_t m_completion_type_mask
Definition: CommandObjectRegexCommand.h:49
lldb_private::CommandObject::GetSyntax
virtual llvm::StringRef GetSyntax()
Definition: CommandObject.cpp:57
lldb_private::CommandReturnObject::SetStatus
void SetStatus(lldb::ReturnStatus status)
Definition: CommandReturnObject.cpp:131
lldb_private::CommandCompletions::InvokeCommonCompletionCallbacks
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
Definition: CommandCompletions.cpp:49
lldb_private::CommandReturnObject::GetOutputStream
Stream & GetOutputStream()
Definition: CommandReturnObject.h:45
lldb_private::CommandInterpreter
Definition: CommandInterpreter.h:221
lldb::eReturnStatusFailed
@ eReturnStatusFailed
Definition: lldb-enumerations.h:265
CommandObjectRegexCommand.h
lldb_private::CommandObject::m_interpreter
CommandInterpreter & m_interpreter
Definition: CommandObject.h:358
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
lldb_private::CommandObjectRegexCommand::~CommandObjectRegexCommand
~CommandObjectRegexCommand() override
Definition: CommandObjectRegexCommand.cpp:26
lldb_private::CommandObjectRaw
Definition: CommandObject.h:395
lldb_private::CommandReturnObject
Definition: CommandReturnObject.h:25
uint32_t
lldb_private::CommandObject::m_cmd_name
std::string m_cmd_name
Definition: CommandObject.h:361
lldb_private::CommandObject::GetCommandInterpreter
CommandInterpreter & GetCommandInterpreter()
Definition: CommandObject.h:124
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::eLazyBoolCalculate
@ eLazyBoolCalculate
Definition: lldb-private-enumerations.h:115
lldb_private::CommandInterpreter::GetExpandRegexAliases
bool GetExpandRegexAliases() const
Definition: CommandInterpreter.cpp:135
CommandInterpreter.h
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:71
lldb
Definition: SBAddress.h:15
lldb_private::CompletionRequest
Definition: CompletionRequest.h:100