diff --git a/src/detection/editor/editor.c b/src/detection/editor/editor.c index 00a30cbbd..6a3997349 100644 --- a/src/detection/editor/editor.c +++ b/src/detection/editor/editor.c @@ -4,6 +4,14 @@ #include +#ifdef _WIN32 +#include +static inline char* realpath(const char* restrict file_name, char* restrict resolved_name) +{ + return _fullpath(resolved_name, file_name, _MAX_PATH); +} +#endif + const char* ffDetectEditor(FFEditorResult* result) { ffStrbufSetS(&result->name, getenv("VISUAL")); @@ -22,6 +30,21 @@ const char* ffDetectEditor(FFEditorResult* result) }) != NULL || result->path.length == 0) return NULL; } + #else + if (!(result->name.length > 3 && ffCharIsEnglishAlphabet(result->name.chars[0]) && result->name.chars[1] == ':' && result->name.chars[2] == '\\')) + { + char buf[32]; + uint32_t len = GetSystemDirectoryA(buf, sizeof(buf)); + if (len < strlen("C:\\WINDOWS\\system32")) return NULL; + strncpy(buf + len, "\\where.exe", sizeof(buf) - len); + if (ffProcessAppendStdOut(&result->path, (char* const[]){ + buf, + result->name.chars, + NULL, + }) != NULL || result->path.length == 0) + return NULL; + } + #endif else ffStrbufSet(&result->path, &result->name); @@ -30,29 +53,45 @@ const char* ffDetectEditor(FFEditorResult* result) return NULL; ffStrbufSetS(&result->path, buf); - const char* exe = &result->path.chars[ffStrbufLastIndexC(&result->path, '/')]; - if (!*exe) return NULL; - ++exe; - result->exe = exe; + + { + uint32_t index = ffStrbufLastIndexC(&result->path, + #ifndef _WIN32 + '/' + #else + '\\' + #endif + ); + if (index == result->path.length) + return NULL; + ffStrbufSetS(&result->exe, &result->path.chars[index + 1]); + if (!result->exe.length) + return NULL; + + #ifdef _WIN32 + if (ffStrbufEndsWithS(&result->exe, ".exe")) + ffStrbufSubstrBefore(&result->exe, result->exe.length - 4); + #endif + } const char* param = NULL; if ( - ffStrEquals(exe, "nano") || - ffStrEquals(exe, "vim") || - ffStrEquals(exe, "nvim") || - ffStrEquals(exe, "micro") || - ffStrEquals(exe, "emacs") || - ffStrStartsWith(exe, "emacs-") || // emacs-29.3 - ffStrEquals(exe, "hx") || - ffStrEquals(exe, "code") || - ffStrEquals(exe, "sublime_text") + ffStrbufEqualS(&result->exe, "nano") || + ffStrbufEqualS(&result->exe, "vim") || + ffStrbufEqualS(&result->exe, "nvim") || + ffStrbufEqualS(&result->exe, "micro") || + ffStrbufEqualS(&result->exe, "emacs") || + ffStrbufStartsWithS(&result->exe, "emacs-") || // emacs-29.3 + ffStrbufEqualS(&result->exe, "hx") || + ffStrbufEqualS(&result->exe, "code") || + ffStrbufEqualS(&result->exe, "sublime_text") ) param = "--version"; else if ( - ffStrEquals(exe, "kak") || - ffStrEquals(exe, "pico") + ffStrbufEqualS(&result->exe, "kak") || + ffStrbufEqualS(&result->exe, "pico") ) param = "-version"; else if ( - ffStrEquals(exe, "ne") + ffStrbufEqualS(&result->exe, "ne") ) param = "-h"; else return NULL; @@ -85,7 +124,6 @@ const char* ffDetectEditor(FFEditorResult* result) break; } } - #endif return NULL; } diff --git a/src/detection/editor/editor.h b/src/detection/editor/editor.h index a90e5556a..b41bdbb13 100644 --- a/src/detection/editor/editor.h +++ b/src/detection/editor/editor.h @@ -5,7 +5,7 @@ typedef struct FFEditorResult { FFstrbuf name; - const char* exe; + FFstrbuf exe; FFstrbuf path; FFstrbuf version; } FFEditorResult; diff --git a/src/modules/editor/editor.c b/src/modules/editor/editor.c index 996096ce5..01c82c10f 100644 --- a/src/modules/editor/editor.c +++ b/src/modules/editor/editor.c @@ -12,6 +12,7 @@ void ffPrintEditor(FFEditorOptions* options) FFEditorResult result = { .name = ffStrbufCreate(), .path = ffStrbufCreate(), + .exe = ffStrbufCreate(), .version = ffStrbufCreate(), }; const char* error = ffDetectEditor(&result); @@ -23,9 +24,9 @@ void ffPrintEditor(FFEditorOptions* options) } ffPrintLogoAndKey(FF_EDITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - if (result.exe) + if (result.exe.length) { - fputs(result.exe, stdout); + ffStrbufWriteTo(&result.exe, stdout); if (result.version.length) printf(" (%s)", result.version.chars); } @@ -37,6 +38,7 @@ void ffPrintEditor(FFEditorOptions* options) ffStrbufDestroy(&result.name); ffStrbufDestroy(&result.path); + ffStrbufDestroy(&result.exe); ffStrbufDestroy(&result.version); } @@ -94,14 +96,12 @@ void ffGenerateEditorJsonResult(FF_MAYBE_UNUSED FFEditorOptions* options, yyjson yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); yyjson_mut_obj_add_strbuf(doc, obj, "name", &result.name); yyjson_mut_obj_add_strbuf(doc, obj, "path", &result.path); - if (result.exe) - yyjson_mut_obj_add_strcpy(doc, obj, "exe", result.exe); - else - yyjson_mut_obj_add_null(doc, obj, "exe"); + yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result.exe); yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version); ffStrbufDestroy(&result.name); ffStrbufDestroy(&result.path); + ffStrbufDestroy(&result.exe); ffStrbufDestroy(&result.version); }