1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://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 boost::mutex ConfigCompiler::m_ZoneDirsMutex;
34 std::map<String, std::vector<ZoneFragment> > ConfigCompiler::m_ZoneDirs;
37 * Constructor for the ConfigCompiler class.
39 * @param path The path of the configuration file (or another name that
40 * identifies the source of the configuration text).
41 * @param input Input stream for the configuration file.
42 * @param zone The zone.
44 ConfigCompiler::ConfigCompiler(const String& path, std::istream *input,
45 const String& zone, const String& package)
46 : m_Path(path), m_Input(input), m_Zone(zone), m_Package(package),
47 m_Eof(false), m_OpenBraces(0)
53 * Destructor for the ConfigCompiler class.
55 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::SetPackage(const String& package)
108 String ConfigCompiler::GetPackage(void) const
113 void ConfigCompiler::CollectIncludes(std::vector<Expression *>& expressions,
114 const String& file, const String& zone, const String& package)
117 Expression *expr = CompileFile(file, zone, package);
118 expressions.push_back(expr);
119 } catch (const std::exception& ex) {
120 Log(LogWarning, "ConfigCompiler")
121 << "Cannot compile file '"
122 << file << "': " << DiagnosticInformation(ex);
127 * Handles an include directive.
129 * @param relativeBath The path this include is relative to.
130 * @param path The path from the include directive.
131 * @param search Whether to search global include dirs.
132 * @param debuginfo Debug information.
134 Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const String& path,
135 bool search, const String& zone, const String& package, const DebugInfo& debuginfo)
139 if (search || (path.GetLength() > 0 && path[0] == '/'))
142 upath = relativeBase + "/" + path;
144 String includePath = upath;
147 BOOST_FOREACH(const String& dir, m_IncludeSearchDirs) {
148 String spath = dir + "/" + path;
150 if (Utility::PathExists(spath)) {
157 std::vector<Expression *> expressions;
159 if (!Utility::Glob(includePath, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zone, package), GlobFile) && includePath.FindFirstOf("*?") == String::NPos) {
160 std::ostringstream msgbuf;
161 msgbuf << "Include file '" + path + "' does not exist";
162 BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debuginfo));
165 DictExpression *expr = new DictExpression(expressions);
171 * Handles recursive includes.
173 * @param relativeBase The path this include is relative to.
174 * @param path The directory path.
175 * @param pattern The file pattern.
176 * @param debuginfo Debug information.
178 Expression *ConfigCompiler::HandleIncludeRecursive(const String& relativeBase, const String& path,
179 const String& pattern, const String& zone, const String& package, const DebugInfo&)
183 if (path.GetLength() > 0 && path[0] == '/')
186 ppath = relativeBase + "/" + path;
188 std::vector<Expression *> expressions;
189 Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zone, package), GlobFile);
191 DictExpression *dict = new DictExpression(expressions);
196 void ConfigCompiler::HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector<Expression *>& expressions)
198 String zoneName = Utility::BaseName(path);
202 if (path.GetLength() > 0 && path[0] == '/')
205 ppath = relativeBase + "/" + path;
207 RegisterZoneDir(tag, ppath, zoneName);
209 Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zoneName, package), GlobFile);
213 * Handles zone includes.
215 * @param relativeBase The path this include is relative to.
216 * @param tag The tag name.
217 * @param path The directory path.
218 * @param pattern The file pattern.
219 * @param debuginfo Debug information.
221 Expression *ConfigCompiler::HandleIncludeZones(const String& relativeBase, const String& tag,
222 const String& path, const String& pattern, const String& package, const DebugInfo&)
225 String newRelativeBase = relativeBase;
227 if (path.GetLength() > 0 && path[0] == '/')
230 ppath = relativeBase + "/" + path;
231 newRelativeBase = ".";
234 std::vector<Expression *> expressions;
235 Utility::Glob(ppath + "/*", boost::bind(&ConfigCompiler::HandleIncludeZone, newRelativeBase, tag, _1, pattern, package, boost::ref(expressions)), GlobDirectory);
236 return new DictExpression(expressions);
242 * @param path A name identifying the stream.
243 * @param stream The input stream.
244 * @returns Configuration items.
246 Expression *ConfigCompiler::CompileStream(const String& path,
247 std::istream *stream, const String& zone, const String& package)
249 CONTEXT("Compiling configuration stream with name '" + path + "'");
251 stream->exceptions(std::istream::badbit);
253 ConfigCompiler ctx(path, stream, zone, package);
256 return ctx.Compile();
257 } catch (const ScriptError& ex) {
258 return new ThrowExpression(MakeLiteral(ex.what()), ex.IsIncompleteExpression(), ex.GetDebugInfo());
259 } catch (const std::exception& ex) {
260 return new ThrowExpression(MakeLiteral(DiagnosticInformation(ex)), false);
267 * @param path The path.
268 * @returns Configuration items.
270 Expression *ConfigCompiler::CompileFile(const String& path, const String& zone,
271 const String& package)
273 CONTEXT("Compiling configuration file '" + path + "'");
275 std::ifstream stream(path.CStr(), std::ifstream::in);
278 BOOST_THROW_EXCEPTION(posix_error()
279 << boost::errinfo_api_function("std::ifstream::open")
280 << boost::errinfo_errno(errno)
281 << boost::errinfo_file_name(path));
283 Log(LogInformation, "ConfigCompiler")
284 << "Compiling config file: " << path;
286 return CompileStream(path, &stream, zone, package);
290 * Compiles a snippet of text.
292 * @param path A name identifying the text.
293 * @param text The text.
294 * @returns Configuration items.
296 Expression *ConfigCompiler::CompileText(const String& path, const String& text,
297 const String& zone, const String& package)
299 std::stringstream stream(text);
300 return CompileStream(path, &stream, zone, package);
304 * Adds a directory to the list of include search dirs.
306 * @param dir The new dir.
308 void ConfigCompiler::AddIncludeSearchDir(const String& dir)
310 Log(LogInformation, "ConfigCompiler")
311 << "Adding include search dir: " << dir;
313 m_IncludeSearchDirs.push_back(dir);
316 std::vector<ZoneFragment> ConfigCompiler::GetZoneDirs(const String& zone)
318 boost::mutex::scoped_lock lock(m_ZoneDirsMutex);
319 std::map<String, std::vector<ZoneFragment> >::const_iterator it = m_ZoneDirs.find(zone);
320 if (it == m_ZoneDirs.end())
321 return std::vector<ZoneFragment>();
326 void ConfigCompiler::RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName)
332 boost::mutex::scoped_lock lock(m_ZoneDirsMutex);
333 m_ZoneDirs[zoneName].push_back(zf);
336 bool ConfigCompiler::HasZoneConfigAuthority(const String& zoneName)
338 std::vector<ZoneFragment> zoneDirs = m_ZoneDirs[zoneName];
340 bool empty = zoneDirs.empty();
343 std::vector<String> paths;
344 BOOST_FOREACH(const ZoneFragment& zf, zoneDirs) {
345 paths.push_back(zf.Path);
348 Log(LogNotice, "ConfigCompiler")
349 << "Registered authoritative config directories for zone '" << zoneName << "': " << Utility::NaturalJoin(paths);