2016-09-01 17:50:48 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2007-01-25 07:40:21 +01:00
|
|
|
*/
|
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
#include <boost/spirit/include/qi.hpp>
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-09-06 01:10:30 +02:00
|
|
|
using namespace ircd;
|
|
|
|
|
2016-09-08 22:29:02 +02:00
|
|
|
namespace qi = boost::spirit::qi;
|
|
|
|
namespace ascii = qi::ascii;
|
|
|
|
using qi::lexeme;
|
|
|
|
using qi::char_;
|
|
|
|
using qi::lit;
|
|
|
|
using qi::eol;
|
|
|
|
using qi::blank;
|
|
|
|
using qi::eps;
|
|
|
|
|
2016-09-06 01:10:30 +02:00
|
|
|
/*
|
|
|
|
registry['A'] = "admin";
|
|
|
|
registry['B'] = "blacklist";
|
|
|
|
registry['C'] = "connect";
|
|
|
|
registry['I'] = "auth";
|
|
|
|
registry['O'] = "operator";
|
|
|
|
registry['P'] = "listen";
|
|
|
|
registry['U'] = "service";
|
|
|
|
registry['Y'] = "class";
|
|
|
|
registry['a'] = "alias";
|
|
|
|
registry['d'] = "exempt";
|
|
|
|
registry['g'] = "general";
|
|
|
|
registry['l'] = "log";
|
|
|
|
*/
|
|
|
|
|
2016-09-08 22:29:02 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace conf {
|
|
|
|
namespace newconf {
|
2016-08-13 05:05:54 +02:00
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
using str = std::string;
|
|
|
|
using strvec = std::vector<str>;
|
|
|
|
using strvecvec = std::vector<strvec>;
|
|
|
|
using strvecvecvec = std::vector<strvecvec>;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
template<class iter>
|
|
|
|
struct ignores
|
2016-09-06 01:10:30 +02:00
|
|
|
:qi::grammar<iter>
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
using rule = qi::rule<iter>;
|
2016-04-02 09:29:48 +02:00
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
rule c_comment;
|
|
|
|
rule cc_comment;
|
|
|
|
rule sh_comment;
|
|
|
|
rule skip;
|
2016-04-02 09:29:48 +02:00
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
ignores();
|
2016-04-02 09:29:48 +02:00
|
|
|
};
|
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
template<class iter,
|
|
|
|
class ignores>
|
2016-09-08 22:29:02 +02:00
|
|
|
struct parser
|
2016-09-06 01:10:30 +02:00
|
|
|
:qi::grammar<iter, strvecvecvec(), ignores>
|
2016-04-01 11:11:04 +02:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
template<class ret> using rule = qi::rule<iter, ret, ignores>;
|
2016-04-02 09:29:48 +02:00
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
rule<str()> unquoted;
|
|
|
|
rule<str()> quoted;
|
|
|
|
rule<str()> key;
|
|
|
|
rule<strvec()> item;
|
|
|
|
rule<strvec()> topitem;
|
|
|
|
rule<strvecvec()> block;
|
|
|
|
rule<strvecvecvec()> conf;
|
2016-04-02 09:29:48 +02:00
|
|
|
|
2016-09-08 22:29:02 +02:00
|
|
|
parser();
|
2016-09-01 17:50:48 +02:00
|
|
|
};
|
2016-04-01 11:11:04 +02:00
|
|
|
|
2016-09-08 22:29:02 +02:00
|
|
|
letters registry;
|
|
|
|
|
|
|
|
} // namespace newconf
|
|
|
|
} // namespace conf
|
|
|
|
} // namespace ircd
|
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
template<class iter,
|
|
|
|
class ignores>
|
2016-09-08 22:29:02 +02:00
|
|
|
conf::newconf::parser<iter, ignores>::parser()
|
|
|
|
:parser::base_type // pass reference the topmost level to begin parsing on
|
2016-04-02 09:29:48 +02:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
conf
|
2016-04-01 11:11:04 +02:00
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
,unquoted // config values without double-quotes, cannot have ';' because that's the ending
|
2016-04-01 11:11:04 +02:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
lexeme[+(char_ - ';')]
|
2016-09-08 22:29:02 +02:00
|
|
|
,"unquoted"
|
2016-04-02 09:29:48 +02:00
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
,quoted // config values surrounded by double quotes, which cannot have double quotes in them
|
2016-04-02 09:29:48 +02:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
lexeme['"' >> +(char_ - '"') >> '"']
|
2016-09-08 22:29:02 +02:00
|
|
|
,"quoted"
|
2016-04-01 11:11:04 +02:00
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
,key // configuration key, must be simple alnum's with underscores
|
2016-04-01 11:11:04 +02:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
+char_("[a-zA-Z0-9_]")
|
2016-09-08 22:29:02 +02:00
|
|
|
,"key"
|
2016-04-01 11:11:04 +02:00
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
,item // a key-value pair
|
2016-04-01 11:11:04 +02:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
+(key) >> '=' >> +(quoted | unquoted) >> ';'
|
2016-09-08 22:29:02 +02:00
|
|
|
,"item"
|
2016-04-01 11:11:04 +02:00
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
,topitem // a key-value pair, but at the topconf no '=' is used
|
2016-04-01 11:11:04 +02:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
+(key) >> +(quoted) >> ';'
|
2016-09-08 22:29:02 +02:00
|
|
|
,"topitem"
|
2016-04-02 09:29:48 +02:00
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
,block // a bracketed conf block, the type-label is like a key, and it can have a unique quoted label
|
2016-04-02 09:29:48 +02:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
*(key >> *(quoted)) >> '{' >> *(item) >> '}' >> ';'
|
2016-09-08 22:29:02 +02:00
|
|
|
,"block"
|
2016-04-02 09:29:48 +02:00
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
,conf // newconf is comprised of either conf blocks or key-value's at the top level
|
2016-04-02 09:29:48 +02:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
*(block | topitem)
|
2016-09-08 22:29:02 +02:00
|
|
|
,"conf"
|
2016-04-02 09:29:48 +02:00
|
|
|
}
|
|
|
|
{
|
2016-04-01 11:11:04 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
template<class iter>
|
2016-09-08 22:29:02 +02:00
|
|
|
conf::newconf::ignores<iter>::ignores()
|
2016-09-01 17:50:48 +02:00
|
|
|
:ignores::base_type // pass reference to the topmost skip-parser
|
2016-04-02 10:11:12 +02:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
skip
|
2016-04-02 10:11:12 +02:00
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
,c_comment // a multiline comment
|
2016-04-03 01:37:59 +02:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
lit('/') >> lit('*') >> *(char_ - (lit('*') >> '/')) >> lit("*/")
|
2016-04-03 01:37:59 +02:00
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
,cc_comment // a single line comment, like this one :)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
lit('/') >> lit('/') >> *(char_ - eol) >> eol | blank
|
2016-02-20 00:11:06 +01:00
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
,sh_comment // a single line shell comment
|
2016-02-20 00:11:06 +01:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
'#' >> *(char_ - eol) >> eol | blank
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
2016-09-01 17:50:48 +02:00
|
|
|
,skip // the skip parser is one and any one of the comment types, or unmatched whitespace
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
+(ascii::space | c_comment | cc_comment | sh_comment)
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-09-08 22:29:02 +02:00
|
|
|
conf::newconf::topconf
|
|
|
|
conf::newconf::parse_file(const std::string &path)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
std::ifstream file(path);
|
|
|
|
return parse(file);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 22:29:02 +02:00
|
|
|
conf::newconf::topconf
|
|
|
|
conf::newconf::parse(std::ifstream &file)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
const std::istreambuf_iterator<char> bit(file), eit;
|
|
|
|
return parse(std::string(bit, eit));
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 22:29:02 +02:00
|
|
|
conf::newconf::topconf
|
|
|
|
conf::newconf::parse(const std::string &str)
|
|
|
|
try
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
using iter = std::string::const_iterator;
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
strvecvecvec vec;
|
|
|
|
const ignores<iter> ign;
|
2016-09-08 22:29:02 +02:00
|
|
|
const newconf::parser<iter, ignores<iter>> p;
|
2016-09-01 17:50:48 +02:00
|
|
|
if(!phrase_parse(begin(str), end(str), p, ign, vec))
|
2016-09-08 22:29:02 +02:00
|
|
|
throw syntax_error("newconf parse failed for unknown reason");
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-09-08 22:29:02 +02:00
|
|
|
// This was my first qi grammar and there's a lot wrong with it.
|
|
|
|
// Once it's revisited it should parse directly into the ideal structures
|
|
|
|
// returned to the user. Until then it's translated here.
|
2016-09-01 17:50:48 +02:00
|
|
|
topconf top;
|
|
|
|
for(const auto &v : vec)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
key k; block b;
|
|
|
|
for(const auto &vv : v)
|
2007-01-25 07:40:21 +01:00
|
|
|
{
|
2016-09-01 17:50:48 +02:00
|
|
|
if(vv.empty())
|
|
|
|
{
|
|
|
|
// For unnamed blocks (without double-quoted names) the "*" serves
|
|
|
|
// as a placeholder indicating no-name rather than ""
|
|
|
|
b.first = "*";
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
item i;
|
|
|
|
for(const auto &vvv : vv)
|
|
|
|
{
|
|
|
|
if(vvv.empty())
|
|
|
|
continue;
|
|
|
|
else if(k.empty())
|
|
|
|
k = vvv;
|
|
|
|
else if(b.first.empty())
|
|
|
|
b.first = vvv;
|
|
|
|
else if(i.first.empty())
|
|
|
|
i.first = vvv;
|
|
|
|
else
|
|
|
|
i.second.emplace_back(vvv);
|
|
|
|
}
|
2007-01-25 07:40:21 +01:00
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
if(!i.first.empty())
|
|
|
|
b.second.emplace_back(i);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2016-09-06 01:10:30 +02:00
|
|
|
top.emplace_back(k, b);
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
|
|
|
|
2016-09-01 17:50:48 +02:00
|
|
|
return top;
|
2007-01-25 07:40:21 +01:00
|
|
|
}
|
2016-09-08 22:29:02 +02:00
|
|
|
catch(const boost::spirit::qi::expectation_failure<std::string::const_iterator> &e)
|
|
|
|
{
|
|
|
|
const size_t character(e.last - e.first);
|
|
|
|
const auto line_num(std::count(begin(str), begin(str)+character, '\n'));
|
|
|
|
|
|
|
|
size_t char_num(0);
|
|
|
|
size_t line_start(character);
|
|
|
|
while(line_start && str[--line_start] != '\n')
|
|
|
|
char_num++;
|
|
|
|
|
|
|
|
throw syntax_error("@line %zu +%zu: expecting :%s",
|
|
|
|
line_num,
|
|
|
|
char_num,
|
|
|
|
string(e.what_).c_str());
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
ircd::log::error("Unexpected newconf error during parsing: %s", e.what());
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2016-09-06 01:10:30 +02:00
|
|
|
|
|
|
|
std::list<std::string>
|
|
|
|
conf::newconf::translate(const topconf &top)
|
|
|
|
{
|
|
|
|
std::list<std::string> ret;
|
|
|
|
translate(top, [&ret](std::string line)
|
|
|
|
{
|
|
|
|
ret.emplace_back(std::move(line));
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
conf::newconf::translate(const topconf &top,
|
|
|
|
const std::function<void (std::string)> &closure)
|
|
|
|
{
|
|
|
|
for(const auto &pair : top) try
|
|
|
|
{
|
|
|
|
const auto &type(pair.first);
|
|
|
|
const auto &block(pair.second);
|
|
|
|
const auto &label(block.first);
|
|
|
|
const auto &items(block.second);
|
|
|
|
const auto &letter(find_letter(type));
|
|
|
|
for(const auto &item : items)
|
|
|
|
{
|
|
|
|
const auto &key(item.first);
|
|
|
|
const auto &vals(item.second);
|
|
|
|
std::stringstream buf;
|
|
|
|
buf << letter << " "
|
|
|
|
<< label << " "
|
|
|
|
<< key << " :";
|
|
|
|
|
|
|
|
for(auto i(0); i < int(vals.size()) - 1; ++i)
|
|
|
|
buf << vals.at(i) << " ";
|
|
|
|
|
|
|
|
if(!vals.empty())
|
|
|
|
buf << vals.back();
|
|
|
|
|
|
|
|
closure(buf.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(const error &e)
|
|
|
|
{
|
|
|
|
log.warning("%s", e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
|
|
|
conf::newconf::find_letter(const std::string &name)
|
|
|
|
{
|
|
|
|
const auto ®istry(newconf::registry);
|
|
|
|
const auto it(std::find(begin(registry), end(registry), name));
|
|
|
|
if(it == end(registry))
|
|
|
|
throw unknown_block("%s is not registered to a letter", name.c_str());
|
|
|
|
|
|
|
|
return std::distance(begin(registry), it);
|
|
|
|
}
|