blob: 534038aeafff56f27222f512c8852a49b78a10a2 [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!
90// if (m_options.verbose)
91// result.GetOutputStream().Printf("%s\n", new_command.c_str());
Greg Clayton238c0a12010-09-18 01:14:36 +000092 return m_interpreter.HandleCommand(new_command.c_str(), true, result);
Chris Lattner24943d22010-06-08 16:52:24 +000093 }
94 }
95 result.SetStatus(eReturnStatusFailed);
96 result.AppendErrorWithFormat("Command contents '%s' failed to match any regular expression in the '%s' regex command.\n",
97 command,
98 m_cmd_name.c_str());
99 return false;
100 }
101 result.AppendError("empty command passed to regular exression command");
102 result.SetStatus(eReturnStatusFailed);
103 return false;
104}
105
106
107bool
108CommandObjectRegexCommand::AddRegexCommand (const char *re_cstr, const char *command_cstr)
109{
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, REG_EXTENDED))
113 {
114 m_entries.back().command.assign (command_cstr);
115 return true;
116 }
117 // The regex didn't compile...
118 m_entries.pop_back();
119 return false;
120}