]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdArgValString.cpp
Upgrade our copy of clang and llvm to 3.5.1 release. This is a bugfix
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdArgValString.cpp
1 //===-- MICmdArgValString.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:                MICmdArgValString.cpp
12 //
13 // Overview:    CMICmdArgValString 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 "MICmdArgValString.h"
24 #include "MICmdArgContext.h"
25
26 //++ ------------------------------------------------------------------------------------
27 // Details:     CMICmdArgValString constructor.
28 // Type:        Method.
29 // Args:        None.
30 // Return:      None.
31 // Throws:      None.
32 //--
33 CMICmdArgValString::CMICmdArgValString( void )
34 :       m_bHandleQuotedString( false )
35 ,       m_bAcceptNumbers( false )
36 ,       m_bHandleDirPaths( false )
37 ,       m_bHandleAnything( false )
38 {
39 }
40
41 //++ ------------------------------------------------------------------------------------
42 // Details:     CMICmdArgValString constructor.
43 // Type:        Method.
44 // Args:        vbAnything      - (R) True = Parse a string and accept anything, false = do not accept anything. 
45 // Return:      None.
46 // Throws:      None.
47 //--
48 CMICmdArgValString::CMICmdArgValString( const bool vbAnything )
49 :       m_bHandleQuotedString( false )
50 ,       m_bAcceptNumbers( false )
51 ,       m_bHandleDirPaths( false )
52 ,       m_bHandleAnything( vbAnything )
53 {
54 }
55
56 //++ ------------------------------------------------------------------------------------
57 // Details:     CMICmdArgValString constructor.
58 // Type:        Method.
59 // Args:        vbHandleQuotes          - (R) True = Parse a string surrounded by quotes spaces are not delimitors, false = only text up to next delimiting space character.
60 //                      vbAcceptNumbers         - (R) True = Parse a string and accept as a number if number, false = numbers not recognised as string types. 
61 //                      vbHandleDirPaths        - (R) True = Parse a string and accept as a file path if a path, false = file paths are not recognised as string types. 
62 // Return:      None.
63 // Throws:      None.
64 //--
65 CMICmdArgValString::CMICmdArgValString( const bool vbHandleQuotes, const bool vbAcceptNumbers, const bool vbHandleDirPaths )
66 :       m_bHandleQuotedString( vbHandleQuotes )
67 ,       m_bAcceptNumbers( vbAcceptNumbers )
68 ,       m_bHandleDirPaths( vbHandleDirPaths )
69 ,       m_bHandleAnything( false )
70 {
71 }
72
73 //++ ------------------------------------------------------------------------------------
74 // Details:     CMICmdArgValString constructor.
75 // Type:        Method.
76 // Args:        vrArgName               - (R) Argument's name to search by.
77 //                      vbMandatory             - (R) True = Yes must be present, false = optional argument.
78 //                      vbHandleByCmd   - (R) True = Command processes *this option, false = not handled.
79 //                      vbHandleQuotes  - (R) True = Parse a string surrounded by quotes spaces are not delimitors, false = only text up to next delimiting space character. (Dflt = false)
80 //                      vbAcceptNumbers - (R) True = Parse a string and accept as a number if number, false = numbers not recognised as string types. (Dflt = false)
81 // Return:      None.
82 // Throws:      None.
83 //--
84 CMICmdArgValString::CMICmdArgValString( const CMIUtilString & vrArgName, const bool vbMandatory, const bool vbHandleByCmd, const bool vbHandleQuotes /* = false */, const bool vbAcceptNumbers  /* = false */ )
85 :       CMICmdArgValBaseTemplate( vrArgName, vbMandatory, vbHandleByCmd )
86 ,       m_bHandleQuotedString( vbHandleQuotes )
87 ,       m_bAcceptNumbers( vbAcceptNumbers )
88 ,       m_bHandleDirPaths( false )
89 ,       m_bHandleAnything( false )
90 {
91 }
92
93 //++ ------------------------------------------------------------------------------------
94 // Details:     CMICmdArgValString destructor.
95 // Type:        Overridden.
96 // Args:        None.
97 // Return:      None.
98 // Throws:      None.
99 //--
100 CMICmdArgValString::~CMICmdArgValString( void )
101 {
102 }
103
104 //++ ------------------------------------------------------------------------------------
105 // Details:     Parse the command's argument options string and try to extract the value *this
106 //                      argument is looking for.
107 // Type:        Overridden.
108 // Args:        vrwArgContext   - (RW) The command's argument options string.
109 // Return:      MIstatus::success - Functional succeeded.
110 //                      MIstatus::failure - Functional failed.
111 // Throws:      None.
112 //--
113 bool CMICmdArgValString::Validate( CMICmdArgContext & vrwArgContext )
114 {
115         if( vrwArgContext.IsEmpty() )
116                 return MIstatus::success;
117
118         if( m_bHandleQuotedString )
119                 return (ValidateQuotedText( vrwArgContext ) || ValidateQuotedTextEmbedded( vrwArgContext ) );
120
121         return ValidateSingleText( vrwArgContext );
122 }
123
124 //++ ------------------------------------------------------------------------------------
125 // Details:     Parse the command's argument options string and try to extract only the next
126 //                      word delimited by the next space.
127 // Type:        Method.
128 // Args:        vrwArgContext   - (RW) The command's argument options string.
129 // Return:      MIstatus::success - Functional succeeded.
130 //                      MIstatus::failure - Functional failed.
131 // Throws:      None.
132 //--
133 bool CMICmdArgValString::ValidateSingleText( CMICmdArgContext & vrwArgContext )
134 {
135         if( vrwArgContext.GetNumberArgsPresent() == 1 )
136         {
137                 const CMIUtilString & rArg( vrwArgContext.GetArgsLeftToParse() ); 
138                 if( IsStringArg( rArg ) )
139                 {
140                         m_bFound = true;
141                         m_bValid = true;
142                         m_argValue = rArg;
143                         vrwArgContext.RemoveArg( rArg );
144                         return MIstatus::success;
145                 }
146                 else
147                         return MIstatus::failure;
148         }
149         
150         // More than one option...
151         const CMIUtilString::VecString_t vecOptions( vrwArgContext.GetArgs() );
152         CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
153         while( it != vecOptions.end() )
154         {
155                 const CMIUtilString & rArg( *it ); 
156                 if( IsStringArg( rArg )  )
157                 {
158                         m_bFound = true;
159                                 
160                         if( vrwArgContext.RemoveArg( rArg ) )
161                         {
162                                 m_bValid = true;
163                                 m_argValue = rArg;
164                                 return MIstatus::success;
165                         }
166                         else
167                                 return MIstatus::failure;
168                 }
169                 
170                 // Next
171                 ++it;
172         }
173
174         return MIstatus::failure;
175 }
176
177 //++ ------------------------------------------------------------------------------------
178 // Details:     Parse the command's argument options string and try to extract all the words
179 //                      between quotes then delimited by the next space. Can fall through to 
180 //                      ValidateSingleText() or ValidateQuotedQuotedTextEmbedded().
181 // Type:        Method.
182 // Args:        vrwArgContext   - (RW) The command's argument options string.
183 // Return:      MIstatus::success - Functional succeeded.
184 //                      MIstatus::failure - Functional failed.
185 // Throws:      None.
186 //--
187 bool CMICmdArgValString::ValidateQuotedText( CMICmdArgContext & vrwArgContext )
188 {
189         // CODETAG_QUOTEDTEXT_SIMILAR_CODE
190         CMIUtilString strOptions = vrwArgContext.GetArgsLeftToParse();
191         const MIchar cQuote = '"';
192
193         // Look for first quote of two
194         MIint nPos = strOptions.find( cQuote );
195         if( nPos == (MIint) std::string::npos )
196                 return ValidateSingleText( vrwArgContext );
197
198         // Is one and only quote at end of the string
199         const MIint nLen = strOptions.length();
200         if( nPos == (MIint)(nLen - 1) )
201                 return MIstatus::failure;
202
203         // Quote must be the first character in the string or be preceeded by a space
204         if( (nPos > 0) && (strOptions[ nPos - 1 ] != ' ') )
205                         return MIstatus::failure;
206                 
207         // Need to find the other quote
208         const MIint nPos2 = strOptions.rfind( cQuote );
209         if( nPos2 == (MIint) std::string::npos )
210                 return MIstatus::failure;
211
212         // Is there quotes surrounding string formatting embedded quotes
213         if( IsStringArgQuotedQuotedTextEmbedded( strOptions ) )
214                 return ValidateQuotedQuotedTextEmbedded( vrwArgContext );
215
216         // Make sure not same back quote, need two quotes
217         if( nPos == nPos2 )
218                 return MIstatus::failure;
219
220         // Extract quoted text
221         const CMIUtilString strQuotedTxt = strOptions.substr( nPos, nPos2 - nPos + 1 ).c_str();
222         if( vrwArgContext.RemoveArg( strQuotedTxt ) )
223         {
224                 m_bFound = true;
225                 m_bValid = true;
226                 m_argValue = strOptions.substr( nPos + 1, nPos2 - nPos - 1 ).c_str();   
227                 return MIstatus::success;
228         }
229
230         return MIstatus::failure;
231 }
232
233 //++ ------------------------------------------------------------------------------------
234 // Details:     Parse the command's argument options string and try to extract all the words
235 //                      between quotes then delimited by the next space. If there any string format
236 //                      characters '\\' used to embed quotes these are ignored i.e. "\\\"%5d\\\""
237 //                      becomes "%5d". Can fall through to ValidateQuotedText().
238 // Type:        Method.
239 // Args:        vrwArgContext   - (RW) The command's argument options string.
240 // Return:      MIstatus::success - Functional succeeded.
241 //                      MIstatus::failure - Functional failed.
242 // Throws:      None.
243 //--
244 bool CMICmdArgValString::ValidateQuotedTextEmbedded( CMICmdArgContext & vrwArgContext )
245 {
246         // CODETAG_QUOTEDTEXT_SIMILAR_CODE
247         CMIUtilString strOptions = vrwArgContext.GetArgsLeftToParse();
248         const MIchar cBckSlash = '\\'; 
249         const MIint nPos = strOptions.find( cBckSlash );
250         if( nPos == (MIint) std::string::npos )
251                 return ValidateQuotedText( vrwArgContext );
252
253         // Back slash must be the first character in the string or be preceeded by a space
254         // or '\\'
255         const MIchar cSpace = ' ';
256         if( (nPos > 0) && (strOptions[ nPos - 1 ] != cSpace) )
257                 return MIstatus::failure;
258
259         // Need to find the other back slash
260         const MIint nPos2 = strOptions.rfind( cBckSlash );
261         if( nPos2 == (MIint) std::string::npos )
262                 return MIstatus::failure;
263
264         // Make sure not same back slash, need two slashs
265         if( nPos == nPos2 )
266                 return MIstatus::failure;
267
268         // Look for the two quotes
269         const MIint nLen = strOptions.length();
270         const MIchar cQuote = '"';
271         const MIint nPosQuote1 = nPos + 1;
272         const MIint nPosQuote2 = (nPos2 < nLen) ? nPos2 + 1 : nPos2;
273         if( (nPosQuote1 != nPosQuote2) && 
274                 (strOptions[ nPosQuote1 ] != cQuote) && (strOptions[ nPosQuote2 ] != cQuote) )
275                 return MIstatus::failure;
276
277         // Extract quoted text
278         const CMIUtilString strQuotedTxt = strOptions.substr( nPos, nPosQuote2 - nPos + 1 ).c_str();
279         if( vrwArgContext.RemoveArg( strQuotedTxt ) )
280         {
281                 m_bFound = true;
282                 m_bValid = true;
283                 m_argValue = strQuotedTxt;      
284                 return MIstatus::success;
285         }
286
287         return MIstatus::failure;
288 }
289
290 //++ ------------------------------------------------------------------------------------
291 // Details:     Parse the command's argument options string and try to extract all the words
292 //                      between quotes then delimited by the next space. If there any string format
293 //                      characters '\\' used to embed quotes these are ignored i.e. "\\\"%5d\\\""
294 //                      becomes "%5d".
295 // Type:        Method.
296 // Args:        vrwArgContext   - (RW) The command's argument options string.
297 // Return:      MIstatus::success - Functional succeeded.
298 //                      MIstatus::failure - Functional failed.
299 // Throws:      None.
300 //--
301 bool CMICmdArgValString::ValidateQuotedQuotedTextEmbedded( CMICmdArgContext & vrwArgContext )
302 {
303         // CODETAG_QUOTEDTEXT_SIMILAR_CODE
304         CMIUtilString strOptions = vrwArgContext.GetArgsLeftToParse();
305         const MIint nPos = strOptions.find( "\"\\\"" );
306         if( nPos == (MIint) std::string::npos )
307                 return MIstatus::failure;
308
309         const MIint nPos2 = strOptions.rfind( "\\\"\"" );
310         if( nPos2 == (MIint) std::string::npos )
311                 return MIstatus::failure;
312
313         const MIint nLen = strOptions.length();
314         if( (nLen > 5) && ((nPos + 2) == (nPos2 - 2)) )
315                 return MIstatus::failure;
316
317         // Quote must be the first character in the string or be preceeded by a space
318         // or '\\'
319         const MIchar cSpace = ' ';
320         if( (nPos > 0) && (strOptions[ nPos - 1 ] != cSpace) )
321                 return MIstatus::failure;
322
323         // Extract quoted text
324         const CMIUtilString strQuotedTxt = strOptions.substr( nPos, nPos2 - nPos + 3 ).c_str();
325         if( vrwArgContext.RemoveArg( strQuotedTxt ) )
326         {
327                 m_bFound = true;
328                 m_bValid = true;
329                 m_argValue = strQuotedTxt;      
330                 return MIstatus::success;
331         }
332
333         return MIstatus::failure;
334 }
335
336 //++ ------------------------------------------------------------------------------------
337 // Details:     Examine the string and determine if it is a valid string type argument.
338 // Type:        Method.
339 // Args:        vrTxt   - (R) Some text.
340 // Return:      bool -  True = yes valid arg, false = no.
341 // Throws:      None.
342 //--
343 bool CMICmdArgValString::IsStringArg( const CMIUtilString & vrTxt ) const
344 {
345         if( m_bHandleQuotedString )
346                 return (IsStringArgQuotedText( vrTxt ) || 
347                                 IsStringArgQuotedTextEmbedded( vrTxt ) || 
348                                 IsStringArgQuotedQuotedTextEmbedded( vrTxt ) ||
349                                 IsStringArgSingleText( vrTxt ) ); // Still test for this as could just be one word still
350         
351         return IsStringArgSingleText( vrTxt );
352 }
353
354 //++ ------------------------------------------------------------------------------------
355 // Details:     Examine the string and determine if it is a valid string type argument or 
356 //                      option value. If the string looks like a long option, short option, a thread
357 //                      group ID or just a number it is rejected as a string type value. There is an
358 //                      option to allow the string to accept a number as a string type.
359 // Type:        Method.
360 // Args:        vrTxt   - (R) Some text.
361 // Return:      bool -  True = yes valid argument value, false = something else.
362 // Throws:      None.
363 //--
364 bool CMICmdArgValString::IsStringArgSingleText( const CMIUtilString & vrTxt ) const
365 {
366         // Accept anything as string word
367         if( m_bHandleAnything )
368                 return true;
369
370         if( !m_bHandleDirPaths )
371         {
372                 // Look for directory file paths, if found reject
373                 const bool bHavePosSlash = (vrTxt.find_first_of( "/" ) != std::string::npos);
374                 const bool bHaveBckSlash = (vrTxt.find_first_of( "\\" ) != std::string::npos);
375                 if( bHavePosSlash || bHaveBckSlash )
376                         return false;
377         }
378
379         // Look for --someLongOption, if found reject
380         if( 0 == vrTxt.find( "--" ) )
381                 return false;
382         
383         // Look for -f type short options, if found reject
384         if( (0 == vrTxt.find( "-" )) && (vrTxt.length() == 2) )
385                 return false;
386                 
387         // Look for thread group i1 i2 i3...., if found reject
388         if( (vrTxt.find( "i" ) == 0) && ::isdigit( vrTxt[ 1 ]) )
389                 return false;
390         
391         // Look for numbers, if found reject
392         if( !m_bAcceptNumbers && vrTxt.IsNumber() )
393                 return false;
394
395         return true;
396 }
397
398 //++ ------------------------------------------------------------------------------------
399 // Details:     Examine the string and determine if it is a valid string type argument.
400 //                      Take into account quotes surrounding the text. Note this function falls 
401 //                      through to IsStringArgSingleText() should the criteria match fail.
402 // Type:        Method.
403 // Args:        vrTxt   - (R) Some text.
404 // Return:      bool -  True = yes valid arg, false = no.
405 // Throws:      None.
406 //--
407 bool CMICmdArgValString::IsStringArgQuotedText( const CMIUtilString & vrTxt ) const
408 {
409         // CODETAG_QUOTEDTEXT_SIMILAR_CODE
410         const MIchar cQuote = '"';
411         const MIint nPos = vrTxt.find( cQuote );
412         if( nPos == (MIint) std::string::npos )
413                 return false;
414
415         // Is one and only quote at end of the string
416         if( nPos == (MIint)(vrTxt.length() - 1) )
417                 return false;
418
419         // Quote must be the first character in the string or be preceeded by a space
420         // Also check for embedded string formating quote
421         const MIchar cBckSlash = '\\'; 
422         const MIchar cSpace = ' ';
423         if( (nPos > 1) && (vrTxt[ nPos - 1 ] == cBckSlash) && (vrTxt[ nPos - 2 ] != cSpace) )
424         {
425                 return false;
426         }
427         if( (nPos > 0) && (vrTxt[ nPos - 1 ] != cSpace) )
428                 return false;
429         
430         // Need to find the other quote
431         const MIint nPos2 = vrTxt.rfind( cQuote );
432         if( nPos2 == (MIint) std::string::npos )
433                 return false;
434
435         // Make sure not same quote, need two quotes
436         if( nPos == nPos2 )
437                 return MIstatus::failure;
438
439         return true;
440 }
441
442 //++ ------------------------------------------------------------------------------------
443 // Details:     Examine the string and determine if it is a valid string type argument.
444 //                      Take into account quotes surrounding the text. Take into account string format
445 //                      embedded quotes surrounding the text i.e. "\\\"%5d\\\"". Note this function falls 
446 //                      through to IsStringArgQuotedText() should the criteria match fail.
447 // Type:        Method.
448 // Args:        vrTxt   - (R) Some text.
449 // Return:      bool -  True = yes valid arg, false = no.
450 // Throws:      None.
451 //--
452 bool CMICmdArgValString::IsStringArgQuotedTextEmbedded( const CMIUtilString & vrTxt ) const
453 {
454         // CODETAG_QUOTEDTEXT_SIMILAR_CODE
455         const MIchar cBckSlash = '\\'; 
456         const MIint nPos = vrTxt.find( cBckSlash );
457         if( nPos == (MIint) std::string::npos )
458                 return false;
459
460         // Slash must be the first character in the string or be preceeded by a space
461         const MIchar cSpace = ' ';
462         if( (nPos > 0) && (vrTxt[ nPos - 1 ] != cSpace) )
463                 return false;
464
465         // Need to find the other matching slash
466         const MIint nPos2 = vrTxt.rfind( cBckSlash );
467         if( nPos2 == (MIint) std::string::npos )
468                 return false;
469
470         // Make sure not same back slash, need two slashs
471         if( nPos == nPos2 )
472                 return MIstatus::failure;
473
474         return false;
475 }
476
477 //++ ------------------------------------------------------------------------------------
478 // Details:     Examine the string and determine if it is a valid string type argument.
479 //                      Take into account quotes surrounding the text. Take into account string format
480 //                      embedded quotes surrounding the text i.e. "\\\"%5d\\\"". Note this function falls 
481 //                      through to IsStringArgQuotedTextEmbedded() should the criteria match fail.
482 // Type:        Method.
483 // Args:        vrTxt   - (R) Some text.
484 // Return:      bool -  True = yes valid arg, false = no.
485 // Throws:      None.
486 //--
487 bool CMICmdArgValString::IsStringArgQuotedQuotedTextEmbedded( const CMIUtilString & vrTxt ) const
488 {
489         const MIint nPos = vrTxt.find( "\"\\\"" );
490         if( nPos == (MIint) std::string::npos )
491                 return false;
492
493         const MIint nPos2 = vrTxt.rfind( "\\\"\"" );
494         if( nPos2 == (MIint) std::string::npos )
495                 return false;
496
497         const MIint nLen = vrTxt.length();
498         if( (nLen > 5) && ((nPos + 2) == (nPos2 - 2)) )
499                 return false;
500         
501         return true;
502 }