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