blob: de6faba4d877779b38ab0f2481507ce869a64d6d [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) :
Jim Inghamda26bd22012-06-08 21:56:10 +000033 CommandObjectRaw (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
Jim Inghamda26bd22012-06-08 21:56:10 +000048CommandObjectRegexCommand::DoExecute
Chris Lattner24943d22010-06-08 16:52:24 +000049(
50 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +000051 CommandReturnObject &result
52)
53{
54 if (command)
55 {
56 EntryCollection::const_iterator pos, end = m_entries.end();
57 for (pos = m_entries.begin(); pos != end; ++pos)
58 {
59 if (pos->regex.Execute (command, m_max_matches))
60 {
61 std::string new_command(pos->command);
62 std::string match_str;
63 char percent_var[8];
64 size_t idx, percent_var_idx;
65 for (uint32_t match_idx=1; match_idx <= m_max_matches; ++match_idx)
66 {
67 if (pos->regex.GetMatchAtIndex (command, match_idx, match_str))
68 {
69 const int percent_var_len = ::snprintf (percent_var, sizeof(percent_var), "%%%u", match_idx);
70 for (idx = 0; (percent_var_idx = new_command.find(percent_var, idx)) != std::string::npos; )
71 {
72 new_command.erase(percent_var_idx, percent_var_len);
73 new_command.insert(percent_var_idx, match_str);
74 idx += percent_var_idx + match_str.size();
75 }
76 }
77 }
78 // Interpret the new command and return this as the result!
Greg Clayton168158a2010-10-12 18:04:53 +000079 result.GetOutputStream().Printf("%s\n", new_command.c_str());
Enrico Granata01bc2d42012-05-31 01:09:06 +000080 return m_interpreter.HandleCommand(new_command.c_str(), eLazyBoolCalculate, result);
Chris Lattner24943d22010-06-08 16:52:24 +000081 }
82 }
83 result.SetStatus(eReturnStatusFailed);
Greg Clayton168158a2010-10-12 18:04:53 +000084 result.AppendErrorWithFormat ("Command contents '%s' failed to match any regular expression in the '%s' regex command.\n",
85 command,
86 m_cmd_name.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +000087 return false;
88 }
Greg Clayton5d187e52011-01-08 20:28:42 +000089 result.AppendError("empty command passed to regular expression command");
Chris Lattner24943d22010-06-08 16:52:24 +000090 result.SetStatus(eReturnStatusFailed);
91 return false;
92}
93
94
95bool
96CommandObjectRegexCommand::AddRegexCommand (const char *re_cstr, const char *command_cstr)
97{
98 m_entries.resize(m_entries.size() + 1);
99 // Only add the regular expression if it compiles
100 if (m_entries.back().regex.Compile (re_cstr, REG_EXTENDED))
101 {
102 m_entries.back().command.assign (command_cstr);
103 return true;
104 }
105 // The regex didn't compile...
106 m_entries.pop_back();
107 return false;
108}