Skip to content

Commit

Permalink
patch 8.2.3079: Powershell core not supported by default
Browse files Browse the repository at this point in the history
Problem:    Powershell core not supported by default.
Solution:   Set option defaults for "pwsh". (Mike Williams, closes #8481)
  • Loading branch information
Mike Williams authored and brammool committed Jun 30, 2021
1 parent 834193a commit a3d1b29
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 52 deletions.
16 changes: 9 additions & 7 deletions runtime/doc/eval.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9878,11 +9878,13 @@ sha256({string}) *sha256()*

shellescape({string} [, {special}]) *shellescape()*
Escape {string} for use as a shell command argument.
On MS-Windows, when the 'shell' contains powershell then it
will enclose {string} in single quotes and will double up all
internal single quotes. With other values for 'shell' when
'shellslash' is not set, it will enclose {string} in double
quotes and double all double quotes within {string}.
When the 'shell' contains powershell (MS-Windows) or pwsh
(MS-Windows, Linux, and MacOS) then it will enclose {string}
in single quotes and will double up all internal single
quotes.
On MS-Windows, when 'shellslash' is not set, it will enclose
{string} in double quotes and double all double quotes within
{string}.
Otherwise it will enclose {string} in single quotes and
replace all "'" with "'\''".

Expand Down Expand Up @@ -11042,8 +11044,8 @@ tempname() *tempname()* *temp-file-name*
:exe "redir > " . tmpfile
< For Unix, the file will be in a private directory |tempfile|.
For MS-Windows forward slashes are used when the 'shellslash'
option is set or when 'shellcmdflag' starts with '-' except
when when 'shell' contains powershell.
option is set, or when 'shellcmdflag' starts with '-' and
'shell' does not contain powershell or pwsh.


term_ functions are documented here: |terminal-function-details|
Expand Down
28 changes: 13 additions & 15 deletions runtime/doc/options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6633,6 +6633,8 @@ A jump table for the options with a short description can be found at |Q_op|.
"bash", "fish", "ash" or "dash" the default becomes "2>&1| tee". This
means that stderr is also included. Before using the 'shell' option a
path is removed, thus "/bin/sh" uses "sh".
For Unix and MS-Windows, when the 'shell' option is "pwsh" the default
becomes ">%s 2>&1" and the output is not echoed to the screen.
The initialization of this option is done after reading the ".vimrc"
and the other initializations, so that when the 'shell' option is set
there, the 'shellpipe' option changes automatically, unless it was
Expand All @@ -6648,19 +6650,15 @@ A jump table for the options with a short description can be found at |Q_op|.
security reasons.

*'shellquote'* *'shq'*
'shellquote' 'shq' string (default: ""; Win32, when 'shell' does not
contain powershell but contains "sh"
somewhere: "\"")
'shellquote' 'shq' string (default: "")
global
Quoting character(s), put around the command passed to the shell, for
the "!" and ":!" commands. The redirection is kept outside of the
quoting. See 'shellxquote' to include the redirection. It's
probably not useful to set both options.
This is an empty string by default. Only known to be useful for
third-party shells on MS-Windows-like systems, such as the MKS Korn
Shell or bash, where it should be "\"". The default is adjusted
according the value of 'shell', to reduce the need to set this option
by the user. See |dos-shell|.
Shell or bash, where it should be "\"". See |dos-shell|.
This option cannot be set from a |modeline| or in the |sandbox|, for
security reasons.

Expand All @@ -6677,12 +6675,12 @@ A jump table for the options with a short description can be found at |Q_op|.
The default is ">". For Unix, if the 'shell' option is "csh" or
"tcsh" during initializations, the default becomes ">&". If the
'shell' option is "sh", "ksh", "mksh", "pdksh", "zsh", "zsh-beta",
"bash" or "fish", the default becomes ">%s 2>&1". This means that
stderr is also included. For Win32, the Unix checks are done and
"bash", "fish", or "pwsh", the default becomes ">%s 2>&1". This means
that stderr is also included. For Win32, the Unix checks are done and
additionally "cmd" is checked for, which makes the default ">%s 2>&1",
and "powershell" is checked for which makes the default
"2>&1 | Out-File -Encoding default". Also, the same names with ".exe"
appended are checked for.
"2>&1 | Out-File -Encoding default" (see |dos-powershell|). Also, the
same names with ".exe" appended are checked for.
The initialization of this option is done after reading the ".vimrc"
and the other initializations, so that when the 'shell' option is set
there, the 'shellredir' option changes automatically unless it was
Expand All @@ -6697,7 +6695,7 @@ A jump table for the options with a short description can be found at |Q_op|.
global
{only for MS-Windows}
When set, a forward slash is used when expanding file names. This is
useful when a Unix-like shell is used instead of cmd.exe or
useful when a Unix-like shell is used instead of cmd.exe, pwsh.exe, or
powershell.exe. Backward slashes can still be typed, but they are
changed to forward slashes by Vim.
Note that setting or resetting this option has no effect for some
Expand Down Expand Up @@ -6767,10 +6765,10 @@ A jump table for the options with a short description can be found at |Q_op|.
then ')"' is appended.
When the value is '(' then also see 'shellxescape'.
This is an empty string by default on most systems, but is known to be
useful for on Win32 version, either for cmd.exe and powershell.exe
which automatically strips off the first and last quote on a command,
or 3rd-party shells such as the MKS Korn Shell or bash, where it
should be "\"". The default is adjusted according the value of
useful for on Win32 version, either for cmd.exe, powershell.exe, or
pwsh.exe which automatically strips off the first and last quote on a
command, or 3rd-party shells such as the MKS Korn Shell or bash, where
it should be "\"". The default is adjusted according the value of
'shell', to reduce the need to set this option by the user. See
|dos-shell|.
This option cannot be set from a |modeline| or in the |sandbox|, for
Expand Down
64 changes: 52 additions & 12 deletions runtime/doc/os_dos.txt
Original file line number Diff line number Diff line change
Expand Up @@ -304,40 +304,80 @@ Vim sets the 'shellcmdflag' and 'shellquote' or 'shellxquote' options will be
set as described above.

==============================================================================
10. PowerShell *dos-powershell*
10. PowerShell *dos-powershell* *dos-pwsh*

Vim also supports Windows PowerShell. If 'shell' has been set to
"powershell.exe" at startup then VIM sets 'shellcmdflag', 'shellxquote',
'shellpipe', and 'shellredir' options to the following values:
Vim supports PowerShell Desktop and PowerShell Core. PowerShell Desktop is
the version of PowerShell that is installed with Windows, while PowerShell
Core is a separate downloadable version that works cross-platform. To see
which version you are using then enter the following in a PowerShell prompt -
$PSVersionTable.PSEdition

If 'shell' includes "powershell" in the filename at startup then VIM sets
'shellcmdflag', 'shellxquote', 'shellpipe', and 'shellredir' options to the
following values:

'shellcmdflag' -Command
'shellxquote' "
'shellpipe' 2>&1 | Out-File -Encoding default
'shellredir' 2>&1 | Out-File -Encoding default

If 'shell' includes "pwsh" in the filename at startup then VIM sets
'shellcmdflag', 'shellxquote', 'shellpipe', and 'shellredir' options to the
following values:

'shellcmdflag' -c
'shellxquote' "
'shellpipe' >%s 2>&1
'shellredir' >%s 2>&1

If you find that PowerShell commands are taking a long time to run then try
setting 'shellcmdflag' to "-NoProfile -Command". Note this will prevent any
PowerShell environment setup by the profile from taking place.
with "-NoProfile" at the beginning of the 'shellcmdflag'. Note this will
prevent any PowerShell environment setup by the profile from taking place.

If you have problems running PowerShell scripts through the 'shell' then try
setting 'shellcmdflag' to "-ExecutionPolicy RemoteSigned -Command". See
online Windows documentation for more information on PowerShell Execution
Policy settings.
with "-ExecutionPolicy RemoteSigned -Command" at the beginning of
'shellcmdflag'. See online Windows documentation for more information on
PowerShell Execution Policy settings.

See |option-backslash| about including spaces in 'shellcmdflag' when using
multiple flags.

The 'shellpipe' and 'shellredir' option values re-encode the UTF-16le output
from Windows PowerShell to your currently configured console codepage. The
from PowerShell Desktop to your currently configured console codepage. The
output can be forced into a different encoding by changing "default" to one of
the following:

unicode - UTF-16le (default output from PowerShell 5.1)
bigendianunicode - UTF-16
utf8 - UTF-8
utf7 - UTF-7 (no-BOM)
utf7 - UTF-7 (no BOM)
utf32 - UTF-32
ascii - 7-bit ASCII character set
default - System's active code page (typically ANSI)
oem - System's current OEM code page

Note Multi-byte Unicode encodings include a leading BOM.
Note The abovce multi-byte Unicode encodings include a leading BOM unless
otherwise indicated.

By default PowerShell Core's output is UTF-8 encoded without a BOM. If you
want to force the output of PowerShell Core into a different encoding then set
'shellredir' and 'shellpipe' to "2>&1 | Out-File -Encoding encoding" where
encoding is one of the following:

ascii - 7-bit ASCII character set
bigendianunicode - UTF-16be
bigendianutf32 - UTF-32be
oem - System's current OEM code page
unicode - UTF-16le
utf7 - UTF-7
utf8 - UTF-8
utf8BOM - UTF-8, with BOM
utf8NoBOM - UTF-8, no BOM (default output from PowerShell Core)
utf32 - UTF-32

Since PowerShell Core 6.2, the Encoding parameter also supports specifying a
numeric ID of a registered code page (-Encoding 1251) or string names of
registered code pages (-Encoding "windows-1251"). The .NET documentation for
Encoding.CodePage has more information

vim:tw=78:ts=8:noet:ft=help:norl:
1 change: 1 addition & 0 deletions runtime/doc/os_win32.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ File formats |dos-file-formats|
Interrupting |dos-CTRL-Break|
Temp files |dos-temp-files|
Shell option default |dos-shell|
PowerShell defaults |dos-powershell|

Win32 GUI |gui-w32|

Expand Down
7 changes: 5 additions & 2 deletions src/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -5222,6 +5222,7 @@ vim_tempname(
WCHAR *chartab = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char_u *retval;
char_u *p;
char_u *shname;
long i;

wcscpy(itmp, L"");
Expand All @@ -5247,8 +5248,10 @@ vim_tempname(
// "sh". NOTE: This also checks 'shellcmdflag' to help those people who
// didn't set 'shellslash' but only if not using PowerShell.
retval = utf16_to_enc(itmp, NULL);
if ((strstr((char *)gettail(p_sh), "powershell") == NULL
&& *p_shcf == '-') || p_ssl)
shname = gettail(p_sh);
if ((*p_shcf == '-' && !(strstr((char *)shname, "powershell") != NULL
|| strstr((char *)shname, "pwsh") != NULL ))
|| p_ssl)
for (p = retval; *p; ++p)
if (*p == '\\')
*p = '/';
Expand Down
14 changes: 7 additions & 7 deletions src/misc2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1414,8 +1414,9 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
char_u *escaped_string;
int l;
int csh_like;
# ifdef MSWIN
char_u *shname;
int powershell;
# ifdef MSWIN
int double_quotes;
# endif

Expand All @@ -1425,9 +1426,12 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
// Csh also needs to have "\n" escaped twice when do_special is set.
csh_like = csh_like_shell();

// PowerShell uses it's own version for quoting single quotes
shname = gettail(p_sh);
powershell = strstr((char *)shname, "pwsh") != NULL;
# ifdef MSWIN
// PowerShell only accepts single quotes so override p_ssl.
powershell = strstr((char *)gettail(p_sh), "powershell") != NULL;
powershell = powershell || strstr((char *)shname, "powershell") != NULL;
// PowerShell only accepts single quotes so override shellslash.
double_quotes = !powershell && !p_ssl;
# endif

Expand All @@ -1445,11 +1449,9 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
# endif
if (*p == '\'')
{
# ifdef MSWIN
if (powershell)
length +=2; // ' => ''
else
# endif
length += 3; // ' => '\''
}
if ((*p == '\n' && (csh_like || do_newline))
Expand Down Expand Up @@ -1497,14 +1499,12 @@ vim_strsave_shellescape(char_u *string, int do_special, int do_newline)
# endif
if (*p == '\'')
{
# ifdef MSWIN
if (powershell)
{
*d++ = '\'';
*d++ = '\'';
}
else
# endif
{
*d++ = '\'';
*d++ = '\\';
Expand Down
12 changes: 9 additions & 3 deletions src/option.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,8 @@ set_init_3(void)
}
}
# ifdef MSWIN
// PowerShell 5.1/.NET outputs UTF-16 with BOM so re-encode to the
// current codepage
// Windows PowerShell output is UTF-16 with BOM so re-encode to the
// current codepage.
else if ( fnamecmp(p, "powershell") == 0
|| fnamecmp(p, "powershell.exe") == 0
)
Expand Down Expand Up @@ -965,6 +965,7 @@ set_init_3(void)
|| fnamecmp(p, "fish") == 0
|| fnamecmp(p, "ash") == 0
|| fnamecmp(p, "dash") == 0
|| fnamecmp(p, "pwsh") == 0
# ifdef MSWIN
|| fnamecmp(p, "cmd") == 0
|| fnamecmp(p, "sh.exe") == 0
Expand All @@ -976,6 +977,7 @@ set_init_3(void)
|| fnamecmp(p, "bash.exe") == 0
|| fnamecmp(p, "cmd.exe") == 0
|| fnamecmp(p, "dash.exe") == 0
|| fnamecmp(p, "pwsh.exe") == 0
# endif
)
{
Expand All @@ -985,7 +987,10 @@ set_init_3(void)
# ifdef MSWIN
p_sp = (char_u *)">%s 2>&1";
# else
p_sp = (char_u *)"2>&1| tee";
if (fnamecmp(p, "pwsh") == 0)
p_sp = (char_u *)">%s 2>&1";
else
p_sp = (char_u *)"2>&1| tee";
# endif
options[idx_sp].def_val[VI_DEFAULT] = p_sp;
}
Expand All @@ -1011,6 +1016,7 @@ set_init_3(void)
* p_shcf p_sxq
* cmd.exe - "/c" "("
* powershell.exe - "-Command" "\""
* pwsh.exe - "-c" "\""
* "sh" like shells - "-c" "\""
*
* For Win32 p_sxq is set instead of p_shq to include shell redirection.
Expand Down
7 changes: 5 additions & 2 deletions src/os_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -2135,15 +2135,18 @@ executable_exists(char *name, char_u **path, int use_path, int use_pathext)
char_u *pathbuf = NULL;
char_u *pathext = NULL;
char_u *pathextbuf = NULL;
char_u *shname = NULL;
int noext = FALSE;
int retval = FALSE;

if (len >= sizeof(buf)) // safety check
return FALSE;

// Using the name directly when a Unix-shell like 'shell'.
if (strstr((char *)gettail(p_sh), "powershell") == NULL
&& strstr((char *)gettail(p_sh), "sh") != NULL)
shname = gettail(p_sh);
if (strstr((char *)shname, "sh") != NULL &&
!(strstr((char *)shname, "powershell") != NULL
|| strstr((char *)shname, "pwsh") != NULL))
noext = TRUE;

if (use_pathext)
Expand Down
19 changes: 15 additions & 4 deletions src/testdir/test_shell.vim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func Test_shell_options()
\ ['ash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
\ ['dash', '-c', '2>&1| tee', '', '>%s 2>&1', '', ''],
\ ['csh', '-c', '|& tee', '', '>&', '', ''],
\ ['tcsh', '-c', '|& tee', '', '>&', '', '']]
\ ['tcsh', '-c', '|& tee', '', '>&', '', ''],
\ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '', '']]
endif
if has('win32')
let shells += [['cmd', '/c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', ''],
Expand All @@ -28,6 +29,8 @@ func Test_shell_options()
\ '', '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'],
\ ['powershell', '-Command', '2>&1 | Out-File -Encoding default', '',
\ '2>&1 | Out-File -Encoding default', '"&|<>()@^', '"'],
\ ['pwsh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
\ ['pwsh', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
\ ['sh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
\ ['ksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
\ ['mksh.exe', '-c', '>%s 2>&1', '', '>%s 2>&1', '"&|<>()@^', '"'],
Expand Down Expand Up @@ -61,6 +64,7 @@ func Test_shell_options()
let str1 = "'cmd \"arg1\" '\\''arg2'\\'' \\!%#'"
let str2 = "'cmd \"arg1\" '\\''arg2'\\'' \\\\!\\%\\#'"
elseif e[0] =~# '.*powershell$' || e[0] =~# '.*powershell.exe$'
\ || e[0] =~# '.*pwsh$' || e[0] =~# '.*pwsh.exe$'
let str1 = "'cmd \"arg1\" ''arg2'' !%#'"
let str2 = "'cmd \"arg1\" ''arg2'' \\!\\%\\#'"
else
Expand All @@ -76,9 +80,14 @@ func Test_shell_options()
let [&shellcmdflag, &shellpipe, &shellquote, &shellredir,
\ &shellxescape, &shellxquote] = e[1:6]
new
r !echo hello
call assert_equal('hello', substitute(getline(2), '\W', '', 'g'), e[0])
bwipe!
try
r !echo hello
call assert_equal('hello', substitute(getline(2), '\W', '', 'g'), e[0])
catch
call assert_report('Failed to run shell command, shell: ' .. e[0])
finally
bwipe!
endtry
endif
endfor
set shell& shellcmdflag& shellpipe& shellquote&
Expand Down Expand Up @@ -149,6 +158,8 @@ func Test_shellslash()
" ".*\\\\[^\\\\]*$"
let shells = [['cmd', '/c', '\\', '/'],
\ ['powershell', '-Command', '\\', '/'],
\ ['pwsh', '-Command', '\\', '/'],
\ ['pwsh', '-c', '\\', '/'],
\ ['sh', '-c', '/', '/']]
for e in shells
exe 'set shell=' .. e[0] .. ' | set shellcmdflag=' .. e[1]
Expand Down

0 comments on commit a3d1b29

Please sign in to comment.