| 547 | } |
| 548 | |
| 549 | int kwsysProcess_AddCommand(kwsysProcess* cp, char const* const* command) |
| 550 | { |
| 551 | int newNumberOfCommands; |
| 552 | wchar_t** newCommands; |
| 553 | |
| 554 | /* Make sure we have a command to add. */ |
| 555 | if (!cp || !command || !*command) { |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | /* Allocate a new array for command pointers. */ |
| 560 | newNumberOfCommands = cp->NumberOfCommands + 1; |
| 561 | if (!(newCommands = |
| 562 | (wchar_t**)malloc(sizeof(wchar_t*) * newNumberOfCommands))) { |
| 563 | /* Out of memory. */ |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | /* Copy any existing commands into the new array. */ |
| 568 | { |
| 569 | int i; |
| 570 | for (i = 0; i < cp->NumberOfCommands; ++i) { |
| 571 | newCommands[i] = cp->Commands[i]; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (cp->Verbatim) { |
| 576 | /* Copy the verbatim command line into the buffer. */ |
| 577 | newCommands[cp->NumberOfCommands] = kwsysEncoding_DupToWide(*command); |
| 578 | } else { |
| 579 | /* Encode the arguments so CommandLineToArgvW can decode |
| 580 | them from the command line string in the child. */ |
| 581 | char buffer[32768]; /* CreateProcess max command-line length. */ |
| 582 | char* end = buffer + sizeof(buffer); |
| 583 | char* out = buffer; |
| 584 | char const* const* a; |
| 585 | for (a = command; *a; ++a) { |
| 586 | int quote = !**a; /* Quote the empty string. */ |
| 587 | int slashes = 0; |
| 588 | char const* c; |
| 589 | if (a != command && out != end) { |
| 590 | *out++ = ' '; |
| 591 | } |
| 592 | for (c = *a; !quote && *c; ++c) { |
| 593 | quote = (*c == ' ' || *c == '\t'); |
| 594 | } |
| 595 | if (quote && out != end) { |
| 596 | *out++ = '"'; |
| 597 | } |
| 598 | for (c = *a; *c; ++c) { |
| 599 | if (*c == '\\') { |
| 600 | ++slashes; |
| 601 | } else { |
| 602 | if (*c == '"') { |
| 603 | // Add n+1 backslashes to total 2n+1 before internal '"'. |
| 604 | while (slashes-- >= 0 && out != end) { |
| 605 | *out++ = '\\'; |
| 606 | } |
no test coverage detected
searching dependent graphs…