| 261 | } |
| 262 | |
| 263 | bool SplitCommandLine(const std::string& command, |
| 264 | std::vector<std::string>* args) |
| 265 | { |
| 266 | args->clear(); |
| 267 | |
| 268 | char state = '\0'; |
| 269 | std::string::const_iterator pos = command.begin(); |
| 270 | std::string::const_iterator end = command.end(); |
| 271 | std::string arg; |
| 272 | while (pos < end) { |
| 273 | char c = *pos++; |
| 274 | switch (state) { |
| 275 | case '\0': |
| 276 | case 'a': |
| 277 | switch (c) { |
| 278 | case '"': |
| 279 | case '\'': |
| 280 | state = c; |
| 281 | break; |
| 282 | case '\\': |
| 283 | UnescapeCharToArg(&pos, end, false, &arg); |
| 284 | break; |
| 285 | default: |
| 286 | if (isspace(c)) { |
| 287 | if (state != '\0') { |
| 288 | args->push_back(arg); |
| 289 | arg.clear(); |
| 290 | } |
| 291 | state = ' '; |
| 292 | } else { |
| 293 | arg += c; |
| 294 | state = 'a'; |
| 295 | } |
| 296 | break; |
| 297 | } |
| 298 | break; |
| 299 | case '\'': |
| 300 | case '"': |
| 301 | if (state == c) { |
| 302 | state = 'a'; |
| 303 | } else { |
| 304 | if (c == '\\') |
| 305 | UnescapeCharToArg(&pos, end, true, &arg); |
| 306 | else |
| 307 | arg += c; |
| 308 | } |
| 309 | break; |
| 310 | case ' ': |
| 311 | switch (c) { |
| 312 | case '\'': |
| 313 | case '"': |
| 314 | state = c; |
| 315 | break; |
| 316 | case '\\': |
| 317 | UnescapeCharToArg(&pos, end, false, &arg); |
| 318 | state = 'a'; |
| 319 | break; |
| 320 | default: |