Skip to content

Commit 7878728

Browse files
a4lgDHowett
authored andcommitted
Escape single quotes while translating dropped Win32 paths (#18007)
When file/folder is dropped to the terminal, its path is translated and quoted with a pair of single quotes if necessary. However, the terminal control does not escape single quotes (allowed in the Win32 subsystem) that need escapes when translated. On the translation styles other than `"none"` (note: all other translation styles are currently intended for the POSIX shell), it causes incorrect path to be pasted when the path contains one or more single quotes (see #18006 for an example). With this commit, the terminal control escapes a single quote with a valid escape sequence `'\''` (finish quote, print a single quote then begin quote again) when the path translation is required. * Changed escape sequence from `'"'"'` to much shorter `'\''`. * Reflected comments by the reviewer. * Overhaul after addition of multiple path translation styles (not just WSL but Cygwin and MSYS). * More clarification both in the code and in the commit message. * Minor clarification both in the code and in the commit message. * #18006 * #16214 * #18195 This is a follow-up of #16214 and #18195, fixing #18006. Closes #18006 (cherry picked from commit ae90d52) Service-Card-Id: PVTI_lADOAF3p4s4AmhmQzgVEvdU Service-Version: 1.22
1 parent ede23a5 commit 7878728

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/cascadia/TerminalControl/TermControl.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -3250,7 +3250,25 @@ namespace winrt::Microsoft::Terminal::Control::implementation
32503250
{
32513251
allPathsString.push_back(quotesChar);
32523252
}
3253-
allPathsString.append(fullPath);
3253+
if (isWSL)
3254+
{
3255+
// Fix quoted path for WSL
3256+
// Single quote is allowed on the Win32 subsystem and must be processed on WSL profiles.
3257+
// Note that we assume that all paths are quoted using a pair of single quotes for WSL.
3258+
std::wstring_view fullPathView{ fullPath };
3259+
size_t pos;
3260+
while ((pos = fullPathView.find(L'\'')) != std::wstring_view::npos)
3261+
{
3262+
allPathsString.append(fullPathView.begin(), fullPathView.begin() + pos);
3263+
allPathsString.append(L"'\\''");
3264+
fullPathView.remove_prefix(pos + 1);
3265+
}
3266+
allPathsString.append(fullPathView);
3267+
}
3268+
else
3269+
{
3270+
allPathsString.append(fullPath);
3271+
}
32543272
if (quotesNeeded)
32553273
{
32563274
allPathsString.push_back(quotesChar);

0 commit comments

Comments
 (0)