| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 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 | |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 10 | // In-house headers: |
| 11 | #include "MICmdArgValFile.h" |
| 12 | #include "MICmdArgContext.h" |
| 13 | |
| 14 | //++ ------------------------------------------------------------------------------------ |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 15 | // Details: CMICmdArgValFile constructor. |
| 16 | // Type: Method. |
| 17 | // Args: None. |
| 18 | // Return: None. |
| 19 | // Throws: None. |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 20 | //-- |
| Bruce Mitchener | e2453af | 2015-08-04 10:24:20 +0000 | [diff] [blame] | 21 | CMICmdArgValFile::CMICmdArgValFile() |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 22 | { |
| 23 | } |
| 24 | |
| 25 | //++ ------------------------------------------------------------------------------------ |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 26 | // 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 Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 33 | //-- |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 34 | CMICmdArgValFile::CMICmdArgValFile(const CMIUtilString &vrArgName, const bool vbMandatory, const bool vbHandleByCmd) |
| 35 | : CMICmdArgValBaseTemplate(vrArgName, vbMandatory, vbHandleByCmd) |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | //++ ------------------------------------------------------------------------------------ |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 40 | // Details: CMICmdArgValFile destructor. |
| 41 | // Type: Overridden. |
| 42 | // Args: None. |
| 43 | // Return: None. |
| 44 | // Throws: None. |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 45 | //-- |
| Bruce Mitchener | e2453af | 2015-08-04 10:24:20 +0000 | [diff] [blame] | 46 | CMICmdArgValFile::~CMICmdArgValFile() |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
| 50 | //++ ------------------------------------------------------------------------------------ |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 51 | // 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 Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 58 | //-- |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 59 | bool |
| 60 | CMICmdArgValFile::Validate(CMICmdArgContext &vwArgContext) |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 61 | { |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 62 | if (vwArgContext.IsEmpty()) |
| Bruce Mitchener | 1425b47 | 2015-06-08 11:15:09 +0000 | [diff] [blame] | 63 | return m_bMandatory ? MIstatus::failure : MIstatus::success; |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 64 | |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 65 | // The GDB/MI spec suggests there is only parameter |
| Deepak Panickal | d249928 | 2014-08-08 16:47:42 +0000 | [diff] [blame] | 66 | |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 67 | 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 Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 81 | |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 82 | // 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 Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | //++ ------------------------------------------------------------------------------------ |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 111 | // 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 Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 117 | //-- |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 118 | CMIUtilString |
| 119 | CMICmdArgValFile::GetFileNamePath(const CMIUtilString &vrTxt) const |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 120 | { |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 121 | CMIUtilString fileNamePath(vrTxt); |
| Deepak Panickal | d249928 | 2014-08-08 16:47:42 +0000 | [diff] [blame] | 122 | |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 123 | // Look for a space in the path |
| Bruce Mitchener | 3b25b47 | 2015-07-03 13:45:34 +0000 | [diff] [blame] | 124 | const char cSpace = ' '; |
| Bruce Mitchener | e86c6fa | 2015-07-03 15:40:44 +0000 | [diff] [blame] | 125 | const size_t nPos = fileNamePath.find(cSpace); |
| 126 | if (nPos != std::string::npos) |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 127 | fileNamePath = CMIUtilString::Format("\"%s\"", fileNamePath.c_str()); |
| 128 | |
| 129 | return fileNamePath; |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | //++ ------------------------------------------------------------------------------------ |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 133 | // 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 Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 138 | //-- |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 139 | bool |
| 140 | CMICmdArgValFile::IsFilePath(const CMIUtilString &vrFileNamePath) const |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 141 | { |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 142 | if (vrFileNamePath.empty()) |
| 143 | return false; |
| Deepak Panickal | d249928 | 2014-08-08 16:47:42 +0000 | [diff] [blame] | 144 | |
| Bruce Mitchener | e8433cc | 2015-09-01 23:57:17 +0000 | [diff] [blame^] | 145 | const bool bHavePosSlash = (vrFileNamePath.find('/') != std::string::npos); |
| 146 | const bool bHaveBckSlash = (vrFileNamePath.find('\\') != std::string::npos); |
| Deepak Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 147 | |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 148 | // Look for --someLongOption |
| Bruce Mitchener | ee188ba | 2015-09-01 04:36:54 +0000 | [diff] [blame] | 149 | size_t nPos = vrFileNamePath.find("--"); |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 150 | const bool bLong = (nPos == 0); |
| 151 | if (bLong) |
| 152 | return false; |
| 153 | |
| 154 | // Look for -f type short parameters |
| Bruce Mitchener | e8433cc | 2015-09-01 23:57:17 +0000 | [diff] [blame^] | 155 | nPos = vrFileNamePath.find('-'); |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 156 | const bool bShort = (nPos == 0); |
| 157 | if (bShort) |
| 158 | return false; |
| 159 | |
| 160 | // Look for i1 i2 i3.... |
| Bruce Mitchener | e8433cc | 2015-09-01 23:57:17 +0000 | [diff] [blame^] | 161 | nPos = vrFileNamePath.find('i'); |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 162 | 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 Panickal | 6f9c468 | 2014-05-16 10:51:01 +0000 | [diff] [blame] | 171 | } |
| Deepak Panickal | d249928 | 2014-08-08 16:47:42 +0000 | [diff] [blame] | 172 | |
| 173 | //++ ------------------------------------------------------------------------------------ |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 174 | // 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 Panickal | d249928 | 2014-08-08 16:47:42 +0000 | [diff] [blame] | 180 | //-- |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 181 | bool |
| 182 | CMICmdArgValFile::IsValidChars(const CMIUtilString &vrText) const |
| Deepak Panickal | d249928 | 2014-08-08 16:47:42 +0000 | [diff] [blame] | 183 | { |
| Ilia K | 4dc4ed0 | 2015-02-25 06:34:05 +0000 | [diff] [blame] | 184 | static CMIUtilString s_strSpecialCharacters(".'\"`@#$%^&*()_+-={}[]| "); |
| Bruce Mitchener | 201dec7 | 2015-07-03 15:31:32 +0000 | [diff] [blame] | 185 | const char *pPtr = vrText.c_str(); |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 186 | for (MIuint i = 0; i < vrText.length(); i++, pPtr++) |
| 187 | { |
| Bruce Mitchener | 3b25b47 | 2015-07-03 13:45:34 +0000 | [diff] [blame] | 188 | const char c = *pPtr; |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 189 | if (::isalnum((int)c) == 0) |
| 190 | { |
| Ilia K | 4dc4ed0 | 2015-02-25 06:34:05 +0000 | [diff] [blame] | 191 | if (s_strSpecialCharacters.find(c) == CMIUtilString::npos) |
| Zachary Turner | 1d6af02 | 2014-11-17 18:06:21 +0000 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return true; |
| Deepak Panickal | d249928 | 2014-08-08 16:47:42 +0000 | [diff] [blame] | 197 | } |