mirror of
https://github.com/neovim/neovim.git
synced 2025-12-11 17:12:40 +00:00
Merge #10561 from justinmk/os_can_exe
jobstart(), system(): use $PATHEXT-resolved exe
This commit is contained in:
@@ -8466,9 +8466,9 @@ static void f_executable(typval_T *argvars, typval_T *rettv, FunPtr fptr)
|
|||||||
|
|
||||||
// Check in $PATH and also check directly if there is a directory name
|
// Check in $PATH and also check directly if there is a directory name
|
||||||
rettv->vval.v_number = (
|
rettv->vval.v_number = (
|
||||||
os_can_exe((const char_u *)name, NULL, true)
|
os_can_exe(name, NULL, true)
|
||||||
|| (gettail_dir(name) != name
|
|| (gettail_dir(name) != name
|
||||||
&& os_can_exe((const char_u *)name, NULL, false)));
|
&& os_can_exe(name, NULL, false)));
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -8573,12 +8573,12 @@ static void f_execute(typval_T *argvars, typval_T *rettv, FunPtr fptr)
|
|||||||
static void f_exepath(typval_T *argvars, typval_T *rettv, FunPtr fptr)
|
static void f_exepath(typval_T *argvars, typval_T *rettv, FunPtr fptr)
|
||||||
{
|
{
|
||||||
const char *arg = tv_get_string(&argvars[0]);
|
const char *arg = tv_get_string(&argvars[0]);
|
||||||
char_u *path = NULL;
|
char *path = NULL;
|
||||||
|
|
||||||
(void)os_can_exe((const char_u *)arg, &path, true);
|
(void)os_can_exe(arg, &path, true);
|
||||||
|
|
||||||
rettv->v_type = VAR_STRING;
|
rettv->v_type = VAR_STRING;
|
||||||
rettv->vval.v_string = path;
|
rettv->vval.v_string = (char_u *)path;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Find a window: When using a Window ID in any tab page, when using a number
|
/// Find a window: When using a Window ID in any tab page, when using a number
|
||||||
@@ -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 *exe_resolved = NULL;
|
||||||
if (exe && executable) {
|
if (!arg0 || !os_can_exe(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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -239,10 +239,10 @@ int os_exepath(char *buffer, size_t *size)
|
|||||||
/// - is absolute.
|
/// - is absolute.
|
||||||
///
|
///
|
||||||
/// @return `false` otherwise.
|
/// @return `false` otherwise.
|
||||||
bool os_can_exe(const char_u *name, char_u **abspath, bool use_path)
|
bool os_can_exe(const char *name, char **abspath, bool use_path)
|
||||||
FUNC_ATTR_NONNULL_ARG(1)
|
FUNC_ATTR_NONNULL_ARG(1)
|
||||||
{
|
{
|
||||||
bool no_path = !use_path || path_is_absolute(name);
|
bool no_path = !use_path || path_is_absolute((char_u *)name);
|
||||||
// If the filename is "qualified" (relative or absolute) do not check $PATH.
|
// If the filename is "qualified" (relative or absolute) do not check $PATH.
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
no_path |= (name[0] == '.'
|
no_path |= (name[0] == '.'
|
||||||
@@ -255,11 +255,11 @@ bool os_can_exe(const char_u *name, char_u **abspath, bool use_path)
|
|||||||
|
|
||||||
if (no_path) {
|
if (no_path) {
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
if (is_executable_ext((char *)name, abspath)) {
|
if (is_executable_ext(name, abspath)) {
|
||||||
#else
|
#else
|
||||||
// Must have path separator, cannot execute files in the current directory.
|
// Must have path separator, cannot execute files in the current directory.
|
||||||
if ((const char_u *)gettail_dir((const char *)name) != name
|
if (gettail_dir(name) != name
|
||||||
&& is_executable((char *)name, abspath)) {
|
&& is_executable(name, abspath)) {
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
@@ -271,10 +271,13 @@ 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.
|
||||||
static bool is_executable(const char *name, char_u **abspath)
|
///
|
||||||
|
/// @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 **abspath)
|
||||||
FUNC_ATTR_NONNULL_ARG(1)
|
FUNC_ATTR_NONNULL_ARG(1)
|
||||||
{
|
{
|
||||||
int32_t mode = os_getperm((const char *)name);
|
int32_t mode = os_getperm(name);
|
||||||
|
|
||||||
if (mode < 0) {
|
if (mode < 0) {
|
||||||
return false;
|
return false;
|
||||||
@@ -292,7 +295,7 @@ static bool is_executable(const char *name, char_u **abspath)
|
|||||||
const bool ok = (r == 0);
|
const bool ok = (r == 0);
|
||||||
#endif
|
#endif
|
||||||
if (ok && abspath != NULL) {
|
if (ok && abspath != NULL) {
|
||||||
*abspath = save_abs_path((char_u *)name);
|
*abspath = save_abs_path(name);
|
||||||
}
|
}
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
@@ -301,7 +304,7 @@ static bool is_executable(const char *name, char_u **abspath)
|
|||||||
/// Checks if file `name` is executable under any of these conditions:
|
/// Checks if file `name` is executable under any of these conditions:
|
||||||
/// - extension is in $PATHEXT and `name` is executable
|
/// - extension is in $PATHEXT and `name` is executable
|
||||||
/// - result of any $PATHEXT extension appended to `name` is executable
|
/// - result of any $PATHEXT extension appended to `name` is executable
|
||||||
static bool is_executable_ext(char *name, char_u **abspath)
|
static bool is_executable_ext(char *name, char **abspath)
|
||||||
FUNC_ATTR_NONNULL_ARG(1)
|
FUNC_ATTR_NONNULL_ARG(1)
|
||||||
{
|
{
|
||||||
const bool is_unix_shell = strstr((char *)path_tail(p_sh), "sh") != NULL;
|
const bool is_unix_shell = strstr((char *)path_tail(p_sh), "sh") != NULL;
|
||||||
@@ -353,7 +356,7 @@ static bool is_executable_ext(char *name, char_u **abspath)
|
|||||||
/// @param[out] abspath Returns resolved executable path, if not NULL.
|
/// @param[out] abspath Returns resolved executable path, if not NULL.
|
||||||
///
|
///
|
||||||
/// @return `true` if `name` is an executable inside `$PATH`.
|
/// @return `true` if `name` is an executable inside `$PATH`.
|
||||||
static bool is_executable_in_path(const char_u *name, char_u **abspath)
|
static bool is_executable_in_path(const char *name, char **abspath)
|
||||||
FUNC_ATTR_NONNULL_ARG(1)
|
FUNC_ATTR_NONNULL_ARG(1)
|
||||||
{
|
{
|
||||||
const char *path_env = os_getenv("PATH");
|
const char *path_env = os_getenv("PATH");
|
||||||
@@ -370,7 +373,7 @@ static bool is_executable_in_path(const char_u *name, char_u **abspath)
|
|||||||
char *path = xstrdup(path_env);
|
char *path = xstrdup(path_env);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
size_t buf_len = STRLEN(name) + strlen(path) + 2;
|
size_t buf_len = strlen(name) + strlen(path) + 2;
|
||||||
char *buf = xmalloc(buf_len);
|
char *buf = xmalloc(buf_len);
|
||||||
|
|
||||||
// Walk through all entries in $PATH to check if "name" exists there and
|
// Walk through all entries in $PATH to check if "name" exists there and
|
||||||
@@ -382,7 +385,7 @@ static bool is_executable_in_path(const char_u *name, char_u **abspath)
|
|||||||
|
|
||||||
// Combine the $PATH segment with `name`.
|
// Combine the $PATH segment with `name`.
|
||||||
STRLCPY(buf, p, e - p + 1);
|
STRLCPY(buf, p, e - p + 1);
|
||||||
append_path(buf, (char *)name, buf_len);
|
append_path(buf, name, buf_len);
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
if (is_executable_ext(buf, abspath)) {
|
if (is_executable_ext(buf, abspath)) {
|
||||||
|
|||||||
@@ -535,7 +535,7 @@ int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file,
|
|||||||
|
|
||||||
// Skip files that are not executable if we check for that.
|
// Skip files that are not executable if we check for that.
|
||||||
if (!dir && (flags & EW_EXEC)
|
if (!dir && (flags & EW_EXEC)
|
||||||
&& !os_can_exe((*file)[i], NULL, !(flags & EW_SHELLCMD))) {
|
&& !os_can_exe((char *)(*file)[i], NULL, !(flags & EW_SHELLCMD))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -453,13 +453,13 @@ char *FullName_save(const char *fname, bool force)
|
|||||||
/// Saves the absolute path.
|
/// Saves the absolute path.
|
||||||
/// @param name An absolute or relative path.
|
/// @param name An absolute or relative path.
|
||||||
/// @return The absolute path of `name`.
|
/// @return The absolute path of `name`.
|
||||||
char_u *save_abs_path(const char_u *name)
|
char *save_abs_path(const char *name)
|
||||||
FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
|
FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
|
||||||
{
|
{
|
||||||
if (!path_is_absolute(name)) {
|
if (!path_is_absolute((char_u *)name)) {
|
||||||
return (char_u *)FullName_save((char *)name, true);
|
return FullName_save(name, true);
|
||||||
}
|
}
|
||||||
return vim_strsave((char_u *) name);
|
return (char *)vim_strsave((char_u *)name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if a path has a wildcard character including '~', unless at the end.
|
/// Checks if a path has a wildcard character including '~', unless at the end.
|
||||||
@@ -1401,7 +1401,7 @@ void addfile(
|
|||||||
// If the file isn't executable, may not add it. Do accept directories.
|
// If the file isn't executable, may not add it. Do accept directories.
|
||||||
// When invoked from expand_shellcmd() do not use $PATH.
|
// When invoked from expand_shellcmd() do not use $PATH.
|
||||||
if (!isdir && (flags & EW_EXEC)
|
if (!isdir && (flags & EW_EXEC)
|
||||||
&& !os_can_exe(f, NULL, !(flags & EW_SHELLCMD))) {
|
&& !os_can_exe((char *)f, NULL, !(flags & EW_SHELLCMD))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2306,7 +2306,7 @@ void path_guess_exepath(const char *argv0, char *buf, size_t bufsize)
|
|||||||
xstrlcpy((char *)NameBuff, dir, dir_len + 1);
|
xstrlcpy((char *)NameBuff, dir, dir_len + 1);
|
||||||
xstrlcat((char *)NameBuff, PATHSEPSTR, sizeof(NameBuff));
|
xstrlcat((char *)NameBuff, PATHSEPSTR, sizeof(NameBuff));
|
||||||
xstrlcat((char *)NameBuff, argv0, sizeof(NameBuff));
|
xstrlcat((char *)NameBuff, argv0, sizeof(NameBuff));
|
||||||
if (os_can_exe(NameBuff, NULL, false)) {
|
if (os_can_exe((char *)NameBuff, NULL, false)) {
|
||||||
xstrlcpy(buf, (char *)NameBuff, bufsize);
|
xstrlcpy(buf, (char *)NameBuff, bufsize);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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'))
|
||||||
|
|||||||
2
test/functional/fixtures/cmdscript.cmd
Normal file
2
test/functional/fixtures/cmdscript.cmd
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
@echo off
|
||||||
|
echo much success
|
||||||
Reference in New Issue
Block a user