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 
10 
13 
14 using namespace lldb;
15 using namespace lldb_private;
16 
17 // CommandObjectRegexCommand constructor
18 CommandObjectRegexCommand::CommandObjectRegexCommand(
19  CommandInterpreter &interpreter, llvm::StringRef name, llvm::StringRef help,
20  llvm::StringRef syntax, uint32_t max_matches, uint32_t completion_type_mask,
21  bool is_removable)
22  : CommandObjectRaw(interpreter, name, help, syntax),
23  m_max_matches(max_matches), m_completion_type_mask(completion_type_mask),
24  m_entries(), m_is_removable(is_removable) {}
25 
26 // Destructor
28 
29 bool CommandObjectRegexCommand::DoExecute(llvm::StringRef command,
30  CommandReturnObject &result) {
31  EntryCollection::const_iterator pos, end = m_entries.end();
32  for (pos = m_entries.begin(); pos != end; ++pos) {
33  llvm::SmallVector<llvm::StringRef, 4> matches;
34  if (pos->regex.Execute(command, &matches)) {
35  std::string new_command(pos->command);
36  char percent_var[8];
37  size_t idx, percent_var_idx;
38  for (uint32_t match_idx = 1; match_idx <= m_max_matches; ++match_idx) {
39  if (match_idx < matches.size()) {
40  const std::string match_str = matches[match_idx].str();
41  const int percent_var_len =
42  ::snprintf(percent_var, sizeof(percent_var), "%%%u", match_idx);
43  for (idx = 0; (percent_var_idx = new_command.find(
44  percent_var, idx)) != std::string::npos;) {
45  new_command.erase(percent_var_idx, percent_var_len);
46  new_command.insert(percent_var_idx, match_str);
47  idx += percent_var_idx + match_str.size();
48  }
49  }
50  }
51  // Interpret the new command and return this as the result!
53  result.GetOutputStream().Printf("%s\n", new_command.c_str());
54  // Pass in true for "no context switching". The command that called us
55  // should have set up the context appropriately, we shouldn't have to
56  // redo that.
58  new_command.c_str(), eLazyBoolCalculate, result, nullptr, true, true);
59  }
60  }
62  if (!GetSyntax().empty())
63  result.AppendError(GetSyntax());
64  else
65  result.GetOutputStream() << "Command contents '" << command
66  << "' failed to match any "
67  "regular expression in the '"
68  << m_cmd_name << "' regex ";
69  return false;
70 }
71 
73  const char *command_cstr) {
74  m_entries.resize(m_entries.size() + 1);
75  // Only add the regular expression if it compiles
76  m_entries.back().regex =
77  RegularExpression(llvm::StringRef::withNullAsEmpty(re_cstr));
78  if (m_entries.back().regex.IsValid()) {
79  m_entries.back().command.assign(command_cstr);
80  return true;
81  }
82  // The regex didn't compile...
83  m_entries.pop_back();
84  return false;
85 }
86 
90  GetCommandInterpreter(), m_completion_type_mask, request, nullptr);
91  }
92 }
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
A class that represents a running process on the host machine.
CommandInterpreter & m_interpreter
bool AddRegexCommand(const char *re_cstr, const char *command_cstr)
"lldb/Utility/ArgCompletionRequest.h"
bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override
void HandleCompletion(CompletionRequest &request) override
This default version handles calling option argument completions and then calls HandleArgumentComplet...
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
CommandInterpreter & GetCommandInterpreter()
void void AppendError(llvm::StringRef in_string)
Definition: SBAddress.h:15
virtual llvm::StringRef GetSyntax()
bool HandleCommand(const char *command_line, LazyBool add_to_history, CommandReturnObject &result, ExecutionContext *override_context=nullptr, bool repeat_on_empty_command=true, bool no_context_switching=false)
void SetStatus(lldb::ReturnStatus status)