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.
58 bool CMICmdInterpreter::Initialize( void )
60 m_clientUsageRefCnt++;
63 return MIstatus::success;
65 m_bInitialized = true;
67 return MIstatus::success;
70 //++ ------------------------------------------------------------------------------------
71 // Details: Release resources for *this Command Interpreter.
74 // Return: MIstatus::success - Functional succeeded.
75 // MIstatus::failure - Functional failed.
78 bool CMICmdInterpreter::Shutdown( void )
80 if( --m_clientUsageRefCnt > 0 )
81 return MIstatus::success;
84 return MIstatus::success;
86 m_bInitialized = false;
88 return MIstatus::success;
91 //++ ------------------------------------------------------------------------------------
92 // Details: Establish whether the text data is an MI format type command.
94 // Args: vTextLine - (R) Text data to interpret.
95 // vwbYesValid - (W) True = MI type command, false = not recognised.
96 // vwbCmdNotInCmdFactor - (W) True = MI command not found in the command factory, false = recognised.
97 // Return: MIstatus::success - Functional succeeded.
98 // MIstatus::failure - Functional failed.
101 bool CMICmdInterpreter::ValidateIsMi( const CMIUtilString & vTextLine, bool & vwbYesValid, bool & vwbCmdNotInCmdFactor, SMICmdData & rwCmdData )
104 vwbCmdNotInCmdFactor = false;
107 if( vTextLine.empty() )
108 return MIstatus::success;
110 // MI format is [cmd #]-[command name]<space>[command arg(s)]
111 // i.e. 1-file-exec-and-symbols --thread-group i1 DEVICE_EXECUTABLE
112 // 5-data-evaluate-expression --thread 1 --frame 0 *(argv)
115 m_miCmdData.strMiCmd = vTextLine;
117 // The following change m_miCmdData as valid parts are indentified
118 vwbYesValid = (MiHasCmdTokenEndingHypthen( vTextLine ) || MiHasCmdTokenEndingAlpha( vTextLine ));
119 vwbYesValid = vwbYesValid && MiHasCmd( vTextLine );
122 vwbCmdNotInCmdFactor = !HasCmdFactoryGotMiCmd( MiGetCmdData() );
123 vwbYesValid = !vwbCmdNotInCmdFactor;
126 // Update command's meta data valid state
127 m_miCmdData.bCmdValid = vwbYesValid;
129 // Ok to return new updated command information
130 rwCmdData = MiGetCmdData();
132 return MIstatus::success;
135 //++ ------------------------------------------------------------------------------------
136 // Details: Establish whether the command name entered on the stdin stream is recognised by
139 // Args: vCmd - (R) Command information structure.
140 // Return: bool - True = yes command is recognised, false = command not recognised.
143 bool CMICmdInterpreter::HasCmdFactoryGotMiCmd( const SMICmdData & vCmd ) const
145 return m_rCmdFactory.CmdExist( vCmd.strMiCmd );
148 //++ ------------------------------------------------------------------------------------
149 // Details: Does the command entered match the criteria for a MI command format.
150 // The format to validate against is 'nn-' where there can be 1 to n digits.
151 // I.e. '2-gdb-exit'.
152 // Is the execution token present? The command token is entered into the
153 // command meta data structure whether correct or not for reporting or later
154 // command execution purposes.
156 // Args: vTextLine - (R) Text data to interpret.
157 // Return: bool - True = yes command token present, false = command not recognised.
160 bool CMICmdInterpreter::MiHasCmdTokenEndingHypthen( const CMIUtilString & vTextLine )
162 // The hythen is mandatory
163 const MIint nPos = vTextLine.find( "-", 0 );
164 if( (nPos == (MIint) std::string::npos) )
167 if( MiHasCmdTokenPresent( vTextLine ) )
169 const std::string strNum = vTextLine.substr( 0, nPos );
170 if( !CMIUtilString( strNum.c_str() ).IsNumber() )
173 m_miCmdData.strMiCmdToken = strNum.c_str();
176 m_miCmdData.bMIOldStyle = false;
181 //++ ------------------------------------------------------------------------------------
182 // Details: Does the command entered match the criteria for a MI command format.
183 // The format to validate against is 'nnA' where there can be 1 to n digits.
184 // 'A' represents any non numeric token. I.e. '1source .gdbinit'.
185 // Is the execution token present? The command token is entered into the
186 // command meta data structure whether correct or not for reporting or later
187 // command execution purposes.
189 // Args: vTextLine - (R) Text data to interpret.
190 // Return: bool - True = yes command token present, false = command not recognised.
193 bool CMICmdInterpreter::MiHasCmdTokenEndingAlpha( const CMIUtilString & vTextLine )
195 MIchar cChar = vTextLine[ 0 ];
197 while( ::isdigit( cChar ) != 0 )
199 cChar = vTextLine[ ++i ];
201 if( ::isalpha( cChar ) == 0 )
206 const std::string strNum = vTextLine.substr( 0, i );
207 m_miCmdData.strMiCmdToken = strNum.c_str();
208 m_miCmdData.bMIOldStyle = true;
213 //++ ------------------------------------------------------------------------------------
214 // Details: Does the command entered match the criteria for a MI command format.
215 // Is the command token present before the hypen?
217 // Args: vTextLine - (R) Text data to interpret.
218 // Return: bool - True = yes command token present, false = token not present.
221 bool CMICmdInterpreter::MiHasCmdTokenPresent( const CMIUtilString & vTextLine )
223 const MIint nPos = vTextLine.find( "-", 0 );
227 //++ ------------------------------------------------------------------------------------
228 // Details: Does the command name entered match the criteria for a MI command format.
229 // Is a recogised command present? The command name is entered into the
230 // command meta data structure whether correct or not for reporting or later
231 // command execution purposes. Command options is present are also put into the
232 // command meta data structure.
234 // Args: vTextLine - (R) Command information structure.
235 // Return: bool - True = yes command name present, false = command not recognised.
238 bool CMICmdInterpreter::MiHasCmd( const CMIUtilString & vTextLine )
241 if( m_miCmdData.bMIOldStyle )
243 char cChar = vTextLine[ 0 ];
245 while( ::isdigit( cChar ) != 0 )
247 cChar = vTextLine[ ++i ];
253 nPos = vTextLine.find( "-", 0 );
256 bool bFoundCmd = false;
257 const MIint nLen = vTextLine.length();
258 const MIint nPos2 = vTextLine.find( " ", nPos );
259 if( nPos2 != (MIint) std::string::npos )
263 const CMIUtilString cmd = CMIUtilString( vTextLine.substr( nPos + 1, nPos2 - nPos - 1 ).c_str() );
267 m_miCmdData.strMiCmd = cmd;
270 m_miCmdData.strMiCmdOption = CMIUtilString( vTextLine.substr( nPos2 + 1, nLen - nPos2 - 1 ).c_str() );
276 const CMIUtilString cmd = CMIUtilString( vTextLine.substr( nPos + 1, nLen - nPos - 1 ).c_str() );
279 m_miCmdData.strMiCmd = cmd;
284 m_miCmdData.strMiCmdAll = vTextLine;
289 //++ ------------------------------------------------------------------------------------
290 // Details: Retrieve the just entered new command from stdin. It contains the command
291 // name, number and any options.
293 // Args: vTextLine - (R) Command information structure.
294 // Return: SMICmdData & - Command meta data information/result/status.
297 const SMICmdData & CMICmdInterpreter::MiGetCmdData( void ) const