2012-06-06 14:38:28 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-09-07 10:27:31 +02:00
|
|
|
#include "i2-config.h"
|
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;
|
|
|
|
|
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 includeHandler Handler function for #include directives.
|
|
|
|
*/
|
|
|
|
ConfigCompiler::ConfigCompiler(const String& path, istream *input,
|
|
|
|
HandleIncludeFunc includeHandler)
|
2012-07-08 21:18:35 +02:00
|
|
|
: m_Path(path), m_Input(input), m_HandleInclude(includeHandler)
|
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
|
|
|
{
|
|
|
|
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 result from the compiler.
|
|
|
|
*
|
|
|
|
* @returns A list of configuration items.
|
|
|
|
*/
|
2012-07-02 10:29:32 +02:00
|
|
|
vector<ConfigItem::Ptr> ConfigCompiler::GetResult(void) const
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
2012-07-16 22:00:50 +02:00
|
|
|
return m_Result;
|
2012-06-01 16:49:33 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Handles an include directive by calling the include handler callback
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* @param include The path from the include directive.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
void ConfigCompiler::HandleInclude(const String& include)
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
String path = Utility::DirName(GetPath()) + "/" + include;
|
2012-07-08 21:18:35 +02:00
|
|
|
vector<ConfigItem::Ptr> items = m_HandleInclude(path);
|
2012-07-02 10:29:32 +02:00
|
|
|
std::copy(items.begin(), items.end(), back_inserter(m_Result));
|
2012-06-01 16:49:33 +02: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.
|
|
|
|
*/
|
|
|
|
vector<ConfigItem::Ptr> ConfigCompiler::CompileStream(const String& path,
|
|
|
|
istream *stream)
|
2012-06-06 14:38:28 +02:00
|
|
|
{
|
2012-07-08 21:18:35 +02:00
|
|
|
ConfigCompiler ctx(path, stream);
|
2012-06-06 14:38:28 +02:00
|
|
|
ctx.Compile();
|
|
|
|
return ctx.GetResult();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Compiles a file.
|
|
|
|
*
|
|
|
|
* @param path The path.
|
|
|
|
* @returns Configuration items.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
vector<ConfigItem::Ptr> ConfigCompiler::CompileFile(const String& path)
|
2012-06-06 14:38:28 +02:00
|
|
|
{
|
|
|
|
ifstream stream;
|
2012-08-02 09:38:08 +02:00
|
|
|
stream.open(path.CStr(), ifstream::in);
|
2012-07-02 10:29:32 +02:00
|
|
|
|
2012-07-24 13:13:02 +02:00
|
|
|
if (!stream)
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(invalid_argument("Could not open config file: " + path));
|
2012-07-02 11:04:20 +02:00
|
|
|
|
2012-09-07 10:27:31 +02:00
|
|
|
Logger::Write(LogInformation, "config", "Compiling config file: " + path);
|
2012-07-02 10:29:32 +02:00
|
|
|
|
2012-07-08 21:18:35 +02: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.
|
|
|
|
*/
|
|
|
|
vector<ConfigItem::Ptr> ConfigCompiler::CompileText(const String& path,
|
|
|
|
const String& text)
|
2012-06-06 14:38:28 +02:00
|
|
|
{
|
|
|
|
stringstream stream(text);
|
2012-07-08 21:18:35 +02:00
|
|
|
return CompileStream(path, &stream);
|
2012-06-06 14:38:28 +02:00
|
|
|
}
|
2012-07-02 10:29:32 +02:00
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Default include handler. Includes the file and returns a list of
|
|
|
|
* configuration items.
|
|
|
|
*
|
|
|
|
* @param include The path from the include directive.
|
|
|
|
* @returns A list of configuration objects.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
vector<ConfigItem::Ptr> ConfigCompiler::HandleFileInclude(const String& include)
|
2012-07-02 10:29:32 +02:00
|
|
|
{
|
|
|
|
/* TODO: implement wildcard includes */
|
|
|
|
return CompileFile(include);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
/**
|
|
|
|
* Adds an object to the result.
|
|
|
|
*
|
|
|
|
* @param object The configuration item.
|
|
|
|
*/
|
2012-07-02 10:29:32 +02:00
|
|
|
void ConfigCompiler::AddObject(const ConfigItem::Ptr& object)
|
|
|
|
{
|
|
|
|
m_Result.push_back(object);
|
|
|
|
}
|