blob: 0fc454eb4da76dec704b1332d4581d4e1457418a [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Interpreter/CommandObjectRegexCommand.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Interpreter/CommandInterpreter.h"
19#include "lldb/Interpreter/CommandReturnObject.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24//----------------------------------------------------------------------
25// CommandObjectRegexCommand constructor
26//----------------------------------------------------------------------
27CommandObjectRegexCommand::CommandObjectRegexCommand
28(
Greg Claytona7015092010-09-18 01:14:36 +000029 CommandInterpreter &interpreter,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030 const char *name,
31 const char *help,
32 const char *syntax,
Greg Clayton7d2ef162013-03-29 17:03:23 +000033 uint32_t max_matches,
34 uint32_t completion_type_mask
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035) :
Jim Ingham5a988412012-06-08 21:56:10 +000036 CommandObjectRaw (interpreter, name, help, syntax),
Benjamin Kramer40e155d2010-07-20 14:37:45 +000037 m_max_matches (max_matches),
Greg Clayton43fe2172013-04-03 02:00:15 +000038 m_completion_type_mask (completion_type_mask),
39 m_entries ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040{
41}
42
43//----------------------------------------------------------------------
44// Destructor
45//----------------------------------------------------------------------
46CommandObjectRegexCommand::~CommandObjectRegexCommand()
47{
48}
49
50
51bool
Jim Ingham5a988412012-06-08 21:56:10 +000052CommandObjectRegexCommand::DoExecute
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053(
54 const char *command,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055 CommandReturnObject &result
56)
57{
58 if (command)
59 {
60 EntryCollection::const_iterator pos, end = m_entries.end();
61 for (pos = m_entries.begin(); pos != end; ++pos)
62 {
63 if (pos->regex.Execute (command, m_max_matches))
64 {
65 std::string new_command(pos->command);
66 std::string match_str;
67 char percent_var[8];
68 size_t idx, percent_var_idx;
69 for (uint32_t match_idx=1; match_idx <= m_max_matches; ++match_idx)
70 {
71 if (pos->regex.GetMatchAtIndex (command, match_idx, match_str))
72 {
73 const int percent_var_len = ::snprintf (percent_var, sizeof(percent_var), "%%%u", match_idx);
74 for (idx = 0; (percent_var_idx = new_command.find(percent_var, idx)) != std::string::npos; )
75 {
76 new_command.erase(percent_var_idx, percent_var_len);
77 new_command.insert(percent_var_idx, match_str);
78 idx += percent_var_idx + match_str.size();
79 }
80 }
81 }
82 // Interpret the new command and return this as the result!
Greg Clayton754a9362012-08-23 00:22:02 +000083 if (m_interpreter.GetExpandRegexAliases())
84 result.GetOutputStream().Printf("%s\n", new_command.c_str());
Jim Ingham31938ca2013-03-15 22:18:26 +000085 // Pass in true for "no context switching". The command that called us should have set up the context
86 // appropriately, we shouldn't have to redo that.
87 return m_interpreter.HandleCommand(new_command.c_str(), eLazyBoolCalculate, result, NULL, true, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088 }
89 }
90 result.SetStatus(eReturnStatusFailed);
Jim Ingham215341c2012-10-06 00:27:04 +000091 if (GetSyntax() != NULL)
92 result.AppendError (GetSyntax());
93 else
94 result.AppendErrorWithFormat ("Command contents '%s' failed to match any regular expression in the '%s' regex command.\n",
95 command,
96 m_cmd_name.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097 return false;
98 }
Greg Clayton710dd5a2011-01-08 20:28:42 +000099 result.AppendError("empty command passed to regular expression command");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 result.SetStatus(eReturnStatusFailed);
101 return false;
102}
103
104
105bool
106CommandObjectRegexCommand::AddRegexCommand (const char *re_cstr, const char *command_cstr)
107{
108 m_entries.resize(m_entries.size() + 1);
109 // Only add the regular expression if it compiles
110 if (m_entries.back().regex.Compile (re_cstr, REG_EXTENDED))
111 {
112 m_entries.back().command.assign (command_cstr);
113 return true;
114 }
115 // The regex didn't compile...
116 m_entries.pop_back();
117 return false;
118}
Greg Clayton7d2ef162013-03-29 17:03:23 +0000119
120int
121CommandObjectRegexCommand::HandleCompletion (Args &input,
122 int &cursor_index,
123 int &cursor_char_position,
124 int match_start_point,
125 int max_return_elements,
126 bool &word_complete,
127 StringList &matches)
128{
129 if (m_completion_type_mask)
130 {
131 std::string completion_str (input.GetArgumentAtIndex (cursor_index), cursor_char_position);
132 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
133 m_completion_type_mask,
134 completion_str.c_str(),
135 match_start_point,
136 max_return_elements,
137 NULL,
138 word_complete,
139 matches);
140 return matches.GetSize();
141 }
142 else
143 {
144 matches.Clear();
145 word_complete = false;
146 }
147 return 0;
148}