Skip to content

Commit

Permalink
Editor: fix bug of executable searching
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed Aug 23, 2024
1 parent b4aac9d commit e199a2f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
28 changes: 17 additions & 11 deletions src/detection/editor/editor.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "editor.h"
#include "common/processing.h"
#include "common/library.h"
#include "util/stringUtils.h"
#include "util/path.h"
#include "util/binary.h"
Expand All @@ -13,15 +14,15 @@ static inline char* realpath(const char* restrict file_name, char* restrict reso
}
#endif

static bool extractNvimVersion(const char* str, uint32_t len, void* userdata)
static bool extractNvimVersionFromBinary(const char* str, uint32_t len, void* userdata)
{
if (len < strlen("NVIM v0.0.0")) return true;
if (!ffStrStartsWith(str, "NVIM v")) return true;
ffStrbufSetS((FFstrbuf*) userdata, str + strlen("NVIM v"));
return false;
}

static bool extractVimVersion(const char* str, uint32_t len, void* userdata)
static bool extractVimVersionFromBinary(const char* str, uint32_t len, void* userdata)
{
if (len < strlen("VIM - Vi IMproved 0.0")) return true;
if (!ffStrStartsWith(str, "VIM - Vi IMproved ")) return true;
Expand All @@ -30,7 +31,7 @@ static bool extractVimVersion(const char* str, uint32_t len, void* userdata)
return false;
}

static bool extractNanoVersion(const char* str, uint32_t len, void* userdata)
static bool extractNanoVersionFromBinary(const char* str, uint32_t len, void* userdata)
{
if (len < strlen("GNU nano 0.0")) return true;
if (!ffStrStartsWith(str, "GNU nano ")) return true;
Expand Down Expand Up @@ -60,13 +61,18 @@ const char* ffDetectEditor(FFEditorResult* result)
if (error) return NULL;
}

char buf[PATH_MAX + 1];
if (!realpath(result->path.chars, buf))
return NULL;
{
char buf[PATH_MAX + 1];
if (!realpath(result->path.chars, buf))
return NULL;

// WIN32: Should we handle scoop shim exe here?
// WIN32: Should we handle scoop shim exe here?

ffStrbufSetS(&result->path, buf);
#ifdef __linux__
if (!ffStrEndsWith(buf, "/snap"))
#endif
ffStrbufSetS(&result->path, buf);
}

{
uint32_t index = ffStrbufLastIndexC(&result->path,
Expand All @@ -91,11 +97,11 @@ const char* ffDetectEditor(FFEditorResult* result)
if (!instance.config.general.detectVersion) return NULL;

if (ffStrbufEqualS(&result->exe, "nvim"))
ffBinaryExtractStrings(buf, extractNvimVersion, &result->version);
ffBinaryExtractStrings(result->path.chars, extractNvimVersionFromBinary, &result->version);
else if (ffStrbufEqualS(&result->exe, "vim"))
ffBinaryExtractStrings(buf, extractVimVersion, &result->version);
ffBinaryExtractStrings(result->path.chars, extractVimVersionFromBinary, &result->version);
else if (ffStrbufEqualS(&result->exe, "nano"))
ffBinaryExtractStrings(buf, extractNanoVersion, &result->version);
ffBinaryExtractStrings(result->path.chars, extractNanoVersionFromBinary, &result->version);

if (result->version.length > 0) return NULL;

Expand Down
17 changes: 10 additions & 7 deletions src/util/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ const char* ffFindExecutableInPath(const char* name, FFstrbuf* result)
const bool appendExe = !ffStrEndsWithIgnCase(name, ".exe");
#endif

for (char* token = NULL; (token = strchr(path,
#ifdef _WIN32
';'
#else
':'
#endif
)) != NULL; path = token + 1)
for (char* token = path; *token; path = token + 1)
{
token = strchr(path,
#ifdef _WIN32
';'
#else
':'
#endif
);
if (!token) token = path + strlen(path);

ffStrbufSetNS(result, (uint32_t)(token - path), path);
ffStrbufEnsureEndsWithC(result,
#ifdef _WIN32
Expand Down

0 comments on commit e199a2f

Please sign in to comment.