From 0168f2e9a8b5ea3953de14be534fe543316c5df0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 18 Jan 2024 15:27:31 +0800 Subject: [PATCH] Shell: improve xonsh version detection --- src/detection/terminalshell/terminalshell.c | 24 ++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/detection/terminalshell/terminalshell.c b/src/detection/terminalshell/terminalshell.c index 98faad02c..78c309766 100644 --- a/src/detection/terminalshell/terminalshell.c +++ b/src/detection/terminalshell/terminalshell.c @@ -168,6 +168,23 @@ static bool getShellVersionAsh(FFstrbuf* exe, FFstrbuf* version) return true; } +static bool getShellVersionXonsh(FFstrbuf* exe, FFstrbuf* version) +{ + ffStrbufSetS(version, getenv("XONSH_VERSION")); + if (version->length) return true; + + if(ffProcessAppendStdErr(version, (char* const[]) { + exe->chars, + "--version", + NULL + }) != NULL) + return false; + + // xonsh/0.14.1 + ffStrbufSubstrAfterFirstC(version, '/'); + return true; +} + #ifdef _WIN32 static bool getShellVersionWinPowerShell(FFstrbuf* exe, FFstrbuf* version) { @@ -228,11 +245,8 @@ bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version) return getExeVersionRaw(exe, version); if(strcasecmp(exeName, "ash") == 0) return getShellVersionAsh(exe, version); - if((strcasecmp(exeName, "xonsh") == 0 || strcasecmp(exeName, "python") == 0) && getenv("XONSH_VERSION")) - { - ffStrbufSetS(version, getenv("XONSH_VERSION")); - return true; - } + if(strcasecmp(exeName, "xonsh") == 0) + return getShellVersionXonsh(exe, version); #ifdef _WIN32 if(strcasecmp(exeName, "powershell") == 0 || strcasecmp(exeName, "powershell_ise") == 0)