2012-06-06 14:38:28 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "config/configcompiler.h"
|
|
|
|
#include "config/configitem.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/logger_fwd.h"
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "base/utility.h"
|
2013-11-19 07:49:41 +01:00
|
|
|
#include "base/context.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <boost/foreach.hpp>
|
2012-05-31 09:43:46 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
using std::ifstream;
|
|
|
|
|
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;
|
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.
|
|
|
|
*/
|
2013-11-29 10:39:48 +01:00
|
|
|
ConfigCompiler::ConfigCompiler(const String& path, std::istream *input)
|
|
|
|
: m_Path(path), m_Input(input)
|
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.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String ConfigCompiler::GetPath(void) const
|
2012-07-08 21:18:35 +02:00
|
|
|
{
|
|
|
|
return m_Path;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @param include 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
|
|
|
*/
|
2013-02-03 01:41:00 +01:00
|
|
|
void ConfigCompiler::HandleInclude(const String& include, bool search, const DebugInfo& debuginfo)
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
2013-02-04 12:43:16 +01:00
|
|
|
String path;
|
|
|
|
|
2013-08-30 12:04:24 +02:00
|
|
|
if (search || (include.GetLength() > 0 && include[0] == '/'))
|
2013-02-04 12:43:16 +01:00
|
|
|
path = include;
|
|
|
|
else
|
|
|
|
path = Utility::DirName(GetPath()) + "/" + include;
|
2013-02-02 14:28:11 +01:00
|
|
|
|
2013-11-29 10:39:48 +01:00
|
|
|
String includePath = path;
|
|
|
|
|
|
|
|
if (search) {
|
|
|
|
BOOST_FOREACH(const String& dir, m_IncludeSearchDirs) {
|
|
|
|
String spath = dir + "/" + include;
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
struct stat statbuf;
|
|
|
|
if (lstat(spath.CStr(), &statbuf) >= 0) {
|
|
|
|
#else /* _WIN32 */
|
|
|
|
struct _stat statbuf;
|
|
|
|
if (_stat(spath.CStr(), &statbuf) >= 0) {
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
includePath = spath;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ConfigItem::Ptr> items;
|
|
|
|
|
|
|
|
if (!Utility::Glob(includePath, boost::bind(&ConfigCompiler::CompileFile, _1), GlobFile) && includePath.FindFirstOf("*?") == String::NPos) {
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
msgbuf << "Include file '" + include + "' does not exist: " << debuginfo;
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles recursive includes.
|
|
|
|
*
|
|
|
|
* @param include The directory path.
|
|
|
|
* @param pattern The file pattern.
|
|
|
|
* @param debuginfo Debug information.
|
|
|
|
*/
|
|
|
|
void ConfigCompiler::HandleIncludeRecursive(const String& include, const String& pattern, const DebugInfo& debuginfo)
|
|
|
|
{
|
|
|
|
String path;
|
|
|
|
|
|
|
|
if (include.GetLength() > 0 && include[0] == '/')
|
|
|
|
path = include;
|
|
|
|
else
|
|
|
|
path = Utility::DirName(GetPath()) + "/" + include;
|
|
|
|
|
|
|
|
Utility::GlobRecursive(path, pattern, boost::bind(&ConfigCompiler::CompileFile, _1), GlobFile);
|
2012-06-01 16:49:33 +02:00
|
|
|
}
|
|
|
|
|
2013-01-17 15:05:34 +01:00
|
|
|
/**
|
|
|
|
* Handles the library directive.
|
|
|
|
*
|
|
|
|
* @param library The name of the library.
|
|
|
|
*/
|
|
|
|
void ConfigCompiler::HandleLibrary(const String& library)
|
|
|
|
{
|
2013-03-12 13:45:54 +01:00
|
|
|
(void) Utility::LoadExtensionLibrary(library);
|
2013-01-17 15:05:34 +01:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2013-03-16 21:18:53 +01:00
|
|
|
void ConfigCompiler::CompileStream(const String& path, std::istream *stream)
|
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
|
|
|
|
2012-07-08 21:18:35 +02:00
|
|
|
ConfigCompiler ctx(path, stream);
|
2012-06-06 14:38:28 +02:00
|
|
|
ctx.Compile();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Compiles a file.
|
|
|
|
*
|
|
|
|
* @param path The path.
|
|
|
|
* @returns Configuration items.
|
|
|
|
*/
|
2013-02-05 13:06:42 +01:00
|
|
|
void ConfigCompiler::CompileFile(const String& path)
|
2012-06-06 14:38:28 +02:00
|
|
|
{
|
2013-11-19 07:49:41 +01:00
|
|
|
CONTEXT("Compiling configuration file '" + path + "'");
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ifstream stream;
|
|
|
|
stream.open(path.CStr(), std::ifstream::in);
|
2012-07-02 10:29:32 +02:00
|
|
|
|
2012-07-24 13:13:02 +02:00
|
|
|
if (!stream)
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Could not open config file: " + path));
|
2012-07-02 11:04:20 +02:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "config", "Compiling config file: " + path);
|
2012-07-02 10:29:32 +02:00
|
|
|
|
2013-02-05 13:06:42 +01:00
|
|
|
return CompileStream(path, &stream);
|
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.
|
|
|
|
*/
|
2013-02-05 13:06:42 +01:00
|
|
|
void ConfigCompiler::CompileText(const String& path, const String& text)
|
2012-06-06 14:38:28 +02:00
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
std::stringstream stream(text);
|
2013-02-05 13:06:42 +01:00
|
|
|
return CompileStream(path, &stream);
|
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)
|
|
|
|
{
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "config", "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);
|
|
|
|
}
|