Skip to content

Commit

Permalink
TerminalShell: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed Dec 15, 2023
1 parent e804dfc commit 224a6a9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
26 changes: 25 additions & 1 deletion src/detection/terminalshell/terminalshell.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "common/properties.h"
#include "util/stringUtils.h"

#include <ctype.h>

#ifdef _WIN32

#include "util/mallocHelper.h"
Expand Down Expand Up @@ -162,6 +164,28 @@ static bool getShellVersionWinPowerShell(FFstrbuf* exe, FFstrbuf* version)
NULL
}) == NULL;
}
#else
static bool getShellVersionGeneric(FFstrbuf* exe, const char* exeName, FFstrbuf* version)
{
FF_STRBUF_AUTO_DESTROY command = ffStrbufCreate();
ffStrbufAppendS(&command, "printf \"%s\" \"$");
ffStrbufAppendTransformS(&command, exeName, toupper);
ffStrbufAppendS(&command, "_VERSION\"");

if (ffProcessAppendStdOut(version, (char* const[]) {
"env",
"-i",
exe->chars,
"-c",
command.chars,
NULL
}) != NULL)
return false;

ffStrbufSubstrBeforeFirstC(version, '(');
ffStrbufRemoveStrings(version, 2, (const char*[]) { "-release", "release" });
return true;
}
#endif

bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version)
Expand Down Expand Up @@ -198,7 +222,7 @@ bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version)

return getFileVersion(exe->chars, version);
#else
return false;
return getShellVersionGeneric(exe, exeName, version);
#endif
}

Expand Down
30 changes: 2 additions & 28 deletions src/detection/terminalshell/terminalshell_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "common/thread.h"
#include "util/stringUtils.h"

#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
Expand Down Expand Up @@ -284,34 +283,8 @@ static void getUserShellFromEnv(FFTerminalShellResult* result)
}
}

static void getShellVersionGeneric(FFstrbuf* exe, const char* exeName, FFstrbuf* version)
{
FF_STRBUF_AUTO_DESTROY command = ffStrbufCreate();
ffStrbufAppendS(&command, "printf \"%s\" \"$");
ffStrbufAppendTransformS(&command, exeName, toupper);
ffStrbufAppendS(&command, "_VERSION\"");

ffProcessAppendStdOut(version, (char* const[]) {
"env",
"-i",
exe->chars,
"-c",
command.chars,
NULL
});
ffStrbufSubstrBeforeFirstC(version, '(');
ffStrbufRemoveStrings(version, 2, (const char*[]) { "-release", "release" });
}

bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version);

static void getShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version)
{
ffStrbufClear(version);
if(!fftsGetShellVersion(exe, exeName, version))
getShellVersionGeneric(exe, exeName, version);
}

bool fftsGetTerminalVersion(FFstrbuf* processName, FFstrbuf* exe, FFstrbuf* version);

const FFTerminalShellResult* ffDetectTerminalShell()
Expand Down Expand Up @@ -348,7 +321,8 @@ const FFTerminalShellResult* ffDetectTerminalShell()
getTerminalFromEnv(&result);
getUserShellFromEnv(&result);

getShellVersion(&result.shellExe, result.shellExeName, &result.shellVersion);
ffStrbufClear(&result.shellVersion);
fftsGetShellVersion(&result.shellExe, result.shellExeName, &result.shellVersion);

if(ffStrbufEqualS(&result.shellProcessName, "pwsh"))
ffStrbufInitStatic(&result.shellPrettyName, "PowerShell");
Expand Down

0 comments on commit 224a6a9

Please sign in to comment.