]> granicus.if.org Git - icinga2/blob - lib/config/configcompiler.hpp
Merge pull request #5803 from Icinga/feature/cxx11-std-bind
[icinga2] / lib / config / configcompiler.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #ifndef CONFIGCOMPILER_H
21 #define CONFIGCOMPILER_H
22
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"
29 #include <iostream>
30 #include <stack>
31
32 typedef union YYSTYPE YYSTYPE;
33 typedef void *yyscan_t;
34
35 namespace icinga
36 {
37
38 struct CompilerDebugInfo
39 {
40         const char *Path;
41
42         int FirstLine;
43         int FirstColumn;
44
45         int LastLine;
46         int LastColumn;
47
48         operator DebugInfo(void) const
49         {
50                 DebugInfo di;
51                 di.Path = Path;
52                 di.FirstLine = FirstLine;
53                 di.FirstColumn = FirstColumn;
54                 di.LastLine = LastLine;
55                 di.LastColumn = LastColumn;
56                 return di;
57         }
58 };
59
60 struct EItemInfo
61 {
62         bool SideEffect;
63         CompilerDebugInfo DebugInfo;
64 };
65
66 enum FlowControlType
67 {
68         FlowControlReturn = 1,
69         FlowControlContinue = 2,
70         FlowControlBreak = 4
71 };
72
73 struct ZoneFragment
74 {
75         String Tag;
76         String Path;
77 };
78
79 /**
80  * The configuration compiler can be used to compile a configuration file
81  * into a number of configuration items.
82  *
83  * @ingroup config
84  */
85 class I2_CONFIG_API ConfigCompiler
86 {
87 public:
88         explicit ConfigCompiler(const String& path, std::istream *input,
89             const String& zone = String(), const String& package = String());
90         virtual ~ConfigCompiler(void);
91
92         Expression *Compile(void);
93
94         static Expression *CompileStream(const String& path, std::istream *stream,
95             const String& zone = String(), const String& package = String());
96         static Expression *CompileFile(const String& path, const String& zone = String(),
97             const String& package = String());
98         static Expression *CompileText(const String& path, const String& text,
99             const String& zone = String(), const String& package = String());
100
101         static void AddIncludeSearchDir(const String& dir);
102
103         const char *GetPath(void) const;
104
105         void SetZone(const String& zone);
106         String GetZone(void) const;
107         
108         void SetPackage(const String& package);
109         String GetPackage(void) const;
110
111         static void CollectIncludes(std::vector<Expression *>& expressions,
112             const String& file, const String& zone, const String& package);
113
114         static Expression *HandleInclude(const String& relativeBase, const String& path, bool search,
115             const String& zone, const String& package, const DebugInfo& debuginfo = DebugInfo());
116         static Expression *HandleIncludeRecursive(const String& relativeBase, const String& path,
117             const String& pattern, const String& zone, const String& package, const DebugInfo& debuginfo = DebugInfo());
118         static Expression *HandleIncludeZones(const String& relativeBase, const String& tag,
119             const String& path, const String& pattern, const String& package, const DebugInfo& debuginfo = DebugInfo());
120
121         size_t ReadInput(char *buffer, size_t max_bytes);
122         void *GetScanner(void) const;
123
124         static std::vector<ZoneFragment> GetZoneDirs(const String& zone);
125         static void RegisterZoneDir(const String& tag, const String& ppath, const String& zoneName);
126
127         static bool HasZoneConfigAuthority(const String& zoneName);
128
129 private:
130         boost::promise<boost::shared_ptr<Expression> > m_Promise;
131
132         String m_Path;
133         std::istream *m_Input;
134         String m_Zone;
135         String m_Package;
136
137         void *m_Scanner;
138
139         static std::vector<String> m_IncludeSearchDirs;
140         static boost::mutex m_ZoneDirsMutex;
141         static std::map<String, std::vector<ZoneFragment> > m_ZoneDirs;
142
143         void InitializeScanner(void);
144         void DestroyScanner(void);
145
146         static void HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector<Expression *>& expressions);
147
148         static bool IsAbsolutePath(const String& path);
149
150 public:
151         bool m_Eof;
152         int m_OpenBraces;
153
154         std::ostringstream m_LexBuffer;
155         CompilerDebugInfo m_LocationBegin;
156
157         std::stack<bool> m_IgnoreNewlines;
158         std::stack<bool> m_Apply;
159         std::stack<bool> m_ObjectAssign;
160         std::stack<bool> m_SeenAssign;
161         std::stack<bool> m_SeenIgnore;
162         std::stack<Expression *> m_Assign;
163         std::stack<Expression *> m_Ignore;
164         std::stack<String> m_FKVar;
165         std::stack<String> m_FVVar;
166         std::stack<Expression *> m_FTerm;
167         std::stack<int> m_FlowControlInfo;
168 };
169
170 }
171
172 #endif /* CONFIGCOMPILER_H */