icinga2/lib/config/configcompiler.cpp

369 lines
10 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2015-01-22 12:00:23 +01:00
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
* *
* 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"
#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;
std::map<String, std::vector<ZoneFragment> > ConfigCompiler::m_ZoneDirs;
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.
* @param zone The zone.
2012-09-19 12:32:39 +02:00
*/
ConfigCompiler::ConfigCompiler(const String& path, std::istream *input, const String& zone)
2015-02-11 15:58:07 +01:00
: m_Path(path), m_Input(input), m_Zone(zone), m_Eof(false), m_OpenBraces(0), m_IgnoreNewlines(0)
2012-05-31 09:43:46 +02:00
{
InitializeScanner();
}
2012-09-19 12:32:39 +02:00
/**
* Destructor for the ConfigCompiler class.
*/
ConfigCompiler::~ConfigCompiler(void)
2012-05-31 09:43:46 +02:00
{
DestroyScanner();
delete m_Input;
2012-05-31 09:43:46 +02:00
}
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.
*/
size_t ConfigCompiler::ReadInput(char *buffer, size_t max_size)
2012-05-31 09:43:46 +02: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.
*/
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.
*/
const char *ConfigCompiler::GetPath(void) const
{
return m_Path.CStr();
}
void ConfigCompiler::SetZone(const String& zone)
{
m_Zone = zone;
}
String ConfigCompiler::GetZone(void) const
{
return m_Zone;
}
void ConfigCompiler::CollectIncludes(std::vector<Expression *>& expressions, const String& file, const String& zone)
{
expressions.push_back(CompileFile(file, true, zone));
}
2012-09-19 12:32:39 +02:00
/**
* Handles an include directive.
2012-09-19 12:32:39 +02:00
*
* @param include The path from the include directive.
* @param search Whether to search global include dirs.
* @param debuginfo Debug information.
2012-09-19 12:32:39 +02:00
*/
Expression *ConfigCompiler::HandleInclude(const String& include, bool search, const DebugInfo& debuginfo)
{
2013-02-04 12:43:16 +01:00
String path;
if (search || (include.GetLength() > 0 && include[0] == '/'))
2013-02-04 12:43:16 +01:00
path = include;
else
path = Utility::DirName(GetPath()) + "/" + include;
String includePath = path;
if (search) {
BOOST_FOREACH(const String& dir, m_IncludeSearchDirs) {
String spath = dir + "/" + include;
if (Utility::PathExists(spath)) {
includePath = spath;
break;
}
}
}
std::vector<Expression *> expressions;
if (!Utility::Glob(includePath, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, m_Zone), GlobFile) && includePath.FindFirstOf("*?") == String::NPos) {
std::ostringstream msgbuf;
msgbuf << "Include file '" + include + "' does not exist";
BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debuginfo));
}
DictExpression *expr = new DictExpression(expressions);
expr->MakeInline();
return expr;
}
/**
* Handles recursive includes.
*
* @param path The directory path.
* @param pattern The file pattern.
* @param debuginfo Debug information.
*/
Expression *ConfigCompiler::HandleIncludeRecursive(const String& path, const String& pattern, const DebugInfo&)
{
String ppath;
if (path.GetLength() > 0 && path[0] == '/')
ppath = path;
else
ppath = Utility::DirName(GetPath()) + "/" + path;
std::vector<Expression *> expressions;
Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, m_Zone), GlobFile);
return new DictExpression(expressions);
}
void ConfigCompiler::HandleIncludeZone(const String& tag, const String& path, const String& pattern, std::vector<Expression *>& expressions)
{
String zoneName = Utility::BaseName(path);
String ppath;
if (path.GetLength() > 0 && path[0] == '/')
ppath = path;
else
ppath = Utility::DirName(GetPath()) + "/" + path;
RegisterZoneDir(tag, ppath, zoneName);
Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zoneName), GlobFile);
}
/**
* Handles zone includes.
*
* @param tag The tag name.
* @param path The directory path.
* @param pattern The file pattern.
* @param debuginfo Debug information.
*/
Expression *ConfigCompiler::HandleIncludeZones(const String& tag, const String& path, const String& pattern, const DebugInfo&)
{
String ppath;
if (path.GetLength() > 0 && path[0] == '/')
ppath = path;
else
ppath = Utility::DirName(GetPath()) + "/" + path;
std::vector<Expression *> expressions;
Utility::Glob(ppath + "/*", boost::bind(&ConfigCompiler::HandleIncludeZone, this, tag, _1, pattern, boost::ref(expressions)), GlobDirectory);
return new DictExpression(expressions);
}
/**
* Handles the library directive.
*
* @param library The name of the library.
*/
void ConfigCompiler::HandleLibrary(const String& library)
{
Loader::LoadExtensionLibrary(library);
}
void ConfigCompiler::CompileHelper(void)
{
try {
m_Promise.set_value(boost::shared_ptr<Expression>(Compile()));
} catch (...) {
m_Promise.set_exception(boost::current_exception());
}
delete this;
}
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.
*/
Expression *ConfigCompiler::CompileStream(const String& path, std::istream *stream, bool async, const String& zone)
{
CONTEXT("Compiling configuration stream with name '" + path + "'");
2013-03-16 21:18:53 +01:00
stream->exceptions(std::istream::badbit);
ConfigCompiler* ctx = new ConfigCompiler(path, stream, zone);
if (async) {
boost::shared_future<boost::shared_ptr<Expression> > ftr = boost::shared_future<boost::shared_ptr<Expression> >(ctx->m_Promise.get_future());
Utility::QueueAsyncCallback(boost::bind(&ConfigCompiler::CompileHelper, ctx));
return new FutureExpression(ftr);
} else {
Expression *expr;
try {
expr = ctx->Compile();
} catch (...) {
delete ctx;
throw;
}
delete ctx;
return expr;
}
}
2012-09-19 12:32:39 +02:00
/**
* Compiles a file.
*
* @param path The path.
* @returns Configuration items.
*/
Expression *ConfigCompiler::CompileFile(const String& path, bool async, const String& zone)
{
CONTEXT("Compiling configuration file '" + path + "'");
std::ifstream *stream = new std::ifstream();
stream->open(path.CStr(), std::ifstream::in);
if (!*stream)
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("std::ifstream::open")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(path));
2014-10-19 17:52:17 +02:00
Log(LogInformation, "ConfigCompiler")
<< "Compiling config file: " << path;
return CompileStream(path, stream, async, zone);
}
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.
*/
Expression *ConfigCompiler::CompileText(const String& path, const String& text, bool async, const String& zone)
{
std::stringstream *stream = new std::stringstream(text);
return CompileStream(path, stream, async, zone);
}
/**
* 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;
m_IncludeSearchDirs.push_back(dir);
}
std::vector<ZoneFragment> ConfigCompiler::GetZoneDirs(const String& zone)
{
std::map<String, std::vector<ZoneFragment> >::const_iterator it;
it = m_ZoneDirs.find(zone);
if (it == m_ZoneDirs.end())
return std::vector<ZoneFragment>();
else
return it->second;
}
void ConfigCompiler::RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName)
{
ZoneFragment zf;
zf.Tag = tag;
zf.Path = ppath;
m_ZoneDirs[zoneName].push_back(zf);
}
const std::vector<String>& ConfigCompiler::GetKeywords(void)
{
static std::vector<String> keywords;
if (keywords.empty()) {
keywords.push_back("object");
keywords.push_back("template");
keywords.push_back("include");
keywords.push_back("include_recursive");
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");
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");
}
return keywords;
}