From e82f9032ebf3d9fabae38cf58dc0255250266019 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Sun, 7 Nov 2021 07:52:11 -0800 Subject: [PATCH] Add 'config get' command to retrieve configuration The command allows the client to retrieve individual parameters from the configuration. This follows a similar syntax to the 'config set' command, with the client supplying the section and key, and if there is a matching entry in the configuration, it will return the corresponding value. --- daemon/x52d_command.c | 14 ++++++++++++++ daemon/x52d_config.c | 13 +++++++++++++ daemon/x52d_config.h | 2 ++ daemon/x52d_config_dump.c | 15 +++++++++++++++ po/libx52.pot | 14 +++++++------- po/xx_PL.po | 14 +++++++------- 6 files changed, 58 insertions(+), 14 deletions(-) diff --git a/daemon/x52d_command.c b/daemon/x52d_command.c index aa18055..aa5ed66 100644 --- a/daemon/x52d_command.c +++ b/daemon/x52d_command.c @@ -278,6 +278,20 @@ static void cmd_config(char *buffer, int *buflen, int argc, char **argv) return; } + MATCH(1, "get") { + if (argc == 4) { + const char *rv = x52d_config_get(argv[2], argv[3]); + if (rv == NULL) { + ERR_fmt("Error getting '%s.%s'", argv[2], argv[3]); + } else { + response_strings(buffer, buflen, "DATA", 3, argv[2], argv[3], rv); + } + } else { + ERR_fmt("Unexpected arguments for 'config get' command; got %d, expected 4", argc); + } + return; + } + ERR_fmt("Unknown subcommand '%s' for 'config' command", argv[1]); } diff --git a/daemon/x52d_config.c b/daemon/x52d_config.c index 60f56ad..175b7d4 100644 --- a/daemon/x52d_config.c +++ b/daemon/x52d_config.c @@ -68,6 +68,19 @@ int x52d_config_set(const char *section, const char *key, const char *value) return x52d_config_process_kv(&x52d_config, section, key, value); } +const char *x52d_config_get(const char *section, const char *key) +{ + const char *value; + if (section == NULL || key == NULL) { + return NULL; + } + + value = x52d_config_get_param(&x52d_config, section, key); + PINELOG_TRACE("Processed config get '%s.%s'='%s'", section, key, value); + + return value; +} + /* Callback stubs * TODO: Remove the ones below when their implementation is complete */ diff --git a/daemon/x52d_config.h b/daemon/x52d_config.h index db7dda5..1f60ab6 100644 --- a/daemon/x52d_config.h +++ b/daemon/x52d_config.h @@ -76,6 +76,7 @@ void x52d_cfg_set_Profiles_ClutchEnabled(bool param); void x52d_cfg_set_Profiles_ClutchLatched(bool param); int x52d_config_process_kv(void *user, const char *section, const char *key, const char *value); +const char *x52d_config_get_param(struct x52d_config *cfg, const char *section, const char *key); int x52d_config_set_defaults(struct x52d_config *cfg); @@ -94,5 +95,6 @@ int x52d_config_save_file(struct x52d_config *cfg, const char *cfg_file); void x52d_config_save(const char *cfg_file); int x52d_config_set(const char *section, const char *key, const char *value); +const char *x52d_config_get(const char *section, const char *key); #endif // !defined X52D_CONFIG_H diff --git a/daemon/x52d_config_dump.c b/daemon/x52d_config_dump.c index 2f49132..53e76ce 100644 --- a/daemon/x52d_config_dump.c +++ b/daemon/x52d_config_dump.c @@ -142,3 +142,18 @@ exit_dump: return (value == NULL); } +const char *x52d_config_get_param(struct x52d_config *cfg, const char *section, const char *key) +{ + const char *value = NULL; + + #define CFG(section_c, key_c, name, type, def) do { \ + if (strcasecmp(section, #section_c) == 0 && strcasecmp(key, #key_c) == 0) { \ + value = type ## _dumper(section, key, cfg, offsetof(struct x52d_config, name)); \ + goto return_value; \ + } \ + } while (0); + #include "x52d_config.def" + +return_value: + return value; +} diff --git a/po/libx52.pot b/po/libx52.pot index 4575c01..03daa8b 100644 --- a/po/libx52.pot +++ b/po/libx52.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: libx52 0.2.3\n" "Report-Msgid-Bugs-To: https://github.com/nirenjan/libx52/issues\n" -"POT-Creation-Date: 2021-11-05 15:27-0700\n" +"POT-Creation-Date: 2021-11-07 06:24-0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -659,31 +659,31 @@ msgstr "" msgid "Shutting down X52 clock manager thread" msgstr "" -#: daemon/x52d_command.c:53 +#: daemon/x52d_command.c:52 #, c-format msgid "Error accepting client connection on command socket: %s" msgstr "" -#: daemon/x52d_command.c:90 +#: daemon/x52d_command.c:89 #, c-format msgid "Error when polling command socket: FD %d, error %d, len %lu" msgstr "" -#: daemon/x52d_command.c:117 +#: daemon/x52d_command.c:116 #, c-format msgid "Error when polling for command: %s" msgstr "" -#: daemon/x52d_command.c:121 +#: daemon/x52d_command.c:120 msgid "Timed out when polling for command" msgstr "" -#: daemon/x52d_command.c:345 +#: daemon/x52d_command.c:354 #, c-format msgid "Error reading from client %d: %s" msgstr "" -#: daemon/x52d_command.c:356 +#: daemon/x52d_command.c:365 #, c-format msgid "Short write to client %d; expected %d bytes, wrote %d bytes" msgstr "" diff --git a/po/xx_PL.po b/po/xx_PL.po index b11d1da..f8a329b 100644 --- a/po/xx_PL.po +++ b/po/xx_PL.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: libx52 0.2.3\n" "Report-Msgid-Bugs-To: https://github.com/nirenjan/libx52/issues\n" -"POT-Creation-Date: 2021-11-05 15:27-0700\n" +"POT-Creation-Date: 2021-11-07 06:24-0800\n" "PO-Revision-Date: 2021-11-04 15:35-0700\n" "Last-Translator: Nirenjan Krishnan \n" "Language-Team: Dummy Language for testing i18n\n" @@ -709,32 +709,32 @@ msgstr "Erroray %d initializingay ockclay eadthray: %s" msgid "Shutting down X52 clock manager thread" msgstr "Uttingshay ownday X52 ockclay anagermay eadthray" -#: daemon/x52d_command.c:53 +#: daemon/x52d_command.c:52 #, c-format msgid "Error accepting client connection on command socket: %s" msgstr "Erroray acceptingay ientclay onnectioncay onay ommandcay ocketsay: %s" -#: daemon/x52d_command.c:90 +#: daemon/x52d_command.c:89 #, c-format msgid "Error when polling command socket: FD %d, error %d, len %lu" msgstr "" "Erroray enwhay ollingpay ommandcay ocketsay: FDay %d, erroray %d, enlay %lu" -#: daemon/x52d_command.c:117 +#: daemon/x52d_command.c:116 #, c-format msgid "Error when polling for command: %s" msgstr "Erroray enwhay ollingpay orfay ommandcay: %s" -#: daemon/x52d_command.c:121 +#: daemon/x52d_command.c:120 msgid "Timed out when polling for command" msgstr "Imedtay outay enwhay ollingpay orfay ommandcay" -#: daemon/x52d_command.c:345 +#: daemon/x52d_command.c:354 #, c-format msgid "Error reading from client %d: %s" msgstr "Erroray eadingray omfray ientclay %d: %s" -#: daemon/x52d_command.c:356 +#: daemon/x52d_command.c:365 #, c-format msgid "Short write to client %d; expected %d bytes, wrote %d bytes" msgstr ""