1 //===-- MICmdInterpreter.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // File: MICmdInterpreter.cpp
13 // Overview: CMICmdInterpreter implementation.
15 // Environment: Compilers: Visual C++ 12.
16 // gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 // Libraries: See MIReadmetxt.
23 #include "MICmdInterpreter.h"
24 #include "MICmdFactory.h"
26 //++ ------------------------------------------------------------------------------------
27 // Details: CMICmdInterpreter constructor.
33 CMICmdInterpreter::CMICmdInterpreter(void)
34 : m_rCmdFactory(CMICmdFactory::Instance())
38 //++ ------------------------------------------------------------------------------------
39 // Details: CMICmdInterpreter destructor.
45 CMICmdInterpreter::~CMICmdInterpreter(void)
50 //++ ------------------------------------------------------------------------------------
51 // Details: Initialize resources for *this Command Interpreter.
54 // Return: MIstatus::success - Functional succeeded.
55 // MIstatus::failure - Functional failed.
59 CMICmdInterpreter::Initialize(void)
61 m_clientUsageRefCnt++;
64 return MIstatus::success;
66 m_bInitialized = true;
68 return MIstatus::success;
71 //++ ------------------------------------------------------------------------------------
72 // Details: Release resources for *this Command Interpreter.
75 // Return: MIstatus::success - Functional succeeded.
76 // MIstatus::failure - Functional failed.
80 CMICmdInterpreter::Shutdown(void)
82 if (--m_clientUsageRefCnt > 0)
83 return MIstatus::success;
86 return MIstatus::success;
88 m_bInitialized = false;
90 return MIstatus::success;
93 //++ ------------------------------------------------------------------------------------
94 // Details: Establish whether the text data is an MI format type command.
96 // Args: vTextLine - (R) Text data to interpret.
97 // vwbYesValid - (W) True = MI type command, false = not recognised.
98 // vwbCmdNotInCmdFactor - (W) True = MI command not found in the command factory, false = recognised.
99 // Return: MIstatus::success - Functional succeeded.
100 // MIstatus::failure - Functional failed.
104 CMICmdInterpreter::ValidateIsMi(const CMIUtilString &vTextLine, bool &vwbYesValid, bool &vwbCmdNotInCmdFactor, SMICmdData &rwCmdData)
107 vwbCmdNotInCmdFactor = false;
110 if (vTextLine.empty())
111 return MIstatus::success;
113 // MI format is [cmd #]-[command name]<space>[command arg(s)]
114 // i.e. 1-file-exec-and-symbols --thread-group i1 DEVICE_EXECUTABLE
115 // 5-data-evaluate-expression --thread 1 --frame 0 *(argv)
118 m_miCmdData.strMiCmd = vTextLine;
120 // The following change m_miCmdData as valid parts are indentified
121 vwbYesValid = (MiHasCmdTokenEndingHypthen(vTextLine) || MiHasCmdTokenEndingAlpha(vTextLine));
122 vwbYesValid = vwbYesValid && MiHasCmd(vTextLine);
125 vwbCmdNotInCmdFactor = !HasCmdFactoryGotMiCmd(MiGetCmdData());
126 vwbYesValid = !vwbCmdNotInCmdFactor;
129 // Update command's meta data valid state
130 m_miCmdData.bCmdValid = vwbYesValid;
132 // Ok to return new updated command information
133 rwCmdData = MiGetCmdData();
135 return MIstatus::success;
138 //++ ------------------------------------------------------------------------------------
139 // Details: Establish whether the command name entered on the stdin stream is recognised by
142 // Args: vCmd - (R) Command information structure.
143 // Return: bool - True = yes command is recognised, false = command not recognised.
147 CMICmdInterpreter::HasCmdFactoryGotMiCmd(const SMICmdData &vCmd) const
149 return m_rCmdFactory.CmdExist(vCmd.strMiCmd);
152 //++ ------------------------------------------------------------------------------------
153 // Details: Does the command entered match the criteria for a MI command format.
154 // The format to validate against is 'nn-' where there can be 1 to n digits.
155 // I.e. '2-gdb-exit'.
156 // Is the execution token present? The command token is entered into the
157 // command meta data structure whether correct or not for reporting or later
158 // command execution purposes.
160 // Args: vTextLine - (R) Text data to interpret.
161 // Return: bool - True = yes command token present, false = command not recognised.
165 CMICmdInterpreter::MiHasCmdTokenEndingHypthen(const CMIUtilString &vTextLine)
167 // The hythen is mandatory
168 const MIint nPos = vTextLine.find("-", 0);
169 if ((nPos == (MIint)std::string::npos))
172 if (MiHasCmdTokenPresent(vTextLine))
174 const std::string strNum = vTextLine.substr(0, nPos);
175 if (!CMIUtilString(strNum.c_str()).IsNumber())
178 m_miCmdData.strMiCmdToken = strNum.c_str();
181 m_miCmdData.bMIOldStyle = false;
186 //++ ------------------------------------------------------------------------------------
187 // Details: Does the command entered match the criteria for a MI command format.
188 // The format to validate against is 'nnA' where there can be 1 to n digits.
189 // 'A' represents any non numeric token. I.e. '1source .gdbinit'.
190 // Is the execution token present? The command token is entered into the
191 // command meta data structure whether correct or not for reporting or later
192 // command execution purposes.
194 // Args: vTextLine - (R) Text data to interpret.
195 // Return: bool - True = yes command token present, false = command not recognised.
199 CMICmdInterpreter::MiHasCmdTokenEndingAlpha(const CMIUtilString &vTextLine)
201 MIchar cChar = vTextLine[0];
203 while (::isdigit(cChar) != 0)
205 cChar = vTextLine[++i];
207 if (::isalpha(cChar) == 0)
212 const std::string strNum = vTextLine.substr(0, i);
213 m_miCmdData.strMiCmdToken = strNum.c_str();
214 m_miCmdData.bMIOldStyle = true;
219 //++ ------------------------------------------------------------------------------------
220 // Details: Does the command entered match the criteria for a MI command format.
221 // Is the command token present before the hypen?
223 // Args: vTextLine - (R) Text data to interpret.
224 // Return: bool - True = yes command token present, false = token not present.
228 CMICmdInterpreter::MiHasCmdTokenPresent(const CMIUtilString &vTextLine)
230 const MIint nPos = vTextLine.find("-", 0);
234 //++ ------------------------------------------------------------------------------------
235 // Details: Does the command name entered match the criteria for a MI command format.
236 // Is a recogised command present? The command name is entered into the
237 // command meta data structure whether correct or not for reporting or later
238 // command execution purposes. Command options is present are also put into the
239 // command meta data structure.
241 // Args: vTextLine - (R) Command information structure.
242 // Return: bool - True = yes command name present, false = command not recognised.
246 CMICmdInterpreter::MiHasCmd(const CMIUtilString &vTextLine)
249 if (m_miCmdData.bMIOldStyle)
251 char cChar = vTextLine[0];
253 while (::isdigit(cChar) != 0)
255 cChar = vTextLine[++i];
261 nPos = vTextLine.find("-", 0);
264 bool bFoundCmd = false;
265 const MIint nLen = vTextLine.length();
266 const MIint nPos2 = vTextLine.find(" ", nPos);
267 if (nPos2 != (MIint)std::string::npos)
271 const CMIUtilString cmd = CMIUtilString(vTextLine.substr(nPos + 1, nPos2 - nPos - 1).c_str());
275 m_miCmdData.strMiCmd = cmd;
278 m_miCmdData.strMiCmdOption = CMIUtilString(vTextLine.substr(nPos2 + 1, nLen - nPos2 - 1).c_str());
284 const CMIUtilString cmd = CMIUtilString(vTextLine.substr(nPos + 1, nLen - nPos - 1).c_str());
287 m_miCmdData.strMiCmd = cmd;
292 m_miCmdData.strMiCmdAll = vTextLine;
297 //++ ------------------------------------------------------------------------------------
298 // Details: Retrieve the just entered new command from stdin. It contains the command
299 // name, number and any options.
301 // Args: vTextLine - (R) Command information structure.
302 // Return: SMICmdData & - Command meta data information/result/status.
306 CMICmdInterpreter::MiGetCmdData(void) const