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