mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-31 01:56:28 +01:00
61 lines
1.5 KiB
C
61 lines
1.5 KiB
C
|
|
#include "common/processing.h"
|
||
|
|
#include "util/FFstrbuf.h"
|
||
|
|
#include "detection/command/command.h"
|
||
|
|
|
||
|
|
typedef struct FFCommandResultBundle
|
||
|
|
{
|
||
|
|
FFProcessHandle handle;
|
||
|
|
const char* error;
|
||
|
|
} FFCommandResultBundle;
|
||
|
|
|
||
|
|
// FIFO, non-thread-safe list of running commands
|
||
|
|
static FFlist commandQueue = {
|
||
|
|
.elementSize = sizeof(FFCommandResultBundle),
|
||
|
|
};
|
||
|
|
|
||
|
|
static const char* spawnProcess(FFCommandOptions* options, FFProcessHandle* handle)
|
||
|
|
{
|
||
|
|
if (options->text.length == 0)
|
||
|
|
return "No command text specified";
|
||
|
|
|
||
|
|
return ffProcessSpawn(options->param.length ? (char* const[]){
|
||
|
|
options->shell.chars,
|
||
|
|
options->param.chars,
|
||
|
|
options->text.chars,
|
||
|
|
NULL
|
||
|
|
} : (char* const[]){
|
||
|
|
options->shell.chars,
|
||
|
|
options->text.chars,
|
||
|
|
NULL
|
||
|
|
}, options->useStdErr, handle);
|
||
|
|
}
|
||
|
|
|
||
|
|
bool ffPrepareCommand(FFCommandOptions* options)
|
||
|
|
{
|
||
|
|
if (!options->parallel) return false;
|
||
|
|
|
||
|
|
FFCommandResultBundle* bundle = ffListAdd(&commandQueue);
|
||
|
|
bundle->error = spawnProcess(options, &bundle->handle);
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
const char* ffDetectCommand(FFCommandOptions* options, FFstrbuf* result)
|
||
|
|
{
|
||
|
|
FFCommandResultBundle bundle;
|
||
|
|
if (!options->parallel)
|
||
|
|
bundle.error = spawnProcess(options, &bundle.handle);
|
||
|
|
else
|
||
|
|
ffListShift(&commandQueue, &bundle);
|
||
|
|
|
||
|
|
if (bundle.error)
|
||
|
|
return bundle.error;
|
||
|
|
|
||
|
|
bundle.error = ffProcessReadOutput(&bundle.handle, result);
|
||
|
|
if (bundle.error)
|
||
|
|
return bundle.error;
|
||
|
|
|
||
|
|
ffStrbufTrimRightSpace(result);
|
||
|
|
return NULL;
|
||
|
|
}
|