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

142 lines
5.2 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
2026-05-28 15:58:08 +08:00
#include "common/strutil.h"
2024-05-28 23:46:13 +08:00
#include "detection/initsystem/initsystem.h"
#include "modules/initsystem/initsystem.h"
2026-03-29 09:28:49 +08:00
bool ffPrintInitSystem(FFInitSystemOptions* options) {
bool success = false;
2024-05-28 23:46:13 +08:00
FFInitSystemResult result = {
.name = ffStrbufCreate(),
2024-05-29 14:22:57 +08:00
.exe = ffStrbufCreate(),
.version = ffStrbufCreate(),
2024-05-28 23:46:13 +08:00
.pid = 1,
};
const char* error = ffDetectInitSystem(&result);
2026-03-29 09:28:49 +08:00
if (error) {
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(InitSystem), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
2024-05-28 23:46:13 +08:00
goto exit;
}
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.outputFormat.length == 0) {
2026-08-14 23:33:54 +08:00
ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(InitSystem), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2024-05-29 14:22:57 +08:00
ffStrbufWriteTo(&result.name, stdout);
2026-03-29 09:28:49 +08:00
if (result.version.length) {
printf(" %s\n", result.version.chars);
2026-03-29 09:28:49 +08:00
} else {
2024-05-29 14:22:57 +08:00
putchar('\n');
2026-03-29 09:28:49 +08:00
}
} else {
2026-08-14 23:33:54 +08:00
FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(InitSystem), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(result.name, "name"),
FF_ARG(result.exe, "exe"),
FF_ARG(result.version, "version"),
FF_ARG(result.pid, "pid"),
}));
2024-05-28 23:46:13 +08:00
}
success = true;
2024-05-28 23:46:13 +08:00
exit:
ffStrbufDestroy(&result.name);
ffStrbufDestroy(&result.exe);
2024-06-06 09:25:02 +08:00
ffStrbufDestroy(&result.version);
return success;
2024-05-28 23:46:13 +08:00
}
2026-03-29 09:28:49 +08:00
void ffParseInitSystemJsonObject(FFInitSystemOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2024-05-28 23:46:13 +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)) {
2024-05-28 23:46:13 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2024-05-28 23:46:13 +08:00
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(InitSystem), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2024-05-28 23:46:13 +08:00
}
}
2026-03-29 09:28:49 +08:00
void ffGenerateInitSystemJsonConfig(FFInitSystemOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2024-05-28 23:46:13 +08:00
}
bool ffGenerateInitSystemJsonResult([[maybe_unused]] FFInitSystemOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
bool success = false;
2024-05-28 23:46:13 +08:00
FFInitSystemResult result = {
.name = ffStrbufCreate(),
2024-05-29 14:22:57 +08:00
.exe = ffStrbufCreate(),
.version = ffStrbufCreate(),
2024-05-28 23:46:13 +08:00
.pid = 1,
};
const char* error = ffDetectInitSystem(&result);
2026-03-29 09:28:49 +08:00
if (error) {
2024-05-28 23:46:13 +08:00
yyjson_mut_obj_add_str(doc, module, "error", error);
goto exit;
}
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_strbuf(doc, obj, "name", &result.name);
yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result.exe);
2024-05-29 14:22:57 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
2024-05-28 23:46:13 +08:00
yyjson_mut_obj_add_uint(doc, obj, "pid", result.pid);
success = true;
2024-05-28 23:46:13 +08:00
exit:
ffStrbufDestroy(&result.name);
ffStrbufDestroy(&result.exe);
2024-06-06 09:25:02 +08:00
ffStrbufDestroy(&result.version);
return success;
2024-05-28 23:46:13 +08:00
}
2026-03-29 09:28:49 +08:00
void ffInitInitSystemOptions(FFInitSystemOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "󰿄");
}
2026-03-29 09:28:49 +08:00
void ffDestroyInitSystemOptions(FFInitSystemOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffInitSystemModuleInfo = {
2026-08-14 23:33:54 +08:00
.name = "InitSystem",
2024-12-11 10:47:02 +08:00
.description = "Print init system (pid 1) name and version",
2026-08-14 20:38:58 +08:00
.displayName = {
.en = "Init System",
2026-08-16 10:51:42 +08:00
.ar = "نظام الإقلاع",
2026-08-21 10:19:06 +08:00
.cs = "Init systém",
2026-08-14 20:38:58 +08:00
.de = "Init-System",
.es = "Sistema de inicio",
.fr = "Système d'init",
2026-08-20 16:25:32 +00:00
.gl = "Sistema de inicio",
2026-08-16 10:51:42 +08:00
.he = "מערכת אתחול",
2026-08-21 10:19:06 +08:00
.id = "Sistem Init",
2026-08-14 20:38:58 +08:00
.it = "Sistema di init",
.ja = "initシステム",
.ko = "초기화 시스템",
.pl = "System init",
2026-08-16 10:37:06 +08:00
.pt = "Sistema de inicialização",
2026-08-16 17:51:44 +02:00
.ru = "Система инициализации",
2026-08-21 10:19:06 +08:00
.tr = "Init Sistemi",
.uk = "Init-система",
.vi = "Hệ thống init",
2026-08-14 20:38:58 +08:00
.zh_CN = "Init 系统",
.zh_TW = "Init 系統",
},
.initOptions = (void*) ffInitInitSystemOptions,
.destroyOptions = (void*) ffDestroyInitSystemOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseInitSystemJsonObject,
.printModule = (void*) ffPrintInitSystem,
.generateJsonResult = (void*) ffGenerateInitSystemJsonResult,
.generateJsonConfig = (void*) ffGenerateInitSystemJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
2026-03-30 10:27:23 +08:00
{ "Init system name", "name" },
{ "Init system exe path", "exe" },
{ "Init system version path", "version" },
{ "Init system pid", "pid" },
})),
.defaultOrder = 10,
};