2014-05-16 18:51:01 +08:00
|
|
|
//===-- MICmdInterpreter.cpp ------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// In-house headers:
|
|
|
|
#include "MICmdInterpreter.h"
|
|
|
|
#include "MICmdFactory.h"
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CMICmdInterpreter constructor.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2015-08-04 18:24:20 +08:00
|
|
|
CMICmdInterpreter::CMICmdInterpreter()
|
2016-09-07 04:57:50 +08:00
|
|
|
: m_rCmdFactory(CMICmdFactory::Instance()) {}
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: CMICmdInterpreter destructor.
|
|
|
|
// Type: Overridable.
|
|
|
|
// Args: None.
|
|
|
|
// Return: None.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
CMICmdInterpreter::~CMICmdInterpreter() { Shutdown(); }
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Initialize resources for *this Command Interpreter.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmdInterpreter::Initialize() {
|
|
|
|
m_clientUsageRefCnt++;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (m_bInitialized)
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
m_bInitialized = true;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Release resources for *this Command Interpreter.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: None.
|
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmdInterpreter::Shutdown() {
|
|
|
|
if (--m_clientUsageRefCnt > 0)
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
if (!m_bInitialized)
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
m_bInitialized = false;
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
return MIstatus::success;
|
2014-11-18 02:06:21 +08:00
|
|
|
}
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Establish whether the text data is an MI format type command.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vTextLine - (R) Text data to interpret.
|
2016-09-07 04:57:50 +08:00
|
|
|
// vwbYesValid - (W) True = MI type command, false = not
|
|
|
|
// recognised.
|
|
|
|
// vwbCmdNotInCmdFactor - (W) True = MI command not found in the
|
|
|
|
// command factory, false = recognised.
|
2014-11-18 02:06:21 +08:00
|
|
|
// Return: MIstatus::success - Functional succeeded.
|
|
|
|
// MIstatus::failure - Functional failed.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmdInterpreter::ValidateIsMi(const CMIUtilString &vTextLine,
|
|
|
|
bool &vwbYesValid,
|
|
|
|
bool &vwbCmdNotInCmdFactor,
|
|
|
|
SMICmdData &rwCmdData) {
|
|
|
|
vwbYesValid = false;
|
|
|
|
vwbCmdNotInCmdFactor = false;
|
|
|
|
rwCmdData.Clear();
|
|
|
|
|
|
|
|
if (vTextLine.empty())
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// MI format is [cmd #]-[command name]<space>[command arg(s)]
|
|
|
|
// i.e. 1-file-exec-and-symbols --thread-group i1 DEVICE_EXECUTABLE
|
|
|
|
// 5-data-evaluate-expression --thread 1 --frame 0 *(argv)
|
2014-06-25 00:35:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
m_miCmdData.Clear();
|
|
|
|
m_miCmdData.strMiCmd = vTextLine;
|
2014-05-16 18:51:01 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
// The following change m_miCmdData as valid parts are identified
|
|
|
|
vwbYesValid = (MiHasCmdTokenEndingHyphen(vTextLine) ||
|
|
|
|
MiHasCmdTokenEndingAlpha(vTextLine));
|
|
|
|
vwbYesValid = vwbYesValid && MiHasCmd(vTextLine);
|
|
|
|
if (vwbYesValid) {
|
|
|
|
vwbCmdNotInCmdFactor = !HasCmdFactoryGotMiCmd(MiGetCmdData());
|
|
|
|
vwbYesValid = !vwbCmdNotInCmdFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update command's meta data valid state
|
|
|
|
m_miCmdData.bCmdValid = vwbYesValid;
|
|
|
|
|
|
|
|
// Ok to return new updated command information
|
|
|
|
rwCmdData = MiGetCmdData();
|
|
|
|
|
|
|
|
return MIstatus::success;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Details: Establish whether the command name entered on the stdin stream is
|
|
|
|
// recognised by
|
2014-11-18 02:06:21 +08:00
|
|
|
// the MI driver.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vCmd - (R) Command information structure.
|
2016-09-07 04:57:50 +08:00
|
|
|
// Return: bool - True = yes command is recognised, false = command not
|
|
|
|
// recognised.
|
2014-11-18 02:06:21 +08:00
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmdInterpreter::HasCmdFactoryGotMiCmd(const SMICmdData &vCmd) const {
|
|
|
|
return m_rCmdFactory.CmdExist(vCmd.strMiCmd);
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Does the command entered match the criteria for a MI command format.
|
2016-09-07 04:57:50 +08:00
|
|
|
// The format to validate against is 'nn-' where there can be 1 to n
|
|
|
|
// digits.
|
2014-11-18 02:06:21 +08:00
|
|
|
// I.e. '2-gdb-exit'.
|
2016-09-07 04:57:50 +08:00
|
|
|
// Is the execution token present? The command token is entered into
|
|
|
|
// the
|
|
|
|
// command meta data structure whether correct or not for reporting or
|
|
|
|
// later
|
2014-11-18 02:06:21 +08:00
|
|
|
// command execution purposes.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vTextLine - (R) Text data to interpret.
|
2016-09-07 04:57:50 +08:00
|
|
|
// Return: bool - True = yes command token present, false = command not
|
|
|
|
// recognised.
|
2014-11-18 02:06:21 +08:00
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmdInterpreter::MiHasCmdTokenEndingHyphen(
|
|
|
|
const CMIUtilString &vTextLine) {
|
|
|
|
// The hyphen is mandatory
|
|
|
|
const size_t nPos = vTextLine.find('-', 0);
|
|
|
|
if ((nPos == std::string::npos))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (MiHasCmdTokenPresent(vTextLine)) {
|
|
|
|
const std::string strNum = vTextLine.substr(0, nPos);
|
|
|
|
if (!CMIUtilString(strNum).IsNumber())
|
|
|
|
return false;
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
m_miCmdData.strMiCmdToken = strNum;
|
|
|
|
}
|
2014-11-18 02:06:21 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
m_miCmdData.bMIOldStyle = false;
|
|
|
|
|
|
|
|
return true;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Does the command entered match the criteria for a MI command format.
|
2016-09-07 04:57:50 +08:00
|
|
|
// The format to validate against is 'nnA' where there can be 1 to n
|
|
|
|
// digits.
|
2014-11-18 02:06:21 +08:00
|
|
|
// 'A' represents any non numeric token. I.e. '1source .gdbinit'.
|
2016-09-07 04:57:50 +08:00
|
|
|
// Is the execution token present? The command token is entered into
|
|
|
|
// the
|
|
|
|
// command meta data structure whether correct or not for reporting or
|
|
|
|
// later
|
2014-11-18 02:06:21 +08:00
|
|
|
// command execution purposes.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vTextLine - (R) Text data to interpret.
|
2016-09-07 04:57:50 +08:00
|
|
|
// Return: bool - True = yes command token present, false = command not
|
|
|
|
// recognised.
|
2014-11-18 02:06:21 +08:00
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmdInterpreter::MiHasCmdTokenEndingAlpha(
|
|
|
|
const CMIUtilString &vTextLine) {
|
|
|
|
char cChar = vTextLine[0];
|
|
|
|
MIuint i = 0;
|
|
|
|
while (::isdigit(cChar) != 0) {
|
|
|
|
cChar = vTextLine[++i];
|
|
|
|
}
|
|
|
|
if (::isalpha(cChar) == 0)
|
|
|
|
return false;
|
|
|
|
if (i == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const std::string strNum = vTextLine.substr(0, i);
|
|
|
|
m_miCmdData.strMiCmdToken = strNum.c_str();
|
|
|
|
m_miCmdData.bMIOldStyle = true;
|
|
|
|
|
|
|
|
return true;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
2014-11-18 02:06:21 +08:00
|
|
|
// Details: Does the command entered match the criteria for a MI command format.
|
2015-06-18 13:27:05 +08:00
|
|
|
// Is the command token present before the hyphen?
|
2014-11-18 02:06:21 +08:00
|
|
|
// Type: Method.
|
|
|
|
// Args: vTextLine - (R) Text data to interpret.
|
|
|
|
// Return: bool - True = yes command token present, false = token not present.
|
|
|
|
// Throws: None.
|
2014-06-25 00:35:50 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmdInterpreter::MiHasCmdTokenPresent(const CMIUtilString &vTextLine) {
|
|
|
|
const size_t nPos = vTextLine.find('-', 0);
|
|
|
|
return (nPos > 0);
|
2014-06-25 00:35:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Details: Does the command name entered match the criteria for a MI command
|
|
|
|
// format.
|
|
|
|
// Is a recognised command present? The command name is entered into
|
|
|
|
// the
|
|
|
|
// command meta data structure whether correct or not for reporting or
|
|
|
|
// later
|
|
|
|
// command execution purposes. Command options is present are also put
|
|
|
|
// into the
|
2014-11-18 02:06:21 +08:00
|
|
|
// command meta data structure.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vTextLine - (R) Command information structure.
|
2016-09-07 04:57:50 +08:00
|
|
|
// Return: bool - True = yes command name present, false = command not
|
|
|
|
// recognised.
|
2014-11-18 02:06:21 +08:00
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
bool CMICmdInterpreter::MiHasCmd(const CMIUtilString &vTextLine) {
|
|
|
|
size_t nPos = 0;
|
|
|
|
if (m_miCmdData.bMIOldStyle) {
|
|
|
|
char cChar = vTextLine[0];
|
|
|
|
size_t i = 0;
|
|
|
|
while (::isdigit(cChar) != 0) {
|
|
|
|
cChar = vTextLine[++i];
|
2014-11-18 02:06:21 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
nPos = --i;
|
|
|
|
} else {
|
|
|
|
nPos = vTextLine.find('-', 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bFoundCmd = false;
|
|
|
|
const size_t nLen = vTextLine.length();
|
|
|
|
const size_t nPos2 = vTextLine.find(' ', nPos);
|
|
|
|
if (nPos2 != std::string::npos) {
|
|
|
|
if (nPos2 == nLen)
|
|
|
|
return false;
|
|
|
|
const CMIUtilString cmd =
|
|
|
|
CMIUtilString(vTextLine.substr(nPos + 1, nPos2 - nPos - 1));
|
|
|
|
if (cmd.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_miCmdData.strMiCmd = cmd;
|
|
|
|
|
|
|
|
if (nPos2 < nLen)
|
|
|
|
m_miCmdData.strMiCmdOption =
|
|
|
|
CMIUtilString(vTextLine.substr(nPos2 + 1, nLen - nPos2 - 1));
|
|
|
|
|
|
|
|
bFoundCmd = true;
|
|
|
|
} else {
|
|
|
|
const CMIUtilString cmd =
|
|
|
|
CMIUtilString(vTextLine.substr(nPos + 1, nLen - nPos - 1));
|
|
|
|
if (cmd.empty())
|
|
|
|
return false;
|
|
|
|
m_miCmdData.strMiCmd = cmd;
|
|
|
|
bFoundCmd = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bFoundCmd)
|
|
|
|
m_miCmdData.strMiCmdAll = vTextLine;
|
|
|
|
|
|
|
|
return bFoundCmd;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
//++
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Details: Retrieve the just entered new command from stdin. It contains the
|
|
|
|
// command
|
2014-11-18 02:06:21 +08:00
|
|
|
// name, number and any options.
|
|
|
|
// Type: Method.
|
|
|
|
// Args: vTextLine - (R) Command information structure.
|
|
|
|
// Return: SMICmdData & - Command meta data information/result/status.
|
|
|
|
// Throws: None.
|
2014-05-16 18:51:01 +08:00
|
|
|
//--
|
2016-09-07 04:57:50 +08:00
|
|
|
const SMICmdData &CMICmdInterpreter::MiGetCmdData() const {
|
|
|
|
return m_miCmdData;
|
2014-05-16 18:51:01 +08:00
|
|
|
}
|