To: vim_dev@googlegroups.com Subject: Patch 9.0.0648 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.0648 Problem: When using powershell input redirection does not work. Solution: Use a different shell command for powershell. (Yegappan Lakshmanan, closes #11257) Files: src/ex_cmds.c, src/testdir/test_shell.vim *** ../vim-9.0.0647/src/ex_cmds.c 2022-09-19 18:20:03.795886974 +0100 --- src/ex_cmds.c 2022-10-03 16:03:30.737928715 +0100 *************** *** 1531,1586 **** { char_u *buf; long_u len; ! ! #if defined(UNIX) int is_fish_shell; ! char_u *shell_name = get_isolated_shell_name(); if (shell_name == NULL) return NULL; // Account for fish's different syntax for subshells ! is_fish_shell = (fnamecmp(shell_name, "fish") == 0); ! vim_free(shell_name); if (is_fish_shell) len = (long_u)STRLEN(cmd) + 13; // "begin; " + "; end" + NUL else #endif ! len = (long_u)STRLEN(cmd) + 3; // "()" + NUL if (itmp != NULL) ! len += (long_u)STRLEN(itmp) + 9; // " { < " + " } " if (otmp != NULL) len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; // " " buf = alloc(len); if (buf == NULL) return NULL; ! #if defined(UNIX) ! /* ! * Put braces around the command (for concatenated commands) when ! * redirecting input and/or output. ! */ ! if (itmp != NULL || otmp != NULL) { ! if (is_fish_shell) ! vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd); else ! vim_snprintf((char *)buf, len, "(%s)", (char *)cmd); } else - STRCPY(buf, cmd); - if (itmp != NULL) - { - STRCAT(buf, " < "); - STRCAT(buf, itmp); - } - #else - // For shells that don't understand braces around commands, at least allow - // the use of commands in a pipe. - if (*p_sxe != NUL && *p_sxq == '(') { if (itmp != NULL || otmp != NULL) ! vim_snprintf((char *)buf, len, "(%s)", (char *)cmd); else STRCPY(buf, cmd); if (itmp != NULL) --- 1531,1598 ---- { char_u *buf; long_u len; ! int is_powershell = FALSE; ! #ifdef UNIX int is_fish_shell; ! #endif + char_u *shell_name = get_isolated_shell_name(); if (shell_name == NULL) return NULL; + #if defined(UNIX) // Account for fish's different syntax for subshells ! is_fish_shell = fnamecmp(shell_name, "fish") == 0; if (is_fish_shell) len = (long_u)STRLEN(cmd) + 13; // "begin; " + "; end" + NUL else #endif ! { ! is_powershell = (shell_name[0] == 'p') ! && (fnamecmp(shell_name, "powershell") == 0 ! || fnamecmp(shell_name, "powershell.exe") == 0 ! || fnamecmp(shell_name, "pwsh") == 0 ! || fnamecmp(shell_name, "pwsh.exe") == 0); ! len = (long_u)STRLEN(cmd) + 3; // "()" + NUL ! } ! if (itmp != NULL) ! { ! if (is_powershell) ! // "& { Get-Content " + " | & " + " }" ! len += (long_u)STRLEN(itmp) + 24; ! else ! len += (long_u)STRLEN(itmp) + 9; // " { < " + " } " ! } if (otmp != NULL) len += (long_u)STRLEN(otmp) + (long_u)STRLEN(p_srr) + 2; // " " + + vim_free(shell_name); + buf = alloc(len); if (buf == NULL) return NULL; ! if (is_powershell) { ! if (itmp != NULL) ! vim_snprintf((char *)buf, len, "& { Get-Content %s | & %s }", ! itmp, cmd); else ! vim_snprintf((char *)buf, len, "(%s)", cmd); } else { + #if defined(UNIX) + // Put braces around the command (for concatenated commands) when + // redirecting input and/or output. if (itmp != NULL || otmp != NULL) ! { ! if (is_fish_shell) ! vim_snprintf((char *)buf, len, "begin; %s; end", (char *)cmd); ! else ! vim_snprintf((char *)buf, len, "(%s)", (char *)cmd); ! } else STRCPY(buf, cmd); if (itmp != NULL) *************** *** 1588,1624 **** STRCAT(buf, " < "); STRCAT(buf, itmp); } ! } ! else ! { ! STRCPY(buf, cmd); ! if (itmp != NULL) { ! char_u *p; ! ! // 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) { ! p = find_pipe(buf); ! if (p != NULL) ! *p = NUL; } ! STRCAT(buf, " <"); // " < " causes problems on Amiga ! STRCAT(buf, itmp); ! if (*p_shq == NUL) { ! p = find_pipe(cmd); ! if (p != NULL) { ! STRCAT(buf, " "); // insert a space before the '|' for DOS ! STRCAT(buf, p); } } } - } #endif if (otmp != NULL) append_redir(buf, (int)len, p_srr, otmp); --- 1600,1652 ---- STRCAT(buf, " < "); STRCAT(buf, itmp); } ! #else ! // For shells that don't understand braces around commands, at least ! // allow the use of commands in a pipe. ! if (*p_sxe != NUL && *p_sxq == '(') { ! if (itmp != NULL || otmp != NULL) ! vim_snprintf((char *)buf, len, "(%s)", (char *)cmd); ! else ! STRCPY(buf, cmd); ! if (itmp != NULL) { ! STRCAT(buf, " < "); ! STRCAT(buf, itmp); } ! } ! else ! { ! STRCPY(buf, cmd); ! if (itmp != NULL) { ! char_u *p; ! ! // 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) { ! p = find_pipe(buf); ! if (p != NULL) ! *p = NUL; ! } ! STRCAT(buf, " <"); // " < " causes problems on Amiga ! STRCAT(buf, itmp); ! if (*p_shq == NUL) ! { ! p = find_pipe(cmd); ! if (p != NULL) ! { ! // insert a space before the '|' for DOS ! STRCAT(buf, " "); ! STRCAT(buf, p); ! } } } } #endif + } if (otmp != NULL) append_redir(buf, (int)len, p_srr, otmp); *** ../vim-9.0.0647/src/testdir/test_shell.vim 2022-09-08 12:27:58.285556514 +0100 --- src/testdir/test_shell.vim 2022-10-03 15:55:06.884066490 +0100 *************** *** 97,102 **** --- 97,114 ---- finally bwipe! endtry + + " filter buffer contents through an external command + new + call setline(1, ['tom', 'sam', 'andy']) + try + %!sort + call assert_equal(['andy', 'sam', 'tom'], getline(1, '$'), e[0]) + catch + call assert_report($'Failed to filter buffer contents, shell: {e[0]}, caught {v:exception}') + finally + bwipe! + endtry endif endfor set shell& shellcmdflag& shellpipe& shellquote& *** ../vim-9.0.0647/src/version.c 2022-10-03 15:27:30.070072144 +0100 --- src/version.c 2022-10-03 15:57:22.823807311 +0100 *************** *** 701,702 **** --- 701,704 ---- { /* Add new patch number below this line */ + /**/ + 648, /**/ -- hundred-and-one symptoms of being an internet addict: 259. When you enter your name in the AltaVista search engine, the top ten matches do indeed refer to you. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///