blob: e165fc8009aa2b49e5549b20db84d8d31cd87678 [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(
27 const char *name,
28 const char *help,
29 const char *syntax,
30 uint32_t max_matches
31) :
32 CommandObject (name, help, syntax),
33 m_entries(),
34 m_max_matches (max_matches)
35{
36}
37
38//----------------------------------------------------------------------
39// Destructor
40//----------------------------------------------------------------------
41CommandObjectRegexCommand::~CommandObjectRegexCommand()
42{
43}
44
45
46bool
47CommandObjectRegexCommand::Execute
48(
Greg Clayton63094e02010-06-23 01:19:29 +000049 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +000050 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +000051 CommandReturnObject &result
52)
53{
54 return false;
55}
56
57
58bool
59CommandObjectRegexCommand::ExecuteRawCommandString
60(
Greg Clayton63094e02010-06-23 01:19:29 +000061 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +000062 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +000063 CommandReturnObject &result
64)
65{
66 if (command)
67 {
68 EntryCollection::const_iterator pos, end = m_entries.end();
69 for (pos = m_entries.begin(); pos != end; ++pos)
70 {
71 if (pos->regex.Execute (command, m_max_matches))
72 {
73 std::string new_command(pos->command);
74 std::string match_str;
75 char percent_var[8];
76 size_t idx, percent_var_idx;
77 for (uint32_t match_idx=1; match_idx <= m_max_matches; ++match_idx)
78 {
79 if (pos->regex.GetMatchAtIndex (command, match_idx, match_str))
80 {
81 const int percent_var_len = ::snprintf (percent_var, sizeof(percent_var), "%%%u", match_idx);
82 for (idx = 0; (percent_var_idx = new_command.find(percent_var, idx)) != std::string::npos; )
83 {
84 new_command.erase(percent_var_idx, percent_var_len);
85 new_command.insert(percent_var_idx, match_str);
86 idx += percent_var_idx + match_str.size();
87 }
88 }
89 }
90 // Interpret the new command and return this as the result!
91// if (m_options.verbose)
92// result.GetOutputStream().Printf("%s\n", new_command.c_str());
Greg Clayton63094e02010-06-23 01:19:29 +000093 return interpreter.HandleCommand(new_command.c_str(), true, result);
Chris Lattner24943d22010-06-08 16:52:24 +000094 }
95 }
96 result.SetStatus(eReturnStatusFailed);
97 result.AppendErrorWithFormat("Command contents '%s' failed to match any regular expression in the '%s' regex command.\n",
98 command,
99 m_cmd_name.c_str());
100 return false;
101 }
102 result.AppendError("empty command passed to regular exression command");
103 result.SetStatus(eReturnStatusFailed);
104 return false;
105}
106
107
108bool
109CommandObjectRegexCommand::AddRegexCommand (const char *re_cstr, const char *command_cstr)
110{
111 m_entries.resize(m_entries.size() + 1);
112 // Only add the regular expression if it compiles
113 if (m_entries.back().regex.Compile (re_cstr, REG_EXTENDED))
114 {
115 m_entries.back().command.assign (command_cstr);
116 return true;
117 }
118 // The regex didn't compile...
119 m_entries.pop_back();
120 return false;
121}