win: jobstart(), system(): $PATHEXT-resolve exe

Windows: In order for jobstart(['foo']), system(['foo']) to find
"foo.cmd", we must replace "foo" with "foo.cmd" before sending `argv` to
process_spawn().

Rationale: jobstart([…]), system([…]) "executable" semantics should be
consistent with the VimL executable() function.

fix #9569
related: #10554
This commit is contained in:
Justin M. Keyes 2019-07-21 16:37:07 +02:00
parent 9d0f8224c9
commit b08dc3ec19
4 changed files with 28 additions and 9 deletions

View File

@ -12084,10 +12084,11 @@ static void f_jobresize(typval_T *argvars, typval_T *rettv, FunPtr fptr)
/// @param[out] executable Returns `false` if argv[0] is not executable. /// @param[out] executable Returns `false` if argv[0] is not executable.
/// ///
/// @returns Result of `shell_build_argv()` if `cmd_tv` is a String. /// @returns Result of `shell_build_argv()` if `cmd_tv` is a String.
/// Else, string values of `cmd_tv` copied to a (char **) list. /// Else, string values of `cmd_tv` copied to a (char **) list with
/// argv[0] resolved to full path ($PATHEXT-resolved on Windows).
static char **tv_to_argv(typval_T *cmd_tv, const char **cmd, bool *executable) static char **tv_to_argv(typval_T *cmd_tv, const char **cmd, bool *executable)
{ {
if (cmd_tv->v_type == VAR_STRING) { if (cmd_tv->v_type == VAR_STRING) { // String => "shell semantics".
const char *cmd_str = tv_get_string(cmd_tv); const char *cmd_str = tv_get_string(cmd_tv);
if (cmd) { if (cmd) {
*cmd = cmd_str; *cmd = cmd_str;
@ -12107,16 +12108,17 @@ static char **tv_to_argv(typval_T *cmd_tv, const char **cmd, bool *executable)
return NULL; return NULL;
} }
const char *exe = tv_get_string_chk(TV_LIST_ITEM_TV(tv_list_first(argl))); const char *arg0 = tv_get_string_chk(TV_LIST_ITEM_TV(tv_list_first(argl)));
if (!exe || !os_can_exe((const char_u *)exe, NULL, true)) { char_u *exe_resolved = NULL;
if (exe && executable) { if (!arg0 || !os_can_exe((const char_u *)arg0, &exe_resolved, true)) {
if (arg0 && executable) {
*executable = false; *executable = false;
} }
return NULL; return NULL;
} }
if (cmd) { if (cmd) {
*cmd = exe; *cmd = exe_resolved;
} }
// Build the argument vector // Build the argument vector
@ -12127,10 +12129,15 @@ static char **tv_to_argv(typval_T *cmd_tv, const char **cmd, bool *executable)
if (!a) { if (!a) {
// Did emsg in tv_get_string_chk; just deallocate argv. // Did emsg in tv_get_string_chk; just deallocate argv.
shell_free_argv(argv); shell_free_argv(argv);
xfree(exe_resolved);
return NULL; return NULL;
} }
argv[i++] = xstrdup(a); argv[i++] = xstrdup(a);
}); });
// Replace argv[0] with absolute path. The only reason for this is to make
// $PATHEXT work on Windows with jobstart([…]). #9569
xfree(argv[0]);
argv[0] = exe_resolved;
return argv; return argv;
} }

View File

@ -230,7 +230,7 @@ int os_exepath(char *buffer, size_t *size)
/// Checks if the file `name` is executable. /// Checks if the file `name` is executable.
/// ///
/// @param[in] name Filename to check. /// @param[in] name Filename to check.
/// @param[out] abspath Returns resolved executable path, if not NULL. /// @param[out,allocated] abspath Returns resolved exe path, if not NULL.
/// @param[in] use_path Also search $PATH. /// @param[in] use_path Also search $PATH.
/// ///
/// @return true if `name` is executable and /// @return true if `name` is executable and
@ -271,6 +271,9 @@ bool os_can_exe(const char_u *name, char_u **abspath, bool use_path)
} }
/// Returns true if `name` is an executable file. /// Returns true if `name` is an executable file.
///
/// @param[in] name Filename to check.
/// @param[out,allocated] abspath Returns full exe path, if not NULL.
static bool is_executable(const char *name, char_u **abspath) static bool is_executable(const char *name, char_u **abspath)
FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_NONNULL_ARG(1)
{ {

View File

@ -2,6 +2,7 @@ local helpers = require('test.functional.helpers')(after_each)
local eq, clear, call, iswin, write_file, command = local eq, clear, call, iswin, write_file, command =
helpers.eq, helpers.clear, helpers.call, helpers.iswin, helpers.write_file, helpers.eq, helpers.clear, helpers.call, helpers.iswin, helpers.write_file,
helpers.command helpers.command
local eval = helpers.eval
describe('executable()', function() describe('executable()', function()
before_each(clear) before_each(clear)
@ -95,10 +96,16 @@ describe('executable() (Windows)', function()
eq(0, call('executable', '.\\test_executable_zzz')) eq(0, call('executable', '.\\test_executable_zzz'))
end) end)
it('system([…]), jobstart([…]) use $PATHEXT #9569', function()
-- Invoking `cmdscript` should find/execute `cmdscript.cmd`.
eq('much success\n', call('system', {'test/functional/fixtures/cmdscript'}))
assert(0 < call('jobstart', {'test/functional/fixtures/cmdscript'}))
end)
it('full path with extension', function() it('full path with extension', function()
-- Some executable we can expect in the test env. -- Some executable we can expect in the test env.
local exe = 'printargs-test' local exe = 'printargs-test'
local exedir = helpers.eval("fnamemodify(v:progpath, ':h')") local exedir = eval("fnamemodify(v:progpath, ':h')")
local exepath = exedir..'/'..exe..'.exe' local exepath = exedir..'/'..exe..'.exe'
eq(1, call('executable', exepath)) eq(1, call('executable', exepath))
eq('arg1=lemon;arg2=sky;arg3=tree;', eq('arg1=lemon;arg2=sky;arg3=tree;',
@ -108,7 +115,7 @@ describe('executable() (Windows)', function()
it('full path without extension', function() it('full path without extension', function()
-- Some executable we can expect in the test env. -- Some executable we can expect in the test env.
local exe = 'printargs-test' local exe = 'printargs-test'
local exedir = helpers.eval("fnamemodify(v:progpath, ':h')") local exedir = eval("fnamemodify(v:progpath, ':h')")
local exepath = exedir..'/'..exe local exepath = exedir..'/'..exe
eq('arg1=lemon;arg2=sky;arg3=tree;', eq('arg1=lemon;arg2=sky;arg3=tree;',
call('system', exepath..' lemon sky tree')) call('system', exepath..' lemon sky tree'))

View File

@ -0,0 +1,2 @@
@echo off
echo much success