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-22 16:13:36 +08:00
|
|
|
#include "detection/libc/libc.h"
|
|
|
|
|
#include "detection/editor/editor.h"
|
|
|
|
|
#include "modules/editor/editor.h"
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
bool ffPrintEditor(FFEditorOptions* options) {
|
2024-05-22 16:13:36 +08:00
|
|
|
FFEditorResult result = {
|
2024-07-24 14:17:47 +08:00
|
|
|
.type = "Unknown",
|
2024-05-22 16:13:36 +08:00
|
|
|
.name = ffStrbufCreate(),
|
|
|
|
|
.path = ffStrbufCreate(),
|
2024-05-24 23:43:01 +08:00
|
|
|
.exe = ffStrbufCreate(),
|
2024-05-22 16:13:36 +08:00
|
|
|
.version = ffStrbufCreate(),
|
|
|
|
|
};
|
|
|
|
|
const char* error = ffDetectEditor(&result);
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (error) {
|
2024-05-22 16:13:36 +08:00
|
|
|
ffPrintError(FF_EDITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2024-05-22 16:13:36 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (options->moduleArgs.outputFormat.length == 0) {
|
2024-05-27 16:41:37 +08:00
|
|
|
ffPrintLogoAndKey(FF_EDITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (result.exe.length) {
|
2024-05-27 16:41:37 +08:00
|
|
|
ffStrbufWriteTo(&result.exe, stdout);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (result.version.length) {
|
2024-07-08 09:12:18 +08:00
|
|
|
printf(" %s", result.version.chars);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2024-05-27 16:41:37 +08:00
|
|
|
ffStrbufWriteTo(&result.name, stdout);
|
|
|
|
|
}
|
|
|
|
|
putchar('\n');
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
|
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_EDITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
|
|
|
|
|
FF_ARG(result.type, "type"),
|
|
|
|
|
FF_ARG(result.name, "name"),
|
|
|
|
|
FF_ARG(result.exe, "exe-name"),
|
|
|
|
|
FF_ARG(result.path, "path"),
|
|
|
|
|
FF_ARG(result.version, "version"),
|
|
|
|
|
}));
|
2024-05-22 16:13:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ffStrbufDestroy(&result.name);
|
|
|
|
|
ffStrbufDestroy(&result.path);
|
2024-05-24 23:43:01 +08:00
|
|
|
ffStrbufDestroy(&result.exe);
|
2024-05-22 16:13:36 +08:00
|
|
|
ffStrbufDestroy(&result.version);
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2024-05-22 16:13:36 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffParseEditorJsonObject(FFEditorOptions* options, yyjson_val* module) {
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_val *key, *val;
|
2024-05-22 16:13:36 +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-22 16:13:36 +08:00
|
|
|
continue;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2024-05-22 16:13:36 +08:00
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
ffPrintError(FF_EDITOR_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
|
2024-05-22 16:13:36 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffGenerateEditorJsonConfig(FFEditorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2025-08-04 16:46:28 +08:00
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
2024-05-22 16:13:36 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-09 16:20:59 +08:00
|
|
|
bool ffGenerateEditorJsonResult(FF_A_UNUSED FFEditorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
|
2024-05-22 16:13:36 +08:00
|
|
|
FFEditorResult result = {
|
|
|
|
|
.name = ffStrbufCreate(),
|
|
|
|
|
.path = ffStrbufCreate(),
|
|
|
|
|
.version = ffStrbufCreate(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char* error = ffDetectEditor(&result);
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (error) {
|
2024-05-22 16:13:36 +08:00
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", error);
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2024-05-22 16:13:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
|
2024-07-24 14:17:47 +08:00
|
|
|
yyjson_mut_obj_add_str(doc, obj, "type", result.type);
|
2024-05-22 16:13:36 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "name", &result.name);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "path", &result.path);
|
2024-05-24 23:43:01 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result.exe);
|
2024-05-22 16:13:36 +08:00
|
|
|
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
|
|
|
|
|
|
|
|
|
|
ffStrbufDestroy(&result.name);
|
|
|
|
|
ffStrbufDestroy(&result.path);
|
2024-05-24 23:43:01 +08:00
|
|
|
ffStrbufDestroy(&result.exe);
|
2024-05-22 16:13:36 +08:00
|
|
|
ffStrbufDestroy(&result.version);
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2024-05-22 16:13:36 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffInitEditorOptions(FFEditorOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffDestroyEditorOptions(FFEditorOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 22:50:56 +08:00
|
|
|
FFModuleBaseInfo ffEditorModuleInfo = {
|
2024-12-11 10:47:02 +08:00
|
|
|
.name = FF_EDITOR_MODULE_NAME,
|
2026-04-16 18:27:11 +08:00
|
|
|
.description = "Print information about the default editor ($VISUAL or $EDITOR)",
|
2025-08-03 18:39:41 +08:00
|
|
|
.initOptions = (void*) ffInitEditorOptions,
|
|
|
|
|
.destroyOptions = (void*) ffDestroyEditorOptions,
|
2024-12-11 10:47:02 +08:00
|
|
|
.parseJsonObject = (void*) ffParseEditorJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintEditor,
|
|
|
|
|
.generateJsonResult = (void*) ffGenerateEditorJsonResult,
|
|
|
|
|
.generateJsonConfig = (void*) ffGenerateEditorJsonConfig,
|
|
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
2026-03-30 10:27:23 +08:00
|
|
|
{ "Type (Visual / Editor)", "type" },
|
|
|
|
|
{ "Name", "name" },
|
|
|
|
|
{ "Exe name of real path", "exe-name" },
|
|
|
|
|
{ "Full path of real path", "path" },
|
|
|
|
|
{ "Version", "version" },
|
2026-03-30 10:17:45 +08:00
|
|
|
}))
|
|
|
|
|
};
|