summaryrefslogtreecommitdiffstats
path: root/src/modules/host
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/host')
-rw-r--r--src/modules/host/host.c156
-rw-r--r--src/modules/host/host.h11
-rw-r--r--src/modules/host/option.h9
3 files changed, 176 insertions, 0 deletions
diff --git a/src/modules/host/host.c b/src/modules/host/host.c
new file mode 100644
index 0000000..2a53cd3
--- /dev/null
+++ b/src/modules/host/host.c
@@ -0,0 +1,156 @@
+#include "common/printing.h"
+#include "common/jsonconfig.h"
+#include "common/strutil.h"
+#include "detection/host/host.h"
+#include "modules/host/host.h"
+
+bool ffPrintHost(FFHostOptions* options) {
+ bool success = false;
+ FFHostResult host;
+ ffStrbufInit(&host.family);
+ ffStrbufInit(&host.name);
+ ffStrbufInit(&host.version);
+ ffStrbufInit(&host.sku);
+ ffStrbufInit(&host.serial);
+ ffStrbufInit(&host.uuid);
+ ffStrbufInit(&host.vendor);
+
+ const char* error = ffDetectHost(&host);
+ if (error) {
+ ffPrintError(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
+ goto exit;
+ }
+
+ if (host.name.length == 0 && host.family.length == 0) {
+ ffPrintError(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "neither product_family nor product_name is set by O.E.M.");
+ goto exit;
+ }
+
+ if (options->moduleArgs.outputFormat.length == 0) {
+ ffPrintLogoAndKey(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
+
+ FF_STRBUF_AUTO_DESTROY output = ffStrbufCreate();
+
+ if (host.name.length > 0) {
+ ffStrbufAppend(&output, &host.name);
+ } else {
+ ffStrbufAppend(&output, &host.family);
+ }
+
+ if (host.version.length > 0) {
+ ffStrbufAppendF(&output, " (%s)", host.version.chars);
+ }
+
+ ffStrbufPutTo(&output, stdout);
+ } else {
+ FF_PRINT_FORMAT_CHECKED(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
+ FF_ARG(host.family, "family"),
+ FF_ARG(host.name, "name"),
+ FF_ARG(host.version, "version"),
+ FF_ARG(host.sku, "sku"),
+ FF_ARG(host.vendor, "vendor"),
+ FF_ARG(host.serial, "serial"),
+ FF_ARG(host.uuid, "uuid"),
+ }));
+ }
+ success = true;
+
+exit:
+ ffStrbufDestroy(&host.family);
+ ffStrbufDestroy(&host.name);
+ ffStrbufDestroy(&host.version);
+ ffStrbufDestroy(&host.sku);
+ ffStrbufDestroy(&host.serial);
+ ffStrbufDestroy(&host.uuid);
+ ffStrbufDestroy(&host.vendor);
+
+ return success;
+}
+
+void ffParseHostJsonObject(FFHostOptions* options, yyjson_val* module) {
+ yyjson_val *key, *val;
+ size_t idx, max;
+ yyjson_obj_foreach (module, idx, max, key, val) {
+ if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
+ continue;
+ }
+
+ ffPrintError(FF_HOST_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
+ }
+}
+
+void ffGenerateHostJsonConfig(FFHostOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
+ ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
+}
+
+bool ffGenerateHostJsonResult(FF_A_UNUSED FFHostOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
+ bool success = false;
+ FFHostResult host;
+ ffStrbufInit(&host.family);
+ ffStrbufInit(&host.name);
+ ffStrbufInit(&host.version);
+ ffStrbufInit(&host.sku);
+ ffStrbufInit(&host.serial);
+ ffStrbufInit(&host.uuid);
+ ffStrbufInit(&host.vendor);
+
+ const char* error = ffDetectHost(&host);
+ if (error) {
+ yyjson_mut_obj_add_str(doc, module, "error", error);
+ goto exit;
+ }
+
+ if (host.family.length == 0 && host.name.length == 0) {
+ yyjson_mut_obj_add_str(doc, module, "error", "neither product_family nor product_name is set by O.E.M.");
+ goto exit;
+ }
+
+ yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
+ yyjson_mut_obj_add_strbuf(doc, obj, "family", &host.family);
+ yyjson_mut_obj_add_strbuf(doc, obj, "name", &host.name);
+ yyjson_mut_obj_add_strbuf(doc, obj, "version", &host.version);
+ yyjson_mut_obj_add_strbuf(doc, obj, "sku", &host.sku);
+ yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &host.vendor);
+ yyjson_mut_obj_add_strbuf(doc, obj, "serial", &host.serial);
+ yyjson_mut_obj_add_strbuf(doc, obj, "uuid", &host.uuid);
+ success = true;
+
+exit:
+ ffStrbufDestroy(&host.family);
+ ffStrbufDestroy(&host.name);
+ ffStrbufDestroy(&host.version);
+ ffStrbufDestroy(&host.sku);
+ ffStrbufDestroy(&host.serial);
+ ffStrbufDestroy(&host.uuid);
+ ffStrbufDestroy(&host.vendor);
+
+ return success;
+}
+
+void ffInitHostOptions(FFHostOptions* options) {
+ ffOptionInitModuleArg(&options->moduleArgs, "󰌢");
+}
+
+void ffDestroyHostOptions(FFHostOptions* options) {
+ ffOptionDestroyModuleArg(&options->moduleArgs);
+}
+
+FFModuleBaseInfo ffHostModuleInfo = {
+ .name = FF_HOST_MODULE_NAME,
+ .description = "Print your computer's product name",
+ .initOptions = (void*) ffInitHostOptions,
+ .destroyOptions = (void*) ffDestroyHostOptions,
+ .parseJsonObject = (void*) ffParseHostJsonObject,
+ .printModule = (void*) ffPrintHost,
+ .generateJsonResult = (void*) ffGenerateHostJsonResult,
+ .generateJsonConfig = (void*) ffGenerateHostJsonConfig,
+ .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
+ { "Product family", "family" },
+ { "Product name", "name" },
+ { "Product version", "version" },
+ { "Product sku", "sku" },
+ { "Product vendor", "vendor" },
+ { "Product serial number", "serial" },
+ { "Product uuid", "uuid" },
+ }))
+};
diff --git a/src/modules/host/host.h b/src/modules/host/host.h
new file mode 100644
index 0000000..1f98fd4
--- /dev/null
+++ b/src/modules/host/host.h
@@ -0,0 +1,11 @@
+#pragma once
+
+#include "option.h"
+
+#define FF_HOST_MODULE_NAME "Host"
+
+bool ffPrintHost(FFHostOptions* options);
+void ffInitHostOptions(FFHostOptions* options);
+void ffDestroyHostOptions(FFHostOptions* options);
+
+extern FFModuleBaseInfo ffHostModuleInfo;
diff --git a/src/modules/host/option.h b/src/modules/host/option.h
new file mode 100644
index 0000000..e46c0eb
--- /dev/null
+++ b/src/modules/host/option.h
@@ -0,0 +1,9 @@
+#pragma once
+
+#include "common/option.h"
+
+typedef struct FFHostOptions {
+ FFModuleArgs moduleArgs;
+} FFHostOptions;
+
+static_assert(sizeof(FFHostOptions) <= FF_OPTION_MAX_SIZE, "FFHostOptions size exceeds maximum allowed size");