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-05-31 09:43:46 +02:00
|
|
|
#include "i2-dyn.h"
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
using std::ifstream;
|
|
|
|
|
2012-05-31 09:43:46 +02:00
|
|
|
using namespace icinga;
|
|
|
|
|
2012-07-08 21:18:35 +02:00
|
|
|
ConfigCompiler::ConfigCompiler(const string& path, istream *input, HandleIncludeFunc includeHandler)
|
|
|
|
: m_Path(path), m_Input(input), m_HandleInclude(includeHandler)
|
2012-05-31 09:43:46 +02:00
|
|
|
{
|
|
|
|
InitializeScanner();
|
|
|
|
}
|
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
ConfigCompiler::~ConfigCompiler(void)
|
2012-05-31 09:43:46 +02:00
|
|
|
{
|
|
|
|
DestroyScanner();
|
|
|
|
}
|
|
|
|
|
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-06-06 14:38:28 +02:00
|
|
|
void *ConfigCompiler::GetScanner(void) const
|
2012-05-31 09:43:46 +02:00
|
|
|
{
|
|
|
|
return m_Scanner;
|
|
|
|
}
|
|
|
|
|
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-02 10:29:32 +02:00
|
|
|
return m_Result;
|
2012-06-01 16:49:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-08 21:18:35 +02:00
|
|
|
string ConfigCompiler::GetPath(void) const
|
|
|
|
{
|
|
|
|
return m_Path;
|
|
|
|
}
|
|
|
|
|
2012-07-02 10:29:32 +02:00
|
|
|
void ConfigCompiler::HandleInclude(const string& include)
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
2012-07-08 21:18:35 +02:00
|
|
|
string path = Utility::DirName(GetPath()) + "/" + include;
|
|
|
|
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-07-08 21:18:35 +02:00
|
|
|
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-07-08 21:18:35 +02:00
|
|
|
vector<ConfigItem::Ptr> ConfigCompiler::CompileFile(const string& path)
|
2012-06-06 14:38:28 +02:00
|
|
|
{
|
|
|
|
ifstream stream;
|
2012-06-12 10:49:30 +02:00
|
|
|
stream.exceptions(ifstream::badbit);
|
2012-07-08 21:18:35 +02:00
|
|
|
stream.open(path.c_str(), ifstream::in);
|
2012-07-02 10:29:32 +02:00
|
|
|
|
2012-07-02 11:04:20 +02:00
|
|
|
if (!stream.good())
|
2012-07-08 21:18:35 +02:00
|
|
|
throw invalid_argument("Could not open config file: " + path);
|
2012-07-02 11:04:20 +02:00
|
|
|
|
2012-07-08 21:18:35 +02:00
|
|
|
Application::Log(LogInformation, "dyn", "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-07-08 21:18:35 +02:00
|
|
|
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
|
|
|
|
|
|
|
vector<ConfigItem::Ptr> ConfigCompiler::HandleFileInclude(const string& include)
|
|
|
|
{
|
|
|
|
/* TODO: implement wildcard includes */
|
|
|
|
return CompileFile(include);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigCompiler::AddObject(const ConfigItem::Ptr& object)
|
|
|
|
{
|
|
|
|
m_Result.push_back(object);
|
|
|
|
}
|