Merge pull request #953 from splinterofchaos/patch235-exepath

vim-patch:7.4.235
This commit is contained in:
Justin M. Keyes 2014-09-17 01:28:35 -04:00
commit a447160a6c
6 changed files with 99 additions and 22 deletions

View File

@ -6355,6 +6355,7 @@ static struct fst {
{"eval", 1, 1, f_eval},
{"eventhandler", 0, 0, f_eventhandler},
{"executable", 1, 1, f_executable},
{"exepath", 1, 1, f_exepath},
{"exists", 1, 1, f_exists},
{"exp", 1, 1, f_exp},
{"expand", 1, 3, f_expand},
@ -8072,7 +8073,19 @@ static void f_eventhandler(typval_T *argvars, typval_T *rettv)
*/
static void f_executable(typval_T *argvars, typval_T *rettv)
{
rettv->vval.v_number = os_can_exe(get_tv_string(&argvars[0]));
rettv->vval.v_number = os_can_exe(get_tv_string(&argvars[0]), NULL);
}
/// "exepath()" function
static void f_exepath(typval_T *argvars, typval_T *rettv)
{
char_u *arg = get_tv_string(&argvars[0]);
char_u *path = NULL;
(void)os_can_exe(arg, &path);
rettv->v_type = VAR_STRING;
rettv->vval.v_string = path;
}
/*
@ -10603,7 +10616,7 @@ static void f_jobstart(typval_T *argvars, typval_T *rettv)
}
}
if (!os_can_exe(get_tv_string(&argvars[1]))) {
if (!os_can_exe(get_tv_string(&argvars[1]), NULL)) {
// String is not executable
EMSG2(e_jobexe, get_tv_string(&argvars[1]));
return;

View File

@ -66,7 +66,10 @@ bool os_isdir(const char_u *name)
return true;
}
/// Check if the given path represents an executable file.
/// Checks if the given path represents an executable file.
///
/// @param[in] name The name of the executable.
/// @param[out] abspath Path of the executable, if found and not `NULL`.
///
/// @return `true` if `name` is executable and
/// - can be found in $PATH,
@ -74,16 +77,24 @@ bool os_isdir(const char_u *name)
/// - is absolute.
///
/// @return `false` otherwise.
bool os_can_exe(const char_u *name)
bool os_can_exe(const char_u *name, char_u **abspath)
{
// If it's an absolute or relative path don't need to use $PATH.
if (path_is_absolute_path(name) ||
(name[0] == '.' && (name[1] == '/' ||
(name[1] == '.' && name[2] == '/')))) {
return is_executable(name);
if (is_executable(name)) {
if (abspath != NULL) {
*abspath = save_absolute_path(name);
}
return true;
}
return false;
}
return is_executable_in_path(name);
return is_executable_in_path(name, abspath);
}
// Return true if "name" is an executable file, false if not or it doesn't
@ -103,10 +114,13 @@ static bool is_executable(const char_u *name)
return false;
}
/// Check if a file is inside the $PATH and is executable.
/// Checks if a file is inside the `$PATH` and is executable.
///
/// @return `true` if `name` is an executable inside $PATH.
static bool is_executable_in_path(const char_u *name)
/// @param[in] name The name of the executable.
/// @param[out] abspath Path of the executable, if found and not `NULL`.
///
/// @return `true` if `name` is an executable inside `$PATH`.
static bool is_executable_in_path(const char_u *name, char_u **abspath)
{
const char *path = getenv("PATH");
// PATH environment variable does not exist or is empty.
@ -131,8 +145,13 @@ static bool is_executable_in_path(const char_u *name)
append_path((char *) buf, (const char *) name, (int)buf_len);
if (is_executable(buf)) {
// Found our executable. Free buf and return.
// Check if the caller asked for a copy of the path.
if (abspath != NULL) {
*abspath = save_absolute_path(buf);
}
free(buf);
return true;
}

View File

@ -1331,7 +1331,7 @@ int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file,
continue;
/* Skip files that are not executable if we check for that. */
if (!dir && (flags & EW_EXEC) && !os_can_exe((*file)[i]))
if (!dir && (flags & EW_EXEC) && !os_can_exe((*file)[i], NULL))
continue;
p = xmalloc(STRLEN((*file)[i]) + 1 + dir);

View File

@ -390,6 +390,19 @@ FullName_save (
return new_fname;
}
/// Saves the absolute path.
/// @param name An absolute or relative path.
/// @return The absolute path of `name`.
char_u *save_absolute_path(const char_u *name)
FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL
{
if (!path_is_absolute_path(name)) {
return FullName_save((char_u *) name, true);
}
return vim_strsave((char_u *) name);
}
#if !defined(NO_EXPANDPATH) || defined(PROTO)
#if defined(UNIX) || defined(USE_UNIXFILENAME) || defined(PROTO)
@ -1219,7 +1232,7 @@ addfile (
return;
/* If the file isn't executable, may not add it. Do accept directories. */
if (!isdir && (flags & EW_EXEC) && !os_can_exe(f))
if (!isdir && (flags & EW_EXEC) && !os_can_exe(f, NULL))
return;
char_u *p = xmalloc(STRLEN(f) + 1 + isdir);

View File

@ -360,7 +360,7 @@ static int included_patches[] = {
//238,
237,
236,
//235,
235,
234,
233,
232,

View File

@ -11,11 +11,16 @@ local cstr = helpers.cstr
local to_cstr = helpers.to_cstr
local OK = helpers.OK
local FAIL = helpers.FAIL
local NULL = helpers.NULL
require('lfs')
require('bit')
cimport('unistd.h')
cimport('./src/nvim/os/shell.h')
cimport('./src/nvim/option_defs.h')
cimport('./src/nvim/os/event.h')
cimport('./src/nvim/fileio.h')
local fs = cimport('./src/nvim/os/os.h')
cppimport('sys/stat.h')
cppimport('sys/fcntl.h')
@ -24,6 +29,7 @@ cppimport('sys/errno.h')
local len = 0
local buf = ""
local directory = nil
local absolute_executable = nil
local executable_name = nil
local function assert_file_exists(filepath)
@ -43,7 +49,7 @@ describe('fs function', function()
lfs.link('test.file', 'unit-test-directory/test_link.file', true)
-- Since the tests are executed, they are called by an executable. We use
-- that executable for several asserts.
local absolute_executable = arg[0]
absolute_executable = arg[0]
-- Split absolute_executable into a directory and the actual file name for
-- later usage.
directory, executable_name = string.match(absolute_executable, '^(.*)/(.*)$')
@ -119,31 +125,57 @@ describe('fs function', function()
describe('os_can_exe', function()
local function os_can_exe(name)
return fs.os_can_exe((to_cstr(name)))
local buf = ffi.new('char *[1]')
buf[0] = NULL
local ok = fs.os_can_exe(to_cstr(name), buf)
-- When os_can_exe returns true, it must set the path.
-- When it returns false, the path must be NULL.
if ok then
neq(NULL, buf[0])
return internalize(buf[0])
else
eq(NULL, buf[0])
return nil
end
end
local function cant_exe(name)
eq(nil, os_can_exe(name))
end
local function exe(name)
return os_can_exe(name)
end
it('returns false when given a directory', function()
eq(false, (os_can_exe('./unit-test-directory')))
cant_exe('./unit-test-directory')
end)
it('returns false when given a regular file without executable bit set', function()
eq(false, (os_can_exe('unit-test-directory/test.file')))
cant_exe('unit-test-directory/test.file')
end)
it('returns false when the given file does not exists', function()
eq(false, (os_can_exe('does-not-exist.file')))
cant_exe('does-not-exist.file')
end)
it('returns true when given an executable inside $PATH', function()
eq(true, (os_can_exe(executable_name)))
it('returns the absolute path when given an executable inside $PATH', function()
-- Since executable_name does not start with "./", the path will be
-- selected from $PATH. Make sure the ends match, ignore the directories.
local _, busted = string.match(absolute_executable, '^(.*)/(.*)$')
local _, name = string.match(exe(executable_name), '^(.*)/(.*)$')
eq(busted, name)
end)
it('returns true when given an executable relative to the current dir', function()
it('returns the absolute path when given an executable relative to the current dir', function()
local old_dir = lfs.currentdir()
lfs.chdir(directory)
local relative_executable = './' .. executable_name
eq(true, (os_can_exe(relative_executable)))
-- Don't test yet; we need to chdir back first.
local res = exe(relative_executable)
lfs.chdir(old_dir)
eq(absolute_executable, res)
end)
end)