| 1181 | } |
| 1182 | |
| 1183 | String Utility::EscapeShellCmd(const String& s) |
| 1184 | { |
| 1185 | String result; |
| 1186 | size_t prev_quote = String::NPos; |
| 1187 | int index = -1; |
| 1188 | |
| 1189 | for (char ch : s) { |
| 1190 | bool escape = false; |
| 1191 | |
| 1192 | index++; |
| 1193 | |
| 1194 | #ifdef _WIN32 |
| 1195 | if (ch == '%' || ch == '"' || ch == '\'') |
| 1196 | escape = true; |
| 1197 | #else /* _WIN32 */ |
| 1198 | if (ch == '"' || ch == '\'') { |
| 1199 | /* Find a matching closing quotation character. */ |
| 1200 | if (prev_quote == String::NPos && (prev_quote = s.FindFirstOf(ch, index + 1)) != String::NPos) |
| 1201 | ; /* Empty statement. */ |
| 1202 | else if (prev_quote != String::NPos && s[prev_quote] == ch) |
| 1203 | prev_quote = String::NPos; |
| 1204 | else |
| 1205 | escape = true; |
| 1206 | } |
| 1207 | #endif /* _WIN32 */ |
| 1208 | |
| 1209 | if (ch == '#' || ch == '&' || ch == ';' || ch == '`' || ch == '|' || |
| 1210 | ch == '*' || ch == '?' || ch == '~' || ch == '<' || ch == '>' || |
| 1211 | ch == '^' || ch == '(' || ch == ')' || ch == '[' || ch == ']' || |
| 1212 | ch == '{' || ch == '}' || ch == '$' || ch == '\\' || ch == '\x0A' || |
| 1213 | ch == '\xFF') |
| 1214 | escape = true; |
| 1215 | |
| 1216 | if (escape) |
| 1217 | #ifdef _WIN32 |
| 1218 | result += '^'; |
| 1219 | #else /* _WIN32 */ |
| 1220 | result += '\\'; |
| 1221 | #endif /* _WIN32 */ |
| 1222 | |
| 1223 | result += ch; |
| 1224 | } |
| 1225 | |
| 1226 | return result; |
| 1227 | } |
| 1228 | |
| 1229 | String Utility::EscapeShellArg(const String& s) |
| 1230 | { |
nothing calls this directly
no test coverage detected