mirror of
				https://github.com/neovim/neovim.git
				synced 2025-11-04 01:34:25 +00:00 
			
		
		
		
	fix: make_filter_cmd for :! powershell
Problem: `Start-Process` requires the command to be split into the shell command and its arguments. Previously it was done by parsing, which didn't handle cases such as - commands with escaped space in their filepath - quoted commands with space in their filepath Solution: Use - `pwsh -Command` instead of `Start-Process` - `Get-Content` instead of `-RedirectStandardInput` - `Out-File` instead of `-RedirectStandardOutput`
This commit is contained in:
		@@ -5357,7 +5357,7 @@ A jump table for the options with a short description can be found at |Q_op|.
 | 
				
			|||||||
	To use PowerShell: >
 | 
						To use PowerShell: >
 | 
				
			||||||
		let &shell = executable('pwsh') ? 'pwsh' : 'powershell'
 | 
							let &shell = executable('pwsh') ? 'pwsh' : 'powershell'
 | 
				
			||||||
		let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command [Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;'
 | 
							let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command [Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;'
 | 
				
			||||||
		let &shellredir = '-RedirectStandardOutput %s -NoNewWindow -Wait'
 | 
							let &shellredir = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
 | 
				
			||||||
		let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
 | 
							let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
 | 
				
			||||||
		set shellquote= shellxquote=
 | 
							set shellquote= shellxquote=
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1544,84 +1544,71 @@ char *make_filter_cmd(char *cmd, char *itmp, char *otmp)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  size_t len = strlen(cmd) + 1;  // At least enough space for cmd + NULL.
 | 
					  size_t len = strlen(cmd) + 1;  // At least enough space for cmd + NULL.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  len += is_fish_shell ?  sizeof("begin; " "; end") - 1
 | 
					  len += is_fish_shell ? sizeof("begin; " "; end") - 1
 | 
				
			||||||
                       :  is_pwsh ? sizeof("Start-Process ")
 | 
					                       : !is_pwsh ? sizeof("(" ")") - 1
 | 
				
			||||||
                                  : sizeof("(" ")") - 1;
 | 
					                                  : 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (itmp != NULL) {
 | 
					  if (itmp != NULL) {
 | 
				
			||||||
    len += is_pwsh  ? strlen(itmp) + sizeof(" -RedirectStandardInput ")
 | 
					    len += is_pwsh  ? strlen(itmp) + sizeof("Get-Content " " | & ") - 1
 | 
				
			||||||
                    : strlen(itmp) + sizeof(" { " " < " " } ") - 1;
 | 
					                    : strlen(itmp) + sizeof(" { " " < " " } ") - 1;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  if (otmp != NULL) {
 | 
					  if (otmp != NULL) {
 | 
				
			||||||
    len += strlen(otmp) + strlen(p_srr) + 2;  // two extra spaces ("  "),
 | 
					    len += strlen(otmp) + strlen(p_srr) + 2;  // two extra spaces ("  "),
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const char *const cmd_args = strchr(cmd, ' ');
 | 
					 | 
				
			||||||
  len += (is_pwsh && cmd_args)
 | 
					 | 
				
			||||||
      ? strlen(" -ArgumentList ") + 2  // two extra quotes
 | 
					 | 
				
			||||||
      : 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  char *const buf = xmalloc(len);
 | 
					  char *const buf = xmalloc(len);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (is_pwsh) {
 | 
					  if (is_pwsh) {
 | 
				
			||||||
    xstrlcpy(buf, "Start-Process ", len);
 | 
					    if (itmp != NULL) {
 | 
				
			||||||
    if (cmd_args == NULL) {
 | 
					      xstrlcpy(buf, "Get-Content ", len - 1);  // FIXME: should we add "-Encoding utf8"?
 | 
				
			||||||
      xstrlcat(buf, cmd, len);
 | 
					      xstrlcat(buf, (const char *)itmp, len - 1);
 | 
				
			||||||
 | 
					      xstrlcat(buf, " | & ", len - 1);  // FIXME: add `&` ourself or leave to user?
 | 
				
			||||||
 | 
					      xstrlcat(buf, cmd, len - 1);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      xstrlcpy(buf + strlen(buf), cmd, (size_t)(cmd_args - cmd + 1));
 | 
					      xstrlcpy(buf, cmd, len - 1);
 | 
				
			||||||
      xstrlcat(buf, " -ArgumentList \"", len);
 | 
					 | 
				
			||||||
      xstrlcat(buf, cmd_args + 1, len);  // +1 to skip the leading space.
 | 
					 | 
				
			||||||
      xstrlcat(buf, "\"", len);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
#if defined(UNIX)
 | 
					#if defined(UNIX)
 | 
				
			||||||
    // Put delimiters around the command (for concatenated commands) when
 | 
					    // Put delimiters around the command (for concatenated commands) when
 | 
				
			||||||
    // redirecting input and/or output.
 | 
					    // redirecting input and/or output.
 | 
				
			||||||
  } else if (itmp != NULL || otmp != NULL) {
 | 
					    if (itmp != NULL || otmp != NULL) {
 | 
				
			||||||
    char *fmt = is_fish_shell ? "begin; %s; end"
 | 
					      char *fmt = is_fish_shell ? "begin; %s; end"
 | 
				
			||||||
                              :       "(%s)";
 | 
					        :       "(%s)";
 | 
				
			||||||
    vim_snprintf(buf, len, fmt, cmd);
 | 
					      vim_snprintf(buf, len, fmt, cmd);
 | 
				
			||||||
#endif
 | 
					    } else {
 | 
				
			||||||
 | 
					      xstrlcpy(buf, cmd, len);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (itmp != NULL) {
 | 
				
			||||||
 | 
					      xstrlcat(buf, " < ", len - 1);
 | 
				
			||||||
 | 
					      xstrlcat(buf, (const char *)itmp, len - 1);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
    // For shells that don't understand braces around commands, at least allow
 | 
					    // For shells that don't understand braces around commands, at least allow
 | 
				
			||||||
    // the use of commands in a pipe.
 | 
					    // the use of commands in a pipe.
 | 
				
			||||||
  } else {
 | 
					    xstrlcpy(buf, (char *)cmd, len);
 | 
				
			||||||
    xstrlcpy(buf, cmd, len);
 | 
					    if (itmp != NULL) {
 | 
				
			||||||
  }
 | 
					      // If there is a pipe, we have to put the '<' in front of it.
 | 
				
			||||||
 | 
					      // Don't do this when 'shellquote' is not empty, otherwise the
 | 
				
			||||||
#if defined(UNIX)
 | 
					      // redirection would be inside the quotes.
 | 
				
			||||||
  if (itmp != NULL) {
 | 
					      if (*p_shq == NUL) {
 | 
				
			||||||
    if (is_pwsh) {
 | 
					        char *const p = find_pipe(buf);
 | 
				
			||||||
      xstrlcat(buf, " -RedirectStandardInput ", len - 1);
 | 
					        if (p != NULL) {
 | 
				
			||||||
    } else {
 | 
					          *p = NUL;
 | 
				
			||||||
      xstrlcat(buf, " < ", len - 1);
 | 
					        }
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    xstrlcat(buf, itmp, len - 1);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
  if (itmp != NULL) {
 | 
					 | 
				
			||||||
    // If there is a pipe, we have to put the '<' in front of it.
 | 
					 | 
				
			||||||
    // Don't do this when 'shellquote' is not empty, otherwise the
 | 
					 | 
				
			||||||
    // redirection would be inside the quotes.
 | 
					 | 
				
			||||||
    if (*p_shq == NUL) {
 | 
					 | 
				
			||||||
      char *const p = find_pipe(buf);
 | 
					 | 
				
			||||||
      if (p != NULL) {
 | 
					 | 
				
			||||||
        *p = NUL;
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    if (is_pwsh) {
 | 
					 | 
				
			||||||
      xstrlcat(buf, " -RedirectStandardInput ", len);
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      xstrlcat(buf, " < ", len);
 | 
					      xstrlcat(buf, " < ", len);
 | 
				
			||||||
    }
 | 
					      xstrlcat(buf, (const char *)itmp, len);
 | 
				
			||||||
    xstrlcat(buf, itmp, len);
 | 
					      if (*p_shq == NUL) {
 | 
				
			||||||
    if (*p_shq == NUL) {
 | 
					        const char *const p = find_pipe((const char *)cmd);
 | 
				
			||||||
      const char *const p = find_pipe(cmd);
 | 
					        if (p != NULL) {
 | 
				
			||||||
      if (p != NULL) {
 | 
					          xstrlcat(buf, " ", len - 1);  // Insert a space before the '|' for DOS
 | 
				
			||||||
        xstrlcat(buf, " ", len - 1);  // Insert a space before the '|' for DOS
 | 
					          xstrlcat(buf, p, len - 1);
 | 
				
			||||||
        xstrlcat(buf, p, len - 1);
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  if (otmp != NULL) {
 | 
					  if (otmp != NULL) {
 | 
				
			||||||
    append_redir(buf, len, p_srr, otmp);
 | 
					    append_redir(buf, len, p_srr, otmp);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -562,16 +562,16 @@ function module.set_shell_powershell(fake)
 | 
				
			|||||||
    assert(found)
 | 
					    assert(found)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
  local shell = found and (iswin() and 'powershell' or 'pwsh') or module.testprg('pwsh-test')
 | 
					  local shell = found and (iswin() and 'powershell' or 'pwsh') or module.testprg('pwsh-test')
 | 
				
			||||||
  local set_encoding = '[Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;'
 | 
					  local set_encoding = '[Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.UTF8Encoding]::new();'
 | 
				
			||||||
  local cmd = set_encoding..'Remove-Item -Force '..table.concat(iswin()
 | 
					  local cmd = set_encoding..'Remove-Item -Force '..table.concat(iswin()
 | 
				
			||||||
    and {'alias:cat', 'alias:echo', 'alias:sleep'}
 | 
					    and {'alias:cat', 'alias:echo', 'alias:sleep', 'alias:sort'}
 | 
				
			||||||
    or  {'alias:echo'}, ',')..';'
 | 
					    or  {'alias:echo'}, ',')..';'
 | 
				
			||||||
  module.exec([[
 | 
					  module.exec([[
 | 
				
			||||||
    let &shell = ']]..shell..[['
 | 
					    let &shell = ']]..shell..[['
 | 
				
			||||||
    set shellquote= shellxquote=
 | 
					    set shellquote= shellxquote=
 | 
				
			||||||
    let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command ]]..cmd..[['
 | 
					    let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command ]]..cmd..[['
 | 
				
			||||||
    let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
 | 
					    let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
 | 
				
			||||||
    let &shellredir = '-RedirectStandardOutput %s -NoNewWindow -Wait'
 | 
					    let &shellredir = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
 | 
				
			||||||
  ]])
 | 
					  ]])
 | 
				
			||||||
  return found
 | 
					  return found
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -642,12 +642,12 @@ describe('shell :!', function()
 | 
				
			|||||||
    if iswin() then
 | 
					    if iswin() then
 | 
				
			||||||
      feed(':4verbose %!sort /R<cr>')
 | 
					      feed(':4verbose %!sort /R<cr>')
 | 
				
			||||||
      screen:expect{
 | 
					      screen:expect{
 | 
				
			||||||
        any=[[Executing command: .?Start%-Process sort %-ArgumentList "/R" %-RedirectStandardInput .* %-RedirectStandardOutput .* %-NoNewWindow %-Wait]]
 | 
					        any=[[Executing command: .?Get%-Content .* | & sort /R 2>&1 | Out%-File %-Encoding UTF8 .*; exit $LastExitCode]]
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
      feed(':4verbose %!sort -r<cr>')
 | 
					      feed(':4verbose %!sort -r<cr>')
 | 
				
			||||||
      screen:expect{
 | 
					      screen:expect{
 | 
				
			||||||
        any=[[Executing command: .?Start%-Process sort %-ArgumentList "%-r" %-RedirectStandardInput .* %-RedirectStandardOutput .* %-NoNewWindow %-Wait]]
 | 
					        any=[[Executing command: .?Get%-Content .* | & sort %-r 2>&1 | Out%-File %-Encoding UTF8 .*; exit $LastExitCode]]
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
    feed('<CR>')
 | 
					    feed('<CR>')
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user