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 | ( |
| 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 | //---------------------------------------------------------------------- |
| 41 | CommandObjectRegexCommand::~CommandObjectRegexCommand() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | |
| 46 | bool |
| 47 | CommandObjectRegexCommand::Execute |
| 48 | ( |
| 49 | Args& command, |
| 50 | CommandContext *context, |
| 51 | CommandInterpreter *interpreter, |
| 52 | CommandReturnObject &result |
| 53 | ) |
| 54 | { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | bool |
| 60 | CommandObjectRegexCommand::ExecuteRawCommandString |
| 61 | ( |
| 62 | const char *command, |
| 63 | CommandContext *context, |
| 64 | CommandInterpreter *interpreter, |
| 65 | CommandReturnObject &result |
| 66 | ) |
| 67 | { |
| 68 | if (command) |
| 69 | { |
| 70 | EntryCollection::const_iterator pos, end = m_entries.end(); |
| 71 | for (pos = m_entries.begin(); pos != end; ++pos) |
| 72 | { |
| 73 | if (pos->regex.Execute (command, m_max_matches)) |
| 74 | { |
| 75 | std::string new_command(pos->command); |
| 76 | std::string match_str; |
| 77 | char percent_var[8]; |
| 78 | size_t idx, percent_var_idx; |
| 79 | for (uint32_t match_idx=1; match_idx <= m_max_matches; ++match_idx) |
| 80 | { |
| 81 | if (pos->regex.GetMatchAtIndex (command, match_idx, match_str)) |
| 82 | { |
| 83 | const int percent_var_len = ::snprintf (percent_var, sizeof(percent_var), "%%%u", match_idx); |
| 84 | for (idx = 0; (percent_var_idx = new_command.find(percent_var, idx)) != std::string::npos; ) |
| 85 | { |
| 86 | new_command.erase(percent_var_idx, percent_var_len); |
| 87 | new_command.insert(percent_var_idx, match_str); |
| 88 | idx += percent_var_idx + match_str.size(); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | // Interpret the new command and return this as the result! |
| 93 | // if (m_options.verbose) |
| 94 | // result.GetOutputStream().Printf("%s\n", new_command.c_str()); |
| 95 | return interpreter->HandleCommand(new_command.c_str(), true, result); |
| 96 | } |
| 97 | } |
| 98 | result.SetStatus(eReturnStatusFailed); |
| 99 | result.AppendErrorWithFormat("Command contents '%s' failed to match any regular expression in the '%s' regex command.\n", |
| 100 | command, |
| 101 | m_cmd_name.c_str()); |
| 102 | return false; |
| 103 | } |
| 104 | result.AppendError("empty command passed to regular exression command"); |
| 105 | result.SetStatus(eReturnStatusFailed); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | bool |
| 111 | CommandObjectRegexCommand::AddRegexCommand (const char *re_cstr, const char *command_cstr) |
| 112 | { |
| 113 | m_entries.resize(m_entries.size() + 1); |
| 114 | // Only add the regular expression if it compiles |
| 115 | if (m_entries.back().regex.Compile (re_cstr, REG_EXTENDED)) |
| 116 | { |
| 117 | m_entries.back().command.assign (command_cstr); |
| 118 | return true; |
| 119 | } |
| 120 | // The regex didn't compile... |
| 121 | m_entries.pop_back(); |
| 122 | return false; |
| 123 | } |