Skip to content

Commit 4d15d3c

Browse files
committed
shellIntegration-rc.zsh: implement value-escaping for P and E codes
1 parent cc55ba8 commit 4d15d3c

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/vs/workbench/contrib/terminal/browser/media/shellIntegration-rc.zsh

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,40 @@ if [ -z "$VSCODE_SHELL_INTEGRATION" ]; then
3434
builtin return
3535
fi
3636

37+
# The property (P) and command (E) codes embed values which require escaping.
38+
# Backslashes are doubled. Non-alphanumeric characters are converted to escaped hex.
39+
__vsc_escape_value() {
40+
builtin emulate -L zsh
41+
42+
# Process text byte by byte, not by codepoint.
43+
builtin local LC_ALL=C str="$1" i byte token out=''
44+
45+
for (( i = 0; i < ${#str}; ++i )); do
46+
byte="${str:$i:1}"
47+
48+
# Backslashes must be doubled.
49+
if [ "$byte" = "\\" ]; then
50+
token="\\\\"
51+
# Conservatively pass alphanumerics through.
52+
elif [[ "$byte" == [0-9A-Za-z] ]]; then
53+
token="$byte"
54+
# Hex-encode anything else.
55+
# (Importantly including: semicolon, newline, and control chars).
56+
else
57+
token="\\x${(l:2::0:)$(( [##16] #byte ))}"
58+
# │ │ │ └┬┘└┬┘ └─┬─┘
59+
# │ │ │ │ │ │
60+
# left-pad ──╯ │ │ │ │ ╰─ the byte value of the character
61+
# two digits ──╯ │ │ ╰────── in hexadecimal
62+
# with '0' ───────╯ ╰───────── with no prefix
63+
fi
64+
65+
out+="$token"
66+
done
67+
68+
builtin print -r "$out"
69+
}
70+
3771
__vsc_in_command_execution="1"
3872
__vsc_current_command=""
3973

@@ -46,13 +80,12 @@ __vsc_prompt_end() {
4680
}
4781

4882
__vsc_update_cwd() {
49-
builtin printf '\e]633;P;Cwd=%s\a' "$PWD"
83+
builtin printf '\e]633;P;Cwd=%s\a' "$(__vsc_escape_value "${PWD}")"
5084
}
5185

5286
__vsc_command_output_start() {
5387
builtin printf '\e]633;C\a'
54-
# Send command line, escaping printf format chars %
55-
builtin printf '\e]633;E;%s\a' "$__vsc_current_command"
88+
builtin printf '\e]633;E;%s\a' "$(__vsc_escape_value "${__vsc_current_command}")"
5689
}
5790

5891
__vsc_continuation_start() {

0 commit comments

Comments
 (0)