1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "base/configwriter.hpp"
#include "base/exception.hpp"
#include <boost/regex.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <set>
#include <iterator>
using namespace icinga;
void ConfigWriter::EmitBoolean(std::ostream& fp, bool val)
{
fp << (val ? "true" : "false");
}
void ConfigWriter::EmitNumber(std::ostream& fp, double val)
{
fp << std::fixed << val;
}
void ConfigWriter::EmitString(std::ostream& fp, const String& val)
{
fp << "\"" << EscapeIcingaString(val) << "\"";
}
void ConfigWriter::EmitEmpty(std::ostream& fp)
{
fp << "null";
}
void ConfigWriter::EmitArray(std::ostream& fp, int indentLevel, const Array::Ptr& val)
{
fp << "[ ";
EmitArrayItems(fp, indentLevel, val);
if (val->GetLength() > 0)
fp << " ";
fp << "]";
}
void ConfigWriter::EmitArrayItems(std::ostream& fp, int indentLevel, const Array::Ptr& val)
{
bool first = true;
ObjectLock olock(val);
for (const Value& item : val) {
if (first)
first = false;
else
fp << ", ";
EmitValue(fp, indentLevel, item);
}
}
void ConfigWriter::EmitScope(std::ostream& fp, int indentLevel, const Dictionary::Ptr& val,
const Array::Ptr& imports, bool splitDot)
{
fp << "{";
if (imports && imports->GetLength() > 0) {
ObjectLock xlock(imports);
for (const Value& import : imports) {
fp << "\n";
EmitIndent(fp, indentLevel);
fp << "import \"" << import << "\"";
}
fp << "\n";
}
if (val) {
ObjectLock olock(val);
for (const Dictionary::Pair& kv : val) {
fp << "\n";
EmitIndent(fp, indentLevel);
if (splitDot) {
std::vector<String> tokens = kv.first.Split(".");
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);
fp << " = ";
EmitValue(fp, indentLevel + 1, kv.second);
}
}
fp << "\n";
EmitIndent(fp, indentLevel - 1);
fp << "}";
}
void ConfigWriter::EmitValue(std::ostream& fp, int indentLevel, const Value& val)
{
if (val.IsObjectType<Array>())
EmitArray(fp, indentLevel, val);
else if (val.IsObjectType<Dictionary>())
EmitScope(fp, indentLevel, val);
else if (val.IsObjectType<ConfigIdentifier>())
EmitIdentifier(fp, static_cast<ConfigIdentifier::Ptr>(val)->GetName(), false);
else if (val.IsString())
EmitString(fp, val);
else if (val.IsNumber())
EmitNumber(fp, val);
else if (val.IsBoolean())
EmitBoolean(fp, val);
else if (val.IsEmpty())
EmitEmpty(fp);
}
void ConfigWriter::EmitRaw(std::ostream& fp, const String& val)
{
fp << val;
}
void ConfigWriter::EmitIndent(std::ostream& fp, int indentLevel)
{
for (int i = 0; i < indentLevel; i++)
fp << "\t";
}
void ConfigWriter::EmitIdentifier(std::ostream& fp, const String& identifier, bool inAssignment)
{
static std::set<String> keywords;
static std::mutex mutex;
{
std::unique_lock<std::mutex> lock(mutex);
if (keywords.empty()) {
const std::vector<String>& vkeywords = GetKeywords();
std::copy(vkeywords.begin(), vkeywords.end(), std::inserter(keywords, keywords.begin()));
}
}
if (keywords.find(identifier) != keywords.end()) {
fp << "@" << identifier;
return;
}
boost::regex expr("^[a-zA-Z_][a-zA-Z0-9\\_]*$");
boost::smatch what;
if (boost::regex_search(identifier.GetData(), what, expr))
fp << identifier;
else if (inAssignment)
EmitString(fp, identifier);
else
BOOST_THROW_EXCEPTION(std::invalid_argument("Invalid identifier"));
}
void ConfigWriter::EmitConfigItem(std::ostream& fp, const String& type, const String& name, bool isTemplate,
bool ignoreOnError, const Array::Ptr& imports, const Dictionary::Ptr& attrs)
{
if (isTemplate)
fp << "template ";
else
fp << "object ";
EmitIdentifier(fp, type, false);
fp << " ";
EmitString(fp, name);
if (ignoreOnError)
fp << " ignore_on_error";
fp << " ";
EmitScope(fp, 1, attrs, imports, true);
}
void ConfigWriter::EmitComment(std::ostream& fp, const String& text)
{
fp << "/* " << text << " */\n";
}
void ConfigWriter::EmitFunctionCall(std::ostream& fp, const String& name, const Array::Ptr& arguments)
{
EmitIdentifier(fp, name, false);
fp << "(";
EmitArrayItems(fp, 0, arguments);
fp << ")";
}
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;
}
const std::vector<String>& ConfigWriter::GetKeywords()
{
static std::vector<String> keywords;
static std::mutex mutex;
std::unique_lock<std::mutex> lock(mutex);
if (keywords.empty()) {
keywords.emplace_back("object");
keywords.emplace_back("template");
keywords.emplace_back("include");
keywords.emplace_back("include_recursive");
keywords.emplace_back("include_zones");
keywords.emplace_back("library");
keywords.emplace_back("null");
keywords.emplace_back("true");
keywords.emplace_back("false");
keywords.emplace_back("const");
keywords.emplace_back("var");
keywords.emplace_back("this");
keywords.emplace_back("globals");
keywords.emplace_back("locals");
keywords.emplace_back("use");
keywords.emplace_back("using");
keywords.emplace_back("namespace");
keywords.emplace_back("default");
keywords.emplace_back("ignore_on_error");
keywords.emplace_back("current_filename");
keywords.emplace_back("current_line");
keywords.emplace_back("apply");
keywords.emplace_back("to");
keywords.emplace_back("where");
keywords.emplace_back("import");
keywords.emplace_back("assign");
keywords.emplace_back("ignore");
keywords.emplace_back("function");
keywords.emplace_back("return");
keywords.emplace_back("break");
keywords.emplace_back("continue");
keywords.emplace_back("for");
keywords.emplace_back("if");
keywords.emplace_back("else");
keywords.emplace_back("while");
keywords.emplace_back("throw");
keywords.emplace_back("try");
keywords.emplace_back("except");
}
return keywords;
}
ConfigIdentifier::ConfigIdentifier(String identifier)
: m_Name(std::move(identifier))
{ }
String ConfigIdentifier::GetName() const
{
return m_Name;
}
|