mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
rewrote shell / terminal detection
This commit is contained in:
@@ -89,6 +89,7 @@ set(SRCS
|
||||
src/common/detectPlasma.c
|
||||
src/common/detectGTK.c
|
||||
src/common/detectWMDE.c
|
||||
src/common/detectTerminalShell.c
|
||||
src/modules/break.c
|
||||
src/modules/custom.c
|
||||
src/modules/title.c
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
static void setExeName(FFstrbuf* exe, const char** exeName)
|
||||
{
|
||||
uint32_t lastSlashIndex = ffStrbufLastIndexC(exe, '/');
|
||||
if(lastSlashIndex < exe->length)
|
||||
*exeName = exe->chars + lastSlashIndex + 1;
|
||||
}
|
||||
|
||||
static void getProcessInformation(const char* pid, FFstrbuf* processName, FFstrbuf* exe, const char** exeName)
|
||||
{
|
||||
FFstrbuf cmdlineFilePath;
|
||||
ffStrbufInit(&cmdlineFilePath);
|
||||
ffStrbufAppendS(&cmdlineFilePath, "/proc/");
|
||||
ffStrbufAppendS(&cmdlineFilePath, pid);
|
||||
ffStrbufAppendS(&cmdlineFilePath, "/cmdline");
|
||||
|
||||
ffGetFileContent(cmdlineFilePath.chars, exe);
|
||||
ffStrbufSubstrBeforeFirstC(exe, '\0'); //Trim the arguments
|
||||
|
||||
if(exe->length == 0)
|
||||
ffStrbufSet(exe, processName);
|
||||
|
||||
setExeName(exe, exeName);
|
||||
|
||||
ffStrbufDestroy(&cmdlineFilePath);
|
||||
}
|
||||
|
||||
static void getTerminalShell(FFTerminalShellResult* result, const char* pid)
|
||||
{
|
||||
if(pid == NULL || *pid == '\0')
|
||||
return;
|
||||
|
||||
FFstrbuf statFilePath;
|
||||
ffStrbufInit(&statFilePath);
|
||||
ffStrbufAppendS(&statFilePath, "/proc/");
|
||||
ffStrbufAppendS(&statFilePath, pid);
|
||||
ffStrbufAppendS(&statFilePath, "/stat");
|
||||
|
||||
FILE* stat = fopen(statFilePath.chars, "r");
|
||||
|
||||
ffStrbufDestroy(&statFilePath);
|
||||
|
||||
if(stat == NULL)
|
||||
return;
|
||||
|
||||
char name[256];
|
||||
name[0] = '\0';
|
||||
|
||||
char ppid[256];
|
||||
ppid[0] = '\0';
|
||||
|
||||
fscanf(stat, "%*s (%255[^)]) %*c %255s", name, ppid); //stat (comm) state ppid
|
||||
fclose(stat);
|
||||
|
||||
if(
|
||||
*name == '\0' ||
|
||||
*ppid == '\0' ||
|
||||
*ppid == '-' ||
|
||||
strcasecmp(ppid, "0") == 0
|
||||
) return;
|
||||
|
||||
//Common programs that are between terminal and own process, but are not the shell
|
||||
if(
|
||||
strcasecmp(name, "sudo") == 0 ||
|
||||
strcasecmp(name, "su") == 0 ||
|
||||
strcasecmp(name, "doas") == 0 ||
|
||||
strcasecmp(name, "strace") == 0 ||
|
||||
strcasecmp(name, "gdb") == 0
|
||||
) {
|
||||
getTerminalShell(result, ppid);
|
||||
return;
|
||||
}
|
||||
|
||||
//Known shells
|
||||
if(
|
||||
strcasecmp(name, "bash") == 0 ||
|
||||
strcasecmp(name, "sh") == 0 ||
|
||||
strcasecmp(name, "zsh") == 0 ||
|
||||
strcasecmp(name, "ksh") == 0 ||
|
||||
strcasecmp(name, "fish") == 0 ||
|
||||
strcasecmp(name, "dash") == 0 ||
|
||||
strcasecmp(name, "git-shell") == 0
|
||||
) {
|
||||
ffStrbufAppendS(&result->shellProcessName, name);
|
||||
getProcessInformation(pid, &result->shellProcessName, &result->shellExe, &result->shellExeName);
|
||||
|
||||
getTerminalShell(result, ppid);
|
||||
return;
|
||||
}
|
||||
|
||||
ffStrbufAppendS(&result->terminalProcessName, name);
|
||||
getProcessInformation(pid, &result->terminalProcessName, &result->terminalExe, &result->terminalExeName);
|
||||
}
|
||||
|
||||
static void getTerminalFromEnv(FFTerminalShellResult* result)
|
||||
{
|
||||
if(
|
||||
result->terminalProcessName.length != 0 &&
|
||||
ffStrbufStartsWithIgnCaseS(&result->terminalProcessName, "login") != 0 &&
|
||||
ffStrbufIgnCaseCompS(&result->terminalProcessName, "systemd") != 0 &&
|
||||
ffStrbufIgnCaseCompS(&result->terminalProcessName, "init") != 0 &&
|
||||
ffStrbufIgnCaseCompS(&result->terminalProcessName, "(init)") != 0
|
||||
) return;
|
||||
|
||||
char* term = getenv("TERM");
|
||||
|
||||
//TTY
|
||||
if(term == NULL || *term == '\0' || strcasecmp(term, "linux") == 0)
|
||||
term = ttyname(STDIN_FILENO);
|
||||
|
||||
ffStrbufAppendS(&result->terminalProcessName, term);
|
||||
ffStrbufSetS(&result->terminalExe, term);
|
||||
setExeName(&result->terminalExe, &result->terminalExeName);
|
||||
}
|
||||
|
||||
static void getUserShellFromEnv(FFTerminalShellResult* result)
|
||||
{
|
||||
ffStrbufAppendS(&result->userShellExe, getenv("SHELL"));
|
||||
setExeName(&result->userShellExe, &result->userShellExeName);
|
||||
|
||||
//If shell detection via processes failed
|
||||
if(result->shellProcessName.length == 0 && result->userShellExe.length > 0)
|
||||
{
|
||||
ffStrbufAppendS(&result->shellProcessName, result->userShellExeName);
|
||||
ffStrbufSet(&result->shellExe, &result->userShellExe);
|
||||
setExeName(&result->shellExe, &result->shellExeName);
|
||||
}
|
||||
}
|
||||
|
||||
static void getShellVersionBash(FFstrbuf* exe, FFstrbuf* version)
|
||||
{
|
||||
ffProcessAppendStdOut(version, (char* const[]) {
|
||||
"env",
|
||||
"-i",
|
||||
exe->chars,
|
||||
"--norc",
|
||||
"--noprofile",
|
||||
"-c",
|
||||
"printf \"%s\" \"$BASH_VERSION\"",
|
||||
NULL
|
||||
});
|
||||
ffStrbufSubstrBeforeFirstC(version, '(');
|
||||
}
|
||||
|
||||
static void getShellVersionZsh(FFstrbuf* exe, FFstrbuf* version)
|
||||
{
|
||||
ffProcessAppendStdOut(version, (char* const[]) {
|
||||
"env",
|
||||
"-i",
|
||||
exe->chars,
|
||||
"--no-rcs",
|
||||
"-c",
|
||||
"printf \"%s\" \"$ZSH_VERSION\"",
|
||||
NULL
|
||||
});
|
||||
}
|
||||
|
||||
static void getShellVersionGeneric(FFstrbuf* exe, const char* exeName, FFstrbuf* version)
|
||||
{
|
||||
FFstrbuf command;
|
||||
ffStrbufInit(&command);
|
||||
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, "-release", "release");
|
||||
|
||||
ffStrbufDestroy(&command);
|
||||
}
|
||||
|
||||
static void getShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version)
|
||||
{
|
||||
if(strcasecmp(exeName, "bash") == 0)
|
||||
getShellVersionBash(exe, version);
|
||||
else if(strcasecmp(exeName, "zsh") == 0)
|
||||
getShellVersionZsh(exe, version);
|
||||
else
|
||||
getShellVersionGeneric(exe, exeName, version);
|
||||
}
|
||||
|
||||
const FFTerminalShellResult* ffDetectTerminalShell(FFinstance* instance)
|
||||
{
|
||||
UNUSED(instance);
|
||||
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static FFTerminalShellResult result;
|
||||
static bool init = false;
|
||||
pthread_mutex_lock(&mutex);
|
||||
if(init)
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return &result;
|
||||
}
|
||||
init = true;
|
||||
|
||||
ffStrbufInit(&result.shellProcessName);
|
||||
ffStrbufInit(&result.shellExe);
|
||||
result.shellExeName = result.shellExe.chars;
|
||||
ffStrbufInit(&result.shellVersion);
|
||||
|
||||
ffStrbufInit(&result.terminalProcessName);
|
||||
ffStrbufInit(&result.terminalExe);
|
||||
result.terminalExeName = result.terminalExe.chars;
|
||||
|
||||
ffStrbufInit(&result.userShellExe);
|
||||
result.userShellExeName = result.userShellExe.chars;
|
||||
ffStrbufInit(&result.userShellVersion);
|
||||
|
||||
char ppid[256];
|
||||
snprintf(ppid, 255, "%i", getppid());
|
||||
getTerminalShell(&result, ppid);
|
||||
|
||||
getTerminalFromEnv(&result);
|
||||
getUserShellFromEnv(&result);
|
||||
getShellVersion(&result.shellExe, result.shellExeName, &result.shellVersion);
|
||||
|
||||
if(strcasecmp(result.shellExeName, result.userShellExeName) != 0)
|
||||
getShellVersion(&result.userShellExe, result.userShellExeName, &result.userShellVersion);
|
||||
else
|
||||
ffStrbufSet(&result.userShellVersion, &result.shellVersion);
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return &result;
|
||||
}
|
||||
@@ -323,7 +323,7 @@ static void getFromProcDir(FFinstance* instance, FFWMDEResult* result, ProtocolH
|
||||
ffStrbufAppendS(&procPath, dirent->d_name);
|
||||
ffStrbufAppendS(&procPath, "/cmdline");
|
||||
ffGetFileContent(procPath.chars, &processName);
|
||||
ffStrbufRecalculateLength(&processName); //Arguments are seperated by a \0 char. We make use of this to extract only the executable path. This is needed if arguments contain the / char
|
||||
ffStrbufSubstrBeforeFirstC(&processName, '\0'); //Trim the arguments
|
||||
ffStrbufSubstrAfterLastC(&processName, '/');
|
||||
ffStrbufSubstrBefore(&procPath, procPathLength);
|
||||
|
||||
|
||||
@@ -32,9 +32,9 @@ static inline void* detectWMDEThreadMain(void* instance)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline void* detectTerminalThreadMain(void* instance)
|
||||
static inline void* detectTerminalShellThreadMain(void* instance)
|
||||
{
|
||||
ffDetectTerminal((FFinstance*)instance);
|
||||
ffDetectTerminalShell((FFinstance*)instance);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -56,9 +56,9 @@ static inline void* startThreadsThreadMain(void* instance)
|
||||
pthread_create(>k4Thread, NULL, detectGTK4ThreadMain, instance);
|
||||
pthread_detach(gtk4Thread);
|
||||
|
||||
pthread_t terminalThread;
|
||||
pthread_create(&terminalThread, NULL, detectTerminalThreadMain, instance);
|
||||
pthread_detach(terminalThread);
|
||||
pthread_t terminalShellThread;
|
||||
pthread_create(&terminalShellThread, NULL, detectTerminalShellThreadMain, instance);
|
||||
pthread_detach(terminalShellThread);
|
||||
|
||||
pthread_t plasmaThread;
|
||||
pthread_create(&plasmaThread, NULL, detectPlasmaThreadMain, instance);
|
||||
|
||||
+10
-7
@@ -292,11 +292,14 @@ static inline void printCommandHelp(const char* command)
|
||||
}
|
||||
else if(strcasecmp(command, "shell-format") == 0)
|
||||
{
|
||||
constructAndPrintCommandHelpFormat("shell", "{2} {4}", 4,
|
||||
"Shell path (without name)",
|
||||
"Shell name",
|
||||
"Shell version raw",
|
||||
"Shell version pretty"
|
||||
constructAndPrintCommandHelpFormat("shell", "{3} {4}", 7,
|
||||
"Shell process name",
|
||||
"Shell path with exe name",
|
||||
"Shell exe name",
|
||||
"Shell version",
|
||||
"User shell path with exe name",
|
||||
"User shell exe name",
|
||||
"User shell version"
|
||||
);
|
||||
}
|
||||
else if(strcasecmp(command, "resolution-format") == 0)
|
||||
@@ -382,9 +385,9 @@ static inline void printCommandHelp(const char* command)
|
||||
else if(strcasecmp(command, "terminal-format") == 0)
|
||||
{
|
||||
constructAndPrintCommandHelpFormat("terminal", "{3}", 3,
|
||||
"Terminal executable name",
|
||||
"Terminal process name",
|
||||
"Terminal name"
|
||||
"Terminal path with exe name",
|
||||
"Terminal exe name"
|
||||
);
|
||||
}
|
||||
else if(strcasecmp(command, "terminal-font-format") == 0)
|
||||
|
||||
+17
-6
@@ -136,12 +136,21 @@ typedef struct FFGTKResult
|
||||
FFstrbuf font;
|
||||
} FFGTKResult;
|
||||
|
||||
typedef struct FFTerminalResult
|
||||
typedef struct FFTerminalShellResult
|
||||
{
|
||||
FFstrbuf exeName;
|
||||
FFstrbuf processName;
|
||||
FFstrbuf error;
|
||||
} FFTerminalResult;
|
||||
FFstrbuf shellProcessName;
|
||||
FFstrbuf shellExe;
|
||||
const char* shellExeName; //pointer to a char in shellExe
|
||||
FFstrbuf shellVersion;
|
||||
|
||||
FFstrbuf terminalProcessName;
|
||||
FFstrbuf terminalExe;
|
||||
const char* terminalExeName; //pointer to a char in terminalExe
|
||||
|
||||
FFstrbuf userShellExe;
|
||||
const char* userShellExeName; //pointer to a char in userShellExe
|
||||
FFstrbuf userShellVersion;
|
||||
} FFTerminalShellResult;
|
||||
|
||||
typedef struct FFWMDEResult
|
||||
{
|
||||
@@ -306,13 +315,15 @@ const FFGTKResult* ffDetectGTK3(FFinstance* instance);
|
||||
//common/detectWMDE.c
|
||||
const FFWMDEResult* ffDetectWMDE(FFinstance* instance);
|
||||
|
||||
//common/detectTerminalShell.c
|
||||
const FFTerminalShellResult* ffDetectTerminalShell(FFinstance* instance);
|
||||
|
||||
/********************/
|
||||
/* Module functions */
|
||||
/********************/
|
||||
|
||||
//Common
|
||||
const FFOSResult* ffDetectOS(FFinstance* instance);
|
||||
const FFTerminalResult* ffDetectTerminal(FFinstance* instance);
|
||||
|
||||
//Printing
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ static void printBattery(FFinstance* instance, FFstrbuf* dir, uint32_t index)
|
||||
putchar('%');
|
||||
|
||||
if(showStatus)
|
||||
fputs(", ", stdout);
|
||||
fputs("; ", stdout);
|
||||
}
|
||||
|
||||
if(showStatus)
|
||||
|
||||
+2
-1
@@ -12,7 +12,8 @@ static bool hostValueSet(FFstrbuf* value)
|
||||
ffStrbufIgnCaseCompS(value, "None") != 0 &&
|
||||
ffStrbufIgnCaseCompS(value, "To be filled by O.E.M.") != 0 &&
|
||||
ffStrbufIgnCaseCompS(value, "System Product Name") != 0 &&
|
||||
ffStrbufIgnCaseCompS(value, "System Version") != 0
|
||||
ffStrbufIgnCaseCompS(value, "System Version") != 0 &&
|
||||
ffStrbufIgnCaseCompS(value, "Type1ProductConfigId") != 0
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
+33
-61
@@ -4,81 +4,53 @@
|
||||
#include <ctype.h>
|
||||
|
||||
#define FF_SHELL_MODULE_NAME "Shell"
|
||||
#define FF_SHELL_NUM_FORMAT_ARGS 4
|
||||
#define FF_SHELL_NUM_FORMAT_ARGS 7
|
||||
|
||||
void ffPrintShell(FFinstance* instance)
|
||||
{
|
||||
if(ffPrintFromCache(instance, FF_SHELL_MODULE_NAME, &instance->config.shellKey, &instance->config.shellFormat, FF_SHELL_NUM_FORMAT_ARGS))
|
||||
return;
|
||||
const FFTerminalShellResult* result = ffDetectTerminalShell(instance);
|
||||
|
||||
char* shellPath = getenv("SHELL");
|
||||
if(shellPath == NULL)
|
||||
if(result->shellProcessName.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shellKey, &instance->config.shellFormat, FF_SHELL_NUM_FORMAT_ARGS, "getenv(\"SHELL\") == NULL");
|
||||
ffPrintError(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shellKey, &instance->config.shellFormat, FF_SHELL_NUM_FORMAT_ARGS, "Couldn't detect shell");
|
||||
return;
|
||||
}
|
||||
|
||||
char* shellName = strrchr(shellPath, '/');
|
||||
if(shellName != NULL)
|
||||
++shellName;
|
||||
FFstrbuf shell;
|
||||
ffStrbufInit(&shell);
|
||||
ffStrbufAppendS(&shell, result->shellExeName);
|
||||
|
||||
FFstrbuf version;
|
||||
ffStrbufInit(&version);
|
||||
|
||||
FFstrbuf command;
|
||||
ffStrbufInit(&command);
|
||||
ffStrbufAppendS(&command, "printf %s \"$");
|
||||
ffStrbufAppendTransformS(&command, shellName != NULL ? shellName : shellPath, toupper);
|
||||
ffStrbufAppendS(&command, "_VERSION\"");
|
||||
|
||||
ffProcessAppendStdOut(&version, (char* const[]){
|
||||
shellPath,
|
||||
"-c",
|
||||
command.chars,
|
||||
NULL
|
||||
});
|
||||
|
||||
ffStrbufDestroy(&command);
|
||||
|
||||
FFstrbuf versionPretty;
|
||||
ffStrbufInitCopy(&versionPretty, &version);
|
||||
ffStrbufSubstrBeforeFirstC(&versionPretty, '(');
|
||||
ffStrbufRemoveStrings(&versionPretty, 1, "-release");
|
||||
|
||||
char null = '\0';
|
||||
|
||||
if(shellName == NULL)
|
||||
if(result->shellVersion.length > 0)
|
||||
{
|
||||
shellName = shellPath;
|
||||
shellPath = &null;
|
||||
ffStrbufAppendC(&shell, ' ');
|
||||
ffStrbufAppend(&shell, &result->shellVersion);
|
||||
}
|
||||
|
||||
if(instance->config.shellFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shellKey);
|
||||
fputs(result->shellExeName, stdout);
|
||||
|
||||
if(result->shellVersion.length > 0)
|
||||
{
|
||||
putchar(' ');
|
||||
ffStrbufWriteTo(&result->shellVersion, stdout);
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
*(shellName - 1) = '\0';
|
||||
ffPrintFormatString(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shellKey, &instance->config.shellFormat, NULL, FF_SHELL_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->shellProcessName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->shellExe},
|
||||
{FF_FORMAT_ARG_TYPE_STRING, result->shellExeName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->shellVersion},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->userShellExe},
|
||||
{FF_FORMAT_ARG_TYPE_STRING, &result->userShellExeName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->userShellVersion}
|
||||
});
|
||||
}
|
||||
|
||||
FF_STRBUF_CREATE(shell);
|
||||
ffStrbufSetS(&shell, shellName);
|
||||
|
||||
if(versionPretty.length > 0)
|
||||
{
|
||||
ffStrbufAppendC(&shell, ' ');
|
||||
ffStrbufAppend(&shell, &versionPretty);
|
||||
}
|
||||
else if(version.length > 0)
|
||||
{
|
||||
ffStrbufAppendC(&shell, ' ');
|
||||
ffStrbufAppend(&shell, &version);
|
||||
}
|
||||
|
||||
ffPrintAndSaveToCache(instance, FF_SHELL_MODULE_NAME, &instance->config.shellKey, &shell, &instance->config.shellFormat, FF_SHELL_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRING, shellPath},
|
||||
{FF_FORMAT_ARG_TYPE_STRING, shellName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &version},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &versionPretty}
|
||||
});
|
||||
|
||||
ffStrbufDestroy(&shell);
|
||||
ffStrbufDestroy(&versionPretty);
|
||||
ffStrbufDestroy(&version);
|
||||
}
|
||||
|
||||
+7
-116
@@ -7,136 +7,27 @@
|
||||
#define FF_TERMINAL_MODULE_NAME "Terminal"
|
||||
#define FF_TERMINAL_NUM_FORMAT_ARGS 3
|
||||
|
||||
static inline void getTerminalFromEnv(FFTerminalResult* result)
|
||||
{
|
||||
char* term = getenv("TERM");
|
||||
|
||||
//TTY
|
||||
if(term == NULL || strcasecmp(term, "linux") == 0)
|
||||
term = ttyname(STDIN_FILENO);
|
||||
|
||||
ffStrbufSetS(&result->exeName, term);
|
||||
ffStrbufSetS(&result->processName, term);
|
||||
}
|
||||
|
||||
static void getTerminalName(FFinstance* instance, const char* pid, FFTerminalResult* result)
|
||||
{
|
||||
char statFile[234];
|
||||
sprintf(statFile, "/proc/%s/stat", pid);
|
||||
|
||||
FILE* stat = fopen(statFile, "r");
|
||||
if(stat == NULL)
|
||||
{
|
||||
ffStrbufSetF(&result->error, "fopen(\"%s\", \"r\") == NULL", statFile);
|
||||
return;
|
||||
}
|
||||
|
||||
char name[256];
|
||||
char ppid[256];
|
||||
if(fscanf(stat, "%*s (%255[^)])%*s%255s", name, ppid) != 2)
|
||||
{
|
||||
ffStrbufSetS(&result->error, "fscanf(stat, \"%*s (%[^)])%*s%s\", name, ppid) != 2");
|
||||
fclose(stat);
|
||||
return;
|
||||
}
|
||||
|
||||
fclose(stat);
|
||||
|
||||
if (
|
||||
strcasecmp(name, "bash") == 0 ||
|
||||
strcasecmp(name, "sh") == 0 ||
|
||||
strcasecmp(name, "zsh") == 0 ||
|
||||
strcasecmp(name, "ksh") == 0 ||
|
||||
strcasecmp(name, "fish") == 0 ||
|
||||
strcasecmp(name, "sudo") == 0 ||
|
||||
strcasecmp(name, "su") == 0 ||
|
||||
strcasecmp(name, "doas") == 0 ||
|
||||
strcasecmp(name, "strace") == 0 ||
|
||||
strcasecmp(name, "gdb") == 0
|
||||
) {
|
||||
getTerminalName(instance, ppid, result);
|
||||
return;
|
||||
}
|
||||
|
||||
char cmdlineFile[234];
|
||||
sprintf(cmdlineFile, "/proc/%s/cmdline", pid);
|
||||
|
||||
ffGetFileContent(cmdlineFile, &result->exeName);
|
||||
ffStrbufSubstrBeforeFirstC(&result->exeName, '\0');
|
||||
ffStrbufSubstrAfterLastC(&result->exeName, '/');
|
||||
|
||||
ffStrbufSetS(&result->processName, name);
|
||||
}
|
||||
|
||||
const FFTerminalResult* ffDetectTerminal(FFinstance* instance)
|
||||
{
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static FFTerminalResult result;
|
||||
static bool init = false;
|
||||
pthread_mutex_lock(&mutex);
|
||||
if(init)
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return &result;
|
||||
}
|
||||
init = true;
|
||||
|
||||
ffStrbufInit(&result.exeName);
|
||||
ffStrbufInit(&result.processName);
|
||||
ffStrbufInit(&result.error);
|
||||
|
||||
char ppid[256];
|
||||
sprintf(ppid, "%i", getppid());
|
||||
|
||||
getTerminalName(instance, ppid, &result);
|
||||
|
||||
// This is mainly used when running in TTY. It also provides an fallback to $TERM if we _somehow_ failed
|
||||
if(
|
||||
ffStrbufStartsWithIgnCaseS(&result.exeName, "login") ||
|
||||
ffStrbufIgnCaseCompS(&result.exeName, "systemd") == 0 ||
|
||||
ffStrbufIgnCaseCompS(&result.exeName, "init") == 0 ||
|
||||
ffStrbufIgnCaseCompS(&result.exeName, "(init)") == 0
|
||||
) getTerminalFromEnv(&result);
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
return &result;
|
||||
}
|
||||
|
||||
void ffPrintTerminal(FFinstance* instance)
|
||||
{
|
||||
const FFTerminalResult* result = ffDetectTerminal(instance);
|
||||
const FFTerminalShellResult* result = ffDetectTerminalShell(instance);
|
||||
|
||||
if(result->error.length > 0)
|
||||
if(result->terminalProcessName.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_TERMINAL_MODULE_NAME, 0, &instance->config.terminalKey, &instance->config.terminalFormat, FF_TERMINAL_NUM_FORMAT_ARGS, result->error.chars);
|
||||
ffPrintError(instance, FF_TERMINAL_MODULE_NAME, 0, &instance->config.terminalKey, &instance->config.terminalFormat, FF_TERMINAL_NUM_FORMAT_ARGS, "Couldn't detect terminal");
|
||||
return;
|
||||
}
|
||||
|
||||
if(result->exeName.length == 0 && result->processName.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_TERMINAL_MODULE_NAME, 0, &instance->config.terminalKey, &instance->config.terminalFormat, FF_TERMINAL_NUM_FORMAT_ARGS, "Terminal names not set");
|
||||
return;
|
||||
}
|
||||
|
||||
const FFstrbuf* name;
|
||||
|
||||
if(ffStrbufStartsWith(&result->exeName, &result->processName))
|
||||
name = &result->exeName;
|
||||
else
|
||||
name = &result->processName;
|
||||
|
||||
if(instance->config.terminalFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_TERMINAL_MODULE_NAME, 0, &instance->config.terminalKey);
|
||||
ffStrbufPutTo(name, stdout);
|
||||
puts(result->terminalExeName);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormatString(instance, FF_TERMINAL_MODULE_NAME, 0, &instance->config.terminalKey, &instance->config.terminalFormat, NULL, FF_TERMINAL_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->exeName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->processName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, name}
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->terminalProcessName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->terminalExe},
|
||||
{FF_FORMAT_ARG_TYPE_STRING, &result->terminalExeName}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,26 +207,26 @@ static void printTTY(FFinstance* instance)
|
||||
|
||||
void ffPrintTerminalFont(FFinstance* instance)
|
||||
{
|
||||
const FFTerminalResult* result = ffDetectTerminal(instance);
|
||||
const FFTerminalShellResult* result = ffDetectTerminalShell(instance);
|
||||
|
||||
if(result->exeName.length == 0)
|
||||
if(result->terminalProcessName.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_TERMFONT_MODULE_NAME, 0, &instance->config.termFontKey, &instance->config.termFontFormat, FF_TERMFONT_NUM_FORMAT_ARGS, "Terminal font needs successfull terminal detection");
|
||||
return;
|
||||
}
|
||||
|
||||
if(ffStrbufIgnCaseCompS(&result->exeName, "konsole") == 0)
|
||||
if(ffStrbufIgnCaseCompS(&result->terminalProcessName, "konsole") == 0)
|
||||
printKonsole(instance);
|
||||
else if(ffStrbufIgnCaseCompS(&result->exeName, "xfce4-terminal") == 0)
|
||||
else if(ffStrbufIgnCaseCompS(&result->terminalProcessName, "xfce4-terminal") == 0)
|
||||
printXCFETerminal(instance);
|
||||
else if(ffStrbufIgnCaseCompS(&result->exeName, "lxterminal") == 0)
|
||||
else if(ffStrbufIgnCaseCompS(&result->terminalProcessName, "lxterminal") == 0)
|
||||
printTerminalFontFromConfigFile(instance, "lxterminal/lxterminal.conf", "fontname =");
|
||||
else if(ffStrbufIgnCaseCompS(&result->exeName, "tilix") == 0)
|
||||
else if(ffStrbufIgnCaseCompS(&result->terminalProcessName, "tilix") == 0)
|
||||
printTerminalFontFromGSettings(instance, "/com/gexperts/Tilix/profiles/", "com.gexperts.Tilix.ProfilesList", "com.gexperts.Tilix.Profile");
|
||||
else if(ffStrbufIgnCaseCompS(&result->exeName, "gnome-terminal-server") == 0)
|
||||
else if(ffStrbufIgnCaseCompS(&result->terminalProcessName, "gnome-terminal-") == 0)
|
||||
printTerminalFontFromGSettings(instance, "/org/gnome/terminal/legacy/profiles:/:", "org.gnome.Terminal.ProfilesList", "org.gnome.Terminal.Legacy.Profile");
|
||||
else if(ffStrbufStartsWithIgnCaseS(&result->exeName, "/dev/tty"))
|
||||
else if(ffStrbufStartsWithIgnCaseS(&result->terminalProcessName, "/dev/tty"))
|
||||
printTTY(instance);
|
||||
else
|
||||
ffPrintError(instance, FF_TERMFONT_MODULE_NAME, 0, &instance->config.termFontKey, &instance->config.termFontFormat, FF_TERMFONT_NUM_FORMAT_ARGS, "Unknown terminal: %s", result->exeName.chars);
|
||||
ffPrintError(instance, FF_TERMFONT_MODULE_NAME, 0, &instance->config.termFontKey, &instance->config.termFontFormat, FF_TERMFONT_NUM_FORMAT_ARGS, "Unknown terminal: %s", result->terminalProcessName.chars);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user