1 /******************************************************************************
3 * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
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"
29 using namespace icinga;
31 std::vector<String> ConfigCompiler::m_IncludeSearchDirs;
32 boost::mutex ConfigCompiler::m_ZoneDirsMutex;
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& package)
45 : m_Path(path), m_Input(input), m_Zone(zone), m_Package(package),
46 m_Eof(false), m_OpenBraces(0)
52 * Destructor for the ConfigCompiler class.
54 ConfigCompiler::~ConfigCompiler(void)
60 * Reads data from the input stream. Used internally by the lexer.
62 * @param buffer Where to store data.
63 * @param max_size The maximum number of bytes to read from the stream.
64 * @returns The actual number of bytes read.
66 size_t ConfigCompiler::ReadInput(char *buffer, size_t max_size)
68 m_Input->read(buffer, max_size);
69 return static_cast<size_t>(m_Input->gcount());
73 * Retrieves the scanner object.
75 * @returns The scanner object.
77 void *ConfigCompiler::GetScanner(void) const
83 * Retrieves the path for the input file.
87 const char *ConfigCompiler::GetPath(void) const
92 void ConfigCompiler::SetZone(const String& zone)
97 String ConfigCompiler::GetZone(void) const
102 void ConfigCompiler::SetPackage(const String& package)
107 String ConfigCompiler::GetPackage(void) const
112 void ConfigCompiler::CollectIncludes(std::vector<Expression *>& expressions,
113 const String& file, const String& zone, const String& package)
116 Expression *expr = CompileFile(file, zone, package);
117 expressions.push_back(expr);
118 } catch (const std::exception& ex) {
119 Log(LogWarning, "ConfigCompiler")
120 << "Cannot compile file '"
121 << file << "': " << DiagnosticInformation(ex);
126 * Handles an include directive.
128 * @param relativeBath The path this include is relative to.
129 * @param path The path from the include directive.
130 * @param search Whether to search global include dirs.
131 * @param debuginfo Debug information.
133 Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const String& path,
134 bool search, const String& zone, const String& package, const DebugInfo& debuginfo)
138 if (search || (IsAbsolutePath(path)))
141 upath = relativeBase + "/" + path;
143 String includePath = upath;
146 for (const String& dir : m_IncludeSearchDirs) {
147 String spath = dir + "/" + path;
149 if (Utility::PathExists(spath)) {
156 std::vector<Expression *> expressions;
158 if (!Utility::Glob(includePath, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zone, package), GlobFile) && includePath.FindFirstOf("*?") == String::NPos) {
159 std::ostringstream msgbuf;
160 msgbuf << "Include file '" + path + "' does not exist";
161 BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debuginfo));
164 DictExpression *expr = new DictExpression(expressions);
170 * Handles recursive includes.
172 * @param relativeBase The path this include is relative to.
173 * @param path The directory path.
174 * @param pattern The file pattern.
175 * @param debuginfo Debug information.
177 Expression *ConfigCompiler::HandleIncludeRecursive(const String& relativeBase, const String& path,
178 const String& pattern, const String& zone, const String& package, const DebugInfo&)
182 if (IsAbsolutePath(path))
185 ppath = relativeBase + "/" + path;
187 std::vector<Expression *> expressions;
188 Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zone, package), GlobFile);
190 DictExpression *dict = new DictExpression(expressions);
195 void ConfigCompiler::HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector<Expression *>& expressions)
197 String zoneName = Utility::BaseName(path);
201 if (IsAbsolutePath(path))
204 ppath = relativeBase + "/" + path;
206 RegisterZoneDir(tag, ppath, zoneName);
208 Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zoneName, package), GlobFile);
212 * Handles zone includes.
214 * @param relativeBase The path this include is relative to.
215 * @param tag The tag name.
216 * @param path The directory path.
217 * @param pattern The file pattern.
218 * @param debuginfo Debug information.
220 Expression *ConfigCompiler::HandleIncludeZones(const String& relativeBase, const String& tag,
221 const String& path, const String& pattern, const String& package, const DebugInfo&)
224 String newRelativeBase = relativeBase;
226 if (IsAbsolutePath(path))
229 ppath = relativeBase + "/" + path;
230 newRelativeBase = ".";
233 std::vector<Expression *> expressions;
234 Utility::Glob(ppath + "/*", boost::bind(&ConfigCompiler::HandleIncludeZone, newRelativeBase, tag, _1, pattern, package, boost::ref(expressions)), GlobDirectory);
235 return new DictExpression(expressions);
241 * @param path A name identifying the stream.
242 * @param stream The input stream.
243 * @returns Configuration items.
245 Expression *ConfigCompiler::CompileStream(const String& path,
246 std::istream *stream, const String& zone, const String& package)
248 CONTEXT("Compiling configuration stream with name '" + path + "'");
250 stream->exceptions(std::istream::badbit);
252 ConfigCompiler ctx(path, stream, zone, package);
255 return ctx.Compile();
256 } catch (const ScriptError& ex) {
257 return new ThrowExpression(MakeLiteral(ex.what()), ex.IsIncompleteExpression(), ex.GetDebugInfo());
258 } catch (const std::exception& ex) {
259 return new ThrowExpression(MakeLiteral(DiagnosticInformation(ex)), false);
266 * @param path The path.
267 * @returns Configuration items.
269 Expression *ConfigCompiler::CompileFile(const String& path, const String& zone,
270 const String& package)
272 CONTEXT("Compiling configuration file '" + path + "'");
274 std::ifstream stream(path.CStr(), std::ifstream::in);
277 BOOST_THROW_EXCEPTION(posix_error()
278 << boost::errinfo_api_function("std::ifstream::open")
279 << boost::errinfo_errno(errno)
280 << boost::errinfo_file_name(path));
282 Log(LogNotice, "ConfigCompiler")
283 << "Compiling config file: " << path;
285 return CompileStream(path, &stream, zone, package);
289 * Compiles a snippet of text.
291 * @param path A name identifying the text.
292 * @param text The text.
293 * @returns Configuration items.
295 Expression *ConfigCompiler::CompileText(const String& path, const String& text,
296 const String& zone, const String& package)
298 std::stringstream stream(text);
299 return CompileStream(path, &stream, zone, package);
303 * Adds a directory to the list of include search dirs.
305 * @param dir The new dir.
307 void ConfigCompiler::AddIncludeSearchDir(const String& dir)
309 Log(LogInformation, "ConfigCompiler")
310 << "Adding include search dir: " << dir;
312 m_IncludeSearchDirs.push_back(dir);
315 std::vector<ZoneFragment> ConfigCompiler::GetZoneDirs(const String& zone)
317 boost::mutex::scoped_lock lock(m_ZoneDirsMutex);
318 auto it = m_ZoneDirs.find(zone);
319 if (it == m_ZoneDirs.end())
320 return std::vector<ZoneFragment>();
325 void ConfigCompiler::RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName)
331 boost::mutex::scoped_lock lock(m_ZoneDirsMutex);
332 m_ZoneDirs[zoneName].push_back(zf);
335 bool ConfigCompiler::HasZoneConfigAuthority(const String& zoneName)
337 std::vector<ZoneFragment> zoneDirs = m_ZoneDirs[zoneName];
339 bool empty = zoneDirs.empty();
342 std::vector<String> paths;
343 for (const ZoneFragment& zf : zoneDirs) {
344 paths.push_back(zf.Path);
347 Log(LogNotice, "ConfigCompiler")
348 << "Registered authoritative config directories for zone '" << zoneName << "': " << Utility::NaturalJoin(paths);
355 bool ConfigCompiler::IsAbsolutePath(const String& path)
358 return (path.GetLength() > 0 && path[0] == '/');
360 return !PathIsRelative(path.CStr());