2026-01-06 09:48:38 +08:00
|
|
|
#include "common/printing.h"
|
|
|
|
|
#include "common/jsonconfig.h"
|
|
|
|
|
#include "common/option.h"
|
2026-05-28 15:58:08 +08:00
|
|
|
#include "common/strutil.h"
|
2022-09-04 13:33:18 +02:00
|
|
|
#include "detection/os/os.h"
|
2023-03-07 14:56:13 +08:00
|
|
|
#include "modules/os/os.h"
|
2021-02-18 22:25:36 +01:00
|
|
|
|
2022-07-21 11:33:19 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
static void buildOutputDefault(const FFOSResult* os, FFstrbuf* result) {
|
|
|
|
|
// Create the basic output
|
|
|
|
|
if (os->name.length > 0) {
|
2024-06-25 10:16:19 +08:00
|
|
|
ffStrbufAppend(result, &os->name);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else if (os->prettyName.length > 0) {
|
2024-07-03 16:37:51 +08:00
|
|
|
ffStrbufAppend(result, &os->prettyName);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else if (os->id.length > 0) {
|
2022-07-21 11:33:19 +02:00
|
|
|
ffStrbufAppend(result, &os->id);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2024-07-02 10:12:26 +08:00
|
|
|
ffStrbufAppend(result, &instance.state.platform.sysinfo.name);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2021-07-12 19:01:31 +02:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
// Append code name if it is missing
|
|
|
|
|
if (os->codename.length > 0 && !ffStrbufContainIgnCase(result, &os->codename)) {
|
2022-09-21 14:52:07 +08:00
|
|
|
ffStrbufAppendC(result, ' ');
|
|
|
|
|
ffStrbufAppend(result, &os->codename);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
// Append version if it is missing
|
|
|
|
|
if (os->versionID.length > 0 && !ffStrbufContainIgnCase(result, &os->versionID)) {
|
2022-07-21 11:33:19 +02:00
|
|
|
ffStrbufAppendC(result, ' ');
|
|
|
|
|
ffStrbufAppend(result, &os->versionID);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else if (os->versionID.length == 0 && os->version.length > 0 && !ffStrbufContainIgnCase(result, &os->version)) {
|
2022-07-21 11:33:19 +02:00
|
|
|
ffStrbufAppendC(result, ' ');
|
|
|
|
|
ffStrbufAppend(result, &os->version);
|
2021-04-15 18:59:26 +02:00
|
|
|
}
|
2021-05-05 21:12:42 +02:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
// Append variant if it is missing
|
|
|
|
|
if (os->variant.length > 0 && !ffStrbufContainIgnCase(result, &os->variant)) {
|
2022-07-21 11:33:19 +02:00
|
|
|
ffStrbufAppendS(result, " (");
|
|
|
|
|
ffStrbufAppend(result, &os->variant);
|
|
|
|
|
ffStrbufAppendC(result, ')');
|
2026-03-29 09:28:49 +08:00
|
|
|
} else if (os->variant.length == 0 && os->variantID.length > 0 && !ffStrbufContainIgnCase(result, &os->variantID)) {
|
2022-07-21 11:33:19 +02:00
|
|
|
ffStrbufAppendS(result, " (");
|
|
|
|
|
ffStrbufAppend(result, &os->variantID);
|
|
|
|
|
ffStrbufAppendC(result, ')');
|
2021-03-19 20:57:07 +01:00
|
|
|
}
|
2022-07-21 11:33:19 +02:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
bool ffPrintOS(FFOSOptions* options) {
|
2023-06-24 10:59:53 +08:00
|
|
|
const FFOSResult* os = ffDetectOS();
|
2022-07-21 11:33:19 +02:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (os->name.length == 0 && os->prettyName.length == 0 && os->id.length == 0) {
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Could not detect OS");
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2021-07-12 19:01:31 +02:00
|
|
|
}
|
2021-04-15 18:59:26 +02:00
|
|
|
|
2025-02-20 15:59:28 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (options->moduleArgs.key.length == 0) {
|
2025-02-20 15:59:28 +08:00
|
|
|
ffStrbufSetStatic(&key, FF_OS_MODULE_NAME);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2025-02-20 15:59:28 +08:00
|
|
|
FF_PARSE_FORMAT_STRING_CHECKED(&key, &options->moduleArgs.key, ((FFformatarg[]) {
|
2026-03-29 09:28:49 +08:00
|
|
|
FF_ARG(instance.state.platform.sysinfo.name, "sysname"),
|
|
|
|
|
FF_ARG(os->name, "name"),
|
|
|
|
|
FF_ARG(options->moduleArgs.keyIcon, "icon"),
|
|
|
|
|
}));
|
2025-02-20 15:59:28 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (options->moduleArgs.outputFormat.length == 0) {
|
2023-04-15 15:29:54 +08:00
|
|
|
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
|
2022-07-21 11:33:19 +02:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (os->prettyName.length > 0) {
|
2025-01-06 08:58:14 +08:00
|
|
|
ffStrbufAppend(&result, &os->prettyName);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2023-06-24 10:59:53 +08:00
|
|
|
buildOutputDefault(os, &result);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2022-07-21 11:33:19 +02:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
// Append architecture if it is missing
|
|
|
|
|
if (!ffStrbufContainIgnCase(&result, &instance.state.platform.sysinfo.architecture)) {
|
2025-01-06 08:58:14 +08:00
|
|
|
ffStrbufAppendC(&result, ' ');
|
|
|
|
|
ffStrbufAppend(&result, &instance.state.platform.sysinfo.architecture);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-20 15:59:28 +08:00
|
|
|
ffPrintLogoAndKey(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY);
|
2022-07-21 11:33:19 +02:00
|
|
|
ffStrbufPutTo(&result, stdout);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
|
|
|
|
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
|
|
|
|
|
FF_ARG(instance.state.platform.sysinfo.name, "sysname"),
|
|
|
|
|
FF_ARG(os->name, "name"),
|
|
|
|
|
FF_ARG(os->prettyName, "pretty-name"),
|
|
|
|
|
FF_ARG(os->id, "id"),
|
|
|
|
|
FF_ARG(os->idLike, "id-like"),
|
|
|
|
|
FF_ARG(os->variant, "variant"),
|
|
|
|
|
FF_ARG(os->variantID, "variant-id"),
|
|
|
|
|
FF_ARG(os->version, "version"),
|
|
|
|
|
FF_ARG(os->versionID, "version-id"),
|
|
|
|
|
FF_ARG(os->codename, "codename"),
|
|
|
|
|
FF_ARG(os->buildID, "build-id"),
|
|
|
|
|
FF_ARG(instance.state.platform.sysinfo.architecture, "arch"),
|
|
|
|
|
FF_ARG(instance.state.platform.sysinfo.release, "kernel-release"),
|
|
|
|
|
}));
|
2022-06-04 22:43:57 +02:00
|
|
|
}
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2021-03-27 18:14:19 +01:00
|
|
|
}
|
2023-03-07 14:56:13 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffParseOSJsonObject(FFOSOptions* options, yyjson_val* module) {
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_val *key, *val;
|
2023-08-17 16:09:41 +08:00
|
|
|
size_t idx, max;
|
2026-03-29 09:28:49 +08:00
|
|
|
yyjson_obj_foreach (module, idx, max, key, val) {
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
|
2023-08-17 16:09:41 +08:00
|
|
|
continue;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-08-17 16:09:41 +08:00
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
ffPrintError(FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
|
2023-03-09 16:14:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-07 09:36:04 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffGenerateOSJsonConfig(FFOSOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2025-08-04 16:46:28 +08:00
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
2023-10-24 14:41:29 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-09 16:20:59 +08:00
|
|
|
bool ffGenerateOSJsonResult(FF_A_UNUSED FFOSOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2023-09-07 09:36:04 +08:00
|
|
|
const FFOSResult* os = ffDetectOS();
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (os->name.length == 0 && os->prettyName.length == 0 && os->id.length == 0) {
|
2023-09-07 09:36:04 +08:00
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", "Could not detect OS");
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2023-09-07 09:36:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "buildID", &os->buildID);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "codename", &os->codename);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "id", &os->id);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "idLike", &os->idLike);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "name", &os->name);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "prettyName", &os->prettyName);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "variant", &os->variant);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "variantID", &os->variantID);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "version", &os->version);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "versionID", &os->versionID);
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2023-09-07 09:36:04 +08:00
|
|
|
}
|
2023-10-07 13:40:40 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffInitOSOptions(FFOSOptions* options) {
|
2024-07-23 23:02:35 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs,
|
2026-03-29 09:28:49 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
""
|
|
|
|
|
#elif __APPLE__
|
|
|
|
|
""
|
|
|
|
|
#elif __FreeBSD__
|
|
|
|
|
""
|
|
|
|
|
#elif __ANDROID__
|
|
|
|
|
""
|
|
|
|
|
#elif __linux__
|
|
|
|
|
""
|
|
|
|
|
#elif __sun
|
|
|
|
|
""
|
|
|
|
|
#elif __OpenBSD__
|
|
|
|
|
""
|
|
|
|
|
#elif __Haiku__
|
|
|
|
|
""
|
|
|
|
|
#else
|
|
|
|
|
""
|
|
|
|
|
#endif
|
2024-07-23 23:02:35 +08:00
|
|
|
);
|
2023-10-23 13:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffDestroyOSOptions(FFOSOptions* options) {
|
2023-10-23 13:32:18 +08:00
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
2025-08-04 14:25:14 +08:00
|
|
|
|
|
|
|
|
FFModuleBaseInfo ffOSModuleInfo = {
|
|
|
|
|
.name = FF_OS_MODULE_NAME,
|
2026-04-16 18:27:11 +08:00
|
|
|
.description = "Print the OS or Linux distribution name and version",
|
2025-08-04 14:25:14 +08:00
|
|
|
.initOptions = (void*) ffInitOSOptions,
|
|
|
|
|
.destroyOptions = (void*) ffDestroyOSOptions,
|
|
|
|
|
.parseJsonObject = (void*) ffParseOSJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintOS,
|
|
|
|
|
.generateJsonResult = (void*) ffGenerateOSJsonResult,
|
|
|
|
|
.generateJsonConfig = (void*) ffGenerateOSJsonConfig,
|
|
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
2026-03-30 10:27:23 +08:00
|
|
|
{ "Name of the kernel", "sysname" },
|
|
|
|
|
{ "Name of the OS", "name" },
|
|
|
|
|
{ "Pretty name of the OS, if available", "pretty-name" },
|
|
|
|
|
{ "ID of the OS", "id" },
|
|
|
|
|
{ "ID like of the OS", "id-like" },
|
|
|
|
|
{ "Variant of the OS", "variant" },
|
|
|
|
|
{ "Variant ID of the OS", "variant-id" },
|
|
|
|
|
{ "Version of the OS", "version" },
|
|
|
|
|
{ "Version ID of the OS", "version-id" },
|
|
|
|
|
{ "Version codename of the OS", "codename" },
|
|
|
|
|
{ "Build ID of the OS", "build-id" },
|
|
|
|
|
{ "Architecture of the OS", "arch" },
|
2026-03-30 10:17:45 +08:00
|
|
|
}))
|
|
|
|
|
};
|