2023-03-10 14:14:32 +08:00
|
|
|
#include "common/printing.h"
|
2023-06-10 15:01:54 +08:00
|
|
|
#include "common/jsonconfig.h"
|
2023-03-10 14:14:32 +08:00
|
|
|
#include "modules/kernel/kernel.h"
|
2023-06-19 15:21:49 +08:00
|
|
|
#include "util/stringUtils.h"
|
2023-03-10 14:14:32 +08:00
|
|
|
|
2024-07-02 10:12:26 +08:00
|
|
|
#define FF_KERNEL_NUM_FORMAT_ARGS 6
|
2023-03-10 14:14:32 +08:00
|
|
|
|
2023-06-24 10:59:53 +08:00
|
|
|
void ffPrintKernel(FFKernelOptions* options)
|
2023-03-10 14:14:32 +08:00
|
|
|
{
|
2024-07-02 10:12:26 +08:00
|
|
|
const FFPlatformSysinfo* info = &instance.state.platform.sysinfo;
|
2023-03-10 14:14:32 +08:00
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
2023-08-15 21:40:22 +08:00
|
|
|
ffPrintLogoAndKey(FF_KERNEL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2024-07-02 10:12:26 +08:00
|
|
|
printf("%s %s", info->name.chars, info->release.chars);
|
2023-03-10 14:14:32 +08:00
|
|
|
|
2024-07-02 10:12:26 +08:00
|
|
|
if(info->displayVersion.length > 0)
|
|
|
|
|
printf(" (%s)\n", info->displayVersion.chars);
|
2023-09-23 11:51:37 +08:00
|
|
|
else
|
|
|
|
|
putchar('\n');
|
2023-03-10 14:14:32 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-02 10:12:26 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
|
|
|
|
|
ffParseSize(info->pageSize, &str);
|
2024-03-07 10:47:28 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_KERNEL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_KERNEL_NUM_FORMAT_ARGS, ((FFformatarg[]){
|
2024-07-02 10:12:26 +08:00
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &info->name, "sysname"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &info->release, "release"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &info->version, "version"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &info->architecture, "arch"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &info->displayVersion, "display-version"},
|
|
|
|
|
{FF_FORMAT_ARG_TYPE_STRBUF, &str, "page-size"},
|
2024-03-07 10:47:28 +08:00
|
|
|
}));
|
2023-03-10 14:14:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ffParseKernelCommandOptions(FFKernelOptions* options, const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, FF_KERNEL_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 ffParseKernelJsonObject(FFKernelOptions* options, yyjson_val* module)
|
2023-03-10 14:14:32 +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 14:14:32 +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 14:14:32 +08:00
|
|
|
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_KERNEL_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
|
2023-03-10 14:14:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-05 16:09:46 +08:00
|
|
|
|
2023-10-24 14:41:29 +08:00
|
|
|
void ffGenerateKernelJsonConfig(FFKernelOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyKernelOptions))) FFKernelOptions defaultOptions;
|
|
|
|
|
ffInitKernelOptions(&defaultOptions);
|
|
|
|
|
|
|
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 10:14:22 +08:00
|
|
|
void ffGenerateKernelJsonResult(FF_MAYBE_UNUSED FFKernelOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
2023-09-05 16:09:46 +08:00
|
|
|
{
|
2024-07-02 10:12:26 +08:00
|
|
|
const FFPlatformSysinfo* info = &instance.state.platform.sysinfo;
|
|
|
|
|
|
2023-09-05 16:09:46 +08:00
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
2024-07-02 10:12:26 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "architecture", &info->architecture);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "name", &info->name);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "release", &info->release);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "version", &info->version);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "displayVersion", &info->displayVersion);
|
|
|
|
|
yyjson_mut_obj_add_uint(doc, obj, "pageSize", info->pageSize);
|
2023-09-05 16:09:46 +08:00
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
|
|
|
|
void ffPrintKernelHelpFormat(void)
|
|
|
|
|
{
|
2024-05-13 16:41:00 +08:00
|
|
|
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_KERNEL_MODULE_NAME, "{1} {2}", FF_KERNEL_NUM_FORMAT_ARGS, ((const char* []) {
|
2024-05-27 16:41:37 +08:00
|
|
|
"Sysname - sysname",
|
|
|
|
|
"Release - release",
|
|
|
|
|
"Version - version",
|
|
|
|
|
"Architecture - arch",
|
|
|
|
|
"Display version - display-version",
|
2024-07-02 10:12:26 +08:00
|
|
|
"Page size - page-size",
|
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 ffInitKernelOptions(FFKernelOptions* options)
|
|
|
|
|
{
|
2023-10-23 13:39:07 +08:00
|
|
|
ffOptionInitModuleBaseInfo(
|
|
|
|
|
&options->moduleInfo,
|
|
|
|
|
FF_KERNEL_MODULE_NAME,
|
2023-11-17 14:12:17 +08:00
|
|
|
"Print system kernel version",
|
2023-10-23 13:39:07 +08:00
|
|
|
ffParseKernelCommandOptions,
|
|
|
|
|
ffParseKernelJsonObject,
|
|
|
|
|
ffPrintKernel,
|
|
|
|
|
ffGenerateKernelJsonResult,
|
|
|
|
|
ffPrintKernelHelpFormat,
|
2023-10-24 14:41:29 +08:00
|
|
|
ffGenerateKernelJsonConfig
|
2023-10-23 13:39:07 +08:00
|
|
|
);
|
2024-07-24 15:46:41 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
2023-10-23 13:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyKernelOptions(FFKernelOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|