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.
reverse-scroll
nirenjan 2021-11-07 07:52:11 -08:00
parent 2fe7b8af43
commit e82f9032eb
6 changed files with 58 additions and 14 deletions

View File

@ -278,6 +278,20 @@ static void cmd_config(char *buffer, int *buflen, int argc, char **argv)
return; 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]); ERR_fmt("Unknown subcommand '%s' for 'config' command", argv[1]);
} }

View File

@ -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); 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 /* Callback stubs
* TODO: Remove the ones below when their implementation is complete * TODO: Remove the ones below when their implementation is complete
*/ */

View File

@ -76,6 +76,7 @@ void x52d_cfg_set_Profiles_ClutchEnabled(bool param);
void x52d_cfg_set_Profiles_ClutchLatched(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); 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); 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); void x52d_config_save(const char *cfg_file);
int x52d_config_set(const char *section, const char *key, const char *value); 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 #endif // !defined X52D_CONFIG_H

View File

@ -142,3 +142,18 @@ exit_dump:
return (value == NULL); 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;
}

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: libx52 0.2.3\n" "Project-Id-Version: libx52 0.2.3\n"
"Report-Msgid-Bugs-To: https://github.com/nirenjan/libx52/issues\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" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -659,31 +659,31 @@ msgstr ""
msgid "Shutting down X52 clock manager thread" msgid "Shutting down X52 clock manager thread"
msgstr "" msgstr ""
#: daemon/x52d_command.c:53 #: daemon/x52d_command.c:52
#, c-format #, c-format
msgid "Error accepting client connection on command socket: %s" msgid "Error accepting client connection on command socket: %s"
msgstr "" msgstr ""
#: daemon/x52d_command.c:90 #: daemon/x52d_command.c:89
#, c-format #, c-format
msgid "Error when polling command socket: FD %d, error %d, len %lu" msgid "Error when polling command socket: FD %d, error %d, len %lu"
msgstr "" msgstr ""
#: daemon/x52d_command.c:117 #: daemon/x52d_command.c:116
#, c-format #, c-format
msgid "Error when polling for command: %s" msgid "Error when polling for command: %s"
msgstr "" msgstr ""
#: daemon/x52d_command.c:121 #: daemon/x52d_command.c:120
msgid "Timed out when polling for command" msgid "Timed out when polling for command"
msgstr "" msgstr ""
#: daemon/x52d_command.c:345 #: daemon/x52d_command.c:354
#, c-format #, c-format
msgid "Error reading from client %d: %s" msgid "Error reading from client %d: %s"
msgstr "" msgstr ""
#: daemon/x52d_command.c:356 #: daemon/x52d_command.c:365
#, c-format #, c-format
msgid "Short write to client %d; expected %d bytes, wrote %d bytes" msgid "Short write to client %d; expected %d bytes, wrote %d bytes"
msgstr "" msgstr ""

View File

@ -7,7 +7,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: libx52 0.2.3\n" "Project-Id-Version: libx52 0.2.3\n"
"Report-Msgid-Bugs-To: https://github.com/nirenjan/libx52/issues\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" "PO-Revision-Date: 2021-11-04 15:35-0700\n"
"Last-Translator: Nirenjan Krishnan <nirenjan@gmail.com>\n" "Last-Translator: Nirenjan Krishnan <nirenjan@gmail.com>\n"
"Language-Team: Dummy Language for testing i18n\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" msgid "Shutting down X52 clock manager thread"
msgstr "Uttingshay ownday X52 ockclay anagermay eadthray" msgstr "Uttingshay ownday X52 ockclay anagermay eadthray"
#: daemon/x52d_command.c:53 #: daemon/x52d_command.c:52
#, c-format #, c-format
msgid "Error accepting client connection on command socket: %s" msgid "Error accepting client connection on command socket: %s"
msgstr "Erroray acceptingay ientclay onnectioncay onay ommandcay ocketsay: %s" msgstr "Erroray acceptingay ientclay onnectioncay onay ommandcay ocketsay: %s"
#: daemon/x52d_command.c:90 #: daemon/x52d_command.c:89
#, c-format #, c-format
msgid "Error when polling command socket: FD %d, error %d, len %lu" msgid "Error when polling command socket: FD %d, error %d, len %lu"
msgstr "" msgstr ""
"Erroray enwhay ollingpay ommandcay ocketsay: FDay %d, erroray %d, enlay %lu" "Erroray enwhay ollingpay ommandcay ocketsay: FDay %d, erroray %d, enlay %lu"
#: daemon/x52d_command.c:117 #: daemon/x52d_command.c:116
#, c-format #, c-format
msgid "Error when polling for command: %s" msgid "Error when polling for command: %s"
msgstr "Erroray enwhay ollingpay orfay ommandcay: %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" msgid "Timed out when polling for command"
msgstr "Imedtay outay enwhay ollingpay orfay ommandcay" msgstr "Imedtay outay enwhay ollingpay orfay ommandcay"
#: daemon/x52d_command.c:345 #: daemon/x52d_command.c:354
#, c-format #, c-format
msgid "Error reading from client %d: %s" msgid "Error reading from client %d: %s"
msgstr "Erroray eadingray omfray ientclay %d: %s" msgstr "Erroray eadingray omfray ientclay %d: %s"
#: daemon/x52d_command.c:356 #: daemon/x52d_command.c:365
#, c-format #, c-format
msgid "Short write to client %d; expected %d bytes, wrote %d bytes" msgid "Short write to client %d; expected %d bytes, wrote %d bytes"
msgstr "" msgstr ""