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