1 //===-- CommandObjectRegexCommand.cpp ---------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Interpreter/CommandObjectRegexCommand.h"
11 #include "lldb/Interpreter/CommandInterpreter.h"
12 #include "lldb/Interpreter/CommandReturnObject.h"
15 using namespace lldb_private;
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,
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) {}
27 CommandObjectRegexCommand::~CommandObjectRegexCommand() {}
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 RegularExpression::Match regex_match(m_max_matches);
35 if (pos->regex.Execute(command, ®ex_match)) {
36 std::string new_command(pos->command);
37 std::string match_str;
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();
52 // Interpret the new command and return this as the result!
53 if (m_interpreter.GetExpandRegexAliases())
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
58 return m_interpreter.HandleCommand(
59 new_command.c_str(), eLazyBoolCalculate, result, nullptr, true, true);
62 result.SetStatus(eReturnStatusFailed);
63 if (!GetSyntax().empty())
64 result.AppendError(GetSyntax());
66 result.GetOutputStream() << "Command contents '" << command
67 << "' failed to match any "
68 "regular expression in the '"
69 << m_cmd_name << "' regex ";
73 bool CommandObjectRegexCommand::AddRegexCommand(const char *re_cstr,
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);
82 // The regex didn't compile...
87 int CommandObjectRegexCommand::HandleCompletion(CompletionRequest &request) {
88 if (m_completion_type_mask) {
89 CommandCompletions::InvokeCommonCompletionCallbacks(
90 GetCommandInterpreter(), m_completion_type_mask, request, nullptr);
91 return request.GetNumberOfMatches();
93 request.SetWordComplete(false);