OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/install_static/install_util.h" | 5 #include "chrome/install_static/install_util.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <assert.h> | 8 #include <assert.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 bool trim_spaces) { | 574 bool trim_spaces) { |
575 return TokenizeStringT<std::string>(str, delimiter, trim_spaces); | 575 return TokenizeStringT<std::string>(str, delimiter, trim_spaces); |
576 } | 576 } |
577 | 577 |
578 std::vector<std::wstring> TokenizeString16(const std::wstring& str, | 578 std::vector<std::wstring> TokenizeString16(const std::wstring& str, |
579 wchar_t delimiter, | 579 wchar_t delimiter, |
580 bool trim_spaces) { | 580 bool trim_spaces) { |
581 return TokenizeStringT<std::wstring>(str, delimiter, trim_spaces); | 581 return TokenizeStringT<std::wstring>(str, delimiter, trim_spaces); |
582 } | 582 } |
583 | 583 |
| 584 std::vector<std::wstring> TokenizeCommandLineToArray( |
| 585 const std::wstring& command_line) { |
| 586 // This is baroquely complex to do properly, see e.g. |
| 587 // https://blogs.msdn.microsoft.com/oldnewthing/20100917-00/?p=12833 |
| 588 // http://www.windowsinspired.com/how-a-windows-programs-splits-its-command-li
ne-into-individual-arguments/ |
| 589 // and many others. We cannot use CommandLineToArgvW() in chrome_elf, because |
| 590 // it's in shell32.dll. Previously, __wgetmainargs() in the CRT was available, |
| 591 // and it's still documented for VS 2015 at |
| 592 // https://msdn.microsoft.com/en-us/library/ff770599.aspx but unfortunately, |
| 593 // isn't actually available. |
| 594 // |
| 595 // This parsing matches CommandLineToArgvW()s for arguments, rather than the |
| 596 // CRTs. These are different only in the most obscure of cases and will not |
| 597 // matter in any practical situation. See the windowsinspired.com post above |
| 598 // for details. |
| 599 // |
| 600 // Indicates whether or not space and tab are interpreted as token separators. |
| 601 enum class SpecialChars { |
| 602 // Space or tab, if encountered, delimit tokens. |
| 603 kInterpret, |
| 604 |
| 605 // Space or tab, if encountered, are part of the current token. |
| 606 kIgnore, |
| 607 } state; |
| 608 |
| 609 static constexpr wchar_t kSpaceTab[] = L" \t"; |
| 610 |
| 611 std::vector<std::wstring> result; |
| 612 const wchar_t* p = command_line.c_str(); |
| 613 |
| 614 // The first argument (the program) is delimited by whitespace or quotes based |
| 615 // on its first character. |
| 616 int argv0_length = 0; |
| 617 if (p[0] == L'"') |
| 618 argv0_length = wcschr(++p, L'"') - (command_line.c_str() + 1); |
| 619 else |
| 620 argv0_length = wcscspn(p, kSpaceTab); |
| 621 result.emplace_back(p, argv0_length); |
| 622 if (p[argv0_length] == 0) |
| 623 return result; |
| 624 p += argv0_length + 1; |
| 625 |
| 626 std::wstring token; |
| 627 // This loops the entire string, with a subloop for each argument. |
| 628 for (;;) { |
| 629 // Advance past leading whitespace (only space and tab are handled). |
| 630 p += wcsspn(p, kSpaceTab); |
| 631 |
| 632 // End of arguments. |
| 633 if (p[0] == 0) { |
| 634 if (!token.empty()) |
| 635 result.push_back(token); |
| 636 break; |
| 637 } |
| 638 |
| 639 state = SpecialChars::kInterpret; |
| 640 |
| 641 // Scan an argument. |
| 642 for (;;) { |
| 643 // Count and advance past collections of backslashes, which have special |
| 644 // meaning when followed by a double quote. |
| 645 int num_backslashes = wcsspn(p, L"\\"); |
| 646 p += num_backslashes; |
| 647 |
| 648 if (p[0] == L'"') { |
| 649 // Emit a backslash for each pair of backslashes found. A non-paired |
| 650 // "extra" backslash is handled below. |
| 651 token.append(num_backslashes / 2, L'\\'); |
| 652 |
| 653 if (num_backslashes % 2 == 1) { |
| 654 // An odd number of backslashes followed by a quote is treated as |
| 655 // pairs of protected backslashes, followed by the protected quote. |
| 656 token += L'"'; |
| 657 } else if (p[1] == L'"' && state == SpecialChars::kIgnore) { |
| 658 // Special case for consecutive double quotes within a quoted string: |
| 659 // emit one for the pair, and switch back to interpreting special |
| 660 // characters. |
| 661 ++p; |
| 662 token += L'"'; |
| 663 state = SpecialChars::kInterpret; |
| 664 } else { |
| 665 state = state == SpecialChars::kInterpret ? SpecialChars::kIgnore |
| 666 : SpecialChars::kInterpret; |
| 667 } |
| 668 } else { |
| 669 // Emit backslashes that do not precede a quote verbatim. |
| 670 token.append(num_backslashes, L'\\'); |
| 671 if (p[0] == 0 || |
| 672 (state == SpecialChars::kInterpret && wcschr(kSpaceTab, p[0]))) { |
| 673 result.push_back(token); |
| 674 token.clear(); |
| 675 break; |
| 676 } |
| 677 |
| 678 token += *p; |
| 679 } |
| 680 |
| 681 ++p; |
| 682 } |
| 683 } |
| 684 |
| 685 return result; |
| 686 } |
| 687 |
584 std::wstring GetSwitchValueFromCommandLine(const std::wstring& command_line, | 688 std::wstring GetSwitchValueFromCommandLine(const std::wstring& command_line, |
585 const std::wstring& switch_name) { | 689 const std::wstring& switch_name) { |
586 assert(!command_line.empty()); | 690 assert(!command_line.empty()); |
587 assert(!switch_name.empty()); | 691 assert(!switch_name.empty()); |
588 | 692 |
589 std::wstring command_line_copy = command_line; | 693 std::vector<std::wstring> as_array = TokenizeCommandLineToArray(command_line); |
590 // Remove leading and trailing spaces. | 694 std::wstring switch_with_equal = L"--" + switch_name + L"="; |
591 TrimT<std::wstring>(&command_line_copy); | 695 for (size_t i = 1; i < as_array.size(); ++i) { |
| 696 const std::wstring& arg = as_array[i]; |
| 697 if (arg.compare(0, switch_with_equal.size(), switch_with_equal) == 0) |
| 698 return arg.substr(switch_with_equal.size()); |
| 699 } |
592 | 700 |
593 // Find the switch in the command line. If we don't find the switch, return | 701 return std::wstring(); |
594 // an empty string. | |
595 std::wstring switch_token = L"--"; | |
596 switch_token += switch_name; | |
597 switch_token += L"="; | |
598 size_t switch_offset = command_line_copy.find(switch_token); | |
599 if (switch_offset == std::string::npos) | |
600 return std::wstring(); | |
601 | |
602 // The format is "--<switch name>=blah". Look for a space after the | |
603 // "--<switch name>=" string. If we don't find a space assume that the switch | |
604 // value ends at the end of the command line. | |
605 size_t switch_value_start_offset = switch_offset + switch_token.length(); | |
606 if (std::wstring(kWhiteSpaces16).find( | |
607 command_line_copy[switch_value_start_offset]) != std::wstring::npos) { | |
608 switch_value_start_offset = command_line_copy.find_first_not_of( | |
609 GetWhiteSpacesForType<std::wstring>(), switch_value_start_offset); | |
610 if (switch_value_start_offset == std::wstring::npos) | |
611 return std::wstring(); | |
612 } | |
613 size_t switch_value_end_offset = | |
614 command_line_copy.find_first_of(GetWhiteSpacesForType<std::wstring>(), | |
615 switch_value_start_offset); | |
616 if (switch_value_end_offset == std::wstring::npos) | |
617 switch_value_end_offset = command_line_copy.length(); | |
618 | |
619 std::wstring switch_value = command_line_copy.substr( | |
620 switch_value_start_offset, | |
621 switch_value_end_offset - (switch_offset + switch_token.length())); | |
622 TrimT<std::wstring>(&switch_value); | |
623 return switch_value; | |
624 } | 702 } |
625 | 703 |
626 bool RecursiveDirectoryCreate(const std::wstring& full_path) { | 704 bool RecursiveDirectoryCreate(const std::wstring& full_path) { |
627 // If the path exists, we've succeeded if it's a directory, failed otherwise. | 705 // If the path exists, we've succeeded if it's a directory, failed otherwise. |
628 const wchar_t* full_path_str = full_path.c_str(); | 706 const wchar_t* full_path_str = full_path.c_str(); |
629 DWORD file_attributes = ::GetFileAttributes(full_path_str); | 707 DWORD file_attributes = ::GetFileAttributes(full_path_str); |
630 if (file_attributes != INVALID_FILE_ATTRIBUTES) { | 708 if (file_attributes != INVALID_FILE_ATTRIBUTES) { |
631 if ((file_attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { | 709 if ((file_attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
632 Trace(L"%hs( %ls directory exists )\n", __func__, full_path_str); | 710 Trace(L"%hs( %ls directory exists )\n", __func__, full_path_str); |
633 return true; | 711 return true; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 case ChannelStrategy::ADDITIONAL_PARAMETERS: | 761 case ChannelStrategy::ADDITIONAL_PARAMETERS: |
684 return ChannelFromAdditionalParameters(mode, system_level, multi_install); | 762 return ChannelFromAdditionalParameters(mode, system_level, multi_install); |
685 case ChannelStrategy::FIXED: | 763 case ChannelStrategy::FIXED: |
686 return mode.default_channel_name; | 764 return mode.default_channel_name; |
687 } | 765 } |
688 | 766 |
689 return std::wstring(); | 767 return std::wstring(); |
690 } | 768 } |
691 | 769 |
692 } // namespace install_static | 770 } // namespace install_static |
OLD | NEW |