blob: 5ee44e2490325946afbf102efe7f6b6791b0edad [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
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-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 Clayton238c0a12010-09-18 01:14:36 +000029 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +000030 const char *name,
31 const char *help,
32 const char *syntax,
33 uint32_t max_matches
34) :
Jim Inghamda26bd22012-06-08 21:56:10 +000035 CommandObjectRaw (interpreter, name, help, syntax),
Benjamin Kramer7a45a2f2010-07-20 14:37:45 +000036 m_max_matches (max_matches),
37 m_entries ()
Chris Lattner24943d22010-06-08 16:52:24 +000038{
39}
40
41//----------------------------------------------------------------------
42// Destructor
43//----------------------------------------------------------------------
44CommandObjectRegexCommand::~CommandObjectRegexCommand()
45{
46}
47
48
49bool
Jim Inghamda26bd22012-06-08 21:56:10 +000050CommandObjectRegexCommand::DoExecute
Chris Lattner24943d22010-06-08 16:52:24 +000051(
52 const char *command,
Chris Lattner24943d22010-06-08 16:52:24 +000053 CommandReturnObject &result
54)
55{
56 if (command)
57 {
58 EntryCollection::const_iterator pos, end = m_entries.end();
59 for (pos = m_entries.begin(); pos != end; ++pos)
60 {
61 if (pos->regex.Execute (command, m_max_matches))
62 {
63 std::string new_command(pos->command);
64 std::string match_str;
65 char percent_var[8];
66 size_t idx, percent_var_idx;
67 for (uint32_t match_idx=1; match_idx <= m_max_matches; ++match_idx)
68 {
69 if (pos->regex.GetMatchAtIndex (command, match_idx, match_str))
70 {
71 const int percent_var_len = ::snprintf (percent_var, sizeof(percent_var), "%%%u", match_idx);
72 for (idx = 0; (percent_var_idx = new_command.find(percent_var, idx)) != std::string::npos; )
73 {
74 new_command.erase(percent_var_idx, percent_var_len);
75 new_command.insert(percent_var_idx, match_str);
76 idx += percent_var_idx + match_str.size();
77 }
78 }
79 }
80 // Interpret the new command and return this as the result!
Greg Clayton9f282852012-08-23 00:22:02 +000081 if (m_interpreter.GetExpandRegexAliases())
82 result.GetOutputStream().Printf("%s\n", new_command.c_str());
Enrico Granata01bc2d42012-05-31 01:09:06 +000083 return m_interpreter.HandleCommand(new_command.c_str(), eLazyBoolCalculate, result);
Chris Lattner24943d22010-06-08 16:52:24 +000084 }
85 }
86 result.SetStatus(eReturnStatusFailed);
Jim Ingham6937bc12012-10-06 00:27:04 +000087 if (GetSyntax() != NULL)
88 result.AppendError (GetSyntax());
89 else
90 result.AppendErrorWithFormat ("Command contents '%s' failed to match any regular expression in the '%s' regex command.\n",
91 command,
92 m_cmd_name.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +000093 return false;
94 }
Greg Clayton5d187e52011-01-08 20:28:42 +000095 result.AppendError("empty command passed to regular expression command");
Chris Lattner24943d22010-06-08 16:52:24 +000096 result.SetStatus(eReturnStatusFailed);
97 return false;
98}
99
100
101bool
102CommandObjectRegexCommand::AddRegexCommand (const char *re_cstr, const char *command_cstr)
103{
104 m_entries.resize(m_entries.size() + 1);
105 // Only add the regular expression if it compiles
106 if (m_entries.back().regex.Compile (re_cstr, REG_EXTENDED))
107 {
108 m_entries.back().command.assign (command_cstr);
109 return true;
110 }
111 // The regex didn't compile...
112 m_entries.pop_back();
113 return false;
114}