]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdArgValOptionLong.cpp
Merge ^/head r275685 through r275714.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdArgValOptionLong.cpp
1 //===-- MICmdArgValOptionLong.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:                MICmdArgValOptionLong.cpp
12 //
13 // Overview:    CMICmdArgValOptionLong 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 "MICmdArgValOptionLong.h"
24 #include "MICmdArgContext.h"
25
26 //++ ------------------------------------------------------------------------------------
27 // Details:     CMICmdArgValOptionLong constructor.
28 // Type:        Method.
29 // Args:        None.
30 // Return:      None.
31 // Throws:      None.
32 //--
33 CMICmdArgValOptionLong::CMICmdArgValOptionLong( void )
34 :       m_nExpectingNOptions( 0 )
35 ,       m_eExpectingOptionType( eArgValType_invalid )
36 {
37 }
38
39 //++ ------------------------------------------------------------------------------------
40 // Details:     CMICmdArgValOptionLong constructor.
41 // Type:        Method.
42 // Args:        vrArgName               - (R) Argument's name to search by.
43 //                      vbMandatory             - (R) True = Yes must be present, false = optional argument.
44 //                      vbHandleByCmd   - (R) True = Command processes *this option, false = not handled.
45 // Return:      None.
46 // Throws:      None.
47 //--
48 CMICmdArgValOptionLong::CMICmdArgValOptionLong( const CMIUtilString & vrArgName, const bool vbMandatory, const bool vbHandleByCmd )
49 :       CMICmdArgValListBase( vrArgName, vbMandatory, vbHandleByCmd )
50 ,       m_nExpectingNOptions( 0 )
51 ,       m_eExpectingOptionType( eArgValType_invalid )
52 {
53 }
54
55 //++ ------------------------------------------------------------------------------------
56 // Details:     CMICmdArgValOptionLong constructor.
57 // Type:        Method.
58 // Args:        vrArgName                       - (R) Argument's name to search by.
59 //                      vbMandatory                     - (R) True = Yes must be present, false = optional argument.
60 //                      vbHandleByCmd           - (R) True = Command processes *this option, false = not handled.
61 //                      veType                          - (R) The type of argument to look for and create argument object of a certain type.    
62 //                      vnExpectingNOptions     - (R) The number of options expected to read following *this argument.
63 // Return:      None.
64 // Throws:      None.
65 //--
66 CMICmdArgValOptionLong::CMICmdArgValOptionLong( const CMIUtilString & vrArgName, const bool vbMandatory, const bool vbHandleByCmd, const ArgValType_e veType, const MIuint vnExpectingNOptions )
67 :       CMICmdArgValListBase( vrArgName, vbMandatory, vbHandleByCmd )
68 ,       m_nExpectingNOptions( vnExpectingNOptions )
69 ,       m_eExpectingOptionType( veType )
70 {
71 }
72
73 //++ ------------------------------------------------------------------------------------
74 // Details:     CMICmdArgValOptionLong destructor.
75 // Type:        Overridden.
76 // Args:        None.
77 // Return:      None.
78 // Throws:      None.
79 //--
80 CMICmdArgValOptionLong::~CMICmdArgValOptionLong( void )
81 {
82         // Tidy up
83         Destroy();
84 }
85
86 //++ ------------------------------------------------------------------------------------
87 // Details:     Tear down resources used by *this object.
88 // Type:        Method.
89 // Args:        None.
90 // Return:      None.
91 // Throws:      None.
92 //--
93 void CMICmdArgValOptionLong::Destroy( void )
94 {
95         // Tidy up
96         VecArgObjPtr_t::const_iterator it = m_vecArgsExpected.begin();
97         while( it != m_vecArgsExpected.end() )
98         {
99                 CMICmdArgValBase * pOptionObj = *it;
100                 delete pOptionObj;
101
102                 // Next
103                 ++it;
104         }
105         m_vecArgsExpected.clear();
106 }
107
108 //++ ------------------------------------------------------------------------------------
109 // Details:     Parse the command's argument options string and try to extract the long
110 //                      argument *this argument type is looking for.
111 // Type:        Overridden.
112 // Args:        vwArgContext    - (RW) The command's argument options string.
113 // Return:      MIstatus::success - Functional succeeded.
114 //                      MIstatus::failure - Functional failed.
115 // Throws:      None.
116 //--
117 bool CMICmdArgValOptionLong::Validate( CMICmdArgContext & vwArgContext )
118 {
119         if( vwArgContext.IsEmpty() )
120                 return MIstatus::success;
121
122         if( vwArgContext.GetNumberArgsPresent() == 1 )
123         {
124                 const CMIUtilString & rArg( vwArgContext.GetArgsLeftToParse() ); 
125                 if( IsArgLongOption( rArg ) && ArgNameMatch( rArg ) )
126                 {
127                         m_bFound = true;
128                         
129                         if( !vwArgContext.RemoveArg( rArg ) )
130                                 return MIstatus::failure;
131
132                         if( m_nExpectingNOptions == 0 )
133                         {
134                                 m_bValid = true;
135                                 return MIstatus::success;
136                         }
137
138                         m_bIsMissingOptions = true;
139                         return MIstatus::failure;
140                 }
141                 else
142                         return MIstatus::failure;
143         }
144         
145         // More than one option...
146         MIuint nArgIndex = 0;
147         const CMIUtilString::VecString_t vecOptions( vwArgContext.GetArgs() );
148         CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
149         while( it != vecOptions.end() )
150         {
151                 const CMIUtilString & rArg( *it ); 
152                 if( IsArgOptionCorrect( rArg ) && ArgNameMatch( rArg ) )
153                 {       
154                         m_bFound = true;
155
156                         if( !vwArgContext.RemoveArg( rArg ) )                                   
157                                 return MIstatus::failure;
158                         
159                         if( m_nExpectingNOptions != 0 )
160                         {
161                                 if( ExtractExpectedOptions( vwArgContext, nArgIndex ) )
162                                 {
163                                         m_bValid = true;
164                                         return MIstatus::success;
165                                 }
166
167                                 m_bIsMissingOptions = true;
168                                 return MIstatus::failure;
169                         }
170                         else
171                         {
172                                 m_bValid = true;
173                                 return MIstatus::success;
174                         }
175                 }
176                 
177                 // Next
178                 ++it;
179                 ++nArgIndex;
180         }
181
182         return MIstatus::failure;
183 }
184
185 //++ ------------------------------------------------------------------------------------
186 // Details:     Parse the text following *this argument and extract the options the values of
187 //                      CMICmdArgValListBase::m_eArgType forming argument objects for each of those
188 //                      options extracted.
189 // Type:        Method.
190 // Args:        vrwTxt          - (RW)  The command's argument options string.
191 //                      nArgIndex       - (R)   The Nth arg position in argument context from the left.
192 // Return:      MIstatus::success - Functional succeeded.
193 //                      MIstatus::failure - Functional failed.
194 // Throws:      None.
195 //--
196 bool CMICmdArgValOptionLong::ExtractExpectedOptions( CMICmdArgContext & vrwTxt, const MIuint nArgIndex ) 
197 {
198         CMIUtilString::VecString_t vecOptions;
199         MIuint nOptionsPresent = 0;
200         if( (m_eExpectingOptionType != eArgValType_StringQuoted) && 
201                 (m_eExpectingOptionType != eArgValType_StringQuotedNumber) &&
202                 (m_eExpectingOptionType != eArgValType_StringQuotedNumberPath) )
203                 nOptionsPresent = vrwTxt.GetArgsLeftToParse().Split( " ", vecOptions );
204         else
205                 nOptionsPresent = vrwTxt.GetArgsLeftToParse().SplitConsiderQuotes( " ", vecOptions );
206         if( nOptionsPresent == 0 )
207                 return MIstatus::failure;
208
209         MIuint nArgIndexCnt = 0;
210         MIuint nTypeCnt = 0;
211         MIuint nTypeCnt2 = 0;
212         MIuint nFoundNOptionsCnt = 0;
213         CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
214         while( it != vecOptions.end() )
215         {
216                 // Move to the Nth argument position from left before do validation/checking
217                 if( nArgIndexCnt++ == nArgIndex )
218                 {
219                         nTypeCnt++;
220                         const CMIUtilString & rOption( *it ); 
221                         if( IsExpectedCorrectType( rOption, m_eExpectingOptionType ) )
222                         {
223                                 nTypeCnt2++;
224                                 CMICmdArgValBase * pOptionObj = CreationObj( rOption, m_eExpectingOptionType );
225                                 if( (pOptionObj != nullptr) && vrwTxt.RemoveArgAtPos( rOption, nArgIndex ) )
226                                 {
227                                         nFoundNOptionsCnt++;
228                                         m_vecArgsExpected.push_back( pOptionObj );
229                                 }
230                         }
231
232                         // Is the sequence 'options' of same type broken. Expecting the same type until the
233                         // next argument.
234                         if( nTypeCnt != nTypeCnt2 )
235                                 return MIstatus::failure;
236
237                         if( nFoundNOptionsCnt == m_nExpectingNOptions )
238                                 return MIstatus::success;
239                 }
240
241                 // Next
242                 ++it;
243         }
244         if( nFoundNOptionsCnt != m_nExpectingNOptions )
245                 return MIstatus::failure;
246
247         return MIstatus::success;
248 }
249
250 //++ ------------------------------------------------------------------------------------
251 // Details:     Examine the string and determine if it is a valid long type option argument.
252 //                      Long type argument looks like --someLongOption.
253 // Type:        Method.
254 // Args:        vrTxt   - (R) Some text.
255 // Return:      bool -  True = yes valid arg, false = no.
256 // Throws:      None.
257 //--
258 bool CMICmdArgValOptionLong::IsArgLongOption( const CMIUtilString & vrTxt ) const
259 {
260         const bool bHavePosSlash = (vrTxt.find_first_of( "/" ) != std::string::npos);
261         const bool bHaveBckSlash = (vrTxt.find_first_of( "\\" ) != std::string::npos);
262         if( bHavePosSlash || bHaveBckSlash )
263                 return false;
264
265         const MIint nPos = vrTxt.find_first_of( "--" );
266         if( nPos != 0 )
267                 return false;
268         
269         if( vrTxt.length() < 3 )
270                 return false;
271
272         const CMIUtilString strArg = vrTxt.substr( 2 ).c_str();
273         if( strArg.IsNumber() )
274                 return false;
275
276         return true;
277 }
278
279 //++ ------------------------------------------------------------------------------------
280 // Details:     Examine the string and determine if it is a valid long type option argument.
281 //                      Long type argument looks like --someLongOption.
282 // Type:        Overideable.
283 // Args:        vrTxt   - (R) Some text.
284 // Return:      bool -  True = yes valid arg, false = no.
285 // Throws:      None.
286 //--
287 bool CMICmdArgValOptionLong::IsArgOptionCorrect( const CMIUtilString & vrTxt ) const
288 {
289         return IsArgLongOption( vrTxt );
290 }
291
292 //++ ------------------------------------------------------------------------------------
293 // Details:     Does the argument name of the argument being parsed ATM match the name of 
294 //                      *this argument object.
295 // Type:        Method.
296 // Args:        vrTxt   - (R) Some text.
297 // Return:      bool -  True = yes arg name matched, false = no.
298 // Throws:      None.
299 //--
300 bool CMICmdArgValOptionLong::ArgNameMatch( const CMIUtilString & vrTxt ) const
301 {
302         const CMIUtilString strArg = vrTxt.substr( 2 ).c_str();
303         return (strArg == GetName() );
304 }
305
306 //++ ------------------------------------------------------------------------------------
307 // Details:     Retrieve the list of CMICmdArgValBase derived option objects found following
308 //                      *this long option argument. For example "list-thread-groups [ --recurse 1 ]"
309 //                      where 1 is the list of expected option to follow.
310 // Type:        Method.
311 // Args:        None.
312 // Return:      CMICmdArgValListBase::VecArgObjPtr_t & -        List of options.
313 // Throws:      None.
314 //--
315 const CMICmdArgValListBase::VecArgObjPtr_t & CMICmdArgValOptionLong::GetExpectedOptions( void ) const
316 {
317         return m_vecArgsExpected;
318 }
319