From d4549d68499260452c8df8c7f14056d575db823a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 9 Mar 2023 16:10:46 +0800 Subject: [PATCH] Command: init support of JSON config --- src/common/config.c | 1 + src/modules/command/command.c | 43 +++++++++++++++++++++++++++++++++++ src/modules/command/command.h | 5 ++++ 3 files changed, 49 insertions(+) diff --git a/src/common/config.c b/src/common/config.c index 75af071c8..09f7d5073 100644 --- a/src/common/config.c +++ b/src/common/config.c @@ -38,6 +38,7 @@ static const char* parseModules(FFinstance* instance, JSONCData* data, json_obje return "modules must be an array of strings or objects"; if(!ffParseBatteryJsonObject(instance, type, data, module)) + if(!ffParseCommandJsonObject(instance, type, data, module)) return "Unknown module type"; } diff --git a/src/modules/command/command.c b/src/modules/command/command.c index c523095e2..43143fb5f 100644 --- a/src/modules/command/command.c +++ b/src/modules/command/command.c @@ -83,3 +83,46 @@ void ffDestroyCommandOptions(FFCommandOptions* options) ffStrbufDestroy(&options->shell); ffStrbufDestroy(&options->text); } + +#ifdef FF_HAVE_JSONC +bool ffParseCommandJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module) +{ + if (strcasecmp(type, FF_COMMAND_MODULE_NAME) != 0) + return false; + + FFCommandOptions __attribute__((__cleanup__(ffDestroyCommandOptions))) options; + ffInitCommandOptions(&options); + + if (module) + { + struct lh_entry* entry; + lh_foreach(data->ffjson_object_get_object(module), entry) + { + const char* key = (const char *)lh_entry_k(entry); + if (strcasecmp(key, "type") == 0) + continue; + json_object* val = (struct json_object *)lh_entry_v(entry); + + if (ffJsonConfigParseModuleArgs(data, key, val, &options.moduleArgs)) + continue; + + if (strcasecmp(key, "shell") == 0) + { + ffStrbufSetS(&options.shell, data->ffjson_object_get_string(val)); + continue; + } + + if (strcasecmp(key, "text") == 0) + { + ffStrbufSetS(&options.text, data->ffjson_object_get_string(val)); + continue; + } + + ffPrintError(instance, FF_COMMAND_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key); + } + } + + ffPrintCommand(instance, &options); + return true; +} +#endif diff --git a/src/modules/command/command.h b/src/modules/command/command.h index 069e423e4..adbf29d04 100644 --- a/src/modules/command/command.h +++ b/src/modules/command/command.h @@ -7,3 +7,8 @@ void ffPrintCommand(FFinstance* instance, FFCommandOptions* options); void ffInitCommandOptions(FFCommandOptions* options); bool ffParseCommandCommandOptions(FFCommandOptions* options, const char* key, const char* value); void ffDestroyCommandOptions(FFCommandOptions* options); + +#ifdef FF_HAVE_JSONC +#include "common/config.h" +bool ffParseCommandJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module); +#endif