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 | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 8 | #include <stdplus/exception.hpp> |
| 9 | #include <stdplus/fd/create.hpp> |
| 10 | #include <stdplus/fd/line.hpp> |
| 11 | #include <utility> |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace network |
| 16 | { |
| 17 | namespace config |
| 18 | { |
| 19 | |
William A. Kennington III | 150753f | 2022-08-05 15:20:54 -0700 | [diff] [blame] | 20 | using std::literals::string_view_literals::operator""sv; |
| 21 | |
| 22 | bool icaseeq(std::string_view in, std::string_view expected) noexcept |
| 23 | { |
| 24 | return std::equal(in.begin(), in.end(), expected.begin(), expected.end(), |
| 25 | [](auto a, auto b) { return tolower(a) == b; }); |
| 26 | } |
| 27 | |
| 28 | std::optional<bool> parseBool(std::string_view in) noexcept |
| 29 | { |
| 30 | if (in == "1"sv || icaseeq(in, "yes"sv) || icaseeq(in, "y"sv) || |
| 31 | icaseeq(in, "true"sv) || icaseeq(in, "t"sv) || icaseeq(in, "on"sv)) |
| 32 | { |
| 33 | return true; |
| 34 | } |
| 35 | if (in == "0"sv || icaseeq(in, "no"sv) || icaseeq(in, "n"sv) || |
| 36 | icaseeq(in, "false"sv) || icaseeq(in, "f"sv) || icaseeq(in, "off"sv)) |
| 37 | { |
| 38 | return false; |
| 39 | } |
| 40 | return std::nullopt; |
| 41 | } |
| 42 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame^] | 43 | fs::path pathForIntfConf(const fs::path& dir, std::string_view intf) |
| 44 | { |
| 45 | return dir / fmt::format(FMT_COMPILE("00-bmc-{}.network"), intf); |
| 46 | } |
| 47 | |
| 48 | fs::path pathForIntfDev(const fs::path& dir, std::string_view intf) |
| 49 | { |
| 50 | return dir / fmt::format(FMT_COMPILE("{}.netdev"), intf); |
| 51 | } |
| 52 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 53 | Parser::Parser(const fs::path& filename) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 54 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 55 | setFile(filename); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 56 | } |
| 57 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 58 | const ValueList& Parser::getValues(std::string_view section, |
| 59 | std::string_view key) const noexcept |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 60 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 61 | static const ValueList empty; |
| 62 | auto sit = sections.find(section); |
| 63 | if (sit == sections.end()) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 64 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 65 | return empty; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 66 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 67 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 68 | auto kit = sit->second.find(key); |
| 69 | if (kit == sit->second.end()) |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 70 | { |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 71 | return empty; |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 72 | } |
| 73 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 74 | return kit->second; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 75 | } |
| 76 | |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 77 | inline bool isspace(char c) noexcept |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 78 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 79 | return c == ' ' || c == '\t'; |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 80 | } |
| 81 | |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 82 | inline bool iscomment(char c) noexcept |
| 83 | { |
| 84 | return c == '#' || c == ';'; |
| 85 | } |
| 86 | |
| 87 | static void removePadding(std::string_view& str) noexcept |
| 88 | { |
| 89 | size_t idx = str.size(); |
| 90 | for (; idx > 0 && isspace(str[idx - 1]); idx--) |
| 91 | ; |
| 92 | str.remove_suffix(str.size() - idx); |
| 93 | |
| 94 | idx = 0; |
| 95 | for (; idx < str.size() && isspace(str[idx]); idx++) |
| 96 | ; |
| 97 | str.remove_prefix(idx); |
| 98 | } |
| 99 | |
| 100 | struct Parse |
| 101 | { |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 102 | std::reference_wrapper<const fs::path> filename; |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 103 | SectionMap sections; |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 104 | KeyValuesMap* section; |
| 105 | std::vector<std::string> warnings; |
| 106 | size_t lineno; |
| 107 | |
| 108 | inline Parse(const fs::path& filename) : |
| 109 | filename(filename), section(nullptr), lineno(0) |
| 110 | { |
| 111 | } |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 112 | |
| 113 | void pumpSection(std::string_view line) |
| 114 | { |
| 115 | auto cpos = line.find(']'); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 116 | if (cpos == line.npos) |
| 117 | { |
| 118 | warnings.emplace_back(fmt::format("{}:{}: Section missing ]", |
| 119 | filename.get().native(), lineno)); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | for (auto c : line.substr(cpos + 1)) |
| 124 | { |
| 125 | if (!isspace(c)) |
| 126 | { |
| 127 | warnings.emplace_back( |
| 128 | fmt::format("{}:{}: Characters outside section name", |
| 129 | filename.get().native(), lineno)); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 134 | auto s = line.substr(0, cpos); |
| 135 | auto it = sections.find(s); |
| 136 | if (it == sections.end()) |
| 137 | { |
| 138 | std::tie(it, std::ignore) = |
| 139 | sections.emplace(Section(s), KeyValuesMap{}); |
| 140 | } |
| 141 | section = &it->second; |
| 142 | } |
| 143 | |
| 144 | void pumpKV(std::string_view line) |
| 145 | { |
| 146 | auto epos = line.find('='); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 147 | std::vector<std::string> new_warnings; |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 148 | if (epos == line.npos) |
| 149 | { |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 150 | new_warnings.emplace_back(fmt::format( |
| 151 | "{}:{}: KV missing `=`", filename.get().native(), lineno)); |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 152 | } |
| 153 | auto k = line.substr(0, epos); |
| 154 | removePadding(k); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 155 | if (section == nullptr) |
| 156 | { |
| 157 | new_warnings.emplace_back( |
| 158 | fmt::format("{}:{}: Key `{}` missing section", |
| 159 | filename.get().native(), lineno, k)); |
| 160 | } |
| 161 | if (!new_warnings.empty()) |
| 162 | { |
| 163 | warnings.insert(warnings.end(), |
| 164 | std::make_move_iterator(new_warnings.begin()), |
| 165 | std::make_move_iterator(new_warnings.end())); |
| 166 | return; |
| 167 | } |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 168 | auto v = line.substr(epos + 1); |
| 169 | removePadding(v); |
| 170 | |
| 171 | auto it = section->find(k); |
| 172 | if (it == section->end()) |
| 173 | { |
| 174 | std::tie(it, std::ignore) = section->emplace(Key(k), ValueList{}); |
| 175 | } |
| 176 | it->second.emplace_back(v); |
| 177 | } |
| 178 | |
| 179 | void pump(std::string_view line) |
| 180 | { |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 181 | lineno++; |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 182 | for (size_t i = 0; i < line.size(); ++i) |
| 183 | { |
| 184 | auto c = line[i]; |
| 185 | if (iscomment(c)) |
| 186 | { |
| 187 | return; |
| 188 | } |
| 189 | else if (c == '[') |
| 190 | { |
| 191 | return pumpSection(line.substr(i + 1)); |
| 192 | } |
| 193 | else if (!isspace(c)) |
| 194 | { |
| 195 | return pumpKV(line.substr(i)); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | }; |
| 200 | |
William A. Kennington III | 25511a1 | 2022-08-04 16:32:28 -0700 | [diff] [blame] | 201 | void Parser::setFile(const fs::path& filename) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 202 | { |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 203 | Parse parse(filename); |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 204 | |
| 205 | try |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 206 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 207 | auto fd = stdplus::fd::open(filename.c_str(), |
| 208 | stdplus::fd::OpenAccess::ReadOnly); |
| 209 | stdplus::fd::LineReader reader(fd); |
| 210 | while (true) |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 211 | { |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 212 | parse.pump(*reader.readLine()); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 213 | } |
| 214 | } |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 215 | catch (const stdplus::exception::Eof&) |
| 216 | { |
| 217 | } |
| 218 | catch (const std::exception& e) |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 219 | { |
| 220 | // TODO: Pass exceptions once callers can handle them |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 221 | parse.warnings.emplace_back( |
| 222 | fmt::format("{}: Read error: {}", filename.native(), e.what())); |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 223 | } |
| 224 | |
William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame^] | 225 | this->filename = filename; |
William A. Kennington III | 61ef4f2 | 2022-08-18 16:29:09 -0700 | [diff] [blame] | 226 | this->sections = std::move(parse.sections); |
William A. Kennington III | bc52d93 | 2022-08-18 16:34:02 -0700 | [diff] [blame] | 227 | this->warnings = std::move(parse.warnings); |
Ratan Gupta | ed123a3 | 2017-06-15 09:07:31 +0530 | [diff] [blame] | 228 | } |
| 229 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 230 | } // namespace config |
| 231 | } // namespace network |
| 232 | } // namespace phosphor |