2023-03-07 19:27:49 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-03-07 19:27:49 +08:00
|
|
|
#include "common/processing.h"
|
|
|
|
|
#include "modules/command/command.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-03-07 19:27:49 +08:00
|
|
|
|
|
|
|
|
void ffPrintCommand(FFinstance* instance, FFCommandOptions* options)
|
|
|
|
|
{
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
|
2023-03-07 19:27:49 +08:00
|
|
|
const char* error = ffProcessAppendStdOut(&result, (char* const[]){
|
|
|
|
|
options->shell.chars,
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
"/c",
|
|
|
|
|
#else
|
|
|
|
|
"-c",
|
|
|
|
|
#endif
|
|
|
|
|
options->text.chars,
|
|
|
|
|
NULL
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
|
|
|
|
ffPrintError(instance, FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!result.length)
|
|
|
|
|
{
|
|
|
|
|
ffPrintError(instance, FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "No result printed");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-18 12:32:47 +08:00
|
|
|
ffPrintLogoAndKey(instance, FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs.key, &options->moduleArgs.keyColor);
|
2023-03-07 19:27:49 +08:00
|
|
|
ffStrbufPutTo(&result, stdout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffInitCommandOptions(FFCommandOptions* options)
|
|
|
|
|
{
|
|
|
|
|
options->moduleName = FF_COMMAND_MODULE_NAME;
|
|
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
|
|
|
|
|
ffStrbufInitS(&options->shell,
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
"cmd"
|
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
|
"csh"
|
|
|
|
|
#else
|
|
|
|
|
"bash"
|
|
|
|
|
#endif
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ffStrbufInit(&options->text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseCommandCommandOptions(FFCommandOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_COMMAND_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if(ffStrEqualsIgnCase(subKey, "shell"))
|
2023-03-07 19:27:49 +08:00
|
|
|
{
|
|
|
|
|
ffOptionParseString(key, value, &options->shell);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if(ffStrEqualsIgnCase(subKey, "text"))
|
2023-03-07 19:27:49 +08:00
|
|
|
{
|
|
|
|
|
ffOptionParseString(key, value, &options->text);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyCommandOptions(FFCommandOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
ffStrbufDestroy(&options->shell);
|
|
|
|
|
ffStrbufDestroy(&options->text);
|
|
|
|
|
}
|
2023-03-09 16:10:46 +08:00
|
|
|
|
2023-06-10 15:01:54 +08:00
|
|
|
void ffParseCommandJsonObject(FFinstance* instance, yyjson_val* module)
|
2023-03-09 16:10:46 +08:00
|
|
|
{
|
|
|
|
|
FFCommandOptions __attribute__((__cleanup__(ffDestroyCommandOptions))) options;
|
|
|
|
|
ffInitCommandOptions(&options);
|
|
|
|
|
|
2023-03-15 15:24:40 +08:00
|
|
|
if (module)
|
2023-03-09 16:10:46 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-03-15 15:24:40 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
2023-06-19 15:21:49 +08:00
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
2023-06-10 15:01:54 +08:00
|
|
|
continue;
|
|
|
|
|
|
2023-03-15 15:24:40 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
|
|
|
|
continue;
|
2023-03-15 11:24:49 +08:00
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "shell"))
|
2023-03-15 15:24:40 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
ffStrbufSetS(&options.shell, yyjson_get_str(val));
|
2023-03-15 15:24:40 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
2023-03-15 11:24:49 +08:00
|
|
|
|
2023-06-19 15:21:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(key, "text"))
|
2023-03-15 15:24:40 +08:00
|
|
|
{
|
2023-06-10 15:01:54 +08:00
|
|
|
ffStrbufSetS(&options.text, yyjson_get_str(val));
|
2023-03-15 15:24:40 +08:00
|
|
|
continue;
|
|
|
|
|
}
|
2023-03-15 11:24:49 +08:00
|
|
|
|
2023-03-15 15:24:40 +08:00
|
|
|
ffPrintError(instance, FF_COMMAND_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
|
|
|
|
}
|
2023-03-09 16:10:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffPrintCommand(instance, &options);
|
|
|
|
|
}
|