2023-03-10 11:26:53 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-03-10 11:26:53 +08:00
|
|
|
#include "detection/host/host.h"
|
|
|
|
|
#include "modules/host/host.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-03-10 11:26:53 +08:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintHost(FFHostOptions* options)
|
2023-03-10 11:26:53 +08:00
|
|
|
{
|
2023-06-12 22:53:12 +08:00
|
|
|
FFHostResult host;
|
2024-01-05 20:38:29 +08:00
|
|
|
ffStrbufInit(&host.family);
|
|
|
|
|
ffStrbufInit(&host.name);
|
|
|
|
|
ffStrbufInit(&host.version);
|
|
|
|
|
ffStrbufInit(&host.sku);
|
|
|
|
|
ffStrbufInit(&host.serial);
|
|
|
|
|
ffStrbufInit(&host.uuid);
|
|
|
|
|
ffStrbufInit(&host.vendor);
|
2023-03-10 11:26:53 +08:00
|
|
|
|
2024-01-06 10:43:51 +08:00
|
|
|
const char* error = ffDetectHost(&host);
|
2023-06-12 22:53:12 +08:00
|
|
|
if(error)
|
2023-03-10 11:26:53 +08:00
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2023-06-12 22:53:12 +08:00
|
|
|
goto exit;
|
2023-03-10 11:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
2024-01-05 20:38:29 +08:00
|
|
|
if(host.family.length == 0 && host.name.length == 0)
|
2023-03-10 11:26:53 +08:00
|
|
|
{
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "neither product_family nor product_name is set by O.E.M.");
|
2023-06-12 22:53:12 +08:00
|
|
|
goto exit;
|
2023-03-10 11:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2023-03-10 11:26:53 +08:00
|
|
|
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY output = ffStrbufCreate();
|
2023-03-10 11:26:53 +08:00
|
|
|
|
2024-01-05 20:38:29 +08:00
|
|
|
if(host.name.length > 0)
|
|
|
|
|
ffStrbufAppend(&output, &host.name);
|
2023-03-10 11:26:53 +08:00
|
|
|
else
|
2024-01-05 20:38:29 +08:00
|
|
|
ffStrbufAppend(&output, &host.family);
|
2023-03-10 11:26:53 +08:00
|
|
|
|
2024-01-05 20:38:29 +08:00
|
|
|
if(host.version.length > 0)
|
|
|
|
|
ffStrbufAppendF(&output, " (%s)", host.version.chars);
|
2023-03-10 11:26:53 +08:00
|
|
|
|
|
|
|
|
ffStrbufPutTo(&output, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-11 10:47:02 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
|
2024-09-03 14:52:40 +08:00
|
|
|
FF_FORMAT_ARG(host.family, "family"),
|
|
|
|
|
FF_FORMAT_ARG(host.name, "name"),
|
|
|
|
|
FF_FORMAT_ARG(host.version, "version"),
|
|
|
|
|
FF_FORMAT_ARG(host.sku, "sku"),
|
|
|
|
|
FF_FORMAT_ARG(host.vendor, "vendor"),
|
|
|
|
|
FF_FORMAT_ARG(host.serial, "serial"),
|
|
|
|
|
FF_FORMAT_ARG(host.uuid, "uuid"),
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-03-10 11:26:53 +08:00
|
|
|
}
|
2023-06-12 22:53:12 +08:00
|
|
|
|
|
|
|
|
exit:
|
2024-01-05 20:38:29 +08:00
|
|
|
ffStrbufDestroy(&host.family);
|
|
|
|
|
ffStrbufDestroy(&host.name);
|
|
|
|
|
ffStrbufDestroy(&host.version);
|
|
|
|
|
ffStrbufDestroy(&host.sku);
|
|
|
|
|
ffStrbufDestroy(&host.serial);
|
|
|
|
|
ffStrbufDestroy(&host.uuid);
|
|
|
|
|
ffStrbufDestroy(&host.vendor);
|
2023-03-10 11:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseHostCommandOptions(FFHostOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_HOST_MODULE_NAME);
|
|
|
|
|
if (!subKey) return false;
|
|
|
|
|
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 10:56:54 +08:00
|
|
|
void ffParseHostJsonObject(FFHostOptions* options, yyjson_val* module)
|
2023-03-10 11:26:53 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
yyjson_val *key_, *val;
|
|
|
|
|
size_t idx, max;
|
|
|
|
|
yyjson_obj_foreach(module, idx, max, key_, val)
|
2023-03-10 11:26:53 +08:00
|
|
|
{
|
2023-08-17 16:09:41 +08:00
|
|
|
const char* key = yyjson_get_str(key_);
|
2025-07-18 16:16:38 +08:00
|
|
|
if(ffStrEqualsIgnCase(key, "type") || ffStrEqualsIgnCase(key, "condition"))
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
2023-06-10 15:01:54 +08:00
|
|
|
|
2023-08-17 16:09:41 +08:00
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
2023-03-10 11:26:53 +08:00
|
|
|
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
|
2023-03-10 11:26:53 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-05 15:57:18 +08:00
|
|
|
|
2023-10-24 14:41:29 +08:00
|
|
|
void ffGenerateHostJsonConfig(FFHostOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyHostOptions))) FFHostOptions defaultOptions;
|
|
|
|
|
ffInitHostOptions(&defaultOptions);
|
|
|
|
|
|
|
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 10:14:22 +08:00
|
|
|
void ffGenerateHostJsonResult(FF_MAYBE_UNUSED FFHostOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
2023-09-05 15:57:18 +08:00
|
|
|
{
|
|
|
|
|
FFHostResult host;
|
2024-01-05 20:38:29 +08:00
|
|
|
ffStrbufInit(&host.family);
|
|
|
|
|
ffStrbufInit(&host.name);
|
|
|
|
|
ffStrbufInit(&host.version);
|
|
|
|
|
ffStrbufInit(&host.sku);
|
|
|
|
|
ffStrbufInit(&host.serial);
|
|
|
|
|
ffStrbufInit(&host.uuid);
|
|
|
|
|
ffStrbufInit(&host.vendor);
|
2023-09-05 15:57:18 +08:00
|
|
|
|
2024-01-06 10:43:51 +08:00
|
|
|
const char* error = ffDetectHost(&host);
|
2023-09-05 15:57:18 +08:00
|
|
|
if (error)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", error);
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 20:38:29 +08:00
|
|
|
if (host.family.length == 0 && host.name.length == 0)
|
2023-09-05 15:57:18 +08:00
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", "neither product_family nor product_name is set by O.E.M.");
|
|
|
|
|
goto exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
2024-01-05 20:38:29 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "family", &host.family);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "name", &host.name);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "version", &host.version);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "sku", &host.sku);
|
2024-01-06 17:07:36 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &host.vendor);
|
2024-01-05 20:38:29 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "serial", &host.serial);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "uuid", &host.uuid);
|
2023-09-05 15:57:18 +08:00
|
|
|
|
|
|
|
|
exit:
|
2024-01-05 20:38:29 +08:00
|
|
|
ffStrbufDestroy(&host.family);
|
|
|
|
|
ffStrbufDestroy(&host.name);
|
|
|
|
|
ffStrbufDestroy(&host.version);
|
|
|
|
|
ffStrbufDestroy(&host.sku);
|
|
|
|
|
ffStrbufDestroy(&host.serial);
|
|
|
|
|
ffStrbufDestroy(&host.uuid);
|
|
|
|
|
ffStrbufDestroy(&host.vendor);
|
2023-09-05 15:57:18 +08:00
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
2024-12-11 10:47:02 +08:00
|
|
|
static FFModuleBaseInfo ffModuleInfo = {
|
|
|
|
|
.name = FF_HOST_MODULE_NAME,
|
|
|
|
|
.description = "Print product name of your computer",
|
|
|
|
|
.parseCommandOptions = (void*) ffParseHostCommandOptions,
|
|
|
|
|
.parseJsonObject = (void*) ffParseHostJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintHost,
|
|
|
|
|
.generateJsonResult = (void*) ffGenerateHostJsonResult,
|
|
|
|
|
.generateJsonConfig = (void*) ffGenerateHostJsonConfig,
|
|
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
|
|
|
|
{"Product family", "family"},
|
|
|
|
|
{"Product name", "name"},
|
|
|
|
|
{"Product version", "version"},
|
|
|
|
|
{"Product sku", "sku"},
|
|
|
|
|
{"Product vendor", "vendor"},
|
|
|
|
|
{"Product serial number", "serial"},
|
|
|
|
|
{"Product uuid", "uuid"},
|
|
|
|
|
}))
|
|
|
|
|
};
|
2023-10-23 13:32:18 +08:00
|
|
|
|
|
|
|
|
void ffInitHostOptions(FFHostOptions* options)
|
|
|
|
|
{
|
2024-12-11 10:47:02 +08:00
|
|
|
options->moduleInfo = ffModuleInfo;
|
2024-07-23 23:02:35 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
2023-10-23 13:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyHostOptions(FFHostOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|