master ohmu/fastfetch-openbsd / src / modules / initsystem / initsystem.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/initsystem/initsystem.h"
  5#include "modules/initsystem/initsystem.h"
  6
  7bool ffPrintInitSystem(FFInitSystemOptions* options) {
  8    bool success = false;
  9    FFInitSystemResult result = {
 10        .name = ffStrbufCreate(),
 11        .exe = ffStrbufCreate(),
 12        .version = ffStrbufCreate(),
 13        .pid = 1,
 14    };
 15
 16    const char* error = ffDetectInitSystem(&result);
 17
 18    if (error) {
 19        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(InitSystem), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 20        goto exit;
 21    }
 22
 23    if (options->moduleArgs.outputFormat.length == 0) {
 24        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(InitSystem), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 25        ffStrbufWriteTo(&result.name, stdout);
 26        if (result.version.length) {
 27            printf(" %s\n", result.version.chars);
 28        } else {
 29            putchar('\n');
 30        }
 31    } else {
 32        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(InitSystem), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 33                                                                                                                FF_ARG(result.name, "name"),
 34                                                                                                                FF_ARG(result.exe, "exe"),
 35                                                                                                                FF_ARG(result.version, "version"),
 36                                                                                                                FF_ARG(result.pid, "pid"),
 37                                                                                                            }));
 38    }
 39    success = true;
 40
 41exit:
 42    ffStrbufDestroy(&result.name);
 43    ffStrbufDestroy(&result.exe);
 44    ffStrbufDestroy(&result.version);
 45
 46    return success;
 47}
 48
 49void ffParseInitSystemJsonObject(FFInitSystemOptions* options, yyjson_val* module) {
 50    yyjson_val *key, *val;
 51    size_t idx, max;
 52    yyjson_obj_foreach (module, idx, max, key, val) {
 53        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 54            continue;
 55        }
 56
 57        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(InitSystem), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 58    }
 59}
 60
 61[[gnu::cold]]
 62void ffGenerateInitSystemJsonConfig(FFInitSystemOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 63    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 64}
 65
 66bool ffGenerateInitSystemJsonResult([[maybe_unused]] FFInitSystemOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 67    bool success = false;
 68    FFInitSystemResult result = {
 69        .name = ffStrbufCreate(),
 70        .exe = ffStrbufCreate(),
 71        .version = ffStrbufCreate(),
 72        .pid = 1,
 73    };
 74
 75    const char* error = ffDetectInitSystem(&result);
 76
 77    if (error) {
 78        yyjson_mut_obj_add_str(doc, module, "error", error);
 79        goto exit;
 80    }
 81
 82    yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
 83    yyjson_mut_obj_add_strbuf(doc, obj, "name", &result.name);
 84    yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result.exe);
 85    yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
 86    yyjson_mut_obj_add_uint(doc, obj, "pid", result.pid);
 87    success = true;
 88
 89exit:
 90    ffStrbufDestroy(&result.name);
 91    ffStrbufDestroy(&result.exe);
 92    ffStrbufDestroy(&result.version);
 93    return success;
 94}
 95
 96void ffInitInitSystemOptions(FFInitSystemOptions* options) {
 97    ffOptionInitModuleArg(&options->moduleArgs, "󰿄");
 98}
 99
100void ffDestroyInitSystemOptions(FFInitSystemOptions* options) {
101    ffOptionDestroyModuleArg(&options->moduleArgs);
102}
103
104FFModuleBaseInfo ffInitSystemModuleInfo = {
105    .name = "InitSystem",
106    .description = "Print init system (pid 1) name and version",
107    .displayName = {
108        .en = "Init System",
109        .ar = "نظام الإقلاع",
110        .cs = "Init systém",
111        .de = "Init-System",
112        .es = "Sistema de inicio",
113        .fi = "Init-järjestelmä",
114        .fr = "Système d'init",
115        .gl = "Sistema de inicio",
116        .he = "מערכת אתחול",
117        .id = "Sistem Init",
118        .it = "Sistema di init",
119        .ja = "initシステム",
120        .ko = "초기화 시스템",
121        .pl = "System init",
122        .pt = "Sistema de inicialização",
123        .ru = "Система инициализации",
124        .tr = "Init Sistemi",
125        .uk = "Init-система",
126        .vi = "Hệ thống init",
127        .zh_CN = "Init 系统",
128        .zh_TW = "Init 系統",
129    },
130    .initOptions = (void*) ffInitInitSystemOptions,
131    .destroyOptions = (void*) ffDestroyInitSystemOptions,
132    .parseJsonObject = (void*) ffParseInitSystemJsonObject,
133    .printModule = (void*) ffPrintInitSystem,
134    .generateJsonResult = (void*) ffGenerateInitSystemJsonResult,
135    .generateJsonConfig = (void*) ffGenerateInitSystemJsonConfig,
136    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
137        { "Init system name", "name" },
138        { "Init system exe path", "exe" },
139        { "Init system version path", "version" },
140        { "Init system pid", "pid" },
141    })),
142    .defaultOrder = 10,
143};