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 | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 3 | #include <fmt/compile.h> |
| 4 | #include <fmt/format.h> |
| 5 | |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 6 | #include <functional> |
| 7 | #include <iterator> |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 8 | #include <stdexcept> |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 9 | #include <stdplus/exception.hpp> |
| 10 | #include <stdplus/fd/create.hpp> |
| 11 | #include <stdplus/fd/line.hpp> |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 12 | #include <string> |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 13 | #include <utility> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace network |
| 18 | { |
| 19 | namespace config |
| 20 | { |
| 21 | |
William A. Kennington III | 150753f | 2022-08-05 15:20:54 -0700 | [diff] [blame] | 22 | using std::literals::string_view_literals::operator""sv; |
| 23 | |
| 24 | bool icaseeq(std::string_view in, std::string_view expected) noexcept |
| 25 | { |
| 26 | return std::equal(in.begin(), in.end(), expected.begin(), expected.end(), |
| 27 | [](auto a, auto b) { return tolower(a) == b; }); |
| 28 | } |
| 29 | |
| 30 | std::optional<bool> parseBool(std::string_view in) noexcept |
| 31 | { |
| 32 | if (in == "1"sv || icaseeq(in, "yes"sv) || icaseeq(in, "y"sv) || |
| 33 | icaseeq(in, "true"sv) || icaseeq(in, "t"sv) || icaseeq(in, "on"sv)) |
| 34 | { |
| 35 | return true; |
| 36 | } |
| 37 | if (in == "0"sv || icaseeq(in, "no"sv) || icaseeq(in, "n"sv) || |
| 38 | icaseeq(in, "false"sv) || icaseeq(in, "f"sv) || icaseeq(in, "off"sv)) |
| 39 | { |
| 40 | return false; |
| 41 | } |
| 42 | return std::nullopt; |
| 43 | } |
| 44 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 45 | fs::path pathForIntfConf(const fs::path& dir, std::string_view intf) |
| 46 | { |
| 47 | return dir / fmt::format(FMT_COMPILE("00-bmc-{}.network"), intf); |
| 48 | } |
| 49 | |
| 50 | fs::path pathForIntfDev(const fs::path& dir, std::string_view intf) |
| 51 | { |
| 52 | return dir / fmt::format(FMT_COMPILE("{}.netdev"), intf); |
| 53 | } |
| 54 | |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 55 | const std::string* |
| 56 | SectionMap::getLastValueString(std::string_view section, |
| 57 | std::string_view key) const noexcept |
| 58 | { |
| 59 | auto sit = find(section); |
| 60 | if (sit == end()) |
| 61 | { |
| 62 | return nullptr; |
| 63 | } |
| 64 | for (auto it = sit->second.rbegin(); it != sit->second.rend(); ++it) |
| 65 | { |
| 66 | auto kit = it->find(key); |
| 67 | if (kit == it->end() || kit->second.empty()) |
| 68 | { |
| 69 | continue; |
| 70 | } |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 71 | return &kit->second.back().get(); |
William A. Kennington III | e21a5cf | 2022-08-09 12:19:14 -0700 | [diff] [blame] | 72 | } |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
| 76 | std::vector<std::string> SectionMap::getValueStrings(std::string_view section, |
| 77 | std::string_view key) const |
| 78 | { |
| 79 | return getValues(section, key, |
| 80 | [](const Value& v) { return std::string(v); }); |
| 81 | } |
| 82 | |
William A. Kennington III | 0dd0937 | 2022-08-18 14:57:07 -0700 | [diff] [blame] | 83 | void KeyCheck::operator()(const std::string& s) |
| 84 | { |
| 85 | for (auto c : s) |
| 86 | { |
| 87 | if (c == '\n' || c == '=') |
| 88 | { |
| 89 | throw std::invalid_argument( |
| 90 | fmt::format(FMT_COMPILE("Invalid Config Key: {}"), s)); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void SectionCheck::operator()(const std::string& s) |
| 96 | { |
| 97 | for (auto c : s) |
| 98 | { |
| 99 | if (c == '\n' || c == ']') |
| 100 | { |
| 101 | throw std::invalid_argument( |
| 102 | fmt::format(FMT_COMPILE("Invalid Config Section: {}"), s)); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void ValueCheck::operator()(const std::string& s) |
| 108 | { |
| 109 | for (auto c : s) |
| 110 | { |
| 111 | if (c == '\n') |
| 112 | { |
| 113 | throw std::invalid_argument( |
| 114 | fmt::format(FMT_COMPILE("Invalid Config Value: {}"), s)); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 119 | Parser::Parser(const fs::path& filename) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 120 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 121 | setFile(filename); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 122 | } |
| 123 | |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 124 | inline bool isspace(char c) noexcept |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 125 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 126 | return c == ' ' || c == '\t'; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 127 | } |
| 128 | |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 129 | inline bool iscomment(char c) noexcept |
| 130 | { |
| 131 | return c == '#' || c == ';'; |
| 132 | } |
| 133 | |
| 134 | static void removePadding(std::string_view& str) noexcept |
| 135 | { |
| 136 | size_t idx = str.size(); |
| 137 | for (; idx > 0 && isspace(str[idx - 1]); idx--) |
| 138 | ; |
| 139 | str.remove_suffix(str.size() - idx); |
| 140 | |
| 141 | idx = 0; |
| 142 | for (; idx < str.size() && isspace(str[idx]); idx++) |
| 143 | ; |
| 144 | str.remove_prefix(idx); |
| 145 | } |
| 146 | |
| 147 | struct Parse |
| 148 | { |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 149 | std::reference_wrapper<const fs::path> filename; |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame^] | 150 | SectionMap map; |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 151 | KeyValuesMap* section; |
| 152 | std::vector<std::string> warnings; |
| 153 | size_t lineno; |
| 154 | |
| 155 | inline Parse(const fs::path& filename) : |
| 156 | filename(filename), section(nullptr), lineno(0) |
| 157 | { |
| 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 | { |
| 165 | warnings.emplace_back(fmt::format("{}:{}: Section missing ]", |
| 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( |
| 175 | fmt::format("{}:{}: Characters outside section name", |
| 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 | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 197 | new_warnings.emplace_back(fmt::format( |
| 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( |
| 205 | fmt::format("{}:{}: Key `{}` missing section", |
| 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 | |
| 253 | try |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 254 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 255 | auto fd = stdplus::fd::open(filename.c_str(), |
| 256 | stdplus::fd::OpenAccess::ReadOnly); |
| 257 | stdplus::fd::LineReader reader(fd); |
| 258 | while (true) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 259 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 260 | parse.pump(*reader.readLine()); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 261 | } |
| 262 | } |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 263 | catch (const stdplus::exception::Eof&) |
| 264 | { |
| 265 | } |
| 266 | catch (const std::exception& e) |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 267 | { |
| 268 | // TODO: Pass exceptions once callers can handle them |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 269 | parse.warnings.emplace_back( |
| 270 | fmt::format("{}: Read error: {}", filename.native(), e.what())); |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 271 | } |
| 272 | |
William A. Kennington III | 34bb3e2 | 2022-08-18 15:17:22 -0700 | [diff] [blame^] | 273 | this->map = std::move(parse.map); |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 274 | this->filename = filename; |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 275 | this->warnings = std::move(parse.warnings); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 276 | } |
| 277 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 278 | } // namespace config |
| 279 | } // namespace network |
| 280 | } // namespace phosphor |