diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/nvim/eval.c | 17 | ||||
| -rw-r--r-- | src/nvim/os/fs.c | 35 | ||||
| -rw-r--r-- | src/nvim/os_unix.c | 2 | ||||
| -rw-r--r-- | src/nvim/path.c | 15 | ||||
| -rw-r--r-- | src/nvim/version.c | 2 | 
5 files changed, 58 insertions, 13 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index e750da01f0..760457b54e 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}, @@ -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; 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. +/// +/// @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) +/// @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 4e05c506f8..6990a1817c 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -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); diff --git a/src/nvim/version.c b/src/nvim/version.c index 1d0366f083..fa96b27341 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,  | 
