2012-06-06 14:38:28 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2015-01-22 12:00:23 +01:00
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
2012-06-06 14:38:28 +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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "config/configcompiler.hpp"
|
|
|
|
#include "config/configitem.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/utility.hpp"
|
2015-03-18 13:24:31 +01:00
|
|
|
#include "base/loader.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/context.hpp"
|
|
|
|
#include "base/exception.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <fstream>
|
|
|
|
#include <boost/foreach.hpp>
|
2012-05-31 09:43:46 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<String> ConfigCompiler::m_IncludeSearchDirs;
|
2015-08-24 14:14:44 +02:00
|
|
|
boost::mutex ConfigCompiler::m_ZoneDirsMutex;
|
2015-07-21 09:32:17 +02:00
|
|
|
std::map<String, std::vector<ZoneFragment> > ConfigCompiler::m_ZoneDirs;
|
2013-02-01 22:44:58 +01:00
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the ConfigCompiler class.
|
|
|
|
*
|
|
|
|
* @param path The path of the configuration file (or another name that
|
|
|
|
* identifies the source of the configuration text).
|
|
|
|
* @param input Input stream for the configuration file.
|
2014-05-13 14:40:12 +02:00
|
|
|
* @param zone The zone.
|
2012-09-19 12:32:39 +02:00
|
|
|
*/
|
2015-08-17 16:08:57 +02:00
|
|
|
ConfigCompiler::ConfigCompiler(const String& path, std::istream *input,
|
2015-08-28 17:58:29 +02:00
|
|
|
const String& zone, const String& package)
|
|
|
|
: m_Path(path), m_Input(input), m_Zone(zone), m_Package(package),
|
2015-09-23 16:37:21 +02:00
|
|
|
m_Eof(false), m_OpenBraces(0)
|
2012-05-31 09:43:46 +02:00
|
|
|
{
|
|
|
|
InitializeScanner();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Destructor for the ConfigCompiler class.
|
|
|
|
*/
|
2012-06-06 14:38:28 +02:00
|
|
|
ConfigCompiler::~ConfigCompiler(void)
|
2012-05-31 09:43:46 +02:00
|
|
|
{
|
|
|
|
DestroyScanner();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Reads data from the input stream. Used internally by the lexer.
|
|
|
|
*
|
|
|
|
* @param buffer Where to store data.
|
|
|
|
* @param max_size The maximum number of bytes to read from the stream.
|
|
|
|
* @returns The actual number of bytes read.
|
|
|
|
*/
|
2012-06-06 14:38:28 +02:00
|
|
|
size_t ConfigCompiler::ReadInput(char *buffer, size_t max_size)
|
2012-05-31 09:43:46 +02:00
|
|
|
{
|
2013-01-23 21:58:19 +01:00
|
|
|
m_Input->read(buffer, max_size);
|
2012-06-15 19:32:41 +02:00
|
|
|
return static_cast<size_t>(m_Input->gcount());
|
2012-05-31 09:43:46 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the scanner object.
|
|
|
|
*
|
|
|
|
* @returns The scanner object.
|
|
|
|
*/
|
2012-06-06 14:38:28 +02:00
|
|
|
void *ConfigCompiler::GetScanner(void) const
|
2012-05-31 09:43:46 +02:00
|
|
|
{
|
|
|
|
return m_Scanner;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the path for the input file.
|
|
|
|
*
|
|
|
|
* @returns The path.
|
|
|
|
*/
|
2014-12-14 11:33:45 +01:00
|
|
|
const char *ConfigCompiler::GetPath(void) const
|
2012-07-08 21:18:35 +02:00
|
|
|
{
|
2014-12-14 11:33:45 +01:00
|
|
|
return m_Path.CStr();
|
2012-07-08 21:18:35 +02:00
|
|
|
}
|
|
|
|
|
2014-05-13 14:40:12 +02:00
|
|
|
void ConfigCompiler::SetZone(const String& zone)
|
|
|
|
{
|
|
|
|
m_Zone = zone;
|
|
|
|
}
|
|
|
|
|
|
|
|
String ConfigCompiler::GetZone(void) const
|
|
|
|
{
|
|
|
|
return m_Zone;
|
|
|
|
}
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
void ConfigCompiler::SetPackage(const String& package)
|
2014-11-15 08:20:22 +01:00
|
|
|
{
|
2015-08-28 17:58:29 +02:00
|
|
|
m_Package = package;
|
2015-08-17 16:08:57 +02:00
|
|
|
}
|
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
String ConfigCompiler::GetPackage(void) const
|
2015-08-17 16:08:57 +02:00
|
|
|
{
|
2015-08-28 17:58:29 +02:00
|
|
|
return m_Package;
|
2015-08-17 16:08:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigCompiler::CollectIncludes(std::vector<Expression *>& expressions,
|
2015-08-28 17:58:29 +02:00
|
|
|
const String& file, const String& zone, const String& package)
|
2015-08-17 16:08:57 +02:00
|
|
|
{
|
2015-08-28 17:58:29 +02:00
|
|
|
expressions.push_back(CompileFile(file, zone, package));
|
2014-11-15 08:20:22 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
2013-11-29 10:39:48 +01:00
|
|
|
* Handles an include directive.
|
2012-09-19 12:32:39 +02:00
|
|
|
*
|
2015-10-22 09:46:31 +02:00
|
|
|
* @param relativeBath The path this include is relative to.
|
|
|
|
* @param path The path from the include directive.
|
2013-02-01 22:44:58 +01:00
|
|
|
* @param search Whether to search global include dirs.
|
2013-02-03 01:41:00 +01:00
|
|
|
* @param debuginfo Debug information.
|
2012-09-19 12:32:39 +02:00
|
|
|
*/
|
2015-10-22 09:46:31 +02:00
|
|
|
Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const String& path,
|
|
|
|
bool search, const String& zone, const String& package, const DebugInfo& debuginfo)
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
2015-10-22 09:46:31 +02:00
|
|
|
String upath;
|
2013-02-04 12:43:16 +01:00
|
|
|
|
2015-10-22 09:46:31 +02:00
|
|
|
if (search || (path.GetLength() > 0 && path[0] == '/'))
|
|
|
|
upath = path;
|
2013-02-04 12:43:16 +01:00
|
|
|
else
|
2015-10-22 09:46:31 +02:00
|
|
|
upath = relativeBase + "/" + path;
|
2013-02-02 14:28:11 +01:00
|
|
|
|
2015-10-22 09:46:31 +02:00
|
|
|
String includePath = upath;
|
2013-11-29 10:39:48 +01:00
|
|
|
|
|
|
|
if (search) {
|
|
|
|
BOOST_FOREACH(const String& dir, m_IncludeSearchDirs) {
|
2015-10-22 09:46:31 +02:00
|
|
|
String spath = dir + "/" + path;
|
2013-11-29 10:39:48 +01:00
|
|
|
|
2014-05-13 14:40:12 +02:00
|
|
|
if (Utility::PathExists(spath)) {
|
2013-11-29 10:39:48 +01:00
|
|
|
includePath = spath;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 08:20:22 +01:00
|
|
|
std::vector<Expression *> expressions;
|
2013-11-29 10:39:48 +01:00
|
|
|
|
2015-10-22 09:46:31 +02:00
|
|
|
if (!Utility::Glob(includePath, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zone, package), GlobFile) && includePath.FindFirstOf("*?") == String::NPos) {
|
2013-11-29 10:39:48 +01:00
|
|
|
std::ostringstream msgbuf;
|
2015-10-22 09:46:31 +02:00
|
|
|
msgbuf << "Include file '" + path + "' does not exist";
|
2015-02-07 21:50:14 +01:00
|
|
|
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debuginfo));
|
2013-11-29 10:39:48 +01:00
|
|
|
}
|
2014-11-15 08:20:22 +01:00
|
|
|
|
2014-12-10 11:30:42 +01:00
|
|
|
DictExpression *expr = new DictExpression(expressions);
|
|
|
|
expr->MakeInline();
|
|
|
|
return expr;
|
2013-11-29 10:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles recursive includes.
|
|
|
|
*
|
2015-10-22 09:46:31 +02:00
|
|
|
* @param relativeBase The path this include is relative to.
|
2014-11-15 08:20:22 +01:00
|
|
|
* @param path The directory path.
|
2013-11-29 10:39:48 +01:00
|
|
|
* @param pattern The file pattern.
|
|
|
|
* @param debuginfo Debug information.
|
|
|
|
*/
|
2015-10-22 09:46:31 +02:00
|
|
|
Expression *ConfigCompiler::HandleIncludeRecursive(const String& relativeBase, const String& path,
|
|
|
|
const String& pattern, const String& zone, const String& package, const DebugInfo&)
|
2013-11-29 10:39:48 +01:00
|
|
|
{
|
2014-11-15 08:20:22 +01:00
|
|
|
String ppath;
|
2013-11-29 10:39:48 +01:00
|
|
|
|
2014-11-15 08:20:22 +01:00
|
|
|
if (path.GetLength() > 0 && path[0] == '/')
|
|
|
|
ppath = path;
|
2013-11-29 10:39:48 +01:00
|
|
|
else
|
2015-10-22 09:46:31 +02:00
|
|
|
ppath = relativeBase + "/" + path;
|
2013-11-29 10:39:48 +01:00
|
|
|
|
2014-11-15 08:20:22 +01:00
|
|
|
std::vector<Expression *> expressions;
|
2015-10-22 09:46:31 +02:00
|
|
|
Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zone, package), GlobFile);
|
2014-11-15 08:20:22 +01:00
|
|
|
return new DictExpression(expressions);
|
2012-06-01 16:49:33 +02:00
|
|
|
}
|
|
|
|
|
2015-10-22 09:46:31 +02:00
|
|
|
void ConfigCompiler::HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector<Expression *>& expressions)
|
2015-07-21 09:32:17 +02:00
|
|
|
{
|
|
|
|
String zoneName = Utility::BaseName(path);
|
|
|
|
|
|
|
|
String ppath;
|
|
|
|
|
|
|
|
if (path.GetLength() > 0 && path[0] == '/')
|
|
|
|
ppath = path;
|
|
|
|
else
|
2015-10-22 09:46:31 +02:00
|
|
|
ppath = relativeBase + "/" + path;
|
2015-07-21 09:32:17 +02:00
|
|
|
|
2015-07-26 17:46:47 +02:00
|
|
|
RegisterZoneDir(tag, ppath, zoneName);
|
2015-07-21 09:32:17 +02:00
|
|
|
|
2015-10-22 09:46:31 +02:00
|
|
|
Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zoneName, package), GlobFile);
|
2015-07-21 09:32:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles zone includes.
|
|
|
|
*
|
2015-10-22 09:46:31 +02:00
|
|
|
* @param relativeBase The path this include is relative to.
|
2015-07-21 09:32:17 +02:00
|
|
|
* @param tag The tag name.
|
|
|
|
* @param path The directory path.
|
|
|
|
* @param pattern The file pattern.
|
|
|
|
* @param debuginfo Debug information.
|
|
|
|
*/
|
2015-10-22 09:46:31 +02:00
|
|
|
Expression *ConfigCompiler::HandleIncludeZones(const String& relativeBase, const String& tag,
|
|
|
|
const String& path, const String& pattern, const String& package, const DebugInfo&)
|
2015-07-21 09:32:17 +02:00
|
|
|
{
|
|
|
|
String ppath;
|
|
|
|
|
|
|
|
if (path.GetLength() > 0 && path[0] == '/')
|
|
|
|
ppath = path;
|
|
|
|
else
|
2015-10-22 09:46:31 +02:00
|
|
|
ppath = relativeBase + "/" + path;
|
2015-07-21 09:32:17 +02:00
|
|
|
|
|
|
|
std::vector<Expression *> expressions;
|
2015-10-22 09:46:31 +02:00
|
|
|
Utility::Glob(ppath + "/*", boost::bind(&ConfigCompiler::HandleIncludeZone, relativeBase, tag, _1, pattern, package, boost::ref(expressions)), GlobDirectory);
|
2015-07-21 09:32:17 +02:00
|
|
|
return new DictExpression(expressions);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Compiles a stream.
|
|
|
|
*
|
|
|
|
* @param path A name identifying the stream.
|
|
|
|
* @param stream The input stream.
|
|
|
|
* @returns Configuration items.
|
|
|
|
*/
|
2015-08-17 16:08:57 +02:00
|
|
|
Expression *ConfigCompiler::CompileStream(const String& path,
|
2015-08-28 17:58:29 +02:00
|
|
|
std::istream *stream, const String& zone, const String& package)
|
2012-06-06 14:38:28 +02:00
|
|
|
{
|
2013-11-19 07:49:41 +01:00
|
|
|
CONTEXT("Compiling configuration stream with name '" + path + "'");
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
stream->exceptions(std::istream::badbit);
|
2013-01-23 09:57:06 +01:00
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
ConfigCompiler ctx(path, stream, zone, package);
|
2014-11-27 08:04:07 +01:00
|
|
|
|
2015-08-24 11:04:26 +02:00
|
|
|
try {
|
|
|
|
return ctx.Compile();
|
|
|
|
} catch (const ScriptError& ex) {
|
2015-10-26 13:04:03 +01:00
|
|
|
return new ThrowExpression(MakeLiteral(ex.what()), ex.IsIncompleteExpression(), ex.GetDebugInfo());
|
2015-08-24 11:04:26 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2015-10-26 13:04:03 +01:00
|
|
|
return new ThrowExpression(MakeLiteral(DiagnosticInformation(ex)), false);
|
2015-03-18 11:12:14 +01:00
|
|
|
}
|
2012-06-06 14:38:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Compiles a file.
|
|
|
|
*
|
|
|
|
* @param path The path.
|
|
|
|
* @returns Configuration items.
|
|
|
|
*/
|
2015-08-26 06:57:24 +02:00
|
|
|
Expression *ConfigCompiler::CompileFile(const String& path, const String& zone,
|
2015-08-28 17:58:29 +02:00
|
|
|
const String& package)
|
2012-06-06 14:38:28 +02:00
|
|
|
{
|
2013-11-19 07:49:41 +01:00
|
|
|
CONTEXT("Compiling configuration file '" + path + "'");
|
|
|
|
|
2015-08-24 11:04:26 +02:00
|
|
|
std::ifstream stream(path.CStr(), std::ifstream::in);
|
2012-07-02 10:29:32 +02:00
|
|
|
|
2015-08-24 11:04:26 +02:00
|
|
|
if (!stream)
|
2014-04-30 00:23:20 +02:00
|
|
|
BOOST_THROW_EXCEPTION(posix_error()
|
|
|
|
<< boost::errinfo_api_function("std::ifstream::open")
|
|
|
|
<< boost::errinfo_errno(errno)
|
|
|
|
<< boost::errinfo_file_name(path));
|
2012-07-02 11:04:20 +02:00
|
|
|
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "ConfigCompiler")
|
|
|
|
<< "Compiling config file: " << path;
|
2012-07-02 10:29:32 +02:00
|
|
|
|
2015-08-28 17:58:29 +02:00
|
|
|
return CompileStream(path, &stream, zone, package);
|
2012-06-06 14:38:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Compiles a snippet of text.
|
|
|
|
*
|
|
|
|
* @param path A name identifying the text.
|
|
|
|
* @param text The text.
|
|
|
|
* @returns Configuration items.
|
|
|
|
*/
|
2015-08-17 16:08:57 +02:00
|
|
|
Expression *ConfigCompiler::CompileText(const String& path, const String& text,
|
2015-08-28 17:58:29 +02:00
|
|
|
const String& zone, const String& package)
|
2012-06-06 14:38:28 +02:00
|
|
|
{
|
2015-08-24 11:04:26 +02:00
|
|
|
std::stringstream stream(text);
|
2015-08-28 17:58:29 +02:00
|
|
|
return CompileStream(path, &stream, zone, package);
|
2013-02-02 00:28:00 +01:00
|
|
|
}
|
|
|
|
|
2013-02-01 22:44:58 +01:00
|
|
|
/**
|
|
|
|
* Adds a directory to the list of include search dirs.
|
|
|
|
*
|
|
|
|
* @param dir The new dir.
|
|
|
|
*/
|
|
|
|
void ConfigCompiler::AddIncludeSearchDir(const String& dir)
|
|
|
|
{
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogInformation, "ConfigCompiler")
|
|
|
|
<< "Adding include search dir: " << dir;
|
2013-02-02 23:22:27 +01:00
|
|
|
|
2013-02-01 22:44:58 +01:00
|
|
|
m_IncludeSearchDirs.push_back(dir);
|
|
|
|
}
|
2014-11-09 13:06:25 +01:00
|
|
|
|
2015-07-21 09:32:17 +02:00
|
|
|
std::vector<ZoneFragment> ConfigCompiler::GetZoneDirs(const String& zone)
|
|
|
|
{
|
2015-08-24 14:14:44 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ZoneDirsMutex);
|
|
|
|
std::map<String, std::vector<ZoneFragment> >::const_iterator it = m_ZoneDirs.find(zone);
|
2015-07-21 09:32:17 +02:00
|
|
|
if (it == m_ZoneDirs.end())
|
|
|
|
return std::vector<ZoneFragment>();
|
|
|
|
else
|
2015-07-26 17:46:47 +02:00
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigCompiler::RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName)
|
|
|
|
{
|
|
|
|
ZoneFragment zf;
|
|
|
|
zf.Tag = tag;
|
|
|
|
zf.Path = ppath;
|
2015-08-24 14:14:44 +02:00
|
|
|
|
|
|
|
boost::mutex::scoped_lock lock(m_ZoneDirsMutex);
|
2015-07-26 17:46:47 +02:00
|
|
|
m_ZoneDirs[zoneName].push_back(zf);
|
2015-07-21 09:32:17 +02:00
|
|
|
}
|
2015-07-26 17:46:47 +02:00
|
|
|
|