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
9 #include "lldb/Interpreter/CommandObjectRegexCommand.h"
10
11 #include "lldb/Interpreter/CommandInterpreter.h"
12 #include "lldb/Interpreter/CommandReturnObject.h"
13
14 using namespace lldb;
15 using namespace lldb_private;
16
17 // CommandObjectRegexCommand constructor
CommandObjectRegexCommand(CommandInterpreter & interpreter,llvm::StringRef name,llvm::StringRef help,llvm::StringRef syntax,uint32_t max_matches,uint32_t completion_type_mask,bool is_removable)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
~CommandObjectRegexCommand()27 CommandObjectRegexCommand::~CommandObjectRegexCommand() {}
28
DoExecute(llvm::StringRef command,CommandReturnObject & result)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!
52 if (m_interpreter.GetExpandRegexAliases())
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.
57 return m_interpreter.HandleCommand(
58 new_command.c_str(), eLazyBoolCalculate, result, nullptr, true, true);
59 }
60 }
61 result.SetStatus(eReturnStatusFailed);
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
AddRegexCommand(const char * re_cstr,const char * command_cstr)72 bool CommandObjectRegexCommand::AddRegexCommand(const char *re_cstr,
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
HandleCompletion(CompletionRequest & request)87 void CommandObjectRegexCommand::HandleCompletion(CompletionRequest &request) {
88 if (m_completion_type_mask) {
89 CommandCompletions::InvokeCommonCompletionCallbacks(
90 GetCommandInterpreter(), m_completion_type_mask, request, nullptr);
91 }
92 }
93