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
|
|
|
|
2024-01-05 16:29:38 +08:00
|
|
|
#define FF_HOST_NUM_FORMAT_ARGS 7
|
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-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_HOST_NUM_FORMAT_ARGS, ((FFformatarg[]) {
|
2024-01-05 20:38:29 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.family},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.name},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.version},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.sku},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.vendor},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.serial},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.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_);
|
|
|
|
|
if(ffStrEqualsIgnCase(key, "type"))
|
|
|
|
|
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
|
|
|
|
|
|
|
|
void ffPrintHostHelpFormat(void)
|
|
|
|
|
{
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_HOST_MODULE_NAME, "{2} {3}", FF_HOST_NUM_FORMAT_ARGS, ((const char* []) {
|
2023-10-07 13:40:40 +08:00
|
|
|
"product family",
|
|
|
|
|
"product name",
|
|
|
|
|
"product version",
|
|
|
|
|
"product sku",
|
2024-01-05 22:44:40 +08:00
|
|
|
"product vendor",
|
2024-01-05 16:29:38 +08:00
|
|
|
"product serial number",
|
|
|
|
|
"product uuid",
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-10-07 13:40:40 +08:00
|
|
|
}
|
2023-10-23 13:32:18 +08:00
|
|
|
|
|
|
|
|
void ffInitHostOptions(FFHostOptions* options)
|
|
|
|
|
{
|
2023-10-23 13:39:07 +08:00
|
|
|
ffOptionInitModuleBaseInfo(
|
|
|
|
|
&options->moduleInfo,
|
|
|
|
|
FF_HOST_MODULE_NAME,
|
2023-11-17 14:12:17 +08:00
|
|
|
"Print product name of your computer",
|
2023-10-23 13:39:07 +08:00
|
|
|
ffParseHostCommandOptions,
|
|
|
|
|
ffParseHostJsonObject,
|
|
|
|
|
ffPrintHost,
|
|
|
|
|
ffGenerateHostJsonResult,
|
|
|
|
|
ffPrintHostHelpFormat,
|
2023-10-24 14:41:29 +08:00
|
|
|
ffGenerateHostJsonConfig
|
2023-10-23 13:39:07 +08:00
|
|
|
);
|
2023-10-23 13:32:18 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyHostOptions(FFHostOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|