]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdInterpreter.cpp
Merge OpenSSL 1.0.1k.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdInterpreter.cpp
1 //===-- MICmdInterpreter.cpp ------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 //++
11 // File:                MICmdInterpreter.cpp
12 //
13 // Overview:    CMICmdInterpreter implementation.
14 //
15 // Environment: Compilers:      Visual C++ 12.
16 //                                                      gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17 //                              Libraries:      See MIReadmetxt. 
18 //
19 // Copyright:   None.
20 //--
21
22 // In-house headers:
23 #include "MICmdInterpreter.h"
24 #include "MICmdFactory.h"
25
26 //++ ------------------------------------------------------------------------------------
27 // Details:     CMICmdInterpreter constructor.
28 // Type:        Method.
29 // Args:        None.
30 // Return:      None.
31 // Throws:      None.
32 //--
33 CMICmdInterpreter::CMICmdInterpreter( void )
34 :       m_rCmdFactory( CMICmdFactory::Instance() )
35 {
36 }
37
38 //++ ------------------------------------------------------------------------------------
39 // Details:     CMICmdInterpreter destructor.
40 // Type:        Overridable.
41 // Args:        None.
42 // Return:      None.
43 // Throws:      None.
44 //--
45 CMICmdInterpreter::~CMICmdInterpreter( void )
46 {
47         Shutdown();
48 }
49
50 //++ ------------------------------------------------------------------------------------
51 // Details:     Initialize resources for *this Command Interpreter.
52 // Type:        Method.
53 // Args:        None.
54 // Return:      MIstatus::success - Functional succeeded.
55 //                      MIstatus::failure - Functional failed.
56 // Throws:      None.
57 //--
58 bool CMICmdInterpreter::Initialize( void )
59 {
60         m_clientUsageRefCnt++;
61
62         if( m_bInitialized )
63                 return MIstatus::success;
64
65         m_bInitialized = true;
66
67         return  MIstatus::success;
68 }
69
70 //++ ------------------------------------------------------------------------------------
71 // Details:     Release resources for *this Command Interpreter.
72 // Type:        Method.
73 // Args:        None.
74 // Return:      MIstatus::success - Functional succeeded.
75 //                      MIstatus::failure - Functional failed.
76 // Throws:      None.
77 //--
78 bool CMICmdInterpreter::Shutdown( void )
79 {
80         if( --m_clientUsageRefCnt > 0 )
81                 return MIstatus::success;
82         
83         if( !m_bInitialized )
84                 return MIstatus::success;
85
86         m_bInitialized = false;
87
88         return MIstatus::success;
89 }       
90
91 //++ ------------------------------------------------------------------------------------
92 // Details:     Establish whether the text data is an MI format type command.
93 // Type:        Method.
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.
99 // Throws:      None.
100 //--
101 bool CMICmdInterpreter::ValidateIsMi( const CMIUtilString & vTextLine, bool & vwbYesValid, bool & vwbCmdNotInCmdFactor, SMICmdData & rwCmdData )
102 {
103         vwbYesValid = false;
104         vwbCmdNotInCmdFactor = false;
105         rwCmdData.Clear();
106
107         if( vTextLine.empty() )
108                 return MIstatus::success;
109
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)
113
114         m_miCmdData.Clear();                    
115         m_miCmdData.strMiCmd = vTextLine;
116
117         // The following change m_miCmdData as valid parts are indentified
118         vwbYesValid = (MiHasCmdTokenEndingHypthen( vTextLine ) || MiHasCmdTokenEndingAlpha( vTextLine ));
119         vwbYesValid = vwbYesValid && MiHasCmd( vTextLine );
120         if( vwbYesValid )
121         {
122                 vwbCmdNotInCmdFactor = !HasCmdFactoryGotMiCmd( MiGetCmdData() );
123                 vwbYesValid = !vwbCmdNotInCmdFactor;
124         }
125
126         // Update command's meta data valid state
127         m_miCmdData.bCmdValid = vwbYesValid;
128
129         // Ok to return new updated command information
130         rwCmdData = MiGetCmdData();
131
132         return MIstatus::success;
133 }
134
135 //++ ------------------------------------------------------------------------------------
136 // Details:     Establish whether the command name entered on the stdin stream is recognised by 
137 //                      the MI driver.
138 // Type:        Method.
139 // Args:        vCmd    - (R) Command information structure.
140 // Return:      bool  - True = yes command is recognised, false = command not recognised.
141 // Throws:      None.
142 //--
143 bool CMICmdInterpreter::HasCmdFactoryGotMiCmd( const SMICmdData & vCmd ) const
144 {
145         return m_rCmdFactory.CmdExist( vCmd.strMiCmd );
146 }
147
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.
155 // Type:        Method.
156 // Args:        vTextLine       - (R) Text data to interpret.
157 // Return:      bool  - True = yes command token present, false = command not recognised.
158 // Throws:      None.
159 //--
160 bool CMICmdInterpreter::MiHasCmdTokenEndingHypthen( const CMIUtilString & vTextLine )
161 {
162         // The hythen is mandatory
163         const MIint nPos = vTextLine.find( "-", 0 );
164         if( (nPos == (MIint) std::string::npos) )
165                 return false;
166
167         if( MiHasCmdTokenPresent( vTextLine ) )
168         {
169                 const std::string strNum = vTextLine.substr( 0, nPos );
170                 if( !CMIUtilString( strNum.c_str() ).IsNumber() )
171                         return false;
172         
173                 m_miCmdData.strMiCmdToken = strNum.c_str();
174         }
175
176         m_miCmdData.bMIOldStyle = false;
177
178         return true;
179 }
180
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.
188 // Type:        Method.
189 // Args:        vTextLine       - (R) Text data to interpret.
190 // Return:      bool  - True = yes command token present, false = command not recognised.
191 // Throws:      None.
192 //--
193 bool CMICmdInterpreter::MiHasCmdTokenEndingAlpha( const CMIUtilString & vTextLine )
194 {
195         MIchar cChar = vTextLine[ 0 ]; 
196         MIuint i = 0;
197         while( ::isdigit( cChar ) != 0 )
198         {
199                 cChar = vTextLine[ ++i ];
200         }
201         if( ::isalpha( cChar ) == 0 )
202                 return false;
203         if( i == 0 )
204                 return false;
205         
206         const std::string strNum = vTextLine.substr( 0, i );
207         m_miCmdData.strMiCmdToken = strNum.c_str();
208         m_miCmdData.bMIOldStyle = true;
209
210         return true;
211 }
212
213 //++ ------------------------------------------------------------------------------------
214 // Details:     Does the command entered match the criteria for a MI command format.
215 //                      Is the command token present before the hypen? 
216 // Type:        Method.
217 // Args:        vTextLine - (R) Text data to interpret.
218 // Return:      bool  - True = yes command token present, false = token not present.
219 // Throws:      None.
220 //--
221 bool CMICmdInterpreter::MiHasCmdTokenPresent( const CMIUtilString & vTextLine )
222 {
223         const MIint nPos = vTextLine.find( "-", 0 );
224         return (nPos > 0);
225 }
226
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.
233 // Type:        Method.
234 // Args:        vTextLine       - (R) Command information structure.
235 // Return:      bool  - True = yes command name present, false = command not recognised.
236 // Throws:      None.
237 //--
238 bool CMICmdInterpreter::MiHasCmd( const CMIUtilString & vTextLine )
239 {
240         MIint nPos = 0;
241         if( m_miCmdData.bMIOldStyle )
242         {
243                 char cChar = vTextLine[ 0 ]; 
244                 MIuint i = 0;
245                 while( ::isdigit( cChar ) != 0 )
246                 {
247                         cChar = vTextLine[ ++i ];
248                 }
249                 nPos = --i;
250         }
251         else
252         {
253                 nPos = vTextLine.find( "-", 0 );
254         }
255
256         bool bFoundCmd = false;
257         const MIint nLen = vTextLine.length();
258         const MIint nPos2 = vTextLine.find( " ", nPos );
259         if( nPos2 != (MIint) std::string::npos )
260         {
261                 if( nPos2 == nLen )
262                         return false;
263                 const CMIUtilString cmd = CMIUtilString( vTextLine.substr( nPos + 1, nPos2 - nPos - 1 ).c_str() );
264                 if( cmd.empty() )
265                         return false;
266                 
267                 m_miCmdData.strMiCmd = cmd;
268                 
269                 if( nPos2 < nLen )
270                         m_miCmdData.strMiCmdOption = CMIUtilString( vTextLine.substr( nPos2 + 1, nLen - nPos2 - 1 ).c_str() );
271                 
272                 bFoundCmd = true;
273         }
274         else
275         {
276                 const CMIUtilString cmd = CMIUtilString( vTextLine.substr( nPos + 1, nLen - nPos - 1 ).c_str() );
277                 if( cmd.empty() )
278                         return false;
279                 m_miCmdData.strMiCmd = cmd;
280                 bFoundCmd = true;
281         }
282
283         if( bFoundCmd )
284                 m_miCmdData.strMiCmdAll = vTextLine;
285
286         return bFoundCmd;
287 }
288
289 //++ ------------------------------------------------------------------------------------
290 // Details:     Retrieve the just entered new command from stdin. It contains the command
291 //                      name, number and any options.
292 // Type:        Method.
293 // Args:        vTextLine       - (R) Command information structure.
294 // Return:      SMICmdData & - Command meta data information/result/status.
295 // Throws:      None.
296 //--
297 const SMICmdData & CMICmdInterpreter::MiGetCmdData( void ) const
298 {
299         return m_miCmdData;
300 }
301