blob: 591ee9b358cea85ec0d2674388d1919daa2c6f20 [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
10//++
Zachary Turner1d6af022014-11-17 18:06:21 +000011// File: MICmdArgValFile.cpp
Deepak Panickal6f9c4682014-05-16 10:51:01 +000012//
Zachary Turner1d6af022014-11-17 18:06:21 +000013// Overview: CMICmdArgValFile implementation.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000014//
Zachary Turner1d6af022014-11-17 18:06:21 +000015// Environment: Compilers: Visual C++ 12.
16// gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
17// Libraries: See MIReadmetxt.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000018//
Zachary Turner1d6af022014-11-17 18:06:21 +000019// Copyright: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000020//--
21
22// In-house headers:
23#include "MICmdArgValFile.h"
24#include "MICmdArgContext.h"
25
26//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000027// Details: CMICmdArgValFile constructor.
28// Type: Method.
29// Args: None.
30// Return: None.
31// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000032//--
Zachary Turner1d6af022014-11-17 18:06:21 +000033CMICmdArgValFile::CMICmdArgValFile(void)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000034{
35}
36
37//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000038// 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.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000045//--
Zachary Turner1d6af022014-11-17 18:06:21 +000046CMICmdArgValFile::CMICmdArgValFile(const CMIUtilString &vrArgName, const bool vbMandatory, const bool vbHandleByCmd)
47 : CMICmdArgValBaseTemplate(vrArgName, vbMandatory, vbHandleByCmd)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000048{
49}
50
51//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000052// Details: CMICmdArgValFile destructor.
53// Type: Overridden.
54// Args: None.
55// Return: None.
56// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000057//--
Zachary Turner1d6af022014-11-17 18:06:21 +000058CMICmdArgValFile::~CMICmdArgValFile(void)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000059{
60}
61
62//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +000063// 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.
Deepak Panickal6f9c4682014-05-16 10:51:01 +000070//--
Zachary Turner1d6af022014-11-17 18:06:21 +000071bool
72CMICmdArgValFile::Validate(CMICmdArgContext &vwArgContext)
Deepak Panickal6f9c4682014-05-16 10:51:01 +000073{
Zachary Turner1d6af022014-11-17 18:06:21 +000074 if (vwArgContext.IsEmpty())
75 return MIstatus::success;
Deepak Panickal6f9c4682014-05-16 10:51:01 +000076
Zachary Turner1d6af022014-11-17 18:06:21 +000077 // The GDB/MI spec suggests there is only parameter
Deepak Panickald2499282014-08-08 16:47:42 +000078
Zachary Turner1d6af022014-11-17 18:06:21 +000079 if (vwArgContext.GetNumberArgsPresent() == 1)
80 {
81 const CMIUtilString &rFile(vwArgContext.GetArgsLeftToParse());
82 if (IsFilePath(rFile))
83 {
84 m_bFound = true;
85 m_bValid = true;
86 m_argValue = rFile.Trim('"');
87 vwArgContext.RemoveArg(rFile);
88 return MIstatus::success;
89 }
90 else
91 return MIstatus::failure;
92 }
Deepak Panickal6f9c4682014-05-16 10:51:01 +000093
Zachary Turner1d6af022014-11-17 18:06:21 +000094 // In reality there are more than one option, if so the file option
95 // is the last one (don't handle that here - find the best looking one)
96 const CMIUtilString::VecString_t vecOptions(vwArgContext.GetArgs());
97 CMIUtilString::VecString_t::const_iterator it = vecOptions.begin();
98 while (it != vecOptions.end())
99 {
100 const CMIUtilString &rTxt(*it);
101 if (IsFilePath(rTxt))
102 {
103 m_bFound = true;
104
105 if (vwArgContext.RemoveArg(rTxt))
106 {
107 m_bValid = true;
108 m_argValue = rTxt.Trim('"');
109 return MIstatus::success;
110 }
111 else
112 return MIstatus::success;
113 }
114
115 // Next
116 ++it;
117 }
118
119 return MIstatus::failure;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000120}
121
122//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +0000123// Details: Given some text extract the file name path from it. If a space is found in
124// path done return the path surrounded in quotes.
125// Type: Method.
126// Args: vrTxt - (R) The text to extract the file name path from.
127// Return: CMIUtilString - File name and or path.
128// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000129//--
Zachary Turner1d6af022014-11-17 18:06:21 +0000130CMIUtilString
131CMICmdArgValFile::GetFileNamePath(const CMIUtilString &vrTxt) const
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000132{
Zachary Turner1d6af022014-11-17 18:06:21 +0000133 CMIUtilString fileNamePath(vrTxt);
Deepak Panickald2499282014-08-08 16:47:42 +0000134
Zachary Turner1d6af022014-11-17 18:06:21 +0000135 // Look for a space in the path
136 const MIchar cSpace = ' ';
137 const MIint nPos = fileNamePath.find(cSpace);
138 if (nPos != (MIint)std::string::npos)
139 fileNamePath = CMIUtilString::Format("\"%s\"", fileNamePath.c_str());
140
141 return fileNamePath;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000142}
143
144//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +0000145// Details: Examine the string and determine if it is a valid file name path.
146// Type: Method.
147// Args: vrFileNamePath - (R) File's name and directory path.
148// Return: bool - True = yes valid file path, false = no.
149// Throws: None.
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000150//--
Zachary Turner1d6af022014-11-17 18:06:21 +0000151bool
152CMICmdArgValFile::IsFilePath(const CMIUtilString &vrFileNamePath) const
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000153{
Zachary Turner1d6af022014-11-17 18:06:21 +0000154 if (vrFileNamePath.empty())
155 return false;
Deepak Panickald2499282014-08-08 16:47:42 +0000156
Zachary Turner1d6af022014-11-17 18:06:21 +0000157 const bool bHavePosSlash = (vrFileNamePath.find_first_of("/") != std::string::npos);
158 const bool bHaveBckSlash = (vrFileNamePath.find_first_of("\\") != std::string::npos);
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000159
Zachary Turner1d6af022014-11-17 18:06:21 +0000160 // Look for --someLongOption
161 MIint nPos = vrFileNamePath.find_first_of("--");
162 const bool bLong = (nPos == 0);
163 if (bLong)
164 return false;
165
166 // Look for -f type short parameters
167 nPos = vrFileNamePath.find_first_of("-");
168 const bool bShort = (nPos == 0);
169 if (bShort)
170 return false;
171
172 // Look for i1 i2 i3....
173 nPos = vrFileNamePath.find_first_of("i");
174 const bool bFoundI1 = ((nPos == 0) && (::isdigit(vrFileNamePath[1])));
175 if (bFoundI1)
176 return false;
177
178 const bool bValidChars = IsValidChars(vrFileNamePath);
179 if (bValidChars || bHavePosSlash || bHaveBckSlash)
180 return true;
181
182 return false;
Deepak Panickal6f9c4682014-05-16 10:51:01 +0000183}
Deepak Panickald2499282014-08-08 16:47:42 +0000184
185//++ ------------------------------------------------------------------------------------
Zachary Turner1d6af022014-11-17 18:06:21 +0000186// Details: Determine if the path contains valid characters for a file path. Letters can be
187// either upper or lower case.
188// Type: Method.
189// Args: vrText - (R) The text data to examine.
190// Return: bool - True = yes valid, false = one or more chars is valid.
191// Throws: None.
Deepak Panickald2499282014-08-08 16:47:42 +0000192//--
Zachary Turner1d6af022014-11-17 18:06:21 +0000193bool
194CMICmdArgValFile::IsValidChars(const CMIUtilString &vrText) const
Deepak Panickald2499282014-08-08 16:47:42 +0000195{
Ilia K4dc4ed02015-02-25 06:34:05 +0000196 static CMIUtilString s_strSpecialCharacters(".'\"`@#$%^&*()_+-={}[]| ");
Zachary Turner1d6af022014-11-17 18:06:21 +0000197 const MIchar *pPtr = const_cast<MIchar *>(vrText.c_str());
198 for (MIuint i = 0; i < vrText.length(); i++, pPtr++)
199 {
200 const MIchar c = *pPtr;
201 if (::isalnum((int)c) == 0)
202 {
Ilia K4dc4ed02015-02-25 06:34:05 +0000203 if (s_strSpecialCharacters.find(c) == CMIUtilString::npos)
Zachary Turner1d6af022014-11-17 18:06:21 +0000204 return false;
205 }
206 }
207
208 return true;
Deepak Panickald2499282014-08-08 16:47:42 +0000209}