blob: 7f2e8c0323c8efe8369a386aa19e22cc97d77b83 [file] [log] [blame]
Chris Lattner24943d22010-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
10#include "lldb/Interpreter/CommandObjectRegexCommand.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Interpreter/CommandInterpreter.h"
17#include "lldb/Interpreter/CommandReturnObject.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
22//----------------------------------------------------------------------
23// CommandObjectRegexCommand constructor
24//----------------------------------------------------------------------
25CommandObjectRegexCommand::CommandObjectRegexCommand
26(
Greg Clayton238c0a12010-09-18 01:14:36 +000027 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +000028 const char *name,
29 const char *help,
30 const char *syntax,
31 uint32_t max_matches
32) :
Greg Clayton238c0a12010-09-18 01:14:36 +000033 CommandObject (interpreter, name, help, syntax),
Benjamin Kramer7a45a2f2010-07-20 14:37:45 +000034 m_max_matches (max_matches),
35 m_entries ()
Chris Lattner24943d22010-06-08 16:52:24 +000036{
37}
38
39//----------------------------------------------------------------------
40// Destructor
41//----------------------------------------------------------------------
42CommandObjectRegexCommand::~CommandObjectRegexCommand()
43{
44}
45
46
47bool
48CommandObjectRegexCommand::Execute
49(
50 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +000051 CommandReturnObject &result
52)
53{
54 return false;
55}
56
57
58bool
59CommandObjectRegexCommand::ExecuteRawCommandString
60(
61 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +000062 CommandReturnObject &result
63)
64{
65 if (command)
66 {
67 EntryCollection::const_iterator pos, end = m_entries.end();
68 for (pos = m_entries.begin(); pos != end; ++pos)
69 {
70 if (pos->regex.Execute (command, m_max_matches))
71 {
72 std::string new_command(pos->command);
73 std::string match_str;
74 char percent_var[8];
75 size_t idx, percent_var_idx;
76 for (uint32_t match_idx=1; match_idx <= m_max_matches; ++match_idx)
77 {
78 if (pos->regex.GetMatchAtIndex (command, match_idx, match_str))
79 {
80 const int percent_var_len = ::snprintf (percent_var, sizeof(percent_var), "%%%u", match_idx);
81 for (idx = 0; (percent_var_idx = new_command.find(percent_var, idx)) != std::string::npos; )
82 {
83 new_command.erase(percent_var_idx, percent_var_len);
84 new_command.insert(percent_var_idx, match_str);
85 idx += percent_var_idx + match_str.size();
86 }
87 }
88 }
89 // Interpret the new command and return this as the result!
Greg Clayton168158a2010-10-12 18:04:53 +000090 result.GetOutputStream().Printf("%s\n", new_command.c_str());
Greg Clayton238c0a12010-09-18 01:14:36 +000091 return m_interpreter.HandleCommand(new_command.c_str(), true, result);
Chris Lattner24943d22010-06-08 16:52:24 +000092 }
93 }
94 result.SetStatus(eReturnStatusFailed);
Greg Clayton168158a2010-10-12 18:04:53 +000095 result.AppendErrorWithFormat ("Command contents '%s' failed to match any regular expression in the '%s' regex command.\n",
96 command,
97 m_cmd_name.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +000098 return false;
99 }
Greg Clayton5d187e52011-01-08 20:28:42 +0000100 result.AppendError("empty command passed to regular expression command");
Chris Lattner24943d22010-06-08 16:52:24 +0000101 result.SetStatus(eReturnStatusFailed);
102 return false;
103}
104
105
106bool
107CommandObjectRegexCommand::AddRegexCommand (const char *re_cstr, const char *command_cstr)
108{
109 m_entries.resize(m_entries.size() + 1);
110 // Only add the regular expression if it compiles
111 if (m_entries.back().regex.Compile (re_cstr, REG_EXTENDED))
112 {
113 m_entries.back().command.assign (command_cstr);
114 return true;
115 }
116 // The regex didn't compile...
117 m_entries.pop_back();
118 return false;
119}