From 9445eaa297a7e01c20cb5e5141be54cc7d8d5cce Mon Sep 17 00:00:00 2001 From: Scott Prager Date: Wed, 17 Sep 2014 00:58:30 -0400 Subject: [PATCH] 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 --- src/nvim/eval.c | 17 +++++++++++++++-- src/nvim/os/fs.c | 35 +++++++++++++++++++++++++++-------- src/nvim/os_unix.c | 2 +- src/nvim/path.c | 2 +- src/nvim/version.c | 2 +- test/unit/os/fs_spec.lua | 3 ++- 6 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 7793f5040c..8ced879880 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -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; diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index bb4e897887..07accb339a 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -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; } diff --git a/src/nvim/os_unix.c b/src/nvim/os_unix.c index 6c79fbd479..a54ed000af 100644 --- a/src/nvim/os_unix.c +++ b/src/nvim/os_unix.c @@ -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); diff --git a/src/nvim/path.c b/src/nvim/path.c index f4806f5974..6990a1817c 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -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); diff --git a/src/nvim/version.c b/src/nvim/version.c index bf0dace6ef..7934552f0f 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -360,7 +360,7 @@ static int included_patches[] = { //238, 237, 236, - //235, + 235, 234, 233, 232, diff --git a/test/unit/os/fs_spec.lua b/test/unit/os/fs_spec.lua index 67ca8f6c24..eefb1c6867 100644 --- a/test/unit/os/fs_spec.lua +++ b/test/unit/os/fs_spec.lua @@ -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