From d97b16950c16c5424f39e2eca5700dce14e0dfcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 8 Sep 2023 16:33:19 +0800 Subject: [PATCH] Shell: JSON format support --- src/modules/shell/shell.c | 22 +++++++++++++++++++++- src/modules/shell/shell.h | 1 + 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/modules/shell/shell.c b/src/modules/shell/shell.c index 1328c7acf..e7f03e730 100644 --- a/src/modules/shell/shell.c +++ b/src/modules/shell/shell.c @@ -45,7 +45,7 @@ void ffPrintShell(FFShellOptions* options) void ffInitShellOptions(FFShellOptions* options) { - ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_SHELL_MODULE_NAME, ffParseShellCommandOptions, ffParseShellJsonObject, ffPrintShell, NULL); + ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_SHELL_MODULE_NAME, ffParseShellCommandOptions, ffParseShellJsonObject, ffPrintShell, ffGenerateShellJson); ffOptionInitModuleArg(&options->moduleArgs); } @@ -80,3 +80,23 @@ void ffParseShellJsonObject(FFShellOptions* options, yyjson_val* module) ffPrintError(FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } + +void ffGenerateShellJson(FF_MAYBE_UNUSED FFShellOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +{ + const FFTerminalShellResult* result = ffDetectTerminalShell(); + + if(result->shellProcessName.length == 0) + { + yyjson_mut_obj_add_str(doc, module, "error", "Couldn't detect shell"); + return; + } + + yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); + yyjson_mut_obj_add_strbuf(doc, obj, "processName", &result->shellProcessName); + yyjson_mut_obj_add_strbuf(doc, obj, "exe", &result->shellExe); + yyjson_mut_obj_add_strcpy(doc, obj, "exeName", result->shellExeName); + yyjson_mut_obj_add_strbuf(doc, obj, "version", &result->shellVersion); + yyjson_mut_obj_add_strbuf(doc, obj, "userShellExe", &result->userShellExe); + yyjson_mut_obj_add_strcpy(doc, obj, "userShellExeName", result->userShellExeName); + yyjson_mut_obj_add_strbuf(doc, obj, "userShellVersion", &result->userShellVersion); +} diff --git a/src/modules/shell/shell.h b/src/modules/shell/shell.h index 4e4f976bf..354b51229 100644 --- a/src/modules/shell/shell.h +++ b/src/modules/shell/shell.h @@ -9,3 +9,4 @@ void ffInitShellOptions(FFShellOptions* options); bool ffParseShellCommandOptions(FFShellOptions* options, const char* key, const char* value); void ffDestroyShellOptions(FFShellOptions* options); void ffParseShellJsonObject(FFShellOptions* options, yyjson_val* module); +void ffGenerateShellJson(FFShellOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module);