Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 1 | #include "config_parser.hpp" |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 2 | |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 3 | #include <stdplus/exception.hpp> |
William A. Kennington III | 409f1a6 | 2022-08-11 15:44:37 -0700 | [diff] [blame] | 4 | #include <stdplus/fd/atomic.hpp> |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 5 | #include <stdplus/fd/create.hpp> |
William A. Kennington III | 409f1a6 | 2022-08-11 15:44:37 -0700 | [diff] [blame] | 6 | #include <stdplus/fd/fmt.hpp> |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 7 | #include <stdplus/fd/line.hpp> |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame] | 8 | #include <stdplus/str/cat.hpp> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 9 | |
William A. Kennington III | cafc151 | 2023-07-25 02:22:32 -0700 | [diff] [blame] | 10 | #include <format> |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 11 | #include <functional> |
| 12 | #include <iterator> |
| 13 | #include <stdexcept> |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 14 | #include <string> |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 15 | #include <utility> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 16 | |
| 17 | namespace phosphor |
| 18 | { |
| 19 | namespace network |
| 20 | { |
| 21 | namespace config |
| 22 | { |
| 23 | |
William A. Kennington III | 150753f | 2022-08-05 15:20:54 -0700 | [diff] [blame] | 24 | using std::literals::string_view_literals::operator""sv; |
| 25 | |
| 26 | bool icaseeq(std::string_view in, std::string_view expected) noexcept |
| 27 | { |
| 28 | return std::equal(in.begin(), in.end(), expected.begin(), expected.end(), |
| 29 | [](auto a, auto b) { return tolower(a) == b; }); |
| 30 | } |
| 31 | |
| 32 | std::optional<bool> parseBool(std::string_view in) noexcept |
| 33 | { |
| 34 | if (in == "1"sv || icaseeq(in, "yes"sv) || icaseeq(in, "y"sv) || |
| 35 | icaseeq(in, "true"sv) || icaseeq(in, "t"sv) || icaseeq(in, "on"sv)) |
| 36 | { |
| 37 | return true; |
| 38 | } |
| 39 | if (in == "0"sv || icaseeq(in, "no"sv) || icaseeq(in, "n"sv) || |
| 40 | icaseeq(in, "false"sv) || icaseeq(in, "f"sv) || icaseeq(in, "off"sv)) |
| 41 | { |
| 42 | return false; |
| 43 | } |
| 44 | return std::nullopt; |
| 45 | } |
| 46 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 47 | fs::path pathForIntfConf(const fs::path& dir, std::string_view intf) |
| 48 | { |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame] | 49 | return dir / stdplus::strCat("00-bmc-"sv, intf, ".network"sv); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | fs::path pathForIntfDev(const fs::path& dir, std::string_view intf) |
| 53 | { |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame] | 54 | return dir / stdplus::strCat(intf, ".netdev"sv); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Patrick Williams | ad20502 | 2024-08-16 15:20:07 -0400 | [diff] [blame] | 57 | const std::string* SectionMap::getLastValueString( |
| 58 | std::string_view section, std::string_view key) const noexcept |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 59 | { |
| 60 | auto sit = find(section); |
| 61 | if (sit == end()) |
| 62 | { |
| 63 | return nullptr; |
| 64 | } |
| 65 | for (auto it = sit->second.rbegin(); it != sit->second.rend(); ++it) |
| 66 | { |
| 67 | auto kit = it->find(key); |
| 68 | if (kit == it->end() || kit->second.empty()) |
| 69 | { |
| 70 | continue; |
| 71 | } |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 72 | return &kit->second.back().get(); |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 73 | } |
| 74 | return nullptr; |
| 75 | } |
| 76 | |
| 77 | std::vector<std::string> SectionMap::getValueStrings(std::string_view section, |
| 78 | std::string_view key) const |
| 79 | { |
| 80 | return getValues(section, key, |
| 81 | [](const Value& v) { return std::string(v); }); |
| 82 | } |
| 83 | |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 84 | void KeyCheck::operator()(std::string_view s) |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 85 | { |
| 86 | for (auto c : s) |
| 87 | { |
| 88 | if (c == '\n' || c == '=') |
| 89 | { |
| 90 | throw std::invalid_argument( |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame] | 91 | stdplus::strCat("Invalid Config Key: "sv, s)); |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 96 | void SectionCheck::operator()(std::string_view s) |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 97 | { |
| 98 | for (auto c : s) |
| 99 | { |
| 100 | if (c == '\n' || c == ']') |
| 101 | { |
| 102 | throw std::invalid_argument( |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame] | 103 | stdplus::strCat("Invalid Config Section: "sv, s)); |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
William A. Kennington III | be3bd2f | 2022-10-11 14:11:27 -0700 | [diff] [blame] | 108 | void ValueCheck::operator()(std::string_view s) |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 109 | { |
| 110 | for (auto c : s) |
| 111 | { |
| 112 | if (c == '\n') |
| 113 | { |
| 114 | throw std::invalid_argument( |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame] | 115 | stdplus::strCat("Invalid Config Value: "sv, s)); |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 120 | Parser::Parser(const fs::path& filename) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 121 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 122 | setFile(filename); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 123 | } |
| 124 | |
William A. Kennington III | f55b7d8 | 2022-10-25 14:21:46 -0700 | [diff] [blame] | 125 | constexpr bool isspace(char c) noexcept |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 126 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 127 | return c == ' ' || c == '\t'; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 128 | } |
| 129 | |
William A. Kennington III | f55b7d8 | 2022-10-25 14:21:46 -0700 | [diff] [blame] | 130 | constexpr bool iscomment(char c) noexcept |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 131 | { |
| 132 | return c == '#' || c == ';'; |
| 133 | } |
| 134 | |
| 135 | static void removePadding(std::string_view& str) noexcept |
| 136 | { |
| 137 | size_t idx = str.size(); |
| 138 | for (; idx > 0 && isspace(str[idx - 1]); idx--) |
| 139 | ; |
| 140 | str.remove_suffix(str.size() - idx); |
| 141 | |
| 142 | idx = 0; |
| 143 | for (; idx < str.size() && isspace(str[idx]); idx++) |
| 144 | ; |
| 145 | str.remove_prefix(idx); |
| 146 | } |
| 147 | |
| 148 | struct Parse |
| 149 | { |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 150 | std::reference_wrapper<const fs::path> filename; |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 151 | SectionMap map; |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 152 | KeyValuesMap* section; |
| 153 | std::vector<std::string> warnings; |
| 154 | size_t lineno; |
| 155 | |
| 156 | inline Parse(const fs::path& filename) : |
| 157 | filename(filename), section(nullptr), lineno(0) |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 158 | {} |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 159 | |
| 160 | void pumpSection(std::string_view line) |
| 161 | { |
| 162 | auto cpos = line.find(']'); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 163 | if (cpos == line.npos) |
| 164 | { |
William A. Kennington III | cafc151 | 2023-07-25 02:22:32 -0700 | [diff] [blame] | 165 | warnings.emplace_back(std::format("{}:{}: Section missing ]", |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 166 | filename.get().native(), lineno)); |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | for (auto c : line.substr(cpos + 1)) |
| 171 | { |
| 172 | if (!isspace(c)) |
| 173 | { |
| 174 | warnings.emplace_back( |
William A. Kennington III | cafc151 | 2023-07-25 02:22:32 -0700 | [diff] [blame] | 175 | std::format("{}:{}: Characters outside section name", |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 176 | filename.get().native(), lineno)); |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | } |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 181 | auto s = line.substr(0, cpos); |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 182 | auto it = map.find(s); |
| 183 | if (it == map.end()) |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 184 | { |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 185 | std::tie(it, std::ignore) = map.emplace( |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 186 | Section(Section::unchecked(), s), KeyValuesMapList{}); |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 187 | } |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 188 | section = &it->second.emplace_back(); |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void pumpKV(std::string_view line) |
| 192 | { |
| 193 | auto epos = line.find('='); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 194 | std::vector<std::string> new_warnings; |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 195 | if (epos == line.npos) |
| 196 | { |
William A. Kennington III | cafc151 | 2023-07-25 02:22:32 -0700 | [diff] [blame] | 197 | new_warnings.emplace_back(std::format( |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 198 | "{}:{}: KV missing `=`", filename.get().native(), lineno)); |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 199 | } |
| 200 | auto k = line.substr(0, epos); |
| 201 | removePadding(k); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 202 | if (section == nullptr) |
| 203 | { |
| 204 | new_warnings.emplace_back( |
William A. Kennington III | cafc151 | 2023-07-25 02:22:32 -0700 | [diff] [blame] | 205 | std::format("{}:{}: Key `{}` missing section", |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 206 | filename.get().native(), lineno, k)); |
| 207 | } |
| 208 | if (!new_warnings.empty()) |
| 209 | { |
| 210 | warnings.insert(warnings.end(), |
| 211 | std::make_move_iterator(new_warnings.begin()), |
| 212 | std::make_move_iterator(new_warnings.end())); |
| 213 | return; |
| 214 | } |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 215 | auto v = line.substr(epos + 1); |
| 216 | removePadding(v); |
| 217 | |
| 218 | auto it = section->find(k); |
| 219 | if (it == section->end()) |
| 220 | { |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 221 | std::tie(it, std::ignore) = |
| 222 | section->emplace(Key(Key::unchecked(), k), ValueList{}); |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 223 | } |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 224 | it->second.emplace_back(Value::unchecked(), v); |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void pump(std::string_view line) |
| 228 | { |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 229 | lineno++; |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 230 | for (size_t i = 0; i < line.size(); ++i) |
| 231 | { |
| 232 | auto c = line[i]; |
| 233 | if (iscomment(c)) |
| 234 | { |
| 235 | return; |
| 236 | } |
| 237 | else if (c == '[') |
| 238 | { |
| 239 | return pumpSection(line.substr(i + 1)); |
| 240 | } |
| 241 | else if (!isspace(c)) |
| 242 | { |
| 243 | return pumpKV(line.substr(i)); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | }; |
| 248 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 249 | void Parser::setFile(const fs::path& filename) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 250 | { |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 251 | Parse parse(filename); |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 252 | |
William A. Kennington III | 301e8ad | 2022-11-15 15:45:32 -0800 | [diff] [blame] | 253 | bool fileExists = true; |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 254 | try |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 255 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 256 | auto fd = stdplus::fd::open(filename.c_str(), |
| 257 | stdplus::fd::OpenAccess::ReadOnly); |
| 258 | stdplus::fd::LineReader reader(fd); |
| 259 | while (true) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 260 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 261 | parse.pump(*reader.readLine()); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 262 | } |
| 263 | } |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 264 | catch (const stdplus::exception::Eof&) |
Patrick Williams | 89d734b | 2023-05-10 07:50:25 -0500 | [diff] [blame] | 265 | {} |
William A. Kennington III | 301e8ad | 2022-11-15 15:45:32 -0800 | [diff] [blame] | 266 | catch (const std::system_error& e) |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 267 | { |
William A. Kennington III | 301e8ad | 2022-11-15 15:45:32 -0800 | [diff] [blame] | 268 | fileExists = false; |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 269 | // TODO: Pass exceptions once callers can handle them |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 270 | parse.warnings.emplace_back( |
William A. Kennington III | cafc151 | 2023-07-25 02:22:32 -0700 | [diff] [blame] | 271 | std::format("{}: Open error: {}", filename.native(), e.what())); |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 272 | } |
| 273 | |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame] | 274 | this->map = std::move(parse.map); |
William A. Kennington III | 301e8ad | 2022-11-15 15:45:32 -0800 | [diff] [blame] | 275 | this->fileExists = fileExists; |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 276 | this->filename = filename; |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 277 | this->warnings = std::move(parse.warnings); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 278 | } |
| 279 | |
William A. Kennington III | 409f1a6 | 2022-08-11 15:44:37 -0700 | [diff] [blame] | 280 | static void writeFileInt(const SectionMap& map, const fs::path& filename) |
| 281 | { |
| 282 | stdplus::fd::AtomicWriter writer(filename, 0644); |
| 283 | stdplus::fd::FormatBuffer out(writer); |
| 284 | for (const auto& [section, maps] : map) |
| 285 | { |
| 286 | for (const auto& map : maps) |
| 287 | { |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame] | 288 | out.appends("["sv, section.get(), "]\n"sv); |
William A. Kennington III | 409f1a6 | 2022-08-11 15:44:37 -0700 | [diff] [blame] | 289 | for (const auto& [key, vals] : map) |
| 290 | { |
| 291 | for (const auto& val : vals) |
| 292 | { |
William A. Kennington III | 8664252 | 2023-07-24 17:55:55 -0700 | [diff] [blame] | 293 | out.appends(key.get(), "="sv, val.get(), "\n"sv); |
William A. Kennington III | 409f1a6 | 2022-08-11 15:44:37 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | out.flush(); |
| 299 | writer.commit(); |
| 300 | } |
| 301 | |
| 302 | void Parser::writeFile() const |
| 303 | { |
| 304 | writeFileInt(map, filename); |
| 305 | } |
| 306 | |
| 307 | void Parser::writeFile(const fs::path& filename) |
| 308 | { |
| 309 | writeFileInt(map, filename); |
| 310 | this->filename = filename; |
| 311 | } |
| 312 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 313 | } // namespace config |
| 314 | } // namespace network |
| 315 | } // namespace phosphor |