1 //===-- CommandObjectRegexCommand.cpp ---------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Interpreter/CommandObjectRegexCommand.h"
14 // Other libraries and framework includes
16 #include "lldb/Interpreter/CommandInterpreter.h"
17 #include "lldb/Interpreter/CommandReturnObject.h"
20 using namespace lldb_private;
22 //----------------------------------------------------------------------
23 // CommandObjectRegexCommand constructor
24 //----------------------------------------------------------------------
25 CommandObjectRegexCommand::CommandObjectRegexCommand
27 CommandInterpreter &interpreter,
32 uint32_t completion_type_mask,
35 CommandObjectRaw (interpreter, name, help, syntax),
36 m_max_matches (max_matches),
37 m_completion_type_mask (completion_type_mask),
39 m_is_removable (is_removable)
43 //----------------------------------------------------------------------
45 //----------------------------------------------------------------------
46 CommandObjectRegexCommand::~CommandObjectRegexCommand()
52 CommandObjectRegexCommand::DoExecute
55 CommandReturnObject &result
60 EntryCollection::const_iterator pos, end = m_entries.end();
61 for (pos = m_entries.begin(); pos != end; ++pos)
63 RegularExpression::Match regex_match(m_max_matches);
65 if (pos->regex.Execute (command, ®ex_match))
67 std::string new_command(pos->command);
68 std::string match_str;
70 size_t idx, percent_var_idx;
71 for (uint32_t match_idx=1; match_idx <= m_max_matches; ++match_idx)
73 if (regex_match.GetMatchAtIndex (command, match_idx, match_str))
75 const int percent_var_len = ::snprintf (percent_var, sizeof(percent_var), "%%%u", match_idx);
76 for (idx = 0; (percent_var_idx = new_command.find(percent_var, idx)) != std::string::npos; )
78 new_command.erase(percent_var_idx, percent_var_len);
79 new_command.insert(percent_var_idx, match_str);
80 idx += percent_var_idx + match_str.size();
84 // Interpret the new command and return this as the result!
85 if (m_interpreter.GetExpandRegexAliases())
86 result.GetOutputStream().Printf("%s\n", new_command.c_str());
87 // Pass in true for "no context switching". The command that called us should have set up the context
88 // appropriately, we shouldn't have to redo that.
89 return m_interpreter.HandleCommand(new_command.c_str(), eLazyBoolCalculate, result, nullptr, true, true);
92 result.SetStatus(eReturnStatusFailed);
93 if (GetSyntax() != nullptr)
94 result.AppendError (GetSyntax());
96 result.AppendErrorWithFormat ("Command contents '%s' failed to match any regular expression in the '%s' regex command.\n",
101 result.AppendError("empty command passed to regular expression command");
102 result.SetStatus(eReturnStatusFailed);
108 CommandObjectRegexCommand::AddRegexCommand (const char *re_cstr, const char *command_cstr)
110 m_entries.resize(m_entries.size() + 1);
111 // Only add the regular expression if it compiles
112 if (m_entries.back().regex.Compile (re_cstr))
114 m_entries.back().command.assign (command_cstr);
117 // The regex didn't compile...
118 m_entries.pop_back();
123 CommandObjectRegexCommand::HandleCompletion (Args &input,
125 int &cursor_char_position,
126 int match_start_point,
127 int max_return_elements,
131 if (m_completion_type_mask)
133 std::string completion_str (input.GetArgumentAtIndex (cursor_index), cursor_char_position);
134 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
135 m_completion_type_mask,
136 completion_str.c_str(),
142 return matches.GetSize();
147 word_complete = false;