1*e8d8bef9SDimitry Andric //===-- CommandObjectRegexCommand.cpp -------------------------------------===// 2*e8d8bef9SDimitry Andric // 3*e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*e8d8bef9SDimitry Andric // 7*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8*e8d8bef9SDimitry Andric 9*e8d8bef9SDimitry Andric #include "CommandObjectRegexCommand.h" 10*e8d8bef9SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h" 11*e8d8bef9SDimitry Andric #include "lldb/Interpreter/CommandReturnObject.h" 12*e8d8bef9SDimitry Andric 13*e8d8bef9SDimitry Andric using namespace lldb; 14*e8d8bef9SDimitry Andric using namespace lldb_private; 15*e8d8bef9SDimitry Andric 16*e8d8bef9SDimitry Andric // CommandObjectRegexCommand constructor 17*e8d8bef9SDimitry Andric CommandObjectRegexCommand::CommandObjectRegexCommand( 18*e8d8bef9SDimitry Andric CommandInterpreter &interpreter, llvm::StringRef name, llvm::StringRef help, 19*e8d8bef9SDimitry Andric llvm::StringRef syntax, uint32_t max_matches, uint32_t completion_type_mask, 20*e8d8bef9SDimitry Andric bool is_removable) 21*e8d8bef9SDimitry Andric : CommandObjectRaw(interpreter, name, help, syntax), 22*e8d8bef9SDimitry Andric m_max_matches(max_matches), m_completion_type_mask(completion_type_mask), 23*e8d8bef9SDimitry Andric m_entries(), m_is_removable(is_removable) {} 24*e8d8bef9SDimitry Andric 25*e8d8bef9SDimitry Andric // Destructor 26*e8d8bef9SDimitry Andric CommandObjectRegexCommand::~CommandObjectRegexCommand() {} 27*e8d8bef9SDimitry Andric 28*e8d8bef9SDimitry Andric bool CommandObjectRegexCommand::DoExecute(llvm::StringRef command, 29*e8d8bef9SDimitry Andric CommandReturnObject &result) { 30*e8d8bef9SDimitry Andric EntryCollection::const_iterator pos, end = m_entries.end(); 31*e8d8bef9SDimitry Andric for (pos = m_entries.begin(); pos != end; ++pos) { 32*e8d8bef9SDimitry Andric llvm::SmallVector<llvm::StringRef, 4> matches; 33*e8d8bef9SDimitry Andric if (pos->regex.Execute(command, &matches)) { 34*e8d8bef9SDimitry Andric std::string new_command(pos->command); 35*e8d8bef9SDimitry Andric char percent_var[8]; 36*e8d8bef9SDimitry Andric size_t idx, percent_var_idx; 37*e8d8bef9SDimitry Andric for (uint32_t match_idx = 1; match_idx <= m_max_matches; ++match_idx) { 38*e8d8bef9SDimitry Andric if (match_idx < matches.size()) { 39*e8d8bef9SDimitry Andric const std::string match_str = matches[match_idx].str(); 40*e8d8bef9SDimitry Andric const int percent_var_len = 41*e8d8bef9SDimitry Andric ::snprintf(percent_var, sizeof(percent_var), "%%%u", match_idx); 42*e8d8bef9SDimitry Andric for (idx = 0; (percent_var_idx = new_command.find( 43*e8d8bef9SDimitry Andric percent_var, idx)) != std::string::npos;) { 44*e8d8bef9SDimitry Andric new_command.erase(percent_var_idx, percent_var_len); 45*e8d8bef9SDimitry Andric new_command.insert(percent_var_idx, match_str); 46*e8d8bef9SDimitry Andric idx += percent_var_idx + match_str.size(); 47*e8d8bef9SDimitry Andric } 48*e8d8bef9SDimitry Andric } 49*e8d8bef9SDimitry Andric } 50*e8d8bef9SDimitry Andric // Interpret the new command and return this as the result! 51*e8d8bef9SDimitry Andric if (m_interpreter.GetExpandRegexAliases()) 52*e8d8bef9SDimitry Andric result.GetOutputStream().Printf("%s\n", new_command.c_str()); 53*e8d8bef9SDimitry Andric // Pass in true for "no context switching". The command that called us 54*e8d8bef9SDimitry Andric // should have set up the context appropriately, we shouldn't have to 55*e8d8bef9SDimitry Andric // redo that. 56*e8d8bef9SDimitry Andric return m_interpreter.HandleCommand( 57*e8d8bef9SDimitry Andric new_command.c_str(), eLazyBoolCalculate, result, nullptr, true, true); 58*e8d8bef9SDimitry Andric } 59*e8d8bef9SDimitry Andric } 60*e8d8bef9SDimitry Andric result.SetStatus(eReturnStatusFailed); 61*e8d8bef9SDimitry Andric if (!GetSyntax().empty()) 62*e8d8bef9SDimitry Andric result.AppendError(GetSyntax()); 63*e8d8bef9SDimitry Andric else 64*e8d8bef9SDimitry Andric result.GetOutputStream() << "Command contents '" << command 65*e8d8bef9SDimitry Andric << "' failed to match any " 66*e8d8bef9SDimitry Andric "regular expression in the '" 67*e8d8bef9SDimitry Andric << m_cmd_name << "' regex "; 68*e8d8bef9SDimitry Andric return false; 69*e8d8bef9SDimitry Andric } 70*e8d8bef9SDimitry Andric 71*e8d8bef9SDimitry Andric bool CommandObjectRegexCommand::AddRegexCommand(llvm::StringRef re_cstr, 72*e8d8bef9SDimitry Andric llvm::StringRef command_cstr) { 73*e8d8bef9SDimitry Andric m_entries.resize(m_entries.size() + 1); 74*e8d8bef9SDimitry Andric // Only add the regular expression if it compiles 75*e8d8bef9SDimitry Andric m_entries.back().regex = RegularExpression(re_cstr); 76*e8d8bef9SDimitry Andric if (m_entries.back().regex.IsValid()) { 77*e8d8bef9SDimitry Andric m_entries.back().command = command_cstr.str(); 78*e8d8bef9SDimitry Andric return true; 79*e8d8bef9SDimitry Andric } 80*e8d8bef9SDimitry Andric // The regex didn't compile... 81*e8d8bef9SDimitry Andric m_entries.pop_back(); 82*e8d8bef9SDimitry Andric return false; 83*e8d8bef9SDimitry Andric } 84*e8d8bef9SDimitry Andric 85*e8d8bef9SDimitry Andric void CommandObjectRegexCommand::HandleCompletion(CompletionRequest &request) { 86*e8d8bef9SDimitry Andric if (m_completion_type_mask) { 87*e8d8bef9SDimitry Andric CommandCompletions::InvokeCommonCompletionCallbacks( 88*e8d8bef9SDimitry Andric GetCommandInterpreter(), m_completion_type_mask, request, nullptr); 89*e8d8bef9SDimitry Andric } 90*e8d8bef9SDimitry Andric } 91