2010-06-09 00:52:24 +08:00
|
|
|
//===-- CommandObjectRegexCommand.cpp ---------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-05 08:20:57 +08:00
|
|
|
#include "lldb/lldb-python.h"
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/Interpreter/CommandObjectRegexCommand.h"
|
|
|
|
|
|
|
|
// C Includes
|
|
|
|
// C++ Includes
|
|
|
|
// Other libraries and framework includes
|
|
|
|
// Project includes
|
|
|
|
#include "lldb/Interpreter/CommandInterpreter.h"
|
|
|
|
#include "lldb/Interpreter/CommandReturnObject.h"
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// CommandObjectRegexCommand constructor
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
CommandObjectRegexCommand::CommandObjectRegexCommand
|
|
|
|
(
|
2010-09-18 09:14:36 +08:00
|
|
|
CommandInterpreter &interpreter,
|
2010-06-09 00:52:24 +08:00
|
|
|
const char *name,
|
|
|
|
const char *help,
|
|
|
|
const char *syntax,
|
2013-03-30 01:03:23 +08:00
|
|
|
uint32_t max_matches,
|
|
|
|
uint32_t completion_type_mask
|
2010-06-09 00:52:24 +08:00
|
|
|
) :
|
2012-06-09 05:56:10 +08:00
|
|
|
CommandObjectRaw (interpreter, name, help, syntax),
|
2010-07-20 22:37:45 +08:00
|
|
|
m_max_matches (max_matches),
|
2013-03-30 01:03:23 +08:00
|
|
|
m_entries (),
|
|
|
|
m_completion_type_mask (completion_type_mask)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Destructor
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
CommandObjectRegexCommand::~CommandObjectRegexCommand()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
2012-06-09 05:56:10 +08:00
|
|
|
CommandObjectRegexCommand::DoExecute
|
2010-06-09 00:52:24 +08:00
|
|
|
(
|
|
|
|
const char *command,
|
|
|
|
CommandReturnObject &result
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (command)
|
|
|
|
{
|
|
|
|
EntryCollection::const_iterator pos, end = m_entries.end();
|
|
|
|
for (pos = m_entries.begin(); pos != end; ++pos)
|
|
|
|
{
|
|
|
|
if (pos->regex.Execute (command, m_max_matches))
|
|
|
|
{
|
|
|
|
std::string new_command(pos->command);
|
|
|
|
std::string match_str;
|
|
|
|
char percent_var[8];
|
|
|
|
size_t idx, percent_var_idx;
|
|
|
|
for (uint32_t match_idx=1; match_idx <= m_max_matches; ++match_idx)
|
|
|
|
{
|
|
|
|
if (pos->regex.GetMatchAtIndex (command, match_idx, match_str))
|
|
|
|
{
|
|
|
|
const int percent_var_len = ::snprintf (percent_var, sizeof(percent_var), "%%%u", match_idx);
|
|
|
|
for (idx = 0; (percent_var_idx = new_command.find(percent_var, idx)) != std::string::npos; )
|
|
|
|
{
|
|
|
|
new_command.erase(percent_var_idx, percent_var_len);
|
|
|
|
new_command.insert(percent_var_idx, match_str);
|
|
|
|
idx += percent_var_idx + match_str.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Interpret the new command and return this as the result!
|
2012-08-23 08:22:02 +08:00
|
|
|
if (m_interpreter.GetExpandRegexAliases())
|
|
|
|
result.GetOutputStream().Printf("%s\n", new_command.c_str());
|
2013-03-16 06:18:26 +08:00
|
|
|
// Pass in true for "no context switching". The command that called us should have set up the context
|
|
|
|
// appropriately, we shouldn't have to redo that.
|
|
|
|
return m_interpreter.HandleCommand(new_command.c_str(), eLazyBoolCalculate, result, NULL, true, true);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result.SetStatus(eReturnStatusFailed);
|
2012-10-06 08:27:04 +08:00
|
|
|
if (GetSyntax() != NULL)
|
|
|
|
result.AppendError (GetSyntax());
|
|
|
|
else
|
|
|
|
result.AppendErrorWithFormat ("Command contents '%s' failed to match any regular expression in the '%s' regex command.\n",
|
|
|
|
command,
|
|
|
|
m_cmd_name.c_str());
|
2010-06-09 00:52:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
2011-01-09 04:28:42 +08:00
|
|
|
result.AppendError("empty command passed to regular expression command");
|
2010-06-09 00:52:24 +08:00
|
|
|
result.SetStatus(eReturnStatusFailed);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
CommandObjectRegexCommand::AddRegexCommand (const char *re_cstr, const char *command_cstr)
|
|
|
|
{
|
|
|
|
m_entries.resize(m_entries.size() + 1);
|
|
|
|
// Only add the regular expression if it compiles
|
|
|
|
if (m_entries.back().regex.Compile (re_cstr, REG_EXTENDED))
|
|
|
|
{
|
|
|
|
m_entries.back().command.assign (command_cstr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// The regex didn't compile...
|
|
|
|
m_entries.pop_back();
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-30 01:03:23 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
CommandObjectRegexCommand::HandleCompletion (Args &input,
|
|
|
|
int &cursor_index,
|
|
|
|
int &cursor_char_position,
|
|
|
|
int match_start_point,
|
|
|
|
int max_return_elements,
|
|
|
|
bool &word_complete,
|
|
|
|
StringList &matches)
|
|
|
|
{
|
|
|
|
if (m_completion_type_mask)
|
|
|
|
{
|
|
|
|
std::string completion_str (input.GetArgumentAtIndex (cursor_index), cursor_char_position);
|
|
|
|
CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter,
|
|
|
|
m_completion_type_mask,
|
|
|
|
completion_str.c_str(),
|
|
|
|
match_start_point,
|
|
|
|
max_return_elements,
|
|
|
|
NULL,
|
|
|
|
word_complete,
|
|
|
|
matches);
|
|
|
|
return matches.GetSize();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
matches.Clear();
|
|
|
|
word_complete = false;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|