aboutsummaryrefslogtreecommitdiff
path: root/tools/lldb-mi/MICmdInterpreter.cpp
blob: dd5b2818705cc90e9887bde45e3169e66ee52115 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//===-- MICmdInterpreter.cpp ------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

//++
// File:		MICmdInterpreter.cpp
//
// Overview:	CMICmdInterpreter implementation.
//
// Environment:	Compilers:	Visual C++ 12.
//							gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
//				Libraries:	See MIReadmetxt. 
//
// Copyright:	None.
//--

// In-house headers:
#include "MICmdInterpreter.h"
#include "MICmdFactory.h"

//++ ------------------------------------------------------------------------------------
// Details:	CMICmdInterpreter constructor.
// Type:	Method.
// Args:	None.
// Return:	None.
// Throws:	None.
//--
CMICmdInterpreter::CMICmdInterpreter( void )
:	m_rCmdFactory( CMICmdFactory::Instance() )
{
}

//++ ------------------------------------------------------------------------------------
// Details:	CMICmdInterpreter destructor.
// Type:	Overridable.
// Args:	None.
// Return:	None.
// Throws:	None.
//--
CMICmdInterpreter::~CMICmdInterpreter( void )
{
	Shutdown();
}

//++ ------------------------------------------------------------------------------------
// Details:	Initialize resources for *this Command Interpreter.
// Type:	Method.
// Args:	None.
// Return:	MIstatus::success - Functional succeeded.
//			MIstatus::failure - Functional failed.
// Throws:	None.
//--
bool CMICmdInterpreter::Initialize( void )
{
	m_clientUsageRefCnt++;

	if( m_bInitialized )
		return MIstatus::success;

	m_bInitialized = true;

	return  MIstatus::success;
}

//++ ------------------------------------------------------------------------------------
// Details:	Release resources for *this Command Interpreter.
// Type:	Method.
// Args:	None.
// Return:	MIstatus::success - Functional succeeded.
//			MIstatus::failure - Functional failed.
// Throws:	None.
//--
bool CMICmdInterpreter::Shutdown( void )
{
	if( --m_clientUsageRefCnt > 0 )
		return MIstatus::success;
	
	if( !m_bInitialized )
		return MIstatus::success;

	m_bInitialized = false;

	return MIstatus::success;
}	

