Files
fastfetch/src/modules/os/os.c
T

227 lines
7.7 KiB
C
Raw Normal View History

#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-03-07 14:56:13 +08:00
#include "common/option.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"
#include "util/stringUtils.h"
2021-02-18 22:25:36 +01:00
2022-07-21 11:33:19 +02:00
#include <ctype.h>
2021-04-15 18:59:26 +02:00
#define FF_OS_NUM_FORMAT_ARGS 12
static void buildOutputDefault(const FFOSResult* os, FFstrbuf* result)
2021-02-18 22:25:36 +01:00
{
2021-07-12 19:01:31 +02:00
//Create the basic output
2024-06-25 10:16:19 +08:00
if(os->prettyName.length > 0)
2022-09-04 13:56:14 +02:00
ffStrbufAppend(result, &os->prettyName);
2024-06-25 10:16:19 +08:00
else if(os->name.length > 0)
ffStrbufAppend(result, &os->name);
2022-07-21 11:33:19 +02:00
else if(os->id.length > 0)
ffStrbufAppend(result, &os->id);
2021-07-12 19:01:31 +02:00
else
ffStrbufAppend(result, &instance.state.platform.systemName);
2021-07-12 19:01:31 +02:00
//Append code name if it is missing
2024-06-07 02:36:17 +00:00
if(os->codename.length > 0 && !ffStrbufContainIgnCase(result, &os->codename))
2022-09-21 14:52:07 +08:00
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->codename);
}
2021-07-12 19:01:31 +02:00
//Append version if it is missing
2024-06-07 02:36:17 +00:00
if(os->versionID.length > 0 && !ffStrbufContainIgnCase(result, &os->versionID))
2021-05-05 21:12:42 +02:00
{
2022-07-21 11:33:19 +02:00
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->versionID);
2021-04-15 18:59:26 +02:00
}
2024-06-07 02:36:17 +00:00
else if(os->versionID.length == 0 && os->version.length > 0 && !ffStrbufContainIgnCase(result, &os->version))
2021-04-15 18:59:26 +02:00
{
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
2021-07-12 19:01:31 +02:00
//Append variant if it is missing
2024-06-07 02:36:17 +00:00
if(os->variant.length > 0 && !ffStrbufContainIgnCase(result, &os->variant))
2021-05-05 21:12:42 +02:00
{
2022-07-21 11:33:19 +02:00
ffStrbufAppendS(result, " (");
ffStrbufAppend(result, &os->variant);
ffStrbufAppendC(result, ')');
2021-05-05 21:12:42 +02:00
}
2024-06-07 02:36:17 +00:00
else if(os->variant.length == 0 && os->variantID.length > 0 && !ffStrbufContainIgnCase(result, &os->variantID))
2021-04-15 18:59:26 +02:00
{
2022-07-21 11:33:19 +02:00
ffStrbufAppendS(result, " (");
ffStrbufAppend(result, &os->variantID);
ffStrbufAppendC(result, ')');
2021-03-19 20:57:07 +01:00
}
2021-02-22 16:52:51 +01:00
2021-07-12 19:01:31 +02:00
//Append architecture if it is missing
2024-06-07 02:36:17 +00:00
if(!ffStrbufContainIgnCase(result, &instance.state.platform.systemArchitecture))
2022-07-21 11:33:19 +02:00
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &instance.state.platform.systemArchitecture);
2022-07-21 11:33:19 +02:00
}
}
static void buildOutputNixOS(const FFOSResult* os, FFstrbuf* result)
2022-07-21 11:33:19 +02:00
{
ffStrbufAppendS(result, "NixOS");
2021-07-12 19:01:31 +02:00
2022-07-21 11:33:19 +02:00
if(os->buildID.length > 0)
2021-07-12 19:01:31 +02:00
{
2022-07-21 11:33:19 +02:00
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &os->buildID);
}
if(os->codename.length > 0)
{
ffStrbufAppendS(result, " (");
ffStrbufAppendC(result, (char) toupper(os->codename.chars[0]));
ffStrbufAppendS(result, os->codename.chars + 1);
ffStrbufAppendC(result, ')');
}
if(instance.state.platform.systemArchitecture.length > 0)
2022-07-21 11:33:19 +02:00
{
ffStrbufAppendC(result, ' ');
ffStrbufAppend(result, &instance.state.platform.systemArchitecture);
2022-07-21 11:33:19 +02:00
}
}
void ffPrintOS(FFOSOptions* options)
2022-07-21 11:33:19 +02:00
{
const FFOSResult* os = ffDetectOS();
2022-07-21 11:33:19 +02:00
2023-01-15 15:57:26 +01:00
if(os->name.length == 0 && os->prettyName.length == 0 && os->id.length == 0)
2022-07-21 11:33:19 +02:00
{
ffPrintError(FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Could not detect OS");
2022-07-21 11:33:19 +02:00
return;
2021-07-12 19:01:31 +02:00
}
2021-04-15 18:59:26 +02:00
2023-03-07 14:56:13 +08:00
if(options->moduleArgs.outputFormat.length == 0)
2022-06-04 22:43:57 +02:00
{
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
2022-07-21 11:33:19 +02:00
if(ffStrbufIgnCaseCompS(&os->id, "nixos") == 0)
buildOutputNixOS(os, &result);
2022-07-21 11:33:19 +02:00
else
buildOutputDefault(os, &result);
2022-07-21 11:33:19 +02:00
ffPrintLogoAndKey(FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2022-07-21 11:33:19 +02:00
ffStrbufPutTo(&result, stdout);
2022-06-04 22:43:57 +02:00
}
else
{
FF_PRINT_FORMAT_CHECKED(FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_OS_NUM_FORMAT_ARGS, ((FFformatarg[]){
2024-05-27 16:41:37 +08:00
{FF_FORMAT_ARG_TYPE_STRBUF, &instance.state.platform.systemName, "sysname"},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->name, "name"},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->prettyName, "pretty-name"},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->id, "id"},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->idLike, "id-like"},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->variant, "variant"},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->variantID, "variant-id"},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->version, "version"},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->versionID, "version-id"},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->codename, "codename"},
{FF_FORMAT_ARG_TYPE_STRBUF, &os->buildID, "build-id"},
{FF_FORMAT_ARG_TYPE_STRBUF, &instance.state.platform.systemArchitecture, "arch"}
}));
2022-06-04 22:43:57 +02:00
}
2021-03-27 18:14:19 +01:00
}
2023-03-07 14:56:13 +08:00
bool ffParseOSCommandOptions(FFOSOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_OS_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 ffParseOSJsonObject(FFOSOptions* options, yyjson_val* module)
2023-03-09 16:14:08 +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-09 16:14:08 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
ffPrintError(FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
2023-03-09 16:14:08 +08:00
}
}
2023-09-07 09:36:04 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateOSJsonConfig(FFOSOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyOSOptions))) FFOSOptions defaultOptions;
ffInitOSOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
}
2023-10-23 10:14:22 +08:00
void ffGenerateOSJsonResult(FF_MAYBE_UNUSED FFOSOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-09-07 09:36:04 +08:00
{
const FFOSResult* os = ffDetectOS();
if(os->name.length == 0 && os->prettyName.length == 0 && os->id.length == 0)
{
yyjson_mut_obj_add_str(doc, module, "error", "Could not detect OS");
return;
}
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);
}
2023-10-07 13:40:40 +08:00
void ffPrintOSHelpFormat(void)
{
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_OS_MODULE_NAME, "{3} {10} {12}", FF_OS_NUM_FORMAT_ARGS, ((const char* []) {
2024-05-27 16:41:37 +08:00
"Name of the kernel (Linux, WIN32_NT, Darwin, FreeBSD) - sysname",
"Name of the OS - name",
"Pretty name of the OS - 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",
}));
2023-10-07 13:40:40 +08:00
}
2023-10-23 13:32:18 +08:00
void ffInitOSOptions(FFOSOptions* options)
{
ffOptionInitModuleBaseInfo(
&options->moduleInfo,
FF_OS_MODULE_NAME,
"Print operating system name and version",
ffParseOSCommandOptions,
ffParseOSJsonObject,
ffPrintOS,
ffGenerateOSJsonResult,
ffPrintOSHelpFormat,
2023-10-24 14:41:29 +08:00
ffGenerateOSJsonConfig
);
2023-10-23 13:32:18 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
}
void ffDestroyOSOptions(FFOSOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}