1 /******************************************************************************
3 * Copyright (C) 2012-2018 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 #ifndef CONFIGCOMPILER_H
21 #define CONFIGCOMPILER_H
23 #include "config/i2-config.hpp"
24 #include "config/expression.hpp"
25 #include "base/debuginfo.hpp"
26 #include "base/registry.hpp"
27 #include "base/initialize.hpp"
28 #include "base/singleton.hpp"
33 typedef union YYSTYPE YYSTYPE;
34 typedef void *yyscan_t;
39 struct CompilerDebugInfo
49 operator DebugInfo(void) const
53 di.FirstLine = FirstLine;
54 di.FirstColumn = FirstColumn;
55 di.LastLine = LastLine;
56 di.LastColumn = LastColumn;
64 CompilerDebugInfo DebugInfo;
69 FlowControlReturn = 1,
70 FlowControlContinue = 2,
81 * The configuration compiler can be used to compile a configuration file
82 * into a number of configuration items.
86 class I2_CONFIG_API ConfigCompiler
89 explicit ConfigCompiler(const String& path, std::istream *input,
90 const String& zone = String(), const String& package = String());
91 virtual ~ConfigCompiler(void);
93 std::unique_ptr<Expression> Compile(void);
95 static std::unique_ptr<Expression>CompileStream(const String& path, std::istream *stream,
96 const String& zone = String(), const String& package = String());
97 static std::unique_ptr<Expression>CompileFile(const String& path, const String& zone = String(),
98 const String& package = String());
99 static std::unique_ptr<Expression>CompileText(const String& path, const String& text,
100 const String& zone = String(), const String& package = String());
102 static void AddIncludeSearchDir(const String& dir);
104 const char *GetPath(void) const;
106 void SetZone(const String& zone);
107 String GetZone(void) const;
109 void SetPackage(const String& package);
110 String GetPackage(void) const;
112 static void CollectIncludes(std::vector<std::unique_ptr<Expression> >& expressions,
113 const String& file, const String& zone, const String& package);
115 static std::unique_ptr<Expression> HandleInclude(const String& relativeBase, const String& path, bool search,
116 const String& zone, const String& package, const DebugInfo& debuginfo = DebugInfo());
117 static std::unique_ptr<Expression> HandleIncludeRecursive(const String& relativeBase, const String& path,
118 const String& pattern, const String& zone, const String& package, const DebugInfo& debuginfo = DebugInfo());
119 static std::unique_ptr<Expression> HandleIncludeZones(const String& relativeBase, const String& tag,
120 const String& path, const String& pattern, const String& package, const DebugInfo& debuginfo = DebugInfo());
122 size_t ReadInput(char *buffer, size_t max_bytes);
123 void *GetScanner(void) const;
125 static std::vector<ZoneFragment> GetZoneDirs(const String& zone);
126 static void RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName);
128 static bool HasZoneConfigAuthority(const String& zoneName);
131 std::promise<std::shared_ptr<Expression> > m_Promise;
134 std::istream *m_Input;
140 static std::vector<String> m_IncludeSearchDirs;
141 static boost::mutex m_ZoneDirsMutex;
142 static std::map<String, std::vector<ZoneFragment> > m_ZoneDirs;
144 void InitializeScanner(void);
145 void DestroyScanner(void);
147 static void HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector<std::unique_ptr<Expression> >& expressions);
149 static bool IsAbsolutePath(const String& path);
155 std::ostringstream m_LexBuffer;
156 CompilerDebugInfo m_LocationBegin;
158 std::stack<bool> m_IgnoreNewlines;
159 std::stack<bool> m_Apply;
160 std::stack<bool> m_ObjectAssign;
161 std::stack<bool> m_SeenAssign;
162 std::stack<bool> m_SeenIgnore;
163 std::stack<Expression *> m_Assign;
164 std::stack<Expression *> m_Ignore;
165 std::stack<String> m_FKVar;
166 std::stack<String> m_FVVar;
167 std::stack<Expression *> m_FTerm;
168 std::stack<int> m_FlowControlInfo;
173 #endif /* CONFIGCOMPILER_H */