blob: 7f5b20ec3140864f08f1dba4a5ca7dcd54497af2 [file] [log] [blame]
Deepak Panickal6f9c4682014-05-16 10:51:01 +00001//===-- 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
Deepak Panickal6f9c4682014-05-16 10:51:01 +000010// In-house headers:
11#include "MICmdArgValFile.h"
12#include "MICmdArgContext.h"
13
14//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000015// Details: CMICmdArgValFile constructor.
16// Type: Method.
17// Args: None.
18// Return: None.
19// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000020//--
Bruce Mitchenere2453af2015-08-04 10:24:20 +000021CMICmdArgValFile::CMICmdArgValFile()
Deepak Panickal6f9c4682014-05-16 10:51:01 +000022{
23}
24
25//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000026// Details: CMICmdArgValFile constructor.
27// Type: Method.
28// Args: vrArgName - (R) Argument's name to search by.
29// vbMandatory - (R) True = Yes must be present, false = optional argument.
30// vbHandleByCmd - (R) True = Command processes *this option, false = not handled.
31// Return: None.
32// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000033//--
Zachary Turner1d6af022014-11-17 18:06:21 +000034CMICmdArgValFile::CMICmdArgValFile(const CMIUtilString &vrArgName, const bool vbMandatory, const bool vbHandleByCmd)
35 : CMICmdArgValBaseTemplate(vrArgName, vbMandatory, vbHandleByCmd)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000036{
37}
38
39//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000040// Details: CMICmdArgValFile destructor.
41// Type: Overridden.
42// Args: None.
43// Return: None.
44// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000045//--
Bruce Mitchenere2453af2015-08-04 10:24:20 +000046CMICmdArgValFile::~CMICmdArgValFile()
Deepak Panickal6f9c4682014-05-16 10:51:01 +000047{
48}
49
50//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000051// Details: Parse the command's argument options string and try to extract the value *this
52// argument is looking for.
53// Type: Overridden.
54// Args: vwArgContext - (R) The command's argument options string.
55// Return: MIstatus::success - Functional succeeded.
56// MIstatus::failure - Functional failed.
57// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000058//--
Zachary Turner1d6af022014-11-17 18:06:21 +000059bool
60CMICmdArgValFile::Validate(CMICmdArgContext &vwArgContext)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000061{
Zachary Turner1d6af022014-11-17 18:06:21 +000062 if (vwArgContext.IsEmpty())
Bruce Mitchener1425b472015-06-08 11:15:09 +000063 return m_bMandatory ? MIstatus::failure : MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000064
Zachary Turner1d6af022014-11-17 18:06:21 +000065 // The GDB/MI spec suggests there is only parameter
Deepak Panickald2499282014-08-08 16:47:42 +000066
Zachary Turner1d6af022014-11-17 18:06:21 +000067 if (vwArgContext.GetNumberArgsPresent() == 1)
68 {
69 const CMIUtilString &rFile(vwArgContext.GetArgsLeftToParse());
70 if (IsFilePath(rFile))
71 {
72 m_bFound = true;
73 m_bValid = true;
74 m_argValue = rFile.Trim('"');
75 vwArgContext.RemoveArg(rFile);
76 return MIstatus::success;
77 }
78 else
79 return MIstatus::failure;
80 }
Deepak Panickal6f9c4682014-05-16 10:51:01 +000081
Zachary Turner1d6af022014-11-17 18:06:21 +000082 // In reality there are more than one option, if so the file option
83 // is the last one (don't handle that here - find the best looking one)
84 const CMIUtilString::VecString_t vecOptions(vwArgContext.GetArgs());
85 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
86 while (it != vecOptions.end())
87 {
88 const CMIUtilString &rTxt(*it);
89 if (IsFilePath(rTxt))
90 {
91 m_bFound = true;
92
93 if (vwArgContext.RemoveArg(rTxt))
94 {
95 m_bValid = true;
96 m_argValue = rTxt.Trim('"');
97 return MIstatus::success;
98 }
99 else
100 return MIstatus::success;
101 }
102
103 // Next
104 ++it;
105 }
106
107 return MIstatus::failure;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000108}
109
110//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +0000111// Details: Given some text extract the file name path from it. If a space is found in
112// path done return the path surrounded in quotes.
113// Type: Method.
114// Args: vrTxt - (R) The text to extract the file name path from.
115// Return: CMIUtilString - File name and or path.
116// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000117//--
Zachary Turner1d6af022014-11-17 18:06:21 +0000118CMIUtilString
119CMICmdArgValFile::GetFileNamePath(const CMIUtilString &vrTxt) const
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000120{
Zachary Turner1d6af022014-11-17 18:06:21 +0000121 CMIUtilString fileNamePath(vrTxt);
Deepak Panickald2499282014-08-08 16:47:42 +0000122
Zachary Turner1d6af022014-11-17 18:06:21 +0000123 // Look for a space in the path
Bruce Mitchener3b25b472015-07-03 13:45:34 +0000124 const char cSpace = ' ';
Bruce Mitchenere86c6fa2015-07-03 15:40:44 +0000125 const size_t nPos = fileNamePath.find(cSpace);
126 if (nPos != std::string::npos)
Zachary Turner1d6af022014-11-17 18:06:21 +0000127 fileNamePath = CMIUtilString::Format("\"%s\"", fileNamePath.c_str());
128
129 return fileNamePath;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000130}
131
132//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +0000133// Details: Examine the string and determine if it is a valid file name path.
134// Type: Method.
135// Args: vrFileNamePath - (R) File's name and directory path.
136// Return: bool - True = yes valid file path, false = no.
137// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000138//--
Zachary Turner1d6af022014-11-17 18:06:21 +0000139bool
140CMICmdArgValFile::IsFilePath(const CMIUtilString &vrFileNamePath) const
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000141{
Zachary Turner1d6af022014-11-17 18:06:21 +0000142 if (vrFileNamePath.empty())
143 return false;
Deepak Panickald2499282014-08-08 16:47:42 +0000144
Bruce Mitchenere8433cc2015-09-01 23:57:17 +0000145 const bool bHavePosSlash = (vrFileNamePath.find('/') != std::string::npos);
146 const bool bHaveBckSlash = (vrFileNamePath.find('\\') != std::string::npos);
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000147
Zachary Turner1d6af022014-11-17 18:06:21 +0000148 // Look for --someLongOption
Bruce Mitcheneree188ba2015-09-01 04:36:54 +0000149 size_t nPos = vrFileNamePath.find("--");
Zachary Turner1d6af022014-11-17 18:06:21 +0000150 const bool bLong = (nPos == 0);
151 if (bLong)
152 return false;
153
154 // Look for -f type short parameters
Bruce Mitchenere8433cc2015-09-01 23:57:17 +0000155 nPos = vrFileNamePath.find('-');
Zachary Turner1d6af022014-11-17 18:06:21 +0000156 const bool bShort = (nPos == 0);
157 if (bShort)
158 return false;
159
160 // Look for i1 i2 i3....
Bruce Mitchenere8433cc2015-09-01 23:57:17 +0000161 nPos = vrFileNamePath.find('i');
Zachary Turner1d6af022014-11-17 18:06:21 +0000162 const bool bFoundI1 = ((nPos == 0) && (::isdigit(vrFileNamePath[1])));
163 if (bFoundI1)
164 return false;
165
166 const bool bValidChars = IsValidChars(vrFileNamePath);
167 if (bValidChars || bHavePosSlash || bHaveBckSlash)
168 return true;
169
170 return false;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000171}
Deepak Panickald2499282014-08-08 16:47:42 +0000172
173//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +0000174// Details: Determine if the path contains valid characters for a file path. Letters can be
175// either upper or lower case.
176// Type: Method.
177// Args: vrText - (R) The text data to examine.
178// Return: bool - True = yes valid, false = one or more chars is valid.
179// Throws: None.
Deepak Panickald2499282014-08-08 16:47:42 +0000180//--
Zachary Turner1d6af022014-11-17 18:06:21 +0000181bool
182CMICmdArgValFile::IsValidChars(const CMIUtilString &vrText) const
Deepak Panickald2499282014-08-08 16:47:42 +0000183{
Ilia K4dc4ed02015-02-25 06:34:05 +0000184 static CMIUtilString s_strSpecialCharacters(".'\"`@#$%^&*()_+-={}[]| ");
Bruce Mitchener201dec72015-07-03 15:31:32 +0000185 const char *pPtr = vrText.c_str();
Zachary Turner1d6af022014-11-17 18:06:21 +0000186 for (MIuint i = 0; i < vrText.length(); i++, pPtr++)
187 {
Bruce Mitchener3b25b472015-07-03 13:45:34 +0000188 const char c = *pPtr;
Zachary Turner1d6af022014-11-17 18:06:21 +0000189 if (::isalnum((int)c) == 0)
190 {
Ilia K4dc4ed02015-02-25 06:34:05 +0000191 if (s_strSpecialCharacters.find(c) == CMIUtilString::npos)
Zachary Turner1d6af022014-11-17 18:06:21 +0000192 return false;
193 }
194 }
195
196 return true;
Deepak Panickald2499282014-08-08 16:47:42 +0000197}