From a89390c0df2a3eb633963959e8d4225230b7a09e Mon Sep 17 00:00:00 2001 From: bqv Date: Sun, 3 Jul 2022 12:41:43 +0100 Subject: [PATCH] config parsing fixes --- config.cpp | 39 +++++++++++++++++++++++---------------- config.hh | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 54 insertions(+), 21 deletions(-) diff --git a/config.cpp b/config.cpp index d1e6332..c3d0ab3 100644 --- a/config.cpp +++ b/config.cpp @@ -13,8 +13,8 @@ #include "account.hh" #include "config.hh" -int account_read_cb(weechat::config_section& section, - const char *option_name, const char *value) +bool account_read_cb(weechat::config_section& section, + const char *option_name, const char *value) { if (!option_name) return WEECHAT_CONFIG_READ_MEMORY_ERROR; @@ -23,14 +23,16 @@ int account_read_cb(weechat::config_section& section, std::getline(breadcrumbs, account_name, '.'); std::getline(breadcrumbs, option_id, '.'); if (account_name.empty()) - return WEECHAT_CONFIG_READ_MEMORY_ERROR; + return false; - int rc = WEECHAT_CONFIG_READ_OK; + bool rc = true; weechat::account* account = nullptr; if (!weechat::account::search(account, account_name)) + { account = &weechat::accounts.emplace( std::piecewise_construct, std::forward_as_tuple(account_name), std::forward_as_tuple(weechat::config::instance->file, account_name)).first->second; + } if (account) { auto options = { @@ -53,15 +55,20 @@ int account_read_cb(weechat::config_section& section, account->reloading_from_config %= options.size(); - if (option_id == "jid") rc |= (account->option_jid = value) == WEECHAT_CONFIG_OPTION_SET_ERROR; - if (option_id == "password") rc |= (account->option_password = value) == WEECHAT_CONFIG_OPTION_SET_ERROR; - if (option_id == "tls") rc |= (account->option_tls = value) == WEECHAT_CONFIG_OPTION_SET_ERROR; - if (option_id == "nickname") rc |= (account->option_nickname = value) == WEECHAT_CONFIG_OPTION_SET_ERROR; - if (option_id == "autoconnect") rc |= (account->option_autoconnect = value) == WEECHAT_CONFIG_OPTION_SET_ERROR; - if (option_id == "resource") rc |= (account->option_resource = value) == WEECHAT_CONFIG_OPTION_SET_ERROR; - if (option_id == "status") rc |= (account->option_status = value) == WEECHAT_CONFIG_OPTION_SET_ERROR; - if (option_id == "pgp_path") rc |= (account->option_pgp_path = value) == WEECHAT_CONFIG_OPTION_SET_ERROR; - if (option_id == "pgp_keyid") rc |= (account->option_pgp_keyid = value) == WEECHAT_CONFIG_OPTION_SET_ERROR; + auto rc_ok = [](int rc) { + return rc == WEECHAT_CONFIG_OPTION_SET_OK_CHANGED + || rc == WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + }; + + if (option_id == "jid") rc &= rc_ok(account->option_jid = value); + if (option_id == "password") rc &= rc_ok(account->option_password = value); + if (option_id == "tls") rc &= rc_ok(account->option_tls = value); + if (option_id == "nickname") rc &= rc_ok(account->option_nickname = value); + if (option_id == "autoconnect") rc &= rc_ok(account->option_autoconnect = value); + if (option_id == "resource") rc &= rc_ok(account->option_resource = value); + if (option_id == "status") rc &= rc_ok(account->option_status = value); + if (option_id == "pgp_path") rc &= rc_ok(account->option_pgp_path = value); + if (option_id == "pgp_keyid") rc &= rc_ok(account->option_pgp_keyid = value); if (!account->reloading_from_config) { @@ -81,7 +88,7 @@ int account_read_cb(weechat::config_section& section, account_name.data()); } - if (rc != WEECHAT_CONFIG_READ_OK) + if (!rc) { weechat_printf( NULL, @@ -91,7 +98,7 @@ int account_read_cb(weechat::config_section& section, return rc; } -int account_write_cb(weechat::config_section& section, const char *section_name) +bool account_write_cb(weechat::config_section& section, const char *section_name) { if (!weechat_config_write_line(section.file, section_name, NULL)) return WEECHAT_CONFIG_WRITE_ERROR; @@ -105,7 +112,7 @@ int account_write_cb(weechat::config_section& section, const char *section_name) return WEECHAT_CONFIG_WRITE_OK; } -int config_reload(weechat::config_file &file) +bool config_reload(weechat::config_file &file) { //weechat_config_section_free_options(file.configuration.section_account_default); //weechat_config_section_free_options(file.configuration.section_account); diff --git a/config.hh b/config.hh index 9b16688..5ef624e 100644 --- a/config.hh +++ b/config.hh @@ -57,6 +57,9 @@ namespace weechat if (file != config_file) throw std::invalid_argument("file != config_file"); if (!file.reload) return 1; return file.reload() ? 1 : 0; + // WEECHAT_CONFIG_READ_OK == 0 + // WEECHAT_CONFIG_READ_MEMORY_ERROR == -1 + // WEECHAT_CONFIG_READ_FILE_NOT_FOUND == -2 }, nullptr, this), config, name) { this->reload = std::bind(cb_reload, std::ref(*this)); } @@ -90,8 +93,17 @@ namespace weechat auto& section = *reinterpret_cast(data); if (section != sect) throw std::invalid_argument("section != sect"); if (section.file != config_file) throw std::invalid_argument("section.file != config_file"); - if (!section.read) return 0; - return section.read(option_name, value) ? 0 : -1; + if (!section.read) return 1; + return section.read(option_name, value) ? 2 : 0; + /// dev manual indicates: + // WEECHAT_CONFIG_READ_OK == 0 + // WEECHAT_CONFIG_READ_MEMORY_ERROR == -1 + // WEECHAT_CONFIG_READ_FILE_NOT_FOUND == -2 + /// code indicates: + // WEECHAT_CONFIG_OPTION_SET_OK_CHANGED == 2 + // WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE == 1 + // WEECHAT_CONFIG_OPTION_SET_ERROR == 0 + // WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND == -1 }, nullptr, this, [](const void *, void *data, struct t_config_file *config_file, const char *section_name) { @@ -99,6 +111,9 @@ namespace weechat if (section.file != config_file) throw std::invalid_argument("section.file != config_file"); if (!section.write) return 0; return section.write(section_name) ? 0 : -1; + // WEECHAT_CONFIG_WRITE_OK == 0 + // WEECHAT_CONFIG_WRITE_ERROR == -1 + // WEECHAT_CONFIG_WRITE_FILE_NOT_FOUND == -2 }, nullptr, this, [](const void *, void *data, struct t_config_file *config_file, const char *section_name) { @@ -106,14 +121,21 @@ namespace weechat if (section.file != config_file) throw std::invalid_argument("section.file != config_file"); if (!section.write_default) return 0; return section.write_default(section_name) ? 0 : -1; + // WEECHAT_CONFIG_WRITE_OK == 0 + // WEECHAT_CONFIG_WRITE_ERROR == -1 + // WEECHAT_CONFIG_WRITE_FILE_NOT_FOUND == -2 }, nullptr, this, [](const void *, void *data, struct t_config_file *config_file, struct t_config_section *sect, const char *option_name, const char *value) { auto& section = *reinterpret_cast(data); if (section != sect) throw std::invalid_argument("section != sect"); if (section.file != config_file) throw std::invalid_argument("section.file != config_file"); - if (!section.create_option) return 0; - return section.create_option(option_name, value) ? 0 : -1; + if (!section.create_option) return 1; + return section.create_option(option_name, value) ? 2 : 0; + // WEECHAT_CONFIG_OPTION_SET_OK_CHANGED == 2 + // WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE == 1 + // WEECHAT_CONFIG_OPTION_SET_ERROR == 0 + // WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND == -1 }, nullptr, this, [](const void *, void *data, struct t_config_file *config_file, struct t_config_section *sect, struct t_config_option *opt) { @@ -123,7 +145,11 @@ namespace weechat if (!section.delete_option) return 0; auto option = section.options.find(opt); if (option == section.options.end()) throw std::invalid_argument("unknown option"); - return section.delete_option(option->second) ? 0 : -1; + return section.delete_option(option->second) ? 1 : -1; + // WEECHAT_CONFIG_OPTION_UNSET_OK_NO_RESET == 0 + // WEECHAT_CONFIG_OPTION_UNSET_OK_RESET == 1 + // WEECHAT_CONFIG_OPTION_UNSET_OK_REMOVED == 2 + // WEECHAT_CONFIG_OPTION_UNSET_ERROR == -1 }, nullptr, this), config_file, name) { if (cb_read) this->read = std::bind(cb_read, std::ref(*this), _1, _2);