Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Malea | d891f9b | 2012-12-05 00:20:57 +0000 | [diff] [blame^] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #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 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | //---------------------------------------------------------------------- |
| 25 | // CommandObjectRegexCommand constructor |
| 26 | //---------------------------------------------------------------------- |
| 27 | CommandObjectRegexCommand::CommandObjectRegexCommand |
| 28 | ( |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 29 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | const char *name, |
| 31 | const char *help, |
| 32 | const char *syntax, |
| 33 | uint32_t max_matches |
| 34 | ) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 35 | CommandObjectRaw (interpreter, name, help, syntax), |
Benjamin Kramer | 7a45a2f | 2010-07-20 14:37:45 +0000 | [diff] [blame] | 36 | m_max_matches (max_matches), |
| 37 | m_entries () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | { |
| 39 | } |
| 40 | |
| 41 | //---------------------------------------------------------------------- |
| 42 | // Destructor |
| 43 | //---------------------------------------------------------------------- |
| 44 | CommandObjectRegexCommand::~CommandObjectRegexCommand() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | |
| 49 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 50 | CommandObjectRegexCommand::DoExecute |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | ( |
| 52 | const char *command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | 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 Clayton | 9f28285 | 2012-08-23 00:22:02 +0000 | [diff] [blame] | 81 | if (m_interpreter.GetExpandRegexAliases()) |
| 82 | result.GetOutputStream().Printf("%s\n", new_command.c_str()); |
Enrico Granata | 01bc2d4 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 83 | return m_interpreter.HandleCommand(new_command.c_str(), eLazyBoolCalculate, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | result.SetStatus(eReturnStatusFailed); |
Jim Ingham | 6937bc1 | 2012-10-06 00:27:04 +0000 | [diff] [blame] | 87 | 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | return false; |
| 94 | } |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 95 | result.AppendError("empty command passed to regular expression command"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | result.SetStatus(eReturnStatusFailed); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | bool |
| 102 | CommandObjectRegexCommand::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 | } |