1 /******************************************************************************
3 * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "config/configcompiler.hpp"
21 #include "config/configitem.hpp"
22 #include "base/logger.hpp"
23 #include "base/utility.hpp"
24 #include "base/loader.hpp"
25 #include "base/context.hpp"
26 #include "base/exception.hpp"
28 #include <boost/foreach.hpp>
30 using namespace icinga;
32 std::vector<String> ConfigCompiler::m_IncludeSearchDirs;
33 std::map<String, std::vector<ZoneFragment> > ConfigCompiler::m_ZoneDirs;
36 * Constructor for the ConfigCompiler class.
38 * @param path The path of the configuration file (or another name that
39 * identifies the source of the configuration text).
40 * @param input Input stream for the configuration file.
41 * @param zone The zone.
43 ConfigCompiler::ConfigCompiler(const String& path, std::istream *input,
44 const String& zone, const String& module)
45 : m_Path(path), m_Input(input), m_Zone(zone), m_Module(module),
46 m_Eof(false), m_OpenBraces(0), m_IgnoreNewlines(0)
52 * Destructor for the ConfigCompiler class.
54 ConfigCompiler::~ConfigCompiler(void)
61 * Reads data from the input stream. Used internally by the lexer.
63 * @param buffer Where to store data.
64 * @param max_size The maximum number of bytes to read from the stream.
65 * @returns The actual number of bytes read.
67 size_t ConfigCompiler::ReadInput(char *buffer, size_t max_size)
69 m_Input->read(buffer, max_size);
70 return static_cast<size_t>(m_Input->gcount());
74 * Retrieves the scanner object.
76 * @returns The scanner object.
78 void *ConfigCompiler::GetScanner(void) const
84 * Retrieves the path for the input file.
88 const char *ConfigCompiler::GetPath(void) const
93 void ConfigCompiler::SetZone(const String& zone)
98 String ConfigCompiler::GetZone(void) const
103 void ConfigCompiler::SetModule(const String& module)
108 String ConfigCompiler::GetModule(void) const
113 void ConfigCompiler::CollectIncludes(std::vector<Expression *>& expressions,
114 const String& file, const String& zone, const String& module)
116 expressions.push_back(CompileFile(file, true, zone, module));
120 * Handles an include directive.
122 * @param include The path from the include directive.
123 * @param search Whether to search global include dirs.
124 * @param debuginfo Debug information.
126 Expression *ConfigCompiler::HandleInclude(const String& include, bool search, const DebugInfo& debuginfo)
130 if (search || (include.GetLength() > 0 && include[0] == '/'))
133 path = Utility::DirName(GetPath()) + "/" + include;
135 String includePath = path;
138 BOOST_FOREACH(const String& dir, m_IncludeSearchDirs) {
139 String spath = dir + "/" + include;
141 if (Utility::PathExists(spath)) {
148 std::vector<Expression *> expressions;
150 if (!Utility::Glob(includePath, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, m_Zone, m_Module), GlobFile) && includePath.FindFirstOf("*?") == String::NPos) {
151 std::ostringstream msgbuf;
152 msgbuf << "Include file '" + include + "' does not exist";
153 BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debuginfo));
156 DictExpression *expr = new DictExpression(expressions);
162 * Handles recursive includes.
164 * @param path The directory path.
165 * @param pattern The file pattern.
166 * @param debuginfo Debug information.
168 Expression *ConfigCompiler::HandleIncludeRecursive(const String& path, const String& pattern, const DebugInfo&)
172 if (path.GetLength() > 0 && path[0] == '/')
175 ppath = Utility::DirName(GetPath()) + "/" + path;
177 std::vector<Expression *> expressions;
178 Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, m_Zone, m_Module), GlobFile);
179 return new DictExpression(expressions);
182 void ConfigCompiler::HandleIncludeZone(const String& tag, const String& path, const String& pattern, std::vector<Expression *>& expressions)
184 String zoneName = Utility::BaseName(path);
188 if (path.GetLength() > 0 && path[0] == '/')
191 ppath = Utility::DirName(GetPath()) + "/" + path;
193 RegisterZoneDir(tag, ppath, zoneName);
195 Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zoneName, m_Module), GlobFile);
199 * Handles zone includes.
201 * @param tag The tag name.
202 * @param path The directory path.
203 * @param pattern The file pattern.
204 * @param debuginfo Debug information.
206 Expression *ConfigCompiler::HandleIncludeZones(const String& tag, const String& path, const String& pattern, const DebugInfo&)
210 if (path.GetLength() > 0 && path[0] == '/')
213 ppath = Utility::DirName(GetPath()) + "/" + path;
215 std::vector<Expression *> expressions;
216 Utility::Glob(ppath + "/*", boost::bind(&ConfigCompiler::HandleIncludeZone, this, tag, _1, pattern, boost::ref(expressions)), GlobDirectory);
217 return new DictExpression(expressions);
221 * Handles the library directive.
223 * @param library The name of the library.
225 void ConfigCompiler::HandleLibrary(const String& library)
227 Loader::LoadExtensionLibrary(library);
230 void ConfigCompiler::CompileHelper(void)
233 m_Promise.set_value(boost::shared_ptr<Expression>(Compile()));
235 m_Promise.set_exception(boost::current_exception());
244 * @param path A name identifying the stream.
245 * @param stream The input stream.
246 * @returns Configuration items.
248 Expression *ConfigCompiler::CompileStream(const String& path,
249 std::istream *stream, bool async, const String& zone, const String& module)
251 CONTEXT("Compiling configuration stream with name '" + path + "'");
253 stream->exceptions(std::istream::badbit);
255 ConfigCompiler* ctx = new ConfigCompiler(path, stream, zone, module);
258 boost::shared_future<boost::shared_ptr<Expression> > ftr = boost::shared_future<boost::shared_ptr<Expression> >(ctx->m_Promise.get_future());
260 Utility::QueueAsyncCallback(boost::bind(&ConfigCompiler::CompileHelper, ctx));
261 return new FutureExpression(ftr);
266 expr = ctx->Compile();
280 * @param path The path.
281 * @returns Configuration items.
283 Expression *ConfigCompiler::CompileFile(const String& path, bool async,
284 const String& zone, const String& module)
286 CONTEXT("Compiling configuration file '" + path + "'");
288 std::ifstream *stream = new std::ifstream();
289 stream->open(path.CStr(), std::ifstream::in);
292 BOOST_THROW_EXCEPTION(posix_error()
293 << boost::errinfo_api_function("std::ifstream::open")
294 << boost::errinfo_errno(errno)
295 << boost::errinfo_file_name(path));
297 Log(LogInformation, "ConfigCompiler")
298 << "Compiling config file: " << path;
300 return CompileStream(path, stream, async, zone, module);
304 * Compiles a snippet of text.
306 * @param path A name identifying the text.
307 * @param text The text.
308 * @returns Configuration items.
310 Expression *ConfigCompiler::CompileText(const String& path, const String& text,
311 bool async, const String& zone, const String& module)
313 std::stringstream *stream = new std::stringstream(text);
314 return CompileStream(path, stream, async, zone, module);
318 * Adds a directory to the list of include search dirs.
320 * @param dir The new dir.
322 void ConfigCompiler::AddIncludeSearchDir(const String& dir)
324 Log(LogInformation, "ConfigCompiler")
325 << "Adding include search dir: " << dir;
327 m_IncludeSearchDirs.push_back(dir);
330 std::vector<ZoneFragment> ConfigCompiler::GetZoneDirs(const String& zone)
332 std::map<String, std::vector<ZoneFragment> >::const_iterator it;
333 it = m_ZoneDirs.find(zone);
334 if (it == m_ZoneDirs.end())
335 return std::vector<ZoneFragment>();
340 void ConfigCompiler::RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName)
345 m_ZoneDirs[zoneName].push_back(zf);
348 const std::vector<String>& ConfigCompiler::GetKeywords(void)
350 static std::vector<String> keywords;
352 if (keywords.empty()) {
353 keywords.push_back("object");
354 keywords.push_back("template");
355 keywords.push_back("include");
356 keywords.push_back("include_recursive");
357 keywords.push_back("library");
358 keywords.push_back("null");
359 keywords.push_back("true");
360 keywords.push_back("false");
361 keywords.push_back("const");
362 keywords.push_back("var");
363 keywords.push_back("this");
364 keywords.push_back("globals");
365 keywords.push_back("locals");
366 keywords.push_back("use");
367 keywords.push_back("apply");
368 keywords.push_back("to");
369 keywords.push_back("where");
370 keywords.push_back("import");
371 keywords.push_back("assign");
372 keywords.push_back("ignore");
373 keywords.push_back("function");
374 keywords.push_back("return");
375 keywords.push_back("break");
376 keywords.push_back("continue");
377 keywords.push_back("for");
378 keywords.push_back("if");
379 keywords.push_back("else");
380 keywords.push_back("while");