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)
116 expressions.push_back(CompileFile(file, zone, package));
120 * Handles an include directive.
122 * @param relativeBath The path this include is relative to.
123 * @param path The path from the include directive.
124 * @param search Whether to search global include dirs.
125 * @param debuginfo Debug information.
127 Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const String& path,
128 bool search, const String& zone, const String& package, const DebugInfo& debuginfo)
132 if (search || (path.GetLength() > 0 && path[0] == '/'))
135 upath = relativeBase + "/" + path;
137 String includePath = upath;
140 BOOST_FOREACH(const String& dir, m_IncludeSearchDirs) {
141 String spath = dir + "/" + path;
143 if (Utility::PathExists(spath)) {
150 std::vector<Expression *> expressions;
152 if (!Utility::Glob(includePath, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zone, package), GlobFile) && includePath.FindFirstOf("*?") == String::NPos) {
153 std::ostringstream msgbuf;
154 msgbuf << "Include file '" + path + "' does not exist";
155 BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debuginfo));
158 DictExpression *expr = new DictExpression(expressions);
164 * Handles recursive includes.
166 * @param relativeBase The path this include is relative to.
167 * @param path The directory path.
168 * @param pattern The file pattern.
169 * @param debuginfo Debug information.
171 Expression *ConfigCompiler::HandleIncludeRecursive(const String& relativeBase, const String& path,
172 const String& pattern, const String& zone, const String& package, const DebugInfo&)
176 if (path.GetLength() > 0 && path[0] == '/')
179 ppath = relativeBase + "/" + path;
181 std::vector<Expression *> expressions;
182 Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zone, package), GlobFile);
183 return new DictExpression(expressions);
186 void ConfigCompiler::HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector<Expression *>& expressions)
188 String zoneName = Utility::BaseName(path);
192 if (path.GetLength() > 0 && path[0] == '/')
195 ppath = relativeBase + "/" + path;
197 RegisterZoneDir(tag, ppath, zoneName);
199 Utility::GlobRecursive(ppath, pattern, boost::bind(&ConfigCompiler::CollectIncludes, boost::ref(expressions), _1, zoneName, package), GlobFile);
203 * Handles zone includes.
205 * @param relativeBase The path this include is relative to.
206 * @param tag The tag name.
207 * @param path The directory path.
208 * @param pattern The file pattern.
209 * @param debuginfo Debug information.
211 Expression *ConfigCompiler::HandleIncludeZones(const String& relativeBase, const String& tag,
212 const String& path, const String& pattern, const String& package, const DebugInfo&)
215 String newRelativeBase = relativeBase;
217 if (path.GetLength() > 0 && path[0] == '/')
220 ppath = relativeBase + "/" + path;
221 newRelativeBase = ".";
224 std::vector<Expression *> expressions;
225 Utility::Glob(ppath + "/*", boost::bind(&ConfigCompiler::HandleIncludeZone, newRelativeBase, tag, _1, pattern, package, boost::ref(expressions)), GlobDirectory);
226 return new DictExpression(expressions);
232 * @param path A name identifying the stream.
233 * @param stream The input stream.
234 * @returns Configuration items.
236 Expression *ConfigCompiler::CompileStream(const String& path,
237 std::istream *stream, const String& zone, const String& package)
239 CONTEXT("Compiling configuration stream with name '" + path + "'");
241 stream->exceptions(std::istream::badbit);
243 ConfigCompiler ctx(path, stream, zone, package);
246 return ctx.Compile();
247 } catch (const ScriptError& ex) {
248 return new ThrowExpression(MakeLiteral(ex.what()), ex.IsIncompleteExpression(), ex.GetDebugInfo());
249 } catch (const std::exception& ex) {
250 return new ThrowExpression(MakeLiteral(DiagnosticInformation(ex)), false);
257 * @param path The path.
258 * @returns Configuration items.
260 Expression *ConfigCompiler::CompileFile(const String& path, const String& zone,
261 const String& package)
263 CONTEXT("Compiling configuration file '" + path + "'");
265 std::ifstream stream(path.CStr(), std::ifstream::in);
268 BOOST_THROW_EXCEPTION(posix_error()
269 << boost::errinfo_api_function("std::ifstream::open")
270 << boost::errinfo_errno(errno)
271 << boost::errinfo_file_name(path));
273 Log(LogInformation, "ConfigCompiler")
274 << "Compiling config file: " << path;
276 return CompileStream(path, &stream, zone, package);
280 * Compiles a snippet of text.
282 * @param path A name identifying the text.
283 * @param text The text.
284 * @returns Configuration items.
286 Expression *ConfigCompiler::CompileText(const String& path, const String& text,
287 const String& zone, const String& package)
289 std::stringstream stream(text);
290 return CompileStream(path, &stream, zone, package);
294 * Adds a directory to the list of include search dirs.
296 * @param dir The new dir.
298 void ConfigCompiler::AddIncludeSearchDir(const String& dir)
300 Log(LogInformation, "ConfigCompiler")
301 << "Adding include search dir: " << dir;
303 m_IncludeSearchDirs.push_back(dir);
306 std::vector<ZoneFragment> ConfigCompiler::GetZoneDirs(const String& zone)
308 boost::mutex::scoped_lock lock(m_ZoneDirsMutex);
309 std::map<String, std::vector<ZoneFragment> >::const_iterator it = m_ZoneDirs.find(zone);
310 if (it == m_ZoneDirs.end())
311 return std::vector<ZoneFragment>();
316 void ConfigCompiler::RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName)
322 boost::mutex::scoped_lock lock(m_ZoneDirsMutex);
323 m_ZoneDirs[zoneName].push_back(zf);