2015-08-03 14:15:05 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2015-08-03 14:15:05 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2015-10-26 11:05:24 +01:00
|
|
|
#include "base/configwriter.hpp"
|
2015-08-03 14:15:05 +02:00
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include <boost/regex.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2015-08-18 16:53:30 +02:00
|
|
|
#include <boost/algorithm/string/split.hpp>
|
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
2015-08-04 14:47:44 +02:00
|
|
|
#include <set>
|
2015-08-11 06:44:42 +02:00
|
|
|
#include <iterator>
|
2015-08-03 14:15:05 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
void ConfigWriter::EmitBoolean(std::ostream& fp, bool val)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << (val ? "true" : "false");
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
void ConfigWriter::EmitNumber(std::ostream& fp, double val)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
2015-09-30 16:43:32 +02:00
|
|
|
fp << std::fixed << val;
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
void ConfigWriter::EmitString(std::ostream& fp, const String& val)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "\"" << EscapeIcingaString(val) << "\"";
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
void ConfigWriter::EmitEmpty(std::ostream& fp)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "null";
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-11-04 16:42:19 +01:00
|
|
|
void ConfigWriter::EmitArray(std::ostream& fp, int indentLevel, const Array::Ptr& val)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "[ ";
|
2015-11-04 16:42:19 +01:00
|
|
|
EmitArrayItems(fp, indentLevel, val);
|
2015-11-04 17:06:33 +01:00
|
|
|
if (val->GetLength() > 0)
|
|
|
|
fp << " ";
|
|
|
|
fp << "]";
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-11-04 16:42:19 +01:00
|
|
|
void ConfigWriter::EmitArrayItems(std::ostream& fp, int indentLevel, const Array::Ptr& val)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
ObjectLock olock(val);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& item : val) {
|
2015-08-03 14:15:05 +02:00
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << ", ";
|
2015-08-03 14:15:05 +02:00
|
|
|
|
2015-11-04 16:42:19 +01:00
|
|
|
EmitValue(fp, indentLevel, item);
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-22 12:34:11 +02:00
|
|
|
void ConfigWriter::EmitScope(std::ostream& fp, int indentLevel, const Dictionary::Ptr& val,
|
|
|
|
const Array::Ptr& imports, bool splitDot)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "{";
|
2015-08-03 14:15:05 +02:00
|
|
|
|
|
|
|
if (imports && imports->GetLength() > 0) {
|
|
|
|
ObjectLock xlock(imports);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& import : imports) {
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "\n";
|
|
|
|
EmitIndent(fp, indentLevel);
|
|
|
|
fp << "import \"" << import << "\"";
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "\n";
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
if (val) {
|
|
|
|
ObjectLock olock(val);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : val) {
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "\n";
|
|
|
|
EmitIndent(fp, indentLevel);
|
2016-06-22 12:34:11 +02:00
|
|
|
|
|
|
|
if (splitDot) {
|
|
|
|
std::vector<String> tokens;
|
|
|
|
boost::algorithm::split(tokens, kv.first, boost::is_any_of("."));
|
|
|
|
|
|
|
|
EmitIdentifier(fp, tokens[0], true);
|
|
|
|
|
|
|
|
for (std::vector<String>::size_type i = 1; i < tokens.size(); i++) {
|
|
|
|
fp << "[";
|
|
|
|
EmitString(fp, tokens[i]);
|
|
|
|
fp << "]";
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
EmitIdentifier(fp, kv.first, true);
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << " = ";
|
|
|
|
EmitValue(fp, indentLevel + 1, kv.second);
|
2015-08-18 16:53:30 +02:00
|
|
|
}
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "\n";
|
|
|
|
EmitIndent(fp, indentLevel - 1);
|
|
|
|
fp << "}";
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
void ConfigWriter::EmitValue(std::ostream& fp, int indentLevel, const Value& val)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
|
|
|
if (val.IsObjectType<Array>())
|
2015-11-04 16:42:19 +01:00
|
|
|
EmitArray(fp, indentLevel, val);
|
2015-08-03 14:15:05 +02:00
|
|
|
else if (val.IsObjectType<Dictionary>())
|
2015-08-20 16:43:03 +02:00
|
|
|
EmitScope(fp, indentLevel, val);
|
2015-11-25 17:41:03 +01:00
|
|
|
else if (val.IsObjectType<ConfigIdentifier>())
|
|
|
|
EmitIdentifier(fp, static_cast<ConfigIdentifier::Ptr>(val)->GetName(), false);
|
2015-08-03 14:15:05 +02:00
|
|
|
else if (val.IsString())
|
2015-08-20 16:43:03 +02:00
|
|
|
EmitString(fp, val);
|
2015-08-03 14:15:05 +02:00
|
|
|
else if (val.IsNumber())
|
2015-08-20 16:43:03 +02:00
|
|
|
EmitNumber(fp, val);
|
2015-08-03 14:15:05 +02:00
|
|
|
else if (val.IsBoolean())
|
2015-08-20 16:43:03 +02:00
|
|
|
EmitBoolean(fp, val);
|
2015-08-03 14:15:05 +02:00
|
|
|
else if (val.IsEmpty())
|
2015-08-20 16:43:03 +02:00
|
|
|
EmitEmpty(fp);
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
void ConfigWriter::EmitRaw(std::ostream& fp, const String& val)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << val;
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
void ConfigWriter::EmitIndent(std::ostream& fp, int indentLevel)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < indentLevel; i++)
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "\t";
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
void ConfigWriter::EmitIdentifier(std::ostream& fp, const String& identifier, bool inAssignment)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
|
|
|
static std::set<String> keywords;
|
2015-11-04 19:05:47 +01:00
|
|
|
static boost::mutex mutex;
|
|
|
|
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(mutex);
|
|
|
|
if (keywords.empty()) {
|
|
|
|
const std::vector<String>& vkeywords = GetKeywords();
|
|
|
|
std::copy(vkeywords.begin(), vkeywords.end(), std::inserter(keywords, keywords.begin()));
|
|
|
|
}
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (keywords.find(identifier) != keywords.end()) {
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "@" << identifier;
|
2015-08-03 14:15:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::regex expr("^[a-zA-Z_][a-zA-Z0-9\\_]*$");
|
|
|
|
boost::smatch what;
|
|
|
|
if (boost::regex_search(identifier.GetData(), what, expr))
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << identifier;
|
2015-08-03 14:15:05 +02:00
|
|
|
else if (inAssignment)
|
2015-08-20 16:43:03 +02:00
|
|
|
EmitString(fp, identifier);
|
2015-08-03 14:15:05 +02:00
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid identifier"));
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
void ConfigWriter::EmitConfigItem(std::ostream& fp, const String& type, const String& name, bool isTemplate,
|
2015-08-20 17:18:48 +02:00
|
|
|
bool ignoreOnError, const Array::Ptr& imports, const Dictionary::Ptr& attrs)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
|
|
|
if (isTemplate)
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "template ";
|
2015-08-03 14:15:05 +02:00
|
|
|
else
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "object ";
|
2015-08-03 14:15:05 +02:00
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
EmitIdentifier(fp, type, false);
|
|
|
|
fp << " ";
|
|
|
|
EmitString(fp, name);
|
2015-08-20 17:18:48 +02:00
|
|
|
|
|
|
|
if (ignoreOnError)
|
|
|
|
fp << " ignore_on_error";
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << " ";
|
2016-06-22 12:34:11 +02:00
|
|
|
EmitScope(fp, 1, attrs, imports, true);
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
void ConfigWriter::EmitComment(std::ostream& fp, const String& text)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << "/* " << text << " */\n";
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-20 16:43:03 +02:00
|
|
|
void ConfigWriter::EmitFunctionCall(std::ostream& fp, const String& name, const Array::Ptr& arguments)
|
2015-08-03 14:15:05 +02:00
|
|
|
{
|
2015-08-20 16:43:03 +02:00
|
|
|
EmitIdentifier(fp, name, false);
|
|
|
|
fp << "(";
|
2015-11-04 16:42:19 +01:00
|
|
|
EmitArrayItems(fp, 0, arguments);
|
2015-08-20 16:43:03 +02:00
|
|
|
fp << ")";
|
2015-08-03 14:15:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String ConfigWriter::EscapeIcingaString(const String& str)
|
|
|
|
{
|
|
|
|
String result = str;
|
|
|
|
boost::algorithm::replace_all(result, "\\", "\\\\");
|
|
|
|
boost::algorithm::replace_all(result, "\n", "\\n");
|
|
|
|
boost::algorithm::replace_all(result, "\t", "\\t");
|
|
|
|
boost::algorithm::replace_all(result, "\r", "\\r");
|
|
|
|
boost::algorithm::replace_all(result, "\b", "\\b");
|
|
|
|
boost::algorithm::replace_all(result, "\f", "\\f");
|
|
|
|
boost::algorithm::replace_all(result, "\"", "\\\"");
|
|
|
|
return result;
|
|
|
|
}
|
2015-10-26 11:05:24 +01:00
|
|
|
|
|
|
|
const std::vector<String>& ConfigWriter::GetKeywords(void)
|
|
|
|
{
|
|
|
|
static std::vector<String> keywords;
|
2015-11-03 09:46:45 +01:00
|
|
|
static boost::mutex mutex;
|
|
|
|
boost::mutex::scoped_lock lock(mutex);
|
2015-10-26 11:05:24 +01:00
|
|
|
|
|
|
|
if (keywords.empty()) {
|
|
|
|
keywords.push_back("object");
|
|
|
|
keywords.push_back("template");
|
|
|
|
keywords.push_back("include");
|
|
|
|
keywords.push_back("include_recursive");
|
|
|
|
keywords.push_back("include_zones");
|
|
|
|
keywords.push_back("library");
|
|
|
|
keywords.push_back("null");
|
|
|
|
keywords.push_back("true");
|
|
|
|
keywords.push_back("false");
|
|
|
|
keywords.push_back("const");
|
|
|
|
keywords.push_back("var");
|
|
|
|
keywords.push_back("this");
|
|
|
|
keywords.push_back("globals");
|
|
|
|
keywords.push_back("locals");
|
|
|
|
keywords.push_back("use");
|
2016-08-12 11:25:36 +02:00
|
|
|
keywords.push_back("__using");
|
2016-08-28 10:27:43 +02:00
|
|
|
keywords.push_back("default");
|
2015-10-26 11:05:24 +01:00
|
|
|
keywords.push_back("ignore_on_error");
|
2015-11-03 16:34:34 +01:00
|
|
|
keywords.push_back("current_filename");
|
|
|
|
keywords.push_back("current_line");
|
2015-10-26 11:05:24 +01:00
|
|
|
keywords.push_back("apply");
|
|
|
|
keywords.push_back("to");
|
|
|
|
keywords.push_back("where");
|
|
|
|
keywords.push_back("import");
|
|
|
|
keywords.push_back("assign");
|
|
|
|
keywords.push_back("ignore");
|
|
|
|
keywords.push_back("function");
|
|
|
|
keywords.push_back("return");
|
|
|
|
keywords.push_back("break");
|
|
|
|
keywords.push_back("continue");
|
|
|
|
keywords.push_back("for");
|
|
|
|
keywords.push_back("if");
|
|
|
|
keywords.push_back("else");
|
|
|
|
keywords.push_back("while");
|
|
|
|
keywords.push_back("throw");
|
|
|
|
}
|
|
|
|
|
|
|
|
return keywords;
|
|
|
|
}
|
2015-11-25 17:41:03 +01:00
|
|
|
|
|
|
|
ConfigIdentifier::ConfigIdentifier(const String& identifier)
|
|
|
|
: m_Name(identifier)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
String ConfigIdentifier::GetName(void) const
|
|
|
|
{
|
|
|
|
return m_Name;
|
|
|
|
}
|