blob: e73c889fea810acf9eddcf398b677ca84a1c6d1e [file] [log] [blame]
Ratan Guptaed123a32017-06-15 09:07:31 +05301#include "config_parser.hpp"
Ratan Guptaed123a32017-06-15 09:07:31 +05302
William A. Kennington III61ef4f22022-08-18 16:29:09 -07003#include <stdplus/exception.hpp>
William A. Kennington III409f1a62022-08-11 15:44:37 -07004#include <stdplus/fd/atomic.hpp>
William A. Kennington III61ef4f22022-08-18 16:29:09 -07005#include <stdplus/fd/create.hpp>
William A. Kennington III409f1a62022-08-11 15:44:37 -07006#include <stdplus/fd/fmt.hpp>
William A. Kennington III61ef4f22022-08-18 16:29:09 -07007#include <stdplus/fd/line.hpp>
William A. Kennington III86642522023-07-24 17:55:55 -07008#include <stdplus/str/cat.hpp>
Patrick Williams89d734b2023-05-10 07:50:25 -05009
William A. Kennington IIIcafc1512023-07-25 02:22:32 -070010#include <format>
Patrick Williams89d734b2023-05-10 07:50:25 -050011#include <functional>
12#include <iterator>
13#include <stdexcept>
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -070014#include <string>
William A. Kennington III61ef4f22022-08-18 16:29:09 -070015#include <utility>
Ratan Guptaed123a32017-06-15 09:07:31 +053016
17namespace phosphor
18{
19namespace network
20{
21namespace config
22{
23
William A. Kennington III150753f2022-08-05 15:20:54 -070024using std::literals::string_view_literals::operator""sv;
25
26bool 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
32std::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 IIIa520a392022-08-08 12:17:34 -070047fs::path pathForIntfConf(const fs::path& dir, std::string_view intf)
48{
William A. Kennington III86642522023-07-24 17:55:55 -070049 return dir / stdplus::strCat("00-bmc-"sv, intf, ".network"sv);
William A. Kennington IIIa520a392022-08-08 12:17:34 -070050}
51
52fs::path pathForIntfDev(const fs::path& dir, std::string_view intf)
53{
William A. Kennington III86642522023-07-24 17:55:55 -070054 return dir / stdplus::strCat(intf, ".netdev"sv);
William A. Kennington IIIa520a392022-08-08 12:17:34 -070055}
56
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -070057const std::string*
58 SectionMap::getLastValueString(std::string_view section,
59 std::string_view key) const noexcept
60{
61 auto sit = find(section);
62 if (sit == end())
63 {
64 return nullptr;
65 }
66 for (auto it = sit->second.rbegin(); it != sit->second.rend(); ++it)
67 {
68 auto kit = it->find(key);
69 if (kit == it->end() || kit->second.empty())
70 {
71 continue;
72 }
William A. Kennington III0dd09372022-08-18 14:57:07 -070073 return &kit->second.back().get();
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -070074 }
75 return nullptr;
76}
77
78std::vector<std::string> SectionMap::getValueStrings(std::string_view section,
79 std::string_view key) const
80{
81 return getValues(section, key,
82 [](const Value& v) { return std::string(v); });
83}
84
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070085void KeyCheck::operator()(std::string_view s)
William A. Kennington III0dd09372022-08-18 14:57:07 -070086{
87 for (auto c : s)
88 {
89 if (c == '\n' || c == '=')
90 {
91 throw std::invalid_argument(
William A. Kennington III86642522023-07-24 17:55:55 -070092 stdplus::strCat("Invalid Config Key: "sv, s));
William A. Kennington III0dd09372022-08-18 14:57:07 -070093 }
94 }
95}
96
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -070097void SectionCheck::operator()(std::string_view s)
William A. Kennington III0dd09372022-08-18 14:57:07 -070098{
99 for (auto c : s)
100 {
101 if (c == '\n' || c == ']')
102 {
103 throw std::invalid_argument(
William A. Kennington III86642522023-07-24 17:55:55 -0700104 stdplus::strCat("Invalid Config Section: "sv, s));
William A. Kennington III0dd09372022-08-18 14:57:07 -0700105 }
106 }
107}
108
William A. Kennington IIIbe3bd2f2022-10-11 14:11:27 -0700109void ValueCheck::operator()(std::string_view s)
William A. Kennington III0dd09372022-08-18 14:57:07 -0700110{
111 for (auto c : s)
112 {
113 if (c == '\n')
114 {
115 throw std::invalid_argument(
William A. Kennington III86642522023-07-24 17:55:55 -0700116 stdplus::strCat("Invalid Config Value: "sv, s));
William A. Kennington III0dd09372022-08-18 14:57:07 -0700117 }
118 }
119}
120
William A. Kennington III25511a12022-08-04 16:32:28 -0700121Parser::Parser(const fs::path& filename)
Ratan Guptaed123a32017-06-15 09:07:31 +0530122{
William A. Kennington III25511a12022-08-04 16:32:28 -0700123 setFile(filename);
Ratan Guptaed123a32017-06-15 09:07:31 +0530124}
125
William A. Kennington IIIf55b7d82022-10-25 14:21:46 -0700126constexpr bool isspace(char c) noexcept
Ratan Guptaed123a32017-06-15 09:07:31 +0530127{
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700128 return c == ' ' || c == '\t';
Ratan Guptaed123a32017-06-15 09:07:31 +0530129}
130
William A. Kennington IIIf55b7d82022-10-25 14:21:46 -0700131constexpr bool iscomment(char c) noexcept
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700132{
133 return c == '#' || c == ';';
134}
135
136static void removePadding(std::string_view& str) noexcept
137{
138 size_t idx = str.size();
139 for (; idx > 0 && isspace(str[idx - 1]); idx--)
140 ;
141 str.remove_suffix(str.size() - idx);
142
143 idx = 0;
144 for (; idx < str.size() && isspace(str[idx]); idx++)
145 ;
146 str.remove_prefix(idx);
147}
148
149struct Parse
150{
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700151 std::reference_wrapper<const fs::path> filename;
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700152 SectionMap map;
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700153 KeyValuesMap* section;
154 std::vector<std::string> warnings;
155 size_t lineno;
156
157 inline Parse(const fs::path& filename) :
158 filename(filename), section(nullptr), lineno(0)
Patrick Williams89d734b2023-05-10 07:50:25 -0500159 {}
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700160
161 void pumpSection(std::string_view line)
162 {
163 auto cpos = line.find(']');
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700164 if (cpos == line.npos)
165 {
William A. Kennington IIIcafc1512023-07-25 02:22:32 -0700166 warnings.emplace_back(std::format("{}:{}: Section missing ]",
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700167 filename.get().native(), lineno));
168 }
169 else
170 {
171 for (auto c : line.substr(cpos + 1))
172 {
173 if (!isspace(c))
174 {
175 warnings.emplace_back(
William A. Kennington IIIcafc1512023-07-25 02:22:32 -0700176 std::format("{}:{}: Characters outside section name",
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700177 filename.get().native(), lineno));
178 break;
179 }
180 }
181 }
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700182 auto s = line.substr(0, cpos);
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700183 auto it = map.find(s);
184 if (it == map.end())
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700185 {
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700186 std::tie(it, std::ignore) = map.emplace(
William A. Kennington III0dd09372022-08-18 14:57:07 -0700187 Section(Section::unchecked(), s), KeyValuesMapList{});
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700188 }
William A. Kennington IIIe21a5cf2022-08-09 12:19:14 -0700189 section = &it->second.emplace_back();
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700190 }
191
192 void pumpKV(std::string_view line)
193 {
194 auto epos = line.find('=');
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700195 std::vector<std::string> new_warnings;
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700196 if (epos == line.npos)
197 {
William A. Kennington IIIcafc1512023-07-25 02:22:32 -0700198 new_warnings.emplace_back(std::format(
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700199 "{}:{}: KV missing `=`", filename.get().native(), lineno));
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700200 }
201 auto k = line.substr(0, epos);
202 removePadding(k);
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700203 if (section == nullptr)
204 {
205 new_warnings.emplace_back(
William A. Kennington IIIcafc1512023-07-25 02:22:32 -0700206 std::format("{}:{}: Key `{}` missing section",
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700207 filename.get().native(), lineno, k));
208 }
209 if (!new_warnings.empty())
210 {
211 warnings.insert(warnings.end(),
212 std::make_move_iterator(new_warnings.begin()),
213 std::make_move_iterator(new_warnings.end()));
214 return;
215 }
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700216 auto v = line.substr(epos + 1);
217 removePadding(v);
218
219 auto it = section->find(k);
220 if (it == section->end())
221 {
William A. Kennington III0dd09372022-08-18 14:57:07 -0700222 std::tie(it, std::ignore) =
223 section->emplace(Key(Key::unchecked(), k), ValueList{});
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700224 }
William A. Kennington III0dd09372022-08-18 14:57:07 -0700225 it->second.emplace_back(Value::unchecked(), v);
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700226 }
227
228 void pump(std::string_view line)
229 {
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700230 lineno++;
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700231 for (size_t i = 0; i < line.size(); ++i)
232 {
233 auto c = line[i];
234 if (iscomment(c))
235 {
236 return;
237 }
238 else if (c == '[')
239 {
240 return pumpSection(line.substr(i + 1));
241 }
242 else if (!isspace(c))
243 {
244 return pumpKV(line.substr(i));
245 }
246 }
247 }
248};
249
William A. Kennington III25511a12022-08-04 16:32:28 -0700250void Parser::setFile(const fs::path& filename)
Ratan Guptaed123a32017-06-15 09:07:31 +0530251{
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700252 Parse parse(filename);
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700253
William A. Kennington III301e8ad2022-11-15 15:45:32 -0800254 bool fileExists = true;
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700255 try
Ratan Guptaed123a32017-06-15 09:07:31 +0530256 {
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700257 auto fd = stdplus::fd::open(filename.c_str(),
258 stdplus::fd::OpenAccess::ReadOnly);
259 stdplus::fd::LineReader reader(fd);
260 while (true)
Ratan Guptaed123a32017-06-15 09:07:31 +0530261 {
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700262 parse.pump(*reader.readLine());
Ratan Guptaed123a32017-06-15 09:07:31 +0530263 }
264 }
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700265 catch (const stdplus::exception::Eof&)
Patrick Williams89d734b2023-05-10 07:50:25 -0500266 {}
William A. Kennington III301e8ad2022-11-15 15:45:32 -0800267 catch (const std::system_error& e)
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700268 {
William A. Kennington III301e8ad2022-11-15 15:45:32 -0800269 fileExists = false;
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700270 // TODO: Pass exceptions once callers can handle them
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700271 parse.warnings.emplace_back(
William A. Kennington IIIcafc1512023-07-25 02:22:32 -0700272 std::format("{}: Open error: {}", filename.native(), e.what()));
William A. Kennington III61ef4f22022-08-18 16:29:09 -0700273 }
274
William A. Kennington III34bb3e22022-08-18 15:17:22 -0700275 this->map = std::move(parse.map);
William A. Kennington III301e8ad2022-11-15 15:45:32 -0800276 this->fileExists = fileExists;
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700277 this->filename = filename;
William A. Kennington IIIbc52d932022-08-18 16:34:02 -0700278 this->warnings = std::move(parse.warnings);
Ratan Guptaed123a32017-06-15 09:07:31 +0530279}
280
William A. Kennington III409f1a62022-08-11 15:44:37 -0700281static void writeFileInt(const SectionMap& map, const fs::path& filename)
282{
283 stdplus::fd::AtomicWriter writer(filename, 0644);
284 stdplus::fd::FormatBuffer out(writer);
285 for (const auto& [section, maps] : map)
286 {
287 for (const auto& map : maps)
288 {
William A. Kennington III86642522023-07-24 17:55:55 -0700289 out.appends("["sv, section.get(), "]\n"sv);
William A. Kennington III409f1a62022-08-11 15:44:37 -0700290 for (const auto& [key, vals] : map)
291 {
292 for (const auto& val : vals)
293 {
William A. Kennington III86642522023-07-24 17:55:55 -0700294 out.appends(key.get(), "="sv, val.get(), "\n"sv);
William A. Kennington III409f1a62022-08-11 15:44:37 -0700295 }
296 }
297 }
298 }
299 out.flush();
300 writer.commit();
301}
302
303void Parser::writeFile() const
304{
305 writeFileInt(map, filename);
306}
307
308void Parser::writeFile(const fs::path& filename)
309{
310 writeFileInt(map, filename);
311 this->filename = filename;
312}
313
Gunnar Mills57d9c502018-09-14 14:42:34 -0500314} // namespace config
315} // namespace network
316} // namespace phosphor