//++ ------------------------------------------------------------------------------------
// Details:	Establish whether the text data is an MI format type command.
// Type:	Method.
// Args:	vTextLine				- (R) Text data to interpret.
//			vwbYesValid				- (W) True = MI type command, false = not recognised.
//			vwbCmdNotInCmdFactor	- (W) True = MI command not found in the command factory, false = recognised.
// Return:	MIstatus::success - Functional succeeded.
//			MIstatus::failure - Functional failed.
// Throws:	None.
//--
bool CMICmdInterpreter::ValidateIsMi( const CMIUtilString & vTextLine, bool & vwbYesValid, bool & vwbCmdNotInCmdFactor, SMICmdData & rwCmdData )
{
	vwbYesValid = false;
	vwbCmdNotInCmdFactor = false;
	rwCmdData.Clear();

	if( vTextLine.empty() )
		return MIstatus::success;

	// 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)

	m_miCmdData.Clear();			
	m_miCmdData.strMiCmd = vTextLine;

	// The following change m_miCmdData as valid parts are indentified
	vwbYesValid = (MiHasCmdTokenEndingHypthen( 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;
}

//++ ------------------------------------------------------------------------------------
// Details:	Establish whether the command name entered on the stdin stream is recognised by 
//			the MI driver.
// Type:	Method.
// Args:	vCmd	- (R) Command information structure.
// Return:	bool  - True = yes command is recognised, false = command not recognised.
// Throws:	None.
//--
bool CMICmdInterpreter::HasCmdFactoryGotMiCmd( const SMICmdData & vCmd ) const
{
	return m_rCmdFactory.CmdExist( vCmd.strMiCmd );
}

//++ ------------------------------------------------------------------------------------
// Details:	Does the command entered match the criteria for a MI command format.
//			The format to validate against is 'nn-' where there can be 1 to n digits.
//			I.e. '2-gdb-exit'.
//			Is the execution token present? The command token is entered into the
//			command meta data structure whether correct or not for reporting or later
//			command execution purposes.
// Type:	Method.
// Args:	vTextLine	- (R) Text data to interpret.
// Return:	bool  - True = yes command token present, false = command not recognised.
// Throws:	None.
//--
bool CMICmdInterpreter::MiHasCmdTokenEndingHypthen( const CMIUtilString & vTextLine )
{
	// The hythen is mandatory
	const MIint nPos = vTextLine.find( "-", 0 );
	if( (nPos == (MIint) std::string::npos) )
		return false;

	if( MiHasCmdTokenPresent( vTextLine ) )
	{
		const std::string strNum = vTextLine.substr( 0, nPos );
		if( !CMIUtilString( strNum.c_str() ).IsNumber() )
			return false;
	
		m_miCmdData.strMiCmdToken = strNum.c_str();
	}

	m_miCmdData.bMIOldStyle = false;

	return true;
}

//++ ------------------------------------------------------------------------------------
// Details:	Does the command entered match the criteria for a MI command format.
//			The format to validate against is 'nnA' where there can be 1 to n digits.
//			'A' represents any non numeric token. I.e. '1source .gdbinit'.
//			Is the execution token present? The command token is entered into the
//			command meta data structure whether correct or not for reporting or later
//			command execution purposes.
// Type:	Method.
// Args:	vTextLine	- (R) Text data to interpret.
// Return:	bool  - True = yes command token present, false = command not recognised.
// Throws:	None.
//--
bool CMICmdInterpreter::MiHasCmdTokenEndingAlpha( const CMIUtilString & vTextLine )
{
	MIchar 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;
}

//++ ------------------------------------------------------------------------------------
// Details:	Does the command entered match the criteria for a MI command format.
//			Is the command token present before the hypen? 
// Type:	Method.
// Args:	vTextLine - (R) Text data to interpret.
// Return:	bool  - True = yes command token present, false = token not present.
// Throws:	None.
//--
bool CMICmdInterpreter::MiHasCmdTokenPresent( const CMIUtilString & vTextLine )
{
	const MIint nPos = vTextLine.find( "-", 0 );
	return (nPos > 0);
}

//++ ------------------------------------------------------------------------------------
// Details:	Does the command name entered match the criteria for a MI command format.
//			Is a recogised 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
//			command meta data structure.
// Type:	Method.
// Args:	vTextLine	- (R) Command information structure.
// Return:	bool  - True = yes command name present, false = command not recognised.
// Throws:	None.
//--
bool CMICmdInterpreter::MiHasCmd( const CMIUtilString & vTextLine )
{
	MIint nPos = 0;
	if( m_miCmdData.bMIOldStyle )
	{
		char cChar = vTextLine[ 0 ]; 
		MIuint i = 0;
		while( ::isdigit( cChar ) != 0 )
		{
			cChar = vTextLine[ ++i ];
		}
		nPos = --i;
	}
	else
	{
		nPos = vTextLine.find( "-", 0 );
	}

	bool bFoundCmd = false;
	const MIint nLen = vTextLine.length();
	const MIint nPos2 = vTextLine.find( " ", nPos );
	if( nPos2 != (MIint) std::string::npos )
	{
		if( nPos2 == nLen )
			return false;
		const CMIUtilString cmd = CMIUtilString( vTextLine.substr( nPos + 1, nPos2 - nPos - 1 ).c_str() );
		if( cmd.empty() )
			return false;
		
		m_miCmdData.strMiCmd = cmd;
		
		if( nPos2 < nLen )
			m_miCmdData.strMiCmdOption = CMIUtilString( vTextLine.substr( nPos2 + 1, nLen - nPos2 - 1 ).c_str() );
		
		bFoundCmd = true;
	}
	else
	{
		const CMIUtilString cmd = CMIUtilString( vTextLine.substr( nPos + 1, nLen - nPos - 1 ).c_str() );
		if( cmd.empty() )
			return false;
		m_miCmdData.strMiCmd = cmd;
		bFoundCmd = true;
	}

	if( bFoundCmd )
		m_miCmdData.strMiCmdAll = vTextLine;

	return bFoundCmd;
}

//++ ------------------------------------------------------------------------------------
// Details:	Retrieve the just entered new command from stdin. It contains the command
//			name, number and any options.
// Type:	Method.
// Args:	vTextLine	- (R) Command information structure.
// Return:	SMICmdData & - Command meta data information/result/status.
// Throws:	None.
//--
const SMICmdData & CMICmdInterpreter::MiGetCmdData( void ) const
{
	return m_miCmdData;
}