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