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
|
|
|
|
|
|
|
|
#define FF_HOST_NUM_FORMAT_ARGS 5
|
|
|
|
|
|
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;
|
2023-06-22 10:45:43 +00:00
|
|
|
ffStrbufInit(&host.productFamily);
|
|
|
|
|
ffStrbufInit(&host.productName);
|
|
|
|
|
ffStrbufInit(&host.productVersion);
|
|
|
|
|
ffStrbufInit(&host.productSku);
|
|
|
|
|
ffStrbufInit(&host.sysVendor);
|
2023-06-12 22:53:12 +08:00
|
|
|
const char* error = ffDetectHost(&host);
|
2023-03-10 11:26:53 +08:00
|
|
|
|
2023-06-12 22:53:12 +08:00
|
|
|
if(error)
|
2023-03-10 11:26:53 +08:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
2023-06-12 22:53:12 +08:00
|
|
|
goto exit;
|
2023-03-10 11:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-12 22:53:12 +08:00
|
|
|
if(host.productFamily.length == 0 && host.productName.length == 0)
|
2023-03-10 11:26:53 +08:00
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, "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
|
|
|
|
2023-06-12 22:53:12 +08:00
|
|
|
if(host.productName.length > 0)
|
|
|
|
|
ffStrbufAppend(&output, &host.productName);
|
2023-03-10 11:26:53 +08:00
|
|
|
else
|
2023-06-12 22:53:12 +08:00
|
|
|
ffStrbufAppend(&output, &host.productFamily);
|
2023-03-10 11:26:53 +08:00
|
|
|
|
2023-06-12 22:53:12 +08:00
|
|
|
if(host.productVersion.length > 0 && !ffStrbufIgnCaseEqualS(&host.productVersion, "none"))
|
2023-03-10 11:26:53 +08:00
|
|
|
{
|
2023-06-12 22:53:12 +08:00
|
|
|
ffStrbufAppendF(&output, " (%s)", host.productVersion.chars);
|
2023-03-10 11:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffStrbufPutTo(&output, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintFormat(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_HOST_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
2023-06-12 22:53:12 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.productFamily},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.productName},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.productVersion},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.productSku},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &host.sysVendor}
|
2023-03-10 11:26:53 +08:00
|
|
|
});
|
|
|
|
|
}
|
2023-06-12 22:53:12 +08:00
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
ffStrbufDestroy(&host.productFamily);
|
|
|
|
|
ffStrbufDestroy(&host.productName);
|
|
|
|
|
ffStrbufDestroy(&host.productVersion);
|
|
|
|
|
ffStrbufDestroy(&host.productSku);
|
|
|
|
|
ffStrbufDestroy(&host.sysVendor);
|
2023-03-10 11:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffInitHostOptions(FFHostOptions* options)
|
|
|
|
|
{
|
|
|
|
|
options->moduleName = FF_HOST_MODULE_NAME;
|
|
|
|
|
ffOptionInitModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyHostOptions(FFHostOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffParseHostJsonObject(yyjson_val* module)
|
2023-03-10 11:26:53 +08:00
|
|
|
{
|
|
|
|
|
FFHostOptions __attribute__((__cleanup__(ffDestroyHostOptions))) options;
|
|
|
|
|
ffInitHostOptions(&options);
|
|
|
|
|
|
2023-03-15 15:24:40 +08:00
|
|
|
if (module)
|
2023-03-10 11:26:53 +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-10 11:26:53 +08:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintError(FF_HOST_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
2023-03-15 15:24:40 +08:00
|
|
|
}
|
2023-03-10 11:26:53 +08:00
|
|
|
}
|
|
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
ffPrintHost(&options);
|
2023-03-10 11:26:53 +08:00
|
|
|
}
|