Files
fastfetch/src/detection/command/command.c
T

59 lines
1.8 KiB
C
Raw Normal View History

#include "detection/command/command.h"
2026-01-06 09:48:38 +08:00
#include "common/processing.h"
#include "common/FFstrbuf.h"
2026-03-29 09:28:49 +08:00
typedef struct FFCommandResultBundle {
FFProcessHandle handle;
const char* error;
} FFCommandResultBundle;
// FIFO, non-thread-safe list of running commands
2026-04-14 10:41:18 +08:00
static FFlist commandQueue;
2026-03-29 09:28:49 +08:00
static const char* spawnProcess(FFCommandOptions* options, FFProcessHandle* handle) {
if (options->text.length == 0) {
return "No command text specified";
2026-03-29 09:28:49 +08:00
}
return ffProcessSpawn(options->param.length ? (char* const[]) {
options->shell.chars,
options->param.chars,
options->text.chars,
2026-03-30 10:27:23 +08:00
NULL }
: (char* const[]) { options->shell.chars, options->text.chars, NULL },
2026-03-29 09:28:49 +08:00
options->useStdErr,
handle);
}
2026-03-29 09:28:49 +08:00
bool ffPrepareCommand(FFCommandOptions* options) {
if (!options->parallel) {
return false;
}
2026-04-14 10:41:18 +08:00
FFCommandResultBundle* bundle = FF_LIST_ADD(FFCommandResultBundle, commandQueue);
bundle->error = spawnProcess(options, &bundle->handle);
return true;
}
2026-03-29 09:28:49 +08:00
const char* ffDetectCommand(FFCommandOptions* options, FFstrbuf* result) {
2025-11-10 22:48:22 +08:00
FFCommandResultBundle bundle = {};
2026-03-29 09:28:49 +08:00
if (!options->parallel) {
bundle.error = spawnProcess(options, &bundle.handle);
2026-04-14 10:41:18 +08:00
} else if (!FF_LIST_SHIFT(commandQueue, &bundle)) {
return "[BUG] command queue is empty";
2026-03-29 09:28:49 +08:00
}
2026-03-29 09:28:49 +08:00
if (bundle.error) {
return bundle.error;
2026-03-29 09:28:49 +08:00
}
bundle.error = ffProcessReadOutput(&bundle.handle, result);
2026-03-29 09:28:49 +08:00
if (bundle.error) {
return bundle.error;
2026-03-29 09:28:49 +08:00
}
ffStrbufTrimRightSpace(result);
return NULL;
}