vim-patch:7.4.235

Problem:    It is not easy to get the full path of a command.
Solution:   Add the exepath() function.

https://code.google.com/p/vim/source/detail?r=5ab2946f7ce560985830fbc3c453bb0f7a01f385
This commit is contained in:
Scott Prager 2014-09-17 00:58:30 -04:00
parent 899878d347
commit 9445eaa297
6 changed files with 47 additions and 14 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},
@ -8066,7 +8067,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;
}
/*
@ -10577,7 +10590,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 is_executable_in_path(name);
return true;
}
return false;
}
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

@ -1232,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,6 +11,7 @@ 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')
@ -118,7 +119,7 @@ describe('fs function', function()
end)
describe('os_can_exe', function()
local function os_can_exe(name)
local function os_can_exe(name, NULL)
return fs.os_can_exe((to_cstr(name)))
end