LLDB  mainline
CommandObjectRegexCommand.cpp
Go to the documentation of this file.
1 //===-- CommandObjectRegexCommand.cpp ---------------------------*- C++ -*-===//
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) {
34 
35  if (pos->regex.Execute(command, &regex_match)) {
36  std::string new_command(pos->command);
37  std::string match_str;
38  char percent_var[8];
39  size_t idx, percent_var_idx;
40  for (uint32_t match_idx = 1; match_idx <= m_max_matches; ++match_idx) {
41  if (regex_match.GetMatchAtIndex(command, match_idx, match_str)) {
42  const int percent_var_len =
43  ::snprintf(percent_var, sizeof(percent_var), "%%%u", match_idx);
44  for (idx = 0; (percent_var_idx = new_command.find(
45  percent_var, idx)) != std::string::npos;) {
46  new_command.erase(percent_var_idx, percent_var_len);
47  new_command.insert(percent_var_idx, match_str);
48  idx += percent_var_idx + match_str.size();
49  }
50  }
51  }
52  // Interpret the new command and return this as the result!
54  result.GetOutputStream().Printf("%s\n", new_command.c_str());
55  // Pass in true for "no context switching". The command that called us
56  // should have set up the context appropriately, we shouldn't have to
57  // redo that.
59  new_command.c_str(), eLazyBoolCalculate, result, nullptr, true, true);
60  }
61  }
63  if (!GetSyntax().empty())
64  result.AppendError(GetSyntax());
65  else
66  result.GetOutputStream() << "Command contents '" << command
67  << "' failed to match any "
68  "regular expression in the '"
69  << m_cmd_name << "' regex ";
70  return false;
71 }
72 
74  const char *command_cstr) {
75  m_entries.resize(m_entries.size() + 1);
76  // Only add the regular expression if it compiles
77  if (m_entries.back().regex.Compile(
78  llvm::StringRef::withNullAsEmpty(re_cstr))) {
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  return request.GetNumberOfMatches();
92  } else {
93  request.SetWordComplete(false);
94  }
95  return 0;
96 }
static bool InvokeCommonCompletionCallbacks(CommandInterpreter &interpreter, uint32_t completion_mask, lldb_private::CompletionRequest &request, SearchFilter *searcher)
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
CommandInterpreter & m_interpreter
int HandleCompletion(CompletionRequest &request) override
This default version handles calling option argument completions and then calls HandleArgumentComplet...
bool AddRegexCommand(const char *re_cstr, const char *command_cstr)
std::size_t GetNumberOfMatches() const
"lldb/Utility/ArgCompletionRequest.h"
bool GetMatchAtIndex(llvm::StringRef s, uint32_t idx, std::string &match_str) const
bool DoExecute(llvm::StringRef command, CommandReturnObject &result) override
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
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)