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 | |
| 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 | |
| 19 | using namespace lldb; |
| 20 | using namespace lldb_private; |
| 21 | |
| 22 | //---------------------------------------------------------------------- |
| 23 | // CommandObjectRegexCommand constructor |
| 24 | //---------------------------------------------------------------------- |
| 25 | CommandObjectRegexCommand::CommandObjectRegexCommand |
| 26 | ( |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 27 | CommandInterpreter &interpreter, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | const char *name, |
| 29 | const char *help, |
| 30 | const char *syntax, |
| 31 | uint32_t max_matches |
| 32 | ) : |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 33 | CommandObject (interpreter, name, help, syntax), |
Benjamin Kramer | 7a45a2f | 2010-07-20 14:37:45 +0000 | [diff] [blame] | 34 | m_max_matches (max_matches), |
| 35 | m_entries () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | //---------------------------------------------------------------------- |
| 40 | // Destructor |
| 41 | //---------------------------------------------------------------------- |
| 42 | CommandObjectRegexCommand::~CommandObjectRegexCommand() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | |
| 47 | bool |
| 48 | CommandObjectRegexCommand::Execute |
| 49 | ( |
| 50 | Args& command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | CommandReturnObject &result |
| 52 | ) |
| 53 | { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | bool |
| 59 | CommandObjectRegexCommand::ExecuteRawCommandString |
| 60 | ( |
| 61 | const char *command, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | 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! |
Greg Clayton | 168158a | 2010-10-12 18:04:53 +0000 | [diff] [blame] | 90 | result.GetOutputStream().Printf("%s\n", new_command.c_str()); |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 91 | return m_interpreter.HandleCommand(new_command.c_str(), true, result); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | result.SetStatus(eReturnStatusFailed); |
Greg Clayton | 168158a | 2010-10-12 18:04:53 +0000 | [diff] [blame] | 95 | result.AppendErrorWithFormat ("Command contents '%s' failed to match any regular expression in the '%s' regex command.\n", |
| 96 | command, |
| 97 | m_cmd_name.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | return false; |
| 99 | } |
Greg Clayton | 5d187e5 | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 100 | result.AppendError("empty command passed to regular expression command"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | result.SetStatus(eReturnStatusFailed); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | bool |
| 107 | CommandObjectRegexCommand::AddRegexCommand (const char *re_cstr, const char *command_cstr) |
| 108 | { |
| 109 | m_entries.resize(m_entries.size() + 1); |
| 110 | // Only add the regular expression if it compiles |
| 111 | if (m_entries.back().regex.Compile (re_cstr, REG_EXTENDED)) |
| 112 | { |
| 113 | m_entries.back().command.assign (command_cstr); |
| 114 | return true; |
| 115 | } |
| 116 | // The regex didn't compile... |
| 117 | m_entries.pop_back(); |
| 118 | return false; |
| 119 | } |