]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdArgValFile.cpp
Merge ^/head r275262 through r275363.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MICmdArgValFile.cpp
1 //===-- MICmdArgValFile.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:                MICmdArgValFile.cpp
12 //
13 // Overview:    CMICmdArgValFile 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 "MICmdArgValFile.h"
24 #include "MICmdArgContext.h"
25
26 //++ ------------------------------------------------------------------------------------
27 // Details:     CMICmdArgValFile constructor.
28 // Type:        Method.
29 // Args:        None.
30 // Return:      None.
31 // Throws:      None.
32 //--
33 CMICmdArgValFile::CMICmdArgValFile( void )
34 {
35 }
36
37 //++ ------------------------------------------------------------------------------------
38 // Details:     CMICmdArgValFile constructor.
39 // Type:        Method.
40 // Args:        vrArgName               - (R) Argument's name to search by.
41 //                      vbMandatory             - (R) True = Yes must be present, false = optional argument.
42 //                      vbHandleByCmd   - (R) True = Command processes *this option, false = not handled.
43 // Return:      None.
44 // Throws:      None.
45 //--
46 CMICmdArgValFile::CMICmdArgValFile( const CMIUtilString & vrArgName, const bool vbMandatory, const bool vbHandleByCmd )
47 :       CMICmdArgValBaseTemplate( vrArgName, vbMandatory, vbHandleByCmd )
48 {
49 }
50
51 //++ ------------------------------------------------------------------------------------
52 // Details:     CMICmdArgValFile destructor.
53 // Type:        Overridden.
54 // Args:        None.
55 // Return:      None.
56 // Throws:      None.
57 //--
58 CMICmdArgValFile::~CMICmdArgValFile( void )
59 {
60 }
61
62 //++ ------------------------------------------------------------------------------------
63 // Details:     Parse the command's argument options string and try to extract the value *this
64 //                      argument is looking for.
65 // Type:        Overridden.
66 // Args:        vwArgContext    - (R) The command's argument options string.
67 // Return:      MIstatus::success - Functional succeeded.
68 //                      MIstatus::failure - Functional failed.
69 // Throws:      None.
70 //--
71 bool CMICmdArgValFile::Validate( CMICmdArgContext & vwArgContext )
72 {
73         if( vwArgContext.IsEmpty() )
74                 return MIstatus::success;
75
76         // The GDB/MI spec suggests there is only parameter
77
78         if( vwArgContext.GetNumberArgsPresent() == 1 )
79         {
80                 const CMIUtilString & rFile( vwArgContext.GetArgsLeftToParse() ); 
81                 if( IsFilePath( rFile ) )
82                 {
83                         m_bFound = true;
84                         m_bValid = true;
85                         m_argValue = rFile.Trim( '"' );
86                         vwArgContext.RemoveArg( rFile );
87                         return MIstatus::success;
88                 }
89                 else
90                         return MIstatus::failure;
91         }
92         
93         // In reality there are more than one option,  if so the file option 
94         // is the last one (don't handle that here - find the best looking one)
95         const CMIUtilString::VecString_t vecOptions( vwArgContext.GetArgs() );
96         CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
97         while( it != vecOptions.end() )
98         {
99                 const CMIUtilString & rTxt( *it ); 
100                 if( IsFilePath( rTxt ) )
101                 {
102                         m_bFound = true;
103                                 
104                         if( vwArgContext.RemoveArg( rTxt ) )
105                         {
106                                 m_bValid = true;
107                                 m_argValue = rTxt.Trim( '"' );
108                                 return MIstatus::success;
109                         }
110                         else
111                                 return MIstatus::success;
112                 }
113                 
114                 // Next
115                 ++it;
116         }
117
118         return MIstatus::failure;
119 }
120
121 //++ ------------------------------------------------------------------------------------
122 // Details:     Given some text extract the file name path from it. If a space is found in 
123 //                      path done return the path surrounded in quotes.
124 // Type:        Method.
125 // Args:        vrTxt   - (R) The text to extract the file name path from.
126 // Return:      CMIUtilString - File name and or path.
127 // Throws:      None.
128 //--
129 CMIUtilString CMICmdArgValFile::GetFileNamePath( const CMIUtilString & vrTxt ) const
130 {
131         CMIUtilString fileNamePath( vrTxt );
132         
133         // Look for a space in the path
134         const MIchar cSpace = ' ';
135         const MIint nPos = fileNamePath.find( cSpace );
136         if( nPos != (MIint) std::string::npos )
137                 fileNamePath = CMIUtilString::Format( "\"%s\"", fileNamePath.c_str() );
138
139         return fileNamePath;
140 }
141
142 //++ ------------------------------------------------------------------------------------
143 // Details:     Examine the string and determine if it is a valid file name path.
144 // Type:        Method.
145 // Args:        vrFileNamePath  - (R) File's name and directory path.
146 // Return:      bool -  True = yes valid file path, false = no.
147 // Throws:      None.
148 //--
149 bool CMICmdArgValFile::IsFilePath( const CMIUtilString & vrFileNamePath ) const
150 {
151         if( vrFileNamePath.empty() )
152                 return false;
153
154         const bool bHavePosSlash = (vrFileNamePath.find_first_of( "/" ) != std::string::npos);
155         const bool bHaveBckSlash = (vrFileNamePath.find_first_of( "\\" ) != std::string::npos);
156         
157         // Look for --someLongOption
158         MIint nPos = vrFileNamePath.find_first_of( "--" );
159         const bool bLong = (nPos == 0);
160         if( bLong )
161                 return false;
162         
163         // Look for -f type short parameters
164         nPos = vrFileNamePath.find_first_of( "-" );
165         const bool bShort = (nPos == 0);
166         if( bShort )
167                 return false;
168         
169         // Look for i1 i2 i3....
170         nPos = vrFileNamePath.find_first_of( "i" );
171         const bool bFoundI1 = ((nPos == 0) && (::isdigit( vrFileNamePath[ 1 ] )) );
172         if( bFoundI1 )
173                 return false;
174         
175         const bool bValidChars = IsValidChars( vrFileNamePath );
176         if( bValidChars || bHavePosSlash || bHaveBckSlash )
177                 return true;
178
179         return false;
180 }
181
182 //++ ------------------------------------------------------------------------------------
183 // Details:     Determine if the path contains valid characters for a file path. Letters can be
184 //                      either upper or lower case.
185 // Type:        Method.
186 // Args:        vrText  - (R) The text data to examine.
187 // Return:      bool - True = yes valid, false = one or more chars is valid.
188 // Throws:      None.
189 //--
190 bool CMICmdArgValFile::IsValidChars( const CMIUtilString & vrText ) const
191 {
192         const MIchar * pPtr = const_cast< MIchar * >( vrText.c_str() );
193         for( MIuint i = 0; i < vrText.length(); i++, pPtr++ )
194         {
195                 const MIchar c = *pPtr;
196                 if( ::isalnum( (int) c ) == 0 )
197                 {
198                         if( (c != '.') && (c != '-') && (c != '_') )
199                                 return false;
200                 }
201         }
202         
203         return true;
204 